blob: 9869db5bb35314144fb5cd69f15c6d3811ff21c5 [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
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010040#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020041#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020042#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Juan Zhaoe10d2792012-04-25 19:09:52 +080047enum animation_type {
48 ANIMATION_NONE,
49
50 ANIMATION_ZOOM,
51 ANIMATION_FADE
52};
53
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020054enum fade_type {
55 FADE_IN,
56 FADE_OUT
57};
58
Jonas Ådahl04769742012-06-13 00:01:24 +020059struct focus_state {
60 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040061 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020062 struct weston_surface *keyboard_focus;
63 struct wl_list link;
64 struct wl_listener seat_destroy_listener;
65 struct wl_listener surface_destroy_listener;
66};
67
Jonas Ådahle3cddce2012-06-13 00:01:22 +020068struct workspace {
69 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020070
71 struct wl_list focus_list;
72 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020073};
74
Philipp Brüschweiler88013572012-08-06 13:44:42 +020075struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -050076 struct wl_resource *resource;
77 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010078
79 struct desktop_shell *shell;
80
Philipp Brüschweiler88013572012-08-06 13:44:42 +020081 struct wl_list link;
82 struct weston_surface *surface;
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 {
159 struct weston_surface *surface;
160 struct weston_surface_animation *animation;
161 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øgsberg02ec0a52011-04-23 13:04:11 -0400167};
168
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500169enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200170 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500171 SHELL_SURFACE_TOPLEVEL,
172 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800174 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300175 SHELL_SURFACE_POPUP,
176 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200177};
178
Scott Moreauff1db4a2012-04-17 19:06:18 -0600179struct ping_timer {
180 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600181 uint32_t serial;
182};
183
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200184struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500185 struct wl_resource *resource;
186 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200187
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500188 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200189 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400190 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300191 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200192
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400193 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400194 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500195 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800196 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800197 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600198 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100199
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500200 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200201 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500202 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 } rotation;
204
205 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100206 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500207 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100208 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400209 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500210 } popup;
211
Alex Wu4539b082012-03-01 12:57:46 +0800212 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300213 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400214 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300215 } transient;
216
217 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800218 enum wl_shell_surface_fullscreen_method type;
219 struct weston_transform transform; /* matrix from x, y */
220 uint32_t framerate;
221 struct weston_surface *black_surface;
222 } fullscreen;
223
Scott Moreauff1db4a2012-04-17 19:06:18 -0600224 struct ping_timer *ping_timer;
225
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200226 struct weston_transform workspace_transform;
227
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500228 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500229 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100230 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400231
232 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200233};
234
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300235struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400236 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300237 struct shell_surface *shsurf;
238 struct wl_listener shsurf_destroy_listener;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400239 struct weston_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300240};
241
242struct weston_move_grab {
243 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100244 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245};
246
Pekka Paalanen460099f2012-01-20 16:48:25 +0200247struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300248 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500249 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200250 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200251 float x;
252 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200253 } center;
254};
255
Giulio Camuffo5085a752013-03-25 21:42:45 +0100256struct shell_seat {
257 struct weston_seat *seat;
258 struct wl_listener seat_destroy_listener;
259
260 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400261 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100262 struct wl_list surfaces_list;
263 struct wl_client *client;
264 int32_t initial_up;
265 } popup_grab;
266};
267
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400268static void
269activate(struct desktop_shell *shell, struct weston_surface *es,
270 struct weston_seat *seat);
271
272static struct workspace *
273get_current_workspace(struct desktop_shell *shell);
274
Alex Wubd3354b2012-04-17 17:20:49 +0800275static struct shell_surface *
276get_shell_surface(struct weston_surface *surface);
277
278static struct desktop_shell *
279shell_surface_get_shell(struct shell_surface *shsurf);
280
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500281static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400282surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500283
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300284static void
285shell_fade_startup(struct desktop_shell *shell);
286
Alex Wubd3354b2012-04-17 17:20:49 +0800287static bool
288shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
289{
290 struct desktop_shell *shell;
291 struct weston_surface *top_fs_es;
292
293 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100294
Alex Wubd3354b2012-04-17 17:20:49 +0800295 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
296 return false;
297
298 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100299 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800300 layer_link);
301 return (shsurf == get_shell_surface(top_fs_es));
302}
303
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500304static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400305destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306{
307 struct shell_grab *grab;
308
309 grab = container_of(listener, struct shell_grab,
310 shsurf_destroy_listener);
311
312 grab->shsurf = NULL;
313}
314
315static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400316popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400317
318static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400322 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300324{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300325 struct desktop_shell *shell = shsurf->shell;
326
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400327 popup_grab_end(pointer);
328
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300329 grab->grab.interface = interface;
330 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400331 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500332 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400333 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300334
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300335 grab->pointer = pointer;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300336
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400337 weston_pointer_start_grab(pointer, &grab->grab);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300338 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400339 weston_pointer_set_focus(pointer, shell->grab_surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400340 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341}
342
343static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300344shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400346 if (grab->shsurf)
347 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300348
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400349 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300350}
351
352static void
Alex Wu4539b082012-03-01 12:57:46 +0800353center_on_output(struct weston_surface *surface,
354 struct weston_output *output);
355
Daniel Stone496ca172012-05-30 16:31:42 +0100356static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300357get_modifier(char *modifier)
358{
359 if (!modifier)
360 return MODIFIER_SUPER;
361
362 if (!strcmp("ctrl", modifier))
363 return MODIFIER_CTRL;
364 else if (!strcmp("alt", modifier))
365 return MODIFIER_ALT;
366 else if (!strcmp("super", modifier))
367 return MODIFIER_SUPER;
368 else
369 return MODIFIER_SUPER;
370}
371
Juan Zhaoe10d2792012-04-25 19:09:52 +0800372static enum animation_type
373get_animation_type(char *animation)
374{
375 if (!animation)
376 return ANIMATION_NONE;
377
378 if (!strcmp("zoom", animation))
379 return ANIMATION_ZOOM;
380 else if (!strcmp("fade", animation))
381 return ANIMATION_FADE;
382 else
383 return ANIMATION_NONE;
384}
385
Alex Wu4539b082012-03-01 12:57:46 +0800386static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400387shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200388{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400389 struct weston_config_section *section;
390 int duration;
391 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200392
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400393 section = weston_config_get_section(shell->compositor->config,
394 "screensaver", NULL, NULL);
395 weston_config_section_get_string(section,
396 "path", &shell->screensaver.path, NULL);
397 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200398 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400399
400 section = weston_config_get_section(shell->compositor->config,
401 "shell", NULL, NULL);
402 weston_config_section_get_string(section,
403 "binding-modifier", &s, "super");
404 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200405 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400406 weston_config_section_get_string(section, "animation", &s, "none");
407 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200408 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400409 weston_config_section_get_uint(section, "num-workspaces",
410 &shell->workspaces.num,
411 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200412}
413
414static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200415focus_state_destroy(struct focus_state *state)
416{
417 wl_list_remove(&state->seat_destroy_listener.link);
418 wl_list_remove(&state->surface_destroy_listener.link);
419 free(state);
420}
421
422static void
423focus_state_seat_destroy(struct wl_listener *listener, void *data)
424{
425 struct focus_state *state = container_of(listener,
426 struct focus_state,
427 seat_destroy_listener);
428
429 wl_list_remove(&state->link);
430 focus_state_destroy(state);
431}
432
433static void
434focus_state_surface_destroy(struct wl_listener *listener, void *data)
435{
436 struct focus_state *state = container_of(listener,
437 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400438 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400439 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300440 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400441 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200442
Pekka Paalanen01388e22013-04-25 13:57:44 +0300443 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
444
Kristian Høgsberge3778222012-07-31 17:29:30 -0400445 next = NULL;
446 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300447 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400448 continue;
449
450 next = surface;
451 break;
452 }
453
Pekka Paalanen01388e22013-04-25 13:57:44 +0300454 /* if the focus was a sub-surface, activate its main surface */
455 if (main_surface != state->keyboard_focus)
456 next = main_surface;
457
Kristian Høgsberge3778222012-07-31 17:29:30 -0400458 if (next) {
459 shell = state->seat->compositor->shell_interface.shell;
460 activate(shell, next, state->seat);
461 } else {
462 wl_list_remove(&state->link);
463 focus_state_destroy(state);
464 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200465}
466
467static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400468focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200469{
Jonas Ådahl04769742012-06-13 00:01:24 +0200470 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200471
472 state = malloc(sizeof *state);
473 if (state == NULL)
474 return NULL;
475
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400476 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200477 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400478 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200479
480 state->seat_destroy_listener.notify = focus_state_seat_destroy;
481 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400482 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200483 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400484 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200485
486 return state;
487}
488
Jonas Ådahl8538b222012-08-29 22:13:03 +0200489static struct focus_state *
490ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
491{
492 struct workspace *ws = get_current_workspace(shell);
493 struct focus_state *state;
494
495 wl_list_for_each(state, &ws->focus_list, link)
496 if (state->seat == seat)
497 break;
498
499 if (&state->link == &ws->focus_list)
500 state = focus_state_create(seat, ws);
501
502 return state;
503}
504
Jonas Ådahl04769742012-06-13 00:01:24 +0200505static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400506restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200507{
508 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400509 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200510
511 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400512 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200513
Kristian Høgsberge3148752013-05-06 23:19:49 -0400514 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200515 }
516}
517
518static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200519replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
520 struct weston_seat *seat)
521{
522 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400523 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200524
525 wl_list_for_each(state, &ws->focus_list, link) {
526 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400527 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500528 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200529 return;
530 }
531 }
532}
533
534static void
535drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
536 struct weston_surface *surface)
537{
538 struct focus_state *state;
539
540 wl_list_for_each(state, &ws->focus_list, link)
541 if (state->keyboard_focus == surface)
542 state->keyboard_focus = NULL;
543}
544
545static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200546workspace_destroy(struct workspace *ws)
547{
Jonas Ådahl04769742012-06-13 00:01:24 +0200548 struct focus_state *state, *next;
549
550 wl_list_for_each_safe(state, next, &ws->focus_list, link)
551 focus_state_destroy(state);
552
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200553 free(ws);
554}
555
Jonas Ådahl04769742012-06-13 00:01:24 +0200556static void
557seat_destroyed(struct wl_listener *listener, void *data)
558{
559 struct weston_seat *seat = data;
560 struct focus_state *state, *next;
561 struct workspace *ws = container_of(listener,
562 struct workspace,
563 seat_destroyed_listener);
564
565 wl_list_for_each_safe(state, next, &ws->focus_list, link)
566 if (state->seat == seat)
567 wl_list_remove(&state->link);
568}
569
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200570static struct workspace *
571workspace_create(void)
572{
573 struct workspace *ws = malloc(sizeof *ws);
574 if (ws == NULL)
575 return NULL;
576
577 weston_layer_init(&ws->layer, NULL);
578
Jonas Ådahl04769742012-06-13 00:01:24 +0200579 wl_list_init(&ws->focus_list);
580 wl_list_init(&ws->seat_destroyed_listener.link);
581 ws->seat_destroyed_listener.notify = seat_destroyed;
582
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200583 return ws;
584}
585
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200586static int
587workspace_is_empty(struct workspace *ws)
588{
589 return wl_list_empty(&ws->layer.surface_list);
590}
591
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200592static struct workspace *
593get_workspace(struct desktop_shell *shell, unsigned int index)
594{
595 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200596 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200597 pws += index;
598 return *pws;
599}
600
601static struct workspace *
602get_current_workspace(struct desktop_shell *shell)
603{
604 return get_workspace(shell, shell->workspaces.current);
605}
606
607static void
608activate_workspace(struct desktop_shell *shell, unsigned int index)
609{
610 struct workspace *ws;
611
612 ws = get_workspace(shell, index);
613 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
614
615 shell->workspaces.current = index;
616}
617
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200618static unsigned int
619get_output_height(struct weston_output *output)
620{
621 return abs(output->region.extents.y1 - output->region.extents.y2);
622}
623
624static void
625surface_translate(struct weston_surface *surface, double d)
626{
627 struct shell_surface *shsurf = get_shell_surface(surface);
628 struct weston_transform *transform;
629
630 transform = &shsurf->workspace_transform;
631 if (wl_list_empty(&transform->link))
632 wl_list_insert(surface->geometry.transformation_list.prev,
633 &shsurf->workspace_transform.link);
634
635 weston_matrix_init(&shsurf->workspace_transform.matrix);
636 weston_matrix_translate(&shsurf->workspace_transform.matrix,
637 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200638 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200639}
640
641static void
642workspace_translate_out(struct workspace *ws, double fraction)
643{
644 struct weston_surface *surface;
645 unsigned int height;
646 double d;
647
648 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
649 height = get_output_height(surface->output);
650 d = height * fraction;
651
652 surface_translate(surface, d);
653 }
654}
655
656static void
657workspace_translate_in(struct workspace *ws, double fraction)
658{
659 struct weston_surface *surface;
660 unsigned int height;
661 double d;
662
663 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
664 height = get_output_height(surface->output);
665
666 if (fraction > 0)
667 d = -(height - height * fraction);
668 else
669 d = height + height * fraction;
670
671 surface_translate(surface, d);
672 }
673}
674
675static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200676broadcast_current_workspace_state(struct desktop_shell *shell)
677{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500678 struct wl_list *link;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200679
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500680 for (link = shell->workspaces.client_list.next;
681 link != &shell->workspaces.client_list;
682 link = link->next) {
683 workspace_manager_send_state(wl_resource_from_link(link),
Jonas Ådahle9d22502012-08-29 22:13:01 +0200684 shell->workspaces.current,
685 shell->workspaces.num);
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500686 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200687}
688
689static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200690reverse_workspace_change_animation(struct desktop_shell *shell,
691 unsigned int index,
692 struct workspace *from,
693 struct workspace *to)
694{
695 shell->workspaces.current = index;
696
697 shell->workspaces.anim_to = to;
698 shell->workspaces.anim_from = from;
699 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
700 shell->workspaces.anim_timestamp = 0;
701
Scott Moreau4272e632012-08-13 09:58:41 -0600702 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200703}
704
705static void
706workspace_deactivate_transforms(struct workspace *ws)
707{
708 struct weston_surface *surface;
709 struct shell_surface *shsurf;
710
711 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
712 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200713 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
714 wl_list_remove(&shsurf->workspace_transform.link);
715 wl_list_init(&shsurf->workspace_transform.link);
716 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200717 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200718 }
719}
720
721static void
722finish_workspace_change_animation(struct desktop_shell *shell,
723 struct workspace *from,
724 struct workspace *to)
725{
Scott Moreau4272e632012-08-13 09:58:41 -0600726 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200727
728 wl_list_remove(&shell->workspaces.animation.link);
729 workspace_deactivate_transforms(from);
730 workspace_deactivate_transforms(to);
731 shell->workspaces.anim_to = NULL;
732
733 wl_list_remove(&shell->workspaces.anim_from->layer.link);
734}
735
736static void
737animate_workspace_change_frame(struct weston_animation *animation,
738 struct weston_output *output, uint32_t msecs)
739{
740 struct desktop_shell *shell =
741 container_of(animation, struct desktop_shell,
742 workspaces.animation);
743 struct workspace *from = shell->workspaces.anim_from;
744 struct workspace *to = shell->workspaces.anim_to;
745 uint32_t t;
746 double x, y;
747
748 if (workspace_is_empty(from) && workspace_is_empty(to)) {
749 finish_workspace_change_animation(shell, from, to);
750 return;
751 }
752
753 if (shell->workspaces.anim_timestamp == 0) {
754 if (shell->workspaces.anim_current == 0.0)
755 shell->workspaces.anim_timestamp = msecs;
756 else
757 shell->workspaces.anim_timestamp =
758 msecs -
759 /* Invers of movement function 'y' below. */
760 (asin(1.0 - shell->workspaces.anim_current) *
761 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
762 M_2_PI);
763 }
764
765 t = msecs - shell->workspaces.anim_timestamp;
766
767 /*
768 * x = [0, π/2]
769 * y(x) = sin(x)
770 */
771 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
772 y = sin(x);
773
774 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600775 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200776
777 workspace_translate_out(from, shell->workspaces.anim_dir * y);
778 workspace_translate_in(to, shell->workspaces.anim_dir * y);
779 shell->workspaces.anim_current = y;
780
Scott Moreau4272e632012-08-13 09:58:41 -0600781 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200782 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200783 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200784 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200785}
786
787static void
788animate_workspace_change(struct desktop_shell *shell,
789 unsigned int index,
790 struct workspace *from,
791 struct workspace *to)
792{
793 struct weston_output *output;
794
795 int dir;
796
797 if (index > shell->workspaces.current)
798 dir = -1;
799 else
800 dir = 1;
801
802 shell->workspaces.current = index;
803
804 shell->workspaces.anim_dir = dir;
805 shell->workspaces.anim_from = from;
806 shell->workspaces.anim_to = to;
807 shell->workspaces.anim_current = 0.0;
808 shell->workspaces.anim_timestamp = 0;
809
810 output = container_of(shell->compositor->output_list.next,
811 struct weston_output, link);
812 wl_list_insert(&output->animation_list,
813 &shell->workspaces.animation.link);
814
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200815 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200816
817 workspace_translate_in(to, 0);
818
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400819 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200820
Scott Moreau4272e632012-08-13 09:58:41 -0600821 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200822}
823
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200824static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200825update_workspace(struct desktop_shell *shell, unsigned int index,
826 struct workspace *from, struct workspace *to)
827{
828 shell->workspaces.current = index;
829 wl_list_insert(&from->layer.link, &to->layer.link);
830 wl_list_remove(&from->layer.link);
831}
832
833static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200834change_workspace(struct desktop_shell *shell, unsigned int index)
835{
836 struct workspace *from;
837 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200838
839 if (index == shell->workspaces.current)
840 return;
841
842 /* Don't change workspace when there is any fullscreen surfaces. */
843 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
844 return;
845
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200846 from = get_current_workspace(shell);
847 to = get_workspace(shell, index);
848
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200849 if (shell->workspaces.anim_from == to &&
850 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200851 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200852 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200853 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200854 return;
855 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200856
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857 if (shell->workspaces.anim_to != NULL)
858 finish_workspace_change_animation(shell,
859 shell->workspaces.anim_from,
860 shell->workspaces.anim_to);
861
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200862 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200863
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200864 if (workspace_is_empty(to) && workspace_is_empty(from))
865 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200866 else
867 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200868
869 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200870}
871
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200872static bool
873workspace_has_only(struct workspace *ws, struct weston_surface *surface)
874{
875 struct wl_list *list = &ws->layer.surface_list;
876 struct wl_list *e;
877
878 if (wl_list_empty(list))
879 return false;
880
881 e = list->next;
882
883 if (e->next != list)
884 return false;
885
886 return container_of(e, struct weston_surface, layer_link) == surface;
887}
888
889static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200890move_surface_to_workspace(struct desktop_shell *shell,
891 struct weston_surface *surface,
892 uint32_t workspace)
893{
894 struct workspace *from;
895 struct workspace *to;
896 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300897 struct weston_surface *focus;
898
899 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200900
901 if (workspace == shell->workspaces.current)
902 return;
903
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200904 if (workspace >= shell->workspaces.num)
905 workspace = shell->workspaces.num - 1;
906
Jonas Ådahle9d22502012-08-29 22:13:01 +0200907 from = get_current_workspace(shell);
908 to = get_workspace(shell, workspace);
909
910 wl_list_remove(&surface->layer_link);
911 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
912
913 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300914 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
915 if (!seat->keyboard)
916 continue;
917
918 focus = weston_surface_get_main_surface(seat->keyboard->focus);
919 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400920 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300921 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200922
923 weston_surface_damage_below(surface);
924}
925
926static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200927take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400928 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200929 unsigned int index)
930{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300931 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932 struct shell_surface *shsurf;
933 struct workspace *from;
934 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200935 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200936
Pekka Paalanen01388e22013-04-25 13:57:44 +0300937 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200938 if (surface == NULL ||
939 index == shell->workspaces.current)
940 return;
941
942 from = get_current_workspace(shell);
943 to = get_workspace(shell, index);
944
945 wl_list_remove(&surface->layer_link);
946 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
947
Jonas Ådahle9d22502012-08-29 22:13:01 +0200948 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200949 drop_focus_state(shell, from, surface);
950
951 if (shell->workspaces.anim_from == to &&
952 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200953 wl_list_remove(&to->layer.link);
954 wl_list_insert(from->layer.link.prev, &to->layer.link);
955
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200956 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200957 broadcast_current_workspace_state(shell);
958
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200959 return;
960 }
961
962 if (shell->workspaces.anim_to != NULL)
963 finish_workspace_change_animation(shell,
964 shell->workspaces.anim_from,
965 shell->workspaces.anim_to);
966
967 if (workspace_is_empty(from) &&
968 workspace_has_only(to, surface))
969 update_workspace(shell, index, from, to);
970 else {
971 shsurf = get_shell_surface(surface);
972 if (wl_list_empty(&shsurf->workspace_transform.link))
973 wl_list_insert(&shell->workspaces.anim_sticky_list,
974 &shsurf->workspace_transform.link);
975
976 animate_workspace_change(shell, index, from, to);
977 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200978
979 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200980
981 state = ensure_focus_state(shell, seat);
982 if (state != NULL)
983 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200984}
985
986static void
987workspace_manager_move_surface(struct wl_client *client,
988 struct wl_resource *resource,
989 struct wl_resource *surface_resource,
990 uint32_t workspace)
991{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500992 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200993 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500994 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300995 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200996
Pekka Paalanen01388e22013-04-25 13:57:44 +0300997 main_surface = weston_surface_get_main_surface(surface);
998 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200999}
1000
1001static const struct workspace_manager_interface workspace_manager_implementation = {
1002 workspace_manager_move_surface,
1003};
1004
1005static void
1006unbind_resource(struct wl_resource *resource)
1007{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001008 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001009}
1010
1011static void
1012bind_workspace_manager(struct wl_client *client,
1013 void *data, uint32_t version, uint32_t id)
1014{
1015 struct desktop_shell *shell = data;
1016 struct wl_resource *resource;
1017
1018 resource = wl_client_add_object(client, &workspace_manager_interface,
1019 &workspace_manager_implementation,
1020 id, shell);
1021
1022 if (resource == NULL) {
1023 weston_log("couldn't add workspace manager object");
1024 return;
1025 }
1026
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001027 wl_resource_set_destructor(resource, unbind_resource);
1028 wl_list_insert(&shell->workspaces.client_list,
1029 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001030
1031 workspace_manager_send_state(resource,
1032 shell->workspaces.current,
1033 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001034}
1035
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001036static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001037noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001038{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001039}
1040
1041static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001042move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001043{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001044 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001045 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001046 struct shell_surface *shsurf = move->base.shsurf;
1047 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001048 int dx = wl_fixed_to_int(pointer->x + move->dx);
1049 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001050
1051 if (!shsurf)
1052 return;
1053
1054 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001055
Daniel Stone103db7f2012-05-08 17:17:55 +01001056 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001057 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001058
1059 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001060}
1061
1062static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001063move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001064 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001065{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001066 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1067 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001068 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001069 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001070
Daniel Stone4dbadb12012-05-30 16:31:51 +01001071 if (pointer->button_count == 0 &&
1072 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001073 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001074 free(grab);
1075 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001076}
1077
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001078static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001079 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001080 move_grab_motion,
1081 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001082};
1083
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001084static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001085surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001086{
1087 struct weston_move_grab *move;
1088
1089 if (!shsurf)
1090 return -1;
1091
1092 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1093 return 0;
1094
1095 move = malloc(sizeof *move);
1096 if (!move)
1097 return -1;
1098
1099 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001100 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001101 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001102 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001103
1104 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001105 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001106
1107 return 0;
1108}
1109
1110static void
1111shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1112 struct wl_resource *seat_resource, uint32_t serial)
1113{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001114 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001115 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001116 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001117
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001118 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001119 if (seat->pointer->button_count == 0 ||
1120 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001121 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001122 return;
1123
Kristian Høgsberge3148752013-05-06 23:19:49 -04001124 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001125 wl_resource_post_no_memory(resource);
1126}
1127
1128struct weston_resize_grab {
1129 struct shell_grab base;
1130 uint32_t edges;
1131 int32_t width, height;
1132};
1133
1134static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001135resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001136{
1137 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001138 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001139 struct shell_surface *shsurf = resize->base.shsurf;
1140 int32_t width, height;
1141 wl_fixed_t from_x, from_y;
1142 wl_fixed_t to_x, to_y;
1143
1144 if (!shsurf)
1145 return;
1146
1147 weston_surface_from_global_fixed(shsurf->surface,
1148 pointer->grab_x, pointer->grab_y,
1149 &from_x, &from_y);
1150 weston_surface_from_global_fixed(shsurf->surface,
1151 pointer->x, pointer->y, &to_x, &to_y);
1152
1153 width = resize->width;
1154 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1155 width += wl_fixed_to_int(from_x - to_x);
1156 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1157 width += wl_fixed_to_int(to_x - from_x);
1158 }
1159
1160 height = resize->height;
1161 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1162 height += wl_fixed_to_int(from_y - to_y);
1163 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1164 height += wl_fixed_to_int(to_y - from_y);
1165 }
1166
1167 shsurf->client->send_configure(shsurf->surface,
1168 resize->edges, width, height);
1169}
1170
1171static void
1172send_configure(struct weston_surface *surface,
1173 uint32_t edges, int32_t width, int32_t height)
1174{
1175 struct shell_surface *shsurf = get_shell_surface(surface);
1176
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001177 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001178 edges, width, height);
1179}
1180
1181static const struct weston_shell_client shell_client = {
1182 send_configure
1183};
1184
1185static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001186resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001187 uint32_t time, uint32_t button, uint32_t state_w)
1188{
1189 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001190 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001191 enum wl_pointer_button_state state = state_w;
1192
1193 if (pointer->button_count == 0 &&
1194 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1195 shell_grab_end(&resize->base);
1196 free(grab);
1197 }
1198}
1199
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001200static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001201 noop_grab_focus,
1202 resize_grab_motion,
1203 resize_grab_button,
1204};
1205
Giulio Camuffob8366642013-04-25 13:57:46 +03001206/*
1207 * Returns the bounding box of a surface and all its sub-surfaces,
1208 * in the surface coordinates system. */
1209static void
1210surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1211 int32_t *y, int32_t *w, int32_t *h) {
1212 pixman_region32_t region;
1213 pixman_box32_t *box;
1214 struct weston_subsurface *subsurface;
1215
1216 pixman_region32_init_rect(&region, 0, 0,
1217 surface->geometry.width,
1218 surface->geometry.height);
1219
1220 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1221 pixman_region32_union_rect(&region, &region,
1222 subsurface->position.x,
1223 subsurface->position.y,
1224 subsurface->surface->geometry.width,
1225 subsurface->surface->geometry.height);
1226 }
1227
1228 box = pixman_region32_extents(&region);
1229 if (x)
1230 *x = box->x1;
1231 if (y)
1232 *y = box->y1;
1233 if (w)
1234 *w = box->x2 - box->x1;
1235 if (h)
1236 *h = box->y2 - box->y1;
1237
1238 pixman_region32_fini(&region);
1239}
1240
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001241static int
1242surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001243 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001244{
1245 struct weston_resize_grab *resize;
1246
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001247 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1248 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001249 return 0;
1250
1251 if (edges == 0 || edges > 15 ||
1252 (edges & 3) == 3 || (edges & 12) == 12)
1253 return 0;
1254
1255 resize = malloc(sizeof *resize);
1256 if (!resize)
1257 return -1;
1258
1259 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001260 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1261 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001262
1263 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001264 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001265
1266 return 0;
1267}
1268
1269static void
1270shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1271 struct wl_resource *seat_resource, uint32_t serial,
1272 uint32_t edges)
1273{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001274 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001275 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001276 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001277
1278 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1279 return;
1280
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001281 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001282 if (seat->pointer->button_count == 0 ||
1283 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001284 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001285 return;
1286
Kristian Høgsberge3148752013-05-06 23:19:49 -04001287 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001288 wl_resource_post_no_memory(resource);
1289}
1290
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001291static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001292busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001293{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001294 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001295 struct weston_pointer *pointer = base->pointer;
1296 struct weston_surface *surface;
1297 wl_fixed_t sx, sy;
1298
1299 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1300 pointer->x, pointer->y,
1301 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001302
Rob Bradford2f8d9aa2013-05-20 12:09:20 +01001303 if (!grab->shsurf || grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001304 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001305 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001306 }
1307}
1308
1309static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001310busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001311{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001312}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001313
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001314static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001315busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001316 uint32_t time, uint32_t button, uint32_t state)
1317{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001318 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001319 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001320 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001321
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001322 if (shsurf && button == BTN_LEFT && state) {
1323 activate(shsurf->shell, shsurf->surface, seat);
1324 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001325 } else if (shsurf && button == BTN_RIGHT && state) {
1326 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001327 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001328 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001329}
1330
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001331static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001332 busy_cursor_grab_focus,
1333 busy_cursor_grab_motion,
1334 busy_cursor_grab_button,
1335};
1336
1337static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001338set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001339{
1340 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001341
1342 grab = malloc(sizeof *grab);
1343 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001344 return;
1345
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001346 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1347 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001348}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001349
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001350static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001351end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001352{
1353 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1354
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001355 if (grab->grab.interface == &busy_cursor_grab_interface &&
1356 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001357 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001358 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001359 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001360}
1361
Scott Moreau9521d5e2012-04-19 13:06:17 -06001362static void
1363ping_timer_destroy(struct shell_surface *shsurf)
1364{
1365 if (!shsurf || !shsurf->ping_timer)
1366 return;
1367
1368 if (shsurf->ping_timer->source)
1369 wl_event_source_remove(shsurf->ping_timer->source);
1370
1371 free(shsurf->ping_timer);
1372 shsurf->ping_timer = NULL;
1373}
1374
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001375static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001376ping_timeout_handler(void *data)
1377{
1378 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001379 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001380
Scott Moreau9521d5e2012-04-19 13:06:17 -06001381 /* Client is not responding */
1382 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001383
1384 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001385 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001386 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001387
1388 return 1;
1389}
1390
1391static void
1392ping_handler(struct weston_surface *surface, uint32_t serial)
1393{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001394 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001395 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001396 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001397
1398 if (!shsurf)
1399 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001400 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001401 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001402
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001403 if (shsurf->surface == shsurf->shell->grab_surface)
1404 return;
1405
Scott Moreauff1db4a2012-04-17 19:06:18 -06001406 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001407 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001408 if (!shsurf->ping_timer)
1409 return;
1410
1411 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001412 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1413 shsurf->ping_timer->source =
1414 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1415 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1416
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001417 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001418 }
1419}
1420
1421static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001422handle_pointer_focus(struct wl_listener *listener, void *data)
1423{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001424 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001425 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001426 struct weston_compositor *compositor;
1427 struct shell_surface *shsurf;
1428 uint32_t serial;
1429
1430 if (!surface)
1431 return;
1432
1433 compositor = surface->compositor;
1434 shsurf = get_shell_surface(surface);
1435
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001436 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001437 set_busy_cursor(shsurf, pointer);
1438 } else {
1439 serial = wl_display_next_serial(compositor->wl_display);
1440 ping_handler(surface, serial);
1441 }
1442}
1443
1444static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001445create_pointer_focus_listener(struct weston_seat *seat)
1446{
1447 struct wl_listener *listener;
1448
Kristian Høgsberge3148752013-05-06 23:19:49 -04001449 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001450 return;
1451
1452 listener = malloc(sizeof *listener);
1453 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001454 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001455}
1456
1457static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001458shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1459 uint32_t serial)
1460{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001461 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001462 struct weston_seat *seat;
1463 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001464
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001465 if (shsurf->ping_timer == NULL)
1466 /* Just ignore unsolicited pong. */
1467 return;
1468
Scott Moreauff1db4a2012-04-17 19:06:18 -06001469 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001470 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001471 wl_list_for_each(seat, &ec->seat_list, link) {
1472 if(seat->pointer)
1473 end_busy_cursor(shsurf, seat->pointer);
1474 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001475 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001476 }
1477}
1478
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001479static void
1480shell_surface_set_title(struct wl_client *client,
1481 struct wl_resource *resource, const char *title)
1482{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001483 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001484
1485 free(shsurf->title);
1486 shsurf->title = strdup(title);
1487}
1488
1489static void
1490shell_surface_set_class(struct wl_client *client,
1491 struct wl_resource *resource, const char *class)
1492{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001493 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001494
1495 free(shsurf->class);
1496 shsurf->class = strdup(class);
1497}
1498
Juan Zhao96879df2012-02-07 08:45:41 +08001499static struct weston_output *
1500get_default_output(struct weston_compositor *compositor)
1501{
1502 return container_of(compositor->output_list.next,
1503 struct weston_output, link);
1504}
1505
Alex Wu4539b082012-03-01 12:57:46 +08001506static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001507restore_output_mode(struct weston_output *output)
1508{
1509 if (output->current != output->origin ||
1510 (int32_t)output->scale != output->origin_scale)
1511 weston_output_switch_mode(output,
1512 output->origin,
1513 output->origin_scale);
1514}
1515
1516static void
1517restore_all_output_modes(struct weston_compositor *compositor)
1518{
1519 struct weston_output *output;
1520
1521 wl_list_for_each(output, &compositor->output_list, link)
1522 restore_output_mode(output);
1523}
1524
1525static void
Alex Wu4539b082012-03-01 12:57:46 +08001526shell_unset_fullscreen(struct shell_surface *shsurf)
1527{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001528 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001529 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001530 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1531 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001532 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001533 }
Alex Wu4539b082012-03-01 12:57:46 +08001534 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1535 shsurf->fullscreen.framerate = 0;
1536 wl_list_remove(&shsurf->fullscreen.transform.link);
1537 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001538 if (shsurf->fullscreen.black_surface)
1539 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001540 shsurf->fullscreen.black_surface = NULL;
1541 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001542 weston_surface_set_position(shsurf->surface,
1543 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001544 if (shsurf->saved_rotation_valid) {
1545 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1546 &shsurf->rotation.transform.link);
1547 shsurf->saved_rotation_valid = false;
1548 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001549
1550 ws = get_current_workspace(shsurf->shell);
1551 wl_list_remove(&shsurf->surface->layer_link);
1552 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001553}
1554
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001555static void
1556shell_unset_maximized(struct shell_surface *shsurf)
1557{
1558 struct workspace *ws;
1559 /* undo all maximized things here */
1560 shsurf->output = get_default_output(shsurf->surface->compositor);
1561 weston_surface_set_position(shsurf->surface,
1562 shsurf->saved_x,
1563 shsurf->saved_y);
1564
1565 if (shsurf->saved_rotation_valid) {
1566 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1567 &shsurf->rotation.transform.link);
1568 shsurf->saved_rotation_valid = false;
1569 }
1570
1571 ws = get_current_workspace(shsurf->shell);
1572 wl_list_remove(&shsurf->surface->layer_link);
1573 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1574}
1575
Pekka Paalanen98262232011-12-01 10:42:22 +02001576static int
1577reset_shell_surface_type(struct shell_surface *surface)
1578{
1579 switch (surface->type) {
1580 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001581 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001582 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001583 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001584 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001585 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001586 case SHELL_SURFACE_NONE:
1587 case SHELL_SURFACE_TOPLEVEL:
1588 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001589 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001590 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001591 break;
1592 }
1593
1594 surface->type = SHELL_SURFACE_NONE;
1595 return 0;
1596}
1597
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001598static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001599set_surface_type(struct shell_surface *shsurf)
1600{
1601 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001602 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001603
1604 reset_shell_surface_type(shsurf);
1605
1606 shsurf->type = shsurf->next_type;
1607 shsurf->next_type = SHELL_SURFACE_NONE;
1608
1609 switch (shsurf->type) {
1610 case SHELL_SURFACE_TOPLEVEL:
1611 break;
1612 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001613 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001614 pes->geometry.x + shsurf->transient.x,
1615 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001616 break;
1617
1618 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001619 case SHELL_SURFACE_FULLSCREEN:
1620 shsurf->saved_x = surface->geometry.x;
1621 shsurf->saved_y = surface->geometry.y;
1622 shsurf->saved_position_valid = true;
1623
1624 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1625 wl_list_remove(&shsurf->rotation.transform.link);
1626 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001627 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001628 shsurf->saved_rotation_valid = true;
1629 }
1630 break;
1631
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001632 case SHELL_SURFACE_XWAYLAND:
1633 weston_surface_set_position(surface, shsurf->transient.x,
1634 shsurf->transient.y);
1635 break;
1636
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001637 default:
1638 break;
1639 }
1640}
1641
1642static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001643set_toplevel(struct shell_surface *shsurf)
1644{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001645 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001646}
1647
1648static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001649shell_surface_set_toplevel(struct wl_client *client,
1650 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001651{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001652 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001653
Tiago Vignattibc052c92012-04-19 16:18:18 +03001654 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001655}
1656
1657static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001658set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001659 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001660{
1661 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001662 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001663 shsurf->transient.x = x;
1664 shsurf->transient.y = y;
1665 shsurf->transient.flags = flags;
1666 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1667}
1668
1669static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001670shell_surface_set_transient(struct wl_client *client,
1671 struct wl_resource *resource,
1672 struct wl_resource *parent_resource,
1673 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001674{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001675 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001676 struct weston_surface *parent =
1677 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001678
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001679 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001680}
1681
Tiago Vignattibe143262012-04-16 17:31:41 +03001682static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001683shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001684{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001685 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001686}
1687
1688static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001689get_output_panel_height(struct desktop_shell *shell,
1690 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001691{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001692 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001693 int panel_height = 0;
1694
1695 if (!output)
1696 return 0;
1697
Juan Zhao4ab94682012-07-09 22:24:09 -07001698 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001699 if (surface->output == output) {
1700 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001701 break;
1702 }
1703 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001704
Juan Zhao96879df2012-02-07 08:45:41 +08001705 return panel_height;
1706}
1707
1708static void
1709shell_surface_set_maximized(struct wl_client *client,
1710 struct wl_resource *resource,
1711 struct wl_resource *output_resource )
1712{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001713 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001714 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001715 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001716 uint32_t edges = 0, panel_height = 0;
1717
1718 /* get the default output, if the client set it as NULL
1719 check whether the ouput is available */
1720 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001721 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001722 else if (es->output)
1723 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001724 else
1725 shsurf->output = get_default_output(es->compositor);
1726
Tiago Vignattibe143262012-04-16 17:31:41 +03001727 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001728 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001729 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001730
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001731 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001732 shsurf->output->width,
1733 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001734
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001735 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001736}
1737
Alex Wu21858432012-04-01 20:13:08 +08001738static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001739black_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 +08001740
Alex Wu4539b082012-03-01 12:57:46 +08001741static struct weston_surface *
1742create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001743 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001744 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001745{
1746 struct weston_surface *surface = NULL;
1747
1748 surface = weston_surface_create(ec);
1749 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001750 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001751 return NULL;
1752 }
1753
Alex Wu21858432012-04-01 20:13:08 +08001754 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001755 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001756 weston_surface_configure(surface, x, y, w, h);
1757 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001758 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001759 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001760 pixman_region32_fini(&surface->input);
1761 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001762
Alex Wu4539b082012-03-01 12:57:46 +08001763 return surface;
1764}
1765
1766/* Create black surface and append it to the associated fullscreen surface.
1767 * Handle size dismatch and positioning according to the method. */
1768static void
1769shell_configure_fullscreen(struct shell_surface *shsurf)
1770{
1771 struct weston_output *output = shsurf->fullscreen_output;
1772 struct weston_surface *surface = shsurf->surface;
1773 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001774 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001775 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001776
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001777 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1778 restore_output_mode(output);
1779
Alex Wu4539b082012-03-01 12:57:46 +08001780 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001781 shsurf->fullscreen.black_surface =
1782 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001783 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001784 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001785 output->width,
1786 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001787
1788 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1789 wl_list_insert(&surface->layer_link,
1790 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001791 shsurf->fullscreen.black_surface->output = output;
1792
Giulio Camuffob8366642013-04-25 13:57:46 +03001793 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1794 &surf_width, &surf_height);
1795
Alex Wu4539b082012-03-01 12:57:46 +08001796 switch (shsurf->fullscreen.type) {
1797 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001798 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001799 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001800 break;
1801 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001802 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001803 if (output->width == surf_width &&
1804 output->height == surf_height) {
1805 weston_surface_set_position(surface, output->x - surf_x,
1806 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001807 break;
1808 }
1809
Alex Wu4539b082012-03-01 12:57:46 +08001810 matrix = &shsurf->fullscreen.transform.matrix;
1811 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001812
Scott Moreau1bad5db2012-08-18 01:04:05 -06001813 output_aspect = (float) output->width /
1814 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001815 surface_aspect = (float) surface->geometry.width /
1816 (float) surface->geometry.height;
1817 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001818 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001819 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001820 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001821 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001822 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001823
Alex Wu4539b082012-03-01 12:57:46 +08001824 weston_matrix_scale(matrix, scale, scale, 1);
1825 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001826 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001827 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001828 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1829 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001830 weston_surface_set_position(surface, x, y);
1831
Alex Wu4539b082012-03-01 12:57:46 +08001832 break;
1833 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001834 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001835 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001836 surf_width * surface->buffer_scale,
1837 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001838 shsurf->fullscreen.framerate};
1839
Alexander Larsson355748e2013-05-28 16:23:38 +02001840 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001841 weston_surface_set_position(surface,
1842 output->x - surf_x,
1843 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001844 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001845 output->x - surf_x,
1846 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001847 output->width,
1848 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001849 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001850 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001851 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001852 center_on_output(surface, output);
1853 }
Alex Wubd3354b2012-04-17 17:20:49 +08001854 }
Alex Wu4539b082012-03-01 12:57:46 +08001855 break;
1856 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001857 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001858 break;
1859 default:
1860 break;
1861 }
1862}
1863
1864/* make the fullscreen and black surface at the top */
1865static void
1866shell_stack_fullscreen(struct shell_surface *shsurf)
1867{
Alex Wubd3354b2012-04-17 17:20:49 +08001868 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001869 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001870 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001871
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001872 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001873 wl_list_insert(&shell->fullscreen_layer.surface_list,
1874 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001875 weston_surface_damage(surface);
1876
1877 if (!shsurf->fullscreen.black_surface)
1878 shsurf->fullscreen.black_surface =
1879 create_black_surface(surface->compositor,
1880 surface,
1881 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001882 output->width,
1883 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001884
1885 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001886 wl_list_insert(&surface->layer_link,
1887 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001888 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001889}
1890
1891static void
1892shell_map_fullscreen(struct shell_surface *shsurf)
1893{
Alex Wu4539b082012-03-01 12:57:46 +08001894 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001895 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001896}
1897
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001898static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001899set_fullscreen(struct shell_surface *shsurf,
1900 uint32_t method,
1901 uint32_t framerate,
1902 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001903{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001904 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001905
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001906 if (output)
1907 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001908 else if (es->output)
1909 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001910 else
1911 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001912
Alex Wu4539b082012-03-01 12:57:46 +08001913 shsurf->fullscreen_output = shsurf->output;
1914 shsurf->fullscreen.type = method;
1915 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001916 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001917
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001918 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001919 shsurf->output->width,
1920 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001921}
1922
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001923static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001924shell_surface_set_fullscreen(struct wl_client *client,
1925 struct wl_resource *resource,
1926 uint32_t method,
1927 uint32_t framerate,
1928 struct wl_resource *output_resource)
1929{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001930 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001931 struct weston_output *output;
1932
1933 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001934 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001935 else
1936 output = NULL;
1937
1938 set_fullscreen(shsurf, method, framerate, output);
1939}
1940
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001941static void
1942set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
1943{
1944 /* XXX: using the same fields for transient type */
1945 shsurf->transient.x = x;
1946 shsurf->transient.y = y;
1947 shsurf->transient.flags = flags;
1948 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
1949}
1950
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001951static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001952
1953static void
1954destroy_shell_seat(struct wl_listener *listener, void *data)
1955{
1956 struct shell_seat *shseat =
1957 container_of(listener,
1958 struct shell_seat, seat_destroy_listener);
1959 struct shell_surface *shsurf, *prev = NULL;
1960
1961 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001962 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001963 shseat->popup_grab.client = NULL;
1964
1965 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1966 shsurf->popup.shseat = NULL;
1967 if (prev) {
1968 wl_list_init(&prev->popup.grab_link);
1969 }
1970 prev = shsurf;
1971 }
1972 wl_list_init(&prev->popup.grab_link);
1973 }
1974
1975 wl_list_remove(&shseat->seat_destroy_listener.link);
1976 free(shseat);
1977}
1978
1979static struct shell_seat *
1980create_shell_seat(struct weston_seat *seat)
1981{
1982 struct shell_seat *shseat;
1983
1984 shseat = calloc(1, sizeof *shseat);
1985 if (!shseat) {
1986 weston_log("no memory to allocate shell seat\n");
1987 return NULL;
1988 }
1989
1990 shseat->seat = seat;
1991 wl_list_init(&shseat->popup_grab.surfaces_list);
1992
1993 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1994 wl_signal_add(&seat->destroy_signal,
1995 &shseat->seat_destroy_listener);
1996
1997 return shseat;
1998}
1999
2000static struct shell_seat *
2001get_shell_seat(struct weston_seat *seat)
2002{
2003 struct wl_listener *listener;
2004
2005 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2006 if (listener == NULL)
2007 return create_shell_seat(seat);
2008
2009 return container_of(listener,
2010 struct shell_seat, seat_destroy_listener);
2011}
2012
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002013static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002014popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002015{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002016 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002017 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002018 struct shell_seat *shseat =
2019 container_of(grab, struct shell_seat, popup_grab.grab);
2020 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002021 wl_fixed_t sx, sy;
2022
2023 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2024 pointer->x, pointer->y,
2025 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002026
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002027 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002028 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002029 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002030 weston_pointer_set_focus(pointer, NULL,
2031 wl_fixed_from_int(0),
2032 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002033 }
2034}
2035
2036static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002037popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002038{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002039 struct weston_pointer *pointer = grab->pointer;
2040 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002041
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002042 if (pointer->focus_resource) {
2043 weston_surface_from_global_fixed(pointer->focus,
2044 pointer->x, pointer->y,
2045 &sx, &sy);
2046 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2047 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002048}
2049
2050static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002051popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002052 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002053{
2054 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002055 struct shell_seat *shseat =
2056 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002057 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002058 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002059 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002060
Daniel Stone37816df2012-05-16 18:45:18 +01002061 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002062 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002063 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002064 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002065 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002066 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002067 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002068 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002069 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002070 }
2071
Daniel Stone4dbadb12012-05-30 16:31:51 +01002072 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002073 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002074}
2075
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002076static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002077 popup_grab_focus,
2078 popup_grab_motion,
2079 popup_grab_button,
2080};
2081
2082static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002083popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002084{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002085 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002086 struct shell_seat *shseat =
2087 container_of(grab, struct shell_seat, popup_grab.grab);
2088 struct shell_surface *shsurf;
2089 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002090
2091 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002092 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002093 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002094 shseat->popup_grab.grab.interface = NULL;
2095 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002096 /* Send the popup_done event to all the popups open */
2097 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002098 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002099 shsurf->popup.shseat = NULL;
2100 if (prev) {
2101 wl_list_init(&prev->popup.grab_link);
2102 }
2103 prev = shsurf;
2104 }
2105 wl_list_init(&prev->popup.grab_link);
2106 wl_list_init(&shseat->popup_grab.surfaces_list);
2107 }
2108}
2109
2110static void
2111add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2112{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002113 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002114
2115 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002116 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002117 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002118 /* We must make sure here that this popup was opened after
2119 * a mouse press, and not just by moving around with other
2120 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002121 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002122 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002123
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002124 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002125 }
2126 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2127}
2128
2129static void
2130remove_popup_grab(struct shell_surface *shsurf)
2131{
2132 struct shell_seat *shseat = shsurf->popup.shseat;
2133
2134 wl_list_remove(&shsurf->popup.grab_link);
2135 wl_list_init(&shsurf->popup.grab_link);
2136 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002137 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002138 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002139 }
2140}
2141
2142static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002143shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002144{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002145 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002146 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002147 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002148
2149 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002150
Pekka Paalanen483243f2013-03-08 14:56:50 +02002151 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002152 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2153 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002154
Kristian Høgsberge3148752013-05-06 23:19:49 -04002155 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002156 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002157 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002158 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002159 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002160 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002161}
2162
2163static void
2164shell_surface_set_popup(struct wl_client *client,
2165 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002166 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002167 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002168 struct wl_resource *parent_resource,
2169 int32_t x, int32_t y, uint32_t flags)
2170{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002171 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002172
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002173 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002174 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002175 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002176 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002177 shsurf->popup.x = x;
2178 shsurf->popup.y = y;
2179}
2180
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002181static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002182 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002183 shell_surface_move,
2184 shell_surface_resize,
2185 shell_surface_set_toplevel,
2186 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002187 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002188 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002189 shell_surface_set_maximized,
2190 shell_surface_set_title,
2191 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002192};
2193
2194static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002195destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002196{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002197 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2198
Giulio Camuffo5085a752013-03-25 21:42:45 +01002199 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2200 remove_popup_grab(shsurf);
2201 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002202
Alex Wubd3354b2012-04-17 17:20:49 +08002203 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002204 shell_surface_is_top_fullscreen(shsurf))
2205 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002206
Alex Wuaa08e2d2012-03-05 11:01:40 +08002207 if (shsurf->fullscreen.black_surface)
2208 weston_surface_destroy(shsurf->fullscreen.black_surface);
2209
Alex Wubd3354b2012-04-17 17:20:49 +08002210 /* As destroy_resource() use wl_list_for_each_safe(),
2211 * we can always remove the listener.
2212 */
2213 wl_list_remove(&shsurf->surface_destroy_listener.link);
2214 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002215 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002216 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002217
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002218 wl_list_remove(&shsurf->link);
2219 free(shsurf);
2220}
2221
2222static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002223shell_destroy_shell_surface(struct wl_resource *resource)
2224{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002225 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002226
2227 destroy_shell_surface(shsurf);
2228}
2229
2230static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002231shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002232{
2233 struct shell_surface *shsurf = container_of(listener,
2234 struct shell_surface,
2235 surface_destroy_listener);
2236
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002237 if (shsurf->resource)
2238 wl_resource_destroy(shsurf->resource);
2239 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002240 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002241}
2242
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002243static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002244shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002245
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002246static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002247get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002248{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002249 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002250 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002251 else
2252 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002253}
2254
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002255static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002256create_shell_surface(void *shell, struct weston_surface *surface,
2257 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002258{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002259 struct shell_surface *shsurf;
2260
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002261 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002262 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002263 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002264 }
2265
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002266 shsurf = calloc(1, sizeof *shsurf);
2267 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002268 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002269 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002270 }
2271
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002272 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002273 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002274
Tiago Vignattibc052c92012-04-19 16:18:18 +03002275 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002276 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002277 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002278 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002279 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002280 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2281 shsurf->fullscreen.framerate = 0;
2282 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002283 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002284 wl_list_init(&shsurf->fullscreen.transform.link);
2285
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002286 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002287 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002288 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002289 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002290
2291 /* init link so its safe to always remove it in destroy_shell_surface */
2292 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002293 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002294
Pekka Paalanen460099f2012-01-20 16:48:25 +02002295 /* empty when not in use */
2296 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002297 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002298
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002299 wl_list_init(&shsurf->workspace_transform.link);
2300
Pekka Paalanen98262232011-12-01 10:42:22 +02002301 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002302 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002303
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002304 shsurf->client = client;
2305
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002306 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002307}
2308
2309static void
2310shell_get_shell_surface(struct wl_client *client,
2311 struct wl_resource *resource,
2312 uint32_t id,
2313 struct wl_resource *surface_resource)
2314{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002315 struct weston_surface *surface =
2316 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002317 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002318 struct shell_surface *shsurf;
2319
2320 if (get_shell_surface(surface)) {
2321 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002322 WL_DISPLAY_ERROR_INVALID_OBJECT,
2323 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002324 return;
2325 }
2326
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002327 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002328 if (!shsurf) {
2329 wl_resource_post_error(surface_resource,
2330 WL_DISPLAY_ERROR_INVALID_OBJECT,
2331 "surface->configure already set");
2332 return;
2333 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002334
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002335 shsurf->resource = wl_client_add_object(client,
2336 &wl_shell_surface_interface,
2337 &shell_surface_implementation,
2338 id, shsurf);
2339 wl_resource_set_destructor(shsurf->resource,
2340 shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002341}
2342
2343static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002344 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002345};
2346
Kristian Høgsberg07937562011-04-12 17:25:42 -04002347static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002348shell_fade(struct desktop_shell *shell, enum fade_type type);
2349
2350static int
2351screensaver_timeout(void *data)
2352{
2353 struct desktop_shell *shell = data;
2354
2355 shell_fade(shell, FADE_OUT);
2356
2357 return 1;
2358}
2359
2360static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002361handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002362{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002363 struct desktop_shell *shell =
2364 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002365
Pekka Paalanen18027e52011-12-02 16:31:49 +02002366 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002367
2368 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002369 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002370}
2371
2372static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002373launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002374{
2375 if (shell->screensaver.binding)
2376 return;
2377
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002378 if (!shell->screensaver.path) {
2379 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002380 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002381 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002382
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002383 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002384 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002385 return;
2386 }
2387
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002388 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002389 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002390 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002391 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002392}
2393
2394static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002395terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002396{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002397 if (shell->screensaver.process.pid == 0)
2398 return;
2399
2400 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002401}
2402
2403static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002404configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002405{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002406 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002407
Giulio Camuffo184df502013-02-21 11:29:21 +01002408 if (width == 0)
2409 return;
2410
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002411 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2412 if (s->output == es->output && s != es) {
2413 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002414 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002415 }
2416 }
2417
Giulio Camuffo184df502013-02-21 11:29:21 +01002418 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002419
2420 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002421 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002422 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002423 }
2424}
2425
2426static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002427background_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 -04002428{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002429 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002430
Giulio Camuffo184df502013-02-21 11:29:21 +01002431 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002432}
2433
2434static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002435desktop_shell_set_background(struct wl_client *client,
2436 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002437 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002438 struct wl_resource *surface_resource)
2439{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002440 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002441 struct weston_surface *surface =
2442 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002443
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002444 if (surface->configure) {
2445 wl_resource_post_error(surface_resource,
2446 WL_DISPLAY_ERROR_INVALID_OBJECT,
2447 "surface role already assigned");
2448 return;
2449 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002450
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002451 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002452 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002453 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002454 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002455 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002456 surface->output->width,
2457 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002458}
2459
2460static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002461panel_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 -04002462{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002463 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002464
Giulio Camuffo184df502013-02-21 11:29:21 +01002465 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002466}
2467
2468static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002469desktop_shell_set_panel(struct wl_client *client,
2470 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002471 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002472 struct wl_resource *surface_resource)
2473{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002474 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002475 struct weston_surface *surface =
2476 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002477
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002478 if (surface->configure) {
2479 wl_resource_post_error(surface_resource,
2480 WL_DISPLAY_ERROR_INVALID_OBJECT,
2481 "surface role already assigned");
2482 return;
2483 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002484
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002485 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002486 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002487 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002488 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002489 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002490 surface->output->width,
2491 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002492}
2493
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002494static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002495lock_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 -04002496{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002497 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002498
Giulio Camuffo184df502013-02-21 11:29:21 +01002499 if (width == 0)
2500 return;
2501
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002502 surface->geometry.width = width;
2503 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002504 center_on_output(surface, get_default_output(shell->compositor));
2505
2506 if (!weston_surface_is_mapped(surface)) {
2507 wl_list_insert(&shell->lock_layer.surface_list,
2508 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002509 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002510 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002511 }
2512}
2513
2514static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002515handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002516{
Tiago Vignattibe143262012-04-16 17:31:41 +03002517 struct desktop_shell *shell =
2518 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002519
Martin Minarik6d118362012-06-07 18:01:59 +02002520 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002521 shell->lock_surface = NULL;
2522}
2523
2524static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002525desktop_shell_set_lock_surface(struct wl_client *client,
2526 struct wl_resource *resource,
2527 struct wl_resource *surface_resource)
2528{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002529 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002530 struct weston_surface *surface =
2531 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002532
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002533 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002534
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002535 if (!shell->locked)
2536 return;
2537
Pekka Paalanen98262232011-12-01 10:42:22 +02002538 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002539
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002540 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002541 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002542 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002543
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002544 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002545 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002546}
2547
2548static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002549resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002550{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002551 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002552
Pekka Paalanen77346a62011-11-30 16:26:35 +02002553 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002554
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002555 wl_list_remove(&shell->lock_layer.link);
2556 wl_list_insert(&shell->compositor->cursor_layer.link,
2557 &shell->fullscreen_layer.link);
2558 wl_list_insert(&shell->fullscreen_layer.link,
2559 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002560 if (shell->showing_input_panels) {
2561 wl_list_insert(&shell->panel_layer.link,
2562 &shell->input_panel_layer.link);
2563 wl_list_insert(&shell->input_panel_layer.link,
2564 &ws->layer.link);
2565 } else {
2566 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2567 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002568
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002569 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002570
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002571 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002572 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002573 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002574}
2575
2576static void
2577desktop_shell_unlock(struct wl_client *client,
2578 struct wl_resource *resource)
2579{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002580 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002581
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002582 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002583
2584 if (shell->locked)
2585 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002586}
2587
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002588static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002589desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002590 struct wl_resource *resource,
2591 struct wl_resource *surface_resource)
2592{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002593 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002594
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002595 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002596}
2597
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002598static void
2599desktop_shell_desktop_ready(struct wl_client *client,
2600 struct wl_resource *resource)
2601{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002602 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002603
2604 shell_fade_startup(shell);
2605}
2606
Kristian Høgsberg75840622011-09-06 13:48:16 -04002607static const struct desktop_shell_interface desktop_shell_implementation = {
2608 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002609 desktop_shell_set_panel,
2610 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002611 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002612 desktop_shell_set_grab_surface,
2613 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002614};
2615
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002616static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002617get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002618{
2619 struct shell_surface *shsurf;
2620
2621 shsurf = get_shell_surface(surface);
2622 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002623 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002624 return shsurf->type;
2625}
2626
Kristian Høgsberg75840622011-09-06 13:48:16 -04002627static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002628move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002629{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002630 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002631 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002632 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002633 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002634
Pekka Paalanen01388e22013-04-25 13:57:44 +03002635 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002636 if (surface == NULL)
2637 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002638
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002639 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002640 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2641 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002642 return;
2643
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002644 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002645}
2646
2647static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002648resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002649{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002650 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002651 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002652 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002653 uint32_t edges = 0;
2654 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002655 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002656
Pekka Paalanen01388e22013-04-25 13:57:44 +03002657 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002658 if (surface == NULL)
2659 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002660
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002661 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002662 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2663 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002664 return;
2665
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002666 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002667 wl_fixed_to_int(seat->pointer->grab_x),
2668 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002669 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002670
Pekka Paalanen60921e52012-01-25 15:55:43 +02002671 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002672 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002673 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002674 edges |= 0;
2675 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002676 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002677
Pekka Paalanen60921e52012-01-25 15:55:43 +02002678 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002679 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002680 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002681 edges |= 0;
2682 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002683 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002684
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002685 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002686}
2687
2688static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002689surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002690 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002691{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002692 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002693 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002694 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002695 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002696 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002697
Pekka Paalanen01388e22013-04-25 13:57:44 +03002698 /* XXX: broken for windows containing sub-surfaces */
2699 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002700 if (surface == NULL)
2701 return;
2702
2703 shsurf = get_shell_surface(surface);
2704 if (!shsurf)
2705 return;
2706
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002707 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002708
Scott Moreau02709af2012-05-22 01:54:10 -06002709 if (surface->alpha > 1.0)
2710 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002711 if (surface->alpha < step)
2712 surface->alpha = step;
2713
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002714 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002715 weston_surface_damage(surface);
2716}
2717
2718static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002719do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002720 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002721{
Daniel Stone37816df2012-05-16 18:45:18 +01002722 struct weston_seat *ws = (struct weston_seat *) seat;
2723 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002724 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002725 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002726
2727 wl_list_for_each(output, &compositor->output_list, link) {
2728 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002729 wl_fixed_to_double(seat->pointer->x),
2730 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002731 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002732 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002733 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002734 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002735 increment = -output->zoom.increment;
2736 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002737 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002738 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002739 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002740 else
2741 increment = 0;
2742
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002743 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002744
Scott Moreaue6603982012-06-11 13:07:51 -06002745 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002746 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002747 else if (output->zoom.level > output->zoom.max_level)
2748 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002749 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002750 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002751 output->disable_planes++;
2752 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002753
Scott Moreaue6603982012-06-11 13:07:51 -06002754 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002755
Scott Moreau8dacaab2012-06-17 18:10:58 -06002756 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002757 }
2758 }
2759}
2760
Scott Moreauccbf29d2012-02-22 14:21:41 -07002761static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002762zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002763 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002764{
2765 do_zoom(seat, time, 0, axis, value);
2766}
2767
2768static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002769zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002770 void *data)
2771{
2772 do_zoom(seat, time, key, 0, 0);
2773}
2774
2775static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002776terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002777 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002778{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002779 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002780
Daniel Stone325fc2d2012-05-30 16:31:58 +01002781 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002782}
2783
2784static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002785rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002786{
2787 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002788 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002789 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002790 struct shell_surface *shsurf = rotate->base.shsurf;
2791 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002792 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002793
2794 if (!shsurf)
2795 return;
2796
2797 surface = shsurf->surface;
2798
2799 cx = 0.5f * surface->geometry.width;
2800 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002801
Daniel Stone37816df2012-05-16 18:45:18 +01002802 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2803 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002804 r = sqrtf(dx * dx + dy * dy);
2805
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002806 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002807 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002808
2809 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002810 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002811 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002812
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002813 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002814 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002815
2816 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002817 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002818 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002819 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002820 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002821
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002822 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002823 &shsurf->surface->geometry.transformation_list,
2824 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002825 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002826 wl_list_init(&shsurf->rotation.transform.link);
2827 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002828 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002829 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002830
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002831 /* We need to adjust the position of the surface
2832 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002833 cposx = surface->geometry.x + cx;
2834 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002835 dposx = rotate->center.x - cposx;
2836 dposy = rotate->center.y - cposy;
2837 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002838 weston_surface_set_position(surface,
2839 surface->geometry.x + dposx,
2840 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002841 }
2842
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002843 /* Repaint implies weston_surface_update_transform(), which
2844 * lazily applies the damage due to rotation update.
2845 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002846 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002847}
2848
2849static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002850rotate_grab_button(struct weston_pointer_grab *grab,
2851 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002852{
2853 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002854 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002855 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002856 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002857 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002858
Daniel Stone4dbadb12012-05-30 16:31:51 +01002859 if (pointer->button_count == 0 &&
2860 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002861 if (shsurf)
2862 weston_matrix_multiply(&shsurf->rotation.rotation,
2863 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002864 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002865 free(rotate);
2866 }
2867}
2868
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002869static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002870 noop_grab_focus,
2871 rotate_grab_motion,
2872 rotate_grab_button,
2873};
2874
2875static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002876surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002877{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002878 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002879 float dx, dy;
2880 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002881
Pekka Paalanen460099f2012-01-20 16:48:25 +02002882 rotate = malloc(sizeof *rotate);
2883 if (!rotate)
2884 return;
2885
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002886 weston_surface_to_global_float(surface->surface,
2887 surface->surface->geometry.width / 2,
2888 surface->surface->geometry.height / 2,
2889 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002890
Daniel Stone37816df2012-05-16 18:45:18 +01002891 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2892 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002893 r = sqrtf(dx * dx + dy * dy);
2894 if (r > 20.0f) {
2895 struct weston_matrix inverse;
2896
2897 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002898 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002899 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002900
2901 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002902 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002903 } else {
2904 weston_matrix_init(&surface->rotation.rotation);
2905 weston_matrix_init(&rotate->rotation);
2906 }
2907
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002908 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2909 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002910}
2911
2912static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002913rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002914 void *data)
2915{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002916 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002917 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002918 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002919 struct shell_surface *surface;
2920
Pekka Paalanen01388e22013-04-25 13:57:44 +03002921 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002922 if (base_surface == NULL)
2923 return;
2924
2925 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002926 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2927 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002928 return;
2929
2930 surface_rotate(surface, seat);
2931}
2932
2933static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002934lower_fullscreen_layer(struct desktop_shell *shell)
2935{
2936 struct workspace *ws;
2937 struct weston_surface *surface, *prev;
2938
2939 ws = get_current_workspace(shell);
2940 wl_list_for_each_reverse_safe(surface, prev,
2941 &shell->fullscreen_layer.surface_list,
2942 layer_link)
2943 weston_surface_restack(surface, &ws->layer.surface_list);
2944}
2945
2946static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002947activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002948 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002949{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002950 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002951 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002952 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002953
Pekka Paalanen01388e22013-04-25 13:57:44 +03002954 main_surface = weston_surface_get_main_surface(es);
2955
Daniel Stone37816df2012-05-16 18:45:18 +01002956 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002957
Jonas Ådahl8538b222012-08-29 22:13:03 +02002958 state = ensure_focus_state(shell, seat);
2959 if (state == NULL)
2960 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002961
2962 state->keyboard_focus = es;
2963 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002964 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002965
Pekka Paalanen01388e22013-04-25 13:57:44 +03002966 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002967 case SHELL_SURFACE_FULLSCREEN:
2968 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002969 shell_stack_fullscreen(get_shell_surface(main_surface));
2970 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002971 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002972 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002973 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002974 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002975 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002976 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002977 }
2978}
2979
Alex Wu21858432012-04-01 20:13:08 +08002980/* no-op func for checking black surface */
2981static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002982black_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 +08002983{
2984}
2985
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002986static bool
Alex Wu21858432012-04-01 20:13:08 +08002987is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2988{
2989 if (es->configure == black_surface_configure) {
2990 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002991 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002992 return true;
2993 }
2994 return false;
2995}
2996
Kristian Høgsberg75840622011-09-06 13:48:16 -04002997static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002998click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002999 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003000{
Daniel Stone37816df2012-05-16 18:45:18 +01003001 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003002 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003003 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003004 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003005
Daniel Stone37816df2012-05-16 18:45:18 +01003006 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003007 if (!focus)
3008 return;
3009
Pekka Paalanen01388e22013-04-25 13:57:44 +03003010 if (is_black_surface(focus, &main_surface))
3011 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003012
Pekka Paalanen01388e22013-04-25 13:57:44 +03003013 main_surface = weston_surface_get_main_surface(focus);
3014 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003015 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003016
Daniel Stone325fc2d2012-05-30 16:31:58 +01003017 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003018 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003019}
3020
3021static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003022lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003023{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003024 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003025
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003026 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003027 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003028 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003029 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003030
3031 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003032
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003033 /* Hide all surfaces by removing the fullscreen, panel and
3034 * toplevel layers. This way nothing else can show or receive
3035 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003036
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003037 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003038 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003039 if (shell->showing_input_panels)
3040 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003041 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003042 wl_list_insert(&shell->compositor->cursor_layer.link,
3043 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003044
Pekka Paalanen77346a62011-11-30 16:26:35 +02003045 launch_screensaver(shell);
3046
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003047 /* TODO: disable bindings that should not work while locked. */
3048
3049 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003050}
3051
3052static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003053unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003054{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003055 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003056 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003057 return;
3058 }
3059
3060 /* If desktop-shell client has gone away, unlock immediately. */
3061 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003062 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003063 return;
3064 }
3065
3066 if (shell->prepare_event_sent)
3067 return;
3068
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003069 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003070 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003071}
3072
3073static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003074shell_fade_done(struct weston_surface_animation *animation, void *data)
3075{
3076 struct desktop_shell *shell = data;
3077
3078 shell->fade.animation = NULL;
3079
3080 switch (shell->fade.type) {
3081 case FADE_IN:
3082 weston_surface_destroy(shell->fade.surface);
3083 shell->fade.surface = NULL;
3084 break;
3085 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003086 lock(shell);
3087 break;
3088 }
3089}
3090
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003091static struct weston_surface *
3092shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003093{
3094 struct weston_compositor *compositor = shell->compositor;
3095 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003096
3097 surface = weston_surface_create(compositor);
3098 if (!surface)
3099 return NULL;
3100
3101 weston_surface_configure(surface, 0, 0, 8192, 8192);
3102 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3103 wl_list_insert(&compositor->fade_layer.surface_list,
3104 &surface->layer_link);
3105 pixman_region32_init(&surface->input);
3106
3107 return surface;
3108}
3109
3110static void
3111shell_fade(struct desktop_shell *shell, enum fade_type type)
3112{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003113 float tint;
3114
3115 switch (type) {
3116 case FADE_IN:
3117 tint = 0.0;
3118 break;
3119 case FADE_OUT:
3120 tint = 1.0;
3121 break;
3122 default:
3123 weston_log("shell: invalid fade type\n");
3124 return;
3125 }
3126
3127 shell->fade.type = type;
3128
3129 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003130 shell->fade.surface = shell_fade_create_surface(shell);
3131 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003132 return;
3133
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003134 shell->fade.surface->alpha = 1.0 - tint;
3135 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003136 }
3137
3138 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003139 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003140 else
3141 shell->fade.animation =
3142 weston_fade_run(shell->fade.surface,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003143 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003144 shell_fade_done, shell);
3145}
3146
3147static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003148do_shell_fade_startup(void *data)
3149{
3150 struct desktop_shell *shell = data;
3151
3152 shell_fade(shell, FADE_IN);
3153}
3154
3155static void
3156shell_fade_startup(struct desktop_shell *shell)
3157{
3158 struct wl_event_loop *loop;
3159
3160 if (!shell->fade.startup_timer)
3161 return;
3162
3163 wl_event_source_remove(shell->fade.startup_timer);
3164 shell->fade.startup_timer = NULL;
3165
3166 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3167 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3168}
3169
3170static int
3171fade_startup_timeout(void *data)
3172{
3173 struct desktop_shell *shell = data;
3174
3175 shell_fade_startup(shell);
3176 return 0;
3177}
3178
3179static void
3180shell_fade_init(struct desktop_shell *shell)
3181{
3182 /* Make compositor output all black, and wait for the desktop-shell
3183 * client to signal it is ready, then fade in. The timer triggers a
3184 * fade-in, in case the desktop-shell client takes too long.
3185 */
3186
3187 struct wl_event_loop *loop;
3188
3189 if (shell->fade.surface != NULL) {
3190 weston_log("%s: warning: fade surface already exists\n",
3191 __func__);
3192 return;
3193 }
3194
3195 shell->fade.surface = shell_fade_create_surface(shell);
3196 if (!shell->fade.surface)
3197 return;
3198
3199 weston_surface_update_transform(shell->fade.surface);
3200 weston_surface_damage(shell->fade.surface);
3201
3202 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3203 shell->fade.startup_timer =
3204 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3205 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3206}
3207
3208static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003209idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003210{
3211 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003212 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003213
3214 shell_fade(shell, FADE_OUT);
3215 /* lock() is called from shell_fade_done() */
3216}
3217
3218static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003219wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003220{
3221 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003222 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003223
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003224 unlock(shell);
3225}
3226
3227static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003228show_input_panels(struct wl_listener *listener, void *data)
3229{
3230 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003231 container_of(listener, struct desktop_shell,
3232 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003233 struct input_panel_surface *surface, *next;
3234 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003235
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003236 shell->text_input.surface = (struct weston_surface*)data;
3237
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003238 if (shell->showing_input_panels)
3239 return;
3240
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003241 shell->showing_input_panels = true;
3242
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003243 if (!shell->locked)
3244 wl_list_insert(&shell->panel_layer.link,
3245 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003246
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003247 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003248 &shell->input_panel.surfaces, link) {
3249 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003250 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003251 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003252 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003253 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003254 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003255 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003256 weston_surface_damage(ws);
3257 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003258 }
3259}
3260
3261static void
3262hide_input_panels(struct wl_listener *listener, void *data)
3263{
3264 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003265 container_of(listener, struct desktop_shell,
3266 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003267 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003268
Jan Arne Petersen61381972013-01-31 15:52:21 +01003269 if (!shell->showing_input_panels)
3270 return;
3271
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003272 shell->showing_input_panels = false;
3273
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003274 if (!shell->locked)
3275 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003276
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003277 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003278 &shell->input_panel_layer.surface_list, layer_link)
3279 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003280}
3281
3282static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003283update_input_panels(struct wl_listener *listener, void *data)
3284{
3285 struct desktop_shell *shell =
3286 container_of(listener, struct desktop_shell,
3287 update_input_panel_listener);
3288
3289 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3290}
3291
3292static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003293center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003294{
Giulio Camuffob8366642013-04-25 13:57:46 +03003295 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003296 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003297
Giulio Camuffob8366642013-04-25 13:57:46 +03003298 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3299
3300 x = output->x + (output->width - width) / 2 - surf_x / 2;
3301 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003302
3303 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003304}
3305
3306static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003307weston_surface_set_initial_position (struct weston_surface *surface,
3308 struct desktop_shell *shell)
3309{
3310 struct weston_compositor *compositor = shell->compositor;
3311 int ix = 0, iy = 0;
3312 int range_x, range_y;
3313 int dx, dy, x, y, panel_height;
3314 struct weston_output *output, *target_output = NULL;
3315 struct weston_seat *seat;
3316
3317 /* As a heuristic place the new window on the same output as the
3318 * pointer. Falling back to the output containing 0, 0.
3319 *
3320 * TODO: Do something clever for touch too?
3321 */
3322 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003323 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003324 ix = wl_fixed_to_int(seat->pointer->x);
3325 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003326 break;
3327 }
3328 }
3329
3330 wl_list_for_each(output, &compositor->output_list, link) {
3331 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3332 target_output = output;
3333 break;
3334 }
3335 }
3336
3337 if (!target_output) {
3338 weston_surface_set_position(surface, 10 + random() % 400,
3339 10 + random() % 400);
3340 return;
3341 }
3342
3343 /* Valid range within output where the surface will still be onscreen.
3344 * If this is negative it means that the surface is bigger than
3345 * output.
3346 */
3347 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003348 range_x = target_output->width - surface->geometry.width;
3349 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003350 surface->geometry.height;
3351
Rob Bradford4cb88c72012-08-13 15:18:44 +01003352 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003353 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003354 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003355 dx = 0;
3356
3357 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003358 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003359 else
3360 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003361
3362 x = target_output->x + dx;
3363 y = target_output->y + dy;
3364
3365 weston_surface_set_position (surface, x, y);
3366}
3367
3368static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003369map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003370 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003371{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003372 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003373 struct shell_surface *shsurf = get_shell_surface(surface);
3374 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003375 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003376 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003377 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003378 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003379 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003380
Pekka Paalanen60921e52012-01-25 15:55:43 +02003381 surface->geometry.width = width;
3382 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003383 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003384
3385 /* initial positioning, see also configure() */
3386 switch (surface_type) {
3387 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003388 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003389 break;
Alex Wu4539b082012-03-01 12:57:46 +08003390 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003391 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003392 shell_map_fullscreen(shsurf);
3393 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003394 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003395 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003396 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003397 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3398 NULL, NULL);
3399 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3400 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003401 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003402 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003403 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003404 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003405 case SHELL_SURFACE_NONE:
3406 weston_surface_set_position(surface,
3407 surface->geometry.x + sx,
3408 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003409 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003410 default:
3411 ;
3412 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003413
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003414 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003415 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003416 case SHELL_SURFACE_POPUP:
3417 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003418 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003419 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3420 break;
Alex Wu4539b082012-03-01 12:57:46 +08003421 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003422 case SHELL_SURFACE_NONE:
3423 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003424 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003425 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003426 ws = get_current_workspace(shell);
3427 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003428 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003429 }
3430
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003431 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003432 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003433 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3434 surface->output = shsurf->output;
3435 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003436
Juan Zhao7bb92f02011-12-15 11:31:51 -05003437 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003438 /* XXX: xwayland's using the same fields for transient type */
3439 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003440 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003441 if (shsurf->transient.flags ==
3442 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3443 break;
3444 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003445 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003446 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003447 if (!shell->locked) {
3448 wl_list_for_each(seat, &compositor->seat_list, link)
3449 activate(shell, surface, seat);
3450 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003451 break;
3452 default:
3453 break;
3454 }
3455
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003456 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003457 {
3458 switch (shell->win_animation_type) {
3459 case ANIMATION_FADE:
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003460 weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003461 break;
3462 case ANIMATION_ZOOM:
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -04003463 weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003464 break;
3465 default:
3466 break;
3467 }
3468 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003469}
3470
3471static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003472configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003473 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003474{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003475 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3476 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003477 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003478
Pekka Paalanen77346a62011-11-30 16:26:35 +02003479 shsurf = get_shell_surface(surface);
3480 if (shsurf)
3481 surface_type = shsurf->type;
3482
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003483 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003484
3485 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003486 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003487 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003488 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003489 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003490 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003491 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003492 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3493 NULL, NULL);
3494 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003495 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003496 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003497 break;
Alex Wu4539b082012-03-01 12:57:46 +08003498 case SHELL_SURFACE_TOPLEVEL:
3499 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003500 default:
3501 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003502 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003503
Alex Wu4539b082012-03-01 12:57:46 +08003504 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003505 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003506 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003507
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003508 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003509 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003510 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003511}
3512
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003513static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003514shell_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 +03003515{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003516 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003517 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003518
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003519 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003520
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003521 if (!weston_surface_is_mapped(es) &&
3522 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003523 remove_popup_grab(shsurf);
3524 }
3525
Giulio Camuffo184df502013-02-21 11:29:21 +01003526 if (width == 0)
3527 return;
3528
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003529 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003530 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003531 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003532 type_changed = 1;
3533 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003534
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003535 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003536 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003537 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003538 es->geometry.width != width ||
3539 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003540 float from_x, from_y;
3541 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003542
3543 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3544 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3545 configure(shell, es,
3546 es->geometry.x + to_x - from_x,
3547 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003548 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003549 }
3550}
3551
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003552static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003553
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003554static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003555desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003556{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003557 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003558 struct desktop_shell *shell =
3559 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003560
3561 shell->child.process.pid = 0;
3562 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003563
3564 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3565 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003566 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003567 shell->child.deathstamp = time;
3568 shell->child.deathcount = 0;
3569 }
3570
3571 shell->child.deathcount++;
3572 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003573 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003574 return;
3575 }
3576
Martin Minarik6d118362012-06-07 18:01:59 +02003577 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003578 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003579 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003580}
3581
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003582static void
3583launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003584{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003585 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003586 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003587
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003588 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003589 &shell->child.process,
3590 shell_exe,
3591 desktop_shell_sigchld);
3592
3593 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003594 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003595}
3596
3597static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003598bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3599{
Tiago Vignattibe143262012-04-16 17:31:41 +03003600 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003601
3602 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003603 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003604}
3605
Kristian Høgsberg75840622011-09-06 13:48:16 -04003606static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003607unbind_desktop_shell(struct wl_resource *resource)
3608{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003609 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003610
3611 if (shell->locked)
3612 resume_desktop(shell);
3613
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003614 shell->child.desktop_shell = NULL;
3615 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003616}
3617
3618static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003619bind_desktop_shell(struct wl_client *client,
3620 void *data, uint32_t version, uint32_t id)
3621{
Tiago Vignattibe143262012-04-16 17:31:41 +03003622 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003623 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003624
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003625 resource = wl_client_add_object(client, &desktop_shell_interface,
3626 &desktop_shell_implementation,
3627 id, shell);
3628
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003629 if (client == shell->child.client) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003630 wl_resource_set_destructor(resource, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003631 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003632
3633 if (version < 2)
3634 shell_fade_startup(shell);
3635
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003636 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003637 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003638
3639 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3640 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003641 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003642}
3643
Pekka Paalanen6e168112011-11-24 11:34:05 +02003644static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003645screensaver_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 -04003646{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003647 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003648
Giulio Camuffo184df502013-02-21 11:29:21 +01003649 if (width == 0)
3650 return;
3651
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003652 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003653 if (!shell->locked)
3654 return;
3655
3656 center_on_output(surface, surface->output);
3657
3658 if (wl_list_empty(&surface->layer_link)) {
3659 wl_list_insert(shell->lock_layer.surface_list.prev,
3660 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003661 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003662 wl_event_source_timer_update(shell->screensaver.timer,
3663 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003664 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003665 }
3666}
3667
3668static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003669screensaver_set_surface(struct wl_client *client,
3670 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003671 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003672 struct wl_resource *output_resource)
3673{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003674 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003675 struct weston_surface *surface =
3676 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003677 struct weston_output *output = wl_resource_get_user_data(output_resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003678
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003679 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003680 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003681 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003682}
3683
3684static const struct screensaver_interface screensaver_implementation = {
3685 screensaver_set_surface
3686};
3687
3688static void
3689unbind_screensaver(struct wl_resource *resource)
3690{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003691 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003692
Pekka Paalanen77346a62011-11-30 16:26:35 +02003693 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003694}
3695
3696static void
3697bind_screensaver(struct wl_client *client,
3698 void *data, uint32_t version, uint32_t id)
3699{
Tiago Vignattibe143262012-04-16 17:31:41 +03003700 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003701 struct wl_resource *resource;
3702
3703 resource = wl_client_add_object(client, &screensaver_interface,
3704 &screensaver_implementation,
3705 id, shell);
3706
Pekka Paalanen77346a62011-11-30 16:26:35 +02003707 if (shell->screensaver.binding == NULL) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003708 wl_resource_set_destructor(resource, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003709 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003710 return;
3711 }
3712
3713 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3714 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003715 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003716}
3717
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003718static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003719input_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 -04003720{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003721 struct input_panel_surface *ip_surface = surface->configure_private;
3722 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003723 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003724 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003725 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003726
Giulio Camuffo184df502013-02-21 11:29:21 +01003727 if (width == 0)
3728 return;
3729
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003730 if (!weston_surface_is_mapped(surface)) {
3731 if (!shell->showing_input_panels)
3732 return;
3733
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003734 show_surface = 1;
3735 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003736
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003737 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003738
3739 if (ip_surface->panel) {
3740 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3741 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3742 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003743 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003744
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003745 x = ip_surface->output->x + (mode->width - width) / 2;
3746 y = ip_surface->output->y + mode->height - height;
3747 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003748
3749 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003750 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003751 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003752
3753 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003754 wl_list_insert(&shell->input_panel_layer.surface_list,
3755 &surface->layer_link);
3756 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003757 weston_surface_damage(surface);
3758 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3759 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003760}
3761
3762static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003763destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003764{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003765 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3766
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003767 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003768 wl_list_remove(&input_panel_surface->link);
3769
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003770 input_panel_surface->surface->configure = NULL;
3771
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003772 free(input_panel_surface);
3773}
3774
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003775static struct input_panel_surface *
3776get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003777{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003778 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003779 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003780 } else {
3781 return NULL;
3782 }
3783}
3784
3785static void
3786input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3787{
3788 struct input_panel_surface *ipsurface = container_of(listener,
3789 struct input_panel_surface,
3790 surface_destroy_listener);
3791
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003792 if (ipsurface->resource) {
3793 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003794 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003795 destroy_input_panel_surface(ipsurface);
3796 }
3797}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003798
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003799static struct input_panel_surface *
3800create_input_panel_surface(struct desktop_shell *shell,
3801 struct weston_surface *surface)
3802{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003803 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003804
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003805 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3806 if (!input_panel_surface)
3807 return NULL;
3808
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003809 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003810 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003811
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003812 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003813
3814 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003815
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003816 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003817 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003818 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003819 &input_panel_surface->surface_destroy_listener);
3820
3821 wl_list_init(&input_panel_surface->link);
3822
3823 return input_panel_surface;
3824}
3825
3826static void
3827input_panel_surface_set_toplevel(struct wl_client *client,
3828 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003829 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003830 uint32_t position)
3831{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003832 struct input_panel_surface *input_panel_surface =
3833 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003834 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003835
3836 wl_list_insert(&shell->input_panel.surfaces,
3837 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003838
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003839 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003840 input_panel_surface->panel = 0;
3841}
3842
3843static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003844input_panel_surface_set_overlay_panel(struct wl_client *client,
3845 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003846{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003847 struct input_panel_surface *input_panel_surface =
3848 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003849 struct desktop_shell *shell = input_panel_surface->shell;
3850
3851 wl_list_insert(&shell->input_panel.surfaces,
3852 &input_panel_surface->link);
3853
3854 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003855}
3856
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003857static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003858 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003859 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003860};
3861
3862static void
3863destroy_input_panel_surface_resource(struct wl_resource *resource)
3864{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003865 struct input_panel_surface *ipsurf =
3866 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003867
3868 destroy_input_panel_surface(ipsurf);
3869}
3870
3871static void
3872input_panel_get_input_panel_surface(struct wl_client *client,
3873 struct wl_resource *resource,
3874 uint32_t id,
3875 struct wl_resource *surface_resource)
3876{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003877 struct weston_surface *surface =
3878 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003879 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003880 struct input_panel_surface *ipsurf;
3881
3882 if (get_input_panel_surface(surface)) {
3883 wl_resource_post_error(surface_resource,
3884 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003885 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003886 return;
3887 }
3888
3889 ipsurf = create_input_panel_surface(shell, surface);
3890 if (!ipsurf) {
3891 wl_resource_post_error(surface_resource,
3892 WL_DISPLAY_ERROR_INVALID_OBJECT,
3893 "surface->configure already set");
3894 return;
3895 }
3896
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003897 ipsurf->resource = wl_client_add_object(client,
3898 &wl_input_panel_surface_interface,
3899 &input_panel_surface_implementation,
3900 id, ipsurf);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003901
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003902 wl_resource_set_destructor(ipsurf->resource,
3903 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003904}
3905
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003906static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003907 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003908};
3909
3910static void
3911unbind_input_panel(struct wl_resource *resource)
3912{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003913 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003914
3915 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003916}
3917
3918static void
3919bind_input_panel(struct wl_client *client,
3920 void *data, uint32_t version, uint32_t id)
3921{
3922 struct desktop_shell *shell = data;
3923 struct wl_resource *resource;
3924
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003925 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003926 &input_panel_implementation,
3927 id, shell);
3928
3929 if (shell->input_panel.binding == NULL) {
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003930 wl_resource_set_destructor(resource, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003931 shell->input_panel.binding = resource;
3932 return;
3933 }
3934
3935 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3936 "interface object already bound");
3937 wl_resource_destroy(resource);
3938}
3939
Kristian Høgsberg07045392012-02-19 18:52:44 -05003940struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003941 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003942 struct weston_surface *current;
3943 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003944 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003945};
3946
3947static void
3948switcher_next(struct switcher *switcher)
3949{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003950 struct weston_surface *surface;
3951 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003952 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003953 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003954
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003955 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003956 switch (get_shell_surface_type(surface)) {
3957 case SHELL_SURFACE_TOPLEVEL:
3958 case SHELL_SURFACE_FULLSCREEN:
3959 case SHELL_SURFACE_MAXIMIZED:
3960 if (first == NULL)
3961 first = surface;
3962 if (prev == switcher->current)
3963 next = surface;
3964 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003965 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003966 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003967 weston_surface_damage(surface);
3968 break;
3969 default:
3970 break;
3971 }
Alex Wu1659daa2012-04-01 20:13:09 +08003972
3973 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003974 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003975 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003976 weston_surface_damage(surface);
3977 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003978 }
3979
3980 if (next == NULL)
3981 next = first;
3982
Alex Wu07b26062012-03-12 16:06:01 +08003983 if (next == NULL)
3984 return;
3985
Kristian Høgsberg07045392012-02-19 18:52:44 -05003986 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003987 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003988
3989 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003990 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003991
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003992 shsurf = get_shell_surface(switcher->current);
3993 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003994 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003995}
3996
3997static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003998switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003999{
4000 struct switcher *switcher =
4001 container_of(listener, struct switcher, listener);
4002
4003 switcher_next(switcher);
4004}
4005
4006static void
Daniel Stone351eb612012-05-31 15:27:47 -04004007switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004008{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004009 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004010 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004011 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004012
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004013 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004014 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004015 weston_surface_damage(surface);
4016 }
4017
Alex Wu07b26062012-03-12 16:06:01 +08004018 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004019 activate(switcher->shell, switcher->current,
4020 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004021 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004022 weston_keyboard_end_grab(keyboard);
4023 if (keyboard->input_method_resource)
4024 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004025 free(switcher);
4026}
4027
4028static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004029switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004030 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004031{
4032 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004033 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004034
Daniel Stonec9785ea2012-05-30 16:31:52 +01004035 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004036 switcher_next(switcher);
4037}
4038
4039static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004040switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004041 uint32_t mods_depressed, uint32_t mods_latched,
4042 uint32_t mods_locked, uint32_t group)
4043{
4044 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004045 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004046
Daniel Stone351eb612012-05-31 15:27:47 -04004047 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4048 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004049}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004050
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004051static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004052 switcher_key,
4053 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004054};
4055
4056static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004057switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004058 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004059{
Tiago Vignattibe143262012-04-16 17:31:41 +03004060 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004061 struct switcher *switcher;
4062
4063 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004064 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004065 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004066 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004067 wl_list_init(&switcher->listener.link);
4068
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004069 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004070 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004071 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004072 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4073 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004074 switcher_next(switcher);
4075}
4076
Pekka Paalanen3c647232011-12-22 13:43:43 +02004077static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004078backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004079 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004080{
4081 struct weston_compositor *compositor = data;
4082 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004083 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004084
4085 /* TODO: we're limiting to simple use cases, where we assume just
4086 * control on the primary display. We'd have to extend later if we
4087 * ever get support for setting backlights on random desktop LCD
4088 * panels though */
4089 output = get_default_output(compositor);
4090 if (!output)
4091 return;
4092
4093 if (!output->set_backlight)
4094 return;
4095
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004096 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4097 backlight_new = output->backlight_current - 25;
4098 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4099 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004100
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004101 if (backlight_new < 5)
4102 backlight_new = 5;
4103 if (backlight_new > 255)
4104 backlight_new = 255;
4105
4106 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004107 output->set_backlight(output, output->backlight_current);
4108}
4109
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004110struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004111 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004112 struct weston_seat *seat;
4113 uint32_t key[2];
4114 int key_released[2];
4115};
4116
4117static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004118debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004119 uint32_t key, uint32_t state)
4120{
4121 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4122 struct wl_resource *resource;
4123 struct wl_display *display;
4124 uint32_t serial;
4125 int send = 0, terminate = 0;
4126 int check_binding = 1;
4127 int i;
4128
4129 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4130 /* Do not run bindings on key releases */
4131 check_binding = 0;
4132
4133 for (i = 0; i < 2; i++)
4134 if (key == db->key[i])
4135 db->key_released[i] = 1;
4136
4137 if (db->key_released[0] && db->key_released[1]) {
4138 /* All key releases been swalled so end the grab */
4139 terminate = 1;
4140 } else if (key != db->key[0] && key != db->key[1]) {
4141 /* Should not swallow release of other keys */
4142 send = 1;
4143 }
4144 } else if (key == db->key[0] && !db->key_released[0]) {
4145 /* Do not check bindings for the first press of the binding
4146 * key. This allows it to be used as a debug shortcut.
4147 * We still need to swallow this event. */
4148 check_binding = 0;
4149 } else if (db->key[1]) {
4150 /* If we already ran a binding don't process another one since
4151 * we can't keep track of all the binding keys that were
4152 * pressed in order to swallow the release events. */
4153 send = 1;
4154 check_binding = 0;
4155 }
4156
4157 if (check_binding) {
4158 struct weston_compositor *ec = db->seat->compositor;
4159
4160 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4161 key, state)) {
4162 /* We ran a binding so swallow the press and keep the
4163 * grab to swallow the released too. */
4164 send = 0;
4165 terminate = 0;
4166 db->key[1] = key;
4167 } else {
4168 /* Terminate the grab since the key pressed is not a
4169 * debug binding key. */
4170 send = 1;
4171 terminate = 1;
4172 }
4173 }
4174
4175 if (send) {
4176 resource = grab->keyboard->focus_resource;
4177
4178 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004179 display = wl_client_get_display(wl_resource_get_client(resource));
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004180 serial = wl_display_next_serial(display);
4181 wl_keyboard_send_key(resource, serial, time, key, state);
4182 }
4183 }
4184
4185 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004186 weston_keyboard_end_grab(grab->keyboard);
4187 if (grab->keyboard->input_method_resource)
4188 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004189 free(db);
4190 }
4191}
4192
4193static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004194debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004195 uint32_t mods_depressed, uint32_t mods_latched,
4196 uint32_t mods_locked, uint32_t group)
4197{
4198 struct wl_resource *resource;
4199
4200 resource = grab->keyboard->focus_resource;
4201 if (!resource)
4202 return;
4203
4204 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4205 mods_latched, mods_locked, group);
4206}
4207
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004208struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004209 debug_binding_key,
4210 debug_binding_modifiers
4211};
4212
4213static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004214debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004215{
4216 struct debug_binding_grab *grab;
4217
4218 grab = calloc(1, sizeof *grab);
4219 if (!grab)
4220 return;
4221
4222 grab->seat = (struct weston_seat *) seat;
4223 grab->key[0] = key;
4224 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004225 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004226}
4227
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004228static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004229force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004230 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004231{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004232 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004233 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004234 struct desktop_shell *shell = data;
4235 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004236 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004237
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004238 focus_surface = seat->keyboard->focus;
4239 if (!focus_surface)
4240 return;
4241
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004242 wl_signal_emit(&compositor->kill_signal, focus_surface);
4243
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004244 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004245 wl_client_get_credentials(client, &pid, NULL, NULL);
4246
4247 /* Skip clients that we launched ourselves (the credentials of
4248 * the socketpair is ours) */
4249 if (pid == getpid())
4250 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004251
Daniel Stone325fc2d2012-05-30 16:31:58 +01004252 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004253}
4254
4255static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004256workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004257 uint32_t key, void *data)
4258{
4259 struct desktop_shell *shell = data;
4260 unsigned int new_index = shell->workspaces.current;
4261
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004262 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004263 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004264 if (new_index != 0)
4265 new_index--;
4266
4267 change_workspace(shell, new_index);
4268}
4269
4270static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004271workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004272 uint32_t key, void *data)
4273{
4274 struct desktop_shell *shell = data;
4275 unsigned int new_index = shell->workspaces.current;
4276
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004277 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004278 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004279 if (new_index < shell->workspaces.num - 1)
4280 new_index++;
4281
4282 change_workspace(shell, new_index);
4283}
4284
4285static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004286workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004287 uint32_t key, void *data)
4288{
4289 struct desktop_shell *shell = data;
4290 unsigned int new_index;
4291
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004292 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004293 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004294 new_index = key - KEY_F1;
4295 if (new_index >= shell->workspaces.num)
4296 new_index = shell->workspaces.num - 1;
4297
4298 change_workspace(shell, new_index);
4299}
4300
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004301static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004302workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004303 uint32_t key, void *data)
4304{
4305 struct desktop_shell *shell = data;
4306 unsigned int new_index = shell->workspaces.current;
4307
4308 if (shell->locked)
4309 return;
4310
4311 if (new_index != 0)
4312 new_index--;
4313
4314 take_surface_to_workspace_by_seat(shell, seat, new_index);
4315}
4316
4317static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004318workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004319 uint32_t key, void *data)
4320{
4321 struct desktop_shell *shell = data;
4322 unsigned int new_index = shell->workspaces.current;
4323
4324 if (shell->locked)
4325 return;
4326
4327 if (new_index < shell->workspaces.num - 1)
4328 new_index++;
4329
4330 take_surface_to_workspace_by_seat(shell, seat, new_index);
4331}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004332
4333static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004334shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004335{
Tiago Vignattibe143262012-04-16 17:31:41 +03004336 struct desktop_shell *shell =
4337 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004338 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004339
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004340 if (shell->child.client)
4341 wl_client_destroy(shell->child.client);
4342
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004343 wl_list_remove(&shell->idle_listener.link);
4344 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004345 wl_list_remove(&shell->show_input_panel_listener.link);
4346 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004347
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004348 wl_array_for_each(ws, &shell->workspaces.array)
4349 workspace_destroy(*ws);
4350 wl_array_release(&shell->workspaces.array);
4351
Pekka Paalanen3c647232011-12-22 13:43:43 +02004352 free(shell->screensaver.path);
4353 free(shell);
4354}
4355
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004356static void
4357shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4358{
4359 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004360 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004361
4362 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004363 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4364 MODIFIER_CTRL | MODIFIER_ALT,
4365 terminate_binding, ec);
4366 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4367 click_to_activate_binding,
4368 shell);
4369 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4370 MODIFIER_SUPER | MODIFIER_ALT,
4371 surface_opacity_binding, NULL);
4372 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4373 MODIFIER_SUPER, zoom_axis_binding,
4374 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004375
4376 /* configurable bindings */
4377 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004378 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4379 zoom_key_binding, NULL);
4380 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4381 zoom_key_binding, NULL);
4382 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4383 shell);
4384 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4385 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004386
4387 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4388 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4389 rotate_binding, NULL);
4390
Daniel Stone325fc2d2012-05-30 16:31:58 +01004391 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4392 shell);
4393 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4394 ec);
4395 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4396 backlight_binding, ec);
4397 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4398 ec);
4399 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4400 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004401 weston_compositor_add_key_binding(ec, KEY_K, mod,
4402 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004403 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4404 workspace_up_binding, shell);
4405 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4406 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004407 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4408 workspace_move_surface_up_binding,
4409 shell);
4410 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4411 workspace_move_surface_down_binding,
4412 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004413
4414 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4415 if (shell->workspaces.num > 1) {
4416 num_workspace_bindings = shell->workspaces.num;
4417 if (num_workspace_bindings > 6)
4418 num_workspace_bindings = 6;
4419 for (i = 0; i < num_workspace_bindings; i++)
4420 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4421 workspace_f_binding,
4422 shell);
4423 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004424
4425 /* Debug bindings */
4426 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4427 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004428}
4429
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004430WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004431module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004432 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004433{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004434 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004435 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004436 struct workspace **pws;
4437 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004438 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004439
4440 shell = malloc(sizeof *shell);
4441 if (shell == NULL)
4442 return -1;
4443
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004444 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004445 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004446
4447 shell->destroy_listener.notify = shell_destroy;
4448 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004449 shell->idle_listener.notify = idle_handler;
4450 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4451 shell->wake_listener.notify = wake_handler;
4452 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004453 shell->show_input_panel_listener.notify = show_input_panels;
4454 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4455 shell->hide_input_panel_listener.notify = hide_input_panels;
4456 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004457 shell->update_input_panel_listener.notify = update_input_panels;
4458 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004459 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004460 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004461 ec->shell_interface.create_shell_surface = create_shell_surface;
4462 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004463 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004464 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004465 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004466 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004467 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004468
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004469 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004470
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004471 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4472 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004473 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4474 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004475 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004476
4477 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004478 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004479
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004480 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004481
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004482 for (i = 0; i < shell->workspaces.num; i++) {
4483 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4484 if (pws == NULL)
4485 return -1;
4486
4487 *pws = workspace_create();
4488 if (*pws == NULL)
4489 return -1;
4490 }
4491 activate_workspace(shell, 0);
4492
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004493 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004494 wl_list_init(&shell->workspaces.animation.link);
4495 shell->workspaces.animation.frame = animate_workspace_change_frame;
4496
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004497 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4498 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004499 return -1;
4500
Kristian Høgsberg75840622011-09-06 13:48:16 -04004501 if (wl_display_add_global(ec->wl_display,
4502 &desktop_shell_interface,
4503 shell, bind_desktop_shell) == NULL)
4504 return -1;
4505
Pekka Paalanen6e168112011-11-24 11:34:05 +02004506 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4507 shell, bind_screensaver) == NULL)
4508 return -1;
4509
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004510 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004511 shell, bind_input_panel) == NULL)
4512 return -1;
4513
Jonas Ådahle9d22502012-08-29 22:13:01 +02004514 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4515 shell, bind_workspace_manager) == NULL)
4516 return -1;
4517
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004518 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004519
4520 loop = wl_display_get_event_loop(ec->wl_display);
4521 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004522
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004523 shell->screensaver.timer =
4524 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4525
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004526 wl_list_for_each(seat, &ec->seat_list, link)
4527 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004528
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004529 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004530
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004531 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004532
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004533 return 0;
4534}