blob: 9663870419f83c9dc48e4924f8494664cdd83e59 [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
Tiago Vignattibe143262012-04-16 17:31:41 +030089struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050090 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040091
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020092 struct wl_listener idle_listener;
93 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040094 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020095 struct wl_listener show_input_panel_listener;
96 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020097 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020098
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050099 struct weston_layer fullscreen_layer;
100 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500101 struct weston_layer background_layer;
102 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200103 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500104
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400105 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300106 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400107
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200108 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500109 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200110 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200111 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200112
113 unsigned deathcount;
114 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200115 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200116
117 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200118 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200119 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200120
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200121 struct {
122 struct weston_surface *surface;
123 pixman_box32_t cursor_rectangle;
124 } text_input;
125
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400126 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500127 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100128
Pekka Paalanen77346a62011-11-30 16:26:35 +0200129 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200130 struct wl_array array;
131 unsigned int current;
132 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200133
Jonas Ådahle9d22502012-08-29 22:13:01 +0200134 struct wl_list client_list;
135
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200136 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200137 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200138 int anim_dir;
139 uint32_t anim_timestamp;
140 double anim_current;
141 struct workspace *anim_from;
142 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200143 } workspaces;
144
145 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200146 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200147 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200148 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500149 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200150 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200151 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500152
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200153 struct {
154 struct wl_resource *binding;
155 struct wl_list surfaces;
156 } input_panel;
157
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200158 struct {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500159 struct weston_view *view;
160 struct weston_view_animation *animation;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200161 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300162 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200163 } fade;
164
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300165 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800166 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700167 enum animation_type startup_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400168};
169
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500170enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200171 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500172 SHELL_SURFACE_TOPLEVEL,
173 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500174 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800175 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300176 SHELL_SURFACE_POPUP,
177 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200178};
179
Scott Moreauff1db4a2012-04-17 19:06:18 -0600180struct ping_timer {
181 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600182 uint32_t serial;
183};
184
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200185struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500186 struct wl_resource *resource;
187 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200188
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500189 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500190 struct weston_view *view;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200191 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400192 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300193 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200194
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400195 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400196 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500197 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800198 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800199 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600200 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100201
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500202 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500204 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200205 } rotation;
206
207 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100208 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500209 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100210 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400211 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500212 } popup;
213
Alex Wu4539b082012-03-01 12:57:46 +0800214 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300215 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400216 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300217 } transient;
218
219 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800220 enum wl_shell_surface_fullscreen_method type;
221 struct weston_transform transform; /* matrix from x, y */
222 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500223 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800224 } fullscreen;
225
Scott Moreauff1db4a2012-04-17 19:06:18 -0600226 struct ping_timer *ping_timer;
227
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200228 struct weston_transform workspace_transform;
229
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500230 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500231 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100232 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400233
234 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200235};
236
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300237struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400238 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300239 struct shell_surface *shsurf;
240 struct wl_listener shsurf_destroy_listener;
241};
242
Rusty Lynch1084da52013-08-15 09:10:08 -0700243struct shell_touch_grab {
244 struct weston_touch_grab grab;
245 struct shell_surface *shsurf;
246 struct wl_listener shsurf_destroy_listener;
247 struct weston_touch *touch;
248};
249
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300250struct weston_move_grab {
251 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100252 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500253};
254
Rusty Lynch1084da52013-08-15 09:10:08 -0700255struct weston_touch_move_grab {
256 struct shell_touch_grab base;
257 wl_fixed_t dx, dy;
258};
259
Pekka Paalanen460099f2012-01-20 16:48:25 +0200260struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300261 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500262 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200263 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200264 float x;
265 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200266 } center;
267};
268
Giulio Camuffo5085a752013-03-25 21:42:45 +0100269struct shell_seat {
270 struct weston_seat *seat;
271 struct wl_listener seat_destroy_listener;
272
273 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400274 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100275 struct wl_list surfaces_list;
276 struct wl_client *client;
277 int32_t initial_up;
278 } popup_grab;
279};
280
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400281static void
282activate(struct desktop_shell *shell, struct weston_surface *es,
283 struct weston_seat *seat);
284
285static struct workspace *
286get_current_workspace(struct desktop_shell *shell);
287
Alex Wubd3354b2012-04-17 17:20:49 +0800288static struct shell_surface *
289get_shell_surface(struct weston_surface *surface);
290
291static struct desktop_shell *
292shell_surface_get_shell(struct shell_surface *shsurf);
293
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500294static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400295surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500296
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300297static void
298shell_fade_startup(struct desktop_shell *shell);
299
Alex Wubd3354b2012-04-17 17:20:49 +0800300static bool
301shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
302{
303 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800305
306 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100307
Jason Ekstranda7af7042013-10-12 22:38:11 -0500308 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800309 return false;
310
Jason Ekstranda7af7042013-10-12 22:38:11 -0500311 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
312 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800313 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500314 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800315}
316
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500317static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400318destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300319{
320 struct shell_grab *grab;
321
322 grab = container_of(listener, struct shell_grab,
323 shsurf_destroy_listener);
324
325 grab->shsurf = NULL;
326}
327
Jason Ekstranda7af7042013-10-12 22:38:11 -0500328static struct weston_view *
329get_default_view(struct weston_surface *surface)
330{
331 struct shell_surface *shsurf;
332 struct weston_view *view;
333
334 if (!surface || wl_list_empty(&surface->views))
335 return NULL;
336
337 shsurf = get_shell_surface(surface);
338 if (shsurf)
339 return shsurf->view;
340
341 wl_list_for_each(view, &surface->views, surface_link)
342 if (weston_view_is_mapped(view))
343 return view;
344
345 return container_of(surface->views.next, struct weston_view, surface_link);
346}
347
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300348static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400349popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400350
351static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300352shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400353 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300354 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400355 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300356 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300357{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300358 struct desktop_shell *shell = shsurf->shell;
359
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400360 popup_grab_end(pointer);
361
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300362 grab->grab.interface = interface;
363 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400364 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500365 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400366 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300367
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400368 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400369 if (shell->child.desktop_shell) {
370 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
371 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500372 weston_pointer_set_focus(pointer,
373 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400374 wl_fixed_from_int(0),
375 wl_fixed_from_int(0));
376 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300377}
378
379static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300380shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300381{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400382 if (grab->shsurf)
383 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300384
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700385 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300386}
387
388static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700389shell_touch_grab_start(struct shell_touch_grab *grab,
390 const struct weston_touch_grab_interface *interface,
391 struct shell_surface *shsurf,
392 struct weston_touch *touch)
393{
394 struct desktop_shell *shell = shsurf->shell;
395
396 grab->grab.interface = interface;
397 grab->shsurf = shsurf;
398 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
399 wl_signal_add(&shsurf->destroy_signal,
400 &grab->shsurf_destroy_listener);
401
402 grab->touch = touch;
403
404 weston_touch_start_grab(touch, &grab->grab);
405 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500406 weston_touch_set_focus(touch->seat,
407 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700408}
409
410static void
411shell_touch_grab_end(struct shell_touch_grab *grab)
412{
413 if (grab->shsurf)
414 wl_list_remove(&grab->shsurf_destroy_listener.link);
415
416 weston_touch_end_grab(grab->touch);
417}
418
419static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800421 struct weston_output *output);
422
Daniel Stone496ca172012-05-30 16:31:42 +0100423static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300424get_modifier(char *modifier)
425{
426 if (!modifier)
427 return MODIFIER_SUPER;
428
429 if (!strcmp("ctrl", modifier))
430 return MODIFIER_CTRL;
431 else if (!strcmp("alt", modifier))
432 return MODIFIER_ALT;
433 else if (!strcmp("super", modifier))
434 return MODIFIER_SUPER;
435 else
436 return MODIFIER_SUPER;
437}
438
Juan Zhaoe10d2792012-04-25 19:09:52 +0800439static enum animation_type
440get_animation_type(char *animation)
441{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800442 if (!strcmp("zoom", animation))
443 return ANIMATION_ZOOM;
444 else if (!strcmp("fade", animation))
445 return ANIMATION_FADE;
446 else
447 return ANIMATION_NONE;
448}
449
Alex Wu4539b082012-03-01 12:57:46 +0800450static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400451shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200452{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400453 struct weston_config_section *section;
454 int duration;
455 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200456
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400457 section = weston_config_get_section(shell->compositor->config,
458 "screensaver", NULL, NULL);
459 weston_config_section_get_string(section,
460 "path", &shell->screensaver.path, NULL);
461 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200462 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400463
464 section = weston_config_get_section(shell->compositor->config,
465 "shell", NULL, NULL);
466 weston_config_section_get_string(section,
467 "binding-modifier", &s, "super");
468 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200469 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400470 weston_config_section_get_string(section, "animation", &s, "none");
471 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200472 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700473 weston_config_section_get_string(section,
474 "startup-animation", &s, "fade");
475 shell->startup_animation_type = get_animation_type(s);
476 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400477 weston_config_section_get_uint(section, "num-workspaces",
478 &shell->workspaces.num,
479 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200480}
481
482static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200483focus_state_destroy(struct focus_state *state)
484{
485 wl_list_remove(&state->seat_destroy_listener.link);
486 wl_list_remove(&state->surface_destroy_listener.link);
487 free(state);
488}
489
490static void
491focus_state_seat_destroy(struct wl_listener *listener, void *data)
492{
493 struct focus_state *state = container_of(listener,
494 struct focus_state,
495 seat_destroy_listener);
496
497 wl_list_remove(&state->link);
498 focus_state_destroy(state);
499}
500
501static void
502focus_state_surface_destroy(struct wl_listener *listener, void *data)
503{
504 struct focus_state *state = container_of(listener,
505 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400506 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400507 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500508 struct weston_surface *main_surface, *next;
509 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200510
Pekka Paalanen01388e22013-04-25 13:57:44 +0300511 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
512
Kristian Høgsberge3778222012-07-31 17:29:30 -0400513 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500514 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
515 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400516 continue;
517
Jason Ekstranda7af7042013-10-12 22:38:11 -0500518 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400519 break;
520 }
521
Pekka Paalanen01388e22013-04-25 13:57:44 +0300522 /* if the focus was a sub-surface, activate its main surface */
523 if (main_surface != state->keyboard_focus)
524 next = main_surface;
525
Kristian Høgsberge3778222012-07-31 17:29:30 -0400526 if (next) {
527 shell = state->seat->compositor->shell_interface.shell;
528 activate(shell, next, state->seat);
529 } else {
530 wl_list_remove(&state->link);
531 focus_state_destroy(state);
532 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200533}
534
535static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400536focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200537{
Jonas Ådahl04769742012-06-13 00:01:24 +0200538 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200539
540 state = malloc(sizeof *state);
541 if (state == NULL)
542 return NULL;
543
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400544 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200545 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400546 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200547
548 state->seat_destroy_listener.notify = focus_state_seat_destroy;
549 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400550 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200551 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400552 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200553
554 return state;
555}
556
Jonas Ådahl8538b222012-08-29 22:13:03 +0200557static struct focus_state *
558ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
559{
560 struct workspace *ws = get_current_workspace(shell);
561 struct focus_state *state;
562
563 wl_list_for_each(state, &ws->focus_list, link)
564 if (state->seat == seat)
565 break;
566
567 if (&state->link == &ws->focus_list)
568 state = focus_state_create(seat, ws);
569
570 return state;
571}
572
Jonas Ådahl04769742012-06-13 00:01:24 +0200573static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400574restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200575{
576 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400577 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200578
579 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400580 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200581
Kristian Høgsberge3148752013-05-06 23:19:49 -0400582 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200583 }
584}
585
586static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200587replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
588 struct weston_seat *seat)
589{
590 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400591 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200592
593 wl_list_for_each(state, &ws->focus_list, link) {
594 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400595 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500596 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200597 return;
598 }
599 }
600}
601
602static void
603drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
604 struct weston_surface *surface)
605{
606 struct focus_state *state;
607
608 wl_list_for_each(state, &ws->focus_list, link)
609 if (state->keyboard_focus == surface)
610 state->keyboard_focus = NULL;
611}
612
613static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200614workspace_destroy(struct workspace *ws)
615{
Jonas Ådahl04769742012-06-13 00:01:24 +0200616 struct focus_state *state, *next;
617
618 wl_list_for_each_safe(state, next, &ws->focus_list, link)
619 focus_state_destroy(state);
620
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200621 free(ws);
622}
623
Jonas Ådahl04769742012-06-13 00:01:24 +0200624static void
625seat_destroyed(struct wl_listener *listener, void *data)
626{
627 struct weston_seat *seat = data;
628 struct focus_state *state, *next;
629 struct workspace *ws = container_of(listener,
630 struct workspace,
631 seat_destroyed_listener);
632
633 wl_list_for_each_safe(state, next, &ws->focus_list, link)
634 if (state->seat == seat)
635 wl_list_remove(&state->link);
636}
637
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200638static struct workspace *
639workspace_create(void)
640{
641 struct workspace *ws = malloc(sizeof *ws);
642 if (ws == NULL)
643 return NULL;
644
645 weston_layer_init(&ws->layer, NULL);
646
Jonas Ådahl04769742012-06-13 00:01:24 +0200647 wl_list_init(&ws->focus_list);
648 wl_list_init(&ws->seat_destroyed_listener.link);
649 ws->seat_destroyed_listener.notify = seat_destroyed;
650
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200651 return ws;
652}
653
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200654static int
655workspace_is_empty(struct workspace *ws)
656{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500657 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200658}
659
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200660static struct workspace *
661get_workspace(struct desktop_shell *shell, unsigned int index)
662{
663 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200664 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200665 pws += index;
666 return *pws;
667}
668
669static struct workspace *
670get_current_workspace(struct desktop_shell *shell)
671{
672 return get_workspace(shell, shell->workspaces.current);
673}
674
675static void
676activate_workspace(struct desktop_shell *shell, unsigned int index)
677{
678 struct workspace *ws;
679
680 ws = get_workspace(shell, index);
681 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
682
683 shell->workspaces.current = index;
684}
685
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200686static unsigned int
687get_output_height(struct weston_output *output)
688{
689 return abs(output->region.extents.y1 - output->region.extents.y2);
690}
691
692static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500693view_translate(struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200694{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500695 struct shell_surface *shsurf = get_shell_surface(view->surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200696 struct weston_transform *transform;
697
698 transform = &shsurf->workspace_transform;
699 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500700 wl_list_insert(view->geometry.transformation_list.prev,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200701 &shsurf->workspace_transform.link);
702
703 weston_matrix_init(&shsurf->workspace_transform.matrix);
704 weston_matrix_translate(&shsurf->workspace_transform.matrix,
705 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500706 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200707}
708
709static void
710workspace_translate_out(struct workspace *ws, double fraction)
711{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500712 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200713 unsigned int height;
714 double d;
715
Jason Ekstranda7af7042013-10-12 22:38:11 -0500716 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
717 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200718 d = height * fraction;
719
Jason Ekstranda7af7042013-10-12 22:38:11 -0500720 view_translate(view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200721 }
722}
723
724static void
725workspace_translate_in(struct workspace *ws, double fraction)
726{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500727 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200728 unsigned int height;
729 double d;
730
Jason Ekstranda7af7042013-10-12 22:38:11 -0500731 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
732 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200733
734 if (fraction > 0)
735 d = -(height - height * fraction);
736 else
737 d = height + height * fraction;
738
Jason Ekstranda7af7042013-10-12 22:38:11 -0500739 view_translate(view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200740 }
741}
742
743static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200744broadcast_current_workspace_state(struct desktop_shell *shell)
745{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700746 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200747
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700748 wl_resource_for_each(resource, &shell->workspaces.client_list)
749 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200750 shell->workspaces.current,
751 shell->workspaces.num);
752}
753
754static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200755reverse_workspace_change_animation(struct desktop_shell *shell,
756 unsigned int index,
757 struct workspace *from,
758 struct workspace *to)
759{
760 shell->workspaces.current = index;
761
762 shell->workspaces.anim_to = to;
763 shell->workspaces.anim_from = from;
764 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
765 shell->workspaces.anim_timestamp = 0;
766
Scott Moreau4272e632012-08-13 09:58:41 -0600767 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200768}
769
770static void
771workspace_deactivate_transforms(struct workspace *ws)
772{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500773 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200774 struct shell_surface *shsurf;
775
Jason Ekstranda7af7042013-10-12 22:38:11 -0500776 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
777 shsurf = get_shell_surface(view->surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200778 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
779 wl_list_remove(&shsurf->workspace_transform.link);
780 wl_list_init(&shsurf->workspace_transform.link);
781 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500782 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200783 }
784}
785
786static void
787finish_workspace_change_animation(struct desktop_shell *shell,
788 struct workspace *from,
789 struct workspace *to)
790{
Scott Moreau4272e632012-08-13 09:58:41 -0600791 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200792
793 wl_list_remove(&shell->workspaces.animation.link);
794 workspace_deactivate_transforms(from);
795 workspace_deactivate_transforms(to);
796 shell->workspaces.anim_to = NULL;
797
798 wl_list_remove(&shell->workspaces.anim_from->layer.link);
799}
800
801static void
802animate_workspace_change_frame(struct weston_animation *animation,
803 struct weston_output *output, uint32_t msecs)
804{
805 struct desktop_shell *shell =
806 container_of(animation, struct desktop_shell,
807 workspaces.animation);
808 struct workspace *from = shell->workspaces.anim_from;
809 struct workspace *to = shell->workspaces.anim_to;
810 uint32_t t;
811 double x, y;
812
813 if (workspace_is_empty(from) && workspace_is_empty(to)) {
814 finish_workspace_change_animation(shell, from, to);
815 return;
816 }
817
818 if (shell->workspaces.anim_timestamp == 0) {
819 if (shell->workspaces.anim_current == 0.0)
820 shell->workspaces.anim_timestamp = msecs;
821 else
822 shell->workspaces.anim_timestamp =
823 msecs -
824 /* Invers of movement function 'y' below. */
825 (asin(1.0 - shell->workspaces.anim_current) *
826 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
827 M_2_PI);
828 }
829
830 t = msecs - shell->workspaces.anim_timestamp;
831
832 /*
833 * x = [0, π/2]
834 * y(x) = sin(x)
835 */
836 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
837 y = sin(x);
838
839 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600840 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200841
842 workspace_translate_out(from, shell->workspaces.anim_dir * y);
843 workspace_translate_in(to, shell->workspaces.anim_dir * y);
844 shell->workspaces.anim_current = y;
845
Scott Moreau4272e632012-08-13 09:58:41 -0600846 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200847 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200848 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200849 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200850}
851
852static void
853animate_workspace_change(struct desktop_shell *shell,
854 unsigned int index,
855 struct workspace *from,
856 struct workspace *to)
857{
858 struct weston_output *output;
859
860 int dir;
861
862 if (index > shell->workspaces.current)
863 dir = -1;
864 else
865 dir = 1;
866
867 shell->workspaces.current = index;
868
869 shell->workspaces.anim_dir = dir;
870 shell->workspaces.anim_from = from;
871 shell->workspaces.anim_to = to;
872 shell->workspaces.anim_current = 0.0;
873 shell->workspaces.anim_timestamp = 0;
874
875 output = container_of(shell->compositor->output_list.next,
876 struct weston_output, link);
877 wl_list_insert(&output->animation_list,
878 &shell->workspaces.animation.link);
879
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200880 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200881
882 workspace_translate_in(to, 0);
883
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400884 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200885
Scott Moreau4272e632012-08-13 09:58:41 -0600886 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200887}
888
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200889static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200890update_workspace(struct desktop_shell *shell, unsigned int index,
891 struct workspace *from, struct workspace *to)
892{
893 shell->workspaces.current = index;
894 wl_list_insert(&from->layer.link, &to->layer.link);
895 wl_list_remove(&from->layer.link);
896}
897
898static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200899change_workspace(struct desktop_shell *shell, unsigned int index)
900{
901 struct workspace *from;
902 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200903
904 if (index == shell->workspaces.current)
905 return;
906
907 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200909 return;
910
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200911 from = get_current_workspace(shell);
912 to = get_workspace(shell, index);
913
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200914 if (shell->workspaces.anim_from == to &&
915 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200916 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200917 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200918 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200919 return;
920 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200921
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200922 if (shell->workspaces.anim_to != NULL)
923 finish_workspace_change_animation(shell,
924 shell->workspaces.anim_from,
925 shell->workspaces.anim_to);
926
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200927 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200928
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200929 if (workspace_is_empty(to) && workspace_is_empty(from))
930 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200931 else
932 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200933
934 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200935}
936
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200937static bool
938workspace_has_only(struct workspace *ws, struct weston_surface *surface)
939{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200941 struct wl_list *e;
942
943 if (wl_list_empty(list))
944 return false;
945
946 e = list->next;
947
948 if (e->next != list)
949 return false;
950
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200952}
953
954static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955move_view_to_workspace(struct desktop_shell *shell,
956 struct weston_view *view,
957 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +0200958{
959 struct workspace *from;
960 struct workspace *to;
961 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300962 struct weston_surface *focus;
963
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200965
966 if (workspace == shell->workspaces.current)
967 return;
968
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200969 if (workspace >= shell->workspaces.num)
970 workspace = shell->workspaces.num - 1;
971
Jonas Ådahle9d22502012-08-29 22:13:01 +0200972 from = get_current_workspace(shell);
973 to = get_workspace(shell, workspace);
974
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 wl_list_remove(&view->layer_link);
976 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200977
Jason Ekstranda7af7042013-10-12 22:38:11 -0500978 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300979 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
980 if (!seat->keyboard)
981 continue;
982
983 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400985 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300986 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200987
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200989}
990
991static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200992take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400993 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200994 unsigned int index)
995{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300996 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200998 struct shell_surface *shsurf;
999 struct workspace *from;
1000 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001001 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001002
Pekka Paalanen01388e22013-04-25 13:57:44 +03001003 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004 view = get_default_view(surface);
1005 if (view == NULL ||
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001006 index == shell->workspaces.current)
1007 return;
1008
1009 from = get_current_workspace(shell);
1010 to = get_workspace(shell, index);
1011
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 wl_list_remove(&view->layer_link);
1013 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001014
Jonas Ådahle9d22502012-08-29 22:13:01 +02001015 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001016 drop_focus_state(shell, from, surface);
1017
1018 if (shell->workspaces.anim_from == to &&
1019 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001020 wl_list_remove(&to->layer.link);
1021 wl_list_insert(from->layer.link.prev, &to->layer.link);
1022
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001023 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001024 broadcast_current_workspace_state(shell);
1025
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001026 return;
1027 }
1028
1029 if (shell->workspaces.anim_to != NULL)
1030 finish_workspace_change_animation(shell,
1031 shell->workspaces.anim_from,
1032 shell->workspaces.anim_to);
1033
1034 if (workspace_is_empty(from) &&
1035 workspace_has_only(to, surface))
1036 update_workspace(shell, index, from, to);
1037 else {
1038 shsurf = get_shell_surface(surface);
1039 if (wl_list_empty(&shsurf->workspace_transform.link))
1040 wl_list_insert(&shell->workspaces.anim_sticky_list,
1041 &shsurf->workspace_transform.link);
1042
1043 animate_workspace_change(shell, index, from, to);
1044 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001045
1046 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001047
1048 state = ensure_focus_state(shell, seat);
1049 if (state != NULL)
1050 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001051}
1052
1053static void
1054workspace_manager_move_surface(struct wl_client *client,
1055 struct wl_resource *resource,
1056 struct wl_resource *surface_resource,
1057 uint32_t workspace)
1058{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001059 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001060 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001061 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001062 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001063 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001064
Pekka Paalanen01388e22013-04-25 13:57:44 +03001065 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 view = get_default_view(main_surface);
1067 if (!view)
1068 return;
1069 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001070}
1071
1072static const struct workspace_manager_interface workspace_manager_implementation = {
1073 workspace_manager_move_surface,
1074};
1075
1076static void
1077unbind_resource(struct wl_resource *resource)
1078{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001079 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001080}
1081
1082static void
1083bind_workspace_manager(struct wl_client *client,
1084 void *data, uint32_t version, uint32_t id)
1085{
1086 struct desktop_shell *shell = data;
1087 struct wl_resource *resource;
1088
Jason Ekstranda85118c2013-06-27 20:17:02 -05001089 resource = wl_resource_create(client,
1090 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001091
1092 if (resource == NULL) {
1093 weston_log("couldn't add workspace manager object");
1094 return;
1095 }
1096
Jason Ekstranda85118c2013-06-27 20:17:02 -05001097 wl_resource_set_implementation(resource,
1098 &workspace_manager_implementation,
1099 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001100 wl_list_insert(&shell->workspaces.client_list,
1101 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001102
1103 workspace_manager_send_state(resource,
1104 shell->workspaces.current,
1105 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001106}
1107
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001108static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001109touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1110 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1111{
1112}
1113
1114static void
1115touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1116{
1117 struct shell_touch_grab *shell_grab = container_of(grab,
1118 struct shell_touch_grab,
1119 grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001120
1121 if (grab->touch->seat->num_tp == 0)
1122 shell_touch_grab_end(shell_grab);
Rusty Lynch1084da52013-08-15 09:10:08 -07001123}
1124
1125static void
1126touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1127 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1128{
1129 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1130 struct shell_surface *shsurf = move->base.shsurf;
1131 struct weston_surface *es;
1132 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1133 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1134
1135 if (!shsurf)
1136 return;
1137
1138 es = shsurf->surface;
1139
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 weston_view_configure(shsurf->view, dx, dy,
1141 shsurf->view->geometry.width,
1142 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001143
1144 weston_compositor_schedule_repaint(es->compositor);
1145}
1146
1147static const struct weston_touch_grab_interface touch_move_grab_interface = {
1148 touch_move_grab_down,
1149 touch_move_grab_up,
1150 touch_move_grab_motion,
1151};
1152
1153static int
1154surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1155{
1156 struct weston_touch_move_grab *move;
1157
1158 if (!shsurf)
1159 return -1;
1160
1161 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1162 return 0;
1163
1164 move = malloc(sizeof *move);
1165 if (!move)
1166 return -1;
1167
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001169 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001171 seat->touch->grab_y;
1172
1173 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1174 seat->touch);
1175
1176 return 0;
1177}
1178
1179static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001180noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001181{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001182}
1183
1184static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001185move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001186{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001187 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001188 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001189 struct shell_surface *shsurf = move->base.shsurf;
Daniel Stone37816df2012-05-16 18:45:18 +01001190 int dx = wl_fixed_to_int(pointer->x + move->dx);
1191 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001192
1193 if (!shsurf)
1194 return;
1195
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 weston_view_configure(shsurf->view, dx, dy,
1197 shsurf->view->geometry.width,
1198 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001199
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001201}
1202
1203static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001204move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001205 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001206{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001207 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1208 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001209 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001210 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001211
Daniel Stone4dbadb12012-05-30 16:31:51 +01001212 if (pointer->button_count == 0 &&
1213 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001214 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001215 free(grab);
1216 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001217}
1218
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001219static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001220 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001221 move_grab_motion,
1222 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001223};
1224
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001225static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001226surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001227{
1228 struct weston_move_grab *move;
1229
1230 if (!shsurf)
1231 return -1;
1232
1233 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1234 return 0;
1235
1236 move = malloc(sizeof *move);
1237 if (!move)
1238 return -1;
1239
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001241 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001243 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001244
1245 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001246 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001247
1248 return 0;
1249}
1250
1251static void
1252shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1253 struct wl_resource *seat_resource, uint32_t serial)
1254{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001255 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001256 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001257 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001258
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001259 if (seat->pointer &&
1260 seat->pointer->button_count > 0 &&
1261 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001263 if ((surface == shsurf->surface) &&
1264 (surface_move(shsurf, seat) < 0))
1265 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001266 } else if (seat->touch &&
1267 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001269 if ((surface == shsurf->surface) &&
1270 (surface_touch_move(shsurf, seat) < 0))
1271 wl_resource_post_no_memory(resource);
1272 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001273}
1274
1275struct weston_resize_grab {
1276 struct shell_grab base;
1277 uint32_t edges;
1278 int32_t width, height;
1279};
1280
1281static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001282resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001283{
1284 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001285 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001286 struct shell_surface *shsurf = resize->base.shsurf;
1287 int32_t width, height;
1288 wl_fixed_t from_x, from_y;
1289 wl_fixed_t to_x, to_y;
1290
1291 if (!shsurf)
1292 return;
1293
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 weston_view_from_global_fixed(shsurf->view,
1295 pointer->grab_x, pointer->grab_y,
1296 &from_x, &from_y);
1297 weston_view_from_global_fixed(shsurf->view,
1298 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001299
1300 width = resize->width;
1301 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1302 width += wl_fixed_to_int(from_x - to_x);
1303 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1304 width += wl_fixed_to_int(to_x - from_x);
1305 }
1306
1307 height = resize->height;
1308 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1309 height += wl_fixed_to_int(from_y - to_y);
1310 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1311 height += wl_fixed_to_int(to_y - from_y);
1312 }
1313
1314 shsurf->client->send_configure(shsurf->surface,
1315 resize->edges, width, height);
1316}
1317
1318static void
1319send_configure(struct weston_surface *surface,
1320 uint32_t edges, int32_t width, int32_t height)
1321{
1322 struct shell_surface *shsurf = get_shell_surface(surface);
1323
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001324 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001325 edges, width, height);
1326}
1327
1328static const struct weston_shell_client shell_client = {
1329 send_configure
1330};
1331
1332static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001333resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001334 uint32_t time, uint32_t button, uint32_t state_w)
1335{
1336 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001337 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001338 enum wl_pointer_button_state state = state_w;
1339
1340 if (pointer->button_count == 0 &&
1341 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1342 shell_grab_end(&resize->base);
1343 free(grab);
1344 }
1345}
1346
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001347static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001348 noop_grab_focus,
1349 resize_grab_motion,
1350 resize_grab_button,
1351};
1352
Giulio Camuffob8366642013-04-25 13:57:46 +03001353/*
1354 * Returns the bounding box of a surface and all its sub-surfaces,
1355 * in the surface coordinates system. */
1356static void
1357surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1358 int32_t *y, int32_t *w, int32_t *h) {
1359 pixman_region32_t region;
1360 pixman_box32_t *box;
1361 struct weston_subsurface *subsurface;
1362
1363 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 surface->width,
1365 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001366
1367 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1368 pixman_region32_union_rect(&region, &region,
1369 subsurface->position.x,
1370 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 subsurface->surface->width,
1372 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001373 }
1374
1375 box = pixman_region32_extents(&region);
1376 if (x)
1377 *x = box->x1;
1378 if (y)
1379 *y = box->y1;
1380 if (w)
1381 *w = box->x2 - box->x1;
1382 if (h)
1383 *h = box->y2 - box->y1;
1384
1385 pixman_region32_fini(&region);
1386}
1387
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001388static int
1389surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001390 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001391{
1392 struct weston_resize_grab *resize;
1393
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001394 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1395 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001396 return 0;
1397
1398 if (edges == 0 || edges > 15 ||
1399 (edges & 3) == 3 || (edges & 12) == 12)
1400 return 0;
1401
1402 resize = malloc(sizeof *resize);
1403 if (!resize)
1404 return -1;
1405
1406 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001407 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1408 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001409
1410 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001411 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001412
1413 return 0;
1414}
1415
1416static void
1417shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1418 struct wl_resource *seat_resource, uint32_t serial,
1419 uint32_t edges)
1420{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001421 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001422 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001423 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001424
1425 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1426 return;
1427
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001429 if (seat->pointer->button_count == 0 ||
1430 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001431 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001432 return;
1433
Kristian Høgsberge3148752013-05-06 23:19:49 -04001434 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001435 wl_resource_post_no_memory(resource);
1436}
1437
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001438static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001439end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1440
1441static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001442busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001443{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001444 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001445 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001447 wl_fixed_t sx, sy;
1448
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 view = weston_compositor_pick_view(pointer->seat->compositor,
1450 pointer->x, pointer->y,
1451 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001452
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001454 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001455}
1456
1457static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001458busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001459{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001460}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001461
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001462static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001463busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001464 uint32_t time, uint32_t button, uint32_t state)
1465{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001466 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001467 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001468 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001469
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001470 if (shsurf && button == BTN_LEFT && state) {
1471 activate(shsurf->shell, shsurf->surface, seat);
1472 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001473 } else if (shsurf && button == BTN_RIGHT && state) {
1474 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001475 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001476 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001477}
1478
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001479static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001480 busy_cursor_grab_focus,
1481 busy_cursor_grab_motion,
1482 busy_cursor_grab_button,
1483};
1484
1485static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001486set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001487{
1488 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001489
1490 grab = malloc(sizeof *grab);
1491 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001492 return;
1493
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001494 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1495 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001496}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001497
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001498static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001499end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001500{
1501 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1502
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001503 if (grab->grab.interface == &busy_cursor_grab_interface &&
1504 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001505 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001506 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001507 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001508}
1509
Scott Moreau9521d5e2012-04-19 13:06:17 -06001510static void
1511ping_timer_destroy(struct shell_surface *shsurf)
1512{
1513 if (!shsurf || !shsurf->ping_timer)
1514 return;
1515
1516 if (shsurf->ping_timer->source)
1517 wl_event_source_remove(shsurf->ping_timer->source);
1518
1519 free(shsurf->ping_timer);
1520 shsurf->ping_timer = NULL;
1521}
1522
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001523static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001524ping_timeout_handler(void *data)
1525{
1526 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001527 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001528
Scott Moreau9521d5e2012-04-19 13:06:17 -06001529 /* Client is not responding */
1530 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001531
1532 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 if (seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001534 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001535
1536 return 1;
1537}
1538
1539static void
1540ping_handler(struct weston_surface *surface, uint32_t serial)
1541{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001542 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001543 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001544 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001545
1546 if (!shsurf)
1547 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001548 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001549 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001550
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001551 if (shsurf->surface == shsurf->shell->grab_surface)
1552 return;
1553
Scott Moreauff1db4a2012-04-17 19:06:18 -06001554 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001555 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001556 if (!shsurf->ping_timer)
1557 return;
1558
1559 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001560 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1561 shsurf->ping_timer->source =
1562 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1563 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1564
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001565 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001566 }
1567}
1568
1569static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001570handle_pointer_focus(struct wl_listener *listener, void *data)
1571{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001572 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001574 struct weston_compositor *compositor;
1575 struct shell_surface *shsurf;
1576 uint32_t serial;
1577
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001579 return;
1580
Jason Ekstranda7af7042013-10-12 22:38:11 -05001581 compositor = view->surface->compositor;
1582 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001583
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001584 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001585 set_busy_cursor(shsurf, pointer);
1586 } else {
1587 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001589 }
1590}
1591
1592static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001593create_pointer_focus_listener(struct weston_seat *seat)
1594{
1595 struct wl_listener *listener;
1596
Kristian Høgsberge3148752013-05-06 23:19:49 -04001597 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001598 return;
1599
1600 listener = malloc(sizeof *listener);
1601 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001602 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001603}
1604
1605static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001606shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1607 uint32_t serial)
1608{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001609 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001610 struct weston_seat *seat;
1611 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001612
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001613 if (shsurf->ping_timer == NULL)
1614 /* Just ignore unsolicited pong. */
1615 return;
1616
Scott Moreauff1db4a2012-04-17 19:06:18 -06001617 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001618 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001619 wl_list_for_each(seat, &ec->seat_list, link) {
1620 if(seat->pointer)
1621 end_busy_cursor(shsurf, seat->pointer);
1622 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001623 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001624 }
1625}
1626
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001627static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001628set_title(struct shell_surface *shsurf, const char *title)
1629{
1630 free(shsurf->title);
1631 shsurf->title = strdup(title);
1632}
1633
1634static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001635shell_surface_set_title(struct wl_client *client,
1636 struct wl_resource *resource, const char *title)
1637{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001638 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001639
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001640 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001641}
1642
1643static void
1644shell_surface_set_class(struct wl_client *client,
1645 struct wl_resource *resource, const char *class)
1646{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001647 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001648
1649 free(shsurf->class);
1650 shsurf->class = strdup(class);
1651}
1652
Juan Zhao96879df2012-02-07 08:45:41 +08001653static struct weston_output *
1654get_default_output(struct weston_compositor *compositor)
1655{
1656 return container_of(compositor->output_list.next,
1657 struct weston_output, link);
1658}
1659
Alex Wu4539b082012-03-01 12:57:46 +08001660static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001661restore_output_mode(struct weston_output *output)
1662{
Hardening57388e42013-09-18 23:56:36 +02001663 if (output->current_mode != output->original_mode ||
1664 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001665 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001666 output->original_mode,
1667 output->original_scale,
1668 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001669}
1670
1671static void
1672restore_all_output_modes(struct weston_compositor *compositor)
1673{
1674 struct weston_output *output;
1675
1676 wl_list_for_each(output, &compositor->output_list, link)
1677 restore_output_mode(output);
1678}
1679
1680static void
Alex Wu4539b082012-03-01 12:57:46 +08001681shell_unset_fullscreen(struct shell_surface *shsurf)
1682{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001683 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001684 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001685 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1686 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001687 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001688 }
Alex Wu4539b082012-03-01 12:57:46 +08001689 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1690 shsurf->fullscreen.framerate = 0;
1691 wl_list_remove(&shsurf->fullscreen.transform.link);
1692 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693 if (shsurf->fullscreen.black_view)
1694 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1695 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001696 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001697 weston_view_set_position(shsurf->view,
1698 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001699 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001700 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001701 &shsurf->rotation.transform.link);
1702 shsurf->saved_rotation_valid = false;
1703 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001704
1705 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 wl_list_remove(&shsurf->view->layer_link);
1707 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001708}
1709
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001710static void
1711shell_unset_maximized(struct shell_surface *shsurf)
1712{
1713 struct workspace *ws;
1714 /* undo all maximized things here */
1715 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716 weston_view_set_position(shsurf->view,
1717 shsurf->saved_x,
1718 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001719
1720 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001721 wl_list_insert(&shsurf->view->geometry.transformation_list,
1722 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001723 shsurf->saved_rotation_valid = false;
1724 }
1725
1726 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727 wl_list_remove(&shsurf->view->layer_link);
1728 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001729}
1730
Pekka Paalanen98262232011-12-01 10:42:22 +02001731static int
1732reset_shell_surface_type(struct shell_surface *surface)
1733{
1734 switch (surface->type) {
1735 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001736 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001737 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001738 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001739 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001740 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001741 case SHELL_SURFACE_NONE:
1742 case SHELL_SURFACE_TOPLEVEL:
1743 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001744 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001745 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001746 break;
1747 }
1748
1749 surface->type = SHELL_SURFACE_NONE;
1750 return 0;
1751}
1752
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001753static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001754set_surface_type(struct shell_surface *shsurf)
1755{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001756 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001758
1759 reset_shell_surface_type(shsurf);
1760
1761 shsurf->type = shsurf->next_type;
1762 shsurf->next_type = SHELL_SURFACE_NONE;
1763
1764 switch (shsurf->type) {
1765 case SHELL_SURFACE_TOPLEVEL:
1766 break;
1767 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001768 if (pev)
1769 weston_view_set_position(shsurf->view,
1770 pev->geometry.x + shsurf->transient.x,
1771 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001772 break;
1773
1774 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001775 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776 shsurf->saved_x = shsurf->view->geometry.x;
1777 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001778 shsurf->saved_position_valid = true;
1779
1780 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1781 wl_list_remove(&shsurf->rotation.transform.link);
1782 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001784 shsurf->saved_rotation_valid = true;
1785 }
1786 break;
1787
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001788 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789 weston_view_set_position(shsurf->view, shsurf->transient.x,
1790 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001791 break;
1792
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001793 default:
1794 break;
1795 }
1796}
1797
1798static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001799set_toplevel(struct shell_surface *shsurf)
1800{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001801 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001802}
1803
1804static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001805shell_surface_set_toplevel(struct wl_client *client,
1806 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001807{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001808 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001809
Tiago Vignattibc052c92012-04-19 16:18:18 +03001810 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001811}
1812
1813static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001814set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001815 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001816{
1817 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001818 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001819 shsurf->transient.x = x;
1820 shsurf->transient.y = y;
1821 shsurf->transient.flags = flags;
1822 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1823}
1824
1825static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001826shell_surface_set_transient(struct wl_client *client,
1827 struct wl_resource *resource,
1828 struct wl_resource *parent_resource,
1829 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001830{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001831 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001832 struct weston_surface *parent =
1833 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001834
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001835 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001836}
1837
Tiago Vignattibe143262012-04-16 17:31:41 +03001838static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001839shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001840{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001841 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001842}
1843
1844static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001845get_output_panel_height(struct desktop_shell *shell,
1846 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001847{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001848 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08001849 int panel_height = 0;
1850
1851 if (!output)
1852 return 0;
1853
Jason Ekstranda7af7042013-10-12 22:38:11 -05001854 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1855 if (view->surface->output == output) {
1856 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001857 break;
1858 }
1859 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001860
Juan Zhao96879df2012-02-07 08:45:41 +08001861 return panel_height;
1862}
1863
1864static void
1865shell_surface_set_maximized(struct wl_client *client,
1866 struct wl_resource *resource,
1867 struct wl_resource *output_resource )
1868{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001869 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001870 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001871 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001872 uint32_t edges = 0, panel_height = 0;
1873
1874 /* get the default output, if the client set it as NULL
1875 check whether the ouput is available */
1876 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001877 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001878 else if (es->output)
1879 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001880 else
1881 shsurf->output = get_default_output(es->compositor);
1882
Tiago Vignattibe143262012-04-16 17:31:41 +03001883 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001884 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001885 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001886
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001887 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001888 shsurf->output->width,
1889 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001890
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001891 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001892}
1893
Alex Wu21858432012-04-01 20:13:08 +08001894static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001895black_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 +08001896
Jason Ekstranda7af7042013-10-12 22:38:11 -05001897static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08001898create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001899 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001900 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001901{
1902 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001903 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08001904
1905 surface = weston_surface_create(ec);
1906 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001907 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001908 return NULL;
1909 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001910 view = weston_view_create(surface);
1911 if (surface == NULL) {
1912 weston_log("no memory\n");
1913 weston_surface_destroy(surface);
1914 return NULL;
1915 }
Alex Wu4539b082012-03-01 12:57:46 +08001916
Alex Wu21858432012-04-01 20:13:08 +08001917 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001918 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001919 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001920 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001921 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001922 pixman_region32_fini(&surface->input);
1923 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001924
Jason Ekstranda7af7042013-10-12 22:38:11 -05001925 weston_view_configure(view, x, y, w, h);
1926
1927 return view;
Alex Wu4539b082012-03-01 12:57:46 +08001928}
1929
1930/* Create black surface and append it to the associated fullscreen surface.
1931 * Handle size dismatch and positioning according to the method. */
1932static void
1933shell_configure_fullscreen(struct shell_surface *shsurf)
1934{
1935 struct weston_output *output = shsurf->fullscreen_output;
1936 struct weston_surface *surface = shsurf->surface;
1937 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001938 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001939 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001940
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001941 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1942 restore_output_mode(output);
1943
Jason Ekstranda7af7042013-10-12 22:38:11 -05001944 if (!shsurf->fullscreen.black_view)
1945 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001946 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001947 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001948 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001949 output->width,
1950 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001951
Jason Ekstranda7af7042013-10-12 22:38:11 -05001952 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
1953 wl_list_insert(&shsurf->view->layer_link,
1954 &shsurf->fullscreen.black_view->layer_link);
1955 shsurf->fullscreen.black_view->surface->output = output;
1956 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08001957
Jason Ekstranda7af7042013-10-12 22:38:11 -05001958 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03001959 &surf_width, &surf_height);
1960
Alex Wu4539b082012-03-01 12:57:46 +08001961 switch (shsurf->fullscreen.type) {
1962 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001963 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001964 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001965 break;
1966 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001967 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001968 if (output->width == surf_width &&
1969 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001970 weston_view_set_position(shsurf->view,
1971 output->x - surf_x,
1972 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001973 break;
1974 }
1975
Alex Wu4539b082012-03-01 12:57:46 +08001976 matrix = &shsurf->fullscreen.transform.matrix;
1977 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001978
Scott Moreau1bad5db2012-08-18 01:04:05 -06001979 output_aspect = (float) output->width /
1980 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001981 /* XXX: Use surf_width and surf_height here? */
1982 surface_aspect = (float) surface->width /
1983 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001984 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001985 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001986 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001987 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001988 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001989 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001990
Alex Wu4539b082012-03-01 12:57:46 +08001991 weston_matrix_scale(matrix, scale, scale, 1);
1992 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001993 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001994 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001995 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1996 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001998
Alex Wu4539b082012-03-01 12:57:46 +08001999 break;
2000 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002001 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002002 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02002003 surf_width * surface->buffer_scale,
2004 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002005 shsurf->fullscreen.framerate};
2006
Hardening57388e42013-09-18 23:56:36 +02002007 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
2008 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002009 weston_view_set_position(shsurf->view,
2010 output->x - surf_x,
2011 output->y - surf_y);
2012 weston_view_configure(shsurf->fullscreen.black_view,
2013 output->x - surf_x,
2014 output->y - surf_y,
2015 output->width,
2016 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002017 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002018 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002019 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002021 }
Alex Wubd3354b2012-04-17 17:20:49 +08002022 }
Alex Wu4539b082012-03-01 12:57:46 +08002023 break;
2024 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002025 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002026 break;
2027 default:
2028 break;
2029 }
2030}
2031
2032/* make the fullscreen and black surface at the top */
2033static void
2034shell_stack_fullscreen(struct shell_surface *shsurf)
2035{
Alex Wubd3354b2012-04-17 17:20:49 +08002036 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002037 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002038
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039 wl_list_remove(&shsurf->view->layer_link);
2040 wl_list_insert(&shell->fullscreen_layer.view_list,
2041 &shsurf->view->layer_link);
2042 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002043
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044 if (!shsurf->fullscreen.black_view)
2045 shsurf->fullscreen.black_view =
2046 create_black_surface(shsurf->surface->compositor,
2047 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002048 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002049 output->width,
2050 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002051
Jason Ekstranda7af7042013-10-12 22:38:11 -05002052 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2053 wl_list_insert(&shsurf->view->layer_link,
2054 &shsurf->fullscreen.black_view->layer_link);
2055 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002056}
2057
2058static void
2059shell_map_fullscreen(struct shell_surface *shsurf)
2060{
Alex Wu4539b082012-03-01 12:57:46 +08002061 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002062 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002063}
2064
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002065static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002066set_fullscreen(struct shell_surface *shsurf,
2067 uint32_t method,
2068 uint32_t framerate,
2069 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002070{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002071 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002072
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002073 if (output)
2074 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002075 else if (es->output)
2076 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002077 else
2078 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002079
Alex Wu4539b082012-03-01 12:57:46 +08002080 shsurf->fullscreen_output = shsurf->output;
2081 shsurf->fullscreen.type = method;
2082 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002083 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002084
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002085 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002086 shsurf->output->width,
2087 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002088}
2089
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002090static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002091shell_surface_set_fullscreen(struct wl_client *client,
2092 struct wl_resource *resource,
2093 uint32_t method,
2094 uint32_t framerate,
2095 struct wl_resource *output_resource)
2096{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002097 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002098 struct weston_output *output;
2099
2100 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002101 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002102 else
2103 output = NULL;
2104
2105 set_fullscreen(shsurf, method, framerate, output);
2106}
2107
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002108static void
2109set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2110{
2111 /* XXX: using the same fields for transient type */
2112 shsurf->transient.x = x;
2113 shsurf->transient.y = y;
2114 shsurf->transient.flags = flags;
2115 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2116}
2117
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002118static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002119
2120static void
2121destroy_shell_seat(struct wl_listener *listener, void *data)
2122{
2123 struct shell_seat *shseat =
2124 container_of(listener,
2125 struct shell_seat, seat_destroy_listener);
2126 struct shell_surface *shsurf, *prev = NULL;
2127
2128 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002129 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002130 shseat->popup_grab.client = NULL;
2131
2132 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2133 shsurf->popup.shseat = NULL;
2134 if (prev) {
2135 wl_list_init(&prev->popup.grab_link);
2136 }
2137 prev = shsurf;
2138 }
2139 wl_list_init(&prev->popup.grab_link);
2140 }
2141
2142 wl_list_remove(&shseat->seat_destroy_listener.link);
2143 free(shseat);
2144}
2145
2146static struct shell_seat *
2147create_shell_seat(struct weston_seat *seat)
2148{
2149 struct shell_seat *shseat;
2150
2151 shseat = calloc(1, sizeof *shseat);
2152 if (!shseat) {
2153 weston_log("no memory to allocate shell seat\n");
2154 return NULL;
2155 }
2156
2157 shseat->seat = seat;
2158 wl_list_init(&shseat->popup_grab.surfaces_list);
2159
2160 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2161 wl_signal_add(&seat->destroy_signal,
2162 &shseat->seat_destroy_listener);
2163
2164 return shseat;
2165}
2166
2167static struct shell_seat *
2168get_shell_seat(struct weston_seat *seat)
2169{
2170 struct wl_listener *listener;
2171
2172 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2173 if (listener == NULL)
2174 return create_shell_seat(seat);
2175
2176 return container_of(listener,
2177 struct shell_seat, seat_destroy_listener);
2178}
2179
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002180static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002181popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002182{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002183 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002184 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002185 struct shell_seat *shseat =
2186 container_of(grab, struct shell_seat, popup_grab.grab);
2187 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002188 wl_fixed_t sx, sy;
2189
Jason Ekstranda7af7042013-10-12 22:38:11 -05002190 view = weston_compositor_pick_view(pointer->seat->compositor,
2191 pointer->x, pointer->y,
2192 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002193
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194 if (view && view->surface->resource &&
2195 wl_resource_get_client(view->surface->resource) == client) {
2196 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002197 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002198 weston_pointer_set_focus(pointer, NULL,
2199 wl_fixed_from_int(0),
2200 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002201 }
2202}
2203
2204static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002205popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002206{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002207 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002208 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002209 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002210
Neil Roberts96d790e2013-09-19 17:32:00 +01002211 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 weston_view_from_global_fixed(pointer->focus,
2213 pointer->x, pointer->y,
2214 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002215 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002216 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002217}
2218
2219static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002220popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002221 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002222{
2223 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002224 struct shell_seat *shseat =
2225 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002226 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002227 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002228 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002229 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002230
Neil Roberts96d790e2013-09-19 17:32:00 +01002231 resource_list = &grab->pointer->focus_resource_list;
2232 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002233 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002234 wl_resource_for_each(resource, resource_list) {
2235 wl_pointer_send_button(resource, serial,
2236 time, button, state);
2237 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002238 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002239 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002240 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002241 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002242 }
2243
Daniel Stone4dbadb12012-05-30 16:31:51 +01002244 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002245 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002246}
2247
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002248static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002249 popup_grab_focus,
2250 popup_grab_motion,
2251 popup_grab_button,
2252};
2253
2254static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002255popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002256{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002257 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002258 struct shell_seat *shseat =
2259 container_of(grab, struct shell_seat, popup_grab.grab);
2260 struct shell_surface *shsurf;
2261 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002262
2263 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002264 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002265 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002266 shseat->popup_grab.grab.interface = NULL;
2267 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002268 /* Send the popup_done event to all the popups open */
2269 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002270 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002271 shsurf->popup.shseat = NULL;
2272 if (prev) {
2273 wl_list_init(&prev->popup.grab_link);
2274 }
2275 prev = shsurf;
2276 }
2277 wl_list_init(&prev->popup.grab_link);
2278 wl_list_init(&shseat->popup_grab.surfaces_list);
2279 }
2280}
2281
2282static void
2283add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2284{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002285 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002286
2287 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002288 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002289 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002290 /* We must make sure here that this popup was opened after
2291 * a mouse press, and not just by moving around with other
2292 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002293 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002294 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002295
Rob Bradforddfe31052013-06-26 19:49:11 +01002296 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002297 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002298 } else {
2299 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002300 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002301}
2302
2303static void
2304remove_popup_grab(struct shell_surface *shsurf)
2305{
2306 struct shell_seat *shseat = shsurf->popup.shseat;
2307
2308 wl_list_remove(&shsurf->popup.grab_link);
2309 wl_list_init(&shsurf->popup.grab_link);
2310 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002311 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002312 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002313 }
2314}
2315
2316static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002317shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002318{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002319 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002321
Jason Ekstranda7af7042013-10-12 22:38:11 -05002322 shsurf->surface->output = parent_view->output;
2323 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002324
Jason Ekstranda7af7042013-10-12 22:38:11 -05002325 weston_view_set_transform_parent(shsurf->view, parent_view);
2326 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2327 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002328
Kristian Høgsberge3148752013-05-06 23:19:49 -04002329 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002330 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002331 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002332 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002333 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002334 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002335}
2336
2337static void
2338shell_surface_set_popup(struct wl_client *client,
2339 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002340 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002341 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002342 struct wl_resource *parent_resource,
2343 int32_t x, int32_t y, uint32_t flags)
2344{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002345 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002346
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002347 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002348 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002349 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002350 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002351 shsurf->popup.x = x;
2352 shsurf->popup.y = y;
2353}
2354
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002355static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002356 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002357 shell_surface_move,
2358 shell_surface_resize,
2359 shell_surface_set_toplevel,
2360 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002361 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002362 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002363 shell_surface_set_maximized,
2364 shell_surface_set_title,
2365 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002366};
2367
2368static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002369destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002370{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002371 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2372
Giulio Camuffo5085a752013-03-25 21:42:45 +01002373 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2374 remove_popup_grab(shsurf);
2375 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002376
Alex Wubd3354b2012-04-17 17:20:49 +08002377 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002378 shell_surface_is_top_fullscreen(shsurf))
2379 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002380
Jason Ekstranda7af7042013-10-12 22:38:11 -05002381 if (shsurf->fullscreen.black_view)
2382 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002383
Alex Wubd3354b2012-04-17 17:20:49 +08002384 /* As destroy_resource() use wl_list_for_each_safe(),
2385 * we can always remove the listener.
2386 */
2387 wl_list_remove(&shsurf->surface_destroy_listener.link);
2388 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002389 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002390 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002391
Jason Ekstranda7af7042013-10-12 22:38:11 -05002392 weston_view_destroy(shsurf->view);
2393
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002394 wl_list_remove(&shsurf->link);
2395 free(shsurf);
2396}
2397
2398static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002399shell_destroy_shell_surface(struct wl_resource *resource)
2400{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002401 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002402
2403 destroy_shell_surface(shsurf);
2404}
2405
2406static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002407shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002408{
2409 struct shell_surface *shsurf = container_of(listener,
2410 struct shell_surface,
2411 surface_destroy_listener);
2412
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002413 if (shsurf->resource)
2414 wl_resource_destroy(shsurf->resource);
2415 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002416 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002417}
2418
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002419static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002420shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002421
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002422static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002423get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002424{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002425 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002426 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002427 else
2428 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002429}
2430
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002431static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002432create_shell_surface(void *shell, struct weston_surface *surface,
2433 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002434{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002435 struct shell_surface *shsurf;
2436
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002437 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002438 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002439 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002440 }
2441
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002442 shsurf = calloc(1, sizeof *shsurf);
2443 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002444 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002445 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002446 }
2447
Jason Ekstranda7af7042013-10-12 22:38:11 -05002448 shsurf->view = weston_view_create(surface);
2449 if (!shsurf->view) {
2450 weston_log("no memory to allocate shell surface\n");
2451 free(shsurf);
2452 return NULL;
2453 }
2454
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002455 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002456 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002457
Tiago Vignattibc052c92012-04-19 16:18:18 +03002458 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002459 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002460 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002461 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002462 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002463 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2464 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002465 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002466 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002467 wl_list_init(&shsurf->fullscreen.transform.link);
2468
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002469 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002470 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002471 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002472 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002473
2474 /* init link so its safe to always remove it in destroy_shell_surface */
2475 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002476 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002477
Pekka Paalanen460099f2012-01-20 16:48:25 +02002478 /* empty when not in use */
2479 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002480 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002481
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002482 wl_list_init(&shsurf->workspace_transform.link);
2483
Pekka Paalanen98262232011-12-01 10:42:22 +02002484 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002485 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002486
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002487 shsurf->client = client;
2488
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002489 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002490}
2491
Jason Ekstranda7af7042013-10-12 22:38:11 -05002492static struct weston_view *
2493get_primary_view(void *shell, struct shell_surface *shsurf)
2494{
2495 return shsurf->view;
2496}
2497
Tiago Vignattibc052c92012-04-19 16:18:18 +03002498static void
2499shell_get_shell_surface(struct wl_client *client,
2500 struct wl_resource *resource,
2501 uint32_t id,
2502 struct wl_resource *surface_resource)
2503{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002504 struct weston_surface *surface =
2505 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002506 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002507 struct shell_surface *shsurf;
2508
2509 if (get_shell_surface(surface)) {
2510 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002511 WL_DISPLAY_ERROR_INVALID_OBJECT,
2512 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002513 return;
2514 }
2515
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002516 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002517 if (!shsurf) {
2518 wl_resource_post_error(surface_resource,
2519 WL_DISPLAY_ERROR_INVALID_OBJECT,
2520 "surface->configure already set");
2521 return;
2522 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002523
Jason Ekstranda85118c2013-06-27 20:17:02 -05002524 shsurf->resource =
2525 wl_resource_create(client,
2526 &wl_shell_surface_interface, 1, id);
2527 wl_resource_set_implementation(shsurf->resource,
2528 &shell_surface_implementation,
2529 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002530}
2531
2532static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002533 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002534};
2535
Kristian Høgsberg07937562011-04-12 17:25:42 -04002536static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002537shell_fade(struct desktop_shell *shell, enum fade_type type);
2538
2539static int
2540screensaver_timeout(void *data)
2541{
2542 struct desktop_shell *shell = data;
2543
2544 shell_fade(shell, FADE_OUT);
2545
2546 return 1;
2547}
2548
2549static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002550handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002551{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002552 struct desktop_shell *shell =
2553 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002554
Pekka Paalanen18027e52011-12-02 16:31:49 +02002555 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002556
2557 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002558 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002559}
2560
2561static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002562launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002563{
2564 if (shell->screensaver.binding)
2565 return;
2566
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002567 if (!shell->screensaver.path) {
2568 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002569 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002570 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002571
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002572 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002573 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002574 return;
2575 }
2576
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002577 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002578 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002579 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002580 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002581}
2582
2583static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002584terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002585{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002586 if (shell->screensaver.process.pid == 0)
2587 return;
2588
2589 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002590}
2591
2592static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002594{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002595 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002596
Giulio Camuffo184df502013-02-21 11:29:21 +01002597 if (width == 0)
2598 return;
2599
Jason Ekstranda7af7042013-10-12 22:38:11 -05002600 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2601 if (v->output == ev->output && v != ev) {
2602 weston_view_unmap(v);
2603 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002604 }
2605 }
2606
Jason Ekstranda7af7042013-10-12 22:38:11 -05002607 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002608
Jason Ekstranda7af7042013-10-12 22:38:11 -05002609 if (wl_list_empty(&ev->layer_link)) {
2610 wl_list_insert(&layer->view_list, &ev->layer_link);
2611 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002612 }
2613}
2614
2615static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002616background_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 -04002617{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002618 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002619 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002620
Jason Ekstranda7af7042013-10-12 22:38:11 -05002621 view = container_of(es->views.next, struct weston_view, surface_link);
2622
2623 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002624}
2625
2626static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002627desktop_shell_set_background(struct wl_client *client,
2628 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002629 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002630 struct wl_resource *surface_resource)
2631{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002632 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002633 struct weston_surface *surface =
2634 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002635 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002636
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002637 if (surface->configure) {
2638 wl_resource_post_error(surface_resource,
2639 WL_DISPLAY_ERROR_INVALID_OBJECT,
2640 "surface role already assigned");
2641 return;
2642 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002643
Jason Ekstranda7af7042013-10-12 22:38:11 -05002644 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2645 weston_view_destroy(view);
2646 view = weston_view_create(surface);
2647
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002648 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002649 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002650 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002651 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002652 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002653 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002654 surface->output->width,
2655 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002656}
2657
2658static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002659panel_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 -04002660{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002661 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002662 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002663
Jason Ekstranda7af7042013-10-12 22:38:11 -05002664 view = container_of(es->views.next, struct weston_view, surface_link);
2665
2666 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002667}
2668
2669static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002670desktop_shell_set_panel(struct wl_client *client,
2671 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002672 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002673 struct wl_resource *surface_resource)
2674{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002675 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002676 struct weston_surface *surface =
2677 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002678 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002679
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002680 if (surface->configure) {
2681 wl_resource_post_error(surface_resource,
2682 WL_DISPLAY_ERROR_INVALID_OBJECT,
2683 "surface role already assigned");
2684 return;
2685 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002686
Jason Ekstranda7af7042013-10-12 22:38:11 -05002687 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2688 weston_view_destroy(view);
2689 view = weston_view_create(surface);
2690
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002691 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002692 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002693 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002694 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002695 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002696 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002697 surface->output->width,
2698 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002699}
2700
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002701static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002702lock_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 -04002703{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002704 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002705 struct weston_view *view;
2706
2707 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002708
Giulio Camuffo184df502013-02-21 11:29:21 +01002709 if (width == 0)
2710 return;
2711
Jason Ekstranda7af7042013-10-12 22:38:11 -05002712 surface->width = width;
2713 surface->height = height;
2714 view->geometry.width = width;
2715 view->geometry.height = height;
2716 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002717
2718 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002719 wl_list_insert(&shell->lock_layer.view_list,
2720 &view->layer_link);
2721 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002722 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002723 }
2724}
2725
2726static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002727handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002728{
Tiago Vignattibe143262012-04-16 17:31:41 +03002729 struct desktop_shell *shell =
2730 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002731
Martin Minarik6d118362012-06-07 18:01:59 +02002732 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002733 shell->lock_surface = NULL;
2734}
2735
2736static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002737desktop_shell_set_lock_surface(struct wl_client *client,
2738 struct wl_resource *resource,
2739 struct wl_resource *surface_resource)
2740{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002741 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002742 struct weston_surface *surface =
2743 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002744
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002745 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002746
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002747 if (!shell->locked)
2748 return;
2749
Pekka Paalanen98262232011-12-01 10:42:22 +02002750 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002751
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002752 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002753 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002754 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002755
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002756 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002757 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002758}
2759
2760static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002761resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002762{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002763 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002764
Pekka Paalanen77346a62011-11-30 16:26:35 +02002765 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002766
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002767 wl_list_remove(&shell->lock_layer.link);
2768 wl_list_insert(&shell->compositor->cursor_layer.link,
2769 &shell->fullscreen_layer.link);
2770 wl_list_insert(&shell->fullscreen_layer.link,
2771 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002772 if (shell->showing_input_panels) {
2773 wl_list_insert(&shell->panel_layer.link,
2774 &shell->input_panel_layer.link);
2775 wl_list_insert(&shell->input_panel_layer.link,
2776 &ws->layer.link);
2777 } else {
2778 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2779 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002780
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002781 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002782
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002783 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002784 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002785 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002786}
2787
2788static void
2789desktop_shell_unlock(struct wl_client *client,
2790 struct wl_resource *resource)
2791{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002792 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002793
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002794 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002795
2796 if (shell->locked)
2797 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002798}
2799
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002800static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002801desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002802 struct wl_resource *resource,
2803 struct wl_resource *surface_resource)
2804{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002805 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002806
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002807 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002808}
2809
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002810static void
2811desktop_shell_desktop_ready(struct wl_client *client,
2812 struct wl_resource *resource)
2813{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002814 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002815
2816 shell_fade_startup(shell);
2817}
2818
Kristian Høgsberg75840622011-09-06 13:48:16 -04002819static const struct desktop_shell_interface desktop_shell_implementation = {
2820 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002821 desktop_shell_set_panel,
2822 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002823 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002824 desktop_shell_set_grab_surface,
2825 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002826};
2827
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002828static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002829get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002830{
2831 struct shell_surface *shsurf;
2832
2833 shsurf = get_shell_surface(surface);
2834 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002835 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002836 return shsurf->type;
2837}
2838
Kristian Høgsberg75840622011-09-06 13:48:16 -04002839static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002840move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002841{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002842 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002843 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002844 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002845 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002846
Pekka Paalanen01388e22013-04-25 13:57:44 +03002847 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002848 if (surface == NULL)
2849 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002850
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002851 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002852 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2853 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002854 return;
2855
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002856 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002857}
2858
2859static void
Neil Robertsaba0f252013-10-03 16:43:05 +01002860touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
2861{
2862 struct weston_surface *focus =
2863 (struct weston_surface *) seat->touch->focus;
2864 struct weston_surface *surface;
2865 struct shell_surface *shsurf;
2866
2867 surface = weston_surface_get_main_surface(focus);
2868 if (surface == NULL)
2869 return;
2870
2871 shsurf = get_shell_surface(surface);
2872 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2873 shsurf->type == SHELL_SURFACE_MAXIMIZED)
2874 return;
2875
2876 surface_touch_move(shsurf, (struct weston_seat *) seat);
2877}
2878
2879static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002880resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002881{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002882 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002883 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002884 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002885 uint32_t edges = 0;
2886 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002887 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002888
Pekka Paalanen01388e22013-04-25 13:57:44 +03002889 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002890 if (surface == NULL)
2891 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002892
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002893 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002894 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2895 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002896 return;
2897
Jason Ekstranda7af7042013-10-12 22:38:11 -05002898 weston_view_from_global(shsurf->view,
2899 wl_fixed_to_int(seat->pointer->grab_x),
2900 wl_fixed_to_int(seat->pointer->grab_y),
2901 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002902
Jason Ekstranda7af7042013-10-12 22:38:11 -05002903 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002904 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002905 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002906 edges |= 0;
2907 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002908 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002909
Jason Ekstranda7af7042013-10-12 22:38:11 -05002910 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002911 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002912 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002913 edges |= 0;
2914 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002915 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002916
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002917 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002918}
2919
2920static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002921surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002922 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002923{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002924 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002925 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002926 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002927 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002928 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002929
Pekka Paalanen01388e22013-04-25 13:57:44 +03002930 /* XXX: broken for windows containing sub-surfaces */
2931 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002932 if (surface == NULL)
2933 return;
2934
2935 shsurf = get_shell_surface(surface);
2936 if (!shsurf)
2937 return;
2938
Jason Ekstranda7af7042013-10-12 22:38:11 -05002939 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002940
Jason Ekstranda7af7042013-10-12 22:38:11 -05002941 if (shsurf->view->alpha > 1.0)
2942 shsurf->view->alpha = 1.0;
2943 if (shsurf->view->alpha < step)
2944 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002945
Jason Ekstranda7af7042013-10-12 22:38:11 -05002946 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002947 weston_surface_damage(surface);
2948}
2949
2950static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002951do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002952 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002953{
Daniel Stone37816df2012-05-16 18:45:18 +01002954 struct weston_seat *ws = (struct weston_seat *) seat;
2955 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002956 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002957 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002958
2959 wl_list_for_each(output, &compositor->output_list, link) {
2960 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002961 wl_fixed_to_double(seat->pointer->x),
2962 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002963 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002964 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002965 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002966 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002967 increment = -output->zoom.increment;
2968 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002969 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002970 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002971 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002972 else
2973 increment = 0;
2974
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002975 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002976
Scott Moreaue6603982012-06-11 13:07:51 -06002977 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002978 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002979 else if (output->zoom.level > output->zoom.max_level)
2980 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002981 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002982 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002983 output->disable_planes++;
2984 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002985
Scott Moreaue6603982012-06-11 13:07:51 -06002986 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002987
Jason Ekstranda7af7042013-10-12 22:38:11 -05002988 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002989 }
2990 }
2991}
2992
Scott Moreauccbf29d2012-02-22 14:21:41 -07002993static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002994zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002995 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002996{
2997 do_zoom(seat, time, 0, axis, value);
2998}
2999
3000static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003001zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003002 void *data)
3003{
3004 do_zoom(seat, time, key, 0, 0);
3005}
3006
3007static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003008terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003009 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003010{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003011 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003012
Daniel Stone325fc2d2012-05-30 16:31:58 +01003013 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003014}
3015
3016static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003017rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003018{
3019 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003020 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003021 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003022 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003023 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003024
3025 if (!shsurf)
3026 return;
3027
Jason Ekstranda7af7042013-10-12 22:38:11 -05003028 cx = 0.5f * shsurf->view->geometry.width;
3029 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003030
Daniel Stone37816df2012-05-16 18:45:18 +01003031 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3032 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003033 r = sqrtf(dx * dx + dy * dy);
3034
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003035 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003036 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003037
3038 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003039 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003040 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003041
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003042 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003043 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003044
3045 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003046 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003047 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003048 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003049 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003050
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003051 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003052 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003053 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003054 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003055 wl_list_init(&shsurf->rotation.transform.link);
3056 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003057 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003058 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003059
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003060 /* We need to adjust the position of the surface
3061 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003062 cposx = shsurf->view->geometry.x + cx;
3063 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003064 dposx = rotate->center.x - cposx;
3065 dposy = rotate->center.y - cposy;
3066 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003067 weston_view_set_position(shsurf->view,
3068 shsurf->view->geometry.x + dposx,
3069 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003070 }
3071
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003072 /* Repaint implies weston_surface_update_transform(), which
3073 * lazily applies the damage due to rotation update.
3074 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003075 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003076}
3077
3078static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003079rotate_grab_button(struct weston_pointer_grab *grab,
3080 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003081{
3082 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003083 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003084 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003085 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003086 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003087
Daniel Stone4dbadb12012-05-30 16:31:51 +01003088 if (pointer->button_count == 0 &&
3089 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003090 if (shsurf)
3091 weston_matrix_multiply(&shsurf->rotation.rotation,
3092 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003093 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003094 free(rotate);
3095 }
3096}
3097
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003098static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003099 noop_grab_focus,
3100 rotate_grab_motion,
3101 rotate_grab_button,
3102};
3103
3104static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003105surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003106{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003107 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003108 float dx, dy;
3109 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003110
Pekka Paalanen460099f2012-01-20 16:48:25 +02003111 rotate = malloc(sizeof *rotate);
3112 if (!rotate)
3113 return;
3114
Jason Ekstranda7af7042013-10-12 22:38:11 -05003115 weston_view_to_global_float(surface->view,
3116 surface->view->geometry.width * 0.5f,
3117 surface->view->geometry.height * 0.5f,
3118 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003119
Daniel Stone37816df2012-05-16 18:45:18 +01003120 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3121 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003122 r = sqrtf(dx * dx + dy * dy);
3123 if (r > 20.0f) {
3124 struct weston_matrix inverse;
3125
3126 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003127 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003128 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003129
3130 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003131 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003132 } else {
3133 weston_matrix_init(&surface->rotation.rotation);
3134 weston_matrix_init(&rotate->rotation);
3135 }
3136
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003137 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3138 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003139}
3140
3141static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003142rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003143 void *data)
3144{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003145 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003146 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003147 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003148 struct shell_surface *surface;
3149
Pekka Paalanen01388e22013-04-25 13:57:44 +03003150 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003151 if (base_surface == NULL)
3152 return;
3153
3154 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003155 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3156 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003157 return;
3158
3159 surface_rotate(surface, seat);
3160}
3161
3162static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003163lower_fullscreen_layer(struct desktop_shell *shell)
3164{
3165 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003166 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003167
3168 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003169 wl_list_for_each_reverse_safe(view, prev,
3170 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003171 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003172 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003173}
3174
3175static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003176activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003177 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003178{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003179 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003180 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003181 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003182 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003183
Pekka Paalanen01388e22013-04-25 13:57:44 +03003184 main_surface = weston_surface_get_main_surface(es);
3185
Daniel Stone37816df2012-05-16 18:45:18 +01003186 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003187
Jonas Ådahl8538b222012-08-29 22:13:03 +02003188 state = ensure_focus_state(shell, seat);
3189 if (state == NULL)
3190 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003191
3192 state->keyboard_focus = es;
3193 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003194 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003195
Pekka Paalanen01388e22013-04-25 13:57:44 +03003196 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003197 case SHELL_SURFACE_FULLSCREEN:
3198 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003199 shell_stack_fullscreen(get_shell_surface(main_surface));
3200 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08003201 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003202 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003203 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003204 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003205 main_view = get_default_view(main_surface);
3206 if (main_view)
3207 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003208 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003209 }
3210}
3211
Alex Wu21858432012-04-01 20:13:08 +08003212/* no-op func for checking black surface */
3213static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003214black_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 +08003215{
3216}
3217
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003218static bool
Alex Wu21858432012-04-01 20:13:08 +08003219is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3220{
3221 if (es->configure == black_surface_configure) {
3222 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003223 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003224 return true;
3225 }
3226 return false;
3227}
3228
Kristian Høgsberg75840622011-09-06 13:48:16 -04003229static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003230activate_binding(struct weston_seat *seat,
3231 struct desktop_shell *shell,
3232 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003233{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003234 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003235
Alex Wu9c35e6b2012-03-05 14:13:13 +08003236 if (!focus)
3237 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003238 focus = seat->pointer->focus->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003239
Pekka Paalanen01388e22013-04-25 13:57:44 +03003240 if (is_black_surface(focus, &main_surface))
3241 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003242
Pekka Paalanen01388e22013-04-25 13:57:44 +03003243 main_surface = weston_surface_get_main_surface(focus);
3244 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003245 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003246
Neil Robertsa28c6932013-10-03 16:43:04 +01003247 activate(shell, focus, seat);
3248}
3249
3250static void
3251click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3252 void *data)
3253{
3254 if (seat->pointer->grab != &seat->pointer->default_grab)
3255 return;
3256
3257 activate_binding(seat, data,
3258 (struct weston_surface *) seat->pointer->focus);
3259}
3260
3261static void
3262touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3263{
3264 if (seat->touch->grab != &seat->touch->default_grab)
3265 return;
3266
3267 activate_binding(seat, data,
3268 (struct weston_surface *) seat->touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003269}
3270
3271static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003272lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003273{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003274 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003275
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003276 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003277 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003278 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003279 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003280
3281 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003282
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003283 /* Hide all surfaces by removing the fullscreen, panel and
3284 * toplevel layers. This way nothing else can show or receive
3285 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003286
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003287 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003288 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003289 if (shell->showing_input_panels)
3290 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003291 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003292 wl_list_insert(&shell->compositor->cursor_layer.link,
3293 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003294
Pekka Paalanen77346a62011-11-30 16:26:35 +02003295 launch_screensaver(shell);
3296
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003297 /* TODO: disable bindings that should not work while locked. */
3298
3299 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003300}
3301
3302static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003303unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003304{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003305 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003306 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003307 return;
3308 }
3309
3310 /* If desktop-shell client has gone away, unlock immediately. */
3311 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003312 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003313 return;
3314 }
3315
3316 if (shell->prepare_event_sent)
3317 return;
3318
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003319 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003320 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003321}
3322
3323static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003324shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003325{
3326 struct desktop_shell *shell = data;
3327
3328 shell->fade.animation = NULL;
3329
3330 switch (shell->fade.type) {
3331 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003332 weston_surface_destroy(shell->fade.view->surface);
3333 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003334 break;
3335 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003336 lock(shell);
3337 break;
3338 }
3339}
3340
Jason Ekstranda7af7042013-10-12 22:38:11 -05003341static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003342shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003343{
3344 struct weston_compositor *compositor = shell->compositor;
3345 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003346 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003347
3348 surface = weston_surface_create(compositor);
3349 if (!surface)
3350 return NULL;
3351
Jason Ekstranda7af7042013-10-12 22:38:11 -05003352 view = weston_view_create(surface);
3353 if (!view) {
3354 weston_surface_destroy(surface);
3355 return NULL;
3356 }
3357
3358 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003359 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003360 wl_list_insert(&compositor->fade_layer.view_list,
3361 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003362 pixman_region32_init(&surface->input);
3363
Jason Ekstranda7af7042013-10-12 22:38:11 -05003364 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003365}
3366
3367static void
3368shell_fade(struct desktop_shell *shell, enum fade_type type)
3369{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003370 float tint;
3371
3372 switch (type) {
3373 case FADE_IN:
3374 tint = 0.0;
3375 break;
3376 case FADE_OUT:
3377 tint = 1.0;
3378 break;
3379 default:
3380 weston_log("shell: invalid fade type\n");
3381 return;
3382 }
3383
3384 shell->fade.type = type;
3385
Jason Ekstranda7af7042013-10-12 22:38:11 -05003386 if (shell->fade.view == NULL) {
3387 shell->fade.view = shell_fade_create_surface(shell);
3388 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003389 return;
3390
Jason Ekstranda7af7042013-10-12 22:38:11 -05003391 shell->fade.view->alpha = 1.0 - tint;
3392 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003393 }
3394
3395 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003396 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003397 else
3398 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003399 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003400 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003401 shell_fade_done, shell);
3402}
3403
3404static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003405do_shell_fade_startup(void *data)
3406{
3407 struct desktop_shell *shell = data;
3408
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003409 if (shell->startup_animation_type == ANIMATION_FADE)
3410 shell_fade(shell, FADE_IN);
3411 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003412 weston_surface_destroy(shell->fade.view->surface);
3413 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003414 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003415}
3416
3417static void
3418shell_fade_startup(struct desktop_shell *shell)
3419{
3420 struct wl_event_loop *loop;
3421
3422 if (!shell->fade.startup_timer)
3423 return;
3424
3425 wl_event_source_remove(shell->fade.startup_timer);
3426 shell->fade.startup_timer = NULL;
3427
3428 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3429 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3430}
3431
3432static int
3433fade_startup_timeout(void *data)
3434{
3435 struct desktop_shell *shell = data;
3436
3437 shell_fade_startup(shell);
3438 return 0;
3439}
3440
3441static void
3442shell_fade_init(struct desktop_shell *shell)
3443{
3444 /* Make compositor output all black, and wait for the desktop-shell
3445 * client to signal it is ready, then fade in. The timer triggers a
3446 * fade-in, in case the desktop-shell client takes too long.
3447 */
3448
3449 struct wl_event_loop *loop;
3450
Jason Ekstranda7af7042013-10-12 22:38:11 -05003451 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003452 weston_log("%s: warning: fade surface already exists\n",
3453 __func__);
3454 return;
3455 }
3456
Jason Ekstranda7af7042013-10-12 22:38:11 -05003457 shell->fade.view = shell_fade_create_surface(shell);
3458 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003459 return;
3460
Jason Ekstranda7af7042013-10-12 22:38:11 -05003461 weston_view_update_transform(shell->fade.view);
3462 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003463
3464 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3465 shell->fade.startup_timer =
3466 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3467 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3468}
3469
3470static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003471idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003472{
3473 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003474 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003475
3476 shell_fade(shell, FADE_OUT);
3477 /* lock() is called from shell_fade_done() */
3478}
3479
3480static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003481wake_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, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003485
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003486 unlock(shell);
3487}
3488
3489static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003490show_input_panels(struct wl_listener *listener, void *data)
3491{
3492 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003493 container_of(listener, struct desktop_shell,
3494 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003495 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003496
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003497 shell->text_input.surface = (struct weston_surface*)data;
3498
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003499 if (shell->showing_input_panels)
3500 return;
3501
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003502 shell->showing_input_panels = true;
3503
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003504 if (!shell->locked)
3505 wl_list_insert(&shell->panel_layer.link,
3506 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003507
Jason Ekstranda7af7042013-10-12 22:38:11 -05003508 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003509 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003510 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003511 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003512 wl_list_insert(&shell->input_panel_layer.view_list,
3513 &ipsurf->view->layer_link);
3514 weston_view_geometry_dirty(ipsurf->view);
3515 weston_view_update_transform(ipsurf->view);
3516 weston_surface_damage(ipsurf->surface);
3517 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
3518 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003519 }
3520}
3521
3522static void
3523hide_input_panels(struct wl_listener *listener, void *data)
3524{
3525 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003526 container_of(listener, struct desktop_shell,
3527 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003528 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003529
Jan Arne Petersen61381972013-01-31 15:52:21 +01003530 if (!shell->showing_input_panels)
3531 return;
3532
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003533 shell->showing_input_panels = false;
3534
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003535 if (!shell->locked)
3536 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003537
Jason Ekstranda7af7042013-10-12 22:38:11 -05003538 wl_list_for_each_safe(view, next,
3539 &shell->input_panel_layer.view_list, layer_link)
3540 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003541}
3542
3543static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003544update_input_panels(struct wl_listener *listener, void *data)
3545{
3546 struct desktop_shell *shell =
3547 container_of(listener, struct desktop_shell,
3548 update_input_panel_listener);
3549
3550 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3551}
3552
3553static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003554center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003555{
Giulio Camuffob8366642013-04-25 13:57:46 +03003556 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003557 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003558
Jason Ekstranda7af7042013-10-12 22:38:11 -05003559 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03003560
3561 x = output->x + (output->width - width) / 2 - surf_x / 2;
3562 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003563
Jason Ekstranda7af7042013-10-12 22:38:11 -05003564 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003565}
3566
3567static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003568weston_view_set_initial_position(struct weston_view *view,
3569 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003570{
3571 struct weston_compositor *compositor = shell->compositor;
3572 int ix = 0, iy = 0;
3573 int range_x, range_y;
3574 int dx, dy, x, y, panel_height;
3575 struct weston_output *output, *target_output = NULL;
3576 struct weston_seat *seat;
3577
3578 /* As a heuristic place the new window on the same output as the
3579 * pointer. Falling back to the output containing 0, 0.
3580 *
3581 * TODO: Do something clever for touch too?
3582 */
3583 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003584 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003585 ix = wl_fixed_to_int(seat->pointer->x);
3586 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003587 break;
3588 }
3589 }
3590
3591 wl_list_for_each(output, &compositor->output_list, link) {
3592 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3593 target_output = output;
3594 break;
3595 }
3596 }
3597
3598 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003599 weston_view_set_position(view, 10 + random() % 400,
3600 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003601 return;
3602 }
3603
3604 /* Valid range within output where the surface will still be onscreen.
3605 * If this is negative it means that the surface is bigger than
3606 * output.
3607 */
3608 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003609 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003610 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05003611 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003612
Rob Bradford4cb88c72012-08-13 15:18:44 +01003613 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003614 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003615 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003616 dx = 0;
3617
3618 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003619 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003620 else
3621 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003622
3623 x = target_output->x + dx;
3624 y = target_output->y + dy;
3625
Jason Ekstranda7af7042013-10-12 22:38:11 -05003626 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003627}
3628
3629static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003630map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003631 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003632{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003633 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003634 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003635 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003636 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003637 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003638 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003639
Jason Ekstranda7af7042013-10-12 22:38:11 -05003640 shsurf->view->geometry.width = width;
3641 shsurf->view->geometry.height = height;
3642 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003643
3644 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003645 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02003646 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003647 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003648 break;
Alex Wu4539b082012-03-01 12:57:46 +08003649 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003650 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003651 shell_map_fullscreen(shsurf);
3652 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003653 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003654 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003655 panel_height = get_output_panel_height(shell, shsurf->output);
3656 surface_subsurfaces_boundingbox(shsurf->surface,
3657 &surf_x, &surf_y, NULL, NULL);
3658 weston_view_set_position(shsurf->view,
3659 shsurf->output->x - surf_x,
3660 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003661 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003662 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003663 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003664 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003665 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003666 weston_view_set_position(shsurf->view,
3667 shsurf->view->geometry.x + sx,
3668 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003669 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003670 default:
3671 ;
3672 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003673
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003674 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003675 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003676 case SHELL_SURFACE_POPUP:
3677 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003678 /* TODO: Handle a parent with multiple views */
3679 parent = get_default_view(shsurf->parent);
3680 if (parent) {
3681 wl_list_remove(&shsurf->view->layer_link);
3682 wl_list_insert(parent->layer_link.prev,
3683 &shsurf->view->layer_link);
3684 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003685 break;
Alex Wu4539b082012-03-01 12:57:46 +08003686 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003687 case SHELL_SURFACE_NONE:
3688 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003689 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003690 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003691 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003692 wl_list_remove(&shsurf->view->layer_link);
3693 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003694 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003695 }
3696
Jason Ekstranda7af7042013-10-12 22:38:11 -05003697 if (shsurf->type != SHELL_SURFACE_NONE) {
3698 weston_view_update_transform(shsurf->view);
3699 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
3700 shsurf->surface->output = shsurf->output;
3701 shsurf->view->output = shsurf->output;
3702 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003703 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003704
Jason Ekstranda7af7042013-10-12 22:38:11 -05003705 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003706 /* XXX: xwayland's using the same fields for transient type */
3707 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003708 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003709 if (shsurf->transient.flags ==
3710 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3711 break;
3712 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003713 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003714 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003715 if (!shell->locked) {
3716 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003717 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01003718 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003719 break;
3720 default:
3721 break;
3722 }
3723
Jason Ekstranda7af7042013-10-12 22:38:11 -05003724 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003725 {
3726 switch (shell->win_animation_type) {
3727 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003728 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003729 break;
3730 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003731 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003732 break;
3733 default:
3734 break;
3735 }
3736 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003737}
3738
3739static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003740configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003741 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003742{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003743 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3744 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003745 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03003746 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003747
Pekka Paalanen77346a62011-11-30 16:26:35 +02003748 shsurf = get_shell_surface(surface);
3749 if (shsurf)
3750 surface_type = shsurf->type;
3751
Jason Ekstranda7af7042013-10-12 22:38:11 -05003752 /* TODO:
3753 * This should probably be changed to be more shell_surface
3754 * dependent
3755 */
3756 wl_list_for_each(view, &surface->views, surface_link)
3757 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003758
3759 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003760 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003761 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003762 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003763 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003764 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003765 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003766 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3767 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003768 shsurf->view->geometry.x = shsurf->output->x - surf_x;
3769 shsurf->view->geometry.y = shsurf->output->y +
3770 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003771 break;
Alex Wu4539b082012-03-01 12:57:46 +08003772 case SHELL_SURFACE_TOPLEVEL:
3773 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003774 default:
3775 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003776 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003777
Alex Wu4539b082012-03-01 12:57:46 +08003778 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003779 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003780 wl_list_for_each(view, &surface->views, surface_link)
3781 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003782
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003783 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003784 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003785 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003786}
3787
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003788static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003789shell_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 +03003790{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003791 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003792 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003793
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003794 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003795
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003796 if (!weston_surface_is_mapped(es) &&
3797 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003798 remove_popup_grab(shsurf);
3799 }
3800
Giulio Camuffo184df502013-02-21 11:29:21 +01003801 if (width == 0)
3802 return;
3803
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003804 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003805 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003806 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003807 type_changed = 1;
3808 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003809
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003810 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003811 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003812 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05003813 shsurf->view->geometry.width != width ||
3814 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003815 float from_x, from_y;
3816 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003817
Jason Ekstranda7af7042013-10-12 22:38:11 -05003818 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
3819 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003820 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003821 shsurf->view->geometry.x + to_x - from_x,
3822 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003823 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003824 }
3825}
3826
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003827static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003828
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003829static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003830desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003831{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003832 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003833 struct desktop_shell *shell =
3834 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003835
3836 shell->child.process.pid = 0;
3837 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003838
3839 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3840 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003841 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003842 shell->child.deathstamp = time;
3843 shell->child.deathcount = 0;
3844 }
3845
3846 shell->child.deathcount++;
3847 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003848 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003849 return;
3850 }
3851
Martin Minarik6d118362012-06-07 18:01:59 +02003852 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003853 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003854 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003855}
3856
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003857static void
3858launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003859{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003860 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003861 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003862
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003863 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003864 &shell->child.process,
3865 shell_exe,
3866 desktop_shell_sigchld);
3867
3868 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003869 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003870}
3871
3872static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003873bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3874{
Tiago Vignattibe143262012-04-16 17:31:41 +03003875 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003876 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003877
Jason Ekstranda85118c2013-06-27 20:17:02 -05003878 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3879 if (resource)
3880 wl_resource_set_implementation(resource, &shell_implementation,
3881 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003882}
3883
Kristian Høgsberg75840622011-09-06 13:48:16 -04003884static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003885unbind_desktop_shell(struct wl_resource *resource)
3886{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003887 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003888
3889 if (shell->locked)
3890 resume_desktop(shell);
3891
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003892 shell->child.desktop_shell = NULL;
3893 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003894}
3895
3896static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003897bind_desktop_shell(struct wl_client *client,
3898 void *data, uint32_t version, uint32_t id)
3899{
Tiago Vignattibe143262012-04-16 17:31:41 +03003900 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003901 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003902
Jason Ekstranda85118c2013-06-27 20:17:02 -05003903 resource = wl_resource_create(client, &desktop_shell_interface,
3904 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003905
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003906 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003907 wl_resource_set_implementation(resource,
3908 &desktop_shell_implementation,
3909 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003910 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003911
3912 if (version < 2)
3913 shell_fade_startup(shell);
3914
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003915 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003916 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003917
3918 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3919 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003920 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003921}
3922
Pekka Paalanen6e168112011-11-24 11:34:05 +02003923static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003924screensaver_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 -04003925{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003926 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003927 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003928
Giulio Camuffo184df502013-02-21 11:29:21 +01003929 if (width == 0)
3930 return;
3931
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003932 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003933 if (!shell->locked)
3934 return;
3935
Jason Ekstranda7af7042013-10-12 22:38:11 -05003936 view = container_of(surface->views.next, struct weston_view, surface_link);
3937 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003938
Jason Ekstranda7af7042013-10-12 22:38:11 -05003939 if (wl_list_empty(&view->layer_link)) {
3940 wl_list_insert(shell->lock_layer.view_list.prev,
3941 &view->layer_link);
3942 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003943 wl_event_source_timer_update(shell->screensaver.timer,
3944 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003945 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003946 }
3947}
3948
3949static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003950screensaver_set_surface(struct wl_client *client,
3951 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003952 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003953 struct wl_resource *output_resource)
3954{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003955 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003956 struct weston_surface *surface =
3957 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003958 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003959 struct weston_view *view, *next;
3960
3961 /* Make sure we only have one view */
3962 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3963 weston_view_destroy(view);
3964 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003965
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003966 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003967 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003968 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003969}
3970
3971static const struct screensaver_interface screensaver_implementation = {
3972 screensaver_set_surface
3973};
3974
3975static void
3976unbind_screensaver(struct wl_resource *resource)
3977{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003978 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003979
Pekka Paalanen77346a62011-11-30 16:26:35 +02003980 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003981}
3982
3983static void
3984bind_screensaver(struct wl_client *client,
3985 void *data, uint32_t version, uint32_t id)
3986{
Tiago Vignattibe143262012-04-16 17:31:41 +03003987 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003988 struct wl_resource *resource;
3989
Jason Ekstranda85118c2013-06-27 20:17:02 -05003990 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003991
Pekka Paalanen77346a62011-11-30 16:26:35 +02003992 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003993 wl_resource_set_implementation(resource,
3994 &screensaver_implementation,
3995 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003996 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003997 return;
3998 }
3999
4000 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4001 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004002 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004003}
4004
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004005static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004006input_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 -04004007{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004008 struct input_panel_surface *ip_surface = surface->configure_private;
4009 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004010 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004011 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004012
Giulio Camuffo184df502013-02-21 11:29:21 +01004013 if (width == 0)
4014 return;
4015
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004016 if (!weston_surface_is_mapped(surface)) {
4017 if (!shell->showing_input_panels)
4018 return;
4019
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004020 show_surface = 1;
4021 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004022
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004023 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004024
4025 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004026 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4027 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004028 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004029 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4030 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004031 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004032
Jason Ekstranda7af7042013-10-12 22:38:11 -05004033 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004034
4035 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004036 wl_list_insert(&shell->input_panel_layer.view_list,
4037 &ip_surface->view->layer_link);
4038 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004039 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004040 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004041 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004042}
4043
4044static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004045destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004046{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004047 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4048
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004049 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004050 wl_list_remove(&input_panel_surface->link);
4051
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004052 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004053 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004054
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004055 free(input_panel_surface);
4056}
4057
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004058static struct input_panel_surface *
4059get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004060{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004061 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004062 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004063 } else {
4064 return NULL;
4065 }
4066}
4067
4068static void
4069input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4070{
4071 struct input_panel_surface *ipsurface = container_of(listener,
4072 struct input_panel_surface,
4073 surface_destroy_listener);
4074
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004075 if (ipsurface->resource) {
4076 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004077 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004078 destroy_input_panel_surface(ipsurface);
4079 }
4080}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004081
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004082static struct input_panel_surface *
4083create_input_panel_surface(struct desktop_shell *shell,
4084 struct weston_surface *surface)
4085{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004086 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004087
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004088 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4089 if (!input_panel_surface)
4090 return NULL;
4091
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004092 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004093 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004094
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004095 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004096
4097 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004098 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004099
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004100 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004101 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004102 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004103 &input_panel_surface->surface_destroy_listener);
4104
4105 wl_list_init(&input_panel_surface->link);
4106
4107 return input_panel_surface;
4108}
4109
4110static void
4111input_panel_surface_set_toplevel(struct wl_client *client,
4112 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004113 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004114 uint32_t position)
4115{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004116 struct input_panel_surface *input_panel_surface =
4117 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004118 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004119
4120 wl_list_insert(&shell->input_panel.surfaces,
4121 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004122
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004123 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004124 input_panel_surface->panel = 0;
4125}
4126
4127static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004128input_panel_surface_set_overlay_panel(struct wl_client *client,
4129 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004130{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004131 struct input_panel_surface *input_panel_surface =
4132 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004133 struct desktop_shell *shell = input_panel_surface->shell;
4134
4135 wl_list_insert(&shell->input_panel.surfaces,
4136 &input_panel_surface->link);
4137
4138 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004139}
4140
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004141static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004142 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004143 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004144};
4145
4146static void
4147destroy_input_panel_surface_resource(struct wl_resource *resource)
4148{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004149 struct input_panel_surface *ipsurf =
4150 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004151
4152 destroy_input_panel_surface(ipsurf);
4153}
4154
4155static void
4156input_panel_get_input_panel_surface(struct wl_client *client,
4157 struct wl_resource *resource,
4158 uint32_t id,
4159 struct wl_resource *surface_resource)
4160{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004161 struct weston_surface *surface =
4162 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004163 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004164 struct input_panel_surface *ipsurf;
4165
4166 if (get_input_panel_surface(surface)) {
4167 wl_resource_post_error(surface_resource,
4168 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004169 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004170 return;
4171 }
4172
4173 ipsurf = create_input_panel_surface(shell, surface);
4174 if (!ipsurf) {
4175 wl_resource_post_error(surface_resource,
4176 WL_DISPLAY_ERROR_INVALID_OBJECT,
4177 "surface->configure already set");
4178 return;
4179 }
4180
Jason Ekstranda85118c2013-06-27 20:17:02 -05004181 ipsurf->resource =
4182 wl_resource_create(client,
4183 &wl_input_panel_surface_interface, 1, id);
4184 wl_resource_set_implementation(ipsurf->resource,
4185 &input_panel_surface_implementation,
4186 ipsurf,
4187 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004188}
4189
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004190static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004191 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004192};
4193
4194static void
4195unbind_input_panel(struct wl_resource *resource)
4196{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004197 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004198
4199 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004200}
4201
4202static void
4203bind_input_panel(struct wl_client *client,
4204 void *data, uint32_t version, uint32_t id)
4205{
4206 struct desktop_shell *shell = data;
4207 struct wl_resource *resource;
4208
Jason Ekstranda85118c2013-06-27 20:17:02 -05004209 resource = wl_resource_create(client,
4210 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004211
4212 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004213 wl_resource_set_implementation(resource,
4214 &input_panel_implementation,
4215 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004216 shell->input_panel.binding = resource;
4217 return;
4218 }
4219
4220 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4221 "interface object already bound");
4222 wl_resource_destroy(resource);
4223}
4224
Kristian Høgsberg07045392012-02-19 18:52:44 -05004225struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004226 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004227 struct weston_surface *current;
4228 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004229 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004230};
4231
4232static void
4233switcher_next(struct switcher *switcher)
4234{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004235 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004236 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004237 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004238 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004239
Jason Ekstranda7af7042013-10-12 22:38:11 -05004240 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4241 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004242 case SHELL_SURFACE_TOPLEVEL:
4243 case SHELL_SURFACE_FULLSCREEN:
4244 case SHELL_SURFACE_MAXIMIZED:
4245 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004246 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004247 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004248 next = view->surface;
4249 prev = view->surface;
4250 view->alpha = 0.25;
4251 weston_view_geometry_dirty(view);
4252 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004253 break;
4254 default:
4255 break;
4256 }
Alex Wu1659daa2012-04-01 20:13:09 +08004257
Jason Ekstranda7af7042013-10-12 22:38:11 -05004258 if (is_black_surface(view->surface, NULL)) {
4259 view->alpha = 0.25;
4260 weston_view_geometry_dirty(view);
4261 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004262 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004263 }
4264
4265 if (next == NULL)
4266 next = first;
4267
Alex Wu07b26062012-03-12 16:06:01 +08004268 if (next == NULL)
4269 return;
4270
Kristian Høgsberg07045392012-02-19 18:52:44 -05004271 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004272 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004273
4274 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004275 wl_list_for_each(view, &next->views, surface_link)
4276 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004277
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004278 shsurf = get_shell_surface(switcher->current);
4279 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004280 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004281}
4282
4283static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004284switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004285{
4286 struct switcher *switcher =
4287 container_of(listener, struct switcher, listener);
4288
4289 switcher_next(switcher);
4290}
4291
4292static void
Daniel Stone351eb612012-05-31 15:27:47 -04004293switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004294{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004295 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004296 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004297 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004298
Jason Ekstranda7af7042013-10-12 22:38:11 -05004299 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4300 view->alpha = 1.0;
4301 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004302 }
4303
Alex Wu07b26062012-03-12 16:06:01 +08004304 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004305 activate(switcher->shell, switcher->current,
4306 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004307 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004308 weston_keyboard_end_grab(keyboard);
4309 if (keyboard->input_method_resource)
4310 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004311 free(switcher);
4312}
4313
4314static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004315switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004316 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004317{
4318 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004319 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004320
Daniel Stonec9785ea2012-05-30 16:31:52 +01004321 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004322 switcher_next(switcher);
4323}
4324
4325static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004326switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004327 uint32_t mods_depressed, uint32_t mods_latched,
4328 uint32_t mods_locked, uint32_t group)
4329{
4330 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004331 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004332
Daniel Stone351eb612012-05-31 15:27:47 -04004333 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4334 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004335}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004336
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004337static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004338 switcher_key,
4339 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004340};
4341
4342static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004343switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004344 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004345{
Tiago Vignattibe143262012-04-16 17:31:41 +03004346 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004347 struct switcher *switcher;
4348
4349 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004350 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004351 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004352 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004353 wl_list_init(&switcher->listener.link);
4354
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004355 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004356 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004357 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004358 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4359 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004360 switcher_next(switcher);
4361}
4362
Pekka Paalanen3c647232011-12-22 13:43:43 +02004363static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004364backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004365 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004366{
4367 struct weston_compositor *compositor = data;
4368 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004369 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004370
4371 /* TODO: we're limiting to simple use cases, where we assume just
4372 * control on the primary display. We'd have to extend later if we
4373 * ever get support for setting backlights on random desktop LCD
4374 * panels though */
4375 output = get_default_output(compositor);
4376 if (!output)
4377 return;
4378
4379 if (!output->set_backlight)
4380 return;
4381
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004382 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4383 backlight_new = output->backlight_current - 25;
4384 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4385 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004386
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004387 if (backlight_new < 5)
4388 backlight_new = 5;
4389 if (backlight_new > 255)
4390 backlight_new = 255;
4391
4392 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004393 output->set_backlight(output, output->backlight_current);
4394}
4395
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004396struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004397 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004398 struct weston_seat *seat;
4399 uint32_t key[2];
4400 int key_released[2];
4401};
4402
4403static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004404debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004405 uint32_t key, uint32_t state)
4406{
4407 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01004408 struct weston_compositor *ec = db->seat->compositor;
4409 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004410 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004411 uint32_t serial;
4412 int send = 0, terminate = 0;
4413 int check_binding = 1;
4414 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01004415 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004416
4417 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4418 /* Do not run bindings on key releases */
4419 check_binding = 0;
4420
4421 for (i = 0; i < 2; i++)
4422 if (key == db->key[i])
4423 db->key_released[i] = 1;
4424
4425 if (db->key_released[0] && db->key_released[1]) {
4426 /* All key releases been swalled so end the grab */
4427 terminate = 1;
4428 } else if (key != db->key[0] && key != db->key[1]) {
4429 /* Should not swallow release of other keys */
4430 send = 1;
4431 }
4432 } else if (key == db->key[0] && !db->key_released[0]) {
4433 /* Do not check bindings for the first press of the binding
4434 * key. This allows it to be used as a debug shortcut.
4435 * We still need to swallow this event. */
4436 check_binding = 0;
4437 } else if (db->key[1]) {
4438 /* If we already ran a binding don't process another one since
4439 * we can't keep track of all the binding keys that were
4440 * pressed in order to swallow the release events. */
4441 send = 1;
4442 check_binding = 0;
4443 }
4444
4445 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004446 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4447 key, state)) {
4448 /* We ran a binding so swallow the press and keep the
4449 * grab to swallow the released too. */
4450 send = 0;
4451 terminate = 0;
4452 db->key[1] = key;
4453 } else {
4454 /* Terminate the grab since the key pressed is not a
4455 * debug binding key. */
4456 send = 1;
4457 terminate = 1;
4458 }
4459 }
4460
4461 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01004462 serial = wl_display_next_serial(display);
4463 resource_list = &grab->keyboard->focus_resource_list;
4464 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004465 wl_keyboard_send_key(resource, serial, time, key, state);
4466 }
4467 }
4468
4469 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004470 weston_keyboard_end_grab(grab->keyboard);
4471 if (grab->keyboard->input_method_resource)
4472 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004473 free(db);
4474 }
4475}
4476
4477static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004478debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004479 uint32_t mods_depressed, uint32_t mods_latched,
4480 uint32_t mods_locked, uint32_t group)
4481{
4482 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01004483 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004484
Neil Roberts96d790e2013-09-19 17:32:00 +01004485 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004486
Neil Roberts96d790e2013-09-19 17:32:00 +01004487 wl_resource_for_each(resource, resource_list) {
4488 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4489 mods_latched, mods_locked, group);
4490 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004491}
4492
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004493struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004494 debug_binding_key,
4495 debug_binding_modifiers
4496};
4497
4498static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004499debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004500{
4501 struct debug_binding_grab *grab;
4502
4503 grab = calloc(1, sizeof *grab);
4504 if (!grab)
4505 return;
4506
4507 grab->seat = (struct weston_seat *) seat;
4508 grab->key[0] = key;
4509 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004510 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004511}
4512
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004513static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004514force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004515 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004516{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004517 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004518 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004519 struct desktop_shell *shell = data;
4520 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004521 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004522
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004523 focus_surface = seat->keyboard->focus;
4524 if (!focus_surface)
4525 return;
4526
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004527 wl_signal_emit(&compositor->kill_signal, focus_surface);
4528
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004529 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004530 wl_client_get_credentials(client, &pid, NULL, NULL);
4531
4532 /* Skip clients that we launched ourselves (the credentials of
4533 * the socketpair is ours) */
4534 if (pid == getpid())
4535 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004536
Daniel Stone325fc2d2012-05-30 16:31:58 +01004537 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004538}
4539
4540static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004541workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004542 uint32_t key, void *data)
4543{
4544 struct desktop_shell *shell = data;
4545 unsigned int new_index = shell->workspaces.current;
4546
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004547 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004548 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004549 if (new_index != 0)
4550 new_index--;
4551
4552 change_workspace(shell, new_index);
4553}
4554
4555static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004556workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004557 uint32_t key, void *data)
4558{
4559 struct desktop_shell *shell = data;
4560 unsigned int new_index = shell->workspaces.current;
4561
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004562 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004563 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004564 if (new_index < shell->workspaces.num - 1)
4565 new_index++;
4566
4567 change_workspace(shell, new_index);
4568}
4569
4570static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004571workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004572 uint32_t key, void *data)
4573{
4574 struct desktop_shell *shell = data;
4575 unsigned int new_index;
4576
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004577 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004578 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004579 new_index = key - KEY_F1;
4580 if (new_index >= shell->workspaces.num)
4581 new_index = shell->workspaces.num - 1;
4582
4583 change_workspace(shell, new_index);
4584}
4585
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004586static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004587workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004588 uint32_t key, void *data)
4589{
4590 struct desktop_shell *shell = data;
4591 unsigned int new_index = shell->workspaces.current;
4592
4593 if (shell->locked)
4594 return;
4595
4596 if (new_index != 0)
4597 new_index--;
4598
4599 take_surface_to_workspace_by_seat(shell, seat, new_index);
4600}
4601
4602static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004603workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004604 uint32_t key, void *data)
4605{
4606 struct desktop_shell *shell = data;
4607 unsigned int new_index = shell->workspaces.current;
4608
4609 if (shell->locked)
4610 return;
4611
4612 if (new_index < shell->workspaces.num - 1)
4613 new_index++;
4614
4615 take_surface_to_workspace_by_seat(shell, seat, new_index);
4616}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004617
4618static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004619shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004620{
Tiago Vignattibe143262012-04-16 17:31:41 +03004621 struct desktop_shell *shell =
4622 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004623 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004624
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004625 if (shell->child.client)
4626 wl_client_destroy(shell->child.client);
4627
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004628 wl_list_remove(&shell->idle_listener.link);
4629 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004630 wl_list_remove(&shell->show_input_panel_listener.link);
4631 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004632
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004633 wl_array_for_each(ws, &shell->workspaces.array)
4634 workspace_destroy(*ws);
4635 wl_array_release(&shell->workspaces.array);
4636
Pekka Paalanen3c647232011-12-22 13:43:43 +02004637 free(shell->screensaver.path);
4638 free(shell);
4639}
4640
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004641static void
4642shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4643{
4644 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004645 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004646
4647 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004648 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4649 MODIFIER_CTRL | MODIFIER_ALT,
4650 terminate_binding, ec);
4651 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4652 click_to_activate_binding,
4653 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004654 weston_compositor_add_touch_binding(ec, 0,
4655 touch_to_activate_binding,
4656 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004657 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4658 MODIFIER_SUPER | MODIFIER_ALT,
4659 surface_opacity_binding, NULL);
4660 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4661 MODIFIER_SUPER, zoom_axis_binding,
4662 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004663
4664 /* configurable bindings */
4665 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004666 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4667 zoom_key_binding, NULL);
4668 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4669 zoom_key_binding, NULL);
4670 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4671 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004672 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004673 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4674 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004675
4676 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4677 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4678 rotate_binding, NULL);
4679
Daniel Stone325fc2d2012-05-30 16:31:58 +01004680 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4681 shell);
4682 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4683 ec);
4684 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4685 backlight_binding, ec);
4686 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4687 ec);
4688 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4689 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004690 weston_compositor_add_key_binding(ec, KEY_K, mod,
4691 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004692 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4693 workspace_up_binding, shell);
4694 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4695 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004696 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4697 workspace_move_surface_up_binding,
4698 shell);
4699 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4700 workspace_move_surface_down_binding,
4701 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004702
4703 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4704 if (shell->workspaces.num > 1) {
4705 num_workspace_bindings = shell->workspaces.num;
4706 if (num_workspace_bindings > 6)
4707 num_workspace_bindings = 6;
4708 for (i = 0; i < num_workspace_bindings; i++)
4709 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4710 workspace_f_binding,
4711 shell);
4712 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004713
4714 /* Debug bindings */
4715 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4716 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004717}
4718
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004719WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004720module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004721 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004722{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004723 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004724 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004725 struct workspace **pws;
4726 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004727 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004728
Peter Huttererf3d62272013-08-08 11:57:05 +10004729 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004730 if (shell == NULL)
4731 return -1;
4732
Kristian Høgsberg75840622011-09-06 13:48:16 -04004733 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004734
4735 shell->destroy_listener.notify = shell_destroy;
4736 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004737 shell->idle_listener.notify = idle_handler;
4738 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4739 shell->wake_listener.notify = wake_handler;
4740 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004741 shell->show_input_panel_listener.notify = show_input_panels;
4742 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4743 shell->hide_input_panel_listener.notify = hide_input_panels;
4744 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004745 shell->update_input_panel_listener.notify = update_input_panels;
4746 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004747 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004748 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004749 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004750 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004751 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004752 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004753 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004754 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004755 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004756 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02004757 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004758
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004759 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004760
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004761 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4762 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004763 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4764 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004765 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004766
4767 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004768 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004769
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004770 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004771
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004772 for (i = 0; i < shell->workspaces.num; i++) {
4773 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4774 if (pws == NULL)
4775 return -1;
4776
4777 *pws = workspace_create();
4778 if (*pws == NULL)
4779 return -1;
4780 }
4781 activate_workspace(shell, 0);
4782
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004783 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004784 wl_list_init(&shell->workspaces.animation.link);
4785 shell->workspaces.animation.frame = animate_workspace_change_frame;
4786
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004787 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004788 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004789 return -1;
4790
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004791 if (wl_global_create(ec->wl_display,
4792 &desktop_shell_interface, 2,
4793 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004794 return -1;
4795
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004796 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4797 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004798 return -1;
4799
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004800 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004801 shell, bind_input_panel) == NULL)
4802 return -1;
4803
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004804 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4805 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004806 return -1;
4807
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004808 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004809
4810 loop = wl_display_get_event_loop(ec->wl_display);
4811 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004812
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004813 shell->screensaver.timer =
4814 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4815
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004816 wl_list_for_each(seat, &ec->seat_list, link)
4817 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004818
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004819 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004820
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004821 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004822
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004823 return 0;
4824}