blob: fdc163e4b8aafab3bc6104671678d5561a2cd313 [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;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600210 int unresponsive;
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øgsberg02bbabb2013-05-06 22:15:05 -0400378 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400379 if (shell->child.desktop_shell) {
380 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
381 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500382 weston_pointer_set_focus(pointer,
383 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400384 wl_fixed_from_int(0),
385 wl_fixed_from_int(0));
386 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300387}
388
389static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300390shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300391{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400392 if (grab->shsurf)
393 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300394
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700395 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300396}
397
398static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700399shell_touch_grab_start(struct shell_touch_grab *grab,
400 const struct weston_touch_grab_interface *interface,
401 struct shell_surface *shsurf,
402 struct weston_touch *touch)
403{
404 struct desktop_shell *shell = shsurf->shell;
405
406 grab->grab.interface = interface;
407 grab->shsurf = shsurf;
408 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
409 wl_signal_add(&shsurf->destroy_signal,
410 &grab->shsurf_destroy_listener);
411
412 grab->touch = touch;
413
414 weston_touch_start_grab(touch, &grab->grab);
415 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500416 weston_touch_set_focus(touch->seat,
417 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700418}
419
420static void
421shell_touch_grab_end(struct shell_touch_grab *grab)
422{
423 if (grab->shsurf)
424 wl_list_remove(&grab->shsurf_destroy_listener.link);
425
426 weston_touch_end_grab(grab->touch);
427}
428
429static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500430center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800431 struct weston_output *output);
432
Daniel Stone496ca172012-05-30 16:31:42 +0100433static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300434get_modifier(char *modifier)
435{
436 if (!modifier)
437 return MODIFIER_SUPER;
438
439 if (!strcmp("ctrl", modifier))
440 return MODIFIER_CTRL;
441 else if (!strcmp("alt", modifier))
442 return MODIFIER_ALT;
443 else if (!strcmp("super", modifier))
444 return MODIFIER_SUPER;
445 else
446 return MODIFIER_SUPER;
447}
448
Juan Zhaoe10d2792012-04-25 19:09:52 +0800449static enum animation_type
450get_animation_type(char *animation)
451{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800452 if (!strcmp("zoom", animation))
453 return ANIMATION_ZOOM;
454 else if (!strcmp("fade", animation))
455 return ANIMATION_FADE;
456 else
457 return ANIMATION_NONE;
458}
459
Alex Wu4539b082012-03-01 12:57:46 +0800460static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400461shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200462{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400463 struct weston_config_section *section;
464 int duration;
465 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200466
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400467 section = weston_config_get_section(shell->compositor->config,
468 "screensaver", NULL, NULL);
469 weston_config_section_get_string(section,
470 "path", &shell->screensaver.path, NULL);
471 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200472 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400473
474 section = weston_config_get_section(shell->compositor->config,
475 "shell", NULL, NULL);
476 weston_config_section_get_string(section,
477 "binding-modifier", &s, "super");
478 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200479 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400480 weston_config_section_get_string(section, "animation", &s, "none");
481 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200482 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700483 weston_config_section_get_string(section,
484 "startup-animation", &s, "fade");
485 shell->startup_animation_type = get_animation_type(s);
486 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400487 weston_config_section_get_uint(section, "num-workspaces",
488 &shell->workspaces.num,
489 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200490}
491
492static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200493focus_state_destroy(struct focus_state *state)
494{
495 wl_list_remove(&state->seat_destroy_listener.link);
496 wl_list_remove(&state->surface_destroy_listener.link);
497 free(state);
498}
499
500static void
501focus_state_seat_destroy(struct wl_listener *listener, void *data)
502{
503 struct focus_state *state = container_of(listener,
504 struct focus_state,
505 seat_destroy_listener);
506
507 wl_list_remove(&state->link);
508 focus_state_destroy(state);
509}
510
511static void
512focus_state_surface_destroy(struct wl_listener *listener, void *data)
513{
514 struct focus_state *state = container_of(listener,
515 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400516 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400517 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500518 struct weston_surface *main_surface, *next;
519 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200520
Pekka Paalanen01388e22013-04-25 13:57:44 +0300521 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
522
Kristian Høgsberge3778222012-07-31 17:29:30 -0400523 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500524 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
525 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400526 continue;
527
Jason Ekstranda7af7042013-10-12 22:38:11 -0500528 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400529 break;
530 }
531
Pekka Paalanen01388e22013-04-25 13:57:44 +0300532 /* if the focus was a sub-surface, activate its main surface */
533 if (main_surface != state->keyboard_focus)
534 next = main_surface;
535
Kristian Høgsberge3778222012-07-31 17:29:30 -0400536 if (next) {
537 shell = state->seat->compositor->shell_interface.shell;
538 activate(shell, next, state->seat);
539 } else {
540 wl_list_remove(&state->link);
541 focus_state_destroy(state);
542 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200543}
544
545static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400546focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200547{
Jonas Ådahl04769742012-06-13 00:01:24 +0200548 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200549
550 state = malloc(sizeof *state);
551 if (state == NULL)
552 return NULL;
553
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400554 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200555 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400556 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200557
558 state->seat_destroy_listener.notify = focus_state_seat_destroy;
559 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400560 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200561 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400562 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200563
564 return state;
565}
566
Jonas Ådahl8538b222012-08-29 22:13:03 +0200567static struct focus_state *
568ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
569{
570 struct workspace *ws = get_current_workspace(shell);
571 struct focus_state *state;
572
573 wl_list_for_each(state, &ws->focus_list, link)
574 if (state->seat == seat)
575 break;
576
577 if (&state->link == &ws->focus_list)
578 state = focus_state_create(seat, ws);
579
580 return state;
581}
582
Jonas Ådahl04769742012-06-13 00:01:24 +0200583static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400584restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200585{
586 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400587 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200588
589 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400590 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200591
Kristian Høgsberge3148752013-05-06 23:19:49 -0400592 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200593 }
594}
595
596static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200597replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
598 struct weston_seat *seat)
599{
600 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400601 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200602
603 wl_list_for_each(state, &ws->focus_list, link) {
604 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400605 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500606 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200607 return;
608 }
609 }
610}
611
612static void
613drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
614 struct weston_surface *surface)
615{
616 struct focus_state *state;
617
618 wl_list_for_each(state, &ws->focus_list, link)
619 if (state->keyboard_focus == surface)
620 state->keyboard_focus = NULL;
621}
622
623static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200624workspace_destroy(struct workspace *ws)
625{
Jonas Ådahl04769742012-06-13 00:01:24 +0200626 struct focus_state *state, *next;
627
628 wl_list_for_each_safe(state, next, &ws->focus_list, link)
629 focus_state_destroy(state);
630
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200631 free(ws);
632}
633
Jonas Ådahl04769742012-06-13 00:01:24 +0200634static void
635seat_destroyed(struct wl_listener *listener, void *data)
636{
637 struct weston_seat *seat = data;
638 struct focus_state *state, *next;
639 struct workspace *ws = container_of(listener,
640 struct workspace,
641 seat_destroyed_listener);
642
643 wl_list_for_each_safe(state, next, &ws->focus_list, link)
644 if (state->seat == seat)
645 wl_list_remove(&state->link);
646}
647
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200648static struct workspace *
649workspace_create(void)
650{
651 struct workspace *ws = malloc(sizeof *ws);
652 if (ws == NULL)
653 return NULL;
654
655 weston_layer_init(&ws->layer, NULL);
656
Jonas Ådahl04769742012-06-13 00:01:24 +0200657 wl_list_init(&ws->focus_list);
658 wl_list_init(&ws->seat_destroyed_listener.link);
659 ws->seat_destroyed_listener.notify = seat_destroyed;
660
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200661 return ws;
662}
663
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200664static int
665workspace_is_empty(struct workspace *ws)
666{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500667 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200668}
669
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200670static struct workspace *
671get_workspace(struct desktop_shell *shell, unsigned int index)
672{
673 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200674 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200675 pws += index;
676 return *pws;
677}
678
679static struct workspace *
680get_current_workspace(struct desktop_shell *shell)
681{
682 return get_workspace(shell, shell->workspaces.current);
683}
684
685static void
686activate_workspace(struct desktop_shell *shell, unsigned int index)
687{
688 struct workspace *ws;
689
690 ws = get_workspace(shell, index);
691 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
692
693 shell->workspaces.current = index;
694}
695
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200696static unsigned int
697get_output_height(struct weston_output *output)
698{
699 return abs(output->region.extents.y1 - output->region.extents.y2);
700}
701
702static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500703view_translate(struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200704{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500705 struct shell_surface *shsurf = get_shell_surface(view->surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200706 struct weston_transform *transform;
707
708 transform = &shsurf->workspace_transform;
709 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500710 wl_list_insert(view->geometry.transformation_list.prev,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200711 &shsurf->workspace_transform.link);
712
713 weston_matrix_init(&shsurf->workspace_transform.matrix);
714 weston_matrix_translate(&shsurf->workspace_transform.matrix,
715 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500716 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200717}
718
719static void
720workspace_translate_out(struct workspace *ws, double fraction)
721{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500722 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200723 unsigned int height;
724 double d;
725
Jason Ekstranda7af7042013-10-12 22:38:11 -0500726 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
727 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200728 d = height * fraction;
729
Jason Ekstranda7af7042013-10-12 22:38:11 -0500730 view_translate(view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200731 }
732}
733
734static void
735workspace_translate_in(struct workspace *ws, double fraction)
736{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500737 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200738 unsigned int height;
739 double d;
740
Jason Ekstranda7af7042013-10-12 22:38:11 -0500741 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
742 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200743
744 if (fraction > 0)
745 d = -(height - height * fraction);
746 else
747 d = height + height * fraction;
748
Jason Ekstranda7af7042013-10-12 22:38:11 -0500749 view_translate(view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200750 }
751}
752
753static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200754broadcast_current_workspace_state(struct desktop_shell *shell)
755{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700756 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200757
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700758 wl_resource_for_each(resource, &shell->workspaces.client_list)
759 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200760 shell->workspaces.current,
761 shell->workspaces.num);
762}
763
764static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200765reverse_workspace_change_animation(struct desktop_shell *shell,
766 unsigned int index,
767 struct workspace *from,
768 struct workspace *to)
769{
770 shell->workspaces.current = index;
771
772 shell->workspaces.anim_to = to;
773 shell->workspaces.anim_from = from;
774 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
775 shell->workspaces.anim_timestamp = 0;
776
Scott Moreau4272e632012-08-13 09:58:41 -0600777 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200778}
779
780static void
781workspace_deactivate_transforms(struct workspace *ws)
782{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500783 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200784 struct shell_surface *shsurf;
785
Jason Ekstranda7af7042013-10-12 22:38:11 -0500786 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
787 shsurf = get_shell_surface(view->surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200788 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
789 wl_list_remove(&shsurf->workspace_transform.link);
790 wl_list_init(&shsurf->workspace_transform.link);
791 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500792 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200793 }
794}
795
796static void
797finish_workspace_change_animation(struct desktop_shell *shell,
798 struct workspace *from,
799 struct workspace *to)
800{
Scott Moreau4272e632012-08-13 09:58:41 -0600801 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200802
803 wl_list_remove(&shell->workspaces.animation.link);
804 workspace_deactivate_transforms(from);
805 workspace_deactivate_transforms(to);
806 shell->workspaces.anim_to = NULL;
807
808 wl_list_remove(&shell->workspaces.anim_from->layer.link);
809}
810
811static void
812animate_workspace_change_frame(struct weston_animation *animation,
813 struct weston_output *output, uint32_t msecs)
814{
815 struct desktop_shell *shell =
816 container_of(animation, struct desktop_shell,
817 workspaces.animation);
818 struct workspace *from = shell->workspaces.anim_from;
819 struct workspace *to = shell->workspaces.anim_to;
820 uint32_t t;
821 double x, y;
822
823 if (workspace_is_empty(from) && workspace_is_empty(to)) {
824 finish_workspace_change_animation(shell, from, to);
825 return;
826 }
827
828 if (shell->workspaces.anim_timestamp == 0) {
829 if (shell->workspaces.anim_current == 0.0)
830 shell->workspaces.anim_timestamp = msecs;
831 else
832 shell->workspaces.anim_timestamp =
833 msecs -
834 /* Invers of movement function 'y' below. */
835 (asin(1.0 - shell->workspaces.anim_current) *
836 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
837 M_2_PI);
838 }
839
840 t = msecs - shell->workspaces.anim_timestamp;
841
842 /*
843 * x = [0, π/2]
844 * y(x) = sin(x)
845 */
846 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
847 y = sin(x);
848
849 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600850 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200851
852 workspace_translate_out(from, shell->workspaces.anim_dir * y);
853 workspace_translate_in(to, shell->workspaces.anim_dir * y);
854 shell->workspaces.anim_current = y;
855
Scott Moreau4272e632012-08-13 09:58:41 -0600856 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200858 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200859 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200860}
861
862static void
863animate_workspace_change(struct desktop_shell *shell,
864 unsigned int index,
865 struct workspace *from,
866 struct workspace *to)
867{
868 struct weston_output *output;
869
870 int dir;
871
872 if (index > shell->workspaces.current)
873 dir = -1;
874 else
875 dir = 1;
876
877 shell->workspaces.current = index;
878
879 shell->workspaces.anim_dir = dir;
880 shell->workspaces.anim_from = from;
881 shell->workspaces.anim_to = to;
882 shell->workspaces.anim_current = 0.0;
883 shell->workspaces.anim_timestamp = 0;
884
885 output = container_of(shell->compositor->output_list.next,
886 struct weston_output, link);
887 wl_list_insert(&output->animation_list,
888 &shell->workspaces.animation.link);
889
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200890 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200891
892 workspace_translate_in(to, 0);
893
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400894 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200895
Scott Moreau4272e632012-08-13 09:58:41 -0600896 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200897}
898
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200899static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200900update_workspace(struct desktop_shell *shell, unsigned int index,
901 struct workspace *from, struct workspace *to)
902{
903 shell->workspaces.current = index;
904 wl_list_insert(&from->layer.link, &to->layer.link);
905 wl_list_remove(&from->layer.link);
906}
907
908static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200909change_workspace(struct desktop_shell *shell, unsigned int index)
910{
911 struct workspace *from;
912 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200913
914 if (index == shell->workspaces.current)
915 return;
916
917 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500918 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200919 return;
920
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200921 from = get_current_workspace(shell);
922 to = get_workspace(shell, index);
923
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200924 if (shell->workspaces.anim_from == to &&
925 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200926 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200927 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200928 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200929 return;
930 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200931
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200932 if (shell->workspaces.anim_to != NULL)
933 finish_workspace_change_animation(shell,
934 shell->workspaces.anim_from,
935 shell->workspaces.anim_to);
936
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200937 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200938
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200939 if (workspace_is_empty(to) && workspace_is_empty(from))
940 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200941 else
942 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200943
944 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200945}
946
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200947static bool
948workspace_has_only(struct workspace *ws, struct weston_surface *surface)
949{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500950 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200951 struct wl_list *e;
952
953 if (wl_list_empty(list))
954 return false;
955
956 e = list->next;
957
958 if (e->next != list)
959 return false;
960
Jason Ekstranda7af7042013-10-12 22:38:11 -0500961 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200962}
963
964static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965move_view_to_workspace(struct desktop_shell *shell,
966 struct weston_view *view,
967 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +0200968{
969 struct workspace *from;
970 struct workspace *to;
971 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300972 struct weston_surface *focus;
973
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200975
976 if (workspace == shell->workspaces.current)
977 return;
978
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200979 if (workspace >= shell->workspaces.num)
980 workspace = shell->workspaces.num - 1;
981
Jonas Ådahle9d22502012-08-29 22:13:01 +0200982 from = get_current_workspace(shell);
983 to = get_workspace(shell, workspace);
984
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985 wl_list_remove(&view->layer_link);
986 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200987
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300989 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
990 if (!seat->keyboard)
991 continue;
992
993 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400995 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300996 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200997
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200999}
1000
1001static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001002take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001003 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001004 unsigned int index)
1005{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001006 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001008 struct shell_surface *shsurf;
1009 struct workspace *from;
1010 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001011 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001012
Pekka Paalanen01388e22013-04-25 13:57:44 +03001013 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 view = get_default_view(surface);
1015 if (view == NULL ||
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001016 index == shell->workspaces.current)
1017 return;
1018
1019 from = get_current_workspace(shell);
1020 to = get_workspace(shell, index);
1021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 wl_list_remove(&view->layer_link);
1023 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001024
Jonas Ådahle9d22502012-08-29 22:13:01 +02001025 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001026 drop_focus_state(shell, from, surface);
1027
1028 if (shell->workspaces.anim_from == to &&
1029 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001030 wl_list_remove(&to->layer.link);
1031 wl_list_insert(from->layer.link.prev, &to->layer.link);
1032
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001033 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001034 broadcast_current_workspace_state(shell);
1035
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001036 return;
1037 }
1038
1039 if (shell->workspaces.anim_to != NULL)
1040 finish_workspace_change_animation(shell,
1041 shell->workspaces.anim_from,
1042 shell->workspaces.anim_to);
1043
1044 if (workspace_is_empty(from) &&
1045 workspace_has_only(to, surface))
1046 update_workspace(shell, index, from, to);
1047 else {
1048 shsurf = get_shell_surface(surface);
1049 if (wl_list_empty(&shsurf->workspace_transform.link))
1050 wl_list_insert(&shell->workspaces.anim_sticky_list,
1051 &shsurf->workspace_transform.link);
1052
1053 animate_workspace_change(shell, index, from, to);
1054 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001055
1056 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001057
1058 state = ensure_focus_state(shell, seat);
1059 if (state != NULL)
1060 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001061}
1062
1063static void
1064workspace_manager_move_surface(struct wl_client *client,
1065 struct wl_resource *resource,
1066 struct wl_resource *surface_resource,
1067 uint32_t workspace)
1068{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001069 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001070 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001071 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001072 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001074
Pekka Paalanen01388e22013-04-25 13:57:44 +03001075 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001076 view = get_default_view(main_surface);
1077 if (!view)
1078 return;
1079 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001080}
1081
1082static const struct workspace_manager_interface workspace_manager_implementation = {
1083 workspace_manager_move_surface,
1084};
1085
1086static void
1087unbind_resource(struct wl_resource *resource)
1088{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001089 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001090}
1091
1092static void
1093bind_workspace_manager(struct wl_client *client,
1094 void *data, uint32_t version, uint32_t id)
1095{
1096 struct desktop_shell *shell = data;
1097 struct wl_resource *resource;
1098
Jason Ekstranda85118c2013-06-27 20:17:02 -05001099 resource = wl_resource_create(client,
1100 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001101
1102 if (resource == NULL) {
1103 weston_log("couldn't add workspace manager object");
1104 return;
1105 }
1106
Jason Ekstranda85118c2013-06-27 20:17:02 -05001107 wl_resource_set_implementation(resource,
1108 &workspace_manager_implementation,
1109 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001110 wl_list_insert(&shell->workspaces.client_list,
1111 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001112
1113 workspace_manager_send_state(resource,
1114 shell->workspaces.current,
1115 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001116}
1117
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001118static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001119touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1120 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1121{
1122}
1123
1124static void
1125touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1126{
1127 struct shell_touch_grab *shell_grab = container_of(grab,
1128 struct shell_touch_grab,
1129 grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001130
1131 if (grab->touch->seat->num_tp == 0)
1132 shell_touch_grab_end(shell_grab);
Rusty Lynch1084da52013-08-15 09:10:08 -07001133}
1134
1135static void
1136touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1137 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1138{
1139 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1140 struct shell_surface *shsurf = move->base.shsurf;
1141 struct weston_surface *es;
1142 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1143 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1144
1145 if (!shsurf)
1146 return;
1147
1148 es = shsurf->surface;
1149
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 weston_view_configure(shsurf->view, dx, dy,
1151 shsurf->view->geometry.width,
1152 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001153
1154 weston_compositor_schedule_repaint(es->compositor);
1155}
1156
1157static const struct weston_touch_grab_interface touch_move_grab_interface = {
1158 touch_move_grab_down,
1159 touch_move_grab_up,
1160 touch_move_grab_motion,
1161};
1162
1163static int
1164surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1165{
1166 struct weston_touch_move_grab *move;
1167
1168 if (!shsurf)
1169 return -1;
1170
1171 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1172 return 0;
1173
1174 move = malloc(sizeof *move);
1175 if (!move)
1176 return -1;
1177
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001179 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001181 seat->touch->grab_y;
1182
1183 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1184 seat->touch);
1185
1186 return 0;
1187}
1188
1189static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001190noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001191{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001192}
1193
1194static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001195move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001196{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001197 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001198 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001199 struct shell_surface *shsurf = move->base.shsurf;
Daniel Stone37816df2012-05-16 18:45:18 +01001200 int dx = wl_fixed_to_int(pointer->x + move->dx);
1201 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001202
1203 if (!shsurf)
1204 return;
1205
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 weston_view_configure(shsurf->view, dx, dy,
1207 shsurf->view->geometry.width,
1208 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001209
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001211}
1212
1213static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001214move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001215 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001216{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001217 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1218 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001219 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001220 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001221
Daniel Stone4dbadb12012-05-30 16:31:51 +01001222 if (pointer->button_count == 0 &&
1223 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001224 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001225 free(grab);
1226 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001227}
1228
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001229static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001230 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001231 move_grab_motion,
1232 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001233};
1234
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001235static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001236surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001237{
1238 struct weston_move_grab *move;
1239
1240 if (!shsurf)
1241 return -1;
1242
1243 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1244 return 0;
1245
1246 move = malloc(sizeof *move);
1247 if (!move)
1248 return -1;
1249
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001251 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001253 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001254
1255 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001256 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001257
1258 return 0;
1259}
1260
1261static void
1262shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1263 struct wl_resource *seat_resource, uint32_t serial)
1264{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001265 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001266 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001267 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001268
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001269 if (seat->pointer &&
1270 seat->pointer->button_count > 0 &&
1271 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001273 if ((surface == shsurf->surface) &&
1274 (surface_move(shsurf, seat) < 0))
1275 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001276 } else if (seat->touch &&
1277 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001279 if ((surface == shsurf->surface) &&
1280 (surface_touch_move(shsurf, seat) < 0))
1281 wl_resource_post_no_memory(resource);
1282 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001283}
1284
1285struct weston_resize_grab {
1286 struct shell_grab base;
1287 uint32_t edges;
1288 int32_t width, height;
1289};
1290
1291static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001292resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001293{
1294 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001295 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001296 struct shell_surface *shsurf = resize->base.shsurf;
1297 int32_t width, height;
1298 wl_fixed_t from_x, from_y;
1299 wl_fixed_t to_x, to_y;
1300
1301 if (!shsurf)
1302 return;
1303
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 weston_view_from_global_fixed(shsurf->view,
1305 pointer->grab_x, pointer->grab_y,
1306 &from_x, &from_y);
1307 weston_view_from_global_fixed(shsurf->view,
1308 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001309
1310 width = resize->width;
1311 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1312 width += wl_fixed_to_int(from_x - to_x);
1313 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1314 width += wl_fixed_to_int(to_x - from_x);
1315 }
1316
1317 height = resize->height;
1318 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1319 height += wl_fixed_to_int(from_y - to_y);
1320 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1321 height += wl_fixed_to_int(to_y - from_y);
1322 }
1323
1324 shsurf->client->send_configure(shsurf->surface,
1325 resize->edges, width, height);
1326}
1327
1328static void
1329send_configure(struct weston_surface *surface,
1330 uint32_t edges, int32_t width, int32_t height)
1331{
1332 struct shell_surface *shsurf = get_shell_surface(surface);
1333
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001334 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001335 edges, width, height);
1336}
1337
1338static const struct weston_shell_client shell_client = {
1339 send_configure
1340};
1341
1342static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001343resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001344 uint32_t time, uint32_t button, uint32_t state_w)
1345{
1346 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001347 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001348 enum wl_pointer_button_state state = state_w;
1349
1350 if (pointer->button_count == 0 &&
1351 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1352 shell_grab_end(&resize->base);
1353 free(grab);
1354 }
1355}
1356
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001357static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001358 noop_grab_focus,
1359 resize_grab_motion,
1360 resize_grab_button,
1361};
1362
Giulio Camuffob8366642013-04-25 13:57:46 +03001363/*
1364 * Returns the bounding box of a surface and all its sub-surfaces,
1365 * in the surface coordinates system. */
1366static void
1367surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1368 int32_t *y, int32_t *w, int32_t *h) {
1369 pixman_region32_t region;
1370 pixman_box32_t *box;
1371 struct weston_subsurface *subsurface;
1372
1373 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374 surface->width,
1375 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001376
1377 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1378 pixman_region32_union_rect(&region, &region,
1379 subsurface->position.x,
1380 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381 subsurface->surface->width,
1382 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001383 }
1384
1385 box = pixman_region32_extents(&region);
1386 if (x)
1387 *x = box->x1;
1388 if (y)
1389 *y = box->y1;
1390 if (w)
1391 *w = box->x2 - box->x1;
1392 if (h)
1393 *h = box->y2 - box->y1;
1394
1395 pixman_region32_fini(&region);
1396}
1397
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001398static int
1399surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001400 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001401{
1402 struct weston_resize_grab *resize;
1403
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001404 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1405 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001406 return 0;
1407
1408 if (edges == 0 || edges > 15 ||
1409 (edges & 3) == 3 || (edges & 12) == 12)
1410 return 0;
1411
1412 resize = malloc(sizeof *resize);
1413 if (!resize)
1414 return -1;
1415
1416 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001417 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1418 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001419
1420 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001421 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001422
1423 return 0;
1424}
1425
1426static void
1427shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1428 struct wl_resource *seat_resource, uint32_t serial,
1429 uint32_t edges)
1430{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001431 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001432 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001433 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001434
1435 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1436 return;
1437
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001439 if (seat->pointer->button_count == 0 ||
1440 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001441 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001442 return;
1443
Kristian Høgsberge3148752013-05-06 23:19:49 -04001444 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001445 wl_resource_post_no_memory(resource);
1446}
1447
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001448static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001449end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1450
1451static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001452busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001453{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001454 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001455 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001457 wl_fixed_t sx, sy;
1458
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 view = weston_compositor_pick_view(pointer->seat->compositor,
1460 pointer->x, pointer->y,
1461 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001462
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001464 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001465}
1466
1467static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001468busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001469{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001470}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001471
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001472static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001473busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001474 uint32_t time, uint32_t button, uint32_t state)
1475{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001476 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001477 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001478 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001479
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001480 if (shsurf && button == BTN_LEFT && state) {
1481 activate(shsurf->shell, shsurf->surface, seat);
1482 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001483 } else if (shsurf && button == BTN_RIGHT && state) {
1484 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001485 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001486 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001487}
1488
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001489static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001490 busy_cursor_grab_focus,
1491 busy_cursor_grab_motion,
1492 busy_cursor_grab_button,
1493};
1494
1495static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001496set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001497{
1498 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001499
1500 grab = malloc(sizeof *grab);
1501 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001502 return;
1503
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001504 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1505 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001506}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001507
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001508static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001509end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001510{
1511 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1512
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001513 if (grab->grab.interface == &busy_cursor_grab_interface &&
1514 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001515 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001516 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001517 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001518}
1519
Scott Moreau9521d5e2012-04-19 13:06:17 -06001520static void
1521ping_timer_destroy(struct shell_surface *shsurf)
1522{
1523 if (!shsurf || !shsurf->ping_timer)
1524 return;
1525
1526 if (shsurf->ping_timer->source)
1527 wl_event_source_remove(shsurf->ping_timer->source);
1528
1529 free(shsurf->ping_timer);
1530 shsurf->ping_timer = NULL;
1531}
1532
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001533static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001534ping_timeout_handler(void *data)
1535{
1536 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001537 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001538
Scott Moreau9521d5e2012-04-19 13:06:17 -06001539 /* Client is not responding */
1540 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001541
1542 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543 if (seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001544 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001545
1546 return 1;
1547}
1548
1549static void
1550ping_handler(struct weston_surface *surface, uint32_t serial)
1551{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001552 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001553 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001554 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001555
1556 if (!shsurf)
1557 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001558 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001559 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001560
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001561 if (shsurf->surface == shsurf->shell->grab_surface)
1562 return;
1563
Scott Moreauff1db4a2012-04-17 19:06:18 -06001564 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001565 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001566 if (!shsurf->ping_timer)
1567 return;
1568
1569 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001570 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1571 shsurf->ping_timer->source =
1572 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1573 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1574
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001575 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001576 }
1577}
1578
1579static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001580handle_pointer_focus(struct wl_listener *listener, void *data)
1581{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001582 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001584 struct weston_compositor *compositor;
1585 struct shell_surface *shsurf;
1586 uint32_t serial;
1587
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001589 return;
1590
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 compositor = view->surface->compositor;
1592 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001593
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001594 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001595 set_busy_cursor(shsurf, pointer);
1596 } else {
1597 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001599 }
1600}
1601
1602static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001603create_pointer_focus_listener(struct weston_seat *seat)
1604{
1605 struct wl_listener *listener;
1606
Kristian Høgsberge3148752013-05-06 23:19:49 -04001607 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001608 return;
1609
1610 listener = malloc(sizeof *listener);
1611 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001612 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001613}
1614
1615static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001616shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1617 uint32_t serial)
1618{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001619 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001620 struct weston_seat *seat;
1621 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001622
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001623 if (shsurf->ping_timer == NULL)
1624 /* Just ignore unsolicited pong. */
1625 return;
1626
Scott Moreauff1db4a2012-04-17 19:06:18 -06001627 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001628 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001629 wl_list_for_each(seat, &ec->seat_list, link) {
1630 if(seat->pointer)
1631 end_busy_cursor(shsurf, seat->pointer);
1632 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001633 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001634 }
1635}
1636
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001637static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001638set_title(struct shell_surface *shsurf, const char *title)
1639{
1640 free(shsurf->title);
1641 shsurf->title = strdup(title);
1642}
1643
1644static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001645shell_surface_set_title(struct wl_client *client,
1646 struct wl_resource *resource, const char *title)
1647{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001648 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001649
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001650 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001651}
1652
1653static void
1654shell_surface_set_class(struct wl_client *client,
1655 struct wl_resource *resource, const char *class)
1656{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001657 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001658
1659 free(shsurf->class);
1660 shsurf->class = strdup(class);
1661}
1662
Juan Zhao96879df2012-02-07 08:45:41 +08001663static struct weston_output *
1664get_default_output(struct weston_compositor *compositor)
1665{
1666 return container_of(compositor->output_list.next,
1667 struct weston_output, link);
1668}
1669
Alex Wu4539b082012-03-01 12:57:46 +08001670static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001671restore_output_mode(struct weston_output *output)
1672{
Hardening57388e42013-09-18 23:56:36 +02001673 if (output->current_mode != output->original_mode ||
1674 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001675 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001676 output->original_mode,
1677 output->original_scale,
1678 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001679}
1680
1681static void
1682restore_all_output_modes(struct weston_compositor *compositor)
1683{
1684 struct weston_output *output;
1685
1686 wl_list_for_each(output, &compositor->output_list, link)
1687 restore_output_mode(output);
1688}
1689
1690static void
Alex Wu4539b082012-03-01 12:57:46 +08001691shell_unset_fullscreen(struct shell_surface *shsurf)
1692{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001693 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001694 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001695 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1696 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001697 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001698 }
Alex Wu4539b082012-03-01 12:57:46 +08001699 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1700 shsurf->fullscreen.framerate = 0;
1701 wl_list_remove(&shsurf->fullscreen.transform.link);
1702 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001703 if (shsurf->fullscreen.black_view)
1704 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1705 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001706 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001707 weston_view_set_position(shsurf->view,
1708 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001709 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001710 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001711 &shsurf->rotation.transform.link);
1712 shsurf->saved_rotation_valid = false;
1713 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001714
1715 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716 wl_list_remove(&shsurf->view->layer_link);
1717 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001718}
1719
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001720static void
1721shell_unset_maximized(struct shell_surface *shsurf)
1722{
1723 struct workspace *ws;
1724 /* undo all maximized things here */
1725 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001726 weston_view_set_position(shsurf->view,
1727 shsurf->saved_x,
1728 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001729
1730 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001731 wl_list_insert(&shsurf->view->geometry.transformation_list,
1732 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001733 shsurf->saved_rotation_valid = false;
1734 }
1735
1736 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001737 wl_list_remove(&shsurf->view->layer_link);
1738 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001739}
1740
Pekka Paalanen98262232011-12-01 10:42:22 +02001741static int
1742reset_shell_surface_type(struct shell_surface *surface)
1743{
1744 switch (surface->type) {
1745 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001746 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001747 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001748 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001749 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001750 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001751 case SHELL_SURFACE_NONE:
1752 case SHELL_SURFACE_TOPLEVEL:
1753 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001754 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001755 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001756 break;
1757 }
1758
1759 surface->type = SHELL_SURFACE_NONE;
1760 return 0;
1761}
1762
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001763static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001764set_surface_type(struct shell_surface *shsurf)
1765{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001766 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001768
1769 reset_shell_surface_type(shsurf);
1770
1771 shsurf->type = shsurf->next_type;
1772 shsurf->next_type = SHELL_SURFACE_NONE;
1773
1774 switch (shsurf->type) {
1775 case SHELL_SURFACE_TOPLEVEL:
1776 break;
1777 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778 if (pev)
1779 weston_view_set_position(shsurf->view,
1780 pev->geometry.x + shsurf->transient.x,
1781 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001782 break;
1783
1784 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001785 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 shsurf->saved_x = shsurf->view->geometry.x;
1787 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001788 shsurf->saved_position_valid = true;
1789
1790 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1791 wl_list_remove(&shsurf->rotation.transform.link);
1792 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001794 shsurf->saved_rotation_valid = true;
1795 }
1796 break;
1797
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001798 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799 weston_view_set_position(shsurf->view, shsurf->transient.x,
1800 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001801 break;
1802
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001803 default:
1804 break;
1805 }
1806}
1807
1808static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001809set_toplevel(struct shell_surface *shsurf)
1810{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001811 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001812}
1813
1814static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001815shell_surface_set_toplevel(struct wl_client *client,
1816 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001817{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001818 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001819
Tiago Vignattibc052c92012-04-19 16:18:18 +03001820 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001821}
1822
1823static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001824set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001825 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001826{
1827 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001828 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001829 shsurf->transient.x = x;
1830 shsurf->transient.y = y;
1831 shsurf->transient.flags = flags;
1832 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1833}
1834
1835static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001836shell_surface_set_transient(struct wl_client *client,
1837 struct wl_resource *resource,
1838 struct wl_resource *parent_resource,
1839 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001840{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001841 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001842 struct weston_surface *parent =
1843 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001844
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001845 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001846}
1847
Tiago Vignattibe143262012-04-16 17:31:41 +03001848static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001849shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001850{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001851 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001852}
1853
1854static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001855get_output_panel_height(struct desktop_shell *shell,
1856 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001857{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08001859 int panel_height = 0;
1860
1861 if (!output)
1862 return 0;
1863
Jason Ekstranda7af7042013-10-12 22:38:11 -05001864 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1865 if (view->surface->output == output) {
1866 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001867 break;
1868 }
1869 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001870
Juan Zhao96879df2012-02-07 08:45:41 +08001871 return panel_height;
1872}
1873
1874static void
1875shell_surface_set_maximized(struct wl_client *client,
1876 struct wl_resource *resource,
1877 struct wl_resource *output_resource )
1878{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001879 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001880 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001881 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001882 uint32_t edges = 0, panel_height = 0;
1883
1884 /* get the default output, if the client set it as NULL
1885 check whether the ouput is available */
1886 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001887 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001888 else if (es->output)
1889 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001890 else
1891 shsurf->output = get_default_output(es->compositor);
1892
Tiago Vignattibe143262012-04-16 17:31:41 +03001893 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001894 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001895 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001896
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001897 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001898 shsurf->output->width,
1899 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001900
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001901 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001902}
1903
Alex Wu21858432012-04-01 20:13:08 +08001904static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001905black_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 +08001906
Jason Ekstranda7af7042013-10-12 22:38:11 -05001907static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08001908create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001909 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001910 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001911{
1912 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001913 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08001914
1915 surface = weston_surface_create(ec);
1916 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001917 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001918 return NULL;
1919 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001920 view = weston_view_create(surface);
1921 if (surface == NULL) {
1922 weston_log("no memory\n");
1923 weston_surface_destroy(surface);
1924 return NULL;
1925 }
Alex Wu4539b082012-03-01 12:57:46 +08001926
Alex Wu21858432012-04-01 20:13:08 +08001927 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001928 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001929 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001930 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001931 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001932 pixman_region32_fini(&surface->input);
1933 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001934
Jason Ekstranda7af7042013-10-12 22:38:11 -05001935 weston_view_configure(view, x, y, w, h);
1936
1937 return view;
Alex Wu4539b082012-03-01 12:57:46 +08001938}
1939
1940/* Create black surface and append it to the associated fullscreen surface.
1941 * Handle size dismatch and positioning according to the method. */
1942static void
1943shell_configure_fullscreen(struct shell_surface *shsurf)
1944{
1945 struct weston_output *output = shsurf->fullscreen_output;
1946 struct weston_surface *surface = shsurf->surface;
1947 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001948 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001949 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001950
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001951 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1952 restore_output_mode(output);
1953
Jason Ekstranda7af7042013-10-12 22:38:11 -05001954 if (!shsurf->fullscreen.black_view)
1955 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001956 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001957 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001958 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001959 output->width,
1960 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001961
Jason Ekstranda7af7042013-10-12 22:38:11 -05001962 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
1963 wl_list_insert(&shsurf->view->layer_link,
1964 &shsurf->fullscreen.black_view->layer_link);
1965 shsurf->fullscreen.black_view->surface->output = output;
1966 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08001967
Jason Ekstranda7af7042013-10-12 22:38:11 -05001968 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03001969 &surf_width, &surf_height);
1970
Alex Wu4539b082012-03-01 12:57:46 +08001971 switch (shsurf->fullscreen.type) {
1972 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001973 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001974 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001975 break;
1976 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001977 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001978 if (output->width == surf_width &&
1979 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980 weston_view_set_position(shsurf->view,
1981 output->x - surf_x,
1982 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001983 break;
1984 }
1985
Alex Wu4539b082012-03-01 12:57:46 +08001986 matrix = &shsurf->fullscreen.transform.matrix;
1987 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001988
Scott Moreau1bad5db2012-08-18 01:04:05 -06001989 output_aspect = (float) output->width /
1990 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001991 /* XXX: Use surf_width and surf_height here? */
1992 surface_aspect = (float) surface->width /
1993 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001994 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001995 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001996 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001997 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001998 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001999 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002000
Alex Wu4539b082012-03-01 12:57:46 +08002001 weston_matrix_scale(matrix, scale, scale, 1);
2002 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002003 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002004 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002005 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2006 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002007 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002008
Alex Wu4539b082012-03-01 12:57:46 +08002009 break;
2010 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002011 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002012 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02002013 surf_width * surface->buffer_scale,
2014 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002015 shsurf->fullscreen.framerate};
2016
Hardening57388e42013-09-18 23:56:36 +02002017 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
2018 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019 weston_view_set_position(shsurf->view,
2020 output->x - surf_x,
2021 output->y - surf_y);
2022 weston_view_configure(shsurf->fullscreen.black_view,
2023 output->x - surf_x,
2024 output->y - surf_y,
2025 output->width,
2026 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002027 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002028 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002029 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002030 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002031 }
Alex Wubd3354b2012-04-17 17:20:49 +08002032 }
Alex Wu4539b082012-03-01 12:57:46 +08002033 break;
2034 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002036 break;
2037 default:
2038 break;
2039 }
2040}
2041
2042/* make the fullscreen and black surface at the top */
2043static void
2044shell_stack_fullscreen(struct shell_surface *shsurf)
2045{
Alex Wubd3354b2012-04-17 17:20:49 +08002046 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002047 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002048
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 wl_list_remove(&shsurf->view->layer_link);
2050 wl_list_insert(&shell->fullscreen_layer.view_list,
2051 &shsurf->view->layer_link);
2052 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002053
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 if (!shsurf->fullscreen.black_view)
2055 shsurf->fullscreen.black_view =
2056 create_black_surface(shsurf->surface->compositor,
2057 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002058 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002059 output->width,
2060 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002061
Jason Ekstranda7af7042013-10-12 22:38:11 -05002062 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2063 wl_list_insert(&shsurf->view->layer_link,
2064 &shsurf->fullscreen.black_view->layer_link);
2065 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002066}
2067
2068static void
2069shell_map_fullscreen(struct shell_surface *shsurf)
2070{
Alex Wu4539b082012-03-01 12:57:46 +08002071 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002072 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002073}
2074
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002075static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002076set_fullscreen(struct shell_surface *shsurf,
2077 uint32_t method,
2078 uint32_t framerate,
2079 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002080{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002081 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002082
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002083 if (output)
2084 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002085 else if (es->output)
2086 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002087 else
2088 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002089
Alex Wu4539b082012-03-01 12:57:46 +08002090 shsurf->fullscreen_output = shsurf->output;
2091 shsurf->fullscreen.type = method;
2092 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002093 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002094
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002095 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002096 shsurf->output->width,
2097 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002098}
2099
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002100static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002101shell_surface_set_fullscreen(struct wl_client *client,
2102 struct wl_resource *resource,
2103 uint32_t method,
2104 uint32_t framerate,
2105 struct wl_resource *output_resource)
2106{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002107 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002108 struct weston_output *output;
2109
2110 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002111 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002112 else
2113 output = NULL;
2114
2115 set_fullscreen(shsurf, method, framerate, output);
2116}
2117
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002118static void
2119set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2120{
2121 /* XXX: using the same fields for transient type */
2122 shsurf->transient.x = x;
2123 shsurf->transient.y = y;
2124 shsurf->transient.flags = flags;
2125 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2126}
2127
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002128static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002129
2130static void
2131destroy_shell_seat(struct wl_listener *listener, void *data)
2132{
2133 struct shell_seat *shseat =
2134 container_of(listener,
2135 struct shell_seat, seat_destroy_listener);
2136 struct shell_surface *shsurf, *prev = NULL;
2137
2138 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002139 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002140 shseat->popup_grab.client = NULL;
2141
2142 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2143 shsurf->popup.shseat = NULL;
2144 if (prev) {
2145 wl_list_init(&prev->popup.grab_link);
2146 }
2147 prev = shsurf;
2148 }
2149 wl_list_init(&prev->popup.grab_link);
2150 }
2151
2152 wl_list_remove(&shseat->seat_destroy_listener.link);
2153 free(shseat);
2154}
2155
2156static struct shell_seat *
2157create_shell_seat(struct weston_seat *seat)
2158{
2159 struct shell_seat *shseat;
2160
2161 shseat = calloc(1, sizeof *shseat);
2162 if (!shseat) {
2163 weston_log("no memory to allocate shell seat\n");
2164 return NULL;
2165 }
2166
2167 shseat->seat = seat;
2168 wl_list_init(&shseat->popup_grab.surfaces_list);
2169
2170 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2171 wl_signal_add(&seat->destroy_signal,
2172 &shseat->seat_destroy_listener);
2173
2174 return shseat;
2175}
2176
2177static struct shell_seat *
2178get_shell_seat(struct weston_seat *seat)
2179{
2180 struct wl_listener *listener;
2181
2182 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2183 if (listener == NULL)
2184 return create_shell_seat(seat);
2185
2186 return container_of(listener,
2187 struct shell_seat, seat_destroy_listener);
2188}
2189
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002190static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002191popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002192{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002193 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002195 struct shell_seat *shseat =
2196 container_of(grab, struct shell_seat, popup_grab.grab);
2197 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002198 wl_fixed_t sx, sy;
2199
Jason Ekstranda7af7042013-10-12 22:38:11 -05002200 view = weston_compositor_pick_view(pointer->seat->compositor,
2201 pointer->x, pointer->y,
2202 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002203
Jason Ekstranda7af7042013-10-12 22:38:11 -05002204 if (view && view->surface->resource &&
2205 wl_resource_get_client(view->surface->resource) == client) {
2206 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002207 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002208 weston_pointer_set_focus(pointer, NULL,
2209 wl_fixed_from_int(0),
2210 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002211 }
2212}
2213
2214static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002215popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002216{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002217 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002218 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002219 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002220
Neil Roberts96d790e2013-09-19 17:32:00 +01002221 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 weston_view_from_global_fixed(pointer->focus,
2223 pointer->x, pointer->y,
2224 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002225 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002226 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002227}
2228
2229static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002230popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002231 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002232{
2233 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002234 struct shell_seat *shseat =
2235 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002236 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002237 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002238 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002239 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002240
Neil Roberts96d790e2013-09-19 17:32:00 +01002241 resource_list = &grab->pointer->focus_resource_list;
2242 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002243 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002244 wl_resource_for_each(resource, resource_list) {
2245 wl_pointer_send_button(resource, serial,
2246 time, button, state);
2247 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002248 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002249 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002250 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002251 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002252 }
2253
Daniel Stone4dbadb12012-05-30 16:31:51 +01002254 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002255 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002256}
2257
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002258static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002259 popup_grab_focus,
2260 popup_grab_motion,
2261 popup_grab_button,
2262};
2263
2264static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002265popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002266{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002267 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002268 struct shell_seat *shseat =
2269 container_of(grab, struct shell_seat, popup_grab.grab);
2270 struct shell_surface *shsurf;
2271 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002272
2273 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002274 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002275 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002276 shseat->popup_grab.grab.interface = NULL;
2277 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002278 /* Send the popup_done event to all the popups open */
2279 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002280 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002281 shsurf->popup.shseat = NULL;
2282 if (prev) {
2283 wl_list_init(&prev->popup.grab_link);
2284 }
2285 prev = shsurf;
2286 }
2287 wl_list_init(&prev->popup.grab_link);
2288 wl_list_init(&shseat->popup_grab.surfaces_list);
2289 }
2290}
2291
2292static void
2293add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2294{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002295 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002296
2297 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002298 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002299 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002300 /* We must make sure here that this popup was opened after
2301 * a mouse press, and not just by moving around with other
2302 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002303 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002304 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002305
Rob Bradforddfe31052013-06-26 19:49:11 +01002306 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002307 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002308 } else {
2309 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002310 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002311}
2312
2313static void
2314remove_popup_grab(struct shell_surface *shsurf)
2315{
2316 struct shell_seat *shseat = shsurf->popup.shseat;
2317
2318 wl_list_remove(&shsurf->popup.grab_link);
2319 wl_list_init(&shsurf->popup.grab_link);
2320 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002321 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002322 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002323 }
2324}
2325
2326static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002327shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002328{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002329 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002330 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002331
Jason Ekstranda7af7042013-10-12 22:38:11 -05002332 shsurf->surface->output = parent_view->output;
2333 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002334
Jason Ekstranda7af7042013-10-12 22:38:11 -05002335 weston_view_set_transform_parent(shsurf->view, parent_view);
2336 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2337 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002338
Kristian Høgsberge3148752013-05-06 23:19:49 -04002339 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002340 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002341 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002342 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002343 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002344 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002345}
2346
2347static void
2348shell_surface_set_popup(struct wl_client *client,
2349 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002350 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002351 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002352 struct wl_resource *parent_resource,
2353 int32_t x, int32_t y, uint32_t flags)
2354{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002355 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002356
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002357 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002358 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002359 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002360 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002361 shsurf->popup.x = x;
2362 shsurf->popup.y = y;
2363}
2364
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002365static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002366 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002367 shell_surface_move,
2368 shell_surface_resize,
2369 shell_surface_set_toplevel,
2370 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002371 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002372 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002373 shell_surface_set_maximized,
2374 shell_surface_set_title,
2375 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002376};
2377
2378static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002379destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002380{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002381 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2382
Giulio Camuffo5085a752013-03-25 21:42:45 +01002383 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2384 remove_popup_grab(shsurf);
2385 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002386
Alex Wubd3354b2012-04-17 17:20:49 +08002387 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002388 shell_surface_is_top_fullscreen(shsurf))
2389 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002390
Jason Ekstranda7af7042013-10-12 22:38:11 -05002391 if (shsurf->fullscreen.black_view)
2392 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002393
Alex Wubd3354b2012-04-17 17:20:49 +08002394 /* As destroy_resource() use wl_list_for_each_safe(),
2395 * we can always remove the listener.
2396 */
2397 wl_list_remove(&shsurf->surface_destroy_listener.link);
2398 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002399 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002400 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002401
Jason Ekstranda7af7042013-10-12 22:38:11 -05002402 weston_view_destroy(shsurf->view);
2403
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002404 wl_list_remove(&shsurf->link);
2405 free(shsurf);
2406}
2407
2408static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002409shell_destroy_shell_surface(struct wl_resource *resource)
2410{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002411 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002412
2413 destroy_shell_surface(shsurf);
2414}
2415
2416static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002417shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002418{
2419 struct shell_surface *shsurf = container_of(listener,
2420 struct shell_surface,
2421 surface_destroy_listener);
2422
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002423 if (shsurf->resource)
2424 wl_resource_destroy(shsurf->resource);
2425 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002426 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002427}
2428
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002429static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002430shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002431
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002432static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002433get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002434{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002435 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002436 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002437 else
2438 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002439}
2440
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002441static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002442create_shell_surface(void *shell, struct weston_surface *surface,
2443 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002444{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002445 struct shell_surface *shsurf;
2446
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002447 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002448 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002449 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002450 }
2451
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002452 shsurf = calloc(1, sizeof *shsurf);
2453 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002454 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002455 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002456 }
2457
Jason Ekstranda7af7042013-10-12 22:38:11 -05002458 shsurf->view = weston_view_create(surface);
2459 if (!shsurf->view) {
2460 weston_log("no memory to allocate shell surface\n");
2461 free(shsurf);
2462 return NULL;
2463 }
2464
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002465 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002466 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002467
Tiago Vignattibc052c92012-04-19 16:18:18 +03002468 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002469 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002470 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002471 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002472 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002473 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2474 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002475 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002476 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002477 wl_list_init(&shsurf->fullscreen.transform.link);
2478
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002479 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002480 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002481 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002482 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002483
2484 /* init link so its safe to always remove it in destroy_shell_surface */
2485 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002486 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002487
Pekka Paalanen460099f2012-01-20 16:48:25 +02002488 /* empty when not in use */
2489 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002490 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002491
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002492 wl_list_init(&shsurf->workspace_transform.link);
2493
Pekka Paalanen98262232011-12-01 10:42:22 +02002494 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002495 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002496
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002497 shsurf->client = client;
2498
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002499 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002500}
2501
Jason Ekstranda7af7042013-10-12 22:38:11 -05002502static struct weston_view *
2503get_primary_view(void *shell, struct shell_surface *shsurf)
2504{
2505 return shsurf->view;
2506}
2507
Tiago Vignattibc052c92012-04-19 16:18:18 +03002508static void
2509shell_get_shell_surface(struct wl_client *client,
2510 struct wl_resource *resource,
2511 uint32_t id,
2512 struct wl_resource *surface_resource)
2513{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002514 struct weston_surface *surface =
2515 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002516 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002517 struct shell_surface *shsurf;
2518
2519 if (get_shell_surface(surface)) {
2520 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002521 WL_DISPLAY_ERROR_INVALID_OBJECT,
2522 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002523 return;
2524 }
2525
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002526 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002527 if (!shsurf) {
2528 wl_resource_post_error(surface_resource,
2529 WL_DISPLAY_ERROR_INVALID_OBJECT,
2530 "surface->configure already set");
2531 return;
2532 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002533
Jason Ekstranda85118c2013-06-27 20:17:02 -05002534 shsurf->resource =
2535 wl_resource_create(client,
2536 &wl_shell_surface_interface, 1, id);
2537 wl_resource_set_implementation(shsurf->resource,
2538 &shell_surface_implementation,
2539 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002540}
2541
2542static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002543 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002544};
2545
Kristian Høgsberg07937562011-04-12 17:25:42 -04002546static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002547shell_fade(struct desktop_shell *shell, enum fade_type type);
2548
2549static int
2550screensaver_timeout(void *data)
2551{
2552 struct desktop_shell *shell = data;
2553
2554 shell_fade(shell, FADE_OUT);
2555
2556 return 1;
2557}
2558
2559static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002560handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002561{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002562 struct desktop_shell *shell =
2563 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002564
Pekka Paalanen18027e52011-12-02 16:31:49 +02002565 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002566
2567 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002568 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002569}
2570
2571static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002572launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002573{
2574 if (shell->screensaver.binding)
2575 return;
2576
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002577 if (!shell->screensaver.path) {
2578 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002579 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002580 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002581
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002582 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002583 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002584 return;
2585 }
2586
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002587 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002588 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002589 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002590 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002591}
2592
2593static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002594terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002595{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002596 if (shell->screensaver.process.pid == 0)
2597 return;
2598
2599 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002600}
2601
2602static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002603configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002604{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002605 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002606
Giulio Camuffo184df502013-02-21 11:29:21 +01002607 if (width == 0)
2608 return;
2609
Jason Ekstranda7af7042013-10-12 22:38:11 -05002610 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2611 if (v->output == ev->output && v != ev) {
2612 weston_view_unmap(v);
2613 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002614 }
2615 }
2616
Jason Ekstranda7af7042013-10-12 22:38:11 -05002617 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002618
Jason Ekstranda7af7042013-10-12 22:38:11 -05002619 if (wl_list_empty(&ev->layer_link)) {
2620 wl_list_insert(&layer->view_list, &ev->layer_link);
2621 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002622 }
2623}
2624
2625static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002626background_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 -04002627{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002628 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002629 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002630
Jason Ekstranda7af7042013-10-12 22:38:11 -05002631 view = container_of(es->views.next, struct weston_view, surface_link);
2632
2633 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002634}
2635
2636static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002637desktop_shell_set_background(struct wl_client *client,
2638 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002639 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002640 struct wl_resource *surface_resource)
2641{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002642 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002643 struct weston_surface *surface =
2644 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002645 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002646
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002647 if (surface->configure) {
2648 wl_resource_post_error(surface_resource,
2649 WL_DISPLAY_ERROR_INVALID_OBJECT,
2650 "surface role already assigned");
2651 return;
2652 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002653
Jason Ekstranda7af7042013-10-12 22:38:11 -05002654 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2655 weston_view_destroy(view);
2656 view = weston_view_create(surface);
2657
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002658 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002659 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002660 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002661 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002662 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002663 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002664 surface->output->width,
2665 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002666}
2667
2668static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002669panel_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 -04002670{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002671 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002673
Jason Ekstranda7af7042013-10-12 22:38:11 -05002674 view = container_of(es->views.next, struct weston_view, surface_link);
2675
2676 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002677}
2678
2679static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002680desktop_shell_set_panel(struct wl_client *client,
2681 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002682 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002683 struct wl_resource *surface_resource)
2684{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002685 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002686 struct weston_surface *surface =
2687 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002688 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002689
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002690 if (surface->configure) {
2691 wl_resource_post_error(surface_resource,
2692 WL_DISPLAY_ERROR_INVALID_OBJECT,
2693 "surface role already assigned");
2694 return;
2695 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002696
Jason Ekstranda7af7042013-10-12 22:38:11 -05002697 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2698 weston_view_destroy(view);
2699 view = weston_view_create(surface);
2700
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002701 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002702 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002703 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002704 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002705 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002706 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002707 surface->output->width,
2708 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002709}
2710
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002711static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002712lock_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 -04002713{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002714 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 struct weston_view *view;
2716
2717 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002718
Giulio Camuffo184df502013-02-21 11:29:21 +01002719 if (width == 0)
2720 return;
2721
Jason Ekstranda7af7042013-10-12 22:38:11 -05002722 surface->width = width;
2723 surface->height = height;
2724 view->geometry.width = width;
2725 view->geometry.height = height;
2726 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002727
2728 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002729 wl_list_insert(&shell->lock_layer.view_list,
2730 &view->layer_link);
2731 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002732 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002733 }
2734}
2735
2736static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002737handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002738{
Tiago Vignattibe143262012-04-16 17:31:41 +03002739 struct desktop_shell *shell =
2740 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002741
Martin Minarik6d118362012-06-07 18:01:59 +02002742 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002743 shell->lock_surface = NULL;
2744}
2745
2746static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002747desktop_shell_set_lock_surface(struct wl_client *client,
2748 struct wl_resource *resource,
2749 struct wl_resource *surface_resource)
2750{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002751 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002752 struct weston_surface *surface =
2753 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002754
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002755 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002756
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002757 if (!shell->locked)
2758 return;
2759
Pekka Paalanen98262232011-12-01 10:42:22 +02002760 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002761
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002762 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002763 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002764 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002765
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002766 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002767 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002768}
2769
2770static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002771resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002772{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002773 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002774
Pekka Paalanen77346a62011-11-30 16:26:35 +02002775 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002776
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002777 wl_list_remove(&shell->lock_layer.link);
2778 wl_list_insert(&shell->compositor->cursor_layer.link,
2779 &shell->fullscreen_layer.link);
2780 wl_list_insert(&shell->fullscreen_layer.link,
2781 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002782 if (shell->showing_input_panels) {
2783 wl_list_insert(&shell->panel_layer.link,
2784 &shell->input_panel_layer.link);
2785 wl_list_insert(&shell->input_panel_layer.link,
2786 &ws->layer.link);
2787 } else {
2788 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2789 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002790
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002791 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002792
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002793 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002794 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002795 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002796}
2797
2798static void
2799desktop_shell_unlock(struct wl_client *client,
2800 struct wl_resource *resource)
2801{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002802 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002803
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002804 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002805
2806 if (shell->locked)
2807 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002808}
2809
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002810static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002811desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002812 struct wl_resource *resource,
2813 struct wl_resource *surface_resource)
2814{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002815 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002816
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002817 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002818}
2819
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002820static void
2821desktop_shell_desktop_ready(struct wl_client *client,
2822 struct wl_resource *resource)
2823{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002824 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002825
2826 shell_fade_startup(shell);
2827}
2828
Kristian Høgsberg75840622011-09-06 13:48:16 -04002829static const struct desktop_shell_interface desktop_shell_implementation = {
2830 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002831 desktop_shell_set_panel,
2832 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002833 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002834 desktop_shell_set_grab_surface,
2835 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002836};
2837
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002838static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002839get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002840{
2841 struct shell_surface *shsurf;
2842
2843 shsurf = get_shell_surface(surface);
2844 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002845 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002846 return shsurf->type;
2847}
2848
Kristian Høgsberg75840622011-09-06 13:48:16 -04002849static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002850move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002851{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002852 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002853 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002854 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002855 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002856
Pekka Paalanen01388e22013-04-25 13:57:44 +03002857 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002858 if (surface == NULL)
2859 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002860
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002861 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002862 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2863 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002864 return;
2865
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002866 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002867}
2868
2869static void
Neil Robertsaba0f252013-10-03 16:43:05 +01002870touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
2871{
2872 struct weston_surface *focus =
2873 (struct weston_surface *) seat->touch->focus;
2874 struct weston_surface *surface;
2875 struct shell_surface *shsurf;
2876
2877 surface = weston_surface_get_main_surface(focus);
2878 if (surface == NULL)
2879 return;
2880
2881 shsurf = get_shell_surface(surface);
2882 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2883 shsurf->type == SHELL_SURFACE_MAXIMIZED)
2884 return;
2885
2886 surface_touch_move(shsurf, (struct weston_seat *) seat);
2887}
2888
2889static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002890resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002891{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002892 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002893 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002894 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002895 uint32_t edges = 0;
2896 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002897 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002898
Pekka Paalanen01388e22013-04-25 13:57:44 +03002899 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002900 if (surface == NULL)
2901 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002902
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002903 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002904 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2905 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002906 return;
2907
Jason Ekstranda7af7042013-10-12 22:38:11 -05002908 weston_view_from_global(shsurf->view,
2909 wl_fixed_to_int(seat->pointer->grab_x),
2910 wl_fixed_to_int(seat->pointer->grab_y),
2911 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002912
Jason Ekstranda7af7042013-10-12 22:38:11 -05002913 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002914 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002915 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002916 edges |= 0;
2917 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002918 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002919
Jason Ekstranda7af7042013-10-12 22:38:11 -05002920 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002921 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002922 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002923 edges |= 0;
2924 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002925 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002926
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002927 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002928}
2929
2930static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002931surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002932 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002933{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002934 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002935 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002936 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002937 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002938 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002939
Pekka Paalanen01388e22013-04-25 13:57:44 +03002940 /* XXX: broken for windows containing sub-surfaces */
2941 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002942 if (surface == NULL)
2943 return;
2944
2945 shsurf = get_shell_surface(surface);
2946 if (!shsurf)
2947 return;
2948
Jason Ekstranda7af7042013-10-12 22:38:11 -05002949 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002950
Jason Ekstranda7af7042013-10-12 22:38:11 -05002951 if (shsurf->view->alpha > 1.0)
2952 shsurf->view->alpha = 1.0;
2953 if (shsurf->view->alpha < step)
2954 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002955
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002957 weston_surface_damage(surface);
2958}
2959
2960static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002961do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002962 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002963{
Daniel Stone37816df2012-05-16 18:45:18 +01002964 struct weston_seat *ws = (struct weston_seat *) seat;
2965 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002966 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002967 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002968
2969 wl_list_for_each(output, &compositor->output_list, link) {
2970 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002971 wl_fixed_to_double(seat->pointer->x),
2972 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002973 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002974 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002975 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002976 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002977 increment = -output->zoom.increment;
2978 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002979 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002980 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002981 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002982 else
2983 increment = 0;
2984
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002985 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002986
Scott Moreaue6603982012-06-11 13:07:51 -06002987 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002988 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002989 else if (output->zoom.level > output->zoom.max_level)
2990 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002991 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002992 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002993 output->disable_planes++;
2994 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002995
Scott Moreaue6603982012-06-11 13:07:51 -06002996 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002997
Jason Ekstranda7af7042013-10-12 22:38:11 -05002998 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002999 }
3000 }
3001}
3002
Scott Moreauccbf29d2012-02-22 14:21:41 -07003003static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003004zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003005 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003006{
3007 do_zoom(seat, time, 0, axis, value);
3008}
3009
3010static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003011zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003012 void *data)
3013{
3014 do_zoom(seat, time, key, 0, 0);
3015}
3016
3017static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003018terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003019 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003020{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003021 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003022
Daniel Stone325fc2d2012-05-30 16:31:58 +01003023 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003024}
3025
3026static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003027rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003028{
3029 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003030 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003031 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003032 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003033 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003034
3035 if (!shsurf)
3036 return;
3037
Jason Ekstranda7af7042013-10-12 22:38:11 -05003038 cx = 0.5f * shsurf->view->geometry.width;
3039 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003040
Daniel Stone37816df2012-05-16 18:45:18 +01003041 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3042 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003043 r = sqrtf(dx * dx + dy * dy);
3044
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003045 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003046 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003047
3048 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003049 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003050 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003051
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003052 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003053 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003054
3055 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003056 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003057 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003058 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003059 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003060
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003061 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003062 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003063 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003064 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003065 wl_list_init(&shsurf->rotation.transform.link);
3066 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003067 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003068 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003069
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003070 /* We need to adjust the position of the surface
3071 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003072 cposx = shsurf->view->geometry.x + cx;
3073 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003074 dposx = rotate->center.x - cposx;
3075 dposy = rotate->center.y - cposy;
3076 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003077 weston_view_set_position(shsurf->view,
3078 shsurf->view->geometry.x + dposx,
3079 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003080 }
3081
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003082 /* Repaint implies weston_surface_update_transform(), which
3083 * lazily applies the damage due to rotation update.
3084 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003085 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003086}
3087
3088static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003089rotate_grab_button(struct weston_pointer_grab *grab,
3090 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003091{
3092 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003093 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003094 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003095 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003096 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003097
Daniel Stone4dbadb12012-05-30 16:31:51 +01003098 if (pointer->button_count == 0 &&
3099 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003100 if (shsurf)
3101 weston_matrix_multiply(&shsurf->rotation.rotation,
3102 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003103 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003104 free(rotate);
3105 }
3106}
3107
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003108static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003109 noop_grab_focus,
3110 rotate_grab_motion,
3111 rotate_grab_button,
3112};
3113
3114static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003115surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003116{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003117 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003118 float dx, dy;
3119 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003120
Pekka Paalanen460099f2012-01-20 16:48:25 +02003121 rotate = malloc(sizeof *rotate);
3122 if (!rotate)
3123 return;
3124
Jason Ekstranda7af7042013-10-12 22:38:11 -05003125 weston_view_to_global_float(surface->view,
3126 surface->view->geometry.width * 0.5f,
3127 surface->view->geometry.height * 0.5f,
3128 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003129
Daniel Stone37816df2012-05-16 18:45:18 +01003130 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3131 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003132 r = sqrtf(dx * dx + dy * dy);
3133 if (r > 20.0f) {
3134 struct weston_matrix inverse;
3135
3136 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003137 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003138 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003139
3140 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003141 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003142 } else {
3143 weston_matrix_init(&surface->rotation.rotation);
3144 weston_matrix_init(&rotate->rotation);
3145 }
3146
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003147 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3148 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003149}
3150
3151static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003152rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003153 void *data)
3154{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003155 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003156 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003157 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003158 struct shell_surface *surface;
3159
Pekka Paalanen01388e22013-04-25 13:57:44 +03003160 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003161 if (base_surface == NULL)
3162 return;
3163
3164 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003165 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3166 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003167 return;
3168
3169 surface_rotate(surface, seat);
3170}
3171
3172static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003173lower_fullscreen_layer(struct desktop_shell *shell)
3174{
3175 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003176 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003177
3178 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003179 wl_list_for_each_reverse_safe(view, prev,
3180 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003181 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003182 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003183}
3184
3185static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003186activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003187 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003188{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003189 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003190 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003191 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003192 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003193
Pekka Paalanen01388e22013-04-25 13:57:44 +03003194 main_surface = weston_surface_get_main_surface(es);
3195
Daniel Stone37816df2012-05-16 18:45:18 +01003196 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003197
Jonas Ådahl8538b222012-08-29 22:13:03 +02003198 state = ensure_focus_state(shell, seat);
3199 if (state == NULL)
3200 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003201
3202 state->keyboard_focus = es;
3203 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003204 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003205
Pekka Paalanen01388e22013-04-25 13:57:44 +03003206 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003207 case SHELL_SURFACE_FULLSCREEN:
3208 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003209 shell_stack_fullscreen(get_shell_surface(main_surface));
3210 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08003211 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003212 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003213 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003214 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003215 main_view = get_default_view(main_surface);
3216 if (main_view)
3217 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003218 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003219 }
3220}
3221
Alex Wu21858432012-04-01 20:13:08 +08003222/* no-op func for checking black surface */
3223static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003224black_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 +08003225{
3226}
3227
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003228static bool
Alex Wu21858432012-04-01 20:13:08 +08003229is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3230{
3231 if (es->configure == black_surface_configure) {
3232 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003233 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003234 return true;
3235 }
3236 return false;
3237}
3238
Kristian Høgsberg75840622011-09-06 13:48:16 -04003239static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003240activate_binding(struct weston_seat *seat,
3241 struct desktop_shell *shell,
3242 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003243{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003244 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003245
Alex Wu9c35e6b2012-03-05 14:13:13 +08003246 if (!focus)
3247 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003248 focus = seat->pointer->focus->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003249
Pekka Paalanen01388e22013-04-25 13:57:44 +03003250 if (is_black_surface(focus, &main_surface))
3251 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003252
Pekka Paalanen01388e22013-04-25 13:57:44 +03003253 main_surface = weston_surface_get_main_surface(focus);
3254 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003255 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003256
Neil Robertsa28c6932013-10-03 16:43:04 +01003257 activate(shell, focus, seat);
3258}
3259
3260static void
3261click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3262 void *data)
3263{
3264 if (seat->pointer->grab != &seat->pointer->default_grab)
3265 return;
3266
3267 activate_binding(seat, data,
3268 (struct weston_surface *) seat->pointer->focus);
3269}
3270
3271static void
3272touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3273{
3274 if (seat->touch->grab != &seat->touch->default_grab)
3275 return;
3276
3277 activate_binding(seat, data,
3278 (struct weston_surface *) seat->touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003279}
3280
3281static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003282lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003283{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003284 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003285
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003286 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003287 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003288 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003289 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003290
3291 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003292
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003293 /* Hide all surfaces by removing the fullscreen, panel and
3294 * toplevel layers. This way nothing else can show or receive
3295 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003296
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003297 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003298 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003299 if (shell->showing_input_panels)
3300 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003301 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003302 wl_list_insert(&shell->compositor->cursor_layer.link,
3303 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003304
Pekka Paalanen77346a62011-11-30 16:26:35 +02003305 launch_screensaver(shell);
3306
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003307 /* TODO: disable bindings that should not work while locked. */
3308
3309 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003310}
3311
3312static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003313unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003314{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003315 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003316 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003317 return;
3318 }
3319
3320 /* If desktop-shell client has gone away, unlock immediately. */
3321 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003322 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003323 return;
3324 }
3325
3326 if (shell->prepare_event_sent)
3327 return;
3328
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003329 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003330 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003331}
3332
3333static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003334shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003335{
3336 struct desktop_shell *shell = data;
3337
3338 shell->fade.animation = NULL;
3339
3340 switch (shell->fade.type) {
3341 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003342 weston_surface_destroy(shell->fade.view->surface);
3343 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003344 break;
3345 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003346 lock(shell);
3347 break;
3348 }
3349}
3350
Jason Ekstranda7af7042013-10-12 22:38:11 -05003351static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003352shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003353{
3354 struct weston_compositor *compositor = shell->compositor;
3355 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003356 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003357
3358 surface = weston_surface_create(compositor);
3359 if (!surface)
3360 return NULL;
3361
Jason Ekstranda7af7042013-10-12 22:38:11 -05003362 view = weston_view_create(surface);
3363 if (!view) {
3364 weston_surface_destroy(surface);
3365 return NULL;
3366 }
3367
3368 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003369 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003370 wl_list_insert(&compositor->fade_layer.view_list,
3371 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003372 pixman_region32_init(&surface->input);
3373
Jason Ekstranda7af7042013-10-12 22:38:11 -05003374 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003375}
3376
3377static void
3378shell_fade(struct desktop_shell *shell, enum fade_type type)
3379{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003380 float tint;
3381
3382 switch (type) {
3383 case FADE_IN:
3384 tint = 0.0;
3385 break;
3386 case FADE_OUT:
3387 tint = 1.0;
3388 break;
3389 default:
3390 weston_log("shell: invalid fade type\n");
3391 return;
3392 }
3393
3394 shell->fade.type = type;
3395
Jason Ekstranda7af7042013-10-12 22:38:11 -05003396 if (shell->fade.view == NULL) {
3397 shell->fade.view = shell_fade_create_surface(shell);
3398 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003399 return;
3400
Jason Ekstranda7af7042013-10-12 22:38:11 -05003401 shell->fade.view->alpha = 1.0 - tint;
3402 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003403 }
3404
3405 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003406 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003407 else
3408 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003409 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003410 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003411 shell_fade_done, shell);
3412}
3413
3414static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003415do_shell_fade_startup(void *data)
3416{
3417 struct desktop_shell *shell = data;
3418
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003419 if (shell->startup_animation_type == ANIMATION_FADE)
3420 shell_fade(shell, FADE_IN);
3421 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003422 weston_surface_destroy(shell->fade.view->surface);
3423 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003424 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003425}
3426
3427static void
3428shell_fade_startup(struct desktop_shell *shell)
3429{
3430 struct wl_event_loop *loop;
3431
3432 if (!shell->fade.startup_timer)
3433 return;
3434
3435 wl_event_source_remove(shell->fade.startup_timer);
3436 shell->fade.startup_timer = NULL;
3437
3438 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3439 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3440}
3441
3442static int
3443fade_startup_timeout(void *data)
3444{
3445 struct desktop_shell *shell = data;
3446
3447 shell_fade_startup(shell);
3448 return 0;
3449}
3450
3451static void
3452shell_fade_init(struct desktop_shell *shell)
3453{
3454 /* Make compositor output all black, and wait for the desktop-shell
3455 * client to signal it is ready, then fade in. The timer triggers a
3456 * fade-in, in case the desktop-shell client takes too long.
3457 */
3458
3459 struct wl_event_loop *loop;
3460
Jason Ekstranda7af7042013-10-12 22:38:11 -05003461 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003462 weston_log("%s: warning: fade surface already exists\n",
3463 __func__);
3464 return;
3465 }
3466
Jason Ekstranda7af7042013-10-12 22:38:11 -05003467 shell->fade.view = shell_fade_create_surface(shell);
3468 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003469 return;
3470
Jason Ekstranda7af7042013-10-12 22:38:11 -05003471 weston_view_update_transform(shell->fade.view);
3472 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003473
3474 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3475 shell->fade.startup_timer =
3476 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3477 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3478}
3479
3480static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003481idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003482{
3483 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003484 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003485
3486 shell_fade(shell, FADE_OUT);
3487 /* lock() is called from shell_fade_done() */
3488}
3489
3490static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003491wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003492{
3493 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003494 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003495
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003496 unlock(shell);
3497}
3498
3499static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003500show_input_panels(struct wl_listener *listener, void *data)
3501{
3502 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003503 container_of(listener, struct desktop_shell,
3504 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003505 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003506
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003507 shell->text_input.surface = (struct weston_surface*)data;
3508
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003509 if (shell->showing_input_panels)
3510 return;
3511
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003512 shell->showing_input_panels = true;
3513
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003514 if (!shell->locked)
3515 wl_list_insert(&shell->panel_layer.link,
3516 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003517
Jason Ekstranda7af7042013-10-12 22:38:11 -05003518 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003519 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003520 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003521 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003522 wl_list_insert(&shell->input_panel_layer.view_list,
3523 &ipsurf->view->layer_link);
3524 weston_view_geometry_dirty(ipsurf->view);
3525 weston_view_update_transform(ipsurf->view);
3526 weston_surface_damage(ipsurf->surface);
3527 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
3528 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003529 }
3530}
3531
3532static void
3533hide_input_panels(struct wl_listener *listener, void *data)
3534{
3535 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003536 container_of(listener, struct desktop_shell,
3537 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003538 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003539
Jan Arne Petersen61381972013-01-31 15:52:21 +01003540 if (!shell->showing_input_panels)
3541 return;
3542
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003543 shell->showing_input_panels = false;
3544
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003545 if (!shell->locked)
3546 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003547
Jason Ekstranda7af7042013-10-12 22:38:11 -05003548 wl_list_for_each_safe(view, next,
3549 &shell->input_panel_layer.view_list, layer_link)
3550 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003551}
3552
3553static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003554update_input_panels(struct wl_listener *listener, void *data)
3555{
3556 struct desktop_shell *shell =
3557 container_of(listener, struct desktop_shell,
3558 update_input_panel_listener);
3559
3560 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3561}
3562
3563static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003564center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003565{
Giulio Camuffob8366642013-04-25 13:57:46 +03003566 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003567 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003568
Jason Ekstranda7af7042013-10-12 22:38:11 -05003569 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03003570
3571 x = output->x + (output->width - width) / 2 - surf_x / 2;
3572 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003573
Jason Ekstranda7af7042013-10-12 22:38:11 -05003574 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003575}
3576
3577static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003578weston_view_set_initial_position(struct weston_view *view,
3579 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003580{
3581 struct weston_compositor *compositor = shell->compositor;
3582 int ix = 0, iy = 0;
3583 int range_x, range_y;
3584 int dx, dy, x, y, panel_height;
3585 struct weston_output *output, *target_output = NULL;
3586 struct weston_seat *seat;
3587
3588 /* As a heuristic place the new window on the same output as the
3589 * pointer. Falling back to the output containing 0, 0.
3590 *
3591 * TODO: Do something clever for touch too?
3592 */
3593 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003594 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003595 ix = wl_fixed_to_int(seat->pointer->x);
3596 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003597 break;
3598 }
3599 }
3600
3601 wl_list_for_each(output, &compositor->output_list, link) {
3602 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3603 target_output = output;
3604 break;
3605 }
3606 }
3607
3608 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003609 weston_view_set_position(view, 10 + random() % 400,
3610 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003611 return;
3612 }
3613
3614 /* Valid range within output where the surface will still be onscreen.
3615 * If this is negative it means that the surface is bigger than
3616 * output.
3617 */
3618 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003619 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003620 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05003621 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003622
Rob Bradford4cb88c72012-08-13 15:18:44 +01003623 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003624 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003625 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003626 dx = 0;
3627
3628 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003629 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003630 else
3631 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003632
3633 x = target_output->x + dx;
3634 y = target_output->y + dy;
3635
Jason Ekstranda7af7042013-10-12 22:38:11 -05003636 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003637}
3638
3639static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003640map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003641 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003642{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003643 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003644 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003645 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003646 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003647 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003648 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003649
Jason Ekstranda7af7042013-10-12 22:38:11 -05003650 shsurf->view->geometry.width = width;
3651 shsurf->view->geometry.height = height;
3652 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003653
3654 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003655 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02003656 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003657 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003658 break;
Alex Wu4539b082012-03-01 12:57:46 +08003659 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003660 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003661 shell_map_fullscreen(shsurf);
3662 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003663 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003664 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003665 panel_height = get_output_panel_height(shell, shsurf->output);
3666 surface_subsurfaces_boundingbox(shsurf->surface,
3667 &surf_x, &surf_y, NULL, NULL);
3668 weston_view_set_position(shsurf->view,
3669 shsurf->output->x - surf_x,
3670 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003671 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003672 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003673 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003674 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003675 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003676 weston_view_set_position(shsurf->view,
3677 shsurf->view->geometry.x + sx,
3678 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003679 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003680 default:
3681 ;
3682 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003683
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003684 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003685 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003686 case SHELL_SURFACE_POPUP:
3687 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003688 /* TODO: Handle a parent with multiple views */
3689 parent = get_default_view(shsurf->parent);
3690 if (parent) {
3691 wl_list_remove(&shsurf->view->layer_link);
3692 wl_list_insert(parent->layer_link.prev,
3693 &shsurf->view->layer_link);
3694 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003695 break;
Alex Wu4539b082012-03-01 12:57:46 +08003696 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003697 case SHELL_SURFACE_NONE:
3698 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003699 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003700 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003701 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003702 wl_list_remove(&shsurf->view->layer_link);
3703 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003704 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003705 }
3706
Jason Ekstranda7af7042013-10-12 22:38:11 -05003707 if (shsurf->type != SHELL_SURFACE_NONE) {
3708 weston_view_update_transform(shsurf->view);
3709 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
3710 shsurf->surface->output = shsurf->output;
3711 shsurf->view->output = shsurf->output;
3712 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003713 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003714
Jason Ekstranda7af7042013-10-12 22:38:11 -05003715 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003716 /* XXX: xwayland's using the same fields for transient type */
3717 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003718 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003719 if (shsurf->transient.flags ==
3720 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3721 break;
3722 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003723 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003724 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003725 if (!shell->locked) {
3726 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003727 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01003728 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003729 break;
3730 default:
3731 break;
3732 }
3733
Jason Ekstranda7af7042013-10-12 22:38:11 -05003734 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003735 {
3736 switch (shell->win_animation_type) {
3737 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003738 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003739 break;
3740 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003741 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003742 break;
3743 default:
3744 break;
3745 }
3746 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003747}
3748
3749static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003750configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003751 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003752{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003753 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3754 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003755 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03003756 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003757
Pekka Paalanen77346a62011-11-30 16:26:35 +02003758 shsurf = get_shell_surface(surface);
3759 if (shsurf)
3760 surface_type = shsurf->type;
3761
Jason Ekstranda7af7042013-10-12 22:38:11 -05003762 /* TODO:
3763 * This should probably be changed to be more shell_surface
3764 * dependent
3765 */
3766 wl_list_for_each(view, &surface->views, surface_link)
3767 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003768
3769 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003770 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003771 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003772 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003773 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003774 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003775 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003776 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3777 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003778 shsurf->view->geometry.x = shsurf->output->x - surf_x;
3779 shsurf->view->geometry.y = shsurf->output->y +
3780 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003781 break;
Alex Wu4539b082012-03-01 12:57:46 +08003782 case SHELL_SURFACE_TOPLEVEL:
3783 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003784 default:
3785 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003786 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003787
Alex Wu4539b082012-03-01 12:57:46 +08003788 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003789 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003790 wl_list_for_each(view, &surface->views, surface_link)
3791 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003792
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003793 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003794 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003795 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003796}
3797
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003798static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003799shell_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 +03003800{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003801 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003802 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003803
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003804 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003805
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003806 if (!weston_surface_is_mapped(es) &&
3807 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003808 remove_popup_grab(shsurf);
3809 }
3810
Giulio Camuffo184df502013-02-21 11:29:21 +01003811 if (width == 0)
3812 return;
3813
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003814 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003815 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003816 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003817 type_changed = 1;
3818 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003819
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003820 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003821 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003822 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05003823 shsurf->view->geometry.width != width ||
3824 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003825 float from_x, from_y;
3826 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003827
Jason Ekstranda7af7042013-10-12 22:38:11 -05003828 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
3829 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003830 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003831 shsurf->view->geometry.x + to_x - from_x,
3832 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003833 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003834 }
3835}
3836
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003837static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003838
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003839static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003840desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003841{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003842 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003843 struct desktop_shell *shell =
3844 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003845
3846 shell->child.process.pid = 0;
3847 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003848
3849 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3850 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003851 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003852 shell->child.deathstamp = time;
3853 shell->child.deathcount = 0;
3854 }
3855
3856 shell->child.deathcount++;
3857 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003858 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003859 return;
3860 }
3861
Martin Minarik6d118362012-06-07 18:01:59 +02003862 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003863 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003864 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003865}
3866
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003867static void
3868launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003869{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003870 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003871 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003872
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003873 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003874 &shell->child.process,
3875 shell_exe,
3876 desktop_shell_sigchld);
3877
3878 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003879 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003880}
3881
3882static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003883bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3884{
Tiago Vignattibe143262012-04-16 17:31:41 +03003885 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003886 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003887
Jason Ekstranda85118c2013-06-27 20:17:02 -05003888 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3889 if (resource)
3890 wl_resource_set_implementation(resource, &shell_implementation,
3891 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003892}
3893
Kristian Høgsberg75840622011-09-06 13:48:16 -04003894static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003895unbind_desktop_shell(struct wl_resource *resource)
3896{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003897 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003898
3899 if (shell->locked)
3900 resume_desktop(shell);
3901
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003902 shell->child.desktop_shell = NULL;
3903 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003904}
3905
3906static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003907bind_desktop_shell(struct wl_client *client,
3908 void *data, uint32_t version, uint32_t id)
3909{
Tiago Vignattibe143262012-04-16 17:31:41 +03003910 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003911 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003912
Jason Ekstranda85118c2013-06-27 20:17:02 -05003913 resource = wl_resource_create(client, &desktop_shell_interface,
3914 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003915
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003916 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003917 wl_resource_set_implementation(resource,
3918 &desktop_shell_implementation,
3919 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003920 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003921
3922 if (version < 2)
3923 shell_fade_startup(shell);
3924
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003925 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003926 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003927
3928 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3929 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003930 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003931}
3932
Pekka Paalanen6e168112011-11-24 11:34:05 +02003933static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003934screensaver_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 -04003935{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003936 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003937 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003938
Giulio Camuffo184df502013-02-21 11:29:21 +01003939 if (width == 0)
3940 return;
3941
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003942 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003943 if (!shell->locked)
3944 return;
3945
Jason Ekstranda7af7042013-10-12 22:38:11 -05003946 view = container_of(surface->views.next, struct weston_view, surface_link);
3947 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003948
Jason Ekstranda7af7042013-10-12 22:38:11 -05003949 if (wl_list_empty(&view->layer_link)) {
3950 wl_list_insert(shell->lock_layer.view_list.prev,
3951 &view->layer_link);
3952 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003953 wl_event_source_timer_update(shell->screensaver.timer,
3954 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003955 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003956 }
3957}
3958
3959static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003960screensaver_set_surface(struct wl_client *client,
3961 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003962 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003963 struct wl_resource *output_resource)
3964{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003965 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003966 struct weston_surface *surface =
3967 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003968 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003969 struct weston_view *view, *next;
3970
3971 /* Make sure we only have one view */
3972 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3973 weston_view_destroy(view);
3974 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003975
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003976 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003977 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003978 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003979}
3980
3981static const struct screensaver_interface screensaver_implementation = {
3982 screensaver_set_surface
3983};
3984
3985static void
3986unbind_screensaver(struct wl_resource *resource)
3987{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003988 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003989
Pekka Paalanen77346a62011-11-30 16:26:35 +02003990 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003991}
3992
3993static void
3994bind_screensaver(struct wl_client *client,
3995 void *data, uint32_t version, uint32_t id)
3996{
Tiago Vignattibe143262012-04-16 17:31:41 +03003997 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003998 struct wl_resource *resource;
3999
Jason Ekstranda85118c2013-06-27 20:17:02 -05004000 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004001
Pekka Paalanen77346a62011-11-30 16:26:35 +02004002 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004003 wl_resource_set_implementation(resource,
4004 &screensaver_implementation,
4005 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004006 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004007 return;
4008 }
4009
4010 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4011 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004012 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004013}
4014
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004015static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004016input_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 -04004017{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004018 struct input_panel_surface *ip_surface = surface->configure_private;
4019 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004020 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004021 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004022
Giulio Camuffo184df502013-02-21 11:29:21 +01004023 if (width == 0)
4024 return;
4025
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004026 if (!weston_surface_is_mapped(surface)) {
4027 if (!shell->showing_input_panels)
4028 return;
4029
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004030 show_surface = 1;
4031 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004032
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004033 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004034
4035 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004036 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4037 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004038 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004039 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4040 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004041 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004042
Jason Ekstranda7af7042013-10-12 22:38:11 -05004043 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004044
4045 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004046 wl_list_insert(&shell->input_panel_layer.view_list,
4047 &ip_surface->view->layer_link);
4048 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004049 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004050 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004051 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004052}
4053
4054static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004055destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004056{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004057 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4058
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004059 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004060 wl_list_remove(&input_panel_surface->link);
4061
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004062 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004063 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004064
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004065 free(input_panel_surface);
4066}
4067
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004068static struct input_panel_surface *
4069get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004070{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004071 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004072 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004073 } else {
4074 return NULL;
4075 }
4076}
4077
4078static void
4079input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4080{
4081 struct input_panel_surface *ipsurface = container_of(listener,
4082 struct input_panel_surface,
4083 surface_destroy_listener);
4084
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004085 if (ipsurface->resource) {
4086 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004087 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004088 destroy_input_panel_surface(ipsurface);
4089 }
4090}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004091
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004092static struct input_panel_surface *
4093create_input_panel_surface(struct desktop_shell *shell,
4094 struct weston_surface *surface)
4095{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004096 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004097
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004098 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4099 if (!input_panel_surface)
4100 return NULL;
4101
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004102 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004103 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004104
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004105 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004106
4107 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004108 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004109
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004110 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004111 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004112 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004113 &input_panel_surface->surface_destroy_listener);
4114
4115 wl_list_init(&input_panel_surface->link);
4116
4117 return input_panel_surface;
4118}
4119
4120static void
4121input_panel_surface_set_toplevel(struct wl_client *client,
4122 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004123 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004124 uint32_t position)
4125{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004126 struct input_panel_surface *input_panel_surface =
4127 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004128 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004129
4130 wl_list_insert(&shell->input_panel.surfaces,
4131 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004132
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004133 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004134 input_panel_surface->panel = 0;
4135}
4136
4137static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004138input_panel_surface_set_overlay_panel(struct wl_client *client,
4139 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004140{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004141 struct input_panel_surface *input_panel_surface =
4142 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004143 struct desktop_shell *shell = input_panel_surface->shell;
4144
4145 wl_list_insert(&shell->input_panel.surfaces,
4146 &input_panel_surface->link);
4147
4148 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004149}
4150
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004151static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004152 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004153 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004154};
4155
4156static void
4157destroy_input_panel_surface_resource(struct wl_resource *resource)
4158{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004159 struct input_panel_surface *ipsurf =
4160 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004161
4162 destroy_input_panel_surface(ipsurf);
4163}
4164
4165static void
4166input_panel_get_input_panel_surface(struct wl_client *client,
4167 struct wl_resource *resource,
4168 uint32_t id,
4169 struct wl_resource *surface_resource)
4170{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004171 struct weston_surface *surface =
4172 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004173 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004174 struct input_panel_surface *ipsurf;
4175
4176 if (get_input_panel_surface(surface)) {
4177 wl_resource_post_error(surface_resource,
4178 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004179 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004180 return;
4181 }
4182
4183 ipsurf = create_input_panel_surface(shell, surface);
4184 if (!ipsurf) {
4185 wl_resource_post_error(surface_resource,
4186 WL_DISPLAY_ERROR_INVALID_OBJECT,
4187 "surface->configure already set");
4188 return;
4189 }
4190
Jason Ekstranda85118c2013-06-27 20:17:02 -05004191 ipsurf->resource =
4192 wl_resource_create(client,
4193 &wl_input_panel_surface_interface, 1, id);
4194 wl_resource_set_implementation(ipsurf->resource,
4195 &input_panel_surface_implementation,
4196 ipsurf,
4197 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004198}
4199
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004200static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004201 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004202};
4203
4204static void
4205unbind_input_panel(struct wl_resource *resource)
4206{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004207 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004208
4209 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004210}
4211
4212static void
4213bind_input_panel(struct wl_client *client,
4214 void *data, uint32_t version, uint32_t id)
4215{
4216 struct desktop_shell *shell = data;
4217 struct wl_resource *resource;
4218
Jason Ekstranda85118c2013-06-27 20:17:02 -05004219 resource = wl_resource_create(client,
4220 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004221
4222 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004223 wl_resource_set_implementation(resource,
4224 &input_panel_implementation,
4225 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004226 shell->input_panel.binding = resource;
4227 return;
4228 }
4229
4230 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4231 "interface object already bound");
4232 wl_resource_destroy(resource);
4233}
4234
Kristian Høgsberg07045392012-02-19 18:52:44 -05004235struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004236 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004237 struct weston_surface *current;
4238 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004239 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004240};
4241
4242static void
4243switcher_next(struct switcher *switcher)
4244{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004245 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004246 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004247 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004248 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004249
Jason Ekstranda7af7042013-10-12 22:38:11 -05004250 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4251 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004252 case SHELL_SURFACE_TOPLEVEL:
4253 case SHELL_SURFACE_FULLSCREEN:
4254 case SHELL_SURFACE_MAXIMIZED:
4255 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004256 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004257 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004258 next = view->surface;
4259 prev = view->surface;
4260 view->alpha = 0.25;
4261 weston_view_geometry_dirty(view);
4262 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004263 break;
4264 default:
4265 break;
4266 }
Alex Wu1659daa2012-04-01 20:13:09 +08004267
Jason Ekstranda7af7042013-10-12 22:38:11 -05004268 if (is_black_surface(view->surface, NULL)) {
4269 view->alpha = 0.25;
4270 weston_view_geometry_dirty(view);
4271 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004272 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004273 }
4274
4275 if (next == NULL)
4276 next = first;
4277
Alex Wu07b26062012-03-12 16:06:01 +08004278 if (next == NULL)
4279 return;
4280
Kristian Høgsberg07045392012-02-19 18:52:44 -05004281 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004282 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004283
4284 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004285 wl_list_for_each(view, &next->views, surface_link)
4286 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004287
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004288 shsurf = get_shell_surface(switcher->current);
4289 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004290 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004291}
4292
4293static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004294switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004295{
4296 struct switcher *switcher =
4297 container_of(listener, struct switcher, listener);
4298
4299 switcher_next(switcher);
4300}
4301
4302static void
Daniel Stone351eb612012-05-31 15:27:47 -04004303switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004304{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004305 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004306 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004307 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004308
Jason Ekstranda7af7042013-10-12 22:38:11 -05004309 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4310 view->alpha = 1.0;
4311 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004312 }
4313
Alex Wu07b26062012-03-12 16:06:01 +08004314 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004315 activate(switcher->shell, switcher->current,
4316 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004317 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004318 weston_keyboard_end_grab(keyboard);
4319 if (keyboard->input_method_resource)
4320 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004321 free(switcher);
4322}
4323
4324static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004325switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004326 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004327{
4328 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004329 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004330
Daniel Stonec9785ea2012-05-30 16:31:52 +01004331 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004332 switcher_next(switcher);
4333}
4334
4335static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004336switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004337 uint32_t mods_depressed, uint32_t mods_latched,
4338 uint32_t mods_locked, uint32_t group)
4339{
4340 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004341 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004342
Daniel Stone351eb612012-05-31 15:27:47 -04004343 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4344 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004345}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004346
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004347static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004348 switcher_key,
4349 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004350};
4351
4352static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004353switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004354 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004355{
Tiago Vignattibe143262012-04-16 17:31:41 +03004356 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004357 struct switcher *switcher;
4358
4359 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004360 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004361 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004362 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004363 wl_list_init(&switcher->listener.link);
4364
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004365 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004366 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004367 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004368 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4369 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004370 switcher_next(switcher);
4371}
4372
Pekka Paalanen3c647232011-12-22 13:43:43 +02004373static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004374backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004375 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004376{
4377 struct weston_compositor *compositor = data;
4378 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004379 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004380
4381 /* TODO: we're limiting to simple use cases, where we assume just
4382 * control on the primary display. We'd have to extend later if we
4383 * ever get support for setting backlights on random desktop LCD
4384 * panels though */
4385 output = get_default_output(compositor);
4386 if (!output)
4387 return;
4388
4389 if (!output->set_backlight)
4390 return;
4391
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004392 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4393 backlight_new = output->backlight_current - 25;
4394 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4395 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004396
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004397 if (backlight_new < 5)
4398 backlight_new = 5;
4399 if (backlight_new > 255)
4400 backlight_new = 255;
4401
4402 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004403 output->set_backlight(output, output->backlight_current);
4404}
4405
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004406struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004407 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004408 struct weston_seat *seat;
4409 uint32_t key[2];
4410 int key_released[2];
4411};
4412
4413static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004414debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004415 uint32_t key, uint32_t state)
4416{
4417 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01004418 struct weston_compositor *ec = db->seat->compositor;
4419 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004420 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004421 uint32_t serial;
4422 int send = 0, terminate = 0;
4423 int check_binding = 1;
4424 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01004425 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004426
4427 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4428 /* Do not run bindings on key releases */
4429 check_binding = 0;
4430
4431 for (i = 0; i < 2; i++)
4432 if (key == db->key[i])
4433 db->key_released[i] = 1;
4434
4435 if (db->key_released[0] && db->key_released[1]) {
4436 /* All key releases been swalled so end the grab */
4437 terminate = 1;
4438 } else if (key != db->key[0] && key != db->key[1]) {
4439 /* Should not swallow release of other keys */
4440 send = 1;
4441 }
4442 } else if (key == db->key[0] && !db->key_released[0]) {
4443 /* Do not check bindings for the first press of the binding
4444 * key. This allows it to be used as a debug shortcut.
4445 * We still need to swallow this event. */
4446 check_binding = 0;
4447 } else if (db->key[1]) {
4448 /* If we already ran a binding don't process another one since
4449 * we can't keep track of all the binding keys that were
4450 * pressed in order to swallow the release events. */
4451 send = 1;
4452 check_binding = 0;
4453 }
4454
4455 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004456 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4457 key, state)) {
4458 /* We ran a binding so swallow the press and keep the
4459 * grab to swallow the released too. */
4460 send = 0;
4461 terminate = 0;
4462 db->key[1] = key;
4463 } else {
4464 /* Terminate the grab since the key pressed is not a
4465 * debug binding key. */
4466 send = 1;
4467 terminate = 1;
4468 }
4469 }
4470
4471 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01004472 serial = wl_display_next_serial(display);
4473 resource_list = &grab->keyboard->focus_resource_list;
4474 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004475 wl_keyboard_send_key(resource, serial, time, key, state);
4476 }
4477 }
4478
4479 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004480 weston_keyboard_end_grab(grab->keyboard);
4481 if (grab->keyboard->input_method_resource)
4482 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004483 free(db);
4484 }
4485}
4486
4487static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004488debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004489 uint32_t mods_depressed, uint32_t mods_latched,
4490 uint32_t mods_locked, uint32_t group)
4491{
4492 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01004493 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004494
Neil Roberts96d790e2013-09-19 17:32:00 +01004495 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004496
Neil Roberts96d790e2013-09-19 17:32:00 +01004497 wl_resource_for_each(resource, resource_list) {
4498 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4499 mods_latched, mods_locked, group);
4500 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004501}
4502
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004503struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004504 debug_binding_key,
4505 debug_binding_modifiers
4506};
4507
4508static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004509debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004510{
4511 struct debug_binding_grab *grab;
4512
4513 grab = calloc(1, sizeof *grab);
4514 if (!grab)
4515 return;
4516
4517 grab->seat = (struct weston_seat *) seat;
4518 grab->key[0] = key;
4519 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004520 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004521}
4522
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004523static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004524force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004525 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004526{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004527 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004528 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004529 struct desktop_shell *shell = data;
4530 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004531 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004532
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004533 focus_surface = seat->keyboard->focus;
4534 if (!focus_surface)
4535 return;
4536
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004537 wl_signal_emit(&compositor->kill_signal, focus_surface);
4538
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004539 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004540 wl_client_get_credentials(client, &pid, NULL, NULL);
4541
4542 /* Skip clients that we launched ourselves (the credentials of
4543 * the socketpair is ours) */
4544 if (pid == getpid())
4545 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004546
Daniel Stone325fc2d2012-05-30 16:31:58 +01004547 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004548}
4549
4550static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004551workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004552 uint32_t key, void *data)
4553{
4554 struct desktop_shell *shell = data;
4555 unsigned int new_index = shell->workspaces.current;
4556
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004557 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004558 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004559 if (new_index != 0)
4560 new_index--;
4561
4562 change_workspace(shell, new_index);
4563}
4564
4565static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004566workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004567 uint32_t key, void *data)
4568{
4569 struct desktop_shell *shell = data;
4570 unsigned int new_index = shell->workspaces.current;
4571
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004572 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004573 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004574 if (new_index < shell->workspaces.num - 1)
4575 new_index++;
4576
4577 change_workspace(shell, new_index);
4578}
4579
4580static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004581workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004582 uint32_t key, void *data)
4583{
4584 struct desktop_shell *shell = data;
4585 unsigned int new_index;
4586
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004587 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004588 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004589 new_index = key - KEY_F1;
4590 if (new_index >= shell->workspaces.num)
4591 new_index = shell->workspaces.num - 1;
4592
4593 change_workspace(shell, new_index);
4594}
4595
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004596static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004597workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004598 uint32_t key, void *data)
4599{
4600 struct desktop_shell *shell = data;
4601 unsigned int new_index = shell->workspaces.current;
4602
4603 if (shell->locked)
4604 return;
4605
4606 if (new_index != 0)
4607 new_index--;
4608
4609 take_surface_to_workspace_by_seat(shell, seat, new_index);
4610}
4611
4612static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004613workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004614 uint32_t key, void *data)
4615{
4616 struct desktop_shell *shell = data;
4617 unsigned int new_index = shell->workspaces.current;
4618
4619 if (shell->locked)
4620 return;
4621
4622 if (new_index < shell->workspaces.num - 1)
4623 new_index++;
4624
4625 take_surface_to_workspace_by_seat(shell, seat, new_index);
4626}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004627
4628static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004629handle_output_destroy(struct wl_listener *listener, void *data)
4630{
4631 struct shell_output *output_listener =
4632 container_of(listener, struct shell_output, destroy_listener);
4633
4634 wl_list_remove(&output_listener->destroy_listener.link);
4635 wl_list_remove(&output_listener->link);
4636 free(output_listener);
4637}
4638
4639static void
4640create_shell_output(struct desktop_shell *shell,
4641 struct weston_output *output)
4642{
4643 struct shell_output *shell_output;
4644
4645 shell_output = zalloc(sizeof *shell_output);
4646 if (shell_output == NULL)
4647 return;
4648
4649 shell_output->output = output;
4650 shell_output->shell = shell;
4651 shell_output->destroy_listener.notify = handle_output_destroy;
4652 wl_signal_add(&output->destroy_signal,
4653 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004654 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004655}
4656
4657static void
4658handle_output_create(struct wl_listener *listener, void *data)
4659{
4660 struct desktop_shell *shell =
4661 container_of(listener, struct desktop_shell, output_create_listener);
4662 struct weston_output *output = (struct weston_output *)data;
4663
4664 create_shell_output(shell, output);
4665}
4666
4667static void
4668setup_output_destroy_handler(struct weston_compositor *ec,
4669 struct desktop_shell *shell)
4670{
4671 struct weston_output *output;
4672
4673 wl_list_init(&shell->output_list);
4674 wl_list_for_each(output, &ec->output_list, link)
4675 create_shell_output(shell, output);
4676
4677 shell->output_create_listener.notify = handle_output_create;
4678 wl_signal_add(&ec->output_created_signal,
4679 &shell->output_create_listener);
4680}
4681
4682static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004683shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004684{
Tiago Vignattibe143262012-04-16 17:31:41 +03004685 struct desktop_shell *shell =
4686 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004687 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004688 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004689
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004690 if (shell->child.client)
4691 wl_client_destroy(shell->child.client);
4692
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004693 wl_list_remove(&shell->idle_listener.link);
4694 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004695 wl_list_remove(&shell->show_input_panel_listener.link);
4696 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004697
Xiong Zhang6b481422013-10-23 13:58:32 +08004698 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4699 wl_list_remove(&shell_output->destroy_listener.link);
4700 wl_list_remove(&shell_output->link);
4701 free(shell_output);
4702 }
4703
4704 wl_list_remove(&shell->output_create_listener.link);
4705
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004706 wl_array_for_each(ws, &shell->workspaces.array)
4707 workspace_destroy(*ws);
4708 wl_array_release(&shell->workspaces.array);
4709
Pekka Paalanen3c647232011-12-22 13:43:43 +02004710 free(shell->screensaver.path);
4711 free(shell);
4712}
4713
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004714static void
4715shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4716{
4717 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004718 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004719
4720 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004721 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4722 MODIFIER_CTRL | MODIFIER_ALT,
4723 terminate_binding, ec);
4724 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4725 click_to_activate_binding,
4726 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004727 weston_compositor_add_touch_binding(ec, 0,
4728 touch_to_activate_binding,
4729 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004730 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4731 MODIFIER_SUPER | MODIFIER_ALT,
4732 surface_opacity_binding, NULL);
4733 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4734 MODIFIER_SUPER, zoom_axis_binding,
4735 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004736
4737 /* configurable bindings */
4738 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004739 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4740 zoom_key_binding, NULL);
4741 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4742 zoom_key_binding, NULL);
4743 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4744 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004745 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004746 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4747 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004748
4749 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4750 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4751 rotate_binding, NULL);
4752
Daniel Stone325fc2d2012-05-30 16:31:58 +01004753 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4754 shell);
4755 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4756 ec);
4757 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4758 backlight_binding, ec);
4759 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4760 ec);
4761 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4762 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004763 weston_compositor_add_key_binding(ec, KEY_K, mod,
4764 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004765 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4766 workspace_up_binding, shell);
4767 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4768 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004769 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4770 workspace_move_surface_up_binding,
4771 shell);
4772 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4773 workspace_move_surface_down_binding,
4774 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004775
4776 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4777 if (shell->workspaces.num > 1) {
4778 num_workspace_bindings = shell->workspaces.num;
4779 if (num_workspace_bindings > 6)
4780 num_workspace_bindings = 6;
4781 for (i = 0; i < num_workspace_bindings; i++)
4782 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4783 workspace_f_binding,
4784 shell);
4785 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004786
4787 /* Debug bindings */
4788 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4789 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004790}
4791
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004792WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004793module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004794 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004795{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004796 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004797 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004798 struct workspace **pws;
4799 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004800 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004801
Peter Huttererf3d62272013-08-08 11:57:05 +10004802 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004803 if (shell == NULL)
4804 return -1;
4805
Kristian Høgsberg75840622011-09-06 13:48:16 -04004806 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004807
4808 shell->destroy_listener.notify = shell_destroy;
4809 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004810 shell->idle_listener.notify = idle_handler;
4811 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4812 shell->wake_listener.notify = wake_handler;
4813 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004814 shell->show_input_panel_listener.notify = show_input_panels;
4815 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4816 shell->hide_input_panel_listener.notify = hide_input_panels;
4817 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004818 shell->update_input_panel_listener.notify = update_input_panels;
4819 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004820 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004821 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004822 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004823 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004824 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004825 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004826 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004827 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004828 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004829 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02004830 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004831
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004832 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004833
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004834 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4835 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004836 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4837 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004838 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004839
4840 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004841 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004842
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004843 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004844
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004845 for (i = 0; i < shell->workspaces.num; i++) {
4846 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4847 if (pws == NULL)
4848 return -1;
4849
4850 *pws = workspace_create();
4851 if (*pws == NULL)
4852 return -1;
4853 }
4854 activate_workspace(shell, 0);
4855
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004856 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004857 wl_list_init(&shell->workspaces.animation.link);
4858 shell->workspaces.animation.frame = animate_workspace_change_frame;
4859
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004860 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004861 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004862 return -1;
4863
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004864 if (wl_global_create(ec->wl_display,
4865 &desktop_shell_interface, 2,
4866 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004867 return -1;
4868
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004869 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4870 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004871 return -1;
4872
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004873 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004874 shell, bind_input_panel) == NULL)
4875 return -1;
4876
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004877 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4878 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004879 return -1;
4880
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004881 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004882
Xiong Zhang6b481422013-10-23 13:58:32 +08004883 setup_output_destroy_handler(ec, shell);
4884
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004885 loop = wl_display_get_event_loop(ec->wl_display);
4886 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004887
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004888 shell->screensaver.timer =
4889 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4890
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004891 wl_list_for_each(seat, &ec->seat_list, link)
4892 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004893
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004894 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004895
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004896 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004897
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004898 return 0;
4899}