blob: 2a8c04ccefdb45aa44e8f9f6a3219fb277472f57 [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.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020029#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050030#include <string.h>
31#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040032#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020033#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020034#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020035#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040036#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010040#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020041#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020042#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Juan Zhaoe10d2792012-04-25 19:09:52 +080047enum animation_type {
48 ANIMATION_NONE,
49
50 ANIMATION_ZOOM,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010051 ANIMATION_FADE,
52 ANIMATION_DIM_LAYER,
Juan Zhaoe10d2792012-04-25 19:09:52 +080053};
54
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020055enum fade_type {
56 FADE_IN,
57 FADE_OUT
58};
59
Daniel Stonedf8133b2013-11-19 11:37:14 +010060enum exposay_target_state {
61 EXPOSAY_TARGET_OVERVIEW, /* show all windows */
62 EXPOSAY_TARGET_CANCEL, /* return to normal, same focus */
63 EXPOSAY_TARGET_SWITCH, /* return to normal, switch focus */
64};
65
66enum exposay_layout_state {
67 EXPOSAY_LAYOUT_INACTIVE = 0, /* normal desktop */
68 EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE, /* in transition to normal */
69 EXPOSAY_LAYOUT_OVERVIEW, /* show all windows */
70 EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW, /* in transition to all windows */
71};
72
Jonas Ådahl04769742012-06-13 00:01:24 +020073struct focus_state {
74 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040075 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020076 struct weston_surface *keyboard_focus;
77 struct wl_list link;
78 struct wl_listener seat_destroy_listener;
79 struct wl_listener surface_destroy_listener;
80};
81
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010082struct focus_surface {
83 struct weston_surface *surface;
84 struct weston_view *view;
85 struct weston_transform workspace_transform;
86};
87
Jonas Ådahle3cddce2012-06-13 00:01:22 +020088struct workspace {
89 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020090
91 struct wl_list focus_list;
92 struct wl_listener seat_destroyed_listener;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010093
94 struct focus_surface *fsurf_front;
95 struct focus_surface *fsurf_back;
96 struct weston_view_animation *focus_animation;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020097};
98
Philipp Brüschweiler88013572012-08-06 13:44:42 +020099struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -0500100 struct wl_resource *resource;
101 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100102
103 struct desktop_shell *shell;
104
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200105 struct wl_list link;
106 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500107 struct weston_view *view;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100108 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200109
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +0200110 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200111 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200112};
113
Xiong Zhang6b481422013-10-23 13:58:32 +0800114struct shell_output {
115 struct desktop_shell *shell;
116 struct weston_output *output;
117 struct wl_listener destroy_listener;
118 struct wl_list link;
119};
120
Tiago Vignattibe143262012-04-16 17:31:41 +0300121struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500122 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400123
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200124 struct wl_listener idle_listener;
125 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400126 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200127 struct wl_listener show_input_panel_listener;
128 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200129 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200130
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500131 struct weston_layer fullscreen_layer;
132 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500133 struct weston_layer background_layer;
134 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200135 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500136
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400137 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300138 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400139
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200140 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500141 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200142 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200143 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200144
145 unsigned deathcount;
146 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200147 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200148
149 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200150 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200151 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200152
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200153 struct {
154 struct weston_surface *surface;
155 pixman_box32_t cursor_rectangle;
156 } text_input;
157
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400158 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500159 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160
Pekka Paalanen77346a62011-11-30 16:26:35 +0200161 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200162 struct wl_array array;
163 unsigned int current;
164 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200165
Jonas Ådahle9d22502012-08-29 22:13:01 +0200166 struct wl_list client_list;
167
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200168 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200169 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200170 int anim_dir;
171 uint32_t anim_timestamp;
172 double anim_current;
173 struct workspace *anim_from;
174 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200175 } workspaces;
176
177 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200178 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200179 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200180 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500181 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200182 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200183 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500184
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200185 struct {
186 struct wl_resource *binding;
187 struct wl_list surfaces;
188 } input_panel;
189
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200190 struct {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500191 struct weston_view *view;
192 struct weston_view_animation *animation;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200193 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300194 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200195 } fade;
196
Daniel Stonedf8133b2013-11-19 11:37:14 +0100197 struct exposay {
198 /* XXX: Make these exposay_surfaces. */
199 struct weston_view *focus_prev;
200 struct weston_view *focus_current;
201 struct weston_view *clicked;
202 struct workspace *workspace;
203 struct weston_seat *seat;
204 struct wl_list surface_list;
205
206 struct weston_keyboard_grab grab_kbd;
207 struct weston_pointer_grab grab_ptr;
208
209 enum exposay_target_state state_target;
210 enum exposay_layout_state state_cur;
211 int in_flight; /* number of animations still running */
212
213 int num_surfaces;
214 int grid_size;
215 int surface_size;
216
217 int hpadding_outer;
218 int vpadding_outer;
219 int padding_inner;
220
221 int row_current;
222 int column_current;
223 } exposay;
224
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300225 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800226 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700227 enum animation_type startup_animation_type;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100228 enum animation_type focus_animation_type;
Xiong Zhang6b481422013-10-23 13:58:32 +0800229
230 struct wl_listener output_create_listener;
231 struct wl_list output_list;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400232};
233
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500234enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200235 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500236 SHELL_SURFACE_TOPLEVEL,
237 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500238 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800239 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300240 SHELL_SURFACE_POPUP,
241 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200242};
243
Scott Moreauff1db4a2012-04-17 19:06:18 -0600244struct ping_timer {
245 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600246 uint32_t serial;
247};
248
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200249struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500250 struct wl_resource *resource;
251 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200252
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500253 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500254 struct weston_view *view;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200255 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400256 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300257 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200258
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400259 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400260 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500261 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800262 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800263 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700264 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100265
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500266 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200267 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500268 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200269 } rotation;
270
271 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100272 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500273 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100274 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400275 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500276 } popup;
277
Alex Wu4539b082012-03-01 12:57:46 +0800278 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300279 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400280 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300281 } transient;
282
283 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800284 enum wl_shell_surface_fullscreen_method type;
285 struct weston_transform transform; /* matrix from x, y */
286 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500287 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800288 } fullscreen;
289
Scott Moreauff1db4a2012-04-17 19:06:18 -0600290 struct ping_timer *ping_timer;
291
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200292 struct weston_transform workspace_transform;
293
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500294 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500295 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100296 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400297
298 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200299};
300
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300301struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400302 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300303 struct shell_surface *shsurf;
304 struct wl_listener shsurf_destroy_listener;
305};
306
Rusty Lynch1084da52013-08-15 09:10:08 -0700307struct shell_touch_grab {
308 struct weston_touch_grab grab;
309 struct shell_surface *shsurf;
310 struct wl_listener shsurf_destroy_listener;
311 struct weston_touch *touch;
312};
313
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300314struct weston_move_grab {
315 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100316 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500317};
318
Rusty Lynch1084da52013-08-15 09:10:08 -0700319struct weston_touch_move_grab {
320 struct shell_touch_grab base;
321 wl_fixed_t dx, dy;
322};
323
Pekka Paalanen460099f2012-01-20 16:48:25 +0200324struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300325 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500326 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200327 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200328 float x;
329 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200330 } center;
331};
332
Giulio Camuffo5085a752013-03-25 21:42:45 +0100333struct shell_seat {
334 struct weston_seat *seat;
335 struct wl_listener seat_destroy_listener;
336
337 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400338 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100339 struct wl_list surfaces_list;
340 struct wl_client *client;
341 int32_t initial_up;
342 } popup_grab;
343};
344
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400345static void
346activate(struct desktop_shell *shell, struct weston_surface *es,
347 struct weston_seat *seat);
348
349static struct workspace *
350get_current_workspace(struct desktop_shell *shell);
351
Alex Wubd3354b2012-04-17 17:20:49 +0800352static struct shell_surface *
353get_shell_surface(struct weston_surface *surface);
354
355static struct desktop_shell *
356shell_surface_get_shell(struct shell_surface *shsurf);
357
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500358static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400359surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500360
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300361static void
362shell_fade_startup(struct desktop_shell *shell);
363
Alex Wubd3354b2012-04-17 17:20:49 +0800364static bool
365shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
366{
367 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800369
370 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100371
Jason Ekstranda7af7042013-10-12 22:38:11 -0500372 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800373 return false;
374
Jason Ekstranda7af7042013-10-12 22:38:11 -0500375 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
376 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800377 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500378 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800379}
380
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500381static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400382destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300383{
384 struct shell_grab *grab;
385
386 grab = container_of(listener, struct shell_grab,
387 shsurf_destroy_listener);
388
389 grab->shsurf = NULL;
390}
391
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392static struct weston_view *
393get_default_view(struct weston_surface *surface)
394{
395 struct shell_surface *shsurf;
396 struct weston_view *view;
397
398 if (!surface || wl_list_empty(&surface->views))
399 return NULL;
400
401 shsurf = get_shell_surface(surface);
402 if (shsurf)
403 return shsurf->view;
404
405 wl_list_for_each(view, &surface->views, surface_link)
406 if (weston_view_is_mapped(view))
407 return view;
408
409 return container_of(surface->views.next, struct weston_view, surface_link);
410}
411
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300412static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400413popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400414
415static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300416shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400417 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300418 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400419 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300420 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300421{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300422 struct desktop_shell *shell = shsurf->shell;
423
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400424 popup_grab_end(pointer);
425
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300426 grab->grab.interface = interface;
427 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400428 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500429 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400430 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300431
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700432 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400433 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400434 if (shell->child.desktop_shell) {
435 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
436 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500437 weston_pointer_set_focus(pointer,
438 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400439 wl_fixed_from_int(0),
440 wl_fixed_from_int(0));
441 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300442}
443
444static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300445shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300446{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700447 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400448 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700449 grab->shsurf->grabbed = 0;
450 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300451
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700452 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300453}
454
455static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700456shell_touch_grab_start(struct shell_touch_grab *grab,
457 const struct weston_touch_grab_interface *interface,
458 struct shell_surface *shsurf,
459 struct weston_touch *touch)
460{
461 struct desktop_shell *shell = shsurf->shell;
462
463 grab->grab.interface = interface;
464 grab->shsurf = shsurf;
465 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
466 wl_signal_add(&shsurf->destroy_signal,
467 &grab->shsurf_destroy_listener);
468
469 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700470 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700471
472 weston_touch_start_grab(touch, &grab->grab);
473 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500474 weston_touch_set_focus(touch->seat,
475 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700476}
477
478static void
479shell_touch_grab_end(struct shell_touch_grab *grab)
480{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700481 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700482 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700483 grab->shsurf->grabbed = 0;
484 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700485
486 weston_touch_end_grab(grab->touch);
487}
488
489static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500490center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800491 struct weston_output *output);
492
Daniel Stone496ca172012-05-30 16:31:42 +0100493static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300494get_modifier(char *modifier)
495{
496 if (!modifier)
497 return MODIFIER_SUPER;
498
499 if (!strcmp("ctrl", modifier))
500 return MODIFIER_CTRL;
501 else if (!strcmp("alt", modifier))
502 return MODIFIER_ALT;
503 else if (!strcmp("super", modifier))
504 return MODIFIER_SUPER;
505 else
506 return MODIFIER_SUPER;
507}
508
Juan Zhaoe10d2792012-04-25 19:09:52 +0800509static enum animation_type
510get_animation_type(char *animation)
511{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800512 if (!strcmp("zoom", animation))
513 return ANIMATION_ZOOM;
514 else if (!strcmp("fade", animation))
515 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100516 else if (!strcmp("dim-layer", animation))
517 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800518 else
519 return ANIMATION_NONE;
520}
521
Alex Wu4539b082012-03-01 12:57:46 +0800522static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400523shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200524{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400525 struct weston_config_section *section;
526 int duration;
527 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200528
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400529 section = weston_config_get_section(shell->compositor->config,
530 "screensaver", NULL, NULL);
531 weston_config_section_get_string(section,
532 "path", &shell->screensaver.path, NULL);
533 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200534 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400535
536 section = weston_config_get_section(shell->compositor->config,
537 "shell", NULL, NULL);
538 weston_config_section_get_string(section,
539 "binding-modifier", &s, "super");
540 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200541 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400542 weston_config_section_get_string(section, "animation", &s, "none");
543 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200544 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700545 weston_config_section_get_string(section,
546 "startup-animation", &s, "fade");
547 shell->startup_animation_type = get_animation_type(s);
548 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700549 if (shell->startup_animation_type == ANIMATION_ZOOM)
550 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100551 weston_config_section_get_string(section, "focus-animation", &s, "none");
552 shell->focus_animation_type = get_animation_type(s);
553 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400554 weston_config_section_get_uint(section, "num-workspaces",
555 &shell->workspaces.num,
556 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200557}
558
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100559static struct weston_output *
560get_default_output(struct weston_compositor *compositor)
561{
562 return container_of(compositor->output_list.next,
563 struct weston_output, link);
564}
565
566
567/* no-op func for checking focus surface */
568static void
569focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy,
570 int32_t width, int32_t height)
571{
572}
573
574static struct focus_surface *
575get_focus_surface(struct weston_surface *surface)
576{
577 if (surface->configure == focus_surface_configure)
578 return surface->configure_private;
579 else
580 return NULL;
581}
582
583static bool
584is_focus_surface (struct weston_surface *es)
585{
586 return (es->configure == focus_surface_configure);
587}
588
589static bool
590is_focus_view (struct weston_view *view)
591{
592 return is_focus_surface (view->surface);
593}
594
595static struct focus_surface *
596create_focus_surface(struct weston_compositor *ec,
597 struct weston_output *output)
598{
599 struct focus_surface *fsurf = NULL;
600 struct weston_surface *surface = NULL;
601
602 fsurf = malloc(sizeof *fsurf);
603 if (!fsurf)
604 return NULL;
605
606 fsurf->surface = weston_surface_create(ec);
607 surface = fsurf->surface;
608 if (surface == NULL) {
609 free(fsurf);
610 return NULL;
611 }
612
613 surface->configure = focus_surface_configure;
614 surface->output = output;
615 surface->configure_private = fsurf;
616
617 fsurf->view = weston_view_create (surface);
618
619 weston_view_configure(fsurf->view, output->x, output->y,
620 output->width, output->height);
621 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
622 pixman_region32_fini(&surface->opaque);
623 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
624 output->width, output->height);
625 pixman_region32_fini(&surface->input);
626 pixman_region32_init(&surface->input);
627
628 wl_list_init(&fsurf->workspace_transform.link);
629
630 return fsurf;
631}
632
633static void
634focus_surface_destroy(struct focus_surface *fsurf)
635{
636 weston_surface_destroy(fsurf->surface);
637 free(fsurf);
638}
639
640static void
641focus_animation_done(struct weston_view_animation *animation, void *data)
642{
643 struct workspace *ws = data;
644
645 ws->focus_animation = NULL;
646}
647
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200648static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200649focus_state_destroy(struct focus_state *state)
650{
651 wl_list_remove(&state->seat_destroy_listener.link);
652 wl_list_remove(&state->surface_destroy_listener.link);
653 free(state);
654}
655
656static void
657focus_state_seat_destroy(struct wl_listener *listener, void *data)
658{
659 struct focus_state *state = container_of(listener,
660 struct focus_state,
661 seat_destroy_listener);
662
663 wl_list_remove(&state->link);
664 focus_state_destroy(state);
665}
666
667static void
668focus_state_surface_destroy(struct wl_listener *listener, void *data)
669{
670 struct focus_state *state = container_of(listener,
671 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400672 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400673 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500674 struct weston_surface *main_surface, *next;
675 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200676
Pekka Paalanen01388e22013-04-25 13:57:44 +0300677 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
678
Kristian Høgsberge3778222012-07-31 17:29:30 -0400679 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500680 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
681 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400682 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100683 if (is_focus_view(view))
684 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400685
Jason Ekstranda7af7042013-10-12 22:38:11 -0500686 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400687 break;
688 }
689
Pekka Paalanen01388e22013-04-25 13:57:44 +0300690 /* if the focus was a sub-surface, activate its main surface */
691 if (main_surface != state->keyboard_focus)
692 next = main_surface;
693
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100694 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400695 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100696 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400697 activate(shell, next, state->seat);
698 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100699 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
700 if (state->ws->focus_animation)
701 weston_view_animation_destroy(state->ws->focus_animation);
702
703 state->ws->focus_animation = weston_fade_run(
704 state->ws->fsurf_front->view,
705 state->ws->fsurf_front->view->alpha, 0.0, 300,
706 focus_animation_done, state->ws);
707 }
708
Kristian Høgsberge3778222012-07-31 17:29:30 -0400709 wl_list_remove(&state->link);
710 focus_state_destroy(state);
711 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200712}
713
714static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400715focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200716{
Jonas Ådahl04769742012-06-13 00:01:24 +0200717 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200718
719 state = malloc(sizeof *state);
720 if (state == NULL)
721 return NULL;
722
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100723 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400724 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200725 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400726 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200727
728 state->seat_destroy_listener.notify = focus_state_seat_destroy;
729 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400730 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200731 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400732 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200733
734 return state;
735}
736
Jonas Ådahl8538b222012-08-29 22:13:03 +0200737static struct focus_state *
738ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
739{
740 struct workspace *ws = get_current_workspace(shell);
741 struct focus_state *state;
742
743 wl_list_for_each(state, &ws->focus_list, link)
744 if (state->seat == seat)
745 break;
746
747 if (&state->link == &ws->focus_list)
748 state = focus_state_create(seat, ws);
749
750 return state;
751}
752
Jonas Ådahl04769742012-06-13 00:01:24 +0200753static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400754restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200755{
756 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400757 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200758
759 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400760 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200761
Kristian Høgsberge3148752013-05-06 23:19:49 -0400762 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200763 }
764}
765
766static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200767replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
768 struct weston_seat *seat)
769{
770 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400771 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200772
773 wl_list_for_each(state, &ws->focus_list, link) {
774 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400775 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500776 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200777 return;
778 }
779 }
780}
781
782static void
783drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
784 struct weston_surface *surface)
785{
786 struct focus_state *state;
787
788 wl_list_for_each(state, &ws->focus_list, link)
789 if (state->keyboard_focus == surface)
790 state->keyboard_focus = NULL;
791}
792
793static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100794animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
795 struct weston_view *from, struct weston_view *to)
796{
797 struct weston_output *output;
798 bool focus_surface_created = false;
799
800 /* FIXME: Only support dim animation using two layers */
801 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
802 return;
803
804 output = get_default_output(shell->compositor);
805 if (ws->fsurf_front == NULL && (from || to)) {
806 ws->fsurf_front = create_focus_surface(shell->compositor, output);
807 ws->fsurf_back = create_focus_surface(shell->compositor, output);
808 ws->fsurf_front->view->alpha = 0.0;
809 ws->fsurf_back->view->alpha = 0.0;
810 focus_surface_created = true;
811 } else {
812 wl_list_remove(&ws->fsurf_front->view->layer_link);
813 wl_list_remove(&ws->fsurf_back->view->layer_link);
814 }
815
816 if (ws->focus_animation) {
817 weston_view_animation_destroy(ws->focus_animation);
818 ws->focus_animation = NULL;
819 }
820
821 if (to)
822 wl_list_insert(&to->layer_link,
823 &ws->fsurf_front->view->layer_link);
824 else if (from)
825 wl_list_insert(&ws->layer.view_list,
826 &ws->fsurf_front->view->layer_link);
827
828 if (focus_surface_created) {
829 ws->focus_animation = weston_fade_run(
830 ws->fsurf_front->view,
831 ws->fsurf_front->view->alpha, 0.6, 300,
832 focus_animation_done, ws);
833 } else if (from) {
834 wl_list_insert(&from->layer_link,
835 &ws->fsurf_back->view->layer_link);
836 ws->focus_animation = weston_stable_fade_run(
837 ws->fsurf_front->view, 0.0,
838 ws->fsurf_back->view, 0.6,
839 focus_animation_done, ws);
840 } else if (to) {
841 wl_list_insert(&ws->layer.view_list,
842 &ws->fsurf_back->view->layer_link);
843 ws->focus_animation = weston_stable_fade_run(
844 ws->fsurf_front->view, 0.0,
845 ws->fsurf_back->view, 0.6,
846 focus_animation_done, ws);
847 }
848}
849
850static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200851workspace_destroy(struct workspace *ws)
852{
Jonas Ådahl04769742012-06-13 00:01:24 +0200853 struct focus_state *state, *next;
854
855 wl_list_for_each_safe(state, next, &ws->focus_list, link)
856 focus_state_destroy(state);
857
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100858 if (ws->fsurf_front)
859 focus_surface_destroy(ws->fsurf_front);
860 if (ws->fsurf_back)
861 focus_surface_destroy(ws->fsurf_back);
862
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200863 free(ws);
864}
865
Jonas Ådahl04769742012-06-13 00:01:24 +0200866static void
867seat_destroyed(struct wl_listener *listener, void *data)
868{
869 struct weston_seat *seat = data;
870 struct focus_state *state, *next;
871 struct workspace *ws = container_of(listener,
872 struct workspace,
873 seat_destroyed_listener);
874
875 wl_list_for_each_safe(state, next, &ws->focus_list, link)
876 if (state->seat == seat)
877 wl_list_remove(&state->link);
878}
879
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200880static struct workspace *
881workspace_create(void)
882{
883 struct workspace *ws = malloc(sizeof *ws);
884 if (ws == NULL)
885 return NULL;
886
887 weston_layer_init(&ws->layer, NULL);
888
Jonas Ådahl04769742012-06-13 00:01:24 +0200889 wl_list_init(&ws->focus_list);
890 wl_list_init(&ws->seat_destroyed_listener.link);
891 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100892 ws->fsurf_front = NULL;
893 ws->fsurf_back = NULL;
894 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200895
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200896 return ws;
897}
898
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200899static int
900workspace_is_empty(struct workspace *ws)
901{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500902 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200903}
904
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905static struct workspace *
906get_workspace(struct desktop_shell *shell, unsigned int index)
907{
908 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200909 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200910 pws += index;
911 return *pws;
912}
913
914static struct workspace *
915get_current_workspace(struct desktop_shell *shell)
916{
917 return get_workspace(shell, shell->workspaces.current);
918}
919
920static void
921activate_workspace(struct desktop_shell *shell, unsigned int index)
922{
923 struct workspace *ws;
924
925 ws = get_workspace(shell, index);
926 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
927
928 shell->workspaces.current = index;
929}
930
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200931static unsigned int
932get_output_height(struct weston_output *output)
933{
934 return abs(output->region.extents.y1 - output->region.extents.y2);
935}
936
937static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100938view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200939{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200940 struct weston_transform *transform;
941
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100942 if (is_focus_view(view)) {
943 struct focus_surface *fsurf = get_focus_surface(view->surface);
944 transform = &fsurf->workspace_transform;
945 } else {
946 struct shell_surface *shsurf = get_shell_surface(view->surface);
947 transform = &shsurf->workspace_transform;
948 }
949
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200950 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100952 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200953
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100954 weston_matrix_init(&transform->matrix);
955 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200956 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200958}
959
960static void
961workspace_translate_out(struct workspace *ws, double fraction)
962{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964 unsigned int height;
965 double d;
966
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
968 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200969 d = height * fraction;
970
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100971 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200972 }
973}
974
975static void
976workspace_translate_in(struct workspace *ws, double fraction)
977{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500978 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200979 unsigned int height;
980 double d;
981
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
983 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984
985 if (fraction > 0)
986 d = -(height - height * fraction);
987 else
988 d = height + height * fraction;
989
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100990 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991 }
992}
993
994static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200995broadcast_current_workspace_state(struct desktop_shell *shell)
996{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700997 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200998
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700999 wl_resource_for_each(resource, &shell->workspaces.client_list)
1000 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001001 shell->workspaces.current,
1002 shell->workspaces.num);
1003}
1004
1005static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001006reverse_workspace_change_animation(struct desktop_shell *shell,
1007 unsigned int index,
1008 struct workspace *from,
1009 struct workspace *to)
1010{
1011 shell->workspaces.current = index;
1012
1013 shell->workspaces.anim_to = to;
1014 shell->workspaces.anim_from = from;
1015 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1016 shell->workspaces.anim_timestamp = 0;
1017
Scott Moreau4272e632012-08-13 09:58:41 -06001018 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001019}
1020
1021static void
1022workspace_deactivate_transforms(struct workspace *ws)
1023{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001025 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001026
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001028 if (is_focus_view(view)) {
1029 struct focus_surface *fsurf = get_focus_surface(view->surface);
1030 transform = &fsurf->workspace_transform;
1031 } else {
1032 struct shell_surface *shsurf = get_shell_surface(view->surface);
1033 transform = &shsurf->workspace_transform;
1034 }
1035
1036 if (!wl_list_empty(&transform->link)) {
1037 wl_list_remove(&transform->link);
1038 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001039 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001041 }
1042}
1043
1044static void
1045finish_workspace_change_animation(struct desktop_shell *shell,
1046 struct workspace *from,
1047 struct workspace *to)
1048{
Scott Moreau4272e632012-08-13 09:58:41 -06001049 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001050
1051 wl_list_remove(&shell->workspaces.animation.link);
1052 workspace_deactivate_transforms(from);
1053 workspace_deactivate_transforms(to);
1054 shell->workspaces.anim_to = NULL;
1055
1056 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1057}
1058
1059static void
1060animate_workspace_change_frame(struct weston_animation *animation,
1061 struct weston_output *output, uint32_t msecs)
1062{
1063 struct desktop_shell *shell =
1064 container_of(animation, struct desktop_shell,
1065 workspaces.animation);
1066 struct workspace *from = shell->workspaces.anim_from;
1067 struct workspace *to = shell->workspaces.anim_to;
1068 uint32_t t;
1069 double x, y;
1070
1071 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1072 finish_workspace_change_animation(shell, from, to);
1073 return;
1074 }
1075
1076 if (shell->workspaces.anim_timestamp == 0) {
1077 if (shell->workspaces.anim_current == 0.0)
1078 shell->workspaces.anim_timestamp = msecs;
1079 else
1080 shell->workspaces.anim_timestamp =
1081 msecs -
1082 /* Invers of movement function 'y' below. */
1083 (asin(1.0 - shell->workspaces.anim_current) *
1084 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1085 M_2_PI);
1086 }
1087
1088 t = msecs - shell->workspaces.anim_timestamp;
1089
1090 /*
1091 * x = [0, π/2]
1092 * y(x) = sin(x)
1093 */
1094 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1095 y = sin(x);
1096
1097 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001098 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001099
1100 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1101 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1102 shell->workspaces.anim_current = y;
1103
Scott Moreau4272e632012-08-13 09:58:41 -06001104 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001106 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001108}
1109
1110static void
1111animate_workspace_change(struct desktop_shell *shell,
1112 unsigned int index,
1113 struct workspace *from,
1114 struct workspace *to)
1115{
1116 struct weston_output *output;
1117
1118 int dir;
1119
1120 if (index > shell->workspaces.current)
1121 dir = -1;
1122 else
1123 dir = 1;
1124
1125 shell->workspaces.current = index;
1126
1127 shell->workspaces.anim_dir = dir;
1128 shell->workspaces.anim_from = from;
1129 shell->workspaces.anim_to = to;
1130 shell->workspaces.anim_current = 0.0;
1131 shell->workspaces.anim_timestamp = 0;
1132
1133 output = container_of(shell->compositor->output_list.next,
1134 struct weston_output, link);
1135 wl_list_insert(&output->animation_list,
1136 &shell->workspaces.animation.link);
1137
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001138 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001139
1140 workspace_translate_in(to, 0);
1141
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001142 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001143
Scott Moreau4272e632012-08-13 09:58:41 -06001144 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001145}
1146
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001147static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001148update_workspace(struct desktop_shell *shell, unsigned int index,
1149 struct workspace *from, struct workspace *to)
1150{
1151 shell->workspaces.current = index;
1152 wl_list_insert(&from->layer.link, &to->layer.link);
1153 wl_list_remove(&from->layer.link);
1154}
1155
1156static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001157change_workspace(struct desktop_shell *shell, unsigned int index)
1158{
1159 struct workspace *from;
1160 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001161 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001162
1163 if (index == shell->workspaces.current)
1164 return;
1165
1166 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001168 return;
1169
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001170 from = get_current_workspace(shell);
1171 to = get_workspace(shell, index);
1172
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001173 if (shell->workspaces.anim_from == to &&
1174 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001175 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001176 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001177 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001178 return;
1179 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001180
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001181 if (shell->workspaces.anim_to != NULL)
1182 finish_workspace_change_animation(shell,
1183 shell->workspaces.anim_from,
1184 shell->workspaces.anim_to);
1185
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001186 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001187
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001188 if (shell->focus_animation_type != ANIMATION_NONE) {
1189 wl_list_for_each(state, &from->focus_list, link)
1190 if (state->keyboard_focus)
1191 animate_focus_change(shell, from,
1192 get_default_view(state->keyboard_focus), NULL);
1193
1194 wl_list_for_each(state, &to->focus_list, link)
1195 if (state->keyboard_focus)
1196 animate_focus_change(shell, to,
1197 NULL, get_default_view(state->keyboard_focus));
1198 }
1199
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001200 if (workspace_is_empty(to) && workspace_is_empty(from))
1201 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001202 else
1203 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001204
1205 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001206}
1207
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001208static bool
1209workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1210{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001211 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001212 struct wl_list *e;
1213
1214 if (wl_list_empty(list))
1215 return false;
1216
1217 e = list->next;
1218
1219 if (e->next != list)
1220 return false;
1221
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001223}
1224
1225static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226move_view_to_workspace(struct desktop_shell *shell,
1227 struct weston_view *view,
1228 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001229{
1230 struct workspace *from;
1231 struct workspace *to;
1232 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001233 struct weston_surface *focus;
1234
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001236
1237 if (workspace == shell->workspaces.current)
1238 return;
1239
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001240 if (workspace >= shell->workspaces.num)
1241 workspace = shell->workspaces.num - 1;
1242
Jonas Ådahle9d22502012-08-29 22:13:01 +02001243 from = get_current_workspace(shell);
1244 to = get_workspace(shell, workspace);
1245
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 wl_list_remove(&view->layer_link);
1247 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001248
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001250 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1251 if (!seat->keyboard)
1252 continue;
1253
1254 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001256 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001257 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001258
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001260}
1261
1262static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001263take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001264 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001265 unsigned int index)
1266{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001267 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001269 struct shell_surface *shsurf;
1270 struct workspace *from;
1271 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001272 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001273
Pekka Paalanen01388e22013-04-25 13:57:44 +03001274 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 view = get_default_view(surface);
1276 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001277 index == shell->workspaces.current ||
1278 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001279 return;
1280
1281 from = get_current_workspace(shell);
1282 to = get_workspace(shell, index);
1283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 wl_list_remove(&view->layer_link);
1285 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001286
Jonas Ådahle9d22502012-08-29 22:13:01 +02001287 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001288 drop_focus_state(shell, from, surface);
1289
1290 if (shell->workspaces.anim_from == to &&
1291 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001292 wl_list_remove(&to->layer.link);
1293 wl_list_insert(from->layer.link.prev, &to->layer.link);
1294
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001296 broadcast_current_workspace_state(shell);
1297
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001298 return;
1299 }
1300
1301 if (shell->workspaces.anim_to != NULL)
1302 finish_workspace_change_animation(shell,
1303 shell->workspaces.anim_from,
1304 shell->workspaces.anim_to);
1305
1306 if (workspace_is_empty(from) &&
1307 workspace_has_only(to, surface))
1308 update_workspace(shell, index, from, to);
1309 else {
1310 shsurf = get_shell_surface(surface);
1311 if (wl_list_empty(&shsurf->workspace_transform.link))
1312 wl_list_insert(&shell->workspaces.anim_sticky_list,
1313 &shsurf->workspace_transform.link);
1314
1315 animate_workspace_change(shell, index, from, to);
1316 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001317
1318 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001319
1320 state = ensure_focus_state(shell, seat);
1321 if (state != NULL)
1322 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323}
1324
1325static void
1326workspace_manager_move_surface(struct wl_client *client,
1327 struct wl_resource *resource,
1328 struct wl_resource *surface_resource,
1329 uint32_t workspace)
1330{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001331 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001332 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001333 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001334 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001336
Pekka Paalanen01388e22013-04-25 13:57:44 +03001337 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 view = get_default_view(main_surface);
1339 if (!view)
1340 return;
1341 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001342}
1343
1344static const struct workspace_manager_interface workspace_manager_implementation = {
1345 workspace_manager_move_surface,
1346};
1347
1348static void
1349unbind_resource(struct wl_resource *resource)
1350{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001351 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001352}
1353
1354static void
1355bind_workspace_manager(struct wl_client *client,
1356 void *data, uint32_t version, uint32_t id)
1357{
1358 struct desktop_shell *shell = data;
1359 struct wl_resource *resource;
1360
Jason Ekstranda85118c2013-06-27 20:17:02 -05001361 resource = wl_resource_create(client,
1362 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001363
1364 if (resource == NULL) {
1365 weston_log("couldn't add workspace manager object");
1366 return;
1367 }
1368
Jason Ekstranda85118c2013-06-27 20:17:02 -05001369 wl_resource_set_implementation(resource,
1370 &workspace_manager_implementation,
1371 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001372 wl_list_insert(&shell->workspaces.client_list,
1373 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001374
1375 workspace_manager_send_state(resource,
1376 shell->workspaces.current,
1377 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001378}
1379
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001380static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001381touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1382 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1383{
1384}
1385
1386static void
1387touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1388{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001389 struct weston_touch_move_grab *move =
1390 (struct weston_touch_move_grab *) container_of(
1391 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001392
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001393 if (grab->touch->seat->num_tp == 0) {
1394 shell_touch_grab_end(&move->base);
1395 free(move);
1396 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001397}
1398
1399static void
1400touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1401 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1402{
1403 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1404 struct shell_surface *shsurf = move->base.shsurf;
1405 struct weston_surface *es;
1406 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1407 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1408
1409 if (!shsurf)
1410 return;
1411
1412 es = shsurf->surface;
1413
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 weston_view_configure(shsurf->view, dx, dy,
1415 shsurf->view->geometry.width,
1416 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001417
1418 weston_compositor_schedule_repaint(es->compositor);
1419}
1420
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001421static void
1422touch_move_grab_cancel(struct weston_touch_grab *grab)
1423{
1424 struct weston_touch_move_grab *move =
1425 (struct weston_touch_move_grab *) container_of(
1426 grab, struct shell_touch_grab, grab);
1427
1428 shell_touch_grab_end(&move->base);
1429 free(move);
1430}
1431
Rusty Lynch1084da52013-08-15 09:10:08 -07001432static const struct weston_touch_grab_interface touch_move_grab_interface = {
1433 touch_move_grab_down,
1434 touch_move_grab_up,
1435 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001436 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001437};
1438
1439static int
1440surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1441{
1442 struct weston_touch_move_grab *move;
1443
1444 if (!shsurf)
1445 return -1;
1446
1447 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1448 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001449 if (shsurf->grabbed)
1450 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001451
1452 move = malloc(sizeof *move);
1453 if (!move)
1454 return -1;
1455
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001457 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001459 seat->touch->grab_y;
1460
1461 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1462 seat->touch);
1463
1464 return 0;
1465}
1466
1467static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001468noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001469{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001470}
1471
1472static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001473move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1474 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001475{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001476 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001477 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001478 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001479 int dx, dy;
1480
1481 weston_pointer_move(pointer, x, y);
1482 dx = wl_fixed_to_int(pointer->x + move->dx);
1483 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001484
1485 if (!shsurf)
1486 return;
1487
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488 weston_view_configure(shsurf->view, dx, dy,
1489 shsurf->view->geometry.width,
1490 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001491
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001493}
1494
1495static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001496move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001497 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001498{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001499 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1500 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001501 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001502 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001503
Daniel Stone4dbadb12012-05-30 16:31:51 +01001504 if (pointer->button_count == 0 &&
1505 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001506 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001507 free(grab);
1508 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001509}
1510
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001511static void
1512move_grab_cancel(struct weston_pointer_grab *grab)
1513{
1514 struct shell_grab *shell_grab =
1515 container_of(grab, struct shell_grab, grab);
1516
1517 shell_grab_end(shell_grab);
1518 free(grab);
1519}
1520
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001521static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001522 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001523 move_grab_motion,
1524 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001525 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001526};
1527
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001528static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001529surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001530{
1531 struct weston_move_grab *move;
1532
1533 if (!shsurf)
1534 return -1;
1535
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001536 if (shsurf->grabbed)
1537 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001538 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1539 return 0;
1540
1541 move = malloc(sizeof *move);
1542 if (!move)
1543 return -1;
1544
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001546 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001548 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001549
1550 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001551 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001552
1553 return 0;
1554}
1555
1556static void
1557shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1558 struct wl_resource *seat_resource, uint32_t serial)
1559{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001560 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001561 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001562 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001563
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001564 if (seat->pointer &&
1565 seat->pointer->button_count > 0 &&
1566 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001567 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001568 if ((surface == shsurf->surface) &&
1569 (surface_move(shsurf, seat) < 0))
1570 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001571 } else if (seat->touch &&
1572 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001574 if ((surface == shsurf->surface) &&
1575 (surface_touch_move(shsurf, seat) < 0))
1576 wl_resource_post_no_memory(resource);
1577 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001578}
1579
1580struct weston_resize_grab {
1581 struct shell_grab base;
1582 uint32_t edges;
1583 int32_t width, height;
1584};
1585
1586static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001587resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1588 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001589{
1590 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001591 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001592 struct shell_surface *shsurf = resize->base.shsurf;
1593 int32_t width, height;
1594 wl_fixed_t from_x, from_y;
1595 wl_fixed_t to_x, to_y;
1596
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001597 weston_pointer_move(pointer, x, y);
1598
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001599 if (!shsurf)
1600 return;
1601
Jason Ekstranda7af7042013-10-12 22:38:11 -05001602 weston_view_from_global_fixed(shsurf->view,
1603 pointer->grab_x, pointer->grab_y,
1604 &from_x, &from_y);
1605 weston_view_from_global_fixed(shsurf->view,
1606 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001607
1608 width = resize->width;
1609 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1610 width += wl_fixed_to_int(from_x - to_x);
1611 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1612 width += wl_fixed_to_int(to_x - from_x);
1613 }
1614
1615 height = resize->height;
1616 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1617 height += wl_fixed_to_int(from_y - to_y);
1618 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1619 height += wl_fixed_to_int(to_y - from_y);
1620 }
1621
1622 shsurf->client->send_configure(shsurf->surface,
1623 resize->edges, width, height);
1624}
1625
1626static void
1627send_configure(struct weston_surface *surface,
1628 uint32_t edges, int32_t width, int32_t height)
1629{
1630 struct shell_surface *shsurf = get_shell_surface(surface);
1631
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001632 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633 edges, width, height);
1634}
1635
1636static const struct weston_shell_client shell_client = {
1637 send_configure
1638};
1639
1640static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001641resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001642 uint32_t time, uint32_t button, uint32_t state_w)
1643{
1644 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001645 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001646 enum wl_pointer_button_state state = state_w;
1647
1648 if (pointer->button_count == 0 &&
1649 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1650 shell_grab_end(&resize->base);
1651 free(grab);
1652 }
1653}
1654
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001655static void
1656resize_grab_cancel(struct weston_pointer_grab *grab)
1657{
1658 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1659
1660 shell_grab_end(&resize->base);
1661 free(grab);
1662}
1663
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001664static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001665 noop_grab_focus,
1666 resize_grab_motion,
1667 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001668 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001669};
1670
Giulio Camuffob8366642013-04-25 13:57:46 +03001671/*
1672 * Returns the bounding box of a surface and all its sub-surfaces,
1673 * in the surface coordinates system. */
1674static void
1675surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1676 int32_t *y, int32_t *w, int32_t *h) {
1677 pixman_region32_t region;
1678 pixman_box32_t *box;
1679 struct weston_subsurface *subsurface;
1680
1681 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 surface->width,
1683 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001684
1685 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1686 pixman_region32_union_rect(&region, &region,
1687 subsurface->position.x,
1688 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001689 subsurface->surface->width,
1690 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001691 }
1692
1693 box = pixman_region32_extents(&region);
1694 if (x)
1695 *x = box->x1;
1696 if (y)
1697 *y = box->y1;
1698 if (w)
1699 *w = box->x2 - box->x1;
1700 if (h)
1701 *h = box->y2 - box->y1;
1702
1703 pixman_region32_fini(&region);
1704}
1705
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001706static int
1707surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001708 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709{
1710 struct weston_resize_grab *resize;
1711
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001712 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1713 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001714 return 0;
1715
1716 if (edges == 0 || edges > 15 ||
1717 (edges & 3) == 3 || (edges & 12) == 12)
1718 return 0;
1719
1720 resize = malloc(sizeof *resize);
1721 if (!resize)
1722 return -1;
1723
1724 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001725 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1726 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727
1728 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001729 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001730
1731 return 0;
1732}
1733
1734static void
1735shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1736 struct wl_resource *seat_resource, uint32_t serial,
1737 uint32_t edges)
1738{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001739 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001740 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001741 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001742
1743 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1744 return;
1745
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001747 if (seat->pointer->button_count == 0 ||
1748 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001749 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001750 return;
1751
Kristian Høgsberge3148752013-05-06 23:19:49 -04001752 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001753 wl_resource_post_no_memory(resource);
1754}
1755
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001756static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001757end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1758
1759static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001760busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001761{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001762 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001763 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001764 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001765 wl_fixed_t sx, sy;
1766
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 view = weston_compositor_pick_view(pointer->seat->compositor,
1768 pointer->x, pointer->y,
1769 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001770
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001772 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001773}
1774
1775static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001776busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1777 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001778{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001779 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001780}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001781
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001782static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001783busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001784 uint32_t time, uint32_t button, uint32_t state)
1785{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001786 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001787 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001788 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001789
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001790 if (shsurf && button == BTN_LEFT && state) {
1791 activate(shsurf->shell, shsurf->surface, seat);
1792 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001793 } else if (shsurf && button == BTN_RIGHT && state) {
1794 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001795 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001796 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001797}
1798
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001799static void
1800busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1801{
1802 struct shell_grab *grab = (struct shell_grab *) base;
1803
1804 shell_grab_end(grab);
1805 free(grab);
1806}
1807
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001808static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001809 busy_cursor_grab_focus,
1810 busy_cursor_grab_motion,
1811 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001812 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001813};
1814
1815static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001816set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001817{
1818 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001819
1820 grab = malloc(sizeof *grab);
1821 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001822 return;
1823
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001824 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1825 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001826}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001827
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001828static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001829end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001830{
1831 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1832
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001833 if (grab->grab.interface == &busy_cursor_grab_interface &&
1834 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001835 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001836 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001837 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001838}
1839
Scott Moreau9521d5e2012-04-19 13:06:17 -06001840static void
1841ping_timer_destroy(struct shell_surface *shsurf)
1842{
1843 if (!shsurf || !shsurf->ping_timer)
1844 return;
1845
1846 if (shsurf->ping_timer->source)
1847 wl_event_source_remove(shsurf->ping_timer->source);
1848
1849 free(shsurf->ping_timer);
1850 shsurf->ping_timer = NULL;
1851}
1852
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001853static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001854ping_timeout_handler(void *data)
1855{
1856 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001857 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001858
Scott Moreau9521d5e2012-04-19 13:06:17 -06001859 /* Client is not responding */
1860 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001861
1862 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001863 if (seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001864 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001865
1866 return 1;
1867}
1868
1869static void
1870ping_handler(struct weston_surface *surface, uint32_t serial)
1871{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001872 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001873 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001874 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001875
1876 if (!shsurf)
1877 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001878 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001879 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001880
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001881 if (shsurf->surface == shsurf->shell->grab_surface)
1882 return;
1883
Scott Moreauff1db4a2012-04-17 19:06:18 -06001884 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001885 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001886 if (!shsurf->ping_timer)
1887 return;
1888
1889 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001890 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1891 shsurf->ping_timer->source =
1892 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1893 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1894
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001895 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001896 }
1897}
1898
1899static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001900handle_pointer_focus(struct wl_listener *listener, void *data)
1901{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001902 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001903 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001904 struct weston_compositor *compositor;
1905 struct shell_surface *shsurf;
1906 uint32_t serial;
1907
Jason Ekstranda7af7042013-10-12 22:38:11 -05001908 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001909 return;
1910
Jason Ekstranda7af7042013-10-12 22:38:11 -05001911 compositor = view->surface->compositor;
1912 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001913
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001914 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001915 set_busy_cursor(shsurf, pointer);
1916 } else {
1917 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001918 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001919 }
1920}
1921
1922static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001923create_pointer_focus_listener(struct weston_seat *seat)
1924{
1925 struct wl_listener *listener;
1926
Kristian Høgsberge3148752013-05-06 23:19:49 -04001927 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001928 return;
1929
1930 listener = malloc(sizeof *listener);
1931 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001932 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001933}
1934
1935static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001936shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1937 uint32_t serial)
1938{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001939 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001940 struct weston_seat *seat;
1941 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001942
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001943 if (shsurf->ping_timer == NULL)
1944 /* Just ignore unsolicited pong. */
1945 return;
1946
Scott Moreauff1db4a2012-04-17 19:06:18 -06001947 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001948 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001949 wl_list_for_each(seat, &ec->seat_list, link) {
1950 if(seat->pointer)
1951 end_busy_cursor(shsurf, seat->pointer);
1952 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001953 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001954 }
1955}
1956
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001957static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001958set_title(struct shell_surface *shsurf, const char *title)
1959{
1960 free(shsurf->title);
1961 shsurf->title = strdup(title);
1962}
1963
1964static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001965shell_surface_set_title(struct wl_client *client,
1966 struct wl_resource *resource, const char *title)
1967{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001968 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001969
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001970 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001971}
1972
1973static void
1974shell_surface_set_class(struct wl_client *client,
1975 struct wl_resource *resource, const char *class)
1976{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001977 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001978
1979 free(shsurf->class);
1980 shsurf->class = strdup(class);
1981}
1982
Alex Wu4539b082012-03-01 12:57:46 +08001983static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001984restore_output_mode(struct weston_output *output)
1985{
Hardening57388e42013-09-18 23:56:36 +02001986 if (output->current_mode != output->original_mode ||
1987 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001988 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001989 output->original_mode,
1990 output->original_scale,
1991 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001992}
1993
1994static void
1995restore_all_output_modes(struct weston_compositor *compositor)
1996{
1997 struct weston_output *output;
1998
1999 wl_list_for_each(output, &compositor->output_list, link)
2000 restore_output_mode(output);
2001}
2002
2003static void
Alex Wu4539b082012-03-01 12:57:46 +08002004shell_unset_fullscreen(struct shell_surface *shsurf)
2005{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002006 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08002007 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08002008 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2009 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002010 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002011 }
Alex Wu4539b082012-03-01 12:57:46 +08002012 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2013 shsurf->fullscreen.framerate = 0;
2014 wl_list_remove(&shsurf->fullscreen.transform.link);
2015 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016 if (shsurf->fullscreen.black_view)
2017 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2018 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002019 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020 weston_view_set_position(shsurf->view,
2021 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002022 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002024 &shsurf->rotation.transform.link);
2025 shsurf->saved_rotation_valid = false;
2026 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002027
2028 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002029 wl_list_remove(&shsurf->view->layer_link);
2030 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08002031}
2032
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002033static void
2034shell_unset_maximized(struct shell_surface *shsurf)
2035{
2036 struct workspace *ws;
2037 /* undo all maximized things here */
2038 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039 weston_view_set_position(shsurf->view,
2040 shsurf->saved_x,
2041 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002042
2043 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044 wl_list_insert(&shsurf->view->geometry.transformation_list,
2045 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002046 shsurf->saved_rotation_valid = false;
2047 }
2048
2049 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002050 wl_list_remove(&shsurf->view->layer_link);
2051 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002052}
2053
Pekka Paalanen98262232011-12-01 10:42:22 +02002054static int
2055reset_shell_surface_type(struct shell_surface *surface)
2056{
2057 switch (surface->type) {
2058 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08002059 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002060 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002061 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002062 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002063 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002064 case SHELL_SURFACE_NONE:
2065 case SHELL_SURFACE_TOPLEVEL:
2066 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002067 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002068 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02002069 break;
2070 }
2071
2072 surface->type = SHELL_SURFACE_NONE;
2073 return 0;
2074}
2075
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002076static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002077set_surface_type(struct shell_surface *shsurf)
2078{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002079 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002081
2082 reset_shell_surface_type(shsurf);
2083
2084 shsurf->type = shsurf->next_type;
2085 shsurf->next_type = SHELL_SURFACE_NONE;
2086
2087 switch (shsurf->type) {
2088 case SHELL_SURFACE_TOPLEVEL:
2089 break;
2090 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002091 if (pev)
2092 weston_view_set_position(shsurf->view,
2093 pev->geometry.x + shsurf->transient.x,
2094 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002095 break;
2096
2097 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002098 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002099 shsurf->saved_x = shsurf->view->geometry.x;
2100 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002101 shsurf->saved_position_valid = true;
2102
2103 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2104 wl_list_remove(&shsurf->rotation.transform.link);
2105 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002107 shsurf->saved_rotation_valid = true;
2108 }
2109 break;
2110
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002111 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112 weston_view_set_position(shsurf->view, shsurf->transient.x,
2113 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002114 break;
2115
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002116 default:
2117 break;
2118 }
2119}
2120
2121static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002122set_toplevel(struct shell_surface *shsurf)
2123{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002124 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002125}
2126
2127static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002128shell_surface_set_toplevel(struct wl_client *client,
2129 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002130{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002131 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002132
Tiago Vignattibc052c92012-04-19 16:18:18 +03002133 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002134}
2135
2136static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04002137set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002138 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04002139{
2140 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002141 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002142 shsurf->transient.x = x;
2143 shsurf->transient.y = y;
2144 shsurf->transient.flags = flags;
2145 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
2146}
2147
2148static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002149shell_surface_set_transient(struct wl_client *client,
2150 struct wl_resource *resource,
2151 struct wl_resource *parent_resource,
2152 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002153{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002154 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002155 struct weston_surface *parent =
2156 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002157
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002158 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002159}
2160
Tiago Vignattibe143262012-04-16 17:31:41 +03002161static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002162shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002163{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002164 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002165}
2166
2167static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002168get_output_panel_height(struct desktop_shell *shell,
2169 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08002170{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08002172 int panel_height = 0;
2173
2174 if (!output)
2175 return 0;
2176
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2178 if (view->surface->output == output) {
2179 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08002180 break;
2181 }
2182 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002183
Juan Zhao96879df2012-02-07 08:45:41 +08002184 return panel_height;
2185}
2186
2187static void
2188shell_surface_set_maximized(struct wl_client *client,
2189 struct wl_resource *resource,
2190 struct wl_resource *output_resource )
2191{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002192 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08002193 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03002194 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08002195 uint32_t edges = 0, panel_height = 0;
2196
2197 /* get the default output, if the client set it as NULL
2198 check whether the ouput is available */
2199 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002200 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002201 else if (es->output)
2202 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002203 else
2204 shsurf->output = get_default_output(es->compositor);
2205
Tiago Vignattibe143262012-04-16 17:31:41 +03002206 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01002207 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002208 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002209
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002210 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002211 shsurf->output->width,
2212 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002213
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002214 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002215}
2216
Alex Wu21858432012-04-01 20:13:08 +08002217static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002218black_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 +08002219
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002221create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002222 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002223 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002224{
2225 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002227
2228 surface = weston_surface_create(ec);
2229 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002230 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002231 return NULL;
2232 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002233 view = weston_view_create(surface);
2234 if (surface == NULL) {
2235 weston_log("no memory\n");
2236 weston_surface_destroy(surface);
2237 return NULL;
2238 }
Alex Wu4539b082012-03-01 12:57:46 +08002239
Alex Wu21858432012-04-01 20:13:08 +08002240 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002241 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002242 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002243 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002244 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002245 pixman_region32_fini(&surface->input);
2246 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002247
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248 weston_view_configure(view, x, y, w, h);
2249
2250 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002251}
2252
2253/* Create black surface and append it to the associated fullscreen surface.
2254 * Handle size dismatch and positioning according to the method. */
2255static void
2256shell_configure_fullscreen(struct shell_surface *shsurf)
2257{
2258 struct weston_output *output = shsurf->fullscreen_output;
2259 struct weston_surface *surface = shsurf->surface;
2260 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002261 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002262 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002263
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002264 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2265 restore_output_mode(output);
2266
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267 if (!shsurf->fullscreen.black_view)
2268 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002269 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002270 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002271 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002272 output->width,
2273 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002274
Jason Ekstranda7af7042013-10-12 22:38:11 -05002275 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2276 wl_list_insert(&shsurf->view->layer_link,
2277 &shsurf->fullscreen.black_view->layer_link);
2278 shsurf->fullscreen.black_view->surface->output = output;
2279 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002280
Jason Ekstranda7af7042013-10-12 22:38:11 -05002281 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002282 &surf_width, &surf_height);
2283
Alex Wu4539b082012-03-01 12:57:46 +08002284 switch (shsurf->fullscreen.type) {
2285 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002286 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002288 break;
2289 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002290 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002291 if (output->width == surf_width &&
2292 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 weston_view_set_position(shsurf->view,
2294 output->x - surf_x,
2295 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002296 break;
2297 }
2298
Alex Wu4539b082012-03-01 12:57:46 +08002299 matrix = &shsurf->fullscreen.transform.matrix;
2300 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002301
Scott Moreau1bad5db2012-08-18 01:04:05 -06002302 output_aspect = (float) output->width /
2303 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002304 /* XXX: Use surf_width and surf_height here? */
2305 surface_aspect = (float) surface->width /
2306 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002307 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002308 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002309 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002310 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002311 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002312 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002313
Alex Wu4539b082012-03-01 12:57:46 +08002314 weston_matrix_scale(matrix, scale, scale, 1);
2315 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002316 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002317 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002318 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2319 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002321
Alex Wu4539b082012-03-01 12:57:46 +08002322 break;
2323 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002324 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002325 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02002326 surf_width * surface->buffer_scale,
2327 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002328 shsurf->fullscreen.framerate};
2329
Hardening57388e42013-09-18 23:56:36 +02002330 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
2331 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002332 weston_view_set_position(shsurf->view,
2333 output->x - surf_x,
2334 output->y - surf_y);
2335 weston_view_configure(shsurf->fullscreen.black_view,
2336 output->x - surf_x,
2337 output->y - surf_y,
2338 output->width,
2339 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002340 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002341 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002342 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002343 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002344 }
Alex Wubd3354b2012-04-17 17:20:49 +08002345 }
Alex Wu4539b082012-03-01 12:57:46 +08002346 break;
2347 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002348 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002349 break;
2350 default:
2351 break;
2352 }
2353}
2354
2355/* make the fullscreen and black surface at the top */
2356static void
2357shell_stack_fullscreen(struct shell_surface *shsurf)
2358{
Alex Wubd3354b2012-04-17 17:20:49 +08002359 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002360 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002361
Jason Ekstranda7af7042013-10-12 22:38:11 -05002362 wl_list_remove(&shsurf->view->layer_link);
2363 wl_list_insert(&shell->fullscreen_layer.view_list,
2364 &shsurf->view->layer_link);
2365 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002366
Jason Ekstranda7af7042013-10-12 22:38:11 -05002367 if (!shsurf->fullscreen.black_view)
2368 shsurf->fullscreen.black_view =
2369 create_black_surface(shsurf->surface->compositor,
2370 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002371 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002372 output->width,
2373 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002374
Jason Ekstranda7af7042013-10-12 22:38:11 -05002375 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2376 wl_list_insert(&shsurf->view->layer_link,
2377 &shsurf->fullscreen.black_view->layer_link);
2378 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002379}
2380
2381static void
2382shell_map_fullscreen(struct shell_surface *shsurf)
2383{
Alex Wu4539b082012-03-01 12:57:46 +08002384 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002385 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002386}
2387
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002388static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002389set_fullscreen(struct shell_surface *shsurf,
2390 uint32_t method,
2391 uint32_t framerate,
2392 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002393{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002394 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002395
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002396 if (output)
2397 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002398 else if (es->output)
2399 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002400 else
2401 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002402
Alex Wu4539b082012-03-01 12:57:46 +08002403 shsurf->fullscreen_output = shsurf->output;
2404 shsurf->fullscreen.type = method;
2405 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002406 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002407
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002408 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002409 shsurf->output->width,
2410 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002411}
2412
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002413static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002414shell_surface_set_fullscreen(struct wl_client *client,
2415 struct wl_resource *resource,
2416 uint32_t method,
2417 uint32_t framerate,
2418 struct wl_resource *output_resource)
2419{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002420 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002421 struct weston_output *output;
2422
2423 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002424 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002425 else
2426 output = NULL;
2427
2428 set_fullscreen(shsurf, method, framerate, output);
2429}
2430
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002431static void
2432set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2433{
2434 /* XXX: using the same fields for transient type */
2435 shsurf->transient.x = x;
2436 shsurf->transient.y = y;
2437 shsurf->transient.flags = flags;
2438 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2439}
2440
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002441static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002442
2443static void
2444destroy_shell_seat(struct wl_listener *listener, void *data)
2445{
2446 struct shell_seat *shseat =
2447 container_of(listener,
2448 struct shell_seat, seat_destroy_listener);
2449 struct shell_surface *shsurf, *prev = NULL;
2450
2451 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002452 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002453 shseat->popup_grab.client = NULL;
2454
2455 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2456 shsurf->popup.shseat = NULL;
2457 if (prev) {
2458 wl_list_init(&prev->popup.grab_link);
2459 }
2460 prev = shsurf;
2461 }
2462 wl_list_init(&prev->popup.grab_link);
2463 }
2464
2465 wl_list_remove(&shseat->seat_destroy_listener.link);
2466 free(shseat);
2467}
2468
2469static struct shell_seat *
2470create_shell_seat(struct weston_seat *seat)
2471{
2472 struct shell_seat *shseat;
2473
2474 shseat = calloc(1, sizeof *shseat);
2475 if (!shseat) {
2476 weston_log("no memory to allocate shell seat\n");
2477 return NULL;
2478 }
2479
2480 shseat->seat = seat;
2481 wl_list_init(&shseat->popup_grab.surfaces_list);
2482
2483 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2484 wl_signal_add(&seat->destroy_signal,
2485 &shseat->seat_destroy_listener);
2486
2487 return shseat;
2488}
2489
2490static struct shell_seat *
2491get_shell_seat(struct weston_seat *seat)
2492{
2493 struct wl_listener *listener;
2494
2495 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2496 if (listener == NULL)
2497 return create_shell_seat(seat);
2498
2499 return container_of(listener,
2500 struct shell_seat, seat_destroy_listener);
2501}
2502
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002503static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002504popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002505{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002506 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002508 struct shell_seat *shseat =
2509 container_of(grab, struct shell_seat, popup_grab.grab);
2510 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002511 wl_fixed_t sx, sy;
2512
Jason Ekstranda7af7042013-10-12 22:38:11 -05002513 view = weston_compositor_pick_view(pointer->seat->compositor,
2514 pointer->x, pointer->y,
2515 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002516
Jason Ekstranda7af7042013-10-12 22:38:11 -05002517 if (view && view->surface->resource &&
2518 wl_resource_get_client(view->surface->resource) == client) {
2519 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002520 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002521 weston_pointer_set_focus(pointer, NULL,
2522 wl_fixed_from_int(0),
2523 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002524 }
2525}
2526
2527static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002528popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2529 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002530{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002531 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002532 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002533 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002534
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002535 weston_pointer_move(pointer, x, y);
2536
Neil Roberts96d790e2013-09-19 17:32:00 +01002537 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002538 weston_view_from_global_fixed(pointer->focus,
2539 pointer->x, pointer->y,
2540 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002541 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002542 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002543}
2544
2545static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002546popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002547 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002548{
2549 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002550 struct shell_seat *shseat =
2551 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002552 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002553 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002554 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002555 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002556
Neil Roberts96d790e2013-09-19 17:32:00 +01002557 resource_list = &grab->pointer->focus_resource_list;
2558 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002559 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002560 wl_resource_for_each(resource, resource_list) {
2561 wl_pointer_send_button(resource, serial,
2562 time, button, state);
2563 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002564 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002565 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002566 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002567 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002568 }
2569
Daniel Stone4dbadb12012-05-30 16:31:51 +01002570 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002571 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002572}
2573
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002574static void
2575popup_grab_cancel(struct weston_pointer_grab *grab)
2576{
2577 popup_grab_end(grab->pointer);
2578}
2579
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002580static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002581 popup_grab_focus,
2582 popup_grab_motion,
2583 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002584 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002585};
2586
2587static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002588popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002589{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002590 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002591 struct shell_seat *shseat =
2592 container_of(grab, struct shell_seat, popup_grab.grab);
2593 struct shell_surface *shsurf;
2594 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002595
2596 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002597 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002598 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002599 shseat->popup_grab.grab.interface = NULL;
2600 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002601 /* Send the popup_done event to all the popups open */
2602 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002603 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002604 shsurf->popup.shseat = NULL;
2605 if (prev) {
2606 wl_list_init(&prev->popup.grab_link);
2607 }
2608 prev = shsurf;
2609 }
2610 wl_list_init(&prev->popup.grab_link);
2611 wl_list_init(&shseat->popup_grab.surfaces_list);
2612 }
2613}
2614
2615static void
2616add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2617{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002618 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002619
2620 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002621 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002622 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002623 /* We must make sure here that this popup was opened after
2624 * a mouse press, and not just by moving around with other
2625 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002626 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002627 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002628
Rob Bradforddfe31052013-06-26 19:49:11 +01002629 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002630 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002631 } else {
2632 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002633 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002634}
2635
2636static void
2637remove_popup_grab(struct shell_surface *shsurf)
2638{
2639 struct shell_seat *shseat = shsurf->popup.shseat;
2640
2641 wl_list_remove(&shsurf->popup.grab_link);
2642 wl_list_init(&shsurf->popup.grab_link);
2643 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002644 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002645 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002646 }
2647}
2648
2649static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002650shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002651{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002652 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002653 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002654
Jason Ekstranda7af7042013-10-12 22:38:11 -05002655 shsurf->surface->output = parent_view->output;
2656 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002657
Jason Ekstranda7af7042013-10-12 22:38:11 -05002658 weston_view_set_transform_parent(shsurf->view, parent_view);
2659 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2660 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002661
Kristian Høgsberge3148752013-05-06 23:19:49 -04002662 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002663 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002664 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002665 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002666 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002667 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002668}
2669
2670static void
2671shell_surface_set_popup(struct wl_client *client,
2672 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002673 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002674 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002675 struct wl_resource *parent_resource,
2676 int32_t x, int32_t y, uint32_t flags)
2677{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002678 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002679
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002680 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002681 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002682 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002683 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002684 shsurf->popup.x = x;
2685 shsurf->popup.y = y;
2686}
2687
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002688static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002689 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002690 shell_surface_move,
2691 shell_surface_resize,
2692 shell_surface_set_toplevel,
2693 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002694 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002695 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002696 shell_surface_set_maximized,
2697 shell_surface_set_title,
2698 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002699};
2700
2701static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002702destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002703{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002704 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2705
Giulio Camuffo5085a752013-03-25 21:42:45 +01002706 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2707 remove_popup_grab(shsurf);
2708 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002709
Alex Wubd3354b2012-04-17 17:20:49 +08002710 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002711 shell_surface_is_top_fullscreen(shsurf))
2712 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002713
Jason Ekstranda7af7042013-10-12 22:38:11 -05002714 if (shsurf->fullscreen.black_view)
2715 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002716
Alex Wubd3354b2012-04-17 17:20:49 +08002717 /* As destroy_resource() use wl_list_for_each_safe(),
2718 * we can always remove the listener.
2719 */
2720 wl_list_remove(&shsurf->surface_destroy_listener.link);
2721 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002722 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002723 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002724
Jason Ekstranda7af7042013-10-12 22:38:11 -05002725 weston_view_destroy(shsurf->view);
2726
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002727 wl_list_remove(&shsurf->link);
2728 free(shsurf);
2729}
2730
2731static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002732shell_destroy_shell_surface(struct wl_resource *resource)
2733{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002734 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002735
2736 destroy_shell_surface(shsurf);
2737}
2738
2739static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002740shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002741{
2742 struct shell_surface *shsurf = container_of(listener,
2743 struct shell_surface,
2744 surface_destroy_listener);
2745
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002746 if (shsurf->resource)
2747 wl_resource_destroy(shsurf->resource);
2748 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002749 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002750}
2751
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002752static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002753shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002754
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002755static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002756get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002757{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002758 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002759 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002760 else
2761 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002762}
2763
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002764static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002765create_shell_surface(void *shell, struct weston_surface *surface,
2766 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002767{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002768 struct shell_surface *shsurf;
2769
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002770 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002771 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002772 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002773 }
2774
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002775 shsurf = calloc(1, sizeof *shsurf);
2776 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002777 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002778 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002779 }
2780
Jason Ekstranda7af7042013-10-12 22:38:11 -05002781 shsurf->view = weston_view_create(surface);
2782 if (!shsurf->view) {
2783 weston_log("no memory to allocate shell surface\n");
2784 free(shsurf);
2785 return NULL;
2786 }
2787
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002788 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002789 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002790
Tiago Vignattibc052c92012-04-19 16:18:18 +03002791 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002792 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002793 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002794 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002795 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002796 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2797 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002798 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002799 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002800 wl_list_init(&shsurf->fullscreen.transform.link);
2801
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002802 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002803 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002804 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002805 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002806
2807 /* init link so its safe to always remove it in destroy_shell_surface */
2808 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002809 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002810
Pekka Paalanen460099f2012-01-20 16:48:25 +02002811 /* empty when not in use */
2812 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002813 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002814
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002815 wl_list_init(&shsurf->workspace_transform.link);
2816
Pekka Paalanen98262232011-12-01 10:42:22 +02002817 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002818 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002819
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002820 shsurf->client = client;
2821
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002822 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002823}
2824
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825static struct weston_view *
2826get_primary_view(void *shell, struct shell_surface *shsurf)
2827{
2828 return shsurf->view;
2829}
2830
Tiago Vignattibc052c92012-04-19 16:18:18 +03002831static void
2832shell_get_shell_surface(struct wl_client *client,
2833 struct wl_resource *resource,
2834 uint32_t id,
2835 struct wl_resource *surface_resource)
2836{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002837 struct weston_surface *surface =
2838 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002839 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002840 struct shell_surface *shsurf;
2841
2842 if (get_shell_surface(surface)) {
2843 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002844 WL_DISPLAY_ERROR_INVALID_OBJECT,
2845 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002846 return;
2847 }
2848
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002849 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002850 if (!shsurf) {
2851 wl_resource_post_error(surface_resource,
2852 WL_DISPLAY_ERROR_INVALID_OBJECT,
2853 "surface->configure already set");
2854 return;
2855 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002856
Jason Ekstranda85118c2013-06-27 20:17:02 -05002857 shsurf->resource =
2858 wl_resource_create(client,
2859 &wl_shell_surface_interface, 1, id);
2860 wl_resource_set_implementation(shsurf->resource,
2861 &shell_surface_implementation,
2862 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002863}
2864
2865static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002866 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002867};
2868
Kristian Høgsberg07937562011-04-12 17:25:42 -04002869static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002870shell_fade(struct desktop_shell *shell, enum fade_type type);
2871
2872static int
2873screensaver_timeout(void *data)
2874{
2875 struct desktop_shell *shell = data;
2876
2877 shell_fade(shell, FADE_OUT);
2878
2879 return 1;
2880}
2881
2882static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002883handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002884{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002885 struct desktop_shell *shell =
2886 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002887
Pekka Paalanen18027e52011-12-02 16:31:49 +02002888 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002889
2890 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002891 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002892}
2893
2894static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002895launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002896{
2897 if (shell->screensaver.binding)
2898 return;
2899
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002900 if (!shell->screensaver.path) {
2901 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002902 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002903 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002904
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002905 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002906 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002907 return;
2908 }
2909
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002910 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002911 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002912 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002913 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002914}
2915
2916static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002917terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002918{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002919 if (shell->screensaver.process.pid == 0)
2920 return;
2921
2922 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002923}
2924
2925static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002926configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002927{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002929
Giulio Camuffo184df502013-02-21 11:29:21 +01002930 if (width == 0)
2931 return;
2932
Jason Ekstranda7af7042013-10-12 22:38:11 -05002933 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2934 if (v->output == ev->output && v != ev) {
2935 weston_view_unmap(v);
2936 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002937 }
2938 }
2939
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002941
Jason Ekstranda7af7042013-10-12 22:38:11 -05002942 if (wl_list_empty(&ev->layer_link)) {
2943 wl_list_insert(&layer->view_list, &ev->layer_link);
2944 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002945 }
2946}
2947
2948static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002949background_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 -04002950{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002951 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002953
Jason Ekstranda7af7042013-10-12 22:38:11 -05002954 view = container_of(es->views.next, struct weston_view, surface_link);
2955
2956 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002957}
2958
2959static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002960desktop_shell_set_background(struct wl_client *client,
2961 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002962 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002963 struct wl_resource *surface_resource)
2964{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002965 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002966 struct weston_surface *surface =
2967 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002968 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002969
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002970 if (surface->configure) {
2971 wl_resource_post_error(surface_resource,
2972 WL_DISPLAY_ERROR_INVALID_OBJECT,
2973 "surface role already assigned");
2974 return;
2975 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002976
Jason Ekstranda7af7042013-10-12 22:38:11 -05002977 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2978 weston_view_destroy(view);
2979 view = weston_view_create(surface);
2980
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002981 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002982 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002983 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002984 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002985 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002986 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002987 surface->output->width,
2988 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002989}
2990
2991static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002992panel_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 -04002993{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002994 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002995 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002996
Jason Ekstranda7af7042013-10-12 22:38:11 -05002997 view = container_of(es->views.next, struct weston_view, surface_link);
2998
2999 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003000}
3001
3002static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003003desktop_shell_set_panel(struct wl_client *client,
3004 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003005 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003006 struct wl_resource *surface_resource)
3007{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003008 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003009 struct weston_surface *surface =
3010 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003011 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003012
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003013 if (surface->configure) {
3014 wl_resource_post_error(surface_resource,
3015 WL_DISPLAY_ERROR_INVALID_OBJECT,
3016 "surface role already assigned");
3017 return;
3018 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003019
Jason Ekstranda7af7042013-10-12 22:38:11 -05003020 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3021 weston_view_destroy(view);
3022 view = weston_view_create(surface);
3023
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003024 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003025 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003026 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003027 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003028 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003029 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003030 surface->output->width,
3031 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003032}
3033
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003034static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003035lock_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 -04003036{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003037 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003038 struct weston_view *view;
3039
3040 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003041
Giulio Camuffo184df502013-02-21 11:29:21 +01003042 if (width == 0)
3043 return;
3044
Jason Ekstranda7af7042013-10-12 22:38:11 -05003045 surface->width = width;
3046 surface->height = height;
3047 view->geometry.width = width;
3048 view->geometry.height = height;
3049 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003050
3051 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003052 wl_list_insert(&shell->lock_layer.view_list,
3053 &view->layer_link);
3054 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003055 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003056 }
3057}
3058
3059static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003060handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003061{
Tiago Vignattibe143262012-04-16 17:31:41 +03003062 struct desktop_shell *shell =
3063 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003064
Martin Minarik6d118362012-06-07 18:01:59 +02003065 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003066 shell->lock_surface = NULL;
3067}
3068
3069static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003070desktop_shell_set_lock_surface(struct wl_client *client,
3071 struct wl_resource *resource,
3072 struct wl_resource *surface_resource)
3073{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003074 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003075 struct weston_surface *surface =
3076 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003077
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003078 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003079
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003080 if (!shell->locked)
3081 return;
3082
Pekka Paalanen98262232011-12-01 10:42:22 +02003083 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003084
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003085 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003086 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003087 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003088
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003089 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003090 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003091 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003092}
3093
3094static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003095resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003096{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003097 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003098
Pekka Paalanen77346a62011-11-30 16:26:35 +02003099 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003100
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003101 wl_list_remove(&shell->lock_layer.link);
3102 wl_list_insert(&shell->compositor->cursor_layer.link,
3103 &shell->fullscreen_layer.link);
3104 wl_list_insert(&shell->fullscreen_layer.link,
3105 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003106 if (shell->showing_input_panels) {
3107 wl_list_insert(&shell->panel_layer.link,
3108 &shell->input_panel_layer.link);
3109 wl_list_insert(&shell->input_panel_layer.link,
3110 &ws->layer.link);
3111 } else {
3112 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3113 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003114
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003115 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003116
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003117 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003118 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003119 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003120}
3121
3122static void
3123desktop_shell_unlock(struct wl_client *client,
3124 struct wl_resource *resource)
3125{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003126 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003127
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003128 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003129
3130 if (shell->locked)
3131 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003132}
3133
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003134static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003135desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003136 struct wl_resource *resource,
3137 struct wl_resource *surface_resource)
3138{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003139 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003140
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003141 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003142 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003143}
3144
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003145static void
3146desktop_shell_desktop_ready(struct wl_client *client,
3147 struct wl_resource *resource)
3148{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003149 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003150
3151 shell_fade_startup(shell);
3152}
3153
Kristian Høgsberg75840622011-09-06 13:48:16 -04003154static const struct desktop_shell_interface desktop_shell_implementation = {
3155 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003156 desktop_shell_set_panel,
3157 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003158 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003159 desktop_shell_set_grab_surface,
3160 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003161};
3162
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003163static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003164get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003165{
3166 struct shell_surface *shsurf;
3167
3168 shsurf = get_shell_surface(surface);
3169 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003170 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003171 return shsurf->type;
3172}
3173
Kristian Høgsberg75840622011-09-06 13:48:16 -04003174static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003175move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003176{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003177 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003178 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003179 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003180
Pekka Paalanen01388e22013-04-25 13:57:44 +03003181 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003182 if (surface == NULL)
3183 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003184
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003185 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003186 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3187 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003188 return;
3189
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003190 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003191}
3192
3193static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003194touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3195{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003196 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003197 struct weston_surface *surface;
3198 struct shell_surface *shsurf;
3199
3200 surface = weston_surface_get_main_surface(focus);
3201 if (surface == NULL)
3202 return;
3203
3204 shsurf = get_shell_surface(surface);
3205 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3206 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3207 return;
3208
3209 surface_touch_move(shsurf, (struct weston_seat *) seat);
3210}
3211
3212static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003213resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003214{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003215 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003216 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003217 uint32_t edges = 0;
3218 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003219 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003220
Pekka Paalanen01388e22013-04-25 13:57:44 +03003221 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003222 if (surface == NULL)
3223 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003224
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003225 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003226 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3227 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003228 return;
3229
Jason Ekstranda7af7042013-10-12 22:38:11 -05003230 weston_view_from_global(shsurf->view,
3231 wl_fixed_to_int(seat->pointer->grab_x),
3232 wl_fixed_to_int(seat->pointer->grab_y),
3233 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003234
Jason Ekstranda7af7042013-10-12 22:38:11 -05003235 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003236 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003237 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003238 edges |= 0;
3239 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003240 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003241
Jason Ekstranda7af7042013-10-12 22:38:11 -05003242 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003243 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003244 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003245 edges |= 0;
3246 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003247 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003248
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003249 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003250}
3251
3252static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003253surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003254 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003255{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003256 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003257 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003258 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003259 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003260
Pekka Paalanen01388e22013-04-25 13:57:44 +03003261 /* XXX: broken for windows containing sub-surfaces */
3262 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003263 if (surface == NULL)
3264 return;
3265
3266 shsurf = get_shell_surface(surface);
3267 if (!shsurf)
3268 return;
3269
Jason Ekstranda7af7042013-10-12 22:38:11 -05003270 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003271
Jason Ekstranda7af7042013-10-12 22:38:11 -05003272 if (shsurf->view->alpha > 1.0)
3273 shsurf->view->alpha = 1.0;
3274 if (shsurf->view->alpha < step)
3275 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003276
Jason Ekstranda7af7042013-10-12 22:38:11 -05003277 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003278 weston_surface_damage(surface);
3279}
3280
3281static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003282do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003283 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003284{
Daniel Stone37816df2012-05-16 18:45:18 +01003285 struct weston_seat *ws = (struct weston_seat *) seat;
3286 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003287 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003288 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003289
3290 wl_list_for_each(output, &compositor->output_list, link) {
3291 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003292 wl_fixed_to_double(seat->pointer->x),
3293 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003294 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003295 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003296 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003297 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003298 increment = -output->zoom.increment;
3299 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003300 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003301 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003302 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003303 else
3304 increment = 0;
3305
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003306 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003307
Scott Moreaue6603982012-06-11 13:07:51 -06003308 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003309 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003310 else if (output->zoom.level > output->zoom.max_level)
3311 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003312 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003313 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003314 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003315
Scott Moreaue6603982012-06-11 13:07:51 -06003316 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003317
Jason Ekstranda7af7042013-10-12 22:38:11 -05003318 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003319 }
3320 }
3321}
3322
Scott Moreauccbf29d2012-02-22 14:21:41 -07003323static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003324zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003325 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003326{
3327 do_zoom(seat, time, 0, axis, value);
3328}
3329
3330static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003331zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003332 void *data)
3333{
3334 do_zoom(seat, time, key, 0, 0);
3335}
3336
3337static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003338terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003339 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003340{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003341 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003342
Daniel Stone325fc2d2012-05-30 16:31:58 +01003343 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003344}
3345
3346static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003347lower_fullscreen_layer(struct desktop_shell *shell);
3348
3349struct alt_tab {
3350 struct desktop_shell *shell;
3351 struct weston_keyboard_grab grab;
3352
3353 struct wl_list *current;
3354
3355 struct wl_list preview_list;
3356};
3357
3358struct alt_tab_preview {
3359 struct alt_tab *alt_tab;
3360
3361 struct weston_view *view;
3362 struct weston_transform transform;
3363
3364 struct wl_listener listener;
3365
3366 struct wl_list link;
3367};
3368
3369static void
3370alt_tab_next(struct alt_tab *alt_tab)
3371{
3372 alt_tab->current = alt_tab->current->next;
3373
3374 /* Make sure we're not pointing to the list header e.g. after
3375 * cycling through the whole list. */
3376 if (alt_tab->current->next == alt_tab->preview_list.next)
3377 alt_tab->current = alt_tab->current->next;
3378}
3379
3380static void
3381alt_tab_destroy(struct alt_tab *alt_tab)
3382{
3383 struct alt_tab_preview *preview, *next;
3384 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3385
3386 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3387 preview = wl_container_of(alt_tab->current, preview, link);
3388
3389 activate(alt_tab->shell, preview->view->surface,
3390 (struct weston_seat *) keyboard->seat);
3391 }
3392
3393 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3394 wl_list_remove(&preview->link);
3395 wl_list_remove(&preview->listener.link);
3396 weston_view_destroy(preview->view);
3397 free(preview);
3398 }
3399
3400 weston_keyboard_end_grab(keyboard);
3401 if (keyboard->input_method_resource)
3402 keyboard->grab = &keyboard->input_method_grab;
3403
3404 free(alt_tab);
3405}
3406
3407static void
3408alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3409{
3410 struct alt_tab_preview *preview =
3411 container_of(listener, struct alt_tab_preview, listener);
3412 struct alt_tab *alt_tab = preview->alt_tab;
3413 int advance = 0;
3414
3415 /* If the preview that we're removing is the currently selected one,
3416 * we want to move to the next one. So we move to ->prev and then
3417 * call _next() after removing the preview. */
3418 if (alt_tab->current == &preview->link) {
3419 alt_tab->current = alt_tab->current->prev;
3420 advance = 1;
3421 }
3422
3423 wl_list_remove(&preview->listener.link);
3424 wl_list_remove(&preview->link);
3425
3426 free(preview);
3427
3428 if (advance)
3429 alt_tab_next(alt_tab);
3430
3431 /* If the last preview goes away, stop the alt-tab */
3432 if (wl_list_empty(alt_tab->current))
3433 alt_tab_destroy(alt_tab);
3434}
3435
3436static void
3437alt_tab_key(struct weston_keyboard_grab *grab,
3438 uint32_t time, uint32_t key, uint32_t state_w)
3439{
3440 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3441 enum wl_keyboard_key_state state = state_w;
3442
3443 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3444 alt_tab_next(alt_tab);
3445}
3446
3447static void
3448alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3449 uint32_t mods_depressed, uint32_t mods_latched,
3450 uint32_t mods_locked, uint32_t group)
3451{
3452 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3453 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3454
3455 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3456 alt_tab_destroy(alt_tab);
3457}
3458
3459static void
3460alt_tab_cancel(struct weston_keyboard_grab *grab)
3461{
3462 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3463
3464 alt_tab_destroy(alt_tab);
3465}
3466
3467static const struct weston_keyboard_grab_interface alt_tab_grab = {
3468 alt_tab_key,
3469 alt_tab_modifier,
3470 alt_tab_cancel,
3471};
3472
3473static int
3474view_for_alt_tab(struct weston_view *view)
3475{
3476 if (!get_shell_surface(view->surface))
3477 return 0;
3478
3479 if (get_shell_surface_type(view->surface) == SHELL_SURFACE_TRANSIENT)
3480 return 0;
3481
3482 if (view != get_default_view(view->surface))
3483 return 0;
3484
3485 return 1;
3486}
3487
3488static void
3489alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3490 void *data)
3491{
3492 struct alt_tab *alt_tab;
3493 struct desktop_shell *shell = data;
3494 struct weston_output *output = get_default_output(shell->compositor);
3495 struct workspace *ws;
3496 struct weston_view *view;
3497 int num_surfaces = 0;
3498 int x, y, side, margin;
3499
3500 wl_list_for_each(view, &shell->compositor->view_list, link) {
3501
3502 if (view_for_alt_tab(view))
3503 num_surfaces++;
3504 }
3505
3506 if (!num_surfaces)
3507 return;
3508
3509 alt_tab = malloc(sizeof *alt_tab);
3510 if (!alt_tab)
3511 return;
3512
3513 alt_tab->shell = shell;
3514 wl_list_init(&alt_tab->preview_list);
3515 alt_tab->current = &alt_tab->preview_list;
3516
3517 restore_all_output_modes(shell->compositor);
3518 lower_fullscreen_layer(alt_tab->shell);
3519
3520 alt_tab->grab.interface = &alt_tab_grab;
3521 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3522 weston_keyboard_set_focus(seat->keyboard, NULL);
3523
3524 ws = get_current_workspace(shell);
3525
3526 /* FIXME: add some visual candy e.g. prelight the selected view
3527 * and/or add a black surrounding background à la gnome-shell */
3528
3529 /* Determine the size for each preview */
3530 side = output->width / num_surfaces;
3531 if (side > 200)
3532 side = 200;
3533 margin = side / 4;
3534 side -= margin;
3535
3536 x = margin;
3537 y = (output->height - side) / 2;
3538
3539 /* Create a view for each surface */
3540 wl_list_for_each(view, &shell->compositor->view_list, link) {
3541 struct alt_tab_preview *preview;
3542 struct weston_view *v;
3543 float scale;
3544
3545 if (!view_for_alt_tab(view))
3546 continue;
3547
3548 preview = malloc(sizeof *preview);
3549 if (!preview) {
3550 alt_tab_destroy(alt_tab);
3551 return;
3552 }
3553
3554 preview->alt_tab = alt_tab;
3555
3556 preview->view = v = weston_view_create(view->surface);
3557 v->output = view->output;
3558 weston_view_restack(v, &ws->layer.view_list);
3559 weston_view_configure(v, x, y, view->geometry.width, view->geometry.height);
3560
3561 preview->listener.notify = alt_tab_handle_surface_destroy;
3562 wl_signal_add(&v->destroy_signal, &preview->listener);
3563
3564 if (view->geometry.width > view->geometry.height)
3565 scale = side / (float) view->geometry.width;
3566 else
3567 scale = side / (float) view->geometry.height;
3568
3569 wl_list_insert(&v->geometry.transformation_list,
3570 &preview->transform.link);
3571 weston_matrix_init(&preview->transform.matrix);
3572 weston_matrix_scale(&preview->transform.matrix,
3573 scale, scale, 1.0f);
3574
3575 weston_view_geometry_dirty(v);
3576 weston_compositor_schedule_repaint(v->surface->compositor);
3577
3578 /* Insert at the end of the list */
3579 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3580
3581 x += side + margin;
3582 }
3583
3584 /* Start at the second preview so a simple <alt>tab changes window.
3585 * We set `current' to the first preview and not the second because
3586 * we're going to receive a key press callback for the initial
3587 * <alt>tab which will make `current' point to the second element. */
3588 alt_tab_next(alt_tab);
3589}
3590
3591static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003592rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3593 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003594{
3595 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003596 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003597 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003598 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003599 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003600
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003601 weston_pointer_move(pointer, x, y);
3602
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003603 if (!shsurf)
3604 return;
3605
Jason Ekstranda7af7042013-10-12 22:38:11 -05003606 cx = 0.5f * shsurf->view->geometry.width;
3607 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003608
Daniel Stone37816df2012-05-16 18:45:18 +01003609 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3610 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003611 r = sqrtf(dx * dx + dy * dy);
3612
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003613 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003614 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003615
3616 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003617 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003618 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003619
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003620 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003621 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003622
3623 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003624 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003625 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003626 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003627 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003628
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003629 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003630 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003631 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003632 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003633 wl_list_init(&shsurf->rotation.transform.link);
3634 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003635 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003636 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003637
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003638 /* We need to adjust the position of the surface
3639 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003640 cposx = shsurf->view->geometry.x + cx;
3641 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003642 dposx = rotate->center.x - cposx;
3643 dposy = rotate->center.y - cposy;
3644 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003645 weston_view_set_position(shsurf->view,
3646 shsurf->view->geometry.x + dposx,
3647 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003648 }
3649
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003650 /* Repaint implies weston_surface_update_transform(), which
3651 * lazily applies the damage due to rotation update.
3652 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003653 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003654}
3655
3656static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003657rotate_grab_button(struct weston_pointer_grab *grab,
3658 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003659{
3660 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003661 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003662 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003663 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003664 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003665
Daniel Stone4dbadb12012-05-30 16:31:51 +01003666 if (pointer->button_count == 0 &&
3667 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003668 if (shsurf)
3669 weston_matrix_multiply(&shsurf->rotation.rotation,
3670 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003671 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003672 free(rotate);
3673 }
3674}
3675
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003676static void
3677rotate_grab_cancel(struct weston_pointer_grab *grab)
3678{
3679 struct rotate_grab *rotate =
3680 container_of(grab, struct rotate_grab, base.grab);
3681
3682 shell_grab_end(&rotate->base);
3683 free(rotate);
3684}
3685
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003686static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003687 noop_grab_focus,
3688 rotate_grab_motion,
3689 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003690 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003691};
3692
3693static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003694surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003695{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003696 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003697 float dx, dy;
3698 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003699
Pekka Paalanen460099f2012-01-20 16:48:25 +02003700 rotate = malloc(sizeof *rotate);
3701 if (!rotate)
3702 return;
3703
Jason Ekstranda7af7042013-10-12 22:38:11 -05003704 weston_view_to_global_float(surface->view,
3705 surface->view->geometry.width * 0.5f,
3706 surface->view->geometry.height * 0.5f,
3707 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003708
Daniel Stone37816df2012-05-16 18:45:18 +01003709 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3710 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003711 r = sqrtf(dx * dx + dy * dy);
3712 if (r > 20.0f) {
3713 struct weston_matrix inverse;
3714
3715 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003716 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003717 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003718
3719 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003720 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003721 } else {
3722 weston_matrix_init(&surface->rotation.rotation);
3723 weston_matrix_init(&rotate->rotation);
3724 }
3725
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003726 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3727 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003728}
3729
3730static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003731rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003732 void *data)
3733{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003734 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003735 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003736 struct shell_surface *surface;
3737
Pekka Paalanen01388e22013-04-25 13:57:44 +03003738 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003739 if (base_surface == NULL)
3740 return;
3741
3742 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003743 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3744 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003745 return;
3746
3747 surface_rotate(surface, seat);
3748}
3749
3750static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003751lower_fullscreen_layer(struct desktop_shell *shell)
3752{
3753 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003754 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003755
3756 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003757 wl_list_for_each_reverse_safe(view, prev,
3758 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003759 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003760 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003761}
3762
3763static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003764activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003765 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003766{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003767 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003768 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003769 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003770 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003771 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003772
Pekka Paalanen01388e22013-04-25 13:57:44 +03003773 main_surface = weston_surface_get_main_surface(es);
3774
Daniel Stone37816df2012-05-16 18:45:18 +01003775 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003776
Jonas Ådahl8538b222012-08-29 22:13:03 +02003777 state = ensure_focus_state(shell, seat);
3778 if (state == NULL)
3779 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003780
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003781 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003782 state->keyboard_focus = es;
3783 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003784 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003785
Pekka Paalanen01388e22013-04-25 13:57:44 +03003786 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003787 case SHELL_SURFACE_FULLSCREEN:
3788 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003789 shell_stack_fullscreen(get_shell_surface(main_surface));
3790 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003791 return;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003792 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003793 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003794 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003795 main_view = get_default_view(main_surface);
3796 if (main_view)
3797 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003798 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003799 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003800
3801 if (shell->focus_animation_type != ANIMATION_NONE)
3802 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Kristian Høgsberg75840622011-09-06 13:48:16 -04003803}
3804
Alex Wu21858432012-04-01 20:13:08 +08003805/* no-op func for checking black surface */
3806static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003807black_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 +08003808{
3809}
3810
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003811static bool
Alex Wu21858432012-04-01 20:13:08 +08003812is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3813{
3814 if (es->configure == black_surface_configure) {
3815 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003816 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003817 return true;
3818 }
3819 return false;
3820}
3821
Kristian Høgsberg75840622011-09-06 13:48:16 -04003822static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003823activate_binding(struct weston_seat *seat,
3824 struct desktop_shell *shell,
3825 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003826{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003827 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003828
Alex Wu9c35e6b2012-03-05 14:13:13 +08003829 if (!focus)
3830 return;
3831
Pekka Paalanen01388e22013-04-25 13:57:44 +03003832 if (is_black_surface(focus, &main_surface))
3833 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003834
Pekka Paalanen01388e22013-04-25 13:57:44 +03003835 main_surface = weston_surface_get_main_surface(focus);
3836 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003837 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003838
Neil Robertsa28c6932013-10-03 16:43:04 +01003839 activate(shell, focus, seat);
3840}
3841
3842static void
3843click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3844 void *data)
3845{
3846 if (seat->pointer->grab != &seat->pointer->default_grab)
3847 return;
3848
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003849 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003850}
3851
3852static void
3853touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3854{
3855 if (seat->touch->grab != &seat->touch->default_grab)
3856 return;
3857
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003858 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003859}
3860
3861static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003862lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003863{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003864 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003865
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003866 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003867 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003868 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003869 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003870
3871 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003872
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003873 /* Hide all surfaces by removing the fullscreen, panel and
3874 * toplevel layers. This way nothing else can show or receive
3875 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003876
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003877 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003878 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003879 if (shell->showing_input_panels)
3880 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003881 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003882 wl_list_insert(&shell->compositor->cursor_layer.link,
3883 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003884
Pekka Paalanen77346a62011-11-30 16:26:35 +02003885 launch_screensaver(shell);
3886
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003887 /* TODO: disable bindings that should not work while locked. */
3888
3889 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003890}
3891
3892static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003893unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003894{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003895 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003896 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003897 return;
3898 }
3899
3900 /* If desktop-shell client has gone away, unlock immediately. */
3901 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003902 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003903 return;
3904 }
3905
3906 if (shell->prepare_event_sent)
3907 return;
3908
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003909 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003910 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003911}
3912
3913static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003914shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003915{
3916 struct desktop_shell *shell = data;
3917
3918 shell->fade.animation = NULL;
3919
3920 switch (shell->fade.type) {
3921 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003922 weston_surface_destroy(shell->fade.view->surface);
3923 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003924 break;
3925 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003926 lock(shell);
3927 break;
3928 }
3929}
3930
Jason Ekstranda7af7042013-10-12 22:38:11 -05003931static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003932shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003933{
3934 struct weston_compositor *compositor = shell->compositor;
3935 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003936 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003937
3938 surface = weston_surface_create(compositor);
3939 if (!surface)
3940 return NULL;
3941
Jason Ekstranda7af7042013-10-12 22:38:11 -05003942 view = weston_view_create(surface);
3943 if (!view) {
3944 weston_surface_destroy(surface);
3945 return NULL;
3946 }
3947
3948 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003949 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003950 wl_list_insert(&compositor->fade_layer.view_list,
3951 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003952 pixman_region32_init(&surface->input);
3953
Jason Ekstranda7af7042013-10-12 22:38:11 -05003954 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003955}
3956
3957static void
3958shell_fade(struct desktop_shell *shell, enum fade_type type)
3959{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003960 float tint;
3961
3962 switch (type) {
3963 case FADE_IN:
3964 tint = 0.0;
3965 break;
3966 case FADE_OUT:
3967 tint = 1.0;
3968 break;
3969 default:
3970 weston_log("shell: invalid fade type\n");
3971 return;
3972 }
3973
3974 shell->fade.type = type;
3975
Jason Ekstranda7af7042013-10-12 22:38:11 -05003976 if (shell->fade.view == NULL) {
3977 shell->fade.view = shell_fade_create_surface(shell);
3978 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003979 return;
3980
Jason Ekstranda7af7042013-10-12 22:38:11 -05003981 shell->fade.view->alpha = 1.0 - tint;
3982 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003983 }
3984
3985 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003986 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003987 else
3988 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003989 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003990 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003991 shell_fade_done, shell);
3992}
3993
3994static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003995do_shell_fade_startup(void *data)
3996{
3997 struct desktop_shell *shell = data;
3998
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003999 if (shell->startup_animation_type == ANIMATION_FADE)
4000 shell_fade(shell, FADE_IN);
4001 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004002 weston_surface_destroy(shell->fade.view->surface);
4003 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004004 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004005}
4006
4007static void
4008shell_fade_startup(struct desktop_shell *shell)
4009{
4010 struct wl_event_loop *loop;
4011
4012 if (!shell->fade.startup_timer)
4013 return;
4014
4015 wl_event_source_remove(shell->fade.startup_timer);
4016 shell->fade.startup_timer = NULL;
4017
4018 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4019 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4020}
4021
4022static int
4023fade_startup_timeout(void *data)
4024{
4025 struct desktop_shell *shell = data;
4026
4027 shell_fade_startup(shell);
4028 return 0;
4029}
4030
4031static void
4032shell_fade_init(struct desktop_shell *shell)
4033{
4034 /* Make compositor output all black, and wait for the desktop-shell
4035 * client to signal it is ready, then fade in. The timer triggers a
4036 * fade-in, in case the desktop-shell client takes too long.
4037 */
4038
4039 struct wl_event_loop *loop;
4040
Jason Ekstranda7af7042013-10-12 22:38:11 -05004041 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004042 weston_log("%s: warning: fade surface already exists\n",
4043 __func__);
4044 return;
4045 }
4046
Jason Ekstranda7af7042013-10-12 22:38:11 -05004047 shell->fade.view = shell_fade_create_surface(shell);
4048 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004049 return;
4050
Jason Ekstranda7af7042013-10-12 22:38:11 -05004051 weston_view_update_transform(shell->fade.view);
4052 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004053
4054 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4055 shell->fade.startup_timer =
4056 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4057 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4058}
4059
4060static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004061idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004062{
4063 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004064 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004065
4066 shell_fade(shell, FADE_OUT);
4067 /* lock() is called from shell_fade_done() */
4068}
4069
4070static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004071wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004072{
4073 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004074 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004075
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004076 unlock(shell);
4077}
4078
4079static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004080show_input_panels(struct wl_listener *listener, void *data)
4081{
4082 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004083 container_of(listener, struct desktop_shell,
4084 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004085 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004086
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004087 shell->text_input.surface = (struct weston_surface*)data;
4088
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004089 if (shell->showing_input_panels)
4090 return;
4091
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004092 shell->showing_input_panels = true;
4093
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004094 if (!shell->locked)
4095 wl_list_insert(&shell->panel_layer.link,
4096 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004097
Jason Ekstranda7af7042013-10-12 22:38:11 -05004098 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004099 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004100 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004101 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004102 wl_list_insert(&shell->input_panel_layer.view_list,
4103 &ipsurf->view->layer_link);
4104 weston_view_geometry_dirty(ipsurf->view);
4105 weston_view_update_transform(ipsurf->view);
4106 weston_surface_damage(ipsurf->surface);
4107 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
4108 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004109 }
4110}
4111
4112static void
4113hide_input_panels(struct wl_listener *listener, void *data)
4114{
4115 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004116 container_of(listener, struct desktop_shell,
4117 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004118 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004119
Jan Arne Petersen61381972013-01-31 15:52:21 +01004120 if (!shell->showing_input_panels)
4121 return;
4122
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004123 shell->showing_input_panels = false;
4124
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004125 if (!shell->locked)
4126 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004127
Jason Ekstranda7af7042013-10-12 22:38:11 -05004128 wl_list_for_each_safe(view, next,
4129 &shell->input_panel_layer.view_list, layer_link)
4130 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004131}
4132
4133static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004134update_input_panels(struct wl_listener *listener, void *data)
4135{
4136 struct desktop_shell *shell =
4137 container_of(listener, struct desktop_shell,
4138 update_input_panel_listener);
4139
4140 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4141}
4142
4143static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004144center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004145{
Giulio Camuffob8366642013-04-25 13:57:46 +03004146 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004147 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004148
Jason Ekstranda7af7042013-10-12 22:38:11 -05004149 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004150
4151 x = output->x + (output->width - width) / 2 - surf_x / 2;
4152 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004153
Jason Ekstranda7af7042013-10-12 22:38:11 -05004154 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004155}
4156
4157static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004158weston_view_set_initial_position(struct weston_view *view,
4159 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004160{
4161 struct weston_compositor *compositor = shell->compositor;
4162 int ix = 0, iy = 0;
4163 int range_x, range_y;
4164 int dx, dy, x, y, panel_height;
4165 struct weston_output *output, *target_output = NULL;
4166 struct weston_seat *seat;
4167
4168 /* As a heuristic place the new window on the same output as the
4169 * pointer. Falling back to the output containing 0, 0.
4170 *
4171 * TODO: Do something clever for touch too?
4172 */
4173 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004174 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004175 ix = wl_fixed_to_int(seat->pointer->x);
4176 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004177 break;
4178 }
4179 }
4180
4181 wl_list_for_each(output, &compositor->output_list, link) {
4182 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4183 target_output = output;
4184 break;
4185 }
4186 }
4187
4188 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004189 weston_view_set_position(view, 10 + random() % 400,
4190 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004191 return;
4192 }
4193
4194 /* Valid range within output where the surface will still be onscreen.
4195 * If this is negative it means that the surface is bigger than
4196 * output.
4197 */
4198 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004199 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004200 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05004201 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004202
Rob Bradford4cb88c72012-08-13 15:18:44 +01004203 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004204 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004205 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004206 dx = 0;
4207
4208 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004209 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004210 else
4211 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004212
4213 x = target_output->x + dx;
4214 y = target_output->y + dy;
4215
Jason Ekstranda7af7042013-10-12 22:38:11 -05004216 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004217}
4218
4219static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004220map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004221 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004222{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004223 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004224 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01004225 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004226 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08004227 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004228 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004229
Jason Ekstranda7af7042013-10-12 22:38:11 -05004230 shsurf->view->geometry.width = width;
4231 shsurf->view->geometry.height = height;
4232 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004233
4234 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004235 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02004236 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004237 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004238 break;
Alex Wu4539b082012-03-01 12:57:46 +08004239 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004240 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08004241 shell_map_fullscreen(shsurf);
4242 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004243 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004244 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004245 panel_height = get_output_panel_height(shell, shsurf->output);
4246 surface_subsurfaces_boundingbox(shsurf->surface,
4247 &surf_x, &surf_y, NULL, NULL);
4248 weston_view_set_position(shsurf->view,
4249 shsurf->output->x - surf_x,
4250 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08004251 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004252 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004253 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004254 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004255 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004256 weston_view_set_position(shsurf->view,
4257 shsurf->view->geometry.x + sx,
4258 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004259 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004260 default:
4261 ;
4262 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004263
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02004264 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004265 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004266 case SHELL_SURFACE_POPUP:
4267 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004268 /* TODO: Handle a parent with multiple views */
4269 parent = get_default_view(shsurf->parent);
4270 if (parent) {
4271 wl_list_remove(&shsurf->view->layer_link);
4272 wl_list_insert(parent->layer_link.prev,
4273 &shsurf->view->layer_link);
4274 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004275 break;
Alex Wu4539b082012-03-01 12:57:46 +08004276 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02004277 case SHELL_SURFACE_NONE:
4278 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004279 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004280 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004281 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004282 wl_list_remove(&shsurf->view->layer_link);
4283 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004284 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004285 }
4286
Jason Ekstranda7af7042013-10-12 22:38:11 -05004287 if (shsurf->type != SHELL_SURFACE_NONE) {
4288 weston_view_update_transform(shsurf->view);
4289 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4290 shsurf->surface->output = shsurf->output;
4291 shsurf->view->output = shsurf->output;
4292 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004293 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004294
Jason Ekstranda7af7042013-10-12 22:38:11 -05004295 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004296 /* XXX: xwayland's using the same fields for transient type */
4297 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004298 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004299 if (shsurf->transient.flags ==
4300 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4301 break;
4302 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004303 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004304 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004305 if (!shell->locked) {
4306 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004307 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004308 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004309 break;
4310 default:
4311 break;
4312 }
4313
Jason Ekstranda7af7042013-10-12 22:38:11 -05004314 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004315 {
4316 switch (shell->win_animation_type) {
4317 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004318 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004319 break;
4320 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004321 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004322 break;
4323 default:
4324 break;
4325 }
4326 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004327}
4328
4329static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004330configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004331 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004332{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004333 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4334 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004335 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03004336 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004337
Pekka Paalanen77346a62011-11-30 16:26:35 +02004338 shsurf = get_shell_surface(surface);
4339 if (shsurf)
4340 surface_type = shsurf->type;
4341
Jason Ekstranda7af7042013-10-12 22:38:11 -05004342 /* TODO:
4343 * This should probably be changed to be more shell_surface
4344 * dependent
4345 */
4346 wl_list_for_each(view, &surface->views, surface_link)
4347 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004348
4349 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004350 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08004351 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004352 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004353 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004354 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004355 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004356 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4357 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004358 shsurf->view->geometry.x = shsurf->output->x - surf_x;
4359 shsurf->view->geometry.y = shsurf->output->y +
4360 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08004361 break;
Alex Wu4539b082012-03-01 12:57:46 +08004362 case SHELL_SURFACE_TOPLEVEL:
4363 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004364 default:
4365 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004366 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004367
Alex Wu4539b082012-03-01 12:57:46 +08004368 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004369 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004370 wl_list_for_each(view, &surface->views, surface_link)
4371 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004372
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004373 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004374 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004375 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004376}
4377
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004378static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004379shell_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 +03004380{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004381 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004382 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004383
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004384 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004385
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004386 if (!weston_surface_is_mapped(es) &&
4387 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004388 remove_popup_grab(shsurf);
4389 }
4390
Giulio Camuffo184df502013-02-21 11:29:21 +01004391 if (width == 0)
4392 return;
4393
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004394 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004395 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004396 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004397 type_changed = 1;
4398 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004399
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004400 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004401 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004402 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05004403 shsurf->view->geometry.width != width ||
4404 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004405 float from_x, from_y;
4406 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004407
Jason Ekstranda7af7042013-10-12 22:38:11 -05004408 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4409 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004410 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 shsurf->view->geometry.x + to_x - from_x,
4412 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004413 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004414 }
4415}
4416
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004417static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004418
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004419static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004420desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004421{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004422 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004423 struct desktop_shell *shell =
4424 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004425
4426 shell->child.process.pid = 0;
4427 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004428
4429 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4430 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004431 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004432 shell->child.deathstamp = time;
4433 shell->child.deathcount = 0;
4434 }
4435
4436 shell->child.deathcount++;
4437 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02004438 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004439 return;
4440 }
4441
Martin Minarik6d118362012-06-07 18:01:59 +02004442 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004443 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004444 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004445}
4446
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004447static void
4448launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004449{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004450 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05004451 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004452
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004453 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004454 &shell->child.process,
4455 shell_exe,
4456 desktop_shell_sigchld);
4457
4458 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004459 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004460}
4461
4462static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004463bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4464{
Tiago Vignattibe143262012-04-16 17:31:41 +03004465 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004466 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004467
Jason Ekstranda85118c2013-06-27 20:17:02 -05004468 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4469 if (resource)
4470 wl_resource_set_implementation(resource, &shell_implementation,
4471 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004472}
4473
Kristian Høgsberg75840622011-09-06 13:48:16 -04004474static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004475unbind_desktop_shell(struct wl_resource *resource)
4476{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004477 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004478
4479 if (shell->locked)
4480 resume_desktop(shell);
4481
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004482 shell->child.desktop_shell = NULL;
4483 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004484}
4485
4486static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004487bind_desktop_shell(struct wl_client *client,
4488 void *data, uint32_t version, uint32_t id)
4489{
Tiago Vignattibe143262012-04-16 17:31:41 +03004490 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004491 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004492
Jason Ekstranda85118c2013-06-27 20:17:02 -05004493 resource = wl_resource_create(client, &desktop_shell_interface,
4494 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004495
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004496 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004497 wl_resource_set_implementation(resource,
4498 &desktop_shell_implementation,
4499 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004500 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004501
4502 if (version < 2)
4503 shell_fade_startup(shell);
4504
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004505 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004506 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004507
4508 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4509 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004510 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004511}
4512
Pekka Paalanen6e168112011-11-24 11:34:05 +02004513static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004514screensaver_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 -04004515{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004516 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004517 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004518
Giulio Camuffo184df502013-02-21 11:29:21 +01004519 if (width == 0)
4520 return;
4521
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004522 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004523 if (!shell->locked)
4524 return;
4525
Jason Ekstranda7af7042013-10-12 22:38:11 -05004526 view = container_of(surface->views.next, struct weston_view, surface_link);
4527 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004528
Jason Ekstranda7af7042013-10-12 22:38:11 -05004529 if (wl_list_empty(&view->layer_link)) {
4530 wl_list_insert(shell->lock_layer.view_list.prev,
4531 &view->layer_link);
4532 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004533 wl_event_source_timer_update(shell->screensaver.timer,
4534 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004535 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004536 }
4537}
4538
4539static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004540screensaver_set_surface(struct wl_client *client,
4541 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004542 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004543 struct wl_resource *output_resource)
4544{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004545 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004546 struct weston_surface *surface =
4547 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004548 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004549 struct weston_view *view, *next;
4550
4551 /* Make sure we only have one view */
4552 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4553 weston_view_destroy(view);
4554 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004555
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004556 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004557 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004558 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004559}
4560
4561static const struct screensaver_interface screensaver_implementation = {
4562 screensaver_set_surface
4563};
4564
4565static void
4566unbind_screensaver(struct wl_resource *resource)
4567{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004568 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004569
Pekka Paalanen77346a62011-11-30 16:26:35 +02004570 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004571}
4572
4573static void
4574bind_screensaver(struct wl_client *client,
4575 void *data, uint32_t version, uint32_t id)
4576{
Tiago Vignattibe143262012-04-16 17:31:41 +03004577 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004578 struct wl_resource *resource;
4579
Jason Ekstranda85118c2013-06-27 20:17:02 -05004580 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004581
Pekka Paalanen77346a62011-11-30 16:26:35 +02004582 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004583 wl_resource_set_implementation(resource,
4584 &screensaver_implementation,
4585 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004586 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004587 return;
4588 }
4589
4590 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4591 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004592 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004593}
4594
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004595static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004596input_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 -04004597{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004598 struct input_panel_surface *ip_surface = surface->configure_private;
4599 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004600 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004601 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004602
Giulio Camuffo184df502013-02-21 11:29:21 +01004603 if (width == 0)
4604 return;
4605
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004606 if (!weston_surface_is_mapped(surface)) {
4607 if (!shell->showing_input_panels)
4608 return;
4609
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004610 show_surface = 1;
4611 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004612
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004613 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004614
4615 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004616 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4617 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004618 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004619 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4620 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004621 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004622
Jason Ekstranda7af7042013-10-12 22:38:11 -05004623 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004624
4625 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004626 wl_list_insert(&shell->input_panel_layer.view_list,
4627 &ip_surface->view->layer_link);
4628 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004629 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004630 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004631 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004632}
4633
4634static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004635destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004636{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004637 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4638
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004639 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004640 wl_list_remove(&input_panel_surface->link);
4641
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004642 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004643 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004644
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004645 free(input_panel_surface);
4646}
4647
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004648static struct input_panel_surface *
4649get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004650{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004651 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004652 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004653 } else {
4654 return NULL;
4655 }
4656}
4657
4658static void
4659input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4660{
4661 struct input_panel_surface *ipsurface = container_of(listener,
4662 struct input_panel_surface,
4663 surface_destroy_listener);
4664
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004665 if (ipsurface->resource) {
4666 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004667 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004668 destroy_input_panel_surface(ipsurface);
4669 }
4670}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004671
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004672static struct input_panel_surface *
4673create_input_panel_surface(struct desktop_shell *shell,
4674 struct weston_surface *surface)
4675{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004676 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004677
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004678 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4679 if (!input_panel_surface)
4680 return NULL;
4681
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004682 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004683 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004684
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004685 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004686
4687 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004688 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004689
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004690 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004691 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004692 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004693 &input_panel_surface->surface_destroy_listener);
4694
4695 wl_list_init(&input_panel_surface->link);
4696
4697 return input_panel_surface;
4698}
4699
4700static void
4701input_panel_surface_set_toplevel(struct wl_client *client,
4702 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004703 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004704 uint32_t position)
4705{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004706 struct input_panel_surface *input_panel_surface =
4707 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004708 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004709
4710 wl_list_insert(&shell->input_panel.surfaces,
4711 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004712
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004713 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004714 input_panel_surface->panel = 0;
4715}
4716
4717static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004718input_panel_surface_set_overlay_panel(struct wl_client *client,
4719 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004720{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004721 struct input_panel_surface *input_panel_surface =
4722 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004723 struct desktop_shell *shell = input_panel_surface->shell;
4724
4725 wl_list_insert(&shell->input_panel.surfaces,
4726 &input_panel_surface->link);
4727
4728 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004729}
4730
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004731static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004732 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004733 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004734};
4735
4736static void
4737destroy_input_panel_surface_resource(struct wl_resource *resource)
4738{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004739 struct input_panel_surface *ipsurf =
4740 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004741
4742 destroy_input_panel_surface(ipsurf);
4743}
4744
4745static void
4746input_panel_get_input_panel_surface(struct wl_client *client,
4747 struct wl_resource *resource,
4748 uint32_t id,
4749 struct wl_resource *surface_resource)
4750{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004751 struct weston_surface *surface =
4752 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004753 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004754 struct input_panel_surface *ipsurf;
4755
4756 if (get_input_panel_surface(surface)) {
4757 wl_resource_post_error(surface_resource,
4758 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004759 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004760 return;
4761 }
4762
4763 ipsurf = create_input_panel_surface(shell, surface);
4764 if (!ipsurf) {
4765 wl_resource_post_error(surface_resource,
4766 WL_DISPLAY_ERROR_INVALID_OBJECT,
4767 "surface->configure already set");
4768 return;
4769 }
4770
Jason Ekstranda85118c2013-06-27 20:17:02 -05004771 ipsurf->resource =
4772 wl_resource_create(client,
4773 &wl_input_panel_surface_interface, 1, id);
4774 wl_resource_set_implementation(ipsurf->resource,
4775 &input_panel_surface_implementation,
4776 ipsurf,
4777 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004778}
4779
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004780static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004781 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004782};
4783
4784static void
4785unbind_input_panel(struct wl_resource *resource)
4786{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004787 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004788
4789 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004790}
4791
4792static void
4793bind_input_panel(struct wl_client *client,
4794 void *data, uint32_t version, uint32_t id)
4795{
4796 struct desktop_shell *shell = data;
4797 struct wl_resource *resource;
4798
Jason Ekstranda85118c2013-06-27 20:17:02 -05004799 resource = wl_resource_create(client,
4800 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004801
4802 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004803 wl_resource_set_implementation(resource,
4804 &input_panel_implementation,
4805 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004806 shell->input_panel.binding = resource;
4807 return;
4808 }
4809
4810 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4811 "interface object already bound");
4812 wl_resource_destroy(resource);
4813}
4814
Kristian Høgsberg07045392012-02-19 18:52:44 -05004815struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004816 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004817 struct weston_surface *current;
4818 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004819 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004820};
4821
4822static void
4823switcher_next(struct switcher *switcher)
4824{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004825 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004826 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004827 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004828 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004829
Jason Ekstranda7af7042013-10-12 22:38:11 -05004830 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4831 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004832 case SHELL_SURFACE_TOPLEVEL:
4833 case SHELL_SURFACE_FULLSCREEN:
4834 case SHELL_SURFACE_MAXIMIZED:
4835 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004836 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004837 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004838 next = view->surface;
4839 prev = view->surface;
4840 view->alpha = 0.25;
4841 weston_view_geometry_dirty(view);
4842 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004843 break;
4844 default:
4845 break;
4846 }
Alex Wu1659daa2012-04-01 20:13:09 +08004847
Jason Ekstranda7af7042013-10-12 22:38:11 -05004848 if (is_black_surface(view->surface, NULL)) {
4849 view->alpha = 0.25;
4850 weston_view_geometry_dirty(view);
4851 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004852 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004853 }
4854
4855 if (next == NULL)
4856 next = first;
4857
Alex Wu07b26062012-03-12 16:06:01 +08004858 if (next == NULL)
4859 return;
4860
Kristian Høgsberg07045392012-02-19 18:52:44 -05004861 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004862 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004863
4864 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004865 wl_list_for_each(view, &next->views, surface_link)
4866 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004867
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004868 shsurf = get_shell_surface(switcher->current);
4869 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004870 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004871}
4872
4873static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004874switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004875{
4876 struct switcher *switcher =
4877 container_of(listener, struct switcher, listener);
4878
4879 switcher_next(switcher);
4880}
4881
4882static void
Daniel Stone351eb612012-05-31 15:27:47 -04004883switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004884{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004885 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004886 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004887 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004888
Jason Ekstranda7af7042013-10-12 22:38:11 -05004889 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004890 if (is_focus_view(view))
4891 continue;
4892
Jason Ekstranda7af7042013-10-12 22:38:11 -05004893 view->alpha = 1.0;
4894 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004895 }
4896
Alex Wu07b26062012-03-12 16:06:01 +08004897 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004898 activate(switcher->shell, switcher->current,
4899 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004900 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004901 weston_keyboard_end_grab(keyboard);
4902 if (keyboard->input_method_resource)
4903 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004904 free(switcher);
4905}
4906
4907static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004908switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004909 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004910{
4911 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004912 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004913
Daniel Stonec9785ea2012-05-30 16:31:52 +01004914 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004915 switcher_next(switcher);
4916}
4917
4918static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004919switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004920 uint32_t mods_depressed, uint32_t mods_latched,
4921 uint32_t mods_locked, uint32_t group)
4922{
4923 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004924 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004925
Daniel Stone351eb612012-05-31 15:27:47 -04004926 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4927 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004928}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004929
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004930static void
4931switcher_cancel(struct weston_keyboard_grab *grab)
4932{
4933 struct switcher *switcher = container_of(grab, struct switcher, grab);
4934
4935 switcher_destroy(switcher);
4936}
4937
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004938static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004939 switcher_key,
4940 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004941 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004942};
4943
4944static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004945switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004946 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004947{
Tiago Vignattibe143262012-04-16 17:31:41 +03004948 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004949 struct switcher *switcher;
4950
4951 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004952 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004953 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004954 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004955 wl_list_init(&switcher->listener.link);
4956
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004957 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004958 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004959 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004960 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4961 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004962 switcher_next(switcher);
4963}
4964
Daniel Stonedf8133b2013-11-19 11:37:14 +01004965struct exposay_surface {
4966 struct desktop_shell *shell;
4967 struct weston_surface *surface;
4968 struct weston_view *view;
4969 struct wl_list link;
4970
4971 int x;
4972 int y;
4973 int width;
4974 int height;
4975 double scale;
4976
4977 int row;
4978 int column;
4979
4980 /* The animations only apply a transformation for their own lifetime,
4981 * and don't have an option to indefinitely maintain the
4982 * transformation in a steady state - so, we apply our own once the
4983 * animation has finished. */
4984 struct weston_transform transform;
4985};
4986
4987static void exposay_set_state(struct desktop_shell *shell,
4988 enum exposay_target_state state,
4989 struct weston_seat *seat);
4990static void exposay_check_state(struct desktop_shell *shell);
4991
4992static void
4993exposay_in_flight_inc(struct desktop_shell *shell)
4994{
4995 shell->exposay.in_flight++;
4996}
4997
4998static void
4999exposay_in_flight_dec(struct desktop_shell *shell)
5000{
5001 if (--shell->exposay.in_flight > 0)
5002 return;
5003
5004 exposay_check_state(shell);
5005}
5006
5007static void
5008exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5009{
5010 struct exposay_surface *esurface = data;
5011
5012 wl_list_insert(&esurface->view->geometry.transformation_list,
5013 &esurface->transform.link);
5014 weston_matrix_init(&esurface->transform.matrix);
5015 weston_matrix_scale(&esurface->transform.matrix,
5016 esurface->scale, esurface->scale, 1.0f);
5017 weston_matrix_translate(&esurface->transform.matrix,
5018 esurface->x - esurface->view->geometry.x,
5019 esurface->y - esurface->view->geometry.y,
5020 0);
5021
5022 weston_view_geometry_dirty(esurface->view);
5023 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5024
5025 exposay_in_flight_dec(esurface->shell);
5026}
5027
5028static void
5029exposay_animate_in(struct exposay_surface *esurface)
5030{
5031 exposay_in_flight_inc(esurface->shell);
5032
5033 weston_move_scale_run(esurface->view,
5034 esurface->x - esurface->view->geometry.x,
5035 esurface->y - esurface->view->geometry.y,
5036 1.0, esurface->scale, 0,
5037 exposay_animate_in_done, esurface);
5038}
5039
5040static void
5041exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5042{
5043 struct exposay_surface *esurface = data;
5044 struct desktop_shell *shell = esurface->shell;
5045
5046 wl_list_remove(&esurface->link);
5047 free(esurface);
5048
5049 exposay_in_flight_dec(shell);
5050}
5051
5052static void
5053exposay_animate_out(struct exposay_surface *esurface)
5054{
5055 exposay_in_flight_inc(esurface->shell);
5056
5057 /* Remove the static transformation set up by
5058 * exposay_transform_in_done(). */
5059 wl_list_remove(&esurface->transform.link);
5060 weston_view_geometry_dirty(esurface->view);
5061
5062 weston_move_scale_run(esurface->view,
5063 esurface->x - esurface->view->geometry.x,
5064 esurface->y - esurface->view->geometry.y,
5065 1.0, esurface->scale, 1,
5066 exposay_animate_out_done, esurface);
5067}
5068
5069static void
5070exposay_highlight_surface(struct desktop_shell *shell,
5071 struct exposay_surface *esurface)
5072{
5073 struct weston_view *view = NULL;
5074
5075 if (esurface) {
5076 shell->exposay.row_current = esurface->row;
5077 shell->exposay.column_current = esurface->column;
5078 view = esurface->view;
5079 }
5080
5081 animate_focus_change(shell, shell->exposay.workspace,
5082 shell->exposay.focus_current, view);
5083 shell->exposay.focus_current = view;
5084}
5085
5086static int
5087exposay_is_animating(struct desktop_shell *shell)
5088{
5089 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5090 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5091 return 0;
5092
5093 return (shell->exposay.in_flight > 0);
5094}
5095
5096static void
5097exposay_pick(struct desktop_shell *shell, int x, int y)
5098{
5099 struct exposay_surface *esurface;
5100
5101 if (exposay_is_animating(shell))
5102 return;
5103
5104 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5105 if (x < esurface->x || x > esurface->x + esurface->width)
5106 continue;
5107 if (y < esurface->y || y > esurface->y + esurface->height)
5108 continue;
5109
5110 exposay_highlight_surface(shell, esurface);
5111 return;
5112 }
5113}
5114
5115/* Pretty lame layout for now; just tries to make a square. Should take
5116 * aspect ratio into account really. Also needs to be notified of surface
5117 * addition and removal and adjust layout/animate accordingly. */
5118static enum exposay_layout_state
5119exposay_layout(struct desktop_shell *shell)
5120{
5121 struct workspace *workspace = shell->exposay.workspace;
5122 struct weston_compositor *compositor = shell->compositor;
5123 struct weston_output *output = get_default_output(compositor);
5124 struct weston_view *view;
5125 struct exposay_surface *esurface;
5126 int w, h;
5127 int i;
5128 int last_row_removed = 0;
5129
5130 wl_list_init(&shell->exposay.surface_list);
5131
5132 shell->exposay.num_surfaces = 0;
5133 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5134 if (!get_shell_surface(view->surface))
5135 continue;
5136 shell->exposay.num_surfaces++;
5137 }
5138
5139 if (shell->exposay.num_surfaces == 0) {
5140 shell->exposay.grid_size = 0;
5141 shell->exposay.hpadding_outer = 0;
5142 shell->exposay.vpadding_outer = 0;
5143 shell->exposay.padding_inner = 0;
5144 shell->exposay.surface_size = 0;
5145 return EXPOSAY_LAYOUT_OVERVIEW;
5146 }
5147
5148 /* Lay the grid out as square as possible, losing surfaces from the
5149 * bottom row if required. Start with fixed padding of a 10% margin
5150 * around the outside and 80px internal padding between surfaces, and
5151 * maximise the area made available to surfaces after this, but only
5152 * to a maximum of 1/3rd the total output size.
5153 *
5154 * If we can't make a square grid, add one extra row at the bottom
5155 * which will have a smaller number of columns.
5156 *
5157 * XXX: Surely there has to be a better way to express this maths,
5158 * right?!
5159 */
5160 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5161 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5162 shell->exposay.grid_size++;
5163 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5164
5165 shell->exposay.hpadding_outer = (output->width / 10);
5166 shell->exposay.vpadding_outer = (output->height / 10);
5167 shell->exposay.padding_inner = 80;
5168
5169 w = output->width - (shell->exposay.hpadding_outer * 2);
5170 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5171 w /= shell->exposay.grid_size;
5172
5173 h = output->height - (shell->exposay.vpadding_outer * 2);
5174 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5175 h /= shell->exposay.grid_size;
5176
5177 shell->exposay.surface_size = (w < h) ? w : h;
5178 if (shell->exposay.surface_size > (output->width / 2))
5179 shell->exposay.surface_size = output->width / 2;
5180 if (shell->exposay.surface_size > (output->height / 2))
5181 shell->exposay.surface_size = output->height / 2;
5182
5183 i = 0;
5184 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5185 int pad;
5186
5187 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5188
5189 if (!get_shell_surface(view->surface))
5190 continue;
5191
5192 esurface = malloc(sizeof(*esurface));
5193 if (!esurface) {
5194 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5195 shell->exposay.seat);
5196 break;
5197 }
5198
5199 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5200 esurface->shell = shell;
5201 esurface->view = view;
5202
5203 esurface->row = i / shell->exposay.grid_size;
5204 esurface->column = i % shell->exposay.grid_size;
5205
5206 esurface->x = shell->exposay.hpadding_outer;
5207 esurface->x += pad * esurface->column;
5208 esurface->y = shell->exposay.vpadding_outer;
5209 esurface->y += pad * esurface->row;
5210
5211 if (esurface->row == shell->exposay.grid_size - 1)
5212 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5213
5214 if (view->geometry.width > view->geometry.height)
5215 esurface->scale = shell->exposay.surface_size / (float) view->geometry.width;
5216 else
5217 esurface->scale = shell->exposay.surface_size / (float) view->geometry.height;
5218 esurface->width = view->geometry.width * esurface->scale;
5219 esurface->height = view->geometry.height * esurface->scale;
5220
5221 if (shell->exposay.focus_current == esurface->view)
5222 exposay_highlight_surface(shell, esurface);
5223
5224 exposay_animate_in(esurface);
5225
5226 i++;
5227 }
5228
5229 weston_compositor_schedule_repaint(shell->compositor);
5230
5231 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5232}
5233
5234static void
5235exposay_motion(struct weston_pointer_grab *grab, uint32_t time)
5236{
5237 struct desktop_shell *shell =
5238 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5239
5240 exposay_pick(shell,
5241 wl_fixed_to_int(grab->pointer->x),
5242 wl_fixed_to_int(grab->pointer->y));
5243}
5244
5245static void
5246exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5247 uint32_t state_w)
5248{
5249 struct desktop_shell *shell =
5250 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5251 struct weston_seat *seat = grab->pointer->seat;
5252 enum wl_pointer_button_state state = state_w;
5253
5254 if (button != BTN_LEFT)
5255 return;
5256
5257 /* Store the surface we clicked on, and don't do anything if we end up
5258 * releasing on a different surface. */
5259 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5260 shell->exposay.clicked = shell->exposay.focus_current;
5261 return;
5262 }
5263
5264 if (shell->exposay.focus_current == shell->exposay.clicked)
5265 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5266 else
5267 shell->exposay.clicked = NULL;
5268}
5269
5270static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5271 noop_grab_focus,
5272 exposay_motion,
5273 exposay_button,
5274};
5275
5276static int
5277exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5278{
5279 struct exposay_surface *esurface;
5280
5281 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5282 if (esurface->row != row || esurface->column != column)
5283 continue;
5284
5285 exposay_highlight_surface(shell, esurface);
5286 return 1;
5287 }
5288
5289 return 0;
5290}
5291
5292static void
5293exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5294 uint32_t state_w)
5295{
5296 struct weston_seat *seat = grab->keyboard->seat;
5297 struct desktop_shell *shell =
5298 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5299 enum wl_keyboard_key_state state = state_w;
5300
5301 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5302 return;
5303
5304 switch (key) {
5305 case KEY_ESC:
5306 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5307 break;
5308 case KEY_ENTER:
5309 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5310 break;
5311 case KEY_UP:
5312 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5313 shell->exposay.column_current);
5314 break;
5315 case KEY_DOWN:
5316 /* Special case for trying to move to the bottom row when it
5317 * has fewer items than all the others. */
5318 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5319 shell->exposay.column_current) &&
5320 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5321 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5322 (shell->exposay.num_surfaces %
5323 shell->exposay.grid_size) - 1);
5324 }
5325 break;
5326 case KEY_LEFT:
5327 exposay_maybe_move(shell, shell->exposay.row_current,
5328 shell->exposay.column_current - 1);
5329 break;
5330 case KEY_RIGHT:
5331 exposay_maybe_move(shell, shell->exposay.row_current,
5332 shell->exposay.column_current + 1);
5333 break;
5334 case KEY_TAB:
5335 /* Try to move right, then down (and to the leftmost column),
5336 * then if all else fails, to the top left. */
5337 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5338 shell->exposay.column_current + 1) &&
5339 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5340 exposay_maybe_move(shell, 0, 0);
5341 break;
5342 default:
5343 break;
5344 }
5345}
5346
5347static void
5348exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5349 uint32_t mods_depressed, uint32_t mods_latched,
5350 uint32_t mods_locked, uint32_t group)
5351{
5352}
5353
5354static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5355 exposay_key,
5356 exposay_modifier,
5357};
5358
5359/**
5360 * Called when the transition from overview -> inactive has completed.
5361 */
5362static enum exposay_layout_state
5363exposay_set_inactive(struct desktop_shell *shell)
5364{
5365 struct weston_seat *seat = shell->exposay.seat;
5366
5367 weston_keyboard_end_grab(seat->keyboard);
5368 weston_pointer_end_grab(seat->pointer);
5369 if (seat->keyboard->input_method_resource)
5370 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5371
5372 return EXPOSAY_LAYOUT_INACTIVE;
5373}
5374
5375/**
5376 * Begins the transition from overview to inactive. */
5377static enum exposay_layout_state
5378exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5379{
5380 struct exposay_surface *esurface;
5381
5382 /* Call activate() before we start the animations to avoid
5383 * animating back the old state and then immediately transitioning
5384 * to the new. */
5385 if (switch_focus && shell->exposay.focus_current)
5386 activate(shell, shell->exposay.focus_current->surface,
5387 shell->exposay.seat);
5388 else if (shell->exposay.focus_prev)
5389 activate(shell, shell->exposay.focus_prev->surface,
5390 shell->exposay.seat);
5391
5392 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5393 exposay_animate_out(esurface);
5394 weston_compositor_schedule_repaint(shell->compositor);
5395
5396 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5397}
5398
5399static enum exposay_layout_state
5400exposay_transition_active(struct desktop_shell *shell)
5401{
5402 struct weston_seat *seat = shell->exposay.seat;
5403
5404 shell->exposay.workspace = get_current_workspace(shell);
5405 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5406 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5407 shell->exposay.clicked = NULL;
5408 wl_list_init(&shell->exposay.surface_list);
5409
5410 lower_fullscreen_layer(shell);
5411 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5412 weston_keyboard_start_grab(seat->keyboard,
5413 &shell->exposay.grab_kbd);
5414 weston_keyboard_set_focus(seat->keyboard, NULL);
5415
5416 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5417 weston_pointer_start_grab(seat->pointer,
5418 &shell->exposay.grab_ptr);
5419 weston_pointer_set_focus(seat->pointer, NULL,
5420 seat->pointer->x, seat->pointer->y);
5421
5422 return exposay_layout(shell);
5423}
5424
5425static void
5426exposay_check_state(struct desktop_shell *shell)
5427{
5428 enum exposay_layout_state state_new = shell->exposay.state_cur;
5429 int do_switch = 0;
5430
5431 /* Don't do anything whilst animations are running, just store up
5432 * target state changes and only act on them when the animations have
5433 * completed. */
5434 if (exposay_is_animating(shell))
5435 return;
5436
5437 switch (shell->exposay.state_target) {
5438 case EXPOSAY_TARGET_OVERVIEW:
5439 switch (shell->exposay.state_cur) {
5440 case EXPOSAY_LAYOUT_OVERVIEW:
5441 goto out;
5442 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5443 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5444 break;
5445 default:
5446 state_new = exposay_transition_active(shell);
5447 break;
5448 }
5449 break;
5450
5451 case EXPOSAY_TARGET_SWITCH:
5452 do_switch = 1; /* fallthrough */
5453 case EXPOSAY_TARGET_CANCEL:
5454 switch (shell->exposay.state_cur) {
5455 case EXPOSAY_LAYOUT_INACTIVE:
5456 goto out;
5457 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5458 state_new = exposay_set_inactive(shell);
5459 break;
5460 default:
5461 state_new = exposay_transition_inactive(shell, do_switch);
5462 break;
5463 }
5464
5465 break;
5466 }
5467
5468out:
5469 shell->exposay.state_cur = state_new;
5470}
5471
5472static void
5473exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5474 struct weston_seat *seat)
5475{
5476 shell->exposay.state_target = state;
5477 shell->exposay.seat = seat;
5478 exposay_check_state(shell);
5479}
5480
5481static void
5482exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5483 void *data)
5484{
5485 struct desktop_shell *shell = data;
5486
5487 if (shell->exposay.state_target == EXPOSAY_TARGET_OVERVIEW)
5488 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5489 else
5490 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
5491}
5492
Pekka Paalanen3c647232011-12-22 13:43:43 +02005493static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005494backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005495 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005496{
5497 struct weston_compositor *compositor = data;
5498 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005499 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005500
5501 /* TODO: we're limiting to simple use cases, where we assume just
5502 * control on the primary display. We'd have to extend later if we
5503 * ever get support for setting backlights on random desktop LCD
5504 * panels though */
5505 output = get_default_output(compositor);
5506 if (!output)
5507 return;
5508
5509 if (!output->set_backlight)
5510 return;
5511
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005512 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5513 backlight_new = output->backlight_current - 25;
5514 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5515 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005516
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005517 if (backlight_new < 5)
5518 backlight_new = 5;
5519 if (backlight_new > 255)
5520 backlight_new = 255;
5521
5522 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005523 output->set_backlight(output, output->backlight_current);
5524}
5525
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005526struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005527 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005528 struct weston_seat *seat;
5529 uint32_t key[2];
5530 int key_released[2];
5531};
5532
5533static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005534debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005535 uint32_t key, uint32_t state)
5536{
5537 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005538 struct weston_compositor *ec = db->seat->compositor;
5539 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005540 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005541 uint32_t serial;
5542 int send = 0, terminate = 0;
5543 int check_binding = 1;
5544 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005545 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005546
5547 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5548 /* Do not run bindings on key releases */
5549 check_binding = 0;
5550
5551 for (i = 0; i < 2; i++)
5552 if (key == db->key[i])
5553 db->key_released[i] = 1;
5554
5555 if (db->key_released[0] && db->key_released[1]) {
5556 /* All key releases been swalled so end the grab */
5557 terminate = 1;
5558 } else if (key != db->key[0] && key != db->key[1]) {
5559 /* Should not swallow release of other keys */
5560 send = 1;
5561 }
5562 } else if (key == db->key[0] && !db->key_released[0]) {
5563 /* Do not check bindings for the first press of the binding
5564 * key. This allows it to be used as a debug shortcut.
5565 * We still need to swallow this event. */
5566 check_binding = 0;
5567 } else if (db->key[1]) {
5568 /* If we already ran a binding don't process another one since
5569 * we can't keep track of all the binding keys that were
5570 * pressed in order to swallow the release events. */
5571 send = 1;
5572 check_binding = 0;
5573 }
5574
5575 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005576 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5577 key, state)) {
5578 /* We ran a binding so swallow the press and keep the
5579 * grab to swallow the released too. */
5580 send = 0;
5581 terminate = 0;
5582 db->key[1] = key;
5583 } else {
5584 /* Terminate the grab since the key pressed is not a
5585 * debug binding key. */
5586 send = 1;
5587 terminate = 1;
5588 }
5589 }
5590
5591 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005592 serial = wl_display_next_serial(display);
5593 resource_list = &grab->keyboard->focus_resource_list;
5594 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005595 wl_keyboard_send_key(resource, serial, time, key, state);
5596 }
5597 }
5598
5599 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005600 weston_keyboard_end_grab(grab->keyboard);
5601 if (grab->keyboard->input_method_resource)
5602 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005603 free(db);
5604 }
5605}
5606
5607static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005608debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005609 uint32_t mods_depressed, uint32_t mods_latched,
5610 uint32_t mods_locked, uint32_t group)
5611{
5612 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005613 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005614
Neil Roberts96d790e2013-09-19 17:32:00 +01005615 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005616
Neil Roberts96d790e2013-09-19 17:32:00 +01005617 wl_resource_for_each(resource, resource_list) {
5618 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5619 mods_latched, mods_locked, group);
5620 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005621}
5622
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005623static void
5624debug_binding_cancel(struct weston_keyboard_grab *grab)
5625{
5626 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5627
5628 weston_keyboard_end_grab(grab->keyboard);
5629 free(db);
5630}
5631
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005632struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005633 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005634 debug_binding_modifiers,
5635 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005636};
5637
5638static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005639debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005640{
5641 struct debug_binding_grab *grab;
5642
5643 grab = calloc(1, sizeof *grab);
5644 if (!grab)
5645 return;
5646
5647 grab->seat = (struct weston_seat *) seat;
5648 grab->key[0] = key;
5649 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005650 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005651}
5652
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005653static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005654force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005655 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005656{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005657 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005658 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005659 struct desktop_shell *shell = data;
5660 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005661 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005662
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005663 focus_surface = seat->keyboard->focus;
5664 if (!focus_surface)
5665 return;
5666
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005667 wl_signal_emit(&compositor->kill_signal, focus_surface);
5668
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005669 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005670 wl_client_get_credentials(client, &pid, NULL, NULL);
5671
5672 /* Skip clients that we launched ourselves (the credentials of
5673 * the socketpair is ours) */
5674 if (pid == getpid())
5675 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005676
Daniel Stone325fc2d2012-05-30 16:31:58 +01005677 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005678}
5679
5680static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005681workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005682 uint32_t key, void *data)
5683{
5684 struct desktop_shell *shell = data;
5685 unsigned int new_index = shell->workspaces.current;
5686
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005687 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005688 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005689 if (new_index != 0)
5690 new_index--;
5691
5692 change_workspace(shell, new_index);
5693}
5694
5695static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005696workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005697 uint32_t key, void *data)
5698{
5699 struct desktop_shell *shell = data;
5700 unsigned int new_index = shell->workspaces.current;
5701
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005702 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005703 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005704 if (new_index < shell->workspaces.num - 1)
5705 new_index++;
5706
5707 change_workspace(shell, new_index);
5708}
5709
5710static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005711workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005712 uint32_t key, void *data)
5713{
5714 struct desktop_shell *shell = data;
5715 unsigned int new_index;
5716
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005717 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005718 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005719 new_index = key - KEY_F1;
5720 if (new_index >= shell->workspaces.num)
5721 new_index = shell->workspaces.num - 1;
5722
5723 change_workspace(shell, new_index);
5724}
5725
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005726static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005727workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005728 uint32_t key, void *data)
5729{
5730 struct desktop_shell *shell = data;
5731 unsigned int new_index = shell->workspaces.current;
5732
5733 if (shell->locked)
5734 return;
5735
5736 if (new_index != 0)
5737 new_index--;
5738
5739 take_surface_to_workspace_by_seat(shell, seat, new_index);
5740}
5741
5742static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005743workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005744 uint32_t key, void *data)
5745{
5746 struct desktop_shell *shell = data;
5747 unsigned int new_index = shell->workspaces.current;
5748
5749 if (shell->locked)
5750 return;
5751
5752 if (new_index < shell->workspaces.num - 1)
5753 new_index++;
5754
5755 take_surface_to_workspace_by_seat(shell, seat, new_index);
5756}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005757
5758static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005759handle_output_destroy(struct wl_listener *listener, void *data)
5760{
5761 struct shell_output *output_listener =
5762 container_of(listener, struct shell_output, destroy_listener);
5763
5764 wl_list_remove(&output_listener->destroy_listener.link);
5765 wl_list_remove(&output_listener->link);
5766 free(output_listener);
5767}
5768
5769static void
5770create_shell_output(struct desktop_shell *shell,
5771 struct weston_output *output)
5772{
5773 struct shell_output *shell_output;
5774
5775 shell_output = zalloc(sizeof *shell_output);
5776 if (shell_output == NULL)
5777 return;
5778
5779 shell_output->output = output;
5780 shell_output->shell = shell;
5781 shell_output->destroy_listener.notify = handle_output_destroy;
5782 wl_signal_add(&output->destroy_signal,
5783 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005784 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005785}
5786
5787static void
5788handle_output_create(struct wl_listener *listener, void *data)
5789{
5790 struct desktop_shell *shell =
5791 container_of(listener, struct desktop_shell, output_create_listener);
5792 struct weston_output *output = (struct weston_output *)data;
5793
5794 create_shell_output(shell, output);
5795}
5796
5797static void
5798setup_output_destroy_handler(struct weston_compositor *ec,
5799 struct desktop_shell *shell)
5800{
5801 struct weston_output *output;
5802
5803 wl_list_init(&shell->output_list);
5804 wl_list_for_each(output, &ec->output_list, link)
5805 create_shell_output(shell, output);
5806
5807 shell->output_create_listener.notify = handle_output_create;
5808 wl_signal_add(&ec->output_created_signal,
5809 &shell->output_create_listener);
5810}
5811
5812static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005813shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005814{
Tiago Vignattibe143262012-04-16 17:31:41 +03005815 struct desktop_shell *shell =
5816 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005817 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005818 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005819
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005820 if (shell->child.client)
5821 wl_client_destroy(shell->child.client);
5822
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005823 wl_list_remove(&shell->idle_listener.link);
5824 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005825 wl_list_remove(&shell->show_input_panel_listener.link);
5826 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005827
Xiong Zhang6b481422013-10-23 13:58:32 +08005828 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5829 wl_list_remove(&shell_output->destroy_listener.link);
5830 wl_list_remove(&shell_output->link);
5831 free(shell_output);
5832 }
5833
5834 wl_list_remove(&shell->output_create_listener.link);
5835
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005836 wl_array_for_each(ws, &shell->workspaces.array)
5837 workspace_destroy(*ws);
5838 wl_array_release(&shell->workspaces.array);
5839
Pekka Paalanen3c647232011-12-22 13:43:43 +02005840 free(shell->screensaver.path);
5841 free(shell);
5842}
5843
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005844static void
5845shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5846{
5847 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005848 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005849
5850 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005851 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5852 MODIFIER_CTRL | MODIFIER_ALT,
5853 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01005854 weston_compositor_add_key_binding(ec, KEY_TAB,
5855 MODIFIER_ALT,
5856 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005857 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5858 click_to_activate_binding,
5859 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005860 weston_compositor_add_touch_binding(ec, 0,
5861 touch_to_activate_binding,
5862 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005863 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5864 MODIFIER_SUPER | MODIFIER_ALT,
5865 surface_opacity_binding, NULL);
5866 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5867 MODIFIER_SUPER, zoom_axis_binding,
5868 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005869
5870 /* configurable bindings */
5871 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005872 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5873 zoom_key_binding, NULL);
5874 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5875 zoom_key_binding, NULL);
5876 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5877 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005878 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005879 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5880 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005881
5882 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5883 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5884 rotate_binding, NULL);
5885
Daniel Stone325fc2d2012-05-30 16:31:58 +01005886 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5887 shell);
5888 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5889 ec);
5890 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5891 backlight_binding, ec);
5892 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5893 ec);
5894 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5895 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005896 weston_compositor_add_key_binding(ec, KEY_K, mod,
5897 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005898 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5899 workspace_up_binding, shell);
5900 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5901 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005902 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5903 workspace_move_surface_up_binding,
5904 shell);
5905 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5906 workspace_move_surface_down_binding,
5907 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005908
Daniel Stonedf8133b2013-11-19 11:37:14 +01005909 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5910
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005911 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5912 if (shell->workspaces.num > 1) {
5913 num_workspace_bindings = shell->workspaces.num;
5914 if (num_workspace_bindings > 6)
5915 num_workspace_bindings = 6;
5916 for (i = 0; i < num_workspace_bindings; i++)
5917 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5918 workspace_f_binding,
5919 shell);
5920 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005921
5922 /* Debug bindings */
5923 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5924 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005925}
5926
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005927WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005928module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005929 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005930{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005931 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005932 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005933 struct workspace **pws;
5934 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005935 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005936
Peter Huttererf3d62272013-08-08 11:57:05 +10005937 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005938 if (shell == NULL)
5939 return -1;
5940
Kristian Høgsberg75840622011-09-06 13:48:16 -04005941 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005942
5943 shell->destroy_listener.notify = shell_destroy;
5944 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005945 shell->idle_listener.notify = idle_handler;
5946 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5947 shell->wake_listener.notify = wake_handler;
5948 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005949 shell->show_input_panel_listener.notify = show_input_panels;
5950 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
5951 shell->hide_input_panel_listener.notify = hide_input_panels;
5952 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005953 shell->update_input_panel_listener.notify = update_input_panels;
5954 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06005955 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005956 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005957 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005958 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005959 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005960 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005961 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005962 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005963 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005964 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005965 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005966
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005967 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005968
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005969 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5970 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005971 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5972 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005973 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005974
5975 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005976 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005977
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005978 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005979
Daniel Stonedf8133b2013-11-19 11:37:14 +01005980 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5981 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5982
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005983 for (i = 0; i < shell->workspaces.num; i++) {
5984 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5985 if (pws == NULL)
5986 return -1;
5987
5988 *pws = workspace_create();
5989 if (*pws == NULL)
5990 return -1;
5991 }
5992 activate_workspace(shell, 0);
5993
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005994 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005995 wl_list_init(&shell->workspaces.animation.link);
5996 shell->workspaces.animation.frame = animate_workspace_change_frame;
5997
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005998 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005999 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006000 return -1;
6001
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006002 if (wl_global_create(ec->wl_display,
6003 &desktop_shell_interface, 2,
6004 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006005 return -1;
6006
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006007 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6008 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006009 return -1;
6010
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006011 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006012 shell, bind_input_panel) == NULL)
6013 return -1;
6014
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006015 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6016 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006017 return -1;
6018
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006019 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006020
Xiong Zhang6b481422013-10-23 13:58:32 +08006021 setup_output_destroy_handler(ec, shell);
6022
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006023 loop = wl_display_get_event_loop(ec->wl_display);
6024 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006025
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006026 shell->screensaver.timer =
6027 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6028
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006029 wl_list_for_each(seat, &ec->seat_list, link)
6030 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006031
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006032 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006033
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006034 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006035
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006036 return 0;
6037}