blob: a5e653ecb2ab12a9aede456844ccdf718795b1d5 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
25
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050026#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040027#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020028#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010039#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020040#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020041#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Juan Zhaoe10d2792012-04-25 19:09:52 +080046enum animation_type {
47 ANIMATION_NONE,
48
49 ANIMATION_ZOOM,
50 ANIMATION_FADE
51};
52
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020053enum fade_type {
54 FADE_IN,
55 FADE_OUT
56};
57
Jonas Ådahl04769742012-06-13 00:01:24 +020058struct focus_state {
59 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040060 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020061 struct weston_surface *keyboard_focus;
62 struct wl_list link;
63 struct wl_listener seat_destroy_listener;
64 struct wl_listener surface_destroy_listener;
65};
66
Jonas Ådahle3cddce2012-06-13 00:01:22 +020067struct workspace {
68 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020069
70 struct wl_list focus_list;
71 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020072};
73
Philipp Brüschweiler88013572012-08-06 13:44:42 +020074struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -050075 struct wl_resource *resource;
76 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010077
78 struct desktop_shell *shell;
79
Philipp Brüschweiler88013572012-08-06 13:44:42 +020080 struct wl_list link;
81 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -050082 struct weston_view *view;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010083 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020084
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020085 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020086 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020087};
88
Xiong Zhang6b481422013-10-23 13:58:32 +080089struct shell_output {
90 struct desktop_shell *shell;
91 struct weston_output *output;
92 struct wl_listener destroy_listener;
93 struct wl_list link;
94};
95
Tiago Vignattibe143262012-04-16 17:31:41 +030096struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050097 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040098
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020099 struct wl_listener idle_listener;
100 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400101 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200102 struct wl_listener show_input_panel_listener;
103 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200104 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200105
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500106 struct weston_layer fullscreen_layer;
107 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500108 struct weston_layer background_layer;
109 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200110 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500111
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400112 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300113 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400114
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200115 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500116 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200117 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200118 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200119
120 unsigned deathcount;
121 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200122 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200123
124 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200125 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200126 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200127
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200128 struct {
129 struct weston_surface *surface;
130 pixman_box32_t cursor_rectangle;
131 } text_input;
132
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400133 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500134 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100135
Pekka Paalanen77346a62011-11-30 16:26:35 +0200136 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200137 struct wl_array array;
138 unsigned int current;
139 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200140
Jonas Ådahle9d22502012-08-29 22:13:01 +0200141 struct wl_list client_list;
142
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200143 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200144 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200145 int anim_dir;
146 uint32_t anim_timestamp;
147 double anim_current;
148 struct workspace *anim_from;
149 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200150 } workspaces;
151
152 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200153 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200154 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200155 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200157 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200158 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500159
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200160 struct {
161 struct wl_resource *binding;
162 struct wl_list surfaces;
163 } input_panel;
164
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200165 struct {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500166 struct weston_view *view;
167 struct weston_view_animation *animation;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200168 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300169 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200170 } fade;
171
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300172 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800173 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700174 enum animation_type startup_animation_type;
Xiong Zhang6b481422013-10-23 13:58:32 +0800175
176 struct wl_listener output_create_listener;
177 struct wl_list output_list;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400178};
179
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500180enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200181 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500182 SHELL_SURFACE_TOPLEVEL,
183 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500184 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800185 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300186 SHELL_SURFACE_POPUP,
187 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200188};
189
Scott Moreauff1db4a2012-04-17 19:06:18 -0600190struct ping_timer {
191 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600192 uint32_t serial;
193};
194
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200195struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500196 struct wl_resource *resource;
197 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200198
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500199 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500200 struct weston_view *view;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200201 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400202 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300203 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200204
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400205 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400206 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500207 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800208 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800209 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700210 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100211
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500212 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200213 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500214 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200215 } rotation;
216
217 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100218 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500219 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100220 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400221 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500222 } popup;
223
Alex Wu4539b082012-03-01 12:57:46 +0800224 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300225 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400226 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300227 } transient;
228
229 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800230 enum wl_shell_surface_fullscreen_method type;
231 struct weston_transform transform; /* matrix from x, y */
232 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500233 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800234 } fullscreen;
235
Scott Moreauff1db4a2012-04-17 19:06:18 -0600236 struct ping_timer *ping_timer;
237
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200238 struct weston_transform workspace_transform;
239
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500240 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500241 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100242 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400243
244 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200245};
246
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300247struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400248 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300249 struct shell_surface *shsurf;
250 struct wl_listener shsurf_destroy_listener;
251};
252
Rusty Lynch1084da52013-08-15 09:10:08 -0700253struct shell_touch_grab {
254 struct weston_touch_grab grab;
255 struct shell_surface *shsurf;
256 struct wl_listener shsurf_destroy_listener;
257 struct weston_touch *touch;
258};
259
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300260struct weston_move_grab {
261 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100262 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500263};
264
Rusty Lynch1084da52013-08-15 09:10:08 -0700265struct weston_touch_move_grab {
266 struct shell_touch_grab base;
267 wl_fixed_t dx, dy;
268};
269
Pekka Paalanen460099f2012-01-20 16:48:25 +0200270struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300271 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500272 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200273 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200274 float x;
275 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200276 } center;
277};
278
Giulio Camuffo5085a752013-03-25 21:42:45 +0100279struct shell_seat {
280 struct weston_seat *seat;
281 struct wl_listener seat_destroy_listener;
282
283 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400284 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100285 struct wl_list surfaces_list;
286 struct wl_client *client;
287 int32_t initial_up;
288 } popup_grab;
289};
290
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400291static void
292activate(struct desktop_shell *shell, struct weston_surface *es,
293 struct weston_seat *seat);
294
295static struct workspace *
296get_current_workspace(struct desktop_shell *shell);
297
Alex Wubd3354b2012-04-17 17:20:49 +0800298static struct shell_surface *
299get_shell_surface(struct weston_surface *surface);
300
301static struct desktop_shell *
302shell_surface_get_shell(struct shell_surface *shsurf);
303
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500304static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400305surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500306
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300307static void
308shell_fade_startup(struct desktop_shell *shell);
309
Alex Wubd3354b2012-04-17 17:20:49 +0800310static bool
311shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
312{
313 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500314 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800315
316 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100317
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800319 return false;
320
Jason Ekstranda7af7042013-10-12 22:38:11 -0500321 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
322 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800323 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500324 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800325}
326
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500327static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400328destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300329{
330 struct shell_grab *grab;
331
332 grab = container_of(listener, struct shell_grab,
333 shsurf_destroy_listener);
334
335 grab->shsurf = NULL;
336}
337
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338static struct weston_view *
339get_default_view(struct weston_surface *surface)
340{
341 struct shell_surface *shsurf;
342 struct weston_view *view;
343
344 if (!surface || wl_list_empty(&surface->views))
345 return NULL;
346
347 shsurf = get_shell_surface(surface);
348 if (shsurf)
349 return shsurf->view;
350
351 wl_list_for_each(view, &surface->views, surface_link)
352 if (weston_view_is_mapped(view))
353 return view;
354
355 return container_of(surface->views.next, struct weston_view, surface_link);
356}
357
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300358static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400359popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400360
361static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300362shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400363 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300364 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400365 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300366 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300367{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300368 struct desktop_shell *shell = shsurf->shell;
369
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400370 popup_grab_end(pointer);
371
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300372 grab->grab.interface = interface;
373 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400374 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500375 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400376 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300377
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700378 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400379 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400380 if (shell->child.desktop_shell) {
381 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
382 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500383 weston_pointer_set_focus(pointer,
384 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400385 wl_fixed_from_int(0),
386 wl_fixed_from_int(0));
387 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300388}
389
390static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300391shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300392{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700393 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400394 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700395 grab->shsurf->grabbed = 0;
396 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300397
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700398 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300399}
400
401static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700402shell_touch_grab_start(struct shell_touch_grab *grab,
403 const struct weston_touch_grab_interface *interface,
404 struct shell_surface *shsurf,
405 struct weston_touch *touch)
406{
407 struct desktop_shell *shell = shsurf->shell;
408
409 grab->grab.interface = interface;
410 grab->shsurf = shsurf;
411 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
412 wl_signal_add(&shsurf->destroy_signal,
413 &grab->shsurf_destroy_listener);
414
415 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700416 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700417
418 weston_touch_start_grab(touch, &grab->grab);
419 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420 weston_touch_set_focus(touch->seat,
421 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700422}
423
424static void
425shell_touch_grab_end(struct shell_touch_grab *grab)
426{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700427 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700428 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700429 grab->shsurf->grabbed = 0;
430 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700431
432 weston_touch_end_grab(grab->touch);
433}
434
435static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500436center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800437 struct weston_output *output);
438
Daniel Stone496ca172012-05-30 16:31:42 +0100439static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300440get_modifier(char *modifier)
441{
442 if (!modifier)
443 return MODIFIER_SUPER;
444
445 if (!strcmp("ctrl", modifier))
446 return MODIFIER_CTRL;
447 else if (!strcmp("alt", modifier))
448 return MODIFIER_ALT;
449 else if (!strcmp("super", modifier))
450 return MODIFIER_SUPER;
451 else
452 return MODIFIER_SUPER;
453}
454
Juan Zhaoe10d2792012-04-25 19:09:52 +0800455static enum animation_type
456get_animation_type(char *animation)
457{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800458 if (!strcmp("zoom", animation))
459 return ANIMATION_ZOOM;
460 else if (!strcmp("fade", animation))
461 return ANIMATION_FADE;
462 else
463 return ANIMATION_NONE;
464}
465
Alex Wu4539b082012-03-01 12:57:46 +0800466static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400467shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200468{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400469 struct weston_config_section *section;
470 int duration;
471 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200472
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400473 section = weston_config_get_section(shell->compositor->config,
474 "screensaver", NULL, NULL);
475 weston_config_section_get_string(section,
476 "path", &shell->screensaver.path, NULL);
477 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200478 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400479
480 section = weston_config_get_section(shell->compositor->config,
481 "shell", NULL, NULL);
482 weston_config_section_get_string(section,
483 "binding-modifier", &s, "super");
484 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200485 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400486 weston_config_section_get_string(section, "animation", &s, "none");
487 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200488 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700489 weston_config_section_get_string(section,
490 "startup-animation", &s, "fade");
491 shell->startup_animation_type = get_animation_type(s);
492 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400493 weston_config_section_get_uint(section, "num-workspaces",
494 &shell->workspaces.num,
495 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200496}
497
498static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200499focus_state_destroy(struct focus_state *state)
500{
501 wl_list_remove(&state->seat_destroy_listener.link);
502 wl_list_remove(&state->surface_destroy_listener.link);
503 free(state);
504}
505
506static void
507focus_state_seat_destroy(struct wl_listener *listener, void *data)
508{
509 struct focus_state *state = container_of(listener,
510 struct focus_state,
511 seat_destroy_listener);
512
513 wl_list_remove(&state->link);
514 focus_state_destroy(state);
515}
516
517static void
518focus_state_surface_destroy(struct wl_listener *listener, void *data)
519{
520 struct focus_state *state = container_of(listener,
521 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400522 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400523 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500524 struct weston_surface *main_surface, *next;
525 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200526
Pekka Paalanen01388e22013-04-25 13:57:44 +0300527 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
528
Kristian Høgsberge3778222012-07-31 17:29:30 -0400529 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500530 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
531 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400532 continue;
533
Jason Ekstranda7af7042013-10-12 22:38:11 -0500534 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400535 break;
536 }
537
Pekka Paalanen01388e22013-04-25 13:57:44 +0300538 /* if the focus was a sub-surface, activate its main surface */
539 if (main_surface != state->keyboard_focus)
540 next = main_surface;
541
Kristian Høgsberge3778222012-07-31 17:29:30 -0400542 if (next) {
543 shell = state->seat->compositor->shell_interface.shell;
544 activate(shell, next, state->seat);
545 } else {
546 wl_list_remove(&state->link);
547 focus_state_destroy(state);
548 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200549}
550
551static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400552focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200553{
Jonas Ådahl04769742012-06-13 00:01:24 +0200554 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200555
556 state = malloc(sizeof *state);
557 if (state == NULL)
558 return NULL;
559
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400560 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200561 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400562 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200563
564 state->seat_destroy_listener.notify = focus_state_seat_destroy;
565 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400566 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200567 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400568 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200569
570 return state;
571}
572
Jonas Ådahl8538b222012-08-29 22:13:03 +0200573static struct focus_state *
574ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
575{
576 struct workspace *ws = get_current_workspace(shell);
577 struct focus_state *state;
578
579 wl_list_for_each(state, &ws->focus_list, link)
580 if (state->seat == seat)
581 break;
582
583 if (&state->link == &ws->focus_list)
584 state = focus_state_create(seat, ws);
585
586 return state;
587}
588
Jonas Ådahl04769742012-06-13 00:01:24 +0200589static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400590restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200591{
592 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400593 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200594
595 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400596 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200597
Kristian Høgsberge3148752013-05-06 23:19:49 -0400598 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200599 }
600}
601
602static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200603replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
604 struct weston_seat *seat)
605{
606 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400607 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200608
609 wl_list_for_each(state, &ws->focus_list, link) {
610 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400611 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500612 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200613 return;
614 }
615 }
616}
617
618static void
619drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
620 struct weston_surface *surface)
621{
622 struct focus_state *state;
623
624 wl_list_for_each(state, &ws->focus_list, link)
625 if (state->keyboard_focus == surface)
626 state->keyboard_focus = NULL;
627}
628
629static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200630workspace_destroy(struct workspace *ws)
631{
Jonas Ådahl04769742012-06-13 00:01:24 +0200632 struct focus_state *state, *next;
633
634 wl_list_for_each_safe(state, next, &ws->focus_list, link)
635 focus_state_destroy(state);
636
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200637 free(ws);
638}
639
Jonas Ådahl04769742012-06-13 00:01:24 +0200640static void
641seat_destroyed(struct wl_listener *listener, void *data)
642{
643 struct weston_seat *seat = data;
644 struct focus_state *state, *next;
645 struct workspace *ws = container_of(listener,
646 struct workspace,
647 seat_destroyed_listener);
648
649 wl_list_for_each_safe(state, next, &ws->focus_list, link)
650 if (state->seat == seat)
651 wl_list_remove(&state->link);
652}
653
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200654static struct workspace *
655workspace_create(void)
656{
657 struct workspace *ws = malloc(sizeof *ws);
658 if (ws == NULL)
659 return NULL;
660
661 weston_layer_init(&ws->layer, NULL);
662
Jonas Ådahl04769742012-06-13 00:01:24 +0200663 wl_list_init(&ws->focus_list);
664 wl_list_init(&ws->seat_destroyed_listener.link);
665 ws->seat_destroyed_listener.notify = seat_destroyed;
666
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200667 return ws;
668}
669
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200670static int
671workspace_is_empty(struct workspace *ws)
672{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500673 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200674}
675
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200676static struct workspace *
677get_workspace(struct desktop_shell *shell, unsigned int index)
678{
679 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200680 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200681 pws += index;
682 return *pws;
683}
684
685static struct workspace *
686get_current_workspace(struct desktop_shell *shell)
687{
688 return get_workspace(shell, shell->workspaces.current);
689}
690
691static void
692activate_workspace(struct desktop_shell *shell, unsigned int index)
693{
694 struct workspace *ws;
695
696 ws = get_workspace(shell, index);
697 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
698
699 shell->workspaces.current = index;
700}
701
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200702static unsigned int
703get_output_height(struct weston_output *output)
704{
705 return abs(output->region.extents.y1 - output->region.extents.y2);
706}
707
708static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500709view_translate(struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200710{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500711 struct shell_surface *shsurf = get_shell_surface(view->surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200712 struct weston_transform *transform;
713
714 transform = &shsurf->workspace_transform;
715 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500716 wl_list_insert(view->geometry.transformation_list.prev,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200717 &shsurf->workspace_transform.link);
718
719 weston_matrix_init(&shsurf->workspace_transform.matrix);
720 weston_matrix_translate(&shsurf->workspace_transform.matrix,
721 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500722 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200723}
724
725static void
726workspace_translate_out(struct workspace *ws, double fraction)
727{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500728 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200729 unsigned int height;
730 double d;
731
Jason Ekstranda7af7042013-10-12 22:38:11 -0500732 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
733 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200734 d = height * fraction;
735
Jason Ekstranda7af7042013-10-12 22:38:11 -0500736 view_translate(view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200737 }
738}
739
740static void
741workspace_translate_in(struct workspace *ws, double fraction)
742{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500743 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200744 unsigned int height;
745 double d;
746
Jason Ekstranda7af7042013-10-12 22:38:11 -0500747 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
748 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200749
750 if (fraction > 0)
751 d = -(height - height * fraction);
752 else
753 d = height + height * fraction;
754
Jason Ekstranda7af7042013-10-12 22:38:11 -0500755 view_translate(view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200756 }
757}
758
759static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200760broadcast_current_workspace_state(struct desktop_shell *shell)
761{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700762 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200763
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700764 wl_resource_for_each(resource, &shell->workspaces.client_list)
765 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200766 shell->workspaces.current,
767 shell->workspaces.num);
768}
769
770static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200771reverse_workspace_change_animation(struct desktop_shell *shell,
772 unsigned int index,
773 struct workspace *from,
774 struct workspace *to)
775{
776 shell->workspaces.current = index;
777
778 shell->workspaces.anim_to = to;
779 shell->workspaces.anim_from = from;
780 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
781 shell->workspaces.anim_timestamp = 0;
782
Scott Moreau4272e632012-08-13 09:58:41 -0600783 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200784}
785
786static void
787workspace_deactivate_transforms(struct workspace *ws)
788{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500789 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200790 struct shell_surface *shsurf;
791
Jason Ekstranda7af7042013-10-12 22:38:11 -0500792 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
793 shsurf = get_shell_surface(view->surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200794 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
795 wl_list_remove(&shsurf->workspace_transform.link);
796 wl_list_init(&shsurf->workspace_transform.link);
797 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500798 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200799 }
800}
801
802static void
803finish_workspace_change_animation(struct desktop_shell *shell,
804 struct workspace *from,
805 struct workspace *to)
806{
Scott Moreau4272e632012-08-13 09:58:41 -0600807 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200808
809 wl_list_remove(&shell->workspaces.animation.link);
810 workspace_deactivate_transforms(from);
811 workspace_deactivate_transforms(to);
812 shell->workspaces.anim_to = NULL;
813
814 wl_list_remove(&shell->workspaces.anim_from->layer.link);
815}
816
817static void
818animate_workspace_change_frame(struct weston_animation *animation,
819 struct weston_output *output, uint32_t msecs)
820{
821 struct desktop_shell *shell =
822 container_of(animation, struct desktop_shell,
823 workspaces.animation);
824 struct workspace *from = shell->workspaces.anim_from;
825 struct workspace *to = shell->workspaces.anim_to;
826 uint32_t t;
827 double x, y;
828
829 if (workspace_is_empty(from) && workspace_is_empty(to)) {
830 finish_workspace_change_animation(shell, from, to);
831 return;
832 }
833
834 if (shell->workspaces.anim_timestamp == 0) {
835 if (shell->workspaces.anim_current == 0.0)
836 shell->workspaces.anim_timestamp = msecs;
837 else
838 shell->workspaces.anim_timestamp =
839 msecs -
840 /* Invers of movement function 'y' below. */
841 (asin(1.0 - shell->workspaces.anim_current) *
842 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
843 M_2_PI);
844 }
845
846 t = msecs - shell->workspaces.anim_timestamp;
847
848 /*
849 * x = [0, π/2]
850 * y(x) = sin(x)
851 */
852 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
853 y = sin(x);
854
855 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600856 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857
858 workspace_translate_out(from, shell->workspaces.anim_dir * y);
859 workspace_translate_in(to, shell->workspaces.anim_dir * y);
860 shell->workspaces.anim_current = y;
861
Scott Moreau4272e632012-08-13 09:58:41 -0600862 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200863 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200864 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200865 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200866}
867
868static void
869animate_workspace_change(struct desktop_shell *shell,
870 unsigned int index,
871 struct workspace *from,
872 struct workspace *to)
873{
874 struct weston_output *output;
875
876 int dir;
877
878 if (index > shell->workspaces.current)
879 dir = -1;
880 else
881 dir = 1;
882
883 shell->workspaces.current = index;
884
885 shell->workspaces.anim_dir = dir;
886 shell->workspaces.anim_from = from;
887 shell->workspaces.anim_to = to;
888 shell->workspaces.anim_current = 0.0;
889 shell->workspaces.anim_timestamp = 0;
890
891 output = container_of(shell->compositor->output_list.next,
892 struct weston_output, link);
893 wl_list_insert(&output->animation_list,
894 &shell->workspaces.animation.link);
895
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200896 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200897
898 workspace_translate_in(to, 0);
899
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400900 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200901
Scott Moreau4272e632012-08-13 09:58:41 -0600902 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200903}
904
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200906update_workspace(struct desktop_shell *shell, unsigned int index,
907 struct workspace *from, struct workspace *to)
908{
909 shell->workspaces.current = index;
910 wl_list_insert(&from->layer.link, &to->layer.link);
911 wl_list_remove(&from->layer.link);
912}
913
914static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200915change_workspace(struct desktop_shell *shell, unsigned int index)
916{
917 struct workspace *from;
918 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200919
920 if (index == shell->workspaces.current)
921 return;
922
923 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500924 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200925 return;
926
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200927 from = get_current_workspace(shell);
928 to = get_workspace(shell, index);
929
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200930 if (shell->workspaces.anim_from == to &&
931 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200933 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200934 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200935 return;
936 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200937
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200938 if (shell->workspaces.anim_to != NULL)
939 finish_workspace_change_animation(shell,
940 shell->workspaces.anim_from,
941 shell->workspaces.anim_to);
942
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200943 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200944
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200945 if (workspace_is_empty(to) && workspace_is_empty(from))
946 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200947 else
948 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200949
950 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200951}
952
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200953static bool
954workspace_has_only(struct workspace *ws, struct weston_surface *surface)
955{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200957 struct wl_list *e;
958
959 if (wl_list_empty(list))
960 return false;
961
962 e = list->next;
963
964 if (e->next != list)
965 return false;
966
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200968}
969
970static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971move_view_to_workspace(struct desktop_shell *shell,
972 struct weston_view *view,
973 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +0200974{
975 struct workspace *from;
976 struct workspace *to;
977 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300978 struct weston_surface *focus;
979
Jason Ekstranda7af7042013-10-12 22:38:11 -0500980 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200981
982 if (workspace == shell->workspaces.current)
983 return;
984
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200985 if (workspace >= shell->workspaces.num)
986 workspace = shell->workspaces.num - 1;
987
Jonas Ådahle9d22502012-08-29 22:13:01 +0200988 from = get_current_workspace(shell);
989 to = get_workspace(shell, workspace);
990
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 wl_list_remove(&view->layer_link);
992 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200993
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300995 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
996 if (!seat->keyboard)
997 continue;
998
999 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001001 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001002 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001003
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001005}
1006
1007static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001008take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001009 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001010 unsigned int index)
1011{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001012 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001014 struct shell_surface *shsurf;
1015 struct workspace *from;
1016 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001017 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001018
Pekka Paalanen01388e22013-04-25 13:57:44 +03001019 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020 view = get_default_view(surface);
1021 if (view == NULL ||
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001022 index == shell->workspaces.current)
1023 return;
1024
1025 from = get_current_workspace(shell);
1026 to = get_workspace(shell, index);
1027
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028 wl_list_remove(&view->layer_link);
1029 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001030
Jonas Ådahle9d22502012-08-29 22:13:01 +02001031 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001032 drop_focus_state(shell, from, surface);
1033
1034 if (shell->workspaces.anim_from == to &&
1035 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001036 wl_list_remove(&to->layer.link);
1037 wl_list_insert(from->layer.link.prev, &to->layer.link);
1038
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001039 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001040 broadcast_current_workspace_state(shell);
1041
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001042 return;
1043 }
1044
1045 if (shell->workspaces.anim_to != NULL)
1046 finish_workspace_change_animation(shell,
1047 shell->workspaces.anim_from,
1048 shell->workspaces.anim_to);
1049
1050 if (workspace_is_empty(from) &&
1051 workspace_has_only(to, surface))
1052 update_workspace(shell, index, from, to);
1053 else {
1054 shsurf = get_shell_surface(surface);
1055 if (wl_list_empty(&shsurf->workspace_transform.link))
1056 wl_list_insert(&shell->workspaces.anim_sticky_list,
1057 &shsurf->workspace_transform.link);
1058
1059 animate_workspace_change(shell, index, from, to);
1060 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001061
1062 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001063
1064 state = ensure_focus_state(shell, seat);
1065 if (state != NULL)
1066 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001067}
1068
1069static void
1070workspace_manager_move_surface(struct wl_client *client,
1071 struct wl_resource *resource,
1072 struct wl_resource *surface_resource,
1073 uint32_t workspace)
1074{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001075 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001076 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001077 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001078 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001080
Pekka Paalanen01388e22013-04-25 13:57:44 +03001081 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 view = get_default_view(main_surface);
1083 if (!view)
1084 return;
1085 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001086}
1087
1088static const struct workspace_manager_interface workspace_manager_implementation = {
1089 workspace_manager_move_surface,
1090};
1091
1092static void
1093unbind_resource(struct wl_resource *resource)
1094{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001095 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001096}
1097
1098static void
1099bind_workspace_manager(struct wl_client *client,
1100 void *data, uint32_t version, uint32_t id)
1101{
1102 struct desktop_shell *shell = data;
1103 struct wl_resource *resource;
1104
Jason Ekstranda85118c2013-06-27 20:17:02 -05001105 resource = wl_resource_create(client,
1106 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001107
1108 if (resource == NULL) {
1109 weston_log("couldn't add workspace manager object");
1110 return;
1111 }
1112
Jason Ekstranda85118c2013-06-27 20:17:02 -05001113 wl_resource_set_implementation(resource,
1114 &workspace_manager_implementation,
1115 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001116 wl_list_insert(&shell->workspaces.client_list,
1117 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001118
1119 workspace_manager_send_state(resource,
1120 shell->workspaces.current,
1121 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001122}
1123
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001124static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001125touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1126 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1127{
1128}
1129
1130static void
1131touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1132{
1133 struct shell_touch_grab *shell_grab = container_of(grab,
1134 struct shell_touch_grab,
1135 grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001136
1137 if (grab->touch->seat->num_tp == 0)
1138 shell_touch_grab_end(shell_grab);
Rusty Lynch1084da52013-08-15 09:10:08 -07001139}
1140
1141static void
1142touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1143 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1144{
1145 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1146 struct shell_surface *shsurf = move->base.shsurf;
1147 struct weston_surface *es;
1148 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1149 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1150
1151 if (!shsurf)
1152 return;
1153
1154 es = shsurf->surface;
1155
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 weston_view_configure(shsurf->view, dx, dy,
1157 shsurf->view->geometry.width,
1158 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001159
1160 weston_compositor_schedule_repaint(es->compositor);
1161}
1162
1163static const struct weston_touch_grab_interface touch_move_grab_interface = {
1164 touch_move_grab_down,
1165 touch_move_grab_up,
1166 touch_move_grab_motion,
1167};
1168
1169static int
1170surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1171{
1172 struct weston_touch_move_grab *move;
1173
1174 if (!shsurf)
1175 return -1;
1176
1177 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1178 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001179 if (shsurf->grabbed)
1180 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001181
1182 move = malloc(sizeof *move);
1183 if (!move)
1184 return -1;
1185
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001187 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001189 seat->touch->grab_y;
1190
1191 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1192 seat->touch);
1193
1194 return 0;
1195}
1196
1197static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001198noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001199{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001200}
1201
1202static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001203move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001204{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001205 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001206 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001207 struct shell_surface *shsurf = move->base.shsurf;
Daniel Stone37816df2012-05-16 18:45:18 +01001208 int dx = wl_fixed_to_int(pointer->x + move->dx);
1209 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001210
1211 if (!shsurf)
1212 return;
1213
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 weston_view_configure(shsurf->view, dx, dy,
1215 shsurf->view->geometry.width,
1216 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001217
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001219}
1220
1221static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001222move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001223 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001224{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001225 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1226 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001227 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001228 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001229
Daniel Stone4dbadb12012-05-30 16:31:51 +01001230 if (pointer->button_count == 0 &&
1231 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001232 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001233 free(grab);
1234 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001235}
1236
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001237static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001238 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001239 move_grab_motion,
1240 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001241};
1242
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001243static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001244surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001245{
1246 struct weston_move_grab *move;
1247
1248 if (!shsurf)
1249 return -1;
1250
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001251 if (shsurf->grabbed)
1252 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001253 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1254 return 0;
1255
1256 move = malloc(sizeof *move);
1257 if (!move)
1258 return -1;
1259
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001261 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001263 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001264
1265 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001266 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001267
1268 return 0;
1269}
1270
1271static void
1272shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1273 struct wl_resource *seat_resource, uint32_t serial)
1274{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001275 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001276 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001277 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001278
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001279 if (seat->pointer &&
1280 seat->pointer->button_count > 0 &&
1281 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001283 if ((surface == shsurf->surface) &&
1284 (surface_move(shsurf, seat) < 0))
1285 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001286 } else if (seat->touch &&
1287 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001289 if ((surface == shsurf->surface) &&
1290 (surface_touch_move(shsurf, seat) < 0))
1291 wl_resource_post_no_memory(resource);
1292 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001293}
1294
1295struct weston_resize_grab {
1296 struct shell_grab base;
1297 uint32_t edges;
1298 int32_t width, height;
1299};
1300
1301static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001302resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001303{
1304 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001305 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001306 struct shell_surface *shsurf = resize->base.shsurf;
1307 int32_t width, height;
1308 wl_fixed_t from_x, from_y;
1309 wl_fixed_t to_x, to_y;
1310
1311 if (!shsurf)
1312 return;
1313
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 weston_view_from_global_fixed(shsurf->view,
1315 pointer->grab_x, pointer->grab_y,
1316 &from_x, &from_y);
1317 weston_view_from_global_fixed(shsurf->view,
1318 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001319
1320 width = resize->width;
1321 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1322 width += wl_fixed_to_int(from_x - to_x);
1323 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1324 width += wl_fixed_to_int(to_x - from_x);
1325 }
1326
1327 height = resize->height;
1328 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1329 height += wl_fixed_to_int(from_y - to_y);
1330 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1331 height += wl_fixed_to_int(to_y - from_y);
1332 }
1333
1334 shsurf->client->send_configure(shsurf->surface,
1335 resize->edges, width, height);
1336}
1337
1338static void
1339send_configure(struct weston_surface *surface,
1340 uint32_t edges, int32_t width, int32_t height)
1341{
1342 struct shell_surface *shsurf = get_shell_surface(surface);
1343
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001344 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001345 edges, width, height);
1346}
1347
1348static const struct weston_shell_client shell_client = {
1349 send_configure
1350};
1351
1352static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001353resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001354 uint32_t time, uint32_t button, uint32_t state_w)
1355{
1356 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001357 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001358 enum wl_pointer_button_state state = state_w;
1359
1360 if (pointer->button_count == 0 &&
1361 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1362 shell_grab_end(&resize->base);
1363 free(grab);
1364 }
1365}
1366
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001367static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001368 noop_grab_focus,
1369 resize_grab_motion,
1370 resize_grab_button,
1371};
1372
Giulio Camuffob8366642013-04-25 13:57:46 +03001373/*
1374 * Returns the bounding box of a surface and all its sub-surfaces,
1375 * in the surface coordinates system. */
1376static void
1377surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1378 int32_t *y, int32_t *w, int32_t *h) {
1379 pixman_region32_t region;
1380 pixman_box32_t *box;
1381 struct weston_subsurface *subsurface;
1382
1383 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 surface->width,
1385 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001386
1387 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1388 pixman_region32_union_rect(&region, &region,
1389 subsurface->position.x,
1390 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 subsurface->surface->width,
1392 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001393 }
1394
1395 box = pixman_region32_extents(&region);
1396 if (x)
1397 *x = box->x1;
1398 if (y)
1399 *y = box->y1;
1400 if (w)
1401 *w = box->x2 - box->x1;
1402 if (h)
1403 *h = box->y2 - box->y1;
1404
1405 pixman_region32_fini(&region);
1406}
1407
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001408static int
1409surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001410 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001411{
1412 struct weston_resize_grab *resize;
1413
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001414 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1415 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001416 return 0;
1417
1418 if (edges == 0 || edges > 15 ||
1419 (edges & 3) == 3 || (edges & 12) == 12)
1420 return 0;
1421
1422 resize = malloc(sizeof *resize);
1423 if (!resize)
1424 return -1;
1425
1426 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001427 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1428 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001429
1430 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001431 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001432
1433 return 0;
1434}
1435
1436static void
1437shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1438 struct wl_resource *seat_resource, uint32_t serial,
1439 uint32_t edges)
1440{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001441 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001442 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001443 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001444
1445 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1446 return;
1447
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001449 if (seat->pointer->button_count == 0 ||
1450 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001451 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001452 return;
1453
Kristian Høgsberge3148752013-05-06 23:19:49 -04001454 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001455 wl_resource_post_no_memory(resource);
1456}
1457
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001458static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001459end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1460
1461static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001462busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001463{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001464 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001465 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001467 wl_fixed_t sx, sy;
1468
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 view = weston_compositor_pick_view(pointer->seat->compositor,
1470 pointer->x, pointer->y,
1471 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001472
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001474 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001475}
1476
1477static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001478busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001479{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001480}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001481
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001482static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001483busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001484 uint32_t time, uint32_t button, uint32_t state)
1485{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001486 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001487 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001488 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001489
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001490 if (shsurf && button == BTN_LEFT && state) {
1491 activate(shsurf->shell, shsurf->surface, seat);
1492 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001493 } else if (shsurf && button == BTN_RIGHT && state) {
1494 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001495 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001496 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001497}
1498
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001499static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001500 busy_cursor_grab_focus,
1501 busy_cursor_grab_motion,
1502 busy_cursor_grab_button,
1503};
1504
1505static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001506set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001507{
1508 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001509
1510 grab = malloc(sizeof *grab);
1511 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001512 return;
1513
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001514 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1515 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001516}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001517
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001518static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001519end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001520{
1521 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1522
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001523 if (grab->grab.interface == &busy_cursor_grab_interface &&
1524 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001525 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001526 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001527 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001528}
1529
Scott Moreau9521d5e2012-04-19 13:06:17 -06001530static void
1531ping_timer_destroy(struct shell_surface *shsurf)
1532{
1533 if (!shsurf || !shsurf->ping_timer)
1534 return;
1535
1536 if (shsurf->ping_timer->source)
1537 wl_event_source_remove(shsurf->ping_timer->source);
1538
1539 free(shsurf->ping_timer);
1540 shsurf->ping_timer = NULL;
1541}
1542
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001543static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001544ping_timeout_handler(void *data)
1545{
1546 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001547 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001548
Scott Moreau9521d5e2012-04-19 13:06:17 -06001549 /* Client is not responding */
1550 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001551
1552 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 if (seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001554 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001555
1556 return 1;
1557}
1558
1559static void
1560ping_handler(struct weston_surface *surface, uint32_t serial)
1561{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001562 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001563 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001564 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001565
1566 if (!shsurf)
1567 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001568 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001569 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001570
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001571 if (shsurf->surface == shsurf->shell->grab_surface)
1572 return;
1573
Scott Moreauff1db4a2012-04-17 19:06:18 -06001574 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001575 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001576 if (!shsurf->ping_timer)
1577 return;
1578
1579 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001580 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1581 shsurf->ping_timer->source =
1582 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1583 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1584
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001585 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001586 }
1587}
1588
1589static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001590handle_pointer_focus(struct wl_listener *listener, void *data)
1591{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001592 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001594 struct weston_compositor *compositor;
1595 struct shell_surface *shsurf;
1596 uint32_t serial;
1597
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001599 return;
1600
Jason Ekstranda7af7042013-10-12 22:38:11 -05001601 compositor = view->surface->compositor;
1602 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001603
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001604 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001605 set_busy_cursor(shsurf, pointer);
1606 } else {
1607 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001608 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001609 }
1610}
1611
1612static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001613create_pointer_focus_listener(struct weston_seat *seat)
1614{
1615 struct wl_listener *listener;
1616
Kristian Høgsberge3148752013-05-06 23:19:49 -04001617 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001618 return;
1619
1620 listener = malloc(sizeof *listener);
1621 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001622 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001623}
1624
1625static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001626shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1627 uint32_t serial)
1628{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001629 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001630 struct weston_seat *seat;
1631 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001632
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001633 if (shsurf->ping_timer == NULL)
1634 /* Just ignore unsolicited pong. */
1635 return;
1636
Scott Moreauff1db4a2012-04-17 19:06:18 -06001637 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001638 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001639 wl_list_for_each(seat, &ec->seat_list, link) {
1640 if(seat->pointer)
1641 end_busy_cursor(shsurf, seat->pointer);
1642 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001643 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001644 }
1645}
1646
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001647static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001648set_title(struct shell_surface *shsurf, const char *title)
1649{
1650 free(shsurf->title);
1651 shsurf->title = strdup(title);
1652}
1653
1654static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001655shell_surface_set_title(struct wl_client *client,
1656 struct wl_resource *resource, const char *title)
1657{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001658 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001659
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001660 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001661}
1662
1663static void
1664shell_surface_set_class(struct wl_client *client,
1665 struct wl_resource *resource, const char *class)
1666{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001667 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001668
1669 free(shsurf->class);
1670 shsurf->class = strdup(class);
1671}
1672
Juan Zhao96879df2012-02-07 08:45:41 +08001673static struct weston_output *
1674get_default_output(struct weston_compositor *compositor)
1675{
1676 return container_of(compositor->output_list.next,
1677 struct weston_output, link);
1678}
1679
Alex Wu4539b082012-03-01 12:57:46 +08001680static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001681restore_output_mode(struct weston_output *output)
1682{
Hardening57388e42013-09-18 23:56:36 +02001683 if (output->current_mode != output->original_mode ||
1684 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001685 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001686 output->original_mode,
1687 output->original_scale,
1688 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001689}
1690
1691static void
1692restore_all_output_modes(struct weston_compositor *compositor)
1693{
1694 struct weston_output *output;
1695
1696 wl_list_for_each(output, &compositor->output_list, link)
1697 restore_output_mode(output);
1698}
1699
1700static void
Alex Wu4539b082012-03-01 12:57:46 +08001701shell_unset_fullscreen(struct shell_surface *shsurf)
1702{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001703 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001704 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001705 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1706 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001707 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001708 }
Alex Wu4539b082012-03-01 12:57:46 +08001709 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1710 shsurf->fullscreen.framerate = 0;
1711 wl_list_remove(&shsurf->fullscreen.transform.link);
1712 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001713 if (shsurf->fullscreen.black_view)
1714 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1715 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001716 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001717 weston_view_set_position(shsurf->view,
1718 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001719 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001720 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001721 &shsurf->rotation.transform.link);
1722 shsurf->saved_rotation_valid = false;
1723 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001724
1725 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001726 wl_list_remove(&shsurf->view->layer_link);
1727 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001728}
1729
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001730static void
1731shell_unset_maximized(struct shell_surface *shsurf)
1732{
1733 struct workspace *ws;
1734 /* undo all maximized things here */
1735 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 weston_view_set_position(shsurf->view,
1737 shsurf->saved_x,
1738 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001739
1740 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 wl_list_insert(&shsurf->view->geometry.transformation_list,
1742 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001743 shsurf->saved_rotation_valid = false;
1744 }
1745
1746 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 wl_list_remove(&shsurf->view->layer_link);
1748 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001749}
1750
Pekka Paalanen98262232011-12-01 10:42:22 +02001751static int
1752reset_shell_surface_type(struct shell_surface *surface)
1753{
1754 switch (surface->type) {
1755 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001756 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001757 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001758 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001759 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001760 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001761 case SHELL_SURFACE_NONE:
1762 case SHELL_SURFACE_TOPLEVEL:
1763 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001764 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001765 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001766 break;
1767 }
1768
1769 surface->type = SHELL_SURFACE_NONE;
1770 return 0;
1771}
1772
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001773static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001774set_surface_type(struct shell_surface *shsurf)
1775{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001776 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001778
1779 reset_shell_surface_type(shsurf);
1780
1781 shsurf->type = shsurf->next_type;
1782 shsurf->next_type = SHELL_SURFACE_NONE;
1783
1784 switch (shsurf->type) {
1785 case SHELL_SURFACE_TOPLEVEL:
1786 break;
1787 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001788 if (pev)
1789 weston_view_set_position(shsurf->view,
1790 pev->geometry.x + shsurf->transient.x,
1791 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001792 break;
1793
1794 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001795 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796 shsurf->saved_x = shsurf->view->geometry.x;
1797 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001798 shsurf->saved_position_valid = true;
1799
1800 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1801 wl_list_remove(&shsurf->rotation.transform.link);
1802 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001803 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001804 shsurf->saved_rotation_valid = true;
1805 }
1806 break;
1807
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001808 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809 weston_view_set_position(shsurf->view, shsurf->transient.x,
1810 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001811 break;
1812
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001813 default:
1814 break;
1815 }
1816}
1817
1818static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001819set_toplevel(struct shell_surface *shsurf)
1820{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001821 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001822}
1823
1824static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001825shell_surface_set_toplevel(struct wl_client *client,
1826 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001827{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001828 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001829
Tiago Vignattibc052c92012-04-19 16:18:18 +03001830 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001831}
1832
1833static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001834set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001835 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001836{
1837 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001838 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001839 shsurf->transient.x = x;
1840 shsurf->transient.y = y;
1841 shsurf->transient.flags = flags;
1842 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1843}
1844
1845static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001846shell_surface_set_transient(struct wl_client *client,
1847 struct wl_resource *resource,
1848 struct wl_resource *parent_resource,
1849 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001850{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001851 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001852 struct weston_surface *parent =
1853 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001854
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001855 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001856}
1857
Tiago Vignattibe143262012-04-16 17:31:41 +03001858static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001859shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001860{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001861 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001862}
1863
1864static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001865get_output_panel_height(struct desktop_shell *shell,
1866 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001867{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001868 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08001869 int panel_height = 0;
1870
1871 if (!output)
1872 return 0;
1873
Jason Ekstranda7af7042013-10-12 22:38:11 -05001874 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1875 if (view->surface->output == output) {
1876 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001877 break;
1878 }
1879 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001880
Juan Zhao96879df2012-02-07 08:45:41 +08001881 return panel_height;
1882}
1883
1884static void
1885shell_surface_set_maximized(struct wl_client *client,
1886 struct wl_resource *resource,
1887 struct wl_resource *output_resource )
1888{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001889 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001890 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001891 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001892 uint32_t edges = 0, panel_height = 0;
1893
1894 /* get the default output, if the client set it as NULL
1895 check whether the ouput is available */
1896 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001897 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001898 else if (es->output)
1899 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001900 else
1901 shsurf->output = get_default_output(es->compositor);
1902
Tiago Vignattibe143262012-04-16 17:31:41 +03001903 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001904 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001905 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001906
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001907 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001908 shsurf->output->width,
1909 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001910
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001911 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001912}
1913
Alex Wu21858432012-04-01 20:13:08 +08001914static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001915black_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 +08001916
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08001918create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001919 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001920 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001921{
1922 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001923 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08001924
1925 surface = weston_surface_create(ec);
1926 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001927 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001928 return NULL;
1929 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001930 view = weston_view_create(surface);
1931 if (surface == NULL) {
1932 weston_log("no memory\n");
1933 weston_surface_destroy(surface);
1934 return NULL;
1935 }
Alex Wu4539b082012-03-01 12:57:46 +08001936
Alex Wu21858432012-04-01 20:13:08 +08001937 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001938 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001939 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001940 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001941 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001942 pixman_region32_fini(&surface->input);
1943 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001944
Jason Ekstranda7af7042013-10-12 22:38:11 -05001945 weston_view_configure(view, x, y, w, h);
1946
1947 return view;
Alex Wu4539b082012-03-01 12:57:46 +08001948}
1949
1950/* Create black surface and append it to the associated fullscreen surface.
1951 * Handle size dismatch and positioning according to the method. */
1952static void
1953shell_configure_fullscreen(struct shell_surface *shsurf)
1954{
1955 struct weston_output *output = shsurf->fullscreen_output;
1956 struct weston_surface *surface = shsurf->surface;
1957 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001958 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001959 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001960
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001961 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1962 restore_output_mode(output);
1963
Jason Ekstranda7af7042013-10-12 22:38:11 -05001964 if (!shsurf->fullscreen.black_view)
1965 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001966 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001967 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001968 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001969 output->width,
1970 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001971
Jason Ekstranda7af7042013-10-12 22:38:11 -05001972 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
1973 wl_list_insert(&shsurf->view->layer_link,
1974 &shsurf->fullscreen.black_view->layer_link);
1975 shsurf->fullscreen.black_view->surface->output = output;
1976 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08001977
Jason Ekstranda7af7042013-10-12 22:38:11 -05001978 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03001979 &surf_width, &surf_height);
1980
Alex Wu4539b082012-03-01 12:57:46 +08001981 switch (shsurf->fullscreen.type) {
1982 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001983 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001984 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001985 break;
1986 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001987 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001988 if (output->width == surf_width &&
1989 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001990 weston_view_set_position(shsurf->view,
1991 output->x - surf_x,
1992 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001993 break;
1994 }
1995
Alex Wu4539b082012-03-01 12:57:46 +08001996 matrix = &shsurf->fullscreen.transform.matrix;
1997 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001998
Scott Moreau1bad5db2012-08-18 01:04:05 -06001999 output_aspect = (float) output->width /
2000 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002001 /* XXX: Use surf_width and surf_height here? */
2002 surface_aspect = (float) surface->width /
2003 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002004 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002005 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002006 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002007 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002008 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002009 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002010
Alex Wu4539b082012-03-01 12:57:46 +08002011 weston_matrix_scale(matrix, scale, scale, 1);
2012 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002014 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002015 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2016 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002017 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002018
Alex Wu4539b082012-03-01 12:57:46 +08002019 break;
2020 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002021 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002022 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02002023 surf_width * surface->buffer_scale,
2024 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002025 shsurf->fullscreen.framerate};
2026
Hardening57388e42013-09-18 23:56:36 +02002027 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
2028 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002029 weston_view_set_position(shsurf->view,
2030 output->x - surf_x,
2031 output->y - surf_y);
2032 weston_view_configure(shsurf->fullscreen.black_view,
2033 output->x - surf_x,
2034 output->y - surf_y,
2035 output->width,
2036 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002037 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002038 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002039 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002040 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002041 }
Alex Wubd3354b2012-04-17 17:20:49 +08002042 }
Alex Wu4539b082012-03-01 12:57:46 +08002043 break;
2044 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002046 break;
2047 default:
2048 break;
2049 }
2050}
2051
2052/* make the fullscreen and black surface at the top */
2053static void
2054shell_stack_fullscreen(struct shell_surface *shsurf)
2055{
Alex Wubd3354b2012-04-17 17:20:49 +08002056 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002057 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002058
Jason Ekstranda7af7042013-10-12 22:38:11 -05002059 wl_list_remove(&shsurf->view->layer_link);
2060 wl_list_insert(&shell->fullscreen_layer.view_list,
2061 &shsurf->view->layer_link);
2062 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002063
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064 if (!shsurf->fullscreen.black_view)
2065 shsurf->fullscreen.black_view =
2066 create_black_surface(shsurf->surface->compositor,
2067 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002068 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002069 output->width,
2070 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002071
Jason Ekstranda7af7042013-10-12 22:38:11 -05002072 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2073 wl_list_insert(&shsurf->view->layer_link,
2074 &shsurf->fullscreen.black_view->layer_link);
2075 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002076}
2077
2078static void
2079shell_map_fullscreen(struct shell_surface *shsurf)
2080{
Alex Wu4539b082012-03-01 12:57:46 +08002081 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002082 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002083}
2084
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002085static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002086set_fullscreen(struct shell_surface *shsurf,
2087 uint32_t method,
2088 uint32_t framerate,
2089 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002090{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002091 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002092
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002093 if (output)
2094 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002095 else if (es->output)
2096 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002097 else
2098 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002099
Alex Wu4539b082012-03-01 12:57:46 +08002100 shsurf->fullscreen_output = shsurf->output;
2101 shsurf->fullscreen.type = method;
2102 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002103 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002104
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002105 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002106 shsurf->output->width,
2107 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002108}
2109
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002110static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002111shell_surface_set_fullscreen(struct wl_client *client,
2112 struct wl_resource *resource,
2113 uint32_t method,
2114 uint32_t framerate,
2115 struct wl_resource *output_resource)
2116{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002117 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002118 struct weston_output *output;
2119
2120 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002121 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002122 else
2123 output = NULL;
2124
2125 set_fullscreen(shsurf, method, framerate, output);
2126}
2127
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002128static void
2129set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2130{
2131 /* XXX: using the same fields for transient type */
2132 shsurf->transient.x = x;
2133 shsurf->transient.y = y;
2134 shsurf->transient.flags = flags;
2135 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2136}
2137
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002138static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002139
2140static void
2141destroy_shell_seat(struct wl_listener *listener, void *data)
2142{
2143 struct shell_seat *shseat =
2144 container_of(listener,
2145 struct shell_seat, seat_destroy_listener);
2146 struct shell_surface *shsurf, *prev = NULL;
2147
2148 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002149 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002150 shseat->popup_grab.client = NULL;
2151
2152 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2153 shsurf->popup.shseat = NULL;
2154 if (prev) {
2155 wl_list_init(&prev->popup.grab_link);
2156 }
2157 prev = shsurf;
2158 }
2159 wl_list_init(&prev->popup.grab_link);
2160 }
2161
2162 wl_list_remove(&shseat->seat_destroy_listener.link);
2163 free(shseat);
2164}
2165
2166static struct shell_seat *
2167create_shell_seat(struct weston_seat *seat)
2168{
2169 struct shell_seat *shseat;
2170
2171 shseat = calloc(1, sizeof *shseat);
2172 if (!shseat) {
2173 weston_log("no memory to allocate shell seat\n");
2174 return NULL;
2175 }
2176
2177 shseat->seat = seat;
2178 wl_list_init(&shseat->popup_grab.surfaces_list);
2179
2180 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2181 wl_signal_add(&seat->destroy_signal,
2182 &shseat->seat_destroy_listener);
2183
2184 return shseat;
2185}
2186
2187static struct shell_seat *
2188get_shell_seat(struct weston_seat *seat)
2189{
2190 struct wl_listener *listener;
2191
2192 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2193 if (listener == NULL)
2194 return create_shell_seat(seat);
2195
2196 return container_of(listener,
2197 struct shell_seat, seat_destroy_listener);
2198}
2199
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002200static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002201popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002202{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002203 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002204 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002205 struct shell_seat *shseat =
2206 container_of(grab, struct shell_seat, popup_grab.grab);
2207 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002208 wl_fixed_t sx, sy;
2209
Jason Ekstranda7af7042013-10-12 22:38:11 -05002210 view = weston_compositor_pick_view(pointer->seat->compositor,
2211 pointer->x, pointer->y,
2212 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002213
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214 if (view && view->surface->resource &&
2215 wl_resource_get_client(view->surface->resource) == client) {
2216 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002217 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002218 weston_pointer_set_focus(pointer, NULL,
2219 wl_fixed_from_int(0),
2220 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002221 }
2222}
2223
2224static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002225popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002226{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002227 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002228 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002229 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002230
Neil Roberts96d790e2013-09-19 17:32:00 +01002231 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232 weston_view_from_global_fixed(pointer->focus,
2233 pointer->x, pointer->y,
2234 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002235 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002236 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002237}
2238
2239static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002240popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002241 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002242{
2243 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002244 struct shell_seat *shseat =
2245 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002246 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002247 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002248 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002249 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002250
Neil Roberts96d790e2013-09-19 17:32:00 +01002251 resource_list = &grab->pointer->focus_resource_list;
2252 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002253 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002254 wl_resource_for_each(resource, resource_list) {
2255 wl_pointer_send_button(resource, serial,
2256 time, button, state);
2257 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002258 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002259 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002260 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002261 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002262 }
2263
Daniel Stone4dbadb12012-05-30 16:31:51 +01002264 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002265 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002266}
2267
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002268static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002269 popup_grab_focus,
2270 popup_grab_motion,
2271 popup_grab_button,
2272};
2273
2274static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002275popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002276{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002277 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002278 struct shell_seat *shseat =
2279 container_of(grab, struct shell_seat, popup_grab.grab);
2280 struct shell_surface *shsurf;
2281 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002282
2283 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002284 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002285 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002286 shseat->popup_grab.grab.interface = NULL;
2287 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002288 /* Send the popup_done event to all the popups open */
2289 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002290 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002291 shsurf->popup.shseat = NULL;
2292 if (prev) {
2293 wl_list_init(&prev->popup.grab_link);
2294 }
2295 prev = shsurf;
2296 }
2297 wl_list_init(&prev->popup.grab_link);
2298 wl_list_init(&shseat->popup_grab.surfaces_list);
2299 }
2300}
2301
2302static void
2303add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2304{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002305 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002306
2307 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002308 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002309 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002310 /* We must make sure here that this popup was opened after
2311 * a mouse press, and not just by moving around with other
2312 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002313 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002314 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002315
Rob Bradforddfe31052013-06-26 19:49:11 +01002316 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002317 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002318 } else {
2319 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002320 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002321}
2322
2323static void
2324remove_popup_grab(struct shell_surface *shsurf)
2325{
2326 struct shell_seat *shseat = shsurf->popup.shseat;
2327
2328 wl_list_remove(&shsurf->popup.grab_link);
2329 wl_list_init(&shsurf->popup.grab_link);
2330 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002331 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002332 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002333 }
2334}
2335
2336static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002337shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002338{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002339 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002340 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002341
Jason Ekstranda7af7042013-10-12 22:38:11 -05002342 shsurf->surface->output = parent_view->output;
2343 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002344
Jason Ekstranda7af7042013-10-12 22:38:11 -05002345 weston_view_set_transform_parent(shsurf->view, parent_view);
2346 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2347 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002348
Kristian Høgsberge3148752013-05-06 23:19:49 -04002349 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002350 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002351 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002352 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002353 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002354 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002355}
2356
2357static void
2358shell_surface_set_popup(struct wl_client *client,
2359 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002360 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002361 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002362 struct wl_resource *parent_resource,
2363 int32_t x, int32_t y, uint32_t flags)
2364{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002365 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002366
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002367 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002368 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002369 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002370 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002371 shsurf->popup.x = x;
2372 shsurf->popup.y = y;
2373}
2374
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002375static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002376 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002377 shell_surface_move,
2378 shell_surface_resize,
2379 shell_surface_set_toplevel,
2380 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002381 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002382 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002383 shell_surface_set_maximized,
2384 shell_surface_set_title,
2385 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002386};
2387
2388static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002389destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002390{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002391 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2392
Giulio Camuffo5085a752013-03-25 21:42:45 +01002393 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2394 remove_popup_grab(shsurf);
2395 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002396
Alex Wubd3354b2012-04-17 17:20:49 +08002397 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002398 shell_surface_is_top_fullscreen(shsurf))
2399 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002400
Jason Ekstranda7af7042013-10-12 22:38:11 -05002401 if (shsurf->fullscreen.black_view)
2402 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002403
Alex Wubd3354b2012-04-17 17:20:49 +08002404 /* As destroy_resource() use wl_list_for_each_safe(),
2405 * we can always remove the listener.
2406 */
2407 wl_list_remove(&shsurf->surface_destroy_listener.link);
2408 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002409 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002410 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002411
Jason Ekstranda7af7042013-10-12 22:38:11 -05002412 weston_view_destroy(shsurf->view);
2413
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002414 wl_list_remove(&shsurf->link);
2415 free(shsurf);
2416}
2417
2418static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002419shell_destroy_shell_surface(struct wl_resource *resource)
2420{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002421 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002422
2423 destroy_shell_surface(shsurf);
2424}
2425
2426static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002427shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002428{
2429 struct shell_surface *shsurf = container_of(listener,
2430 struct shell_surface,
2431 surface_destroy_listener);
2432
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002433 if (shsurf->resource)
2434 wl_resource_destroy(shsurf->resource);
2435 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002436 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002437}
2438
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002439static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002440shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002441
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002442static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002443get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002444{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002445 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002446 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002447 else
2448 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002449}
2450
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002451static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002452create_shell_surface(void *shell, struct weston_surface *surface,
2453 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002454{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002455 struct shell_surface *shsurf;
2456
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002457 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002458 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002459 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002460 }
2461
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002462 shsurf = calloc(1, sizeof *shsurf);
2463 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002464 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002465 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002466 }
2467
Jason Ekstranda7af7042013-10-12 22:38:11 -05002468 shsurf->view = weston_view_create(surface);
2469 if (!shsurf->view) {
2470 weston_log("no memory to allocate shell surface\n");
2471 free(shsurf);
2472 return NULL;
2473 }
2474
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002475 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002476 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002477
Tiago Vignattibc052c92012-04-19 16:18:18 +03002478 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002479 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002480 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002481 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002482 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002483 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2484 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002485 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002486 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002487 wl_list_init(&shsurf->fullscreen.transform.link);
2488
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002489 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002490 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002491 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002492 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002493
2494 /* init link so its safe to always remove it in destroy_shell_surface */
2495 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002496 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002497
Pekka Paalanen460099f2012-01-20 16:48:25 +02002498 /* empty when not in use */
2499 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002500 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002501
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002502 wl_list_init(&shsurf->workspace_transform.link);
2503
Pekka Paalanen98262232011-12-01 10:42:22 +02002504 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002505 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002506
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002507 shsurf->client = client;
2508
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002509 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002510}
2511
Jason Ekstranda7af7042013-10-12 22:38:11 -05002512static struct weston_view *
2513get_primary_view(void *shell, struct shell_surface *shsurf)
2514{
2515 return shsurf->view;
2516}
2517
Tiago Vignattibc052c92012-04-19 16:18:18 +03002518static void
2519shell_get_shell_surface(struct wl_client *client,
2520 struct wl_resource *resource,
2521 uint32_t id,
2522 struct wl_resource *surface_resource)
2523{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002524 struct weston_surface *surface =
2525 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002526 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002527 struct shell_surface *shsurf;
2528
2529 if (get_shell_surface(surface)) {
2530 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002531 WL_DISPLAY_ERROR_INVALID_OBJECT,
2532 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002533 return;
2534 }
2535
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002536 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002537 if (!shsurf) {
2538 wl_resource_post_error(surface_resource,
2539 WL_DISPLAY_ERROR_INVALID_OBJECT,
2540 "surface->configure already set");
2541 return;
2542 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002543
Jason Ekstranda85118c2013-06-27 20:17:02 -05002544 shsurf->resource =
2545 wl_resource_create(client,
2546 &wl_shell_surface_interface, 1, id);
2547 wl_resource_set_implementation(shsurf->resource,
2548 &shell_surface_implementation,
2549 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002550}
2551
2552static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002553 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002554};
2555
Kristian Høgsberg07937562011-04-12 17:25:42 -04002556static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002557shell_fade(struct desktop_shell *shell, enum fade_type type);
2558
2559static int
2560screensaver_timeout(void *data)
2561{
2562 struct desktop_shell *shell = data;
2563
2564 shell_fade(shell, FADE_OUT);
2565
2566 return 1;
2567}
2568
2569static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002570handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002571{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002572 struct desktop_shell *shell =
2573 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002574
Pekka Paalanen18027e52011-12-02 16:31:49 +02002575 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002576
2577 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002578 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002579}
2580
2581static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002582launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002583{
2584 if (shell->screensaver.binding)
2585 return;
2586
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002587 if (!shell->screensaver.path) {
2588 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002589 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002590 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002591
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002592 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002593 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002594 return;
2595 }
2596
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002597 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002598 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002599 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002600 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002601}
2602
2603static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002604terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002605{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002606 if (shell->screensaver.process.pid == 0)
2607 return;
2608
2609 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002610}
2611
2612static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002613configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002614{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002615 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002616
Giulio Camuffo184df502013-02-21 11:29:21 +01002617 if (width == 0)
2618 return;
2619
Jason Ekstranda7af7042013-10-12 22:38:11 -05002620 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2621 if (v->output == ev->output && v != ev) {
2622 weston_view_unmap(v);
2623 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002624 }
2625 }
2626
Jason Ekstranda7af7042013-10-12 22:38:11 -05002627 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002628
Jason Ekstranda7af7042013-10-12 22:38:11 -05002629 if (wl_list_empty(&ev->layer_link)) {
2630 wl_list_insert(&layer->view_list, &ev->layer_link);
2631 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002632 }
2633}
2634
2635static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002636background_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 -04002637{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002638 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002639 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002640
Jason Ekstranda7af7042013-10-12 22:38:11 -05002641 view = container_of(es->views.next, struct weston_view, surface_link);
2642
2643 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002644}
2645
2646static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002647desktop_shell_set_background(struct wl_client *client,
2648 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002649 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002650 struct wl_resource *surface_resource)
2651{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002652 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002653 struct weston_surface *surface =
2654 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002655 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002656
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002657 if (surface->configure) {
2658 wl_resource_post_error(surface_resource,
2659 WL_DISPLAY_ERROR_INVALID_OBJECT,
2660 "surface role already assigned");
2661 return;
2662 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002663
Jason Ekstranda7af7042013-10-12 22:38:11 -05002664 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2665 weston_view_destroy(view);
2666 view = weston_view_create(surface);
2667
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002668 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002669 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002670 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002671 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002672 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002673 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002674 surface->output->width,
2675 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002676}
2677
2678static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002679panel_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 -04002680{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002681 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002682 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002683
Jason Ekstranda7af7042013-10-12 22:38:11 -05002684 view = container_of(es->views.next, struct weston_view, surface_link);
2685
2686 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002687}
2688
2689static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002690desktop_shell_set_panel(struct wl_client *client,
2691 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002692 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002693 struct wl_resource *surface_resource)
2694{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002695 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002696 struct weston_surface *surface =
2697 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002698 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002699
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002700 if (surface->configure) {
2701 wl_resource_post_error(surface_resource,
2702 WL_DISPLAY_ERROR_INVALID_OBJECT,
2703 "surface role already assigned");
2704 return;
2705 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002706
Jason Ekstranda7af7042013-10-12 22:38:11 -05002707 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2708 weston_view_destroy(view);
2709 view = weston_view_create(surface);
2710
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002711 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002712 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002713 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002714 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002715 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002716 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002717 surface->output->width,
2718 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002719}
2720
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002721static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002722lock_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 -04002723{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002724 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002725 struct weston_view *view;
2726
2727 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002728
Giulio Camuffo184df502013-02-21 11:29:21 +01002729 if (width == 0)
2730 return;
2731
Jason Ekstranda7af7042013-10-12 22:38:11 -05002732 surface->width = width;
2733 surface->height = height;
2734 view->geometry.width = width;
2735 view->geometry.height = height;
2736 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002737
2738 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002739 wl_list_insert(&shell->lock_layer.view_list,
2740 &view->layer_link);
2741 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002742 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002743 }
2744}
2745
2746static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002747handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002748{
Tiago Vignattibe143262012-04-16 17:31:41 +03002749 struct desktop_shell *shell =
2750 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002751
Martin Minarik6d118362012-06-07 18:01:59 +02002752 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002753 shell->lock_surface = NULL;
2754}
2755
2756static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002757desktop_shell_set_lock_surface(struct wl_client *client,
2758 struct wl_resource *resource,
2759 struct wl_resource *surface_resource)
2760{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002761 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002762 struct weston_surface *surface =
2763 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002764
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002765 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002766
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002767 if (!shell->locked)
2768 return;
2769
Pekka Paalanen98262232011-12-01 10:42:22 +02002770 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002771
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002772 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002773 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002774 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002775
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002776 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002777 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002778}
2779
2780static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002781resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002782{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002783 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002784
Pekka Paalanen77346a62011-11-30 16:26:35 +02002785 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002786
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002787 wl_list_remove(&shell->lock_layer.link);
2788 wl_list_insert(&shell->compositor->cursor_layer.link,
2789 &shell->fullscreen_layer.link);
2790 wl_list_insert(&shell->fullscreen_layer.link,
2791 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002792 if (shell->showing_input_panels) {
2793 wl_list_insert(&shell->panel_layer.link,
2794 &shell->input_panel_layer.link);
2795 wl_list_insert(&shell->input_panel_layer.link,
2796 &ws->layer.link);
2797 } else {
2798 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2799 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002800
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002801 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002802
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002803 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002804 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002805 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002806}
2807
2808static void
2809desktop_shell_unlock(struct wl_client *client,
2810 struct wl_resource *resource)
2811{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002812 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002813
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002814 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002815
2816 if (shell->locked)
2817 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002818}
2819
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002820static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002821desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002822 struct wl_resource *resource,
2823 struct wl_resource *surface_resource)
2824{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002825 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002826
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002827 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07002828 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002829}
2830
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002831static void
2832desktop_shell_desktop_ready(struct wl_client *client,
2833 struct wl_resource *resource)
2834{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002835 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002836
2837 shell_fade_startup(shell);
2838}
2839
Kristian Høgsberg75840622011-09-06 13:48:16 -04002840static const struct desktop_shell_interface desktop_shell_implementation = {
2841 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002842 desktop_shell_set_panel,
2843 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002844 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002845 desktop_shell_set_grab_surface,
2846 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002847};
2848
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002849static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002850get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002851{
2852 struct shell_surface *shsurf;
2853
2854 shsurf = get_shell_surface(surface);
2855 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002856 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002857 return shsurf->type;
2858}
2859
Kristian Høgsberg75840622011-09-06 13:48:16 -04002860static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002861move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002862{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07002863 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002864 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002865 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002866
Pekka Paalanen01388e22013-04-25 13:57:44 +03002867 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002868 if (surface == NULL)
2869 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002870
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002871 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002872 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2873 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002874 return;
2875
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002876 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002877}
2878
2879static void
Neil Robertsaba0f252013-10-03 16:43:05 +01002880touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
2881{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07002882 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01002883 struct weston_surface *surface;
2884 struct shell_surface *shsurf;
2885
2886 surface = weston_surface_get_main_surface(focus);
2887 if (surface == NULL)
2888 return;
2889
2890 shsurf = get_shell_surface(surface);
2891 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2892 shsurf->type == SHELL_SURFACE_MAXIMIZED)
2893 return;
2894
2895 surface_touch_move(shsurf, (struct weston_seat *) seat);
2896}
2897
2898static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002899resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002900{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07002901 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002902 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002903 uint32_t edges = 0;
2904 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002905 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002906
Pekka Paalanen01388e22013-04-25 13:57:44 +03002907 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002908 if (surface == NULL)
2909 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002910
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002911 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002912 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2913 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002914 return;
2915
Jason Ekstranda7af7042013-10-12 22:38:11 -05002916 weston_view_from_global(shsurf->view,
2917 wl_fixed_to_int(seat->pointer->grab_x),
2918 wl_fixed_to_int(seat->pointer->grab_y),
2919 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002920
Jason Ekstranda7af7042013-10-12 22:38:11 -05002921 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002922 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002923 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002924 edges |= 0;
2925 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002926 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002927
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002929 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002930 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002931 edges |= 0;
2932 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002933 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002934
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002935 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002936}
2937
2938static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002939surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002940 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002941{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002942 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002943 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07002944 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002945 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002946
Pekka Paalanen01388e22013-04-25 13:57:44 +03002947 /* XXX: broken for windows containing sub-surfaces */
2948 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002949 if (surface == NULL)
2950 return;
2951
2952 shsurf = get_shell_surface(surface);
2953 if (!shsurf)
2954 return;
2955
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002957
Jason Ekstranda7af7042013-10-12 22:38:11 -05002958 if (shsurf->view->alpha > 1.0)
2959 shsurf->view->alpha = 1.0;
2960 if (shsurf->view->alpha < step)
2961 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002962
Jason Ekstranda7af7042013-10-12 22:38:11 -05002963 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002964 weston_surface_damage(surface);
2965}
2966
2967static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002968do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002969 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002970{
Daniel Stone37816df2012-05-16 18:45:18 +01002971 struct weston_seat *ws = (struct weston_seat *) seat;
2972 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002973 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002974 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002975
2976 wl_list_for_each(output, &compositor->output_list, link) {
2977 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002978 wl_fixed_to_double(seat->pointer->x),
2979 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002980 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002981 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002982 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002983 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002984 increment = -output->zoom.increment;
2985 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002986 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002987 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002988 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002989 else
2990 increment = 0;
2991
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002992 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002993
Scott Moreaue6603982012-06-11 13:07:51 -06002994 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002995 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002996 else if (output->zoom.level > output->zoom.max_level)
2997 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002998 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002999 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003000 output->disable_planes++;
3001 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003002
Scott Moreaue6603982012-06-11 13:07:51 -06003003 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003004
Jason Ekstranda7af7042013-10-12 22:38:11 -05003005 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003006 }
3007 }
3008}
3009
Scott Moreauccbf29d2012-02-22 14:21:41 -07003010static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003011zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003012 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003013{
3014 do_zoom(seat, time, 0, axis, value);
3015}
3016
3017static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003018zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003019 void *data)
3020{
3021 do_zoom(seat, time, key, 0, 0);
3022}
3023
3024static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003025terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003026 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003027{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003028 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003029
Daniel Stone325fc2d2012-05-30 16:31:58 +01003030 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003031}
3032
3033static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003034rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003035{
3036 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003037 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003038 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003039 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003040 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003041
3042 if (!shsurf)
3043 return;
3044
Jason Ekstranda7af7042013-10-12 22:38:11 -05003045 cx = 0.5f * shsurf->view->geometry.width;
3046 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003047
Daniel Stone37816df2012-05-16 18:45:18 +01003048 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3049 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003050 r = sqrtf(dx * dx + dy * dy);
3051
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003052 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003053 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003054
3055 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003056 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003057 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003058
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003059 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003060 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003061
3062 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003063 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003064 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003065 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003066 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003067
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003068 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003069 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003070 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003071 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003072 wl_list_init(&shsurf->rotation.transform.link);
3073 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003074 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003075 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003076
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003077 /* We need to adjust the position of the surface
3078 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003079 cposx = shsurf->view->geometry.x + cx;
3080 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003081 dposx = rotate->center.x - cposx;
3082 dposy = rotate->center.y - cposy;
3083 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003084 weston_view_set_position(shsurf->view,
3085 shsurf->view->geometry.x + dposx,
3086 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003087 }
3088
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003089 /* Repaint implies weston_surface_update_transform(), which
3090 * lazily applies the damage due to rotation update.
3091 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003092 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003093}
3094
3095static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003096rotate_grab_button(struct weston_pointer_grab *grab,
3097 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003098{
3099 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003100 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003101 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003102 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003103 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003104
Daniel Stone4dbadb12012-05-30 16:31:51 +01003105 if (pointer->button_count == 0 &&
3106 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003107 if (shsurf)
3108 weston_matrix_multiply(&shsurf->rotation.rotation,
3109 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003110 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003111 free(rotate);
3112 }
3113}
3114
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003115static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003116 noop_grab_focus,
3117 rotate_grab_motion,
3118 rotate_grab_button,
3119};
3120
3121static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003122surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003123{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003124 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003125 float dx, dy;
3126 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003127
Pekka Paalanen460099f2012-01-20 16:48:25 +02003128 rotate = malloc(sizeof *rotate);
3129 if (!rotate)
3130 return;
3131
Jason Ekstranda7af7042013-10-12 22:38:11 -05003132 weston_view_to_global_float(surface->view,
3133 surface->view->geometry.width * 0.5f,
3134 surface->view->geometry.height * 0.5f,
3135 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003136
Daniel Stone37816df2012-05-16 18:45:18 +01003137 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3138 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003139 r = sqrtf(dx * dx + dy * dy);
3140 if (r > 20.0f) {
3141 struct weston_matrix inverse;
3142
3143 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003144 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003145 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003146
3147 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003148 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003149 } else {
3150 weston_matrix_init(&surface->rotation.rotation);
3151 weston_matrix_init(&rotate->rotation);
3152 }
3153
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003154 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3155 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003156}
3157
3158static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003159rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003160 void *data)
3161{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003162 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003163 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003164 struct shell_surface *surface;
3165
Pekka Paalanen01388e22013-04-25 13:57:44 +03003166 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003167 if (base_surface == NULL)
3168 return;
3169
3170 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003171 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3172 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003173 return;
3174
3175 surface_rotate(surface, seat);
3176}
3177
3178static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003179lower_fullscreen_layer(struct desktop_shell *shell)
3180{
3181 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003182 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003183
3184 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003185 wl_list_for_each_reverse_safe(view, prev,
3186 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003187 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003188 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003189}
3190
3191static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003192activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003193 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003194{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003195 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003196 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003197 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003198 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003199
Pekka Paalanen01388e22013-04-25 13:57:44 +03003200 main_surface = weston_surface_get_main_surface(es);
3201
Daniel Stone37816df2012-05-16 18:45:18 +01003202 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003203
Jonas Ådahl8538b222012-08-29 22:13:03 +02003204 state = ensure_focus_state(shell, seat);
3205 if (state == NULL)
3206 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003207
3208 state->keyboard_focus = es;
3209 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003210 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003211
Pekka Paalanen01388e22013-04-25 13:57:44 +03003212 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003213 case SHELL_SURFACE_FULLSCREEN:
3214 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003215 shell_stack_fullscreen(get_shell_surface(main_surface));
3216 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08003217 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003218 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003219 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003220 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003221 main_view = get_default_view(main_surface);
3222 if (main_view)
3223 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003224 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003225 }
3226}
3227
Alex Wu21858432012-04-01 20:13:08 +08003228/* no-op func for checking black surface */
3229static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003230black_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 +08003231{
3232}
3233
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003234static bool
Alex Wu21858432012-04-01 20:13:08 +08003235is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3236{
3237 if (es->configure == black_surface_configure) {
3238 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003239 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003240 return true;
3241 }
3242 return false;
3243}
3244
Kristian Høgsberg75840622011-09-06 13:48:16 -04003245static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003246activate_binding(struct weston_seat *seat,
3247 struct desktop_shell *shell,
3248 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003249{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003250 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003251
Alex Wu9c35e6b2012-03-05 14:13:13 +08003252 if (!focus)
3253 return;
3254
Pekka Paalanen01388e22013-04-25 13:57:44 +03003255 if (is_black_surface(focus, &main_surface))
3256 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003257
Pekka Paalanen01388e22013-04-25 13:57:44 +03003258 main_surface = weston_surface_get_main_surface(focus);
3259 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003260 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003261
Neil Robertsa28c6932013-10-03 16:43:04 +01003262 activate(shell, focus, seat);
3263}
3264
3265static void
3266click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3267 void *data)
3268{
3269 if (seat->pointer->grab != &seat->pointer->default_grab)
3270 return;
3271
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003272 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003273}
3274
3275static void
3276touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3277{
3278 if (seat->touch->grab != &seat->touch->default_grab)
3279 return;
3280
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003281 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003282}
3283
3284static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003285lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003286{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003287 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003288
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003289 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003290 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003291 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003292 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003293
3294 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003295
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003296 /* Hide all surfaces by removing the fullscreen, panel and
3297 * toplevel layers. This way nothing else can show or receive
3298 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003299
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003300 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003301 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003302 if (shell->showing_input_panels)
3303 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003304 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003305 wl_list_insert(&shell->compositor->cursor_layer.link,
3306 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003307
Pekka Paalanen77346a62011-11-30 16:26:35 +02003308 launch_screensaver(shell);
3309
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003310 /* TODO: disable bindings that should not work while locked. */
3311
3312 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003313}
3314
3315static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003316unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003317{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003318 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003319 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003320 return;
3321 }
3322
3323 /* If desktop-shell client has gone away, unlock immediately. */
3324 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003325 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003326 return;
3327 }
3328
3329 if (shell->prepare_event_sent)
3330 return;
3331
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003332 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003333 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003334}
3335
3336static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003337shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003338{
3339 struct desktop_shell *shell = data;
3340
3341 shell->fade.animation = NULL;
3342
3343 switch (shell->fade.type) {
3344 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003345 weston_surface_destroy(shell->fade.view->surface);
3346 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003347 break;
3348 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003349 lock(shell);
3350 break;
3351 }
3352}
3353
Jason Ekstranda7af7042013-10-12 22:38:11 -05003354static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003355shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003356{
3357 struct weston_compositor *compositor = shell->compositor;
3358 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003359 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003360
3361 surface = weston_surface_create(compositor);
3362 if (!surface)
3363 return NULL;
3364
Jason Ekstranda7af7042013-10-12 22:38:11 -05003365 view = weston_view_create(surface);
3366 if (!view) {
3367 weston_surface_destroy(surface);
3368 return NULL;
3369 }
3370
3371 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003372 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003373 wl_list_insert(&compositor->fade_layer.view_list,
3374 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003375 pixman_region32_init(&surface->input);
3376
Jason Ekstranda7af7042013-10-12 22:38:11 -05003377 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003378}
3379
3380static void
3381shell_fade(struct desktop_shell *shell, enum fade_type type)
3382{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003383 float tint;
3384
3385 switch (type) {
3386 case FADE_IN:
3387 tint = 0.0;
3388 break;
3389 case FADE_OUT:
3390 tint = 1.0;
3391 break;
3392 default:
3393 weston_log("shell: invalid fade type\n");
3394 return;
3395 }
3396
3397 shell->fade.type = type;
3398
Jason Ekstranda7af7042013-10-12 22:38:11 -05003399 if (shell->fade.view == NULL) {
3400 shell->fade.view = shell_fade_create_surface(shell);
3401 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003402 return;
3403
Jason Ekstranda7af7042013-10-12 22:38:11 -05003404 shell->fade.view->alpha = 1.0 - tint;
3405 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003406 }
3407
3408 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003409 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003410 else
3411 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003412 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003413 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003414 shell_fade_done, shell);
3415}
3416
3417static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003418do_shell_fade_startup(void *data)
3419{
3420 struct desktop_shell *shell = data;
3421
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003422 if (shell->startup_animation_type == ANIMATION_FADE)
3423 shell_fade(shell, FADE_IN);
3424 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003425 weston_surface_destroy(shell->fade.view->surface);
3426 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003427 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003428}
3429
3430static void
3431shell_fade_startup(struct desktop_shell *shell)
3432{
3433 struct wl_event_loop *loop;
3434
3435 if (!shell->fade.startup_timer)
3436 return;
3437
3438 wl_event_source_remove(shell->fade.startup_timer);
3439 shell->fade.startup_timer = NULL;
3440
3441 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3442 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3443}
3444
3445static int
3446fade_startup_timeout(void *data)
3447{
3448 struct desktop_shell *shell = data;
3449
3450 shell_fade_startup(shell);
3451 return 0;
3452}
3453
3454static void
3455shell_fade_init(struct desktop_shell *shell)
3456{
3457 /* Make compositor output all black, and wait for the desktop-shell
3458 * client to signal it is ready, then fade in. The timer triggers a
3459 * fade-in, in case the desktop-shell client takes too long.
3460 */
3461
3462 struct wl_event_loop *loop;
3463
Jason Ekstranda7af7042013-10-12 22:38:11 -05003464 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003465 weston_log("%s: warning: fade surface already exists\n",
3466 __func__);
3467 return;
3468 }
3469
Jason Ekstranda7af7042013-10-12 22:38:11 -05003470 shell->fade.view = shell_fade_create_surface(shell);
3471 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003472 return;
3473
Jason Ekstranda7af7042013-10-12 22:38:11 -05003474 weston_view_update_transform(shell->fade.view);
3475 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003476
3477 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3478 shell->fade.startup_timer =
3479 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3480 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3481}
3482
3483static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003484idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003485{
3486 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003487 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003488
3489 shell_fade(shell, FADE_OUT);
3490 /* lock() is called from shell_fade_done() */
3491}
3492
3493static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003494wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003495{
3496 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003497 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003498
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003499 unlock(shell);
3500}
3501
3502static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003503show_input_panels(struct wl_listener *listener, void *data)
3504{
3505 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003506 container_of(listener, struct desktop_shell,
3507 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003508 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003509
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003510 shell->text_input.surface = (struct weston_surface*)data;
3511
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003512 if (shell->showing_input_panels)
3513 return;
3514
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003515 shell->showing_input_panels = true;
3516
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003517 if (!shell->locked)
3518 wl_list_insert(&shell->panel_layer.link,
3519 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003520
Jason Ekstranda7af7042013-10-12 22:38:11 -05003521 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003522 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003523 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003524 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003525 wl_list_insert(&shell->input_panel_layer.view_list,
3526 &ipsurf->view->layer_link);
3527 weston_view_geometry_dirty(ipsurf->view);
3528 weston_view_update_transform(ipsurf->view);
3529 weston_surface_damage(ipsurf->surface);
3530 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
3531 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003532 }
3533}
3534
3535static void
3536hide_input_panels(struct wl_listener *listener, void *data)
3537{
3538 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003539 container_of(listener, struct desktop_shell,
3540 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003541 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003542
Jan Arne Petersen61381972013-01-31 15:52:21 +01003543 if (!shell->showing_input_panels)
3544 return;
3545
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003546 shell->showing_input_panels = false;
3547
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003548 if (!shell->locked)
3549 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003550
Jason Ekstranda7af7042013-10-12 22:38:11 -05003551 wl_list_for_each_safe(view, next,
3552 &shell->input_panel_layer.view_list, layer_link)
3553 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003554}
3555
3556static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003557update_input_panels(struct wl_listener *listener, void *data)
3558{
3559 struct desktop_shell *shell =
3560 container_of(listener, struct desktop_shell,
3561 update_input_panel_listener);
3562
3563 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3564}
3565
3566static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003567center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003568{
Giulio Camuffob8366642013-04-25 13:57:46 +03003569 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003570 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003571
Jason Ekstranda7af7042013-10-12 22:38:11 -05003572 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03003573
3574 x = output->x + (output->width - width) / 2 - surf_x / 2;
3575 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003576
Jason Ekstranda7af7042013-10-12 22:38:11 -05003577 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003578}
3579
3580static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003581weston_view_set_initial_position(struct weston_view *view,
3582 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003583{
3584 struct weston_compositor *compositor = shell->compositor;
3585 int ix = 0, iy = 0;
3586 int range_x, range_y;
3587 int dx, dy, x, y, panel_height;
3588 struct weston_output *output, *target_output = NULL;
3589 struct weston_seat *seat;
3590
3591 /* As a heuristic place the new window on the same output as the
3592 * pointer. Falling back to the output containing 0, 0.
3593 *
3594 * TODO: Do something clever for touch too?
3595 */
3596 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003597 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003598 ix = wl_fixed_to_int(seat->pointer->x);
3599 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003600 break;
3601 }
3602 }
3603
3604 wl_list_for_each(output, &compositor->output_list, link) {
3605 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3606 target_output = output;
3607 break;
3608 }
3609 }
3610
3611 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003612 weston_view_set_position(view, 10 + random() % 400,
3613 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003614 return;
3615 }
3616
3617 /* Valid range within output where the surface will still be onscreen.
3618 * If this is negative it means that the surface is bigger than
3619 * output.
3620 */
3621 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003622 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003623 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05003624 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003625
Rob Bradford4cb88c72012-08-13 15:18:44 +01003626 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003627 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003628 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003629 dx = 0;
3630
3631 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003632 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003633 else
3634 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003635
3636 x = target_output->x + dx;
3637 y = target_output->y + dy;
3638
Jason Ekstranda7af7042013-10-12 22:38:11 -05003639 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003640}
3641
3642static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003643map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003644 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003645{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003646 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003647 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003648 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003649 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003650 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003651 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003652
Jason Ekstranda7af7042013-10-12 22:38:11 -05003653 shsurf->view->geometry.width = width;
3654 shsurf->view->geometry.height = height;
3655 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003656
3657 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003658 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02003659 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003660 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003661 break;
Alex Wu4539b082012-03-01 12:57:46 +08003662 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003663 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003664 shell_map_fullscreen(shsurf);
3665 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003666 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003667 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003668 panel_height = get_output_panel_height(shell, shsurf->output);
3669 surface_subsurfaces_boundingbox(shsurf->surface,
3670 &surf_x, &surf_y, NULL, NULL);
3671 weston_view_set_position(shsurf->view,
3672 shsurf->output->x - surf_x,
3673 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003674 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003675 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003676 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003677 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003678 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003679 weston_view_set_position(shsurf->view,
3680 shsurf->view->geometry.x + sx,
3681 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003682 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003683 default:
3684 ;
3685 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003686
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003687 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003688 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003689 case SHELL_SURFACE_POPUP:
3690 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003691 /* TODO: Handle a parent with multiple views */
3692 parent = get_default_view(shsurf->parent);
3693 if (parent) {
3694 wl_list_remove(&shsurf->view->layer_link);
3695 wl_list_insert(parent->layer_link.prev,
3696 &shsurf->view->layer_link);
3697 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003698 break;
Alex Wu4539b082012-03-01 12:57:46 +08003699 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003700 case SHELL_SURFACE_NONE:
3701 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003702 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003703 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003704 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003705 wl_list_remove(&shsurf->view->layer_link);
3706 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003707 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003708 }
3709
Jason Ekstranda7af7042013-10-12 22:38:11 -05003710 if (shsurf->type != SHELL_SURFACE_NONE) {
3711 weston_view_update_transform(shsurf->view);
3712 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
3713 shsurf->surface->output = shsurf->output;
3714 shsurf->view->output = shsurf->output;
3715 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003716 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003717
Jason Ekstranda7af7042013-10-12 22:38:11 -05003718 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003719 /* XXX: xwayland's using the same fields for transient type */
3720 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003721 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003722 if (shsurf->transient.flags ==
3723 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3724 break;
3725 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003726 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003727 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003728 if (!shell->locked) {
3729 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003730 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01003731 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003732 break;
3733 default:
3734 break;
3735 }
3736
Jason Ekstranda7af7042013-10-12 22:38:11 -05003737 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003738 {
3739 switch (shell->win_animation_type) {
3740 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003741 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003742 break;
3743 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003744 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003745 break;
3746 default:
3747 break;
3748 }
3749 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003750}
3751
3752static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003753configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003754 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003755{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003756 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3757 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003758 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03003759 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003760
Pekka Paalanen77346a62011-11-30 16:26:35 +02003761 shsurf = get_shell_surface(surface);
3762 if (shsurf)
3763 surface_type = shsurf->type;
3764
Jason Ekstranda7af7042013-10-12 22:38:11 -05003765 /* TODO:
3766 * This should probably be changed to be more shell_surface
3767 * dependent
3768 */
3769 wl_list_for_each(view, &surface->views, surface_link)
3770 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003771
3772 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003773 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003774 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003775 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003776 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003777 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003778 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003779 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3780 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003781 shsurf->view->geometry.x = shsurf->output->x - surf_x;
3782 shsurf->view->geometry.y = shsurf->output->y +
3783 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003784 break;
Alex Wu4539b082012-03-01 12:57:46 +08003785 case SHELL_SURFACE_TOPLEVEL:
3786 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003787 default:
3788 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003789 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003790
Alex Wu4539b082012-03-01 12:57:46 +08003791 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003792 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003793 wl_list_for_each(view, &surface->views, surface_link)
3794 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003795
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003796 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003797 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003798 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003799}
3800
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003801static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003802shell_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 +03003803{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003804 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003805 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003806
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003807 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003808
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003809 if (!weston_surface_is_mapped(es) &&
3810 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003811 remove_popup_grab(shsurf);
3812 }
3813
Giulio Camuffo184df502013-02-21 11:29:21 +01003814 if (width == 0)
3815 return;
3816
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003817 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003818 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003819 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003820 type_changed = 1;
3821 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003822
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003823 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003824 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003825 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05003826 shsurf->view->geometry.width != width ||
3827 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003828 float from_x, from_y;
3829 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003830
Jason Ekstranda7af7042013-10-12 22:38:11 -05003831 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
3832 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003833 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003834 shsurf->view->geometry.x + to_x - from_x,
3835 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003836 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003837 }
3838}
3839
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003840static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003841
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003842static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003843desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003844{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003845 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003846 struct desktop_shell *shell =
3847 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003848
3849 shell->child.process.pid = 0;
3850 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003851
3852 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3853 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003854 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003855 shell->child.deathstamp = time;
3856 shell->child.deathcount = 0;
3857 }
3858
3859 shell->child.deathcount++;
3860 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003861 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003862 return;
3863 }
3864
Martin Minarik6d118362012-06-07 18:01:59 +02003865 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003866 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003867 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003868}
3869
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003870static void
3871launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003872{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003873 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003874 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003875
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003876 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003877 &shell->child.process,
3878 shell_exe,
3879 desktop_shell_sigchld);
3880
3881 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003882 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003883}
3884
3885static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003886bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3887{
Tiago Vignattibe143262012-04-16 17:31:41 +03003888 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003889 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003890
Jason Ekstranda85118c2013-06-27 20:17:02 -05003891 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3892 if (resource)
3893 wl_resource_set_implementation(resource, &shell_implementation,
3894 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003895}
3896
Kristian Høgsberg75840622011-09-06 13:48:16 -04003897static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003898unbind_desktop_shell(struct wl_resource *resource)
3899{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003900 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003901
3902 if (shell->locked)
3903 resume_desktop(shell);
3904
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003905 shell->child.desktop_shell = NULL;
3906 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003907}
3908
3909static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003910bind_desktop_shell(struct wl_client *client,
3911 void *data, uint32_t version, uint32_t id)
3912{
Tiago Vignattibe143262012-04-16 17:31:41 +03003913 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003914 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003915
Jason Ekstranda85118c2013-06-27 20:17:02 -05003916 resource = wl_resource_create(client, &desktop_shell_interface,
3917 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003918
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003919 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003920 wl_resource_set_implementation(resource,
3921 &desktop_shell_implementation,
3922 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003923 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003924
3925 if (version < 2)
3926 shell_fade_startup(shell);
3927
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003928 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003929 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003930
3931 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3932 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003933 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003934}
3935
Pekka Paalanen6e168112011-11-24 11:34:05 +02003936static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003937screensaver_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 -04003938{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003939 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003940 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003941
Giulio Camuffo184df502013-02-21 11:29:21 +01003942 if (width == 0)
3943 return;
3944
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003945 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003946 if (!shell->locked)
3947 return;
3948
Jason Ekstranda7af7042013-10-12 22:38:11 -05003949 view = container_of(surface->views.next, struct weston_view, surface_link);
3950 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003951
Jason Ekstranda7af7042013-10-12 22:38:11 -05003952 if (wl_list_empty(&view->layer_link)) {
3953 wl_list_insert(shell->lock_layer.view_list.prev,
3954 &view->layer_link);
3955 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003956 wl_event_source_timer_update(shell->screensaver.timer,
3957 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003958 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003959 }
3960}
3961
3962static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003963screensaver_set_surface(struct wl_client *client,
3964 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003965 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003966 struct wl_resource *output_resource)
3967{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003968 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003969 struct weston_surface *surface =
3970 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003971 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003972 struct weston_view *view, *next;
3973
3974 /* Make sure we only have one view */
3975 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3976 weston_view_destroy(view);
3977 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003978
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003979 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003980 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003981 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003982}
3983
3984static const struct screensaver_interface screensaver_implementation = {
3985 screensaver_set_surface
3986};
3987
3988static void
3989unbind_screensaver(struct wl_resource *resource)
3990{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003991 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003992
Pekka Paalanen77346a62011-11-30 16:26:35 +02003993 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003994}
3995
3996static void
3997bind_screensaver(struct wl_client *client,
3998 void *data, uint32_t version, uint32_t id)
3999{
Tiago Vignattibe143262012-04-16 17:31:41 +03004000 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004001 struct wl_resource *resource;
4002
Jason Ekstranda85118c2013-06-27 20:17:02 -05004003 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004004
Pekka Paalanen77346a62011-11-30 16:26:35 +02004005 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004006 wl_resource_set_implementation(resource,
4007 &screensaver_implementation,
4008 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004009 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004010 return;
4011 }
4012
4013 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4014 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004015 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004016}
4017
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004018static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004019input_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 -04004020{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004021 struct input_panel_surface *ip_surface = surface->configure_private;
4022 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004023 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004024 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004025
Giulio Camuffo184df502013-02-21 11:29:21 +01004026 if (width == 0)
4027 return;
4028
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004029 if (!weston_surface_is_mapped(surface)) {
4030 if (!shell->showing_input_panels)
4031 return;
4032
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004033 show_surface = 1;
4034 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004035
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004036 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004037
4038 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004039 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4040 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004041 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004042 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4043 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004044 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004045
Jason Ekstranda7af7042013-10-12 22:38:11 -05004046 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004047
4048 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004049 wl_list_insert(&shell->input_panel_layer.view_list,
4050 &ip_surface->view->layer_link);
4051 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004052 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004053 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004054 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004055}
4056
4057static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004058destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004059{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004060 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4061
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004062 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004063 wl_list_remove(&input_panel_surface->link);
4064
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004065 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004066 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004067
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004068 free(input_panel_surface);
4069}
4070
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004071static struct input_panel_surface *
4072get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004073{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004074 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004075 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004076 } else {
4077 return NULL;
4078 }
4079}
4080
4081static void
4082input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4083{
4084 struct input_panel_surface *ipsurface = container_of(listener,
4085 struct input_panel_surface,
4086 surface_destroy_listener);
4087
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004088 if (ipsurface->resource) {
4089 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004090 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004091 destroy_input_panel_surface(ipsurface);
4092 }
4093}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004094
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004095static struct input_panel_surface *
4096create_input_panel_surface(struct desktop_shell *shell,
4097 struct weston_surface *surface)
4098{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004099 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004100
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004101 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4102 if (!input_panel_surface)
4103 return NULL;
4104
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004105 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004106 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004107
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004108 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004109
4110 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004111 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004112
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004113 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004114 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004115 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004116 &input_panel_surface->surface_destroy_listener);
4117
4118 wl_list_init(&input_panel_surface->link);
4119
4120 return input_panel_surface;
4121}
4122
4123static void
4124input_panel_surface_set_toplevel(struct wl_client *client,
4125 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004126 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004127 uint32_t position)
4128{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004129 struct input_panel_surface *input_panel_surface =
4130 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004131 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004132
4133 wl_list_insert(&shell->input_panel.surfaces,
4134 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004135
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004136 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004137 input_panel_surface->panel = 0;
4138}
4139
4140static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004141input_panel_surface_set_overlay_panel(struct wl_client *client,
4142 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004143{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004144 struct input_panel_surface *input_panel_surface =
4145 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004146 struct desktop_shell *shell = input_panel_surface->shell;
4147
4148 wl_list_insert(&shell->input_panel.surfaces,
4149 &input_panel_surface->link);
4150
4151 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004152}
4153
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004154static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004155 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004156 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004157};
4158
4159static void
4160destroy_input_panel_surface_resource(struct wl_resource *resource)
4161{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004162 struct input_panel_surface *ipsurf =
4163 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004164
4165 destroy_input_panel_surface(ipsurf);
4166}
4167
4168static void
4169input_panel_get_input_panel_surface(struct wl_client *client,
4170 struct wl_resource *resource,
4171 uint32_t id,
4172 struct wl_resource *surface_resource)
4173{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004174 struct weston_surface *surface =
4175 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004176 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004177 struct input_panel_surface *ipsurf;
4178
4179 if (get_input_panel_surface(surface)) {
4180 wl_resource_post_error(surface_resource,
4181 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004182 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004183 return;
4184 }
4185
4186 ipsurf = create_input_panel_surface(shell, surface);
4187 if (!ipsurf) {
4188 wl_resource_post_error(surface_resource,
4189 WL_DISPLAY_ERROR_INVALID_OBJECT,
4190 "surface->configure already set");
4191 return;
4192 }
4193
Jason Ekstranda85118c2013-06-27 20:17:02 -05004194 ipsurf->resource =
4195 wl_resource_create(client,
4196 &wl_input_panel_surface_interface, 1, id);
4197 wl_resource_set_implementation(ipsurf->resource,
4198 &input_panel_surface_implementation,
4199 ipsurf,
4200 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004201}
4202
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004203static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004204 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004205};
4206
4207static void
4208unbind_input_panel(struct wl_resource *resource)
4209{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004210 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004211
4212 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004213}
4214
4215static void
4216bind_input_panel(struct wl_client *client,
4217 void *data, uint32_t version, uint32_t id)
4218{
4219 struct desktop_shell *shell = data;
4220 struct wl_resource *resource;
4221
Jason Ekstranda85118c2013-06-27 20:17:02 -05004222 resource = wl_resource_create(client,
4223 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004224
4225 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004226 wl_resource_set_implementation(resource,
4227 &input_panel_implementation,
4228 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004229 shell->input_panel.binding = resource;
4230 return;
4231 }
4232
4233 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4234 "interface object already bound");
4235 wl_resource_destroy(resource);
4236}
4237
Kristian Høgsberg07045392012-02-19 18:52:44 -05004238struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004239 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004240 struct weston_surface *current;
4241 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004242 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004243};
4244
4245static void
4246switcher_next(struct switcher *switcher)
4247{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004248 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004249 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004250 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004251 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004252
Jason Ekstranda7af7042013-10-12 22:38:11 -05004253 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4254 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004255 case SHELL_SURFACE_TOPLEVEL:
4256 case SHELL_SURFACE_FULLSCREEN:
4257 case SHELL_SURFACE_MAXIMIZED:
4258 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004259 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004260 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004261 next = view->surface;
4262 prev = view->surface;
4263 view->alpha = 0.25;
4264 weston_view_geometry_dirty(view);
4265 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004266 break;
4267 default:
4268 break;
4269 }
Alex Wu1659daa2012-04-01 20:13:09 +08004270
Jason Ekstranda7af7042013-10-12 22:38:11 -05004271 if (is_black_surface(view->surface, NULL)) {
4272 view->alpha = 0.25;
4273 weston_view_geometry_dirty(view);
4274 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004275 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004276 }
4277
4278 if (next == NULL)
4279 next = first;
4280
Alex Wu07b26062012-03-12 16:06:01 +08004281 if (next == NULL)
4282 return;
4283
Kristian Høgsberg07045392012-02-19 18:52:44 -05004284 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004285 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004286
4287 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004288 wl_list_for_each(view, &next->views, surface_link)
4289 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004290
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004291 shsurf = get_shell_surface(switcher->current);
4292 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004293 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004294}
4295
4296static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004297switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004298{
4299 struct switcher *switcher =
4300 container_of(listener, struct switcher, listener);
4301
4302 switcher_next(switcher);
4303}
4304
4305static void
Daniel Stone351eb612012-05-31 15:27:47 -04004306switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004307{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004308 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004309 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004310 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004311
Jason Ekstranda7af7042013-10-12 22:38:11 -05004312 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4313 view->alpha = 1.0;
4314 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004315 }
4316
Alex Wu07b26062012-03-12 16:06:01 +08004317 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004318 activate(switcher->shell, switcher->current,
4319 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004320 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004321 weston_keyboard_end_grab(keyboard);
4322 if (keyboard->input_method_resource)
4323 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004324 free(switcher);
4325}
4326
4327static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004328switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004329 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004330{
4331 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004332 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004333
Daniel Stonec9785ea2012-05-30 16:31:52 +01004334 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004335 switcher_next(switcher);
4336}
4337
4338static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004339switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004340 uint32_t mods_depressed, uint32_t mods_latched,
4341 uint32_t mods_locked, uint32_t group)
4342{
4343 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004344 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004345
Daniel Stone351eb612012-05-31 15:27:47 -04004346 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4347 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004348}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004349
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004350static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004351 switcher_key,
4352 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004353};
4354
4355static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004356switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004357 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004358{
Tiago Vignattibe143262012-04-16 17:31:41 +03004359 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004360 struct switcher *switcher;
4361
4362 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004363 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004364 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004365 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004366 wl_list_init(&switcher->listener.link);
4367
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004368 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004369 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004370 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004371 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4372 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004373 switcher_next(switcher);
4374}
4375
Pekka Paalanen3c647232011-12-22 13:43:43 +02004376static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004377backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004378 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004379{
4380 struct weston_compositor *compositor = data;
4381 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004382 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004383
4384 /* TODO: we're limiting to simple use cases, where we assume just
4385 * control on the primary display. We'd have to extend later if we
4386 * ever get support for setting backlights on random desktop LCD
4387 * panels though */
4388 output = get_default_output(compositor);
4389 if (!output)
4390 return;
4391
4392 if (!output->set_backlight)
4393 return;
4394
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004395 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4396 backlight_new = output->backlight_current - 25;
4397 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4398 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004399
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004400 if (backlight_new < 5)
4401 backlight_new = 5;
4402 if (backlight_new > 255)
4403 backlight_new = 255;
4404
4405 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004406 output->set_backlight(output, output->backlight_current);
4407}
4408
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004409struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004410 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004411 struct weston_seat *seat;
4412 uint32_t key[2];
4413 int key_released[2];
4414};
4415
4416static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004417debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004418 uint32_t key, uint32_t state)
4419{
4420 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01004421 struct weston_compositor *ec = db->seat->compositor;
4422 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004423 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004424 uint32_t serial;
4425 int send = 0, terminate = 0;
4426 int check_binding = 1;
4427 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01004428 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004429
4430 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4431 /* Do not run bindings on key releases */
4432 check_binding = 0;
4433
4434 for (i = 0; i < 2; i++)
4435 if (key == db->key[i])
4436 db->key_released[i] = 1;
4437
4438 if (db->key_released[0] && db->key_released[1]) {
4439 /* All key releases been swalled so end the grab */
4440 terminate = 1;
4441 } else if (key != db->key[0] && key != db->key[1]) {
4442 /* Should not swallow release of other keys */
4443 send = 1;
4444 }
4445 } else if (key == db->key[0] && !db->key_released[0]) {
4446 /* Do not check bindings for the first press of the binding
4447 * key. This allows it to be used as a debug shortcut.
4448 * We still need to swallow this event. */
4449 check_binding = 0;
4450 } else if (db->key[1]) {
4451 /* If we already ran a binding don't process another one since
4452 * we can't keep track of all the binding keys that were
4453 * pressed in order to swallow the release events. */
4454 send = 1;
4455 check_binding = 0;
4456 }
4457
4458 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004459 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4460 key, state)) {
4461 /* We ran a binding so swallow the press and keep the
4462 * grab to swallow the released too. */
4463 send = 0;
4464 terminate = 0;
4465 db->key[1] = key;
4466 } else {
4467 /* Terminate the grab since the key pressed is not a
4468 * debug binding key. */
4469 send = 1;
4470 terminate = 1;
4471 }
4472 }
4473
4474 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01004475 serial = wl_display_next_serial(display);
4476 resource_list = &grab->keyboard->focus_resource_list;
4477 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004478 wl_keyboard_send_key(resource, serial, time, key, state);
4479 }
4480 }
4481
4482 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004483 weston_keyboard_end_grab(grab->keyboard);
4484 if (grab->keyboard->input_method_resource)
4485 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004486 free(db);
4487 }
4488}
4489
4490static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004491debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004492 uint32_t mods_depressed, uint32_t mods_latched,
4493 uint32_t mods_locked, uint32_t group)
4494{
4495 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01004496 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004497
Neil Roberts96d790e2013-09-19 17:32:00 +01004498 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004499
Neil Roberts96d790e2013-09-19 17:32:00 +01004500 wl_resource_for_each(resource, resource_list) {
4501 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4502 mods_latched, mods_locked, group);
4503 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004504}
4505
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004506struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004507 debug_binding_key,
4508 debug_binding_modifiers
4509};
4510
4511static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004512debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004513{
4514 struct debug_binding_grab *grab;
4515
4516 grab = calloc(1, sizeof *grab);
4517 if (!grab)
4518 return;
4519
4520 grab->seat = (struct weston_seat *) seat;
4521 grab->key[0] = key;
4522 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004523 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004524}
4525
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004526static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004527force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004528 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004529{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004530 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004531 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004532 struct desktop_shell *shell = data;
4533 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004534 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004535
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004536 focus_surface = seat->keyboard->focus;
4537 if (!focus_surface)
4538 return;
4539
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004540 wl_signal_emit(&compositor->kill_signal, focus_surface);
4541
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004542 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004543 wl_client_get_credentials(client, &pid, NULL, NULL);
4544
4545 /* Skip clients that we launched ourselves (the credentials of
4546 * the socketpair is ours) */
4547 if (pid == getpid())
4548 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004549
Daniel Stone325fc2d2012-05-30 16:31:58 +01004550 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004551}
4552
4553static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004554workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004555 uint32_t key, void *data)
4556{
4557 struct desktop_shell *shell = data;
4558 unsigned int new_index = shell->workspaces.current;
4559
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004560 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004561 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004562 if (new_index != 0)
4563 new_index--;
4564
4565 change_workspace(shell, new_index);
4566}
4567
4568static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004569workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004570 uint32_t key, void *data)
4571{
4572 struct desktop_shell *shell = data;
4573 unsigned int new_index = shell->workspaces.current;
4574
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004575 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004576 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004577 if (new_index < shell->workspaces.num - 1)
4578 new_index++;
4579
4580 change_workspace(shell, new_index);
4581}
4582
4583static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004584workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004585 uint32_t key, void *data)
4586{
4587 struct desktop_shell *shell = data;
4588 unsigned int new_index;
4589
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004590 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004591 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004592 new_index = key - KEY_F1;
4593 if (new_index >= shell->workspaces.num)
4594 new_index = shell->workspaces.num - 1;
4595
4596 change_workspace(shell, new_index);
4597}
4598
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004599static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004600workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004601 uint32_t key, void *data)
4602{
4603 struct desktop_shell *shell = data;
4604 unsigned int new_index = shell->workspaces.current;
4605
4606 if (shell->locked)
4607 return;
4608
4609 if (new_index != 0)
4610 new_index--;
4611
4612 take_surface_to_workspace_by_seat(shell, seat, new_index);
4613}
4614
4615static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004616workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004617 uint32_t key, void *data)
4618{
4619 struct desktop_shell *shell = data;
4620 unsigned int new_index = shell->workspaces.current;
4621
4622 if (shell->locked)
4623 return;
4624
4625 if (new_index < shell->workspaces.num - 1)
4626 new_index++;
4627
4628 take_surface_to_workspace_by_seat(shell, seat, new_index);
4629}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004630
4631static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004632handle_output_destroy(struct wl_listener *listener, void *data)
4633{
4634 struct shell_output *output_listener =
4635 container_of(listener, struct shell_output, destroy_listener);
4636
4637 wl_list_remove(&output_listener->destroy_listener.link);
4638 wl_list_remove(&output_listener->link);
4639 free(output_listener);
4640}
4641
4642static void
4643create_shell_output(struct desktop_shell *shell,
4644 struct weston_output *output)
4645{
4646 struct shell_output *shell_output;
4647
4648 shell_output = zalloc(sizeof *shell_output);
4649 if (shell_output == NULL)
4650 return;
4651
4652 shell_output->output = output;
4653 shell_output->shell = shell;
4654 shell_output->destroy_listener.notify = handle_output_destroy;
4655 wl_signal_add(&output->destroy_signal,
4656 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004657 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004658}
4659
4660static void
4661handle_output_create(struct wl_listener *listener, void *data)
4662{
4663 struct desktop_shell *shell =
4664 container_of(listener, struct desktop_shell, output_create_listener);
4665 struct weston_output *output = (struct weston_output *)data;
4666
4667 create_shell_output(shell, output);
4668}
4669
4670static void
4671setup_output_destroy_handler(struct weston_compositor *ec,
4672 struct desktop_shell *shell)
4673{
4674 struct weston_output *output;
4675
4676 wl_list_init(&shell->output_list);
4677 wl_list_for_each(output, &ec->output_list, link)
4678 create_shell_output(shell, output);
4679
4680 shell->output_create_listener.notify = handle_output_create;
4681 wl_signal_add(&ec->output_created_signal,
4682 &shell->output_create_listener);
4683}
4684
4685static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004686shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004687{
Tiago Vignattibe143262012-04-16 17:31:41 +03004688 struct desktop_shell *shell =
4689 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004690 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004691 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004692
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004693 if (shell->child.client)
4694 wl_client_destroy(shell->child.client);
4695
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004696 wl_list_remove(&shell->idle_listener.link);
4697 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004698 wl_list_remove(&shell->show_input_panel_listener.link);
4699 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004700
Xiong Zhang6b481422013-10-23 13:58:32 +08004701 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4702 wl_list_remove(&shell_output->destroy_listener.link);
4703 wl_list_remove(&shell_output->link);
4704 free(shell_output);
4705 }
4706
4707 wl_list_remove(&shell->output_create_listener.link);
4708
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004709 wl_array_for_each(ws, &shell->workspaces.array)
4710 workspace_destroy(*ws);
4711 wl_array_release(&shell->workspaces.array);
4712
Pekka Paalanen3c647232011-12-22 13:43:43 +02004713 free(shell->screensaver.path);
4714 free(shell);
4715}
4716
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004717static void
4718shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4719{
4720 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004721 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004722
4723 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004724 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4725 MODIFIER_CTRL | MODIFIER_ALT,
4726 terminate_binding, ec);
4727 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4728 click_to_activate_binding,
4729 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004730 weston_compositor_add_touch_binding(ec, 0,
4731 touch_to_activate_binding,
4732 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004733 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4734 MODIFIER_SUPER | MODIFIER_ALT,
4735 surface_opacity_binding, NULL);
4736 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4737 MODIFIER_SUPER, zoom_axis_binding,
4738 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004739
4740 /* configurable bindings */
4741 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004742 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4743 zoom_key_binding, NULL);
4744 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4745 zoom_key_binding, NULL);
4746 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4747 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004748 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004749 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4750 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004751
4752 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4753 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4754 rotate_binding, NULL);
4755
Daniel Stone325fc2d2012-05-30 16:31:58 +01004756 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4757 shell);
4758 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4759 ec);
4760 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4761 backlight_binding, ec);
4762 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4763 ec);
4764 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4765 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004766 weston_compositor_add_key_binding(ec, KEY_K, mod,
4767 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004768 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4769 workspace_up_binding, shell);
4770 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4771 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004772 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4773 workspace_move_surface_up_binding,
4774 shell);
4775 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4776 workspace_move_surface_down_binding,
4777 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004778
4779 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4780 if (shell->workspaces.num > 1) {
4781 num_workspace_bindings = shell->workspaces.num;
4782 if (num_workspace_bindings > 6)
4783 num_workspace_bindings = 6;
4784 for (i = 0; i < num_workspace_bindings; i++)
4785 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4786 workspace_f_binding,
4787 shell);
4788 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004789
4790 /* Debug bindings */
4791 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4792 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004793}
4794
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004795WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004796module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004797 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004798{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004799 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004800 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004801 struct workspace **pws;
4802 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004803 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004804
Peter Huttererf3d62272013-08-08 11:57:05 +10004805 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004806 if (shell == NULL)
4807 return -1;
4808
Kristian Høgsberg75840622011-09-06 13:48:16 -04004809 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004810
4811 shell->destroy_listener.notify = shell_destroy;
4812 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004813 shell->idle_listener.notify = idle_handler;
4814 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4815 shell->wake_listener.notify = wake_handler;
4816 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004817 shell->show_input_panel_listener.notify = show_input_panels;
4818 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4819 shell->hide_input_panel_listener.notify = hide_input_panels;
4820 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004821 shell->update_input_panel_listener.notify = update_input_panels;
4822 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004823 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004824 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004825 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004826 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004827 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004828 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004829 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004830 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004831 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004832 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02004833 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004834
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004835 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004836
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004837 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4838 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004839 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4840 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004841 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004842
4843 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004844 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004845
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004846 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004847
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004848 for (i = 0; i < shell->workspaces.num; i++) {
4849 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4850 if (pws == NULL)
4851 return -1;
4852
4853 *pws = workspace_create();
4854 if (*pws == NULL)
4855 return -1;
4856 }
4857 activate_workspace(shell, 0);
4858
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004859 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004860 wl_list_init(&shell->workspaces.animation.link);
4861 shell->workspaces.animation.frame = animate_workspace_change_frame;
4862
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004863 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004864 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004865 return -1;
4866
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004867 if (wl_global_create(ec->wl_display,
4868 &desktop_shell_interface, 2,
4869 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004870 return -1;
4871
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004872 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4873 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004874 return -1;
4875
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004876 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004877 shell, bind_input_panel) == NULL)
4878 return -1;
4879
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004880 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4881 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004882 return -1;
4883
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004884 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004885
Xiong Zhang6b481422013-10-23 13:58:32 +08004886 setup_output_destroy_handler(ec, shell);
4887
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004888 loop = wl_display_get_event_loop(ec->wl_display);
4889 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004890
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004891 shell->screensaver.timer =
4892 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4893
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004894 wl_list_for_each(seat, &ec->seat_list, link)
4895 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004896
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004897 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004898
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004899 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004900
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004901 return 0;
4902}