blob: 9ad36bb32a8b242a1333781c991af4433fa8c65f [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
25
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050026#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040027#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020028#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010039#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020040#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020041#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Juan Zhaoe10d2792012-04-25 19:09:52 +080046enum animation_type {
47 ANIMATION_NONE,
48
49 ANIMATION_ZOOM,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010050 ANIMATION_FADE,
51 ANIMATION_DIM_LAYER,
Juan Zhaoe10d2792012-04-25 19:09:52 +080052};
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
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010068struct focus_surface {
69 struct weston_surface *surface;
70 struct weston_view *view;
71 struct weston_transform workspace_transform;
72};
73
Jonas Ådahle3cddce2012-06-13 00:01:22 +020074struct workspace {
75 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020076
77 struct wl_list focus_list;
78 struct wl_listener seat_destroyed_listener;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010079
80 struct focus_surface *fsurf_front;
81 struct focus_surface *fsurf_back;
82 struct weston_view_animation *focus_animation;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020083};
84
Philipp Brüschweiler88013572012-08-06 13:44:42 +020085struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -050086 struct wl_resource *resource;
87 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010088
89 struct desktop_shell *shell;
90
Philipp Brüschweiler88013572012-08-06 13:44:42 +020091 struct wl_list link;
92 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -050093 struct weston_view *view;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010094 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020095
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020096 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020097 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020098};
99
Xiong Zhang6b481422013-10-23 13:58:32 +0800100struct shell_output {
101 struct desktop_shell *shell;
102 struct weston_output *output;
103 struct wl_listener destroy_listener;
104 struct wl_list link;
105};
106
Tiago Vignattibe143262012-04-16 17:31:41 +0300107struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400109
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200110 struct wl_listener idle_listener;
111 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400112 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200113 struct wl_listener show_input_panel_listener;
114 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200115 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200116
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500117 struct weston_layer fullscreen_layer;
118 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500119 struct weston_layer background_layer;
120 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200121 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500122
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400123 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300124 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400125
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200126 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500127 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200128 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200129 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200130
131 unsigned deathcount;
132 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200133 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200134
135 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200136 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200137 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200138
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200139 struct {
140 struct weston_surface *surface;
141 pixman_box32_t cursor_rectangle;
142 } text_input;
143
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400144 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500145 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100146
Pekka Paalanen77346a62011-11-30 16:26:35 +0200147 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200148 struct wl_array array;
149 unsigned int current;
150 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200151
Jonas Ådahle9d22502012-08-29 22:13:01 +0200152 struct wl_list client_list;
153
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200154 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200155 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200156 int anim_dir;
157 uint32_t anim_timestamp;
158 double anim_current;
159 struct workspace *anim_from;
160 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200161 } workspaces;
162
163 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200164 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200165 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200166 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500167 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200168 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200169 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500170
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200171 struct {
172 struct wl_resource *binding;
173 struct wl_list surfaces;
174 } input_panel;
175
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200176 struct {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500177 struct weston_view *view;
178 struct weston_view_animation *animation;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200179 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300180 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200181 } fade;
182
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300183 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800184 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700185 enum animation_type startup_animation_type;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100186 enum animation_type focus_animation_type;
Xiong Zhang6b481422013-10-23 13:58:32 +0800187
188 struct wl_listener output_create_listener;
189 struct wl_list output_list;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400190};
191
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500192enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200193 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500194 SHELL_SURFACE_TOPLEVEL,
195 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500196 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800197 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300198 SHELL_SURFACE_POPUP,
199 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200200};
201
Scott Moreauff1db4a2012-04-17 19:06:18 -0600202struct ping_timer {
203 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600204 uint32_t serial;
205};
206
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200207struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500208 struct wl_resource *resource;
209 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200210
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500211 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500212 struct weston_view *view;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200213 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400214 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300215 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200216
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400217 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400218 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500219 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800220 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800221 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700222 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100223
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500224 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200225 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500226 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200227 } rotation;
228
229 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100230 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500231 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100232 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400233 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500234 } popup;
235
Alex Wu4539b082012-03-01 12:57:46 +0800236 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300237 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400238 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300239 } transient;
240
241 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800242 enum wl_shell_surface_fullscreen_method type;
243 struct weston_transform transform; /* matrix from x, y */
244 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500245 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800246 } fullscreen;
247
Scott Moreauff1db4a2012-04-17 19:06:18 -0600248 struct ping_timer *ping_timer;
249
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200250 struct weston_transform workspace_transform;
251
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500252 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500253 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100254 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400255
256 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200257};
258
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300259struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400260 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300261 struct shell_surface *shsurf;
262 struct wl_listener shsurf_destroy_listener;
263};
264
Rusty Lynch1084da52013-08-15 09:10:08 -0700265struct shell_touch_grab {
266 struct weston_touch_grab grab;
267 struct shell_surface *shsurf;
268 struct wl_listener shsurf_destroy_listener;
269 struct weston_touch *touch;
270};
271
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300272struct weston_move_grab {
273 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100274 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500275};
276
Rusty Lynch1084da52013-08-15 09:10:08 -0700277struct weston_touch_move_grab {
278 struct shell_touch_grab base;
279 wl_fixed_t dx, dy;
280};
281
Pekka Paalanen460099f2012-01-20 16:48:25 +0200282struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300283 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500284 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200285 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200286 float x;
287 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200288 } center;
289};
290
Giulio Camuffo5085a752013-03-25 21:42:45 +0100291struct shell_seat {
292 struct weston_seat *seat;
293 struct wl_listener seat_destroy_listener;
294
295 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400296 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100297 struct wl_list surfaces_list;
298 struct wl_client *client;
299 int32_t initial_up;
300 } popup_grab;
301};
302
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400303static void
304activate(struct desktop_shell *shell, struct weston_surface *es,
305 struct weston_seat *seat);
306
307static struct workspace *
308get_current_workspace(struct desktop_shell *shell);
309
Alex Wubd3354b2012-04-17 17:20:49 +0800310static struct shell_surface *
311get_shell_surface(struct weston_surface *surface);
312
313static struct desktop_shell *
314shell_surface_get_shell(struct shell_surface *shsurf);
315
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500316static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400317surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500318
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300319static void
320shell_fade_startup(struct desktop_shell *shell);
321
Alex Wubd3354b2012-04-17 17:20:49 +0800322static bool
323shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
324{
325 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500326 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800327
328 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100329
Jason Ekstranda7af7042013-10-12 22:38:11 -0500330 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800331 return false;
332
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
334 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800335 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500336 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800337}
338
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500339static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400340destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341{
342 struct shell_grab *grab;
343
344 grab = container_of(listener, struct shell_grab,
345 shsurf_destroy_listener);
346
347 grab->shsurf = NULL;
348}
349
Jason Ekstranda7af7042013-10-12 22:38:11 -0500350static struct weston_view *
351get_default_view(struct weston_surface *surface)
352{
353 struct shell_surface *shsurf;
354 struct weston_view *view;
355
356 if (!surface || wl_list_empty(&surface->views))
357 return NULL;
358
359 shsurf = get_shell_surface(surface);
360 if (shsurf)
361 return shsurf->view;
362
363 wl_list_for_each(view, &surface->views, surface_link)
364 if (weston_view_is_mapped(view))
365 return view;
366
367 return container_of(surface->views.next, struct weston_view, surface_link);
368}
369
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300370static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400371popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400372
373static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400375 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300376 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400377 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300378 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300379{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300380 struct desktop_shell *shell = shsurf->shell;
381
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400382 popup_grab_end(pointer);
383
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300384 grab->grab.interface = interface;
385 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400386 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500387 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400388 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300389
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700390 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400391 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400392 if (shell->child.desktop_shell) {
393 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
394 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500395 weston_pointer_set_focus(pointer,
396 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400397 wl_fixed_from_int(0),
398 wl_fixed_from_int(0));
399 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300400}
401
402static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300403shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300404{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700405 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400406 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700407 grab->shsurf->grabbed = 0;
408 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300409
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700410 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300411}
412
413static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700414shell_touch_grab_start(struct shell_touch_grab *grab,
415 const struct weston_touch_grab_interface *interface,
416 struct shell_surface *shsurf,
417 struct weston_touch *touch)
418{
419 struct desktop_shell *shell = shsurf->shell;
420
421 grab->grab.interface = interface;
422 grab->shsurf = shsurf;
423 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
424 wl_signal_add(&shsurf->destroy_signal,
425 &grab->shsurf_destroy_listener);
426
427 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700428 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700429
430 weston_touch_start_grab(touch, &grab->grab);
431 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500432 weston_touch_set_focus(touch->seat,
433 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700434}
435
436static void
437shell_touch_grab_end(struct shell_touch_grab *grab)
438{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700439 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700440 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700441 grab->shsurf->grabbed = 0;
442 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700443
444 weston_touch_end_grab(grab->touch);
445}
446
447static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500448center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800449 struct weston_output *output);
450
Daniel Stone496ca172012-05-30 16:31:42 +0100451static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300452get_modifier(char *modifier)
453{
454 if (!modifier)
455 return MODIFIER_SUPER;
456
457 if (!strcmp("ctrl", modifier))
458 return MODIFIER_CTRL;
459 else if (!strcmp("alt", modifier))
460 return MODIFIER_ALT;
461 else if (!strcmp("super", modifier))
462 return MODIFIER_SUPER;
463 else
464 return MODIFIER_SUPER;
465}
466
Juan Zhaoe10d2792012-04-25 19:09:52 +0800467static enum animation_type
468get_animation_type(char *animation)
469{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800470 if (!strcmp("zoom", animation))
471 return ANIMATION_ZOOM;
472 else if (!strcmp("fade", animation))
473 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100474 else if (!strcmp("dim-layer", animation))
475 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800476 else
477 return ANIMATION_NONE;
478}
479
Alex Wu4539b082012-03-01 12:57:46 +0800480static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400481shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200482{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400483 struct weston_config_section *section;
484 int duration;
485 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200486
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400487 section = weston_config_get_section(shell->compositor->config,
488 "screensaver", NULL, NULL);
489 weston_config_section_get_string(section,
490 "path", &shell->screensaver.path, NULL);
491 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200492 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400493
494 section = weston_config_get_section(shell->compositor->config,
495 "shell", NULL, NULL);
496 weston_config_section_get_string(section,
497 "binding-modifier", &s, "super");
498 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200499 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400500 weston_config_section_get_string(section, "animation", &s, "none");
501 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200502 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700503 weston_config_section_get_string(section,
504 "startup-animation", &s, "fade");
505 shell->startup_animation_type = get_animation_type(s);
506 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700507 if (shell->startup_animation_type == ANIMATION_ZOOM)
508 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100509 weston_config_section_get_string(section, "focus-animation", &s, "none");
510 shell->focus_animation_type = get_animation_type(s);
511 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400512 weston_config_section_get_uint(section, "num-workspaces",
513 &shell->workspaces.num,
514 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200515}
516
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100517static struct weston_output *
518get_default_output(struct weston_compositor *compositor)
519{
520 return container_of(compositor->output_list.next,
521 struct weston_output, link);
522}
523
524
525/* no-op func for checking focus surface */
526static void
527focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy,
528 int32_t width, int32_t height)
529{
530}
531
532static struct focus_surface *
533get_focus_surface(struct weston_surface *surface)
534{
535 if (surface->configure == focus_surface_configure)
536 return surface->configure_private;
537 else
538 return NULL;
539}
540
541static bool
542is_focus_surface (struct weston_surface *es)
543{
544 return (es->configure == focus_surface_configure);
545}
546
547static bool
548is_focus_view (struct weston_view *view)
549{
550 return is_focus_surface (view->surface);
551}
552
553static struct focus_surface *
554create_focus_surface(struct weston_compositor *ec,
555 struct weston_output *output)
556{
557 struct focus_surface *fsurf = NULL;
558 struct weston_surface *surface = NULL;
559
560 fsurf = malloc(sizeof *fsurf);
561 if (!fsurf)
562 return NULL;
563
564 fsurf->surface = weston_surface_create(ec);
565 surface = fsurf->surface;
566 if (surface == NULL) {
567 free(fsurf);
568 return NULL;
569 }
570
571 surface->configure = focus_surface_configure;
572 surface->output = output;
573 surface->configure_private = fsurf;
574
575 fsurf->view = weston_view_create (surface);
576
577 weston_view_configure(fsurf->view, output->x, output->y,
578 output->width, output->height);
579 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
580 pixman_region32_fini(&surface->opaque);
581 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
582 output->width, output->height);
583 pixman_region32_fini(&surface->input);
584 pixman_region32_init(&surface->input);
585
586 wl_list_init(&fsurf->workspace_transform.link);
587
588 return fsurf;
589}
590
591static void
592focus_surface_destroy(struct focus_surface *fsurf)
593{
594 weston_surface_destroy(fsurf->surface);
595 free(fsurf);
596}
597
598static void
599focus_animation_done(struct weston_view_animation *animation, void *data)
600{
601 struct workspace *ws = data;
602
603 ws->focus_animation = NULL;
604}
605
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200606static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200607focus_state_destroy(struct focus_state *state)
608{
609 wl_list_remove(&state->seat_destroy_listener.link);
610 wl_list_remove(&state->surface_destroy_listener.link);
611 free(state);
612}
613
614static void
615focus_state_seat_destroy(struct wl_listener *listener, void *data)
616{
617 struct focus_state *state = container_of(listener,
618 struct focus_state,
619 seat_destroy_listener);
620
621 wl_list_remove(&state->link);
622 focus_state_destroy(state);
623}
624
625static void
626focus_state_surface_destroy(struct wl_listener *listener, void *data)
627{
628 struct focus_state *state = container_of(listener,
629 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400630 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400631 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500632 struct weston_surface *main_surface, *next;
633 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200634
Pekka Paalanen01388e22013-04-25 13:57:44 +0300635 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
636
Kristian Høgsberge3778222012-07-31 17:29:30 -0400637 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500638 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
639 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400640 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100641 if (is_focus_view(view))
642 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400643
Jason Ekstranda7af7042013-10-12 22:38:11 -0500644 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400645 break;
646 }
647
Pekka Paalanen01388e22013-04-25 13:57:44 +0300648 /* if the focus was a sub-surface, activate its main surface */
649 if (main_surface != state->keyboard_focus)
650 next = main_surface;
651
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100652 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400653 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100654 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400655 activate(shell, next, state->seat);
656 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100657 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
658 if (state->ws->focus_animation)
659 weston_view_animation_destroy(state->ws->focus_animation);
660
661 state->ws->focus_animation = weston_fade_run(
662 state->ws->fsurf_front->view,
663 state->ws->fsurf_front->view->alpha, 0.0, 300,
664 focus_animation_done, state->ws);
665 }
666
Kristian Høgsberge3778222012-07-31 17:29:30 -0400667 wl_list_remove(&state->link);
668 focus_state_destroy(state);
669 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200670}
671
672static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400673focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200674{
Jonas Ådahl04769742012-06-13 00:01:24 +0200675 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200676
677 state = malloc(sizeof *state);
678 if (state == NULL)
679 return NULL;
680
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100681 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400682 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200683 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400684 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200685
686 state->seat_destroy_listener.notify = focus_state_seat_destroy;
687 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400688 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200689 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400690 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200691
692 return state;
693}
694
Jonas Ådahl8538b222012-08-29 22:13:03 +0200695static struct focus_state *
696ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
697{
698 struct workspace *ws = get_current_workspace(shell);
699 struct focus_state *state;
700
701 wl_list_for_each(state, &ws->focus_list, link)
702 if (state->seat == seat)
703 break;
704
705 if (&state->link == &ws->focus_list)
706 state = focus_state_create(seat, ws);
707
708 return state;
709}
710
Jonas Ådahl04769742012-06-13 00:01:24 +0200711static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400712restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200713{
714 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400715 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200716
717 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400718 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200719
Kristian Høgsberge3148752013-05-06 23:19:49 -0400720 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200721 }
722}
723
724static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200725replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
726 struct weston_seat *seat)
727{
728 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400729 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200730
731 wl_list_for_each(state, &ws->focus_list, link) {
732 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400733 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500734 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200735 return;
736 }
737 }
738}
739
740static void
741drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
742 struct weston_surface *surface)
743{
744 struct focus_state *state;
745
746 wl_list_for_each(state, &ws->focus_list, link)
747 if (state->keyboard_focus == surface)
748 state->keyboard_focus = NULL;
749}
750
751static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100752animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
753 struct weston_view *from, struct weston_view *to)
754{
755 struct weston_output *output;
756 bool focus_surface_created = false;
757
758 /* FIXME: Only support dim animation using two layers */
759 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
760 return;
761
762 output = get_default_output(shell->compositor);
763 if (ws->fsurf_front == NULL && (from || to)) {
764 ws->fsurf_front = create_focus_surface(shell->compositor, output);
765 ws->fsurf_back = create_focus_surface(shell->compositor, output);
766 ws->fsurf_front->view->alpha = 0.0;
767 ws->fsurf_back->view->alpha = 0.0;
768 focus_surface_created = true;
769 } else {
770 wl_list_remove(&ws->fsurf_front->view->layer_link);
771 wl_list_remove(&ws->fsurf_back->view->layer_link);
772 }
773
774 if (ws->focus_animation) {
775 weston_view_animation_destroy(ws->focus_animation);
776 ws->focus_animation = NULL;
777 }
778
779 if (to)
780 wl_list_insert(&to->layer_link,
781 &ws->fsurf_front->view->layer_link);
782 else if (from)
783 wl_list_insert(&ws->layer.view_list,
784 &ws->fsurf_front->view->layer_link);
785
786 if (focus_surface_created) {
787 ws->focus_animation = weston_fade_run(
788 ws->fsurf_front->view,
789 ws->fsurf_front->view->alpha, 0.6, 300,
790 focus_animation_done, ws);
791 } else if (from) {
792 wl_list_insert(&from->layer_link,
793 &ws->fsurf_back->view->layer_link);
794 ws->focus_animation = weston_stable_fade_run(
795 ws->fsurf_front->view, 0.0,
796 ws->fsurf_back->view, 0.6,
797 focus_animation_done, ws);
798 } else if (to) {
799 wl_list_insert(&ws->layer.view_list,
800 &ws->fsurf_back->view->layer_link);
801 ws->focus_animation = weston_stable_fade_run(
802 ws->fsurf_front->view, 0.0,
803 ws->fsurf_back->view, 0.6,
804 focus_animation_done, ws);
805 }
806}
807
808static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200809workspace_destroy(struct workspace *ws)
810{
Jonas Ådahl04769742012-06-13 00:01:24 +0200811 struct focus_state *state, *next;
812
813 wl_list_for_each_safe(state, next, &ws->focus_list, link)
814 focus_state_destroy(state);
815
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100816 if (ws->fsurf_front)
817 focus_surface_destroy(ws->fsurf_front);
818 if (ws->fsurf_back)
819 focus_surface_destroy(ws->fsurf_back);
820
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200821 free(ws);
822}
823
Jonas Ådahl04769742012-06-13 00:01:24 +0200824static void
825seat_destroyed(struct wl_listener *listener, void *data)
826{
827 struct weston_seat *seat = data;
828 struct focus_state *state, *next;
829 struct workspace *ws = container_of(listener,
830 struct workspace,
831 seat_destroyed_listener);
832
833 wl_list_for_each_safe(state, next, &ws->focus_list, link)
834 if (state->seat == seat)
835 wl_list_remove(&state->link);
836}
837
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200838static struct workspace *
839workspace_create(void)
840{
841 struct workspace *ws = malloc(sizeof *ws);
842 if (ws == NULL)
843 return NULL;
844
845 weston_layer_init(&ws->layer, NULL);
846
Jonas Ådahl04769742012-06-13 00:01:24 +0200847 wl_list_init(&ws->focus_list);
848 wl_list_init(&ws->seat_destroyed_listener.link);
849 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850 ws->fsurf_front = NULL;
851 ws->fsurf_back = NULL;
852 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200853
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200854 return ws;
855}
856
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857static int
858workspace_is_empty(struct workspace *ws)
859{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500860 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200861}
862
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200863static struct workspace *
864get_workspace(struct desktop_shell *shell, unsigned int index)
865{
866 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200867 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200868 pws += index;
869 return *pws;
870}
871
872static struct workspace *
873get_current_workspace(struct desktop_shell *shell)
874{
875 return get_workspace(shell, shell->workspaces.current);
876}
877
878static void
879activate_workspace(struct desktop_shell *shell, unsigned int index)
880{
881 struct workspace *ws;
882
883 ws = get_workspace(shell, index);
884 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
885
886 shell->workspaces.current = index;
887}
888
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200889static unsigned int
890get_output_height(struct weston_output *output)
891{
892 return abs(output->region.extents.y1 - output->region.extents.y2);
893}
894
895static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100896view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200897{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200898 struct weston_transform *transform;
899
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100900 if (is_focus_view(view)) {
901 struct focus_surface *fsurf = get_focus_surface(view->surface);
902 transform = &fsurf->workspace_transform;
903 } else {
904 struct shell_surface *shsurf = get_shell_surface(view->surface);
905 transform = &shsurf->workspace_transform;
906 }
907
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200908 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500909 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100910 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200911
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100912 weston_matrix_init(&transform->matrix);
913 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200914 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500915 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200916}
917
918static void
919workspace_translate_out(struct workspace *ws, double fraction)
920{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500921 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200922 unsigned int height;
923 double d;
924
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
926 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200927 d = height * fraction;
928
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100929 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200930 }
931}
932
933static void
934workspace_translate_in(struct workspace *ws, double fraction)
935{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200937 unsigned int height;
938 double d;
939
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
941 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200942
943 if (fraction > 0)
944 d = -(height - height * fraction);
945 else
946 d = height + height * fraction;
947
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100948 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200949 }
950}
951
952static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200953broadcast_current_workspace_state(struct desktop_shell *shell)
954{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700955 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200956
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700957 wl_resource_for_each(resource, &shell->workspaces.client_list)
958 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200959 shell->workspaces.current,
960 shell->workspaces.num);
961}
962
963static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964reverse_workspace_change_animation(struct desktop_shell *shell,
965 unsigned int index,
966 struct workspace *from,
967 struct workspace *to)
968{
969 shell->workspaces.current = index;
970
971 shell->workspaces.anim_to = to;
972 shell->workspaces.anim_from = from;
973 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
974 shell->workspaces.anim_timestamp = 0;
975
Scott Moreau4272e632012-08-13 09:58:41 -0600976 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200977}
978
979static void
980workspace_deactivate_transforms(struct workspace *ws)
981{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100983 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100986 if (is_focus_view(view)) {
987 struct focus_surface *fsurf = get_focus_surface(view->surface);
988 transform = &fsurf->workspace_transform;
989 } else {
990 struct shell_surface *shsurf = get_shell_surface(view->surface);
991 transform = &shsurf->workspace_transform;
992 }
993
994 if (!wl_list_empty(&transform->link)) {
995 wl_list_remove(&transform->link);
996 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200997 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200999 }
1000}
1001
1002static void
1003finish_workspace_change_animation(struct desktop_shell *shell,
1004 struct workspace *from,
1005 struct workspace *to)
1006{
Scott Moreau4272e632012-08-13 09:58:41 -06001007 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001008
1009 wl_list_remove(&shell->workspaces.animation.link);
1010 workspace_deactivate_transforms(from);
1011 workspace_deactivate_transforms(to);
1012 shell->workspaces.anim_to = NULL;
1013
1014 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1015}
1016
1017static void
1018animate_workspace_change_frame(struct weston_animation *animation,
1019 struct weston_output *output, uint32_t msecs)
1020{
1021 struct desktop_shell *shell =
1022 container_of(animation, struct desktop_shell,
1023 workspaces.animation);
1024 struct workspace *from = shell->workspaces.anim_from;
1025 struct workspace *to = shell->workspaces.anim_to;
1026 uint32_t t;
1027 double x, y;
1028
1029 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1030 finish_workspace_change_animation(shell, from, to);
1031 return;
1032 }
1033
1034 if (shell->workspaces.anim_timestamp == 0) {
1035 if (shell->workspaces.anim_current == 0.0)
1036 shell->workspaces.anim_timestamp = msecs;
1037 else
1038 shell->workspaces.anim_timestamp =
1039 msecs -
1040 /* Invers of movement function 'y' below. */
1041 (asin(1.0 - shell->workspaces.anim_current) *
1042 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1043 M_2_PI);
1044 }
1045
1046 t = msecs - shell->workspaces.anim_timestamp;
1047
1048 /*
1049 * x = [0, π/2]
1050 * y(x) = sin(x)
1051 */
1052 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1053 y = sin(x);
1054
1055 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001056 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001057
1058 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1059 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1060 shell->workspaces.anim_current = y;
1061
Scott Moreau4272e632012-08-13 09:58:41 -06001062 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001063 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001064 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001065 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001066}
1067
1068static void
1069animate_workspace_change(struct desktop_shell *shell,
1070 unsigned int index,
1071 struct workspace *from,
1072 struct workspace *to)
1073{
1074 struct weston_output *output;
1075
1076 int dir;
1077
1078 if (index > shell->workspaces.current)
1079 dir = -1;
1080 else
1081 dir = 1;
1082
1083 shell->workspaces.current = index;
1084
1085 shell->workspaces.anim_dir = dir;
1086 shell->workspaces.anim_from = from;
1087 shell->workspaces.anim_to = to;
1088 shell->workspaces.anim_current = 0.0;
1089 shell->workspaces.anim_timestamp = 0;
1090
1091 output = container_of(shell->compositor->output_list.next,
1092 struct weston_output, link);
1093 wl_list_insert(&output->animation_list,
1094 &shell->workspaces.animation.link);
1095
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001096 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097
1098 workspace_translate_in(to, 0);
1099
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001100 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001101
Scott Moreau4272e632012-08-13 09:58:41 -06001102 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001103}
1104
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001105static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001106update_workspace(struct desktop_shell *shell, unsigned int index,
1107 struct workspace *from, struct workspace *to)
1108{
1109 shell->workspaces.current = index;
1110 wl_list_insert(&from->layer.link, &to->layer.link);
1111 wl_list_remove(&from->layer.link);
1112}
1113
1114static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001115change_workspace(struct desktop_shell *shell, unsigned int index)
1116{
1117 struct workspace *from;
1118 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001119 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001120
1121 if (index == shell->workspaces.current)
1122 return;
1123
1124 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001126 return;
1127
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001128 from = get_current_workspace(shell);
1129 to = get_workspace(shell, index);
1130
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001131 if (shell->workspaces.anim_from == to &&
1132 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001133 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001134 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001135 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001136 return;
1137 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001138
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001139 if (shell->workspaces.anim_to != NULL)
1140 finish_workspace_change_animation(shell,
1141 shell->workspaces.anim_from,
1142 shell->workspaces.anim_to);
1143
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001144 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001145
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001146 if (shell->focus_animation_type != ANIMATION_NONE) {
1147 wl_list_for_each(state, &from->focus_list, link)
1148 if (state->keyboard_focus)
1149 animate_focus_change(shell, from,
1150 get_default_view(state->keyboard_focus), NULL);
1151
1152 wl_list_for_each(state, &to->focus_list, link)
1153 if (state->keyboard_focus)
1154 animate_focus_change(shell, to,
1155 NULL, get_default_view(state->keyboard_focus));
1156 }
1157
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001158 if (workspace_is_empty(to) && workspace_is_empty(from))
1159 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160 else
1161 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001162
1163 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001164}
1165
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001166static bool
1167workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1168{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001170 struct wl_list *e;
1171
1172 if (wl_list_empty(list))
1173 return false;
1174
1175 e = list->next;
1176
1177 if (e->next != list)
1178 return false;
1179
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001181}
1182
1183static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184move_view_to_workspace(struct desktop_shell *shell,
1185 struct weston_view *view,
1186 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001187{
1188 struct workspace *from;
1189 struct workspace *to;
1190 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001191 struct weston_surface *focus;
1192
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001194
1195 if (workspace == shell->workspaces.current)
1196 return;
1197
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001198 if (workspace >= shell->workspaces.num)
1199 workspace = shell->workspaces.num - 1;
1200
Jonas Ådahle9d22502012-08-29 22:13:01 +02001201 from = get_current_workspace(shell);
1202 to = get_workspace(shell, workspace);
1203
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 wl_list_remove(&view->layer_link);
1205 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001208 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1209 if (!seat->keyboard)
1210 continue;
1211
1212 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001214 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001215 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001216
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001218}
1219
1220static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001222 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001223 unsigned int index)
1224{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001225 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001227 struct shell_surface *shsurf;
1228 struct workspace *from;
1229 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001230 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001231
Pekka Paalanen01388e22013-04-25 13:57:44 +03001232 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 view = get_default_view(surface);
1234 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001235 index == shell->workspaces.current ||
1236 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001237 return;
1238
1239 from = get_current_workspace(shell);
1240 to = get_workspace(shell, index);
1241
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 wl_list_remove(&view->layer_link);
1243 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001244
Jonas Ådahle9d22502012-08-29 22:13:01 +02001245 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001246 drop_focus_state(shell, from, surface);
1247
1248 if (shell->workspaces.anim_from == to &&
1249 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001250 wl_list_remove(&to->layer.link);
1251 wl_list_insert(from->layer.link.prev, &to->layer.link);
1252
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001253 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001254 broadcast_current_workspace_state(shell);
1255
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001256 return;
1257 }
1258
1259 if (shell->workspaces.anim_to != NULL)
1260 finish_workspace_change_animation(shell,
1261 shell->workspaces.anim_from,
1262 shell->workspaces.anim_to);
1263
1264 if (workspace_is_empty(from) &&
1265 workspace_has_only(to, surface))
1266 update_workspace(shell, index, from, to);
1267 else {
1268 shsurf = get_shell_surface(surface);
1269 if (wl_list_empty(&shsurf->workspace_transform.link))
1270 wl_list_insert(&shell->workspaces.anim_sticky_list,
1271 &shsurf->workspace_transform.link);
1272
1273 animate_workspace_change(shell, index, from, to);
1274 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001275
1276 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001277
1278 state = ensure_focus_state(shell, seat);
1279 if (state != NULL)
1280 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001281}
1282
1283static void
1284workspace_manager_move_surface(struct wl_client *client,
1285 struct wl_resource *resource,
1286 struct wl_resource *surface_resource,
1287 uint32_t workspace)
1288{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001289 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001290 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001291 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001292 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001294
Pekka Paalanen01388e22013-04-25 13:57:44 +03001295 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 view = get_default_view(main_surface);
1297 if (!view)
1298 return;
1299 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001300}
1301
1302static const struct workspace_manager_interface workspace_manager_implementation = {
1303 workspace_manager_move_surface,
1304};
1305
1306static void
1307unbind_resource(struct wl_resource *resource)
1308{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001309 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001310}
1311
1312static void
1313bind_workspace_manager(struct wl_client *client,
1314 void *data, uint32_t version, uint32_t id)
1315{
1316 struct desktop_shell *shell = data;
1317 struct wl_resource *resource;
1318
Jason Ekstranda85118c2013-06-27 20:17:02 -05001319 resource = wl_resource_create(client,
1320 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001321
1322 if (resource == NULL) {
1323 weston_log("couldn't add workspace manager object");
1324 return;
1325 }
1326
Jason Ekstranda85118c2013-06-27 20:17:02 -05001327 wl_resource_set_implementation(resource,
1328 &workspace_manager_implementation,
1329 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001330 wl_list_insert(&shell->workspaces.client_list,
1331 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001332
1333 workspace_manager_send_state(resource,
1334 shell->workspaces.current,
1335 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001336}
1337
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001338static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001339touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1340 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1341{
1342}
1343
1344static void
1345touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1346{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001347 struct weston_touch_move_grab *move =
1348 (struct weston_touch_move_grab *) container_of(
1349 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001350
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001351 if (grab->touch->seat->num_tp == 0) {
1352 shell_touch_grab_end(&move->base);
1353 free(move);
1354 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001355}
1356
1357static void
1358touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1359 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1360{
1361 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1362 struct shell_surface *shsurf = move->base.shsurf;
1363 struct weston_surface *es;
1364 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1365 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1366
1367 if (!shsurf)
1368 return;
1369
1370 es = shsurf->surface;
1371
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 weston_view_configure(shsurf->view, dx, dy,
1373 shsurf->view->geometry.width,
1374 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001375
1376 weston_compositor_schedule_repaint(es->compositor);
1377}
1378
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001379static void
1380touch_move_grab_cancel(struct weston_touch_grab *grab)
1381{
1382 struct weston_touch_move_grab *move =
1383 (struct weston_touch_move_grab *) container_of(
1384 grab, struct shell_touch_grab, grab);
1385
1386 shell_touch_grab_end(&move->base);
1387 free(move);
1388}
1389
Rusty Lynch1084da52013-08-15 09:10:08 -07001390static const struct weston_touch_grab_interface touch_move_grab_interface = {
1391 touch_move_grab_down,
1392 touch_move_grab_up,
1393 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001394 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001395};
1396
1397static int
1398surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1399{
1400 struct weston_touch_move_grab *move;
1401
1402 if (!shsurf)
1403 return -1;
1404
1405 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1406 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001407 if (shsurf->grabbed)
1408 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001409
1410 move = malloc(sizeof *move);
1411 if (!move)
1412 return -1;
1413
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001415 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001417 seat->touch->grab_y;
1418
1419 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1420 seat->touch);
1421
1422 return 0;
1423}
1424
1425static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001426noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001427{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001428}
1429
1430static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001431move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1432 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001433{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001434 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001435 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001436 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001437 int dx, dy;
1438
1439 weston_pointer_move(pointer, x, y);
1440 dx = wl_fixed_to_int(pointer->x + move->dx);
1441 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001442
1443 if (!shsurf)
1444 return;
1445
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 weston_view_configure(shsurf->view, dx, dy,
1447 shsurf->view->geometry.width,
1448 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001451}
1452
1453static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001454move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001455 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001456{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001457 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1458 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001459 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001460 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001461
Daniel Stone4dbadb12012-05-30 16:31:51 +01001462 if (pointer->button_count == 0 &&
1463 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001464 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001465 free(grab);
1466 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001467}
1468
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001469static void
1470move_grab_cancel(struct weston_pointer_grab *grab)
1471{
1472 struct shell_grab *shell_grab =
1473 container_of(grab, struct shell_grab, grab);
1474
1475 shell_grab_end(shell_grab);
1476 free(grab);
1477}
1478
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001479static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001480 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001481 move_grab_motion,
1482 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001483 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001484};
1485
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001486static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001487surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001488{
1489 struct weston_move_grab *move;
1490
1491 if (!shsurf)
1492 return -1;
1493
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001494 if (shsurf->grabbed)
1495 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001496 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1497 return 0;
1498
1499 move = malloc(sizeof *move);
1500 if (!move)
1501 return -1;
1502
Jason Ekstranda7af7042013-10-12 22:38:11 -05001503 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001504 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001506 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001507
1508 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001509 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001510
1511 return 0;
1512}
1513
1514static void
1515shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1516 struct wl_resource *seat_resource, uint32_t serial)
1517{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001518 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001519 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001520 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001521
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001522 if (seat->pointer &&
1523 seat->pointer->button_count > 0 &&
1524 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001526 if ((surface == shsurf->surface) &&
1527 (surface_move(shsurf, seat) < 0))
1528 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001529 } else if (seat->touch &&
1530 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001532 if ((surface == shsurf->surface) &&
1533 (surface_touch_move(shsurf, seat) < 0))
1534 wl_resource_post_no_memory(resource);
1535 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001536}
1537
1538struct weston_resize_grab {
1539 struct shell_grab base;
1540 uint32_t edges;
1541 int32_t width, height;
1542};
1543
1544static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001545resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1546 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001547{
1548 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001549 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001550 struct shell_surface *shsurf = resize->base.shsurf;
1551 int32_t width, height;
1552 wl_fixed_t from_x, from_y;
1553 wl_fixed_t to_x, to_y;
1554
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001555 weston_pointer_move(pointer, x, y);
1556
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001557 if (!shsurf)
1558 return;
1559
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 weston_view_from_global_fixed(shsurf->view,
1561 pointer->grab_x, pointer->grab_y,
1562 &from_x, &from_y);
1563 weston_view_from_global_fixed(shsurf->view,
1564 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001565
1566 width = resize->width;
1567 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1568 width += wl_fixed_to_int(from_x - to_x);
1569 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1570 width += wl_fixed_to_int(to_x - from_x);
1571 }
1572
1573 height = resize->height;
1574 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1575 height += wl_fixed_to_int(from_y - to_y);
1576 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1577 height += wl_fixed_to_int(to_y - from_y);
1578 }
1579
1580 shsurf->client->send_configure(shsurf->surface,
1581 resize->edges, width, height);
1582}
1583
1584static void
1585send_configure(struct weston_surface *surface,
1586 uint32_t edges, int32_t width, int32_t height)
1587{
1588 struct shell_surface *shsurf = get_shell_surface(surface);
1589
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001590 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001591 edges, width, height);
1592}
1593
1594static const struct weston_shell_client shell_client = {
1595 send_configure
1596};
1597
1598static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001599resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001600 uint32_t time, uint32_t button, uint32_t state_w)
1601{
1602 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001603 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001604 enum wl_pointer_button_state state = state_w;
1605
1606 if (pointer->button_count == 0 &&
1607 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1608 shell_grab_end(&resize->base);
1609 free(grab);
1610 }
1611}
1612
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001613static void
1614resize_grab_cancel(struct weston_pointer_grab *grab)
1615{
1616 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1617
1618 shell_grab_end(&resize->base);
1619 free(grab);
1620}
1621
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001622static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001623 noop_grab_focus,
1624 resize_grab_motion,
1625 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001626 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001627};
1628
Giulio Camuffob8366642013-04-25 13:57:46 +03001629/*
1630 * Returns the bounding box of a surface and all its sub-surfaces,
1631 * in the surface coordinates system. */
1632static void
1633surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1634 int32_t *y, int32_t *w, int32_t *h) {
1635 pixman_region32_t region;
1636 pixman_box32_t *box;
1637 struct weston_subsurface *subsurface;
1638
1639 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640 surface->width,
1641 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001642
1643 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1644 pixman_region32_union_rect(&region, &region,
1645 subsurface->position.x,
1646 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001647 subsurface->surface->width,
1648 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001649 }
1650
1651 box = pixman_region32_extents(&region);
1652 if (x)
1653 *x = box->x1;
1654 if (y)
1655 *y = box->y1;
1656 if (w)
1657 *w = box->x2 - box->x1;
1658 if (h)
1659 *h = box->y2 - box->y1;
1660
1661 pixman_region32_fini(&region);
1662}
1663
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001664static int
1665surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001666 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001667{
1668 struct weston_resize_grab *resize;
1669
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001670 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1671 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001672 return 0;
1673
1674 if (edges == 0 || edges > 15 ||
1675 (edges & 3) == 3 || (edges & 12) == 12)
1676 return 0;
1677
1678 resize = malloc(sizeof *resize);
1679 if (!resize)
1680 return -1;
1681
1682 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001683 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1684 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001685
1686 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001687 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688
1689 return 0;
1690}
1691
1692static void
1693shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1694 struct wl_resource *seat_resource, uint32_t serial,
1695 uint32_t edges)
1696{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001697 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001698 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001699 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001700
1701 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1702 return;
1703
Jason Ekstranda7af7042013-10-12 22:38:11 -05001704 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001705 if (seat->pointer->button_count == 0 ||
1706 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001707 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708 return;
1709
Kristian Høgsberge3148752013-05-06 23:19:49 -04001710 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001711 wl_resource_post_no_memory(resource);
1712}
1713
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001714static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001715end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1716
1717static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001718busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001719{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001720 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001721 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001722 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001723 wl_fixed_t sx, sy;
1724
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725 view = weston_compositor_pick_view(pointer->seat->compositor,
1726 pointer->x, pointer->y,
1727 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001728
Jason Ekstranda7af7042013-10-12 22:38:11 -05001729 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001730 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001731}
1732
1733static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001734busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1735 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001736{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001737 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001738}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001739
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001740static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001741busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001742 uint32_t time, uint32_t button, uint32_t state)
1743{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001744 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001745 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001746 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001747
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001748 if (shsurf && button == BTN_LEFT && state) {
1749 activate(shsurf->shell, shsurf->surface, seat);
1750 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001751 } else if (shsurf && button == BTN_RIGHT && state) {
1752 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001753 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001754 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001755}
1756
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001757static void
1758busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1759{
1760 struct shell_grab *grab = (struct shell_grab *) base;
1761
1762 shell_grab_end(grab);
1763 free(grab);
1764}
1765
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001766static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001767 busy_cursor_grab_focus,
1768 busy_cursor_grab_motion,
1769 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001770 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001771};
1772
1773static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001774set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001775{
1776 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001777
1778 grab = malloc(sizeof *grab);
1779 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001780 return;
1781
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001782 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1783 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001784}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001785
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001786static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001787end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001788{
1789 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1790
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001791 if (grab->grab.interface == &busy_cursor_grab_interface &&
1792 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001793 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001795 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001796}
1797
Scott Moreau9521d5e2012-04-19 13:06:17 -06001798static void
1799ping_timer_destroy(struct shell_surface *shsurf)
1800{
1801 if (!shsurf || !shsurf->ping_timer)
1802 return;
1803
1804 if (shsurf->ping_timer->source)
1805 wl_event_source_remove(shsurf->ping_timer->source);
1806
1807 free(shsurf->ping_timer);
1808 shsurf->ping_timer = NULL;
1809}
1810
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001811static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001812ping_timeout_handler(void *data)
1813{
1814 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001815 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001816
Scott Moreau9521d5e2012-04-19 13:06:17 -06001817 /* Client is not responding */
1818 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001819
1820 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 if (seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001822 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001823
1824 return 1;
1825}
1826
1827static void
1828ping_handler(struct weston_surface *surface, uint32_t serial)
1829{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001830 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001831 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001832 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001833
1834 if (!shsurf)
1835 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001836 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001837 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001838
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001839 if (shsurf->surface == shsurf->shell->grab_surface)
1840 return;
1841
Scott Moreauff1db4a2012-04-17 19:06:18 -06001842 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001843 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001844 if (!shsurf->ping_timer)
1845 return;
1846
1847 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001848 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1849 shsurf->ping_timer->source =
1850 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1851 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1852
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001853 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001854 }
1855}
1856
1857static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001858handle_pointer_focus(struct wl_listener *listener, void *data)
1859{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001860 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001861 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001862 struct weston_compositor *compositor;
1863 struct shell_surface *shsurf;
1864 uint32_t serial;
1865
Jason Ekstranda7af7042013-10-12 22:38:11 -05001866 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001867 return;
1868
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 compositor = view->surface->compositor;
1870 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001871
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001872 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001873 set_busy_cursor(shsurf, pointer);
1874 } else {
1875 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001876 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001877 }
1878}
1879
1880static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001881create_pointer_focus_listener(struct weston_seat *seat)
1882{
1883 struct wl_listener *listener;
1884
Kristian Høgsberge3148752013-05-06 23:19:49 -04001885 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001886 return;
1887
1888 listener = malloc(sizeof *listener);
1889 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001890 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001891}
1892
1893static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001894shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1895 uint32_t serial)
1896{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001897 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001898 struct weston_seat *seat;
1899 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001900
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001901 if (shsurf->ping_timer == NULL)
1902 /* Just ignore unsolicited pong. */
1903 return;
1904
Scott Moreauff1db4a2012-04-17 19:06:18 -06001905 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001906 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001907 wl_list_for_each(seat, &ec->seat_list, link) {
1908 if(seat->pointer)
1909 end_busy_cursor(shsurf, seat->pointer);
1910 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001911 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001912 }
1913}
1914
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001915static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001916set_title(struct shell_surface *shsurf, const char *title)
1917{
1918 free(shsurf->title);
1919 shsurf->title = strdup(title);
1920}
1921
1922static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001923shell_surface_set_title(struct wl_client *client,
1924 struct wl_resource *resource, const char *title)
1925{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001926 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001927
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001928 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001929}
1930
1931static void
1932shell_surface_set_class(struct wl_client *client,
1933 struct wl_resource *resource, const char *class)
1934{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001935 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001936
1937 free(shsurf->class);
1938 shsurf->class = strdup(class);
1939}
1940
Alex Wu4539b082012-03-01 12:57:46 +08001941static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001942restore_output_mode(struct weston_output *output)
1943{
Hardening57388e42013-09-18 23:56:36 +02001944 if (output->current_mode != output->original_mode ||
1945 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001946 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001947 output->original_mode,
1948 output->original_scale,
1949 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001950}
1951
1952static void
1953restore_all_output_modes(struct weston_compositor *compositor)
1954{
1955 struct weston_output *output;
1956
1957 wl_list_for_each(output, &compositor->output_list, link)
1958 restore_output_mode(output);
1959}
1960
1961static void
Alex Wu4539b082012-03-01 12:57:46 +08001962shell_unset_fullscreen(struct shell_surface *shsurf)
1963{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001964 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001965 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001966 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1967 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001968 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001969 }
Alex Wu4539b082012-03-01 12:57:46 +08001970 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1971 shsurf->fullscreen.framerate = 0;
1972 wl_list_remove(&shsurf->fullscreen.transform.link);
1973 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001974 if (shsurf->fullscreen.black_view)
1975 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1976 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001977 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001978 weston_view_set_position(shsurf->view,
1979 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001980 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001981 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001982 &shsurf->rotation.transform.link);
1983 shsurf->saved_rotation_valid = false;
1984 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001985
1986 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001987 wl_list_remove(&shsurf->view->layer_link);
1988 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001989}
1990
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001991static void
1992shell_unset_maximized(struct shell_surface *shsurf)
1993{
1994 struct workspace *ws;
1995 /* undo all maximized things here */
1996 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 weston_view_set_position(shsurf->view,
1998 shsurf->saved_x,
1999 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002000
2001 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002002 wl_list_insert(&shsurf->view->geometry.transformation_list,
2003 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002004 shsurf->saved_rotation_valid = false;
2005 }
2006
2007 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002008 wl_list_remove(&shsurf->view->layer_link);
2009 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002010}
2011
Pekka Paalanen98262232011-12-01 10:42:22 +02002012static int
2013reset_shell_surface_type(struct shell_surface *surface)
2014{
2015 switch (surface->type) {
2016 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08002017 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002018 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002019 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002020 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002021 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002022 case SHELL_SURFACE_NONE:
2023 case SHELL_SURFACE_TOPLEVEL:
2024 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002025 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002026 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02002027 break;
2028 }
2029
2030 surface->type = SHELL_SURFACE_NONE;
2031 return 0;
2032}
2033
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002034static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002035set_surface_type(struct shell_surface *shsurf)
2036{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002037 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002038 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002039
2040 reset_shell_surface_type(shsurf);
2041
2042 shsurf->type = shsurf->next_type;
2043 shsurf->next_type = SHELL_SURFACE_NONE;
2044
2045 switch (shsurf->type) {
2046 case SHELL_SURFACE_TOPLEVEL:
2047 break;
2048 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 if (pev)
2050 weston_view_set_position(shsurf->view,
2051 pev->geometry.x + shsurf->transient.x,
2052 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002053 break;
2054
2055 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002056 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 shsurf->saved_x = shsurf->view->geometry.x;
2058 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002059 shsurf->saved_position_valid = true;
2060
2061 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2062 wl_list_remove(&shsurf->rotation.transform.link);
2063 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002065 shsurf->saved_rotation_valid = true;
2066 }
2067 break;
2068
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002069 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070 weston_view_set_position(shsurf->view, shsurf->transient.x,
2071 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002072 break;
2073
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002074 default:
2075 break;
2076 }
2077}
2078
2079static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002080set_toplevel(struct shell_surface *shsurf)
2081{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002082 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002083}
2084
2085static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002086shell_surface_set_toplevel(struct wl_client *client,
2087 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002088{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002089 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002090
Tiago Vignattibc052c92012-04-19 16:18:18 +03002091 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002092}
2093
2094static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04002095set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002096 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04002097{
2098 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002099 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002100 shsurf->transient.x = x;
2101 shsurf->transient.y = y;
2102 shsurf->transient.flags = flags;
2103 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
2104}
2105
2106static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002107shell_surface_set_transient(struct wl_client *client,
2108 struct wl_resource *resource,
2109 struct wl_resource *parent_resource,
2110 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002111{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002112 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002113 struct weston_surface *parent =
2114 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002115
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002116 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002117}
2118
Tiago Vignattibe143262012-04-16 17:31:41 +03002119static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002120shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002121{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002122 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002123}
2124
2125static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002126get_output_panel_height(struct desktop_shell *shell,
2127 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08002128{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002129 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08002130 int panel_height = 0;
2131
2132 if (!output)
2133 return 0;
2134
Jason Ekstranda7af7042013-10-12 22:38:11 -05002135 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2136 if (view->surface->output == output) {
2137 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08002138 break;
2139 }
2140 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002141
Juan Zhao96879df2012-02-07 08:45:41 +08002142 return panel_height;
2143}
2144
2145static void
2146shell_surface_set_maximized(struct wl_client *client,
2147 struct wl_resource *resource,
2148 struct wl_resource *output_resource )
2149{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002150 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08002151 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03002152 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08002153 uint32_t edges = 0, panel_height = 0;
2154
2155 /* get the default output, if the client set it as NULL
2156 check whether the ouput is available */
2157 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002158 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002159 else if (es->output)
2160 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002161 else
2162 shsurf->output = get_default_output(es->compositor);
2163
Tiago Vignattibe143262012-04-16 17:31:41 +03002164 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01002165 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002166 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002167
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002168 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002169 shsurf->output->width,
2170 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002171
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002172 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002173}
2174
Alex Wu21858432012-04-01 20:13:08 +08002175static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002176black_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 +08002177
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002179create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002180 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002181 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002182{
2183 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002184 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002185
2186 surface = weston_surface_create(ec);
2187 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002188 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002189 return NULL;
2190 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 view = weston_view_create(surface);
2192 if (surface == NULL) {
2193 weston_log("no memory\n");
2194 weston_surface_destroy(surface);
2195 return NULL;
2196 }
Alex Wu4539b082012-03-01 12:57:46 +08002197
Alex Wu21858432012-04-01 20:13:08 +08002198 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002199 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002200 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002201 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002202 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002203 pixman_region32_fini(&surface->input);
2204 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002205
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206 weston_view_configure(view, x, y, w, h);
2207
2208 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002209}
2210
2211/* Create black surface and append it to the associated fullscreen surface.
2212 * Handle size dismatch and positioning according to the method. */
2213static void
2214shell_configure_fullscreen(struct shell_surface *shsurf)
2215{
2216 struct weston_output *output = shsurf->fullscreen_output;
2217 struct weston_surface *surface = shsurf->surface;
2218 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002219 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002220 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002221
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002222 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2223 restore_output_mode(output);
2224
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 if (!shsurf->fullscreen.black_view)
2226 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002227 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002228 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002229 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002230 output->width,
2231 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002232
Jason Ekstranda7af7042013-10-12 22:38:11 -05002233 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2234 wl_list_insert(&shsurf->view->layer_link,
2235 &shsurf->fullscreen.black_view->layer_link);
2236 shsurf->fullscreen.black_view->surface->output = output;
2237 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002238
Jason Ekstranda7af7042013-10-12 22:38:11 -05002239 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002240 &surf_width, &surf_height);
2241
Alex Wu4539b082012-03-01 12:57:46 +08002242 switch (shsurf->fullscreen.type) {
2243 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002244 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002245 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002246 break;
2247 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002248 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002249 if (output->width == surf_width &&
2250 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002251 weston_view_set_position(shsurf->view,
2252 output->x - surf_x,
2253 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002254 break;
2255 }
2256
Alex Wu4539b082012-03-01 12:57:46 +08002257 matrix = &shsurf->fullscreen.transform.matrix;
2258 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002259
Scott Moreau1bad5db2012-08-18 01:04:05 -06002260 output_aspect = (float) output->width /
2261 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002262 /* XXX: Use surf_width and surf_height here? */
2263 surface_aspect = (float) surface->width /
2264 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002265 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002266 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002267 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002268 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002269 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002270 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002271
Alex Wu4539b082012-03-01 12:57:46 +08002272 weston_matrix_scale(matrix, scale, scale, 1);
2273 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002275 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002276 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2277 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002278 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002279
Alex Wu4539b082012-03-01 12:57:46 +08002280 break;
2281 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002282 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002283 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02002284 surf_width * surface->buffer_scale,
2285 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002286 shsurf->fullscreen.framerate};
2287
Hardening57388e42013-09-18 23:56:36 +02002288 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
2289 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002290 weston_view_set_position(shsurf->view,
2291 output->x - surf_x,
2292 output->y - surf_y);
2293 weston_view_configure(shsurf->fullscreen.black_view,
2294 output->x - surf_x,
2295 output->y - surf_y,
2296 output->width,
2297 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002298 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002299 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002300 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002301 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002302 }
Alex Wubd3354b2012-04-17 17:20:49 +08002303 }
Alex Wu4539b082012-03-01 12:57:46 +08002304 break;
2305 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002306 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002307 break;
2308 default:
2309 break;
2310 }
2311}
2312
2313/* make the fullscreen and black surface at the top */
2314static void
2315shell_stack_fullscreen(struct shell_surface *shsurf)
2316{
Alex Wubd3354b2012-04-17 17:20:49 +08002317 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002318 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002319
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 wl_list_remove(&shsurf->view->layer_link);
2321 wl_list_insert(&shell->fullscreen_layer.view_list,
2322 &shsurf->view->layer_link);
2323 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002324
Jason Ekstranda7af7042013-10-12 22:38:11 -05002325 if (!shsurf->fullscreen.black_view)
2326 shsurf->fullscreen.black_view =
2327 create_black_surface(shsurf->surface->compositor,
2328 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002329 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002330 output->width,
2331 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002332
Jason Ekstranda7af7042013-10-12 22:38:11 -05002333 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2334 wl_list_insert(&shsurf->view->layer_link,
2335 &shsurf->fullscreen.black_view->layer_link);
2336 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002337}
2338
2339static void
2340shell_map_fullscreen(struct shell_surface *shsurf)
2341{
Alex Wu4539b082012-03-01 12:57:46 +08002342 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002343 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002344}
2345
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002346static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002347set_fullscreen(struct shell_surface *shsurf,
2348 uint32_t method,
2349 uint32_t framerate,
2350 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002351{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002352 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002353
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002354 if (output)
2355 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002356 else if (es->output)
2357 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002358 else
2359 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002360
Alex Wu4539b082012-03-01 12:57:46 +08002361 shsurf->fullscreen_output = shsurf->output;
2362 shsurf->fullscreen.type = method;
2363 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002364 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002365
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002366 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002367 shsurf->output->width,
2368 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002369}
2370
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002371static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002372shell_surface_set_fullscreen(struct wl_client *client,
2373 struct wl_resource *resource,
2374 uint32_t method,
2375 uint32_t framerate,
2376 struct wl_resource *output_resource)
2377{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002378 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002379 struct weston_output *output;
2380
2381 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002382 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002383 else
2384 output = NULL;
2385
2386 set_fullscreen(shsurf, method, framerate, output);
2387}
2388
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002389static void
2390set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2391{
2392 /* XXX: using the same fields for transient type */
2393 shsurf->transient.x = x;
2394 shsurf->transient.y = y;
2395 shsurf->transient.flags = flags;
2396 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2397}
2398
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002399static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002400
2401static void
2402destroy_shell_seat(struct wl_listener *listener, void *data)
2403{
2404 struct shell_seat *shseat =
2405 container_of(listener,
2406 struct shell_seat, seat_destroy_listener);
2407 struct shell_surface *shsurf, *prev = NULL;
2408
2409 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002410 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002411 shseat->popup_grab.client = NULL;
2412
2413 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2414 shsurf->popup.shseat = NULL;
2415 if (prev) {
2416 wl_list_init(&prev->popup.grab_link);
2417 }
2418 prev = shsurf;
2419 }
2420 wl_list_init(&prev->popup.grab_link);
2421 }
2422
2423 wl_list_remove(&shseat->seat_destroy_listener.link);
2424 free(shseat);
2425}
2426
2427static struct shell_seat *
2428create_shell_seat(struct weston_seat *seat)
2429{
2430 struct shell_seat *shseat;
2431
2432 shseat = calloc(1, sizeof *shseat);
2433 if (!shseat) {
2434 weston_log("no memory to allocate shell seat\n");
2435 return NULL;
2436 }
2437
2438 shseat->seat = seat;
2439 wl_list_init(&shseat->popup_grab.surfaces_list);
2440
2441 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2442 wl_signal_add(&seat->destroy_signal,
2443 &shseat->seat_destroy_listener);
2444
2445 return shseat;
2446}
2447
2448static struct shell_seat *
2449get_shell_seat(struct weston_seat *seat)
2450{
2451 struct wl_listener *listener;
2452
2453 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2454 if (listener == NULL)
2455 return create_shell_seat(seat);
2456
2457 return container_of(listener,
2458 struct shell_seat, seat_destroy_listener);
2459}
2460
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002461static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002462popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002463{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002464 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002465 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002466 struct shell_seat *shseat =
2467 container_of(grab, struct shell_seat, popup_grab.grab);
2468 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002469 wl_fixed_t sx, sy;
2470
Jason Ekstranda7af7042013-10-12 22:38:11 -05002471 view = weston_compositor_pick_view(pointer->seat->compositor,
2472 pointer->x, pointer->y,
2473 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002474
Jason Ekstranda7af7042013-10-12 22:38:11 -05002475 if (view && view->surface->resource &&
2476 wl_resource_get_client(view->surface->resource) == client) {
2477 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002478 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002479 weston_pointer_set_focus(pointer, NULL,
2480 wl_fixed_from_int(0),
2481 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002482 }
2483}
2484
2485static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002486popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2487 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002488{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002489 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002490 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002491 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002492
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002493 weston_pointer_move(pointer, x, y);
2494
Neil Roberts96d790e2013-09-19 17:32:00 +01002495 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002496 weston_view_from_global_fixed(pointer->focus,
2497 pointer->x, pointer->y,
2498 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002499 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002500 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002501}
2502
2503static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002504popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002505 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002506{
2507 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002508 struct shell_seat *shseat =
2509 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002510 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002511 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002512 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002513 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002514
Neil Roberts96d790e2013-09-19 17:32:00 +01002515 resource_list = &grab->pointer->focus_resource_list;
2516 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002517 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002518 wl_resource_for_each(resource, resource_list) {
2519 wl_pointer_send_button(resource, serial,
2520 time, button, state);
2521 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002522 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002523 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002524 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002525 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002526 }
2527
Daniel Stone4dbadb12012-05-30 16:31:51 +01002528 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002529 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002530}
2531
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002532static void
2533popup_grab_cancel(struct weston_pointer_grab *grab)
2534{
2535 popup_grab_end(grab->pointer);
2536}
2537
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002538static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002539 popup_grab_focus,
2540 popup_grab_motion,
2541 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002542 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002543};
2544
2545static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002546popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002547{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002548 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002549 struct shell_seat *shseat =
2550 container_of(grab, struct shell_seat, popup_grab.grab);
2551 struct shell_surface *shsurf;
2552 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002553
2554 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002555 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002556 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002557 shseat->popup_grab.grab.interface = NULL;
2558 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002559 /* Send the popup_done event to all the popups open */
2560 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002561 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002562 shsurf->popup.shseat = NULL;
2563 if (prev) {
2564 wl_list_init(&prev->popup.grab_link);
2565 }
2566 prev = shsurf;
2567 }
2568 wl_list_init(&prev->popup.grab_link);
2569 wl_list_init(&shseat->popup_grab.surfaces_list);
2570 }
2571}
2572
2573static void
2574add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2575{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002576 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002577
2578 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002579 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002580 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002581 /* We must make sure here that this popup was opened after
2582 * a mouse press, and not just by moving around with other
2583 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002584 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002585 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002586
Rob Bradforddfe31052013-06-26 19:49:11 +01002587 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002588 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002589 } else {
2590 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002591 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002592}
2593
2594static void
2595remove_popup_grab(struct shell_surface *shsurf)
2596{
2597 struct shell_seat *shseat = shsurf->popup.shseat;
2598
2599 wl_list_remove(&shsurf->popup.grab_link);
2600 wl_list_init(&shsurf->popup.grab_link);
2601 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002602 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002603 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002604 }
2605}
2606
2607static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002608shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002609{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002610 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002611 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002612
Jason Ekstranda7af7042013-10-12 22:38:11 -05002613 shsurf->surface->output = parent_view->output;
2614 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002615
Jason Ekstranda7af7042013-10-12 22:38:11 -05002616 weston_view_set_transform_parent(shsurf->view, parent_view);
2617 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2618 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002619
Kristian Høgsberge3148752013-05-06 23:19:49 -04002620 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002621 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002622 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002623 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002624 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002625 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002626}
2627
2628static void
2629shell_surface_set_popup(struct wl_client *client,
2630 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002631 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002632 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002633 struct wl_resource *parent_resource,
2634 int32_t x, int32_t y, uint32_t flags)
2635{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002636 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002637
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002638 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002639 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002640 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002641 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002642 shsurf->popup.x = x;
2643 shsurf->popup.y = y;
2644}
2645
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002646static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002647 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002648 shell_surface_move,
2649 shell_surface_resize,
2650 shell_surface_set_toplevel,
2651 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002652 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002653 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002654 shell_surface_set_maximized,
2655 shell_surface_set_title,
2656 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002657};
2658
2659static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002660destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002661{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002662 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2663
Giulio Camuffo5085a752013-03-25 21:42:45 +01002664 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2665 remove_popup_grab(shsurf);
2666 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002667
Alex Wubd3354b2012-04-17 17:20:49 +08002668 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002669 shell_surface_is_top_fullscreen(shsurf))
2670 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002671
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672 if (shsurf->fullscreen.black_view)
2673 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002674
Alex Wubd3354b2012-04-17 17:20:49 +08002675 /* As destroy_resource() use wl_list_for_each_safe(),
2676 * we can always remove the listener.
2677 */
2678 wl_list_remove(&shsurf->surface_destroy_listener.link);
2679 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002680 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002681 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002682
Jason Ekstranda7af7042013-10-12 22:38:11 -05002683 weston_view_destroy(shsurf->view);
2684
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002685 wl_list_remove(&shsurf->link);
2686 free(shsurf);
2687}
2688
2689static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002690shell_destroy_shell_surface(struct wl_resource *resource)
2691{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002692 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002693
2694 destroy_shell_surface(shsurf);
2695}
2696
2697static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002698shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002699{
2700 struct shell_surface *shsurf = container_of(listener,
2701 struct shell_surface,
2702 surface_destroy_listener);
2703
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002704 if (shsurf->resource)
2705 wl_resource_destroy(shsurf->resource);
2706 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002707 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002708}
2709
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002710static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002711shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002712
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002713static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002714get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002715{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002716 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002717 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002718 else
2719 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002720}
2721
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002722static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002723create_shell_surface(void *shell, struct weston_surface *surface,
2724 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002725{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002726 struct shell_surface *shsurf;
2727
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002728 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002729 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002730 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002731 }
2732
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002733 shsurf = calloc(1, sizeof *shsurf);
2734 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002735 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002736 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002737 }
2738
Jason Ekstranda7af7042013-10-12 22:38:11 -05002739 shsurf->view = weston_view_create(surface);
2740 if (!shsurf->view) {
2741 weston_log("no memory to allocate shell surface\n");
2742 free(shsurf);
2743 return NULL;
2744 }
2745
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002746 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002747 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002748
Tiago Vignattibc052c92012-04-19 16:18:18 +03002749 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002750 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002751 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002752 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002753 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002754 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2755 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002757 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002758 wl_list_init(&shsurf->fullscreen.transform.link);
2759
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002760 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002761 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002762 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002763 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002764
2765 /* init link so its safe to always remove it in destroy_shell_surface */
2766 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002767 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002768
Pekka Paalanen460099f2012-01-20 16:48:25 +02002769 /* empty when not in use */
2770 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002771 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002772
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002773 wl_list_init(&shsurf->workspace_transform.link);
2774
Pekka Paalanen98262232011-12-01 10:42:22 +02002775 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002776 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002777
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002778 shsurf->client = client;
2779
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002780 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002781}
2782
Jason Ekstranda7af7042013-10-12 22:38:11 -05002783static struct weston_view *
2784get_primary_view(void *shell, struct shell_surface *shsurf)
2785{
2786 return shsurf->view;
2787}
2788
Tiago Vignattibc052c92012-04-19 16:18:18 +03002789static void
2790shell_get_shell_surface(struct wl_client *client,
2791 struct wl_resource *resource,
2792 uint32_t id,
2793 struct wl_resource *surface_resource)
2794{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002795 struct weston_surface *surface =
2796 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002797 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002798 struct shell_surface *shsurf;
2799
2800 if (get_shell_surface(surface)) {
2801 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002802 WL_DISPLAY_ERROR_INVALID_OBJECT,
2803 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002804 return;
2805 }
2806
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002807 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002808 if (!shsurf) {
2809 wl_resource_post_error(surface_resource,
2810 WL_DISPLAY_ERROR_INVALID_OBJECT,
2811 "surface->configure already set");
2812 return;
2813 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002814
Jason Ekstranda85118c2013-06-27 20:17:02 -05002815 shsurf->resource =
2816 wl_resource_create(client,
2817 &wl_shell_surface_interface, 1, id);
2818 wl_resource_set_implementation(shsurf->resource,
2819 &shell_surface_implementation,
2820 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002821}
2822
2823static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002824 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002825};
2826
Kristian Høgsberg07937562011-04-12 17:25:42 -04002827static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002828shell_fade(struct desktop_shell *shell, enum fade_type type);
2829
2830static int
2831screensaver_timeout(void *data)
2832{
2833 struct desktop_shell *shell = data;
2834
2835 shell_fade(shell, FADE_OUT);
2836
2837 return 1;
2838}
2839
2840static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002841handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002842{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002843 struct desktop_shell *shell =
2844 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002845
Pekka Paalanen18027e52011-12-02 16:31:49 +02002846 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002847
2848 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002849 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002850}
2851
2852static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002853launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002854{
2855 if (shell->screensaver.binding)
2856 return;
2857
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002858 if (!shell->screensaver.path) {
2859 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002860 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002861 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002862
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002863 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002864 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002865 return;
2866 }
2867
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002868 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002869 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002870 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002871 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002872}
2873
2874static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002875terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002876{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002877 if (shell->screensaver.process.pid == 0)
2878 return;
2879
2880 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002881}
2882
2883static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002884configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002885{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002886 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002887
Giulio Camuffo184df502013-02-21 11:29:21 +01002888 if (width == 0)
2889 return;
2890
Jason Ekstranda7af7042013-10-12 22:38:11 -05002891 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2892 if (v->output == ev->output && v != ev) {
2893 weston_view_unmap(v);
2894 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002895 }
2896 }
2897
Jason Ekstranda7af7042013-10-12 22:38:11 -05002898 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002899
Jason Ekstranda7af7042013-10-12 22:38:11 -05002900 if (wl_list_empty(&ev->layer_link)) {
2901 wl_list_insert(&layer->view_list, &ev->layer_link);
2902 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002903 }
2904}
2905
2906static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002907background_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 -04002908{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002909 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002910 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002911
Jason Ekstranda7af7042013-10-12 22:38:11 -05002912 view = container_of(es->views.next, struct weston_view, surface_link);
2913
2914 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002915}
2916
2917static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002918desktop_shell_set_background(struct wl_client *client,
2919 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002920 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002921 struct wl_resource *surface_resource)
2922{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002923 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002924 struct weston_surface *surface =
2925 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002926 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002927
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002928 if (surface->configure) {
2929 wl_resource_post_error(surface_resource,
2930 WL_DISPLAY_ERROR_INVALID_OBJECT,
2931 "surface role already assigned");
2932 return;
2933 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002934
Jason Ekstranda7af7042013-10-12 22:38:11 -05002935 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2936 weston_view_destroy(view);
2937 view = weston_view_create(surface);
2938
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002939 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002940 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002941 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002942 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002943 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002944 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002945 surface->output->width,
2946 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002947}
2948
2949static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002950panel_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 -04002951{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002952 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002953 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002954
Jason Ekstranda7af7042013-10-12 22:38:11 -05002955 view = container_of(es->views.next, struct weston_view, surface_link);
2956
2957 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002958}
2959
2960static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002961desktop_shell_set_panel(struct wl_client *client,
2962 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002963 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002964 struct wl_resource *surface_resource)
2965{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002966 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002967 struct weston_surface *surface =
2968 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002969 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002970
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002971 if (surface->configure) {
2972 wl_resource_post_error(surface_resource,
2973 WL_DISPLAY_ERROR_INVALID_OBJECT,
2974 "surface role already assigned");
2975 return;
2976 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002977
Jason Ekstranda7af7042013-10-12 22:38:11 -05002978 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2979 weston_view_destroy(view);
2980 view = weston_view_create(surface);
2981
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002982 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002983 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002984 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002985 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002986 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002987 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002988 surface->output->width,
2989 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002990}
2991
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002992static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002993lock_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 -04002994{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002995 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002996 struct weston_view *view;
2997
2998 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002999
Giulio Camuffo184df502013-02-21 11:29:21 +01003000 if (width == 0)
3001 return;
3002
Jason Ekstranda7af7042013-10-12 22:38:11 -05003003 surface->width = width;
3004 surface->height = height;
3005 view->geometry.width = width;
3006 view->geometry.height = height;
3007 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003008
3009 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003010 wl_list_insert(&shell->lock_layer.view_list,
3011 &view->layer_link);
3012 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003013 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003014 }
3015}
3016
3017static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003018handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003019{
Tiago Vignattibe143262012-04-16 17:31:41 +03003020 struct desktop_shell *shell =
3021 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003022
Martin Minarik6d118362012-06-07 18:01:59 +02003023 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003024 shell->lock_surface = NULL;
3025}
3026
3027static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003028desktop_shell_set_lock_surface(struct wl_client *client,
3029 struct wl_resource *resource,
3030 struct wl_resource *surface_resource)
3031{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003032 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003033 struct weston_surface *surface =
3034 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003035
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003036 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003037
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003038 if (!shell->locked)
3039 return;
3040
Pekka Paalanen98262232011-12-01 10:42:22 +02003041 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003042
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003043 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003044 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003045 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003046
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003047 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003048 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003049 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003050}
3051
3052static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003053resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003054{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003055 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003056
Pekka Paalanen77346a62011-11-30 16:26:35 +02003057 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003058
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003059 wl_list_remove(&shell->lock_layer.link);
3060 wl_list_insert(&shell->compositor->cursor_layer.link,
3061 &shell->fullscreen_layer.link);
3062 wl_list_insert(&shell->fullscreen_layer.link,
3063 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003064 if (shell->showing_input_panels) {
3065 wl_list_insert(&shell->panel_layer.link,
3066 &shell->input_panel_layer.link);
3067 wl_list_insert(&shell->input_panel_layer.link,
3068 &ws->layer.link);
3069 } else {
3070 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3071 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003072
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003073 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003074
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003075 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003076 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003077 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003078}
3079
3080static void
3081desktop_shell_unlock(struct wl_client *client,
3082 struct wl_resource *resource)
3083{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003084 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003085
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003086 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003087
3088 if (shell->locked)
3089 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003090}
3091
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003092static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003093desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003094 struct wl_resource *resource,
3095 struct wl_resource *surface_resource)
3096{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003097 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003098
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003099 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003100 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003101}
3102
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003103static void
3104desktop_shell_desktop_ready(struct wl_client *client,
3105 struct wl_resource *resource)
3106{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003107 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003108
3109 shell_fade_startup(shell);
3110}
3111
Kristian Høgsberg75840622011-09-06 13:48:16 -04003112static const struct desktop_shell_interface desktop_shell_implementation = {
3113 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003114 desktop_shell_set_panel,
3115 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003116 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003117 desktop_shell_set_grab_surface,
3118 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003119};
3120
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003121static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003122get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003123{
3124 struct shell_surface *shsurf;
3125
3126 shsurf = get_shell_surface(surface);
3127 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003128 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003129 return shsurf->type;
3130}
3131
Kristian Høgsberg75840622011-09-06 13:48:16 -04003132static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003133move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003134{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003135 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003136 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003137 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003138
Pekka Paalanen01388e22013-04-25 13:57:44 +03003139 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003140 if (surface == NULL)
3141 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003142
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003143 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003144 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3145 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003146 return;
3147
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003148 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003149}
3150
3151static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003152touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3153{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003154 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003155 struct weston_surface *surface;
3156 struct shell_surface *shsurf;
3157
3158 surface = weston_surface_get_main_surface(focus);
3159 if (surface == NULL)
3160 return;
3161
3162 shsurf = get_shell_surface(surface);
3163 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3164 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3165 return;
3166
3167 surface_touch_move(shsurf, (struct weston_seat *) seat);
3168}
3169
3170static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003171resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003172{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003173 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003174 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003175 uint32_t edges = 0;
3176 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003177 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003178
Pekka Paalanen01388e22013-04-25 13:57:44 +03003179 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003180 if (surface == NULL)
3181 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003182
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003183 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003184 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3185 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003186 return;
3187
Jason Ekstranda7af7042013-10-12 22:38:11 -05003188 weston_view_from_global(shsurf->view,
3189 wl_fixed_to_int(seat->pointer->grab_x),
3190 wl_fixed_to_int(seat->pointer->grab_y),
3191 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003192
Jason Ekstranda7af7042013-10-12 22:38:11 -05003193 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003194 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003195 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003196 edges |= 0;
3197 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003198 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003199
Jason Ekstranda7af7042013-10-12 22:38:11 -05003200 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003201 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003202 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003203 edges |= 0;
3204 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003205 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003206
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003207 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003208}
3209
3210static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003211surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003212 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003213{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003214 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003215 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003216 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003217 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003218
Pekka Paalanen01388e22013-04-25 13:57:44 +03003219 /* XXX: broken for windows containing sub-surfaces */
3220 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003221 if (surface == NULL)
3222 return;
3223
3224 shsurf = get_shell_surface(surface);
3225 if (!shsurf)
3226 return;
3227
Jason Ekstranda7af7042013-10-12 22:38:11 -05003228 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003229
Jason Ekstranda7af7042013-10-12 22:38:11 -05003230 if (shsurf->view->alpha > 1.0)
3231 shsurf->view->alpha = 1.0;
3232 if (shsurf->view->alpha < step)
3233 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003234
Jason Ekstranda7af7042013-10-12 22:38:11 -05003235 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003236 weston_surface_damage(surface);
3237}
3238
3239static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003240do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003241 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003242{
Daniel Stone37816df2012-05-16 18:45:18 +01003243 struct weston_seat *ws = (struct weston_seat *) seat;
3244 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003245 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003246 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003247
3248 wl_list_for_each(output, &compositor->output_list, link) {
3249 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003250 wl_fixed_to_double(seat->pointer->x),
3251 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003252 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003253 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003254 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003255 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003256 increment = -output->zoom.increment;
3257 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003258 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003259 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003260 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003261 else
3262 increment = 0;
3263
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003264 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003265
Scott Moreaue6603982012-06-11 13:07:51 -06003266 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003267 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003268 else if (output->zoom.level > output->zoom.max_level)
3269 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003270 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003271 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003272 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003273
Scott Moreaue6603982012-06-11 13:07:51 -06003274 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003275
Jason Ekstranda7af7042013-10-12 22:38:11 -05003276 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003277 }
3278 }
3279}
3280
Scott Moreauccbf29d2012-02-22 14:21:41 -07003281static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003282zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003283 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003284{
3285 do_zoom(seat, time, 0, axis, value);
3286}
3287
3288static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003289zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003290 void *data)
3291{
3292 do_zoom(seat, time, key, 0, 0);
3293}
3294
3295static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003296terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003297 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003298{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003299 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003300
Daniel Stone325fc2d2012-05-30 16:31:58 +01003301 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003302}
3303
3304static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003305rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3306 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003307{
3308 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003309 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003310 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003311 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003312 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003313
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003314 weston_pointer_move(pointer, x, y);
3315
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003316 if (!shsurf)
3317 return;
3318
Jason Ekstranda7af7042013-10-12 22:38:11 -05003319 cx = 0.5f * shsurf->view->geometry.width;
3320 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003321
Daniel Stone37816df2012-05-16 18:45:18 +01003322 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3323 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003324 r = sqrtf(dx * dx + dy * dy);
3325
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003326 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003327 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003328
3329 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003330 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003331 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003332
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003333 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003334 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003335
3336 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003337 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003338 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003339 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003340 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003341
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003342 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003343 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003344 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003345 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003346 wl_list_init(&shsurf->rotation.transform.link);
3347 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003348 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003349 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003350
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003351 /* We need to adjust the position of the surface
3352 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003353 cposx = shsurf->view->geometry.x + cx;
3354 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003355 dposx = rotate->center.x - cposx;
3356 dposy = rotate->center.y - cposy;
3357 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003358 weston_view_set_position(shsurf->view,
3359 shsurf->view->geometry.x + dposx,
3360 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003361 }
3362
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003363 /* Repaint implies weston_surface_update_transform(), which
3364 * lazily applies the damage due to rotation update.
3365 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003366 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003367}
3368
3369static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003370rotate_grab_button(struct weston_pointer_grab *grab,
3371 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003372{
3373 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003374 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003375 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003376 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003377 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003378
Daniel Stone4dbadb12012-05-30 16:31:51 +01003379 if (pointer->button_count == 0 &&
3380 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003381 if (shsurf)
3382 weston_matrix_multiply(&shsurf->rotation.rotation,
3383 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003384 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003385 free(rotate);
3386 }
3387}
3388
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003389static void
3390rotate_grab_cancel(struct weston_pointer_grab *grab)
3391{
3392 struct rotate_grab *rotate =
3393 container_of(grab, struct rotate_grab, base.grab);
3394
3395 shell_grab_end(&rotate->base);
3396 free(rotate);
3397}
3398
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003399static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003400 noop_grab_focus,
3401 rotate_grab_motion,
3402 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003403 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003404};
3405
3406static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003407surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003408{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003409 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003410 float dx, dy;
3411 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003412
Pekka Paalanen460099f2012-01-20 16:48:25 +02003413 rotate = malloc(sizeof *rotate);
3414 if (!rotate)
3415 return;
3416
Jason Ekstranda7af7042013-10-12 22:38:11 -05003417 weston_view_to_global_float(surface->view,
3418 surface->view->geometry.width * 0.5f,
3419 surface->view->geometry.height * 0.5f,
3420 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003421
Daniel Stone37816df2012-05-16 18:45:18 +01003422 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3423 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003424 r = sqrtf(dx * dx + dy * dy);
3425 if (r > 20.0f) {
3426 struct weston_matrix inverse;
3427
3428 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003429 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003430 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003431
3432 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003433 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003434 } else {
3435 weston_matrix_init(&surface->rotation.rotation);
3436 weston_matrix_init(&rotate->rotation);
3437 }
3438
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003439 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3440 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003441}
3442
3443static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003444rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003445 void *data)
3446{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003447 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003448 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003449 struct shell_surface *surface;
3450
Pekka Paalanen01388e22013-04-25 13:57:44 +03003451 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003452 if (base_surface == NULL)
3453 return;
3454
3455 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003456 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3457 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003458 return;
3459
3460 surface_rotate(surface, seat);
3461}
3462
3463static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003464lower_fullscreen_layer(struct desktop_shell *shell)
3465{
3466 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003467 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003468
3469 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003470 wl_list_for_each_reverse_safe(view, prev,
3471 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003472 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003473 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003474}
3475
3476static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003477activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003478 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003479{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003480 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003481 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003482 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003483 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003484 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003485
Pekka Paalanen01388e22013-04-25 13:57:44 +03003486 main_surface = weston_surface_get_main_surface(es);
3487
Daniel Stone37816df2012-05-16 18:45:18 +01003488 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003489
Jonas Ådahl8538b222012-08-29 22:13:03 +02003490 state = ensure_focus_state(shell, seat);
3491 if (state == NULL)
3492 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003493
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003494 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003495 state->keyboard_focus = es;
3496 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003497 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003498
Pekka Paalanen01388e22013-04-25 13:57:44 +03003499 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003500 case SHELL_SURFACE_FULLSCREEN:
3501 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003502 shell_stack_fullscreen(get_shell_surface(main_surface));
3503 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003504 return;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003505 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003506 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003507 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003508 main_view = get_default_view(main_surface);
3509 if (main_view)
3510 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003511 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003512 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003513
3514 if (shell->focus_animation_type != ANIMATION_NONE)
3515 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Kristian Høgsberg75840622011-09-06 13:48:16 -04003516}
3517
Alex Wu21858432012-04-01 20:13:08 +08003518/* no-op func for checking black surface */
3519static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003520black_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 +08003521{
3522}
3523
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003524static bool
Alex Wu21858432012-04-01 20:13:08 +08003525is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3526{
3527 if (es->configure == black_surface_configure) {
3528 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003529 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003530 return true;
3531 }
3532 return false;
3533}
3534
Kristian Høgsberg75840622011-09-06 13:48:16 -04003535static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003536activate_binding(struct weston_seat *seat,
3537 struct desktop_shell *shell,
3538 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003539{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003540 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003541
Alex Wu9c35e6b2012-03-05 14:13:13 +08003542 if (!focus)
3543 return;
3544
Pekka Paalanen01388e22013-04-25 13:57:44 +03003545 if (is_black_surface(focus, &main_surface))
3546 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003547
Pekka Paalanen01388e22013-04-25 13:57:44 +03003548 main_surface = weston_surface_get_main_surface(focus);
3549 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003550 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003551
Neil Robertsa28c6932013-10-03 16:43:04 +01003552 activate(shell, focus, seat);
3553}
3554
3555static void
3556click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3557 void *data)
3558{
3559 if (seat->pointer->grab != &seat->pointer->default_grab)
3560 return;
3561
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003562 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003563}
3564
3565static void
3566touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3567{
3568 if (seat->touch->grab != &seat->touch->default_grab)
3569 return;
3570
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003571 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003572}
3573
3574static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003575lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003576{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003577 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003578
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003579 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003580 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003581 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003582 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003583
3584 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003585
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003586 /* Hide all surfaces by removing the fullscreen, panel and
3587 * toplevel layers. This way nothing else can show or receive
3588 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003589
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003590 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003591 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003592 if (shell->showing_input_panels)
3593 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003594 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003595 wl_list_insert(&shell->compositor->cursor_layer.link,
3596 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003597
Pekka Paalanen77346a62011-11-30 16:26:35 +02003598 launch_screensaver(shell);
3599
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003600 /* TODO: disable bindings that should not work while locked. */
3601
3602 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003603}
3604
3605static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003606unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003607{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003608 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003609 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003610 return;
3611 }
3612
3613 /* If desktop-shell client has gone away, unlock immediately. */
3614 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003615 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003616 return;
3617 }
3618
3619 if (shell->prepare_event_sent)
3620 return;
3621
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003622 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003623 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003624}
3625
3626static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003627shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003628{
3629 struct desktop_shell *shell = data;
3630
3631 shell->fade.animation = NULL;
3632
3633 switch (shell->fade.type) {
3634 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003635 weston_surface_destroy(shell->fade.view->surface);
3636 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003637 break;
3638 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003639 lock(shell);
3640 break;
3641 }
3642}
3643
Jason Ekstranda7af7042013-10-12 22:38:11 -05003644static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003645shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003646{
3647 struct weston_compositor *compositor = shell->compositor;
3648 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003649 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003650
3651 surface = weston_surface_create(compositor);
3652 if (!surface)
3653 return NULL;
3654
Jason Ekstranda7af7042013-10-12 22:38:11 -05003655 view = weston_view_create(surface);
3656 if (!view) {
3657 weston_surface_destroy(surface);
3658 return NULL;
3659 }
3660
3661 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003662 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003663 wl_list_insert(&compositor->fade_layer.view_list,
3664 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003665 pixman_region32_init(&surface->input);
3666
Jason Ekstranda7af7042013-10-12 22:38:11 -05003667 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003668}
3669
3670static void
3671shell_fade(struct desktop_shell *shell, enum fade_type type)
3672{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003673 float tint;
3674
3675 switch (type) {
3676 case FADE_IN:
3677 tint = 0.0;
3678 break;
3679 case FADE_OUT:
3680 tint = 1.0;
3681 break;
3682 default:
3683 weston_log("shell: invalid fade type\n");
3684 return;
3685 }
3686
3687 shell->fade.type = type;
3688
Jason Ekstranda7af7042013-10-12 22:38:11 -05003689 if (shell->fade.view == NULL) {
3690 shell->fade.view = shell_fade_create_surface(shell);
3691 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003692 return;
3693
Jason Ekstranda7af7042013-10-12 22:38:11 -05003694 shell->fade.view->alpha = 1.0 - tint;
3695 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003696 }
3697
3698 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003699 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003700 else
3701 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003702 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003703 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003704 shell_fade_done, shell);
3705}
3706
3707static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003708do_shell_fade_startup(void *data)
3709{
3710 struct desktop_shell *shell = data;
3711
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003712 if (shell->startup_animation_type == ANIMATION_FADE)
3713 shell_fade(shell, FADE_IN);
3714 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003715 weston_surface_destroy(shell->fade.view->surface);
3716 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003717 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003718}
3719
3720static void
3721shell_fade_startup(struct desktop_shell *shell)
3722{
3723 struct wl_event_loop *loop;
3724
3725 if (!shell->fade.startup_timer)
3726 return;
3727
3728 wl_event_source_remove(shell->fade.startup_timer);
3729 shell->fade.startup_timer = NULL;
3730
3731 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3732 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3733}
3734
3735static int
3736fade_startup_timeout(void *data)
3737{
3738 struct desktop_shell *shell = data;
3739
3740 shell_fade_startup(shell);
3741 return 0;
3742}
3743
3744static void
3745shell_fade_init(struct desktop_shell *shell)
3746{
3747 /* Make compositor output all black, and wait for the desktop-shell
3748 * client to signal it is ready, then fade in. The timer triggers a
3749 * fade-in, in case the desktop-shell client takes too long.
3750 */
3751
3752 struct wl_event_loop *loop;
3753
Jason Ekstranda7af7042013-10-12 22:38:11 -05003754 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003755 weston_log("%s: warning: fade surface already exists\n",
3756 __func__);
3757 return;
3758 }
3759
Jason Ekstranda7af7042013-10-12 22:38:11 -05003760 shell->fade.view = shell_fade_create_surface(shell);
3761 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003762 return;
3763
Jason Ekstranda7af7042013-10-12 22:38:11 -05003764 weston_view_update_transform(shell->fade.view);
3765 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003766
3767 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3768 shell->fade.startup_timer =
3769 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3770 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3771}
3772
3773static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003774idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003775{
3776 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003777 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003778
3779 shell_fade(shell, FADE_OUT);
3780 /* lock() is called from shell_fade_done() */
3781}
3782
3783static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003784wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003785{
3786 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003787 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003788
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003789 unlock(shell);
3790}
3791
3792static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003793show_input_panels(struct wl_listener *listener, void *data)
3794{
3795 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003796 container_of(listener, struct desktop_shell,
3797 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003798 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003799
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003800 shell->text_input.surface = (struct weston_surface*)data;
3801
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003802 if (shell->showing_input_panels)
3803 return;
3804
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003805 shell->showing_input_panels = true;
3806
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003807 if (!shell->locked)
3808 wl_list_insert(&shell->panel_layer.link,
3809 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003810
Jason Ekstranda7af7042013-10-12 22:38:11 -05003811 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003812 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003813 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003814 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003815 wl_list_insert(&shell->input_panel_layer.view_list,
3816 &ipsurf->view->layer_link);
3817 weston_view_geometry_dirty(ipsurf->view);
3818 weston_view_update_transform(ipsurf->view);
3819 weston_surface_damage(ipsurf->surface);
3820 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
3821 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003822 }
3823}
3824
3825static void
3826hide_input_panels(struct wl_listener *listener, void *data)
3827{
3828 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003829 container_of(listener, struct desktop_shell,
3830 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003831 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003832
Jan Arne Petersen61381972013-01-31 15:52:21 +01003833 if (!shell->showing_input_panels)
3834 return;
3835
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003836 shell->showing_input_panels = false;
3837
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003838 if (!shell->locked)
3839 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003840
Jason Ekstranda7af7042013-10-12 22:38:11 -05003841 wl_list_for_each_safe(view, next,
3842 &shell->input_panel_layer.view_list, layer_link)
3843 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003844}
3845
3846static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003847update_input_panels(struct wl_listener *listener, void *data)
3848{
3849 struct desktop_shell *shell =
3850 container_of(listener, struct desktop_shell,
3851 update_input_panel_listener);
3852
3853 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3854}
3855
3856static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003857center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003858{
Giulio Camuffob8366642013-04-25 13:57:46 +03003859 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003860 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003861
Jason Ekstranda7af7042013-10-12 22:38:11 -05003862 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03003863
3864 x = output->x + (output->width - width) / 2 - surf_x / 2;
3865 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003866
Jason Ekstranda7af7042013-10-12 22:38:11 -05003867 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003868}
3869
3870static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003871weston_view_set_initial_position(struct weston_view *view,
3872 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003873{
3874 struct weston_compositor *compositor = shell->compositor;
3875 int ix = 0, iy = 0;
3876 int range_x, range_y;
3877 int dx, dy, x, y, panel_height;
3878 struct weston_output *output, *target_output = NULL;
3879 struct weston_seat *seat;
3880
3881 /* As a heuristic place the new window on the same output as the
3882 * pointer. Falling back to the output containing 0, 0.
3883 *
3884 * TODO: Do something clever for touch too?
3885 */
3886 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003887 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003888 ix = wl_fixed_to_int(seat->pointer->x);
3889 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003890 break;
3891 }
3892 }
3893
3894 wl_list_for_each(output, &compositor->output_list, link) {
3895 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3896 target_output = output;
3897 break;
3898 }
3899 }
3900
3901 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003902 weston_view_set_position(view, 10 + random() % 400,
3903 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003904 return;
3905 }
3906
3907 /* Valid range within output where the surface will still be onscreen.
3908 * If this is negative it means that the surface is bigger than
3909 * output.
3910 */
3911 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003912 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003913 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05003914 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003915
Rob Bradford4cb88c72012-08-13 15:18:44 +01003916 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003917 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003918 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003919 dx = 0;
3920
3921 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003922 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003923 else
3924 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003925
3926 x = target_output->x + dx;
3927 y = target_output->y + dy;
3928
Jason Ekstranda7af7042013-10-12 22:38:11 -05003929 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003930}
3931
3932static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003933map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003934 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003935{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003936 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003937 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003938 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003939 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003940 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003941 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003942
Jason Ekstranda7af7042013-10-12 22:38:11 -05003943 shsurf->view->geometry.width = width;
3944 shsurf->view->geometry.height = height;
3945 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003946
3947 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003948 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02003949 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003950 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003951 break;
Alex Wu4539b082012-03-01 12:57:46 +08003952 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003953 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003954 shell_map_fullscreen(shsurf);
3955 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003956 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003957 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003958 panel_height = get_output_panel_height(shell, shsurf->output);
3959 surface_subsurfaces_boundingbox(shsurf->surface,
3960 &surf_x, &surf_y, NULL, NULL);
3961 weston_view_set_position(shsurf->view,
3962 shsurf->output->x - surf_x,
3963 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003964 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003965 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003966 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003967 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003968 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003969 weston_view_set_position(shsurf->view,
3970 shsurf->view->geometry.x + sx,
3971 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003972 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003973 default:
3974 ;
3975 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003976
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003977 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003978 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003979 case SHELL_SURFACE_POPUP:
3980 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003981 /* TODO: Handle a parent with multiple views */
3982 parent = get_default_view(shsurf->parent);
3983 if (parent) {
3984 wl_list_remove(&shsurf->view->layer_link);
3985 wl_list_insert(parent->layer_link.prev,
3986 &shsurf->view->layer_link);
3987 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003988 break;
Alex Wu4539b082012-03-01 12:57:46 +08003989 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003990 case SHELL_SURFACE_NONE:
3991 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003992 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003993 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003994 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003995 wl_list_remove(&shsurf->view->layer_link);
3996 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003997 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003998 }
3999
Jason Ekstranda7af7042013-10-12 22:38:11 -05004000 if (shsurf->type != SHELL_SURFACE_NONE) {
4001 weston_view_update_transform(shsurf->view);
4002 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4003 shsurf->surface->output = shsurf->output;
4004 shsurf->view->output = shsurf->output;
4005 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004006 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004007
Jason Ekstranda7af7042013-10-12 22:38:11 -05004008 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004009 /* XXX: xwayland's using the same fields for transient type */
4010 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004011 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004012 if (shsurf->transient.flags ==
4013 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4014 break;
4015 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004016 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004017 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004018 if (!shell->locked) {
4019 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004020 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004021 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004022 break;
4023 default:
4024 break;
4025 }
4026
Jason Ekstranda7af7042013-10-12 22:38:11 -05004027 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004028 {
4029 switch (shell->win_animation_type) {
4030 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004031 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004032 break;
4033 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004034 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004035 break;
4036 default:
4037 break;
4038 }
4039 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004040}
4041
4042static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004043configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004044 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004045{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004046 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4047 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004048 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03004049 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004050
Pekka Paalanen77346a62011-11-30 16:26:35 +02004051 shsurf = get_shell_surface(surface);
4052 if (shsurf)
4053 surface_type = shsurf->type;
4054
Jason Ekstranda7af7042013-10-12 22:38:11 -05004055 /* TODO:
4056 * This should probably be changed to be more shell_surface
4057 * dependent
4058 */
4059 wl_list_for_each(view, &surface->views, surface_link)
4060 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004061
4062 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004063 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08004064 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004065 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004066 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004067 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004068 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004069 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4070 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004071 shsurf->view->geometry.x = shsurf->output->x - surf_x;
4072 shsurf->view->geometry.y = shsurf->output->y +
4073 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08004074 break;
Alex Wu4539b082012-03-01 12:57:46 +08004075 case SHELL_SURFACE_TOPLEVEL:
4076 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004077 default:
4078 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004079 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004080
Alex Wu4539b082012-03-01 12:57:46 +08004081 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004082 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004083 wl_list_for_each(view, &surface->views, surface_link)
4084 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004085
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004086 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004087 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004088 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004089}
4090
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004091static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004092shell_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 +03004093{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004094 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004095 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004096
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004097 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004098
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004099 if (!weston_surface_is_mapped(es) &&
4100 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004101 remove_popup_grab(shsurf);
4102 }
4103
Giulio Camuffo184df502013-02-21 11:29:21 +01004104 if (width == 0)
4105 return;
4106
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004107 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004108 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004109 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004110 type_changed = 1;
4111 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004112
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004113 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004114 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004115 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05004116 shsurf->view->geometry.width != width ||
4117 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004118 float from_x, from_y;
4119 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004120
Jason Ekstranda7af7042013-10-12 22:38:11 -05004121 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4122 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004123 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004124 shsurf->view->geometry.x + to_x - from_x,
4125 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004126 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004127 }
4128}
4129
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004130static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004131
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004132static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004133desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004134{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004135 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004136 struct desktop_shell *shell =
4137 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004138
4139 shell->child.process.pid = 0;
4140 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004141
4142 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4143 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004144 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004145 shell->child.deathstamp = time;
4146 shell->child.deathcount = 0;
4147 }
4148
4149 shell->child.deathcount++;
4150 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02004151 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004152 return;
4153 }
4154
Martin Minarik6d118362012-06-07 18:01:59 +02004155 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004156 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004157 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004158}
4159
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004160static void
4161launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004162{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004163 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05004164 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004165
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004166 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004167 &shell->child.process,
4168 shell_exe,
4169 desktop_shell_sigchld);
4170
4171 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004172 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004173}
4174
4175static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004176bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4177{
Tiago Vignattibe143262012-04-16 17:31:41 +03004178 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004179 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004180
Jason Ekstranda85118c2013-06-27 20:17:02 -05004181 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4182 if (resource)
4183 wl_resource_set_implementation(resource, &shell_implementation,
4184 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004185}
4186
Kristian Høgsberg75840622011-09-06 13:48:16 -04004187static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004188unbind_desktop_shell(struct wl_resource *resource)
4189{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004190 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004191
4192 if (shell->locked)
4193 resume_desktop(shell);
4194
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004195 shell->child.desktop_shell = NULL;
4196 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004197}
4198
4199static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004200bind_desktop_shell(struct wl_client *client,
4201 void *data, uint32_t version, uint32_t id)
4202{
Tiago Vignattibe143262012-04-16 17:31:41 +03004203 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004204 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004205
Jason Ekstranda85118c2013-06-27 20:17:02 -05004206 resource = wl_resource_create(client, &desktop_shell_interface,
4207 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004208
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004209 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004210 wl_resource_set_implementation(resource,
4211 &desktop_shell_implementation,
4212 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004213 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004214
4215 if (version < 2)
4216 shell_fade_startup(shell);
4217
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004218 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004219 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004220
4221 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4222 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004223 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004224}
4225
Pekka Paalanen6e168112011-11-24 11:34:05 +02004226static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004227screensaver_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 -04004228{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004229 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004230 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004231
Giulio Camuffo184df502013-02-21 11:29:21 +01004232 if (width == 0)
4233 return;
4234
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004235 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004236 if (!shell->locked)
4237 return;
4238
Jason Ekstranda7af7042013-10-12 22:38:11 -05004239 view = container_of(surface->views.next, struct weston_view, surface_link);
4240 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004241
Jason Ekstranda7af7042013-10-12 22:38:11 -05004242 if (wl_list_empty(&view->layer_link)) {
4243 wl_list_insert(shell->lock_layer.view_list.prev,
4244 &view->layer_link);
4245 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004246 wl_event_source_timer_update(shell->screensaver.timer,
4247 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004248 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004249 }
4250}
4251
4252static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004253screensaver_set_surface(struct wl_client *client,
4254 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004255 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004256 struct wl_resource *output_resource)
4257{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004258 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004259 struct weston_surface *surface =
4260 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004261 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004262 struct weston_view *view, *next;
4263
4264 /* Make sure we only have one view */
4265 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4266 weston_view_destroy(view);
4267 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004268
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004269 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004270 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004271 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004272}
4273
4274static const struct screensaver_interface screensaver_implementation = {
4275 screensaver_set_surface
4276};
4277
4278static void
4279unbind_screensaver(struct wl_resource *resource)
4280{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004281 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004282
Pekka Paalanen77346a62011-11-30 16:26:35 +02004283 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004284}
4285
4286static void
4287bind_screensaver(struct wl_client *client,
4288 void *data, uint32_t version, uint32_t id)
4289{
Tiago Vignattibe143262012-04-16 17:31:41 +03004290 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004291 struct wl_resource *resource;
4292
Jason Ekstranda85118c2013-06-27 20:17:02 -05004293 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004294
Pekka Paalanen77346a62011-11-30 16:26:35 +02004295 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004296 wl_resource_set_implementation(resource,
4297 &screensaver_implementation,
4298 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004299 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004300 return;
4301 }
4302
4303 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4304 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004305 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004306}
4307
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004308static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004309input_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 -04004310{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004311 struct input_panel_surface *ip_surface = surface->configure_private;
4312 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004313 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004314 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004315
Giulio Camuffo184df502013-02-21 11:29:21 +01004316 if (width == 0)
4317 return;
4318
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004319 if (!weston_surface_is_mapped(surface)) {
4320 if (!shell->showing_input_panels)
4321 return;
4322
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004323 show_surface = 1;
4324 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004325
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004326 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004327
4328 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004329 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4330 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004331 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004332 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4333 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004334 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004335
Jason Ekstranda7af7042013-10-12 22:38:11 -05004336 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004337
4338 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004339 wl_list_insert(&shell->input_panel_layer.view_list,
4340 &ip_surface->view->layer_link);
4341 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004342 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004343 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004344 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004345}
4346
4347static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004348destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004349{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004350 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4351
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004352 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004353 wl_list_remove(&input_panel_surface->link);
4354
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004355 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004356 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004357
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004358 free(input_panel_surface);
4359}
4360
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004361static struct input_panel_surface *
4362get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004363{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004364 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004365 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004366 } else {
4367 return NULL;
4368 }
4369}
4370
4371static void
4372input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4373{
4374 struct input_panel_surface *ipsurface = container_of(listener,
4375 struct input_panel_surface,
4376 surface_destroy_listener);
4377
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004378 if (ipsurface->resource) {
4379 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004380 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004381 destroy_input_panel_surface(ipsurface);
4382 }
4383}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004384
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004385static struct input_panel_surface *
4386create_input_panel_surface(struct desktop_shell *shell,
4387 struct weston_surface *surface)
4388{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004389 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004390
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004391 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4392 if (!input_panel_surface)
4393 return NULL;
4394
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004395 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004396 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004397
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004398 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004399
4400 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004401 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004402
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004403 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004404 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004405 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004406 &input_panel_surface->surface_destroy_listener);
4407
4408 wl_list_init(&input_panel_surface->link);
4409
4410 return input_panel_surface;
4411}
4412
4413static void
4414input_panel_surface_set_toplevel(struct wl_client *client,
4415 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004416 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004417 uint32_t position)
4418{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004419 struct input_panel_surface *input_panel_surface =
4420 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004421 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004422
4423 wl_list_insert(&shell->input_panel.surfaces,
4424 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004425
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004426 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004427 input_panel_surface->panel = 0;
4428}
4429
4430static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004431input_panel_surface_set_overlay_panel(struct wl_client *client,
4432 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004433{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004434 struct input_panel_surface *input_panel_surface =
4435 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004436 struct desktop_shell *shell = input_panel_surface->shell;
4437
4438 wl_list_insert(&shell->input_panel.surfaces,
4439 &input_panel_surface->link);
4440
4441 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004442}
4443
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004444static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004445 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004446 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004447};
4448
4449static void
4450destroy_input_panel_surface_resource(struct wl_resource *resource)
4451{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004452 struct input_panel_surface *ipsurf =
4453 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004454
4455 destroy_input_panel_surface(ipsurf);
4456}
4457
4458static void
4459input_panel_get_input_panel_surface(struct wl_client *client,
4460 struct wl_resource *resource,
4461 uint32_t id,
4462 struct wl_resource *surface_resource)
4463{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004464 struct weston_surface *surface =
4465 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004466 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004467 struct input_panel_surface *ipsurf;
4468
4469 if (get_input_panel_surface(surface)) {
4470 wl_resource_post_error(surface_resource,
4471 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004472 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004473 return;
4474 }
4475
4476 ipsurf = create_input_panel_surface(shell, surface);
4477 if (!ipsurf) {
4478 wl_resource_post_error(surface_resource,
4479 WL_DISPLAY_ERROR_INVALID_OBJECT,
4480 "surface->configure already set");
4481 return;
4482 }
4483
Jason Ekstranda85118c2013-06-27 20:17:02 -05004484 ipsurf->resource =
4485 wl_resource_create(client,
4486 &wl_input_panel_surface_interface, 1, id);
4487 wl_resource_set_implementation(ipsurf->resource,
4488 &input_panel_surface_implementation,
4489 ipsurf,
4490 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004491}
4492
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004493static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004494 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004495};
4496
4497static void
4498unbind_input_panel(struct wl_resource *resource)
4499{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004500 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004501
4502 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004503}
4504
4505static void
4506bind_input_panel(struct wl_client *client,
4507 void *data, uint32_t version, uint32_t id)
4508{
4509 struct desktop_shell *shell = data;
4510 struct wl_resource *resource;
4511
Jason Ekstranda85118c2013-06-27 20:17:02 -05004512 resource = wl_resource_create(client,
4513 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004514
4515 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004516 wl_resource_set_implementation(resource,
4517 &input_panel_implementation,
4518 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004519 shell->input_panel.binding = resource;
4520 return;
4521 }
4522
4523 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4524 "interface object already bound");
4525 wl_resource_destroy(resource);
4526}
4527
Kristian Høgsberg07045392012-02-19 18:52:44 -05004528struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004529 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004530 struct weston_surface *current;
4531 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004532 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004533};
4534
4535static void
4536switcher_next(struct switcher *switcher)
4537{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004538 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004539 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004540 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004541 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004542
Jason Ekstranda7af7042013-10-12 22:38:11 -05004543 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4544 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004545 case SHELL_SURFACE_TOPLEVEL:
4546 case SHELL_SURFACE_FULLSCREEN:
4547 case SHELL_SURFACE_MAXIMIZED:
4548 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004549 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004550 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004551 next = view->surface;
4552 prev = view->surface;
4553 view->alpha = 0.25;
4554 weston_view_geometry_dirty(view);
4555 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004556 break;
4557 default:
4558 break;
4559 }
Alex Wu1659daa2012-04-01 20:13:09 +08004560
Jason Ekstranda7af7042013-10-12 22:38:11 -05004561 if (is_black_surface(view->surface, NULL)) {
4562 view->alpha = 0.25;
4563 weston_view_geometry_dirty(view);
4564 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004565 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004566 }
4567
4568 if (next == NULL)
4569 next = first;
4570
Alex Wu07b26062012-03-12 16:06:01 +08004571 if (next == NULL)
4572 return;
4573
Kristian Høgsberg07045392012-02-19 18:52:44 -05004574 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004575 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004576
4577 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004578 wl_list_for_each(view, &next->views, surface_link)
4579 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004580
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004581 shsurf = get_shell_surface(switcher->current);
4582 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004583 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004584}
4585
4586static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004587switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004588{
4589 struct switcher *switcher =
4590 container_of(listener, struct switcher, listener);
4591
4592 switcher_next(switcher);
4593}
4594
4595static void
Daniel Stone351eb612012-05-31 15:27:47 -04004596switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004597{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004598 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004599 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004600 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004601
Jason Ekstranda7af7042013-10-12 22:38:11 -05004602 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004603 if (is_focus_view(view))
4604 continue;
4605
Jason Ekstranda7af7042013-10-12 22:38:11 -05004606 view->alpha = 1.0;
4607 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004608 }
4609
Alex Wu07b26062012-03-12 16:06:01 +08004610 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004611 activate(switcher->shell, switcher->current,
4612 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004613 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004614 weston_keyboard_end_grab(keyboard);
4615 if (keyboard->input_method_resource)
4616 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004617 free(switcher);
4618}
4619
4620static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004621switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004622 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004623{
4624 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004625 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004626
Daniel Stonec9785ea2012-05-30 16:31:52 +01004627 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004628 switcher_next(switcher);
4629}
4630
4631static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004632switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004633 uint32_t mods_depressed, uint32_t mods_latched,
4634 uint32_t mods_locked, uint32_t group)
4635{
4636 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004637 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004638
Daniel Stone351eb612012-05-31 15:27:47 -04004639 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4640 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004641}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004642
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004643static void
4644switcher_cancel(struct weston_keyboard_grab *grab)
4645{
4646 struct switcher *switcher = container_of(grab, struct switcher, grab);
4647
4648 switcher_destroy(switcher);
4649}
4650
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004651static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004652 switcher_key,
4653 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004654 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004655};
4656
4657static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004658switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004659 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004660{
Tiago Vignattibe143262012-04-16 17:31:41 +03004661 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004662 struct switcher *switcher;
4663
4664 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004665 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004666 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004667 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004668 wl_list_init(&switcher->listener.link);
4669
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004670 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004671 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004672 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004673 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4674 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004675 switcher_next(switcher);
4676}
4677
Pekka Paalanen3c647232011-12-22 13:43:43 +02004678static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004679backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004680 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004681{
4682 struct weston_compositor *compositor = data;
4683 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004684 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004685
4686 /* TODO: we're limiting to simple use cases, where we assume just
4687 * control on the primary display. We'd have to extend later if we
4688 * ever get support for setting backlights on random desktop LCD
4689 * panels though */
4690 output = get_default_output(compositor);
4691 if (!output)
4692 return;
4693
4694 if (!output->set_backlight)
4695 return;
4696
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004697 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4698 backlight_new = output->backlight_current - 25;
4699 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4700 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004701
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004702 if (backlight_new < 5)
4703 backlight_new = 5;
4704 if (backlight_new > 255)
4705 backlight_new = 255;
4706
4707 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004708 output->set_backlight(output, output->backlight_current);
4709}
4710
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004711struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004712 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004713 struct weston_seat *seat;
4714 uint32_t key[2];
4715 int key_released[2];
4716};
4717
4718static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004719debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004720 uint32_t key, uint32_t state)
4721{
4722 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01004723 struct weston_compositor *ec = db->seat->compositor;
4724 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004725 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004726 uint32_t serial;
4727 int send = 0, terminate = 0;
4728 int check_binding = 1;
4729 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01004730 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004731
4732 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4733 /* Do not run bindings on key releases */
4734 check_binding = 0;
4735
4736 for (i = 0; i < 2; i++)
4737 if (key == db->key[i])
4738 db->key_released[i] = 1;
4739
4740 if (db->key_released[0] && db->key_released[1]) {
4741 /* All key releases been swalled so end the grab */
4742 terminate = 1;
4743 } else if (key != db->key[0] && key != db->key[1]) {
4744 /* Should not swallow release of other keys */
4745 send = 1;
4746 }
4747 } else if (key == db->key[0] && !db->key_released[0]) {
4748 /* Do not check bindings for the first press of the binding
4749 * key. This allows it to be used as a debug shortcut.
4750 * We still need to swallow this event. */
4751 check_binding = 0;
4752 } else if (db->key[1]) {
4753 /* If we already ran a binding don't process another one since
4754 * we can't keep track of all the binding keys that were
4755 * pressed in order to swallow the release events. */
4756 send = 1;
4757 check_binding = 0;
4758 }
4759
4760 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004761 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4762 key, state)) {
4763 /* We ran a binding so swallow the press and keep the
4764 * grab to swallow the released too. */
4765 send = 0;
4766 terminate = 0;
4767 db->key[1] = key;
4768 } else {
4769 /* Terminate the grab since the key pressed is not a
4770 * debug binding key. */
4771 send = 1;
4772 terminate = 1;
4773 }
4774 }
4775
4776 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01004777 serial = wl_display_next_serial(display);
4778 resource_list = &grab->keyboard->focus_resource_list;
4779 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004780 wl_keyboard_send_key(resource, serial, time, key, state);
4781 }
4782 }
4783
4784 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004785 weston_keyboard_end_grab(grab->keyboard);
4786 if (grab->keyboard->input_method_resource)
4787 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004788 free(db);
4789 }
4790}
4791
4792static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004793debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004794 uint32_t mods_depressed, uint32_t mods_latched,
4795 uint32_t mods_locked, uint32_t group)
4796{
4797 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01004798 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004799
Neil Roberts96d790e2013-09-19 17:32:00 +01004800 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004801
Neil Roberts96d790e2013-09-19 17:32:00 +01004802 wl_resource_for_each(resource, resource_list) {
4803 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4804 mods_latched, mods_locked, group);
4805 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004806}
4807
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004808static void
4809debug_binding_cancel(struct weston_keyboard_grab *grab)
4810{
4811 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4812
4813 weston_keyboard_end_grab(grab->keyboard);
4814 free(db);
4815}
4816
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004817struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004818 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004819 debug_binding_modifiers,
4820 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004821};
4822
4823static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004824debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004825{
4826 struct debug_binding_grab *grab;
4827
4828 grab = calloc(1, sizeof *grab);
4829 if (!grab)
4830 return;
4831
4832 grab->seat = (struct weston_seat *) seat;
4833 grab->key[0] = key;
4834 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004835 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004836}
4837
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004838static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004839force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004840 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004841{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004842 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004843 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004844 struct desktop_shell *shell = data;
4845 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004846 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004847
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004848 focus_surface = seat->keyboard->focus;
4849 if (!focus_surface)
4850 return;
4851
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004852 wl_signal_emit(&compositor->kill_signal, focus_surface);
4853
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004854 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004855 wl_client_get_credentials(client, &pid, NULL, NULL);
4856
4857 /* Skip clients that we launched ourselves (the credentials of
4858 * the socketpair is ours) */
4859 if (pid == getpid())
4860 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004861
Daniel Stone325fc2d2012-05-30 16:31:58 +01004862 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004863}
4864
4865static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004866workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004867 uint32_t key, void *data)
4868{
4869 struct desktop_shell *shell = data;
4870 unsigned int new_index = shell->workspaces.current;
4871
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004872 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004873 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004874 if (new_index != 0)
4875 new_index--;
4876
4877 change_workspace(shell, new_index);
4878}
4879
4880static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004881workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004882 uint32_t key, void *data)
4883{
4884 struct desktop_shell *shell = data;
4885 unsigned int new_index = shell->workspaces.current;
4886
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004887 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004888 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004889 if (new_index < shell->workspaces.num - 1)
4890 new_index++;
4891
4892 change_workspace(shell, new_index);
4893}
4894
4895static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004896workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004897 uint32_t key, void *data)
4898{
4899 struct desktop_shell *shell = data;
4900 unsigned int new_index;
4901
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004902 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004903 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004904 new_index = key - KEY_F1;
4905 if (new_index >= shell->workspaces.num)
4906 new_index = shell->workspaces.num - 1;
4907
4908 change_workspace(shell, new_index);
4909}
4910
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004911static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004912workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004913 uint32_t key, void *data)
4914{
4915 struct desktop_shell *shell = data;
4916 unsigned int new_index = shell->workspaces.current;
4917
4918 if (shell->locked)
4919 return;
4920
4921 if (new_index != 0)
4922 new_index--;
4923
4924 take_surface_to_workspace_by_seat(shell, seat, new_index);
4925}
4926
4927static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004928workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004929 uint32_t key, void *data)
4930{
4931 struct desktop_shell *shell = data;
4932 unsigned int new_index = shell->workspaces.current;
4933
4934 if (shell->locked)
4935 return;
4936
4937 if (new_index < shell->workspaces.num - 1)
4938 new_index++;
4939
4940 take_surface_to_workspace_by_seat(shell, seat, new_index);
4941}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004942
4943static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004944handle_output_destroy(struct wl_listener *listener, void *data)
4945{
4946 struct shell_output *output_listener =
4947 container_of(listener, struct shell_output, destroy_listener);
4948
4949 wl_list_remove(&output_listener->destroy_listener.link);
4950 wl_list_remove(&output_listener->link);
4951 free(output_listener);
4952}
4953
4954static void
4955create_shell_output(struct desktop_shell *shell,
4956 struct weston_output *output)
4957{
4958 struct shell_output *shell_output;
4959
4960 shell_output = zalloc(sizeof *shell_output);
4961 if (shell_output == NULL)
4962 return;
4963
4964 shell_output->output = output;
4965 shell_output->shell = shell;
4966 shell_output->destroy_listener.notify = handle_output_destroy;
4967 wl_signal_add(&output->destroy_signal,
4968 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004969 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004970}
4971
4972static void
4973handle_output_create(struct wl_listener *listener, void *data)
4974{
4975 struct desktop_shell *shell =
4976 container_of(listener, struct desktop_shell, output_create_listener);
4977 struct weston_output *output = (struct weston_output *)data;
4978
4979 create_shell_output(shell, output);
4980}
4981
4982static void
4983setup_output_destroy_handler(struct weston_compositor *ec,
4984 struct desktop_shell *shell)
4985{
4986 struct weston_output *output;
4987
4988 wl_list_init(&shell->output_list);
4989 wl_list_for_each(output, &ec->output_list, link)
4990 create_shell_output(shell, output);
4991
4992 shell->output_create_listener.notify = handle_output_create;
4993 wl_signal_add(&ec->output_created_signal,
4994 &shell->output_create_listener);
4995}
4996
4997static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004998shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004999{
Tiago Vignattibe143262012-04-16 17:31:41 +03005000 struct desktop_shell *shell =
5001 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005002 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005003 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005004
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005005 if (shell->child.client)
5006 wl_client_destroy(shell->child.client);
5007
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005008 wl_list_remove(&shell->idle_listener.link);
5009 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005010 wl_list_remove(&shell->show_input_panel_listener.link);
5011 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005012
Xiong Zhang6b481422013-10-23 13:58:32 +08005013 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5014 wl_list_remove(&shell_output->destroy_listener.link);
5015 wl_list_remove(&shell_output->link);
5016 free(shell_output);
5017 }
5018
5019 wl_list_remove(&shell->output_create_listener.link);
5020
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005021 wl_array_for_each(ws, &shell->workspaces.array)
5022 workspace_destroy(*ws);
5023 wl_array_release(&shell->workspaces.array);
5024
Pekka Paalanen3c647232011-12-22 13:43:43 +02005025 free(shell->screensaver.path);
5026 free(shell);
5027}
5028
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005029static void
5030shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5031{
5032 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005033 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005034
5035 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005036 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5037 MODIFIER_CTRL | MODIFIER_ALT,
5038 terminate_binding, ec);
5039 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5040 click_to_activate_binding,
5041 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005042 weston_compositor_add_touch_binding(ec, 0,
5043 touch_to_activate_binding,
5044 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005045 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5046 MODIFIER_SUPER | MODIFIER_ALT,
5047 surface_opacity_binding, NULL);
5048 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5049 MODIFIER_SUPER, zoom_axis_binding,
5050 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005051
5052 /* configurable bindings */
5053 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005054 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5055 zoom_key_binding, NULL);
5056 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5057 zoom_key_binding, NULL);
5058 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5059 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005060 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005061 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5062 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005063
5064 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5065 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5066 rotate_binding, NULL);
5067
Daniel Stone325fc2d2012-05-30 16:31:58 +01005068 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5069 shell);
5070 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5071 ec);
5072 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5073 backlight_binding, ec);
5074 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5075 ec);
5076 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5077 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005078 weston_compositor_add_key_binding(ec, KEY_K, mod,
5079 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005080 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5081 workspace_up_binding, shell);
5082 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5083 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005084 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5085 workspace_move_surface_up_binding,
5086 shell);
5087 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5088 workspace_move_surface_down_binding,
5089 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005090
5091 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5092 if (shell->workspaces.num > 1) {
5093 num_workspace_bindings = shell->workspaces.num;
5094 if (num_workspace_bindings > 6)
5095 num_workspace_bindings = 6;
5096 for (i = 0; i < num_workspace_bindings; i++)
5097 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5098 workspace_f_binding,
5099 shell);
5100 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005101
5102 /* Debug bindings */
5103 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5104 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005105}
5106
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005107WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005108module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005109 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005110{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005111 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005112 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005113 struct workspace **pws;
5114 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005115 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005116
Peter Huttererf3d62272013-08-08 11:57:05 +10005117 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005118 if (shell == NULL)
5119 return -1;
5120
Kristian Høgsberg75840622011-09-06 13:48:16 -04005121 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005122
5123 shell->destroy_listener.notify = shell_destroy;
5124 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005125 shell->idle_listener.notify = idle_handler;
5126 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5127 shell->wake_listener.notify = wake_handler;
5128 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005129 shell->show_input_panel_listener.notify = show_input_panels;
5130 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
5131 shell->hide_input_panel_listener.notify = hide_input_panels;
5132 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005133 shell->update_input_panel_listener.notify = update_input_panels;
5134 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06005135 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005136 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005137 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005138 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005139 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005140 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005141 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005142 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005143 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005144 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005145 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005146
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005147 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005148
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005149 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5150 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005151 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5152 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005153 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005154
5155 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005156 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005157
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005158 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005159
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005160 for (i = 0; i < shell->workspaces.num; i++) {
5161 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5162 if (pws == NULL)
5163 return -1;
5164
5165 *pws = workspace_create();
5166 if (*pws == NULL)
5167 return -1;
5168 }
5169 activate_workspace(shell, 0);
5170
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005171 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005172 wl_list_init(&shell->workspaces.animation.link);
5173 shell->workspaces.animation.frame = animate_workspace_change_frame;
5174
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005175 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005176 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005177 return -1;
5178
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005179 if (wl_global_create(ec->wl_display,
5180 &desktop_shell_interface, 2,
5181 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005182 return -1;
5183
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005184 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5185 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005186 return -1;
5187
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005188 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005189 shell, bind_input_panel) == NULL)
5190 return -1;
5191
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005192 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5193 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005194 return -1;
5195
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005196 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005197
Xiong Zhang6b481422013-10-23 13:58:32 +08005198 setup_output_destroy_handler(ec, shell);
5199
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005200 loop = wl_display_get_event_loop(ec->wl_display);
5201 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005202
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005203 shell->screensaver.timer =
5204 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5205
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005206 wl_list_for_each(seat, &ec->seat_list, link)
5207 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005208
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005209 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005210
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005211 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005212
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005213 return 0;
5214}