blob: bf5c704bb7de79364dd1fc0cb10a3609f02c4df1 [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);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100618 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100619
620 weston_view_configure(fsurf->view, output->x, output->y,
621 output->width, output->height);
622 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
623 pixman_region32_fini(&surface->opaque);
624 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
625 output->width, output->height);
626 pixman_region32_fini(&surface->input);
627 pixman_region32_init(&surface->input);
628
629 wl_list_init(&fsurf->workspace_transform.link);
630
631 return fsurf;
632}
633
634static void
635focus_surface_destroy(struct focus_surface *fsurf)
636{
637 weston_surface_destroy(fsurf->surface);
638 free(fsurf);
639}
640
641static void
642focus_animation_done(struct weston_view_animation *animation, void *data)
643{
644 struct workspace *ws = data;
645
646 ws->focus_animation = NULL;
647}
648
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200649static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200650focus_state_destroy(struct focus_state *state)
651{
652 wl_list_remove(&state->seat_destroy_listener.link);
653 wl_list_remove(&state->surface_destroy_listener.link);
654 free(state);
655}
656
657static void
658focus_state_seat_destroy(struct wl_listener *listener, void *data)
659{
660 struct focus_state *state = container_of(listener,
661 struct focus_state,
662 seat_destroy_listener);
663
664 wl_list_remove(&state->link);
665 focus_state_destroy(state);
666}
667
668static void
669focus_state_surface_destroy(struct wl_listener *listener, void *data)
670{
671 struct focus_state *state = container_of(listener,
672 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400673 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400674 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500675 struct weston_surface *main_surface, *next;
676 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200677
Pekka Paalanen01388e22013-04-25 13:57:44 +0300678 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
679
Kristian Høgsberge3778222012-07-31 17:29:30 -0400680 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500681 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
682 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400683 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100684 if (is_focus_view(view))
685 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400686
Jason Ekstranda7af7042013-10-12 22:38:11 -0500687 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400688 break;
689 }
690
Pekka Paalanen01388e22013-04-25 13:57:44 +0300691 /* if the focus was a sub-surface, activate its main surface */
692 if (main_surface != state->keyboard_focus)
693 next = main_surface;
694
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100695 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400696 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100697 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400698 activate(shell, next, state->seat);
699 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100700 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
701 if (state->ws->focus_animation)
702 weston_view_animation_destroy(state->ws->focus_animation);
703
704 state->ws->focus_animation = weston_fade_run(
705 state->ws->fsurf_front->view,
706 state->ws->fsurf_front->view->alpha, 0.0, 300,
707 focus_animation_done, state->ws);
708 }
709
Kristian Høgsberge3778222012-07-31 17:29:30 -0400710 wl_list_remove(&state->link);
711 focus_state_destroy(state);
712 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200713}
714
715static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400716focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200717{
Jonas Ådahl04769742012-06-13 00:01:24 +0200718 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200719
720 state = malloc(sizeof *state);
721 if (state == NULL)
722 return NULL;
723
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100724 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400725 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200726 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400727 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200728
729 state->seat_destroy_listener.notify = focus_state_seat_destroy;
730 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400731 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200732 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400733 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200734
735 return state;
736}
737
Jonas Ådahl8538b222012-08-29 22:13:03 +0200738static struct focus_state *
739ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
740{
741 struct workspace *ws = get_current_workspace(shell);
742 struct focus_state *state;
743
744 wl_list_for_each(state, &ws->focus_list, link)
745 if (state->seat == seat)
746 break;
747
748 if (&state->link == &ws->focus_list)
749 state = focus_state_create(seat, ws);
750
751 return state;
752}
753
Jonas Ådahl04769742012-06-13 00:01:24 +0200754static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400755restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200756{
757 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400758 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200759
760 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400761 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200762
Kristian Høgsberge3148752013-05-06 23:19:49 -0400763 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200764 }
765}
766
767static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200768replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
769 struct weston_seat *seat)
770{
771 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400772 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200773
774 wl_list_for_each(state, &ws->focus_list, link) {
775 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400776 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500777 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200778 return;
779 }
780 }
781}
782
783static void
784drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
785 struct weston_surface *surface)
786{
787 struct focus_state *state;
788
789 wl_list_for_each(state, &ws->focus_list, link)
790 if (state->keyboard_focus == surface)
791 state->keyboard_focus = NULL;
792}
793
794static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100795animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
796 struct weston_view *from, struct weston_view *to)
797{
798 struct weston_output *output;
799 bool focus_surface_created = false;
800
801 /* FIXME: Only support dim animation using two layers */
802 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
803 return;
804
805 output = get_default_output(shell->compositor);
806 if (ws->fsurf_front == NULL && (from || to)) {
807 ws->fsurf_front = create_focus_surface(shell->compositor, output);
808 ws->fsurf_back = create_focus_surface(shell->compositor, output);
809 ws->fsurf_front->view->alpha = 0.0;
810 ws->fsurf_back->view->alpha = 0.0;
811 focus_surface_created = true;
812 } else {
813 wl_list_remove(&ws->fsurf_front->view->layer_link);
814 wl_list_remove(&ws->fsurf_back->view->layer_link);
815 }
816
817 if (ws->focus_animation) {
818 weston_view_animation_destroy(ws->focus_animation);
819 ws->focus_animation = NULL;
820 }
821
822 if (to)
823 wl_list_insert(&to->layer_link,
824 &ws->fsurf_front->view->layer_link);
825 else if (from)
826 wl_list_insert(&ws->layer.view_list,
827 &ws->fsurf_front->view->layer_link);
828
829 if (focus_surface_created) {
830 ws->focus_animation = weston_fade_run(
831 ws->fsurf_front->view,
832 ws->fsurf_front->view->alpha, 0.6, 300,
833 focus_animation_done, ws);
834 } else if (from) {
835 wl_list_insert(&from->layer_link,
836 &ws->fsurf_back->view->layer_link);
837 ws->focus_animation = weston_stable_fade_run(
838 ws->fsurf_front->view, 0.0,
839 ws->fsurf_back->view, 0.6,
840 focus_animation_done, ws);
841 } else if (to) {
842 wl_list_insert(&ws->layer.view_list,
843 &ws->fsurf_back->view->layer_link);
844 ws->focus_animation = weston_stable_fade_run(
845 ws->fsurf_front->view, 0.0,
846 ws->fsurf_back->view, 0.6,
847 focus_animation_done, ws);
848 }
849}
850
851static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200852workspace_destroy(struct workspace *ws)
853{
Jonas Ådahl04769742012-06-13 00:01:24 +0200854 struct focus_state *state, *next;
855
856 wl_list_for_each_safe(state, next, &ws->focus_list, link)
857 focus_state_destroy(state);
858
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100859 if (ws->fsurf_front)
860 focus_surface_destroy(ws->fsurf_front);
861 if (ws->fsurf_back)
862 focus_surface_destroy(ws->fsurf_back);
863
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200864 free(ws);
865}
866
Jonas Ådahl04769742012-06-13 00:01:24 +0200867static void
868seat_destroyed(struct wl_listener *listener, void *data)
869{
870 struct weston_seat *seat = data;
871 struct focus_state *state, *next;
872 struct workspace *ws = container_of(listener,
873 struct workspace,
874 seat_destroyed_listener);
875
876 wl_list_for_each_safe(state, next, &ws->focus_list, link)
877 if (state->seat == seat)
878 wl_list_remove(&state->link);
879}
880
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200881static struct workspace *
882workspace_create(void)
883{
884 struct workspace *ws = malloc(sizeof *ws);
885 if (ws == NULL)
886 return NULL;
887
888 weston_layer_init(&ws->layer, NULL);
889
Jonas Ådahl04769742012-06-13 00:01:24 +0200890 wl_list_init(&ws->focus_list);
891 wl_list_init(&ws->seat_destroyed_listener.link);
892 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100893 ws->fsurf_front = NULL;
894 ws->fsurf_back = NULL;
895 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200896
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200897 return ws;
898}
899
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200900static int
901workspace_is_empty(struct workspace *ws)
902{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500903 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200904}
905
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200906static struct workspace *
907get_workspace(struct desktop_shell *shell, unsigned int index)
908{
909 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200910 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200911 pws += index;
912 return *pws;
913}
914
915static struct workspace *
916get_current_workspace(struct desktop_shell *shell)
917{
918 return get_workspace(shell, shell->workspaces.current);
919}
920
921static void
922activate_workspace(struct desktop_shell *shell, unsigned int index)
923{
924 struct workspace *ws;
925
926 ws = get_workspace(shell, index);
927 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
928
929 shell->workspaces.current = index;
930}
931
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200932static unsigned int
933get_output_height(struct weston_output *output)
934{
935 return abs(output->region.extents.y1 - output->region.extents.y2);
936}
937
938static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100939view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200940{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200941 struct weston_transform *transform;
942
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100943 if (is_focus_view(view)) {
944 struct focus_surface *fsurf = get_focus_surface(view->surface);
945 transform = &fsurf->workspace_transform;
946 } else {
947 struct shell_surface *shsurf = get_shell_surface(view->surface);
948 transform = &shsurf->workspace_transform;
949 }
950
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200951 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100953 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200954
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100955 weston_matrix_init(&transform->matrix);
956 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200957 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500958 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200959}
960
961static void
962workspace_translate_out(struct workspace *ws, double fraction)
963{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965 unsigned int height;
966 double d;
967
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
969 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200970 d = height * fraction;
971
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100972 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200973 }
974}
975
976static void
977workspace_translate_in(struct workspace *ws, double fraction)
978{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200980 unsigned int height;
981 double d;
982
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
984 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200985
986 if (fraction > 0)
987 d = -(height - height * fraction);
988 else
989 d = height + height * fraction;
990
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100991 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200992 }
993}
994
995static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200996broadcast_current_workspace_state(struct desktop_shell *shell)
997{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700998 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200999
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001000 wl_resource_for_each(resource, &shell->workspaces.client_list)
1001 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001002 shell->workspaces.current,
1003 shell->workspaces.num);
1004}
1005
1006static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001007reverse_workspace_change_animation(struct desktop_shell *shell,
1008 unsigned int index,
1009 struct workspace *from,
1010 struct workspace *to)
1011{
1012 shell->workspaces.current = index;
1013
1014 shell->workspaces.anim_to = to;
1015 shell->workspaces.anim_from = from;
1016 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1017 shell->workspaces.anim_timestamp = 0;
1018
Scott Moreau4272e632012-08-13 09:58:41 -06001019 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001020}
1021
1022static void
1023workspace_deactivate_transforms(struct workspace *ws)
1024{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001025 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001026 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001027
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001029 if (is_focus_view(view)) {
1030 struct focus_surface *fsurf = get_focus_surface(view->surface);
1031 transform = &fsurf->workspace_transform;
1032 } else {
1033 struct shell_surface *shsurf = get_shell_surface(view->surface);
1034 transform = &shsurf->workspace_transform;
1035 }
1036
1037 if (!wl_list_empty(&transform->link)) {
1038 wl_list_remove(&transform->link);
1039 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001040 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001042 }
1043}
1044
1045static void
1046finish_workspace_change_animation(struct desktop_shell *shell,
1047 struct workspace *from,
1048 struct workspace *to)
1049{
Scott Moreau4272e632012-08-13 09:58:41 -06001050 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001051
1052 wl_list_remove(&shell->workspaces.animation.link);
1053 workspace_deactivate_transforms(from);
1054 workspace_deactivate_transforms(to);
1055 shell->workspaces.anim_to = NULL;
1056
1057 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1058}
1059
1060static void
1061animate_workspace_change_frame(struct weston_animation *animation,
1062 struct weston_output *output, uint32_t msecs)
1063{
1064 struct desktop_shell *shell =
1065 container_of(animation, struct desktop_shell,
1066 workspaces.animation);
1067 struct workspace *from = shell->workspaces.anim_from;
1068 struct workspace *to = shell->workspaces.anim_to;
1069 uint32_t t;
1070 double x, y;
1071
1072 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1073 finish_workspace_change_animation(shell, from, to);
1074 return;
1075 }
1076
1077 if (shell->workspaces.anim_timestamp == 0) {
1078 if (shell->workspaces.anim_current == 0.0)
1079 shell->workspaces.anim_timestamp = msecs;
1080 else
1081 shell->workspaces.anim_timestamp =
1082 msecs -
1083 /* Invers of movement function 'y' below. */
1084 (asin(1.0 - shell->workspaces.anim_current) *
1085 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1086 M_2_PI);
1087 }
1088
1089 t = msecs - shell->workspaces.anim_timestamp;
1090
1091 /*
1092 * x = [0, π/2]
1093 * y(x) = sin(x)
1094 */
1095 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1096 y = sin(x);
1097
1098 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001099 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001100
1101 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1102 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1103 shell->workspaces.anim_current = y;
1104
Scott Moreau4272e632012-08-13 09:58:41 -06001105 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001107 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001108 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001109}
1110
1111static void
1112animate_workspace_change(struct desktop_shell *shell,
1113 unsigned int index,
1114 struct workspace *from,
1115 struct workspace *to)
1116{
1117 struct weston_output *output;
1118
1119 int dir;
1120
1121 if (index > shell->workspaces.current)
1122 dir = -1;
1123 else
1124 dir = 1;
1125
1126 shell->workspaces.current = index;
1127
1128 shell->workspaces.anim_dir = dir;
1129 shell->workspaces.anim_from = from;
1130 shell->workspaces.anim_to = to;
1131 shell->workspaces.anim_current = 0.0;
1132 shell->workspaces.anim_timestamp = 0;
1133
1134 output = container_of(shell->compositor->output_list.next,
1135 struct weston_output, link);
1136 wl_list_insert(&output->animation_list,
1137 &shell->workspaces.animation.link);
1138
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001139 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001140
1141 workspace_translate_in(to, 0);
1142
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001143 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001144
Scott Moreau4272e632012-08-13 09:58:41 -06001145 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001146}
1147
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001148static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001149update_workspace(struct desktop_shell *shell, unsigned int index,
1150 struct workspace *from, struct workspace *to)
1151{
1152 shell->workspaces.current = index;
1153 wl_list_insert(&from->layer.link, &to->layer.link);
1154 wl_list_remove(&from->layer.link);
1155}
1156
1157static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001158change_workspace(struct desktop_shell *shell, unsigned int index)
1159{
1160 struct workspace *from;
1161 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001162 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001163
1164 if (index == shell->workspaces.current)
1165 return;
1166
1167 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001169 return;
1170
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001171 from = get_current_workspace(shell);
1172 to = get_workspace(shell, index);
1173
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001174 if (shell->workspaces.anim_from == to &&
1175 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001176 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001177 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001178 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001179 return;
1180 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001181
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182 if (shell->workspaces.anim_to != NULL)
1183 finish_workspace_change_animation(shell,
1184 shell->workspaces.anim_from,
1185 shell->workspaces.anim_to);
1186
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001187 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001188
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001189 if (shell->focus_animation_type != ANIMATION_NONE) {
1190 wl_list_for_each(state, &from->focus_list, link)
1191 if (state->keyboard_focus)
1192 animate_focus_change(shell, from,
1193 get_default_view(state->keyboard_focus), NULL);
1194
1195 wl_list_for_each(state, &to->focus_list, link)
1196 if (state->keyboard_focus)
1197 animate_focus_change(shell, to,
1198 NULL, get_default_view(state->keyboard_focus));
1199 }
1200
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001201 if (workspace_is_empty(to) && workspace_is_empty(from))
1202 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001203 else
1204 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001205
1206 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001207}
1208
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001209static bool
1210workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1211{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001213 struct wl_list *e;
1214
1215 if (wl_list_empty(list))
1216 return false;
1217
1218 e = list->next;
1219
1220 if (e->next != list)
1221 return false;
1222
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224}
1225
1226static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227move_view_to_workspace(struct desktop_shell *shell,
1228 struct weston_view *view,
1229 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001230{
1231 struct workspace *from;
1232 struct workspace *to;
1233 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001234 struct weston_surface *focus;
1235
Jason Ekstranda7af7042013-10-12 22:38:11 -05001236 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001237
1238 if (workspace == shell->workspaces.current)
1239 return;
1240
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001241 if (workspace >= shell->workspaces.num)
1242 workspace = shell->workspaces.num - 1;
1243
Jonas Ådahle9d22502012-08-29 22:13:01 +02001244 from = get_current_workspace(shell);
1245 to = get_workspace(shell, workspace);
1246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 wl_list_remove(&view->layer_link);
1248 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001249
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001251 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1252 if (!seat->keyboard)
1253 continue;
1254
1255 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001257 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001258 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001259
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001261}
1262
1263static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001264take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001265 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001266 unsigned int index)
1267{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001268 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270 struct shell_surface *shsurf;
1271 struct workspace *from;
1272 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001273 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001274
Pekka Paalanen01388e22013-04-25 13:57:44 +03001275 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 view = get_default_view(surface);
1277 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001278 index == shell->workspaces.current ||
1279 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001280 return;
1281
1282 from = get_current_workspace(shell);
1283 to = get_workspace(shell, index);
1284
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 wl_list_remove(&view->layer_link);
1286 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001287
Jonas Ådahle9d22502012-08-29 22:13:01 +02001288 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289 drop_focus_state(shell, from, surface);
1290
1291 if (shell->workspaces.anim_from == to &&
1292 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001293 wl_list_remove(&to->layer.link);
1294 wl_list_insert(from->layer.link.prev, &to->layer.link);
1295
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001296 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001297 broadcast_current_workspace_state(shell);
1298
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001299 return;
1300 }
1301
1302 if (shell->workspaces.anim_to != NULL)
1303 finish_workspace_change_animation(shell,
1304 shell->workspaces.anim_from,
1305 shell->workspaces.anim_to);
1306
1307 if (workspace_is_empty(from) &&
1308 workspace_has_only(to, surface))
1309 update_workspace(shell, index, from, to);
1310 else {
1311 shsurf = get_shell_surface(surface);
1312 if (wl_list_empty(&shsurf->workspace_transform.link))
1313 wl_list_insert(&shell->workspaces.anim_sticky_list,
1314 &shsurf->workspace_transform.link);
1315
1316 animate_workspace_change(shell, index, from, to);
1317 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001318
1319 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001320
1321 state = ensure_focus_state(shell, seat);
1322 if (state != NULL)
1323 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001324}
1325
1326static void
1327workspace_manager_move_surface(struct wl_client *client,
1328 struct wl_resource *resource,
1329 struct wl_resource *surface_resource,
1330 uint32_t workspace)
1331{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001332 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001333 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001334 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001335 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001337
Pekka Paalanen01388e22013-04-25 13:57:44 +03001338 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339 view = get_default_view(main_surface);
1340 if (!view)
1341 return;
1342 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001343}
1344
1345static const struct workspace_manager_interface workspace_manager_implementation = {
1346 workspace_manager_move_surface,
1347};
1348
1349static void
1350unbind_resource(struct wl_resource *resource)
1351{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001352 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001353}
1354
1355static void
1356bind_workspace_manager(struct wl_client *client,
1357 void *data, uint32_t version, uint32_t id)
1358{
1359 struct desktop_shell *shell = data;
1360 struct wl_resource *resource;
1361
Jason Ekstranda85118c2013-06-27 20:17:02 -05001362 resource = wl_resource_create(client,
1363 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001364
1365 if (resource == NULL) {
1366 weston_log("couldn't add workspace manager object");
1367 return;
1368 }
1369
Jason Ekstranda85118c2013-06-27 20:17:02 -05001370 wl_resource_set_implementation(resource,
1371 &workspace_manager_implementation,
1372 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001373 wl_list_insert(&shell->workspaces.client_list,
1374 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001375
1376 workspace_manager_send_state(resource,
1377 shell->workspaces.current,
1378 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001379}
1380
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001381static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001382touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1383 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1384{
1385}
1386
1387static void
1388touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1389{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001390 struct weston_touch_move_grab *move =
1391 (struct weston_touch_move_grab *) container_of(
1392 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001393
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001394 if (grab->touch->seat->num_tp == 0) {
1395 shell_touch_grab_end(&move->base);
1396 free(move);
1397 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001398}
1399
1400static void
1401touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1402 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1403{
1404 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1405 struct shell_surface *shsurf = move->base.shsurf;
1406 struct weston_surface *es;
1407 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1408 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1409
1410 if (!shsurf)
1411 return;
1412
1413 es = shsurf->surface;
1414
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415 weston_view_configure(shsurf->view, dx, dy,
1416 shsurf->view->geometry.width,
1417 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001418
1419 weston_compositor_schedule_repaint(es->compositor);
1420}
1421
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001422static void
1423touch_move_grab_cancel(struct weston_touch_grab *grab)
1424{
1425 struct weston_touch_move_grab *move =
1426 (struct weston_touch_move_grab *) container_of(
1427 grab, struct shell_touch_grab, grab);
1428
1429 shell_touch_grab_end(&move->base);
1430 free(move);
1431}
1432
Rusty Lynch1084da52013-08-15 09:10:08 -07001433static const struct weston_touch_grab_interface touch_move_grab_interface = {
1434 touch_move_grab_down,
1435 touch_move_grab_up,
1436 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001437 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001438};
1439
1440static int
1441surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1442{
1443 struct weston_touch_move_grab *move;
1444
1445 if (!shsurf)
1446 return -1;
1447
1448 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1449 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001450 if (shsurf->grabbed)
1451 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001452
1453 move = malloc(sizeof *move);
1454 if (!move)
1455 return -1;
1456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001458 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001460 seat->touch->grab_y;
1461
1462 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1463 seat->touch);
1464
1465 return 0;
1466}
1467
1468static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001469noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001470{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001471}
1472
1473static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001474move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1475 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001476{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001477 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001478 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001479 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001480 int dx, dy;
1481
1482 weston_pointer_move(pointer, x, y);
1483 dx = wl_fixed_to_int(pointer->x + move->dx);
1484 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001485
1486 if (!shsurf)
1487 return;
1488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 weston_view_configure(shsurf->view, dx, dy,
1490 shsurf->view->geometry.width,
1491 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001492
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001494}
1495
1496static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001497move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001498 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001499{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001500 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1501 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001502 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001503 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001504
Daniel Stone4dbadb12012-05-30 16:31:51 +01001505 if (pointer->button_count == 0 &&
1506 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001507 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001508 free(grab);
1509 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001510}
1511
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001512static void
1513move_grab_cancel(struct weston_pointer_grab *grab)
1514{
1515 struct shell_grab *shell_grab =
1516 container_of(grab, struct shell_grab, grab);
1517
1518 shell_grab_end(shell_grab);
1519 free(grab);
1520}
1521
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001522static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001523 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001524 move_grab_motion,
1525 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001526 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001527};
1528
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001529static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001530surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001531{
1532 struct weston_move_grab *move;
1533
1534 if (!shsurf)
1535 return -1;
1536
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001537 if (shsurf->grabbed)
1538 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001539 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1540 return 0;
1541
1542 move = malloc(sizeof *move);
1543 if (!move)
1544 return -1;
1545
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001547 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001549 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001550
1551 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001552 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001553
1554 return 0;
1555}
1556
1557static void
1558shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1559 struct wl_resource *seat_resource, uint32_t serial)
1560{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001561 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001562 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001563 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001564
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001565 if (seat->pointer &&
1566 seat->pointer->button_count > 0 &&
1567 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001568 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001569 if ((surface == shsurf->surface) &&
1570 (surface_move(shsurf, seat) < 0))
1571 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001572 } else if (seat->touch &&
1573 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001575 if ((surface == shsurf->surface) &&
1576 (surface_touch_move(shsurf, seat) < 0))
1577 wl_resource_post_no_memory(resource);
1578 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001579}
1580
1581struct weston_resize_grab {
1582 struct shell_grab base;
1583 uint32_t edges;
1584 int32_t width, height;
1585};
1586
1587static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001588resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1589 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001590{
1591 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001592 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001593 struct shell_surface *shsurf = resize->base.shsurf;
1594 int32_t width, height;
1595 wl_fixed_t from_x, from_y;
1596 wl_fixed_t to_x, to_y;
1597
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001598 weston_pointer_move(pointer, x, y);
1599
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001600 if (!shsurf)
1601 return;
1602
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603 weston_view_from_global_fixed(shsurf->view,
1604 pointer->grab_x, pointer->grab_y,
1605 &from_x, &from_y);
1606 weston_view_from_global_fixed(shsurf->view,
1607 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001608
1609 width = resize->width;
1610 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1611 width += wl_fixed_to_int(from_x - to_x);
1612 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1613 width += wl_fixed_to_int(to_x - from_x);
1614 }
1615
1616 height = resize->height;
1617 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1618 height += wl_fixed_to_int(from_y - to_y);
1619 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1620 height += wl_fixed_to_int(to_y - from_y);
1621 }
1622
1623 shsurf->client->send_configure(shsurf->surface,
1624 resize->edges, width, height);
1625}
1626
1627static void
1628send_configure(struct weston_surface *surface,
1629 uint32_t edges, int32_t width, int32_t height)
1630{
1631 struct shell_surface *shsurf = get_shell_surface(surface);
1632
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001633 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001634 edges, width, height);
1635}
1636
1637static const struct weston_shell_client shell_client = {
1638 send_configure
1639};
1640
1641static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001642resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001643 uint32_t time, uint32_t button, uint32_t state_w)
1644{
1645 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001646 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001647 enum wl_pointer_button_state state = state_w;
1648
1649 if (pointer->button_count == 0 &&
1650 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1651 shell_grab_end(&resize->base);
1652 free(grab);
1653 }
1654}
1655
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001656static void
1657resize_grab_cancel(struct weston_pointer_grab *grab)
1658{
1659 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1660
1661 shell_grab_end(&resize->base);
1662 free(grab);
1663}
1664
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001665static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001666 noop_grab_focus,
1667 resize_grab_motion,
1668 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001669 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001670};
1671
Giulio Camuffob8366642013-04-25 13:57:46 +03001672/*
1673 * Returns the bounding box of a surface and all its sub-surfaces,
1674 * in the surface coordinates system. */
1675static void
1676surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1677 int32_t *y, int32_t *w, int32_t *h) {
1678 pixman_region32_t region;
1679 pixman_box32_t *box;
1680 struct weston_subsurface *subsurface;
1681
1682 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001683 surface->width,
1684 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001685
1686 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1687 pixman_region32_union_rect(&region, &region,
1688 subsurface->position.x,
1689 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 subsurface->surface->width,
1691 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001692 }
1693
1694 box = pixman_region32_extents(&region);
1695 if (x)
1696 *x = box->x1;
1697 if (y)
1698 *y = box->y1;
1699 if (w)
1700 *w = box->x2 - box->x1;
1701 if (h)
1702 *h = box->y2 - box->y1;
1703
1704 pixman_region32_fini(&region);
1705}
1706
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001707static int
1708surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001709 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001710{
1711 struct weston_resize_grab *resize;
1712
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001713 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1714 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715 return 0;
1716
1717 if (edges == 0 || edges > 15 ||
1718 (edges & 3) == 3 || (edges & 12) == 12)
1719 return 0;
1720
1721 resize = malloc(sizeof *resize);
1722 if (!resize)
1723 return -1;
1724
1725 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001726 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1727 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001728
1729 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001730 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731
1732 return 0;
1733}
1734
1735static void
1736shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1737 struct wl_resource *seat_resource, uint32_t serial,
1738 uint32_t edges)
1739{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001740 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001741 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001742 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001743
1744 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1745 return;
1746
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001748 if (seat->pointer->button_count == 0 ||
1749 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001750 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001751 return;
1752
Kristian Høgsberge3148752013-05-06 23:19:49 -04001753 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001754 wl_resource_post_no_memory(resource);
1755}
1756
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001757static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001758end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1759
1760static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001761busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001762{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001763 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001764 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001766 wl_fixed_t sx, sy;
1767
Jason Ekstranda7af7042013-10-12 22:38:11 -05001768 view = weston_compositor_pick_view(pointer->seat->compositor,
1769 pointer->x, pointer->y,
1770 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001771
Jason Ekstranda7af7042013-10-12 22:38:11 -05001772 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001773 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001774}
1775
1776static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001777busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1778 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001779{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001780 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001781}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001782
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001783static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001784busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001785 uint32_t time, uint32_t button, uint32_t state)
1786{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001787 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001788 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001789 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001790
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001791 if (shsurf && button == BTN_LEFT && state) {
1792 activate(shsurf->shell, shsurf->surface, seat);
1793 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001794 } else if (shsurf && button == BTN_RIGHT && state) {
1795 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001796 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001797 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001798}
1799
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001800static void
1801busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1802{
1803 struct shell_grab *grab = (struct shell_grab *) base;
1804
1805 shell_grab_end(grab);
1806 free(grab);
1807}
1808
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001809static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001810 busy_cursor_grab_focus,
1811 busy_cursor_grab_motion,
1812 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001813 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001814};
1815
1816static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001817set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001818{
1819 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001820
1821 grab = malloc(sizeof *grab);
1822 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001823 return;
1824
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001825 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1826 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001827}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001828
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001829static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001830end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001831{
1832 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1833
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001834 if (grab->grab.interface == &busy_cursor_grab_interface &&
1835 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001836 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001837 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001838 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001839}
1840
Scott Moreau9521d5e2012-04-19 13:06:17 -06001841static void
1842ping_timer_destroy(struct shell_surface *shsurf)
1843{
1844 if (!shsurf || !shsurf->ping_timer)
1845 return;
1846
1847 if (shsurf->ping_timer->source)
1848 wl_event_source_remove(shsurf->ping_timer->source);
1849
1850 free(shsurf->ping_timer);
1851 shsurf->ping_timer = NULL;
1852}
1853
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001854static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001855ping_timeout_handler(void *data)
1856{
1857 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001858 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001859
Scott Moreau9521d5e2012-04-19 13:06:17 -06001860 /* Client is not responding */
1861 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001862
1863 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001864 if (seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001865 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001866
1867 return 1;
1868}
1869
1870static void
1871ping_handler(struct weston_surface *surface, uint32_t serial)
1872{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001873 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001874 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001875 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001876
1877 if (!shsurf)
1878 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001879 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001880 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001881
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001882 if (shsurf->surface == shsurf->shell->grab_surface)
1883 return;
1884
Scott Moreauff1db4a2012-04-17 19:06:18 -06001885 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001886 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001887 if (!shsurf->ping_timer)
1888 return;
1889
1890 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001891 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1892 shsurf->ping_timer->source =
1893 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1894 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1895
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001896 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001897 }
1898}
1899
1900static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001901handle_pointer_focus(struct wl_listener *listener, void *data)
1902{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001903 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001904 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001905 struct weston_compositor *compositor;
1906 struct shell_surface *shsurf;
1907 uint32_t serial;
1908
Jason Ekstranda7af7042013-10-12 22:38:11 -05001909 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001910 return;
1911
Jason Ekstranda7af7042013-10-12 22:38:11 -05001912 compositor = view->surface->compositor;
1913 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001914
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001915 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001916 set_busy_cursor(shsurf, pointer);
1917 } else {
1918 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001919 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001920 }
1921}
1922
1923static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001924create_pointer_focus_listener(struct weston_seat *seat)
1925{
1926 struct wl_listener *listener;
1927
Kristian Høgsberge3148752013-05-06 23:19:49 -04001928 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001929 return;
1930
1931 listener = malloc(sizeof *listener);
1932 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001933 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001934}
1935
1936static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001937shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1938 uint32_t serial)
1939{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001940 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001941 struct weston_seat *seat;
1942 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001943
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001944 if (shsurf->ping_timer == NULL)
1945 /* Just ignore unsolicited pong. */
1946 return;
1947
Scott Moreauff1db4a2012-04-17 19:06:18 -06001948 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001949 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001950 wl_list_for_each(seat, &ec->seat_list, link) {
1951 if(seat->pointer)
1952 end_busy_cursor(shsurf, seat->pointer);
1953 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001954 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001955 }
1956}
1957
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001958static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001959set_title(struct shell_surface *shsurf, const char *title)
1960{
1961 free(shsurf->title);
1962 shsurf->title = strdup(title);
1963}
1964
1965static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001966shell_surface_set_title(struct wl_client *client,
1967 struct wl_resource *resource, const char *title)
1968{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001969 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001970
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001971 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001972}
1973
1974static void
1975shell_surface_set_class(struct wl_client *client,
1976 struct wl_resource *resource, const char *class)
1977{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001978 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001979
1980 free(shsurf->class);
1981 shsurf->class = strdup(class);
1982}
1983
Alex Wu4539b082012-03-01 12:57:46 +08001984static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001985restore_output_mode(struct weston_output *output)
1986{
Hardening57388e42013-09-18 23:56:36 +02001987 if (output->current_mode != output->original_mode ||
1988 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001989 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001990 output->original_mode,
1991 output->original_scale,
1992 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001993}
1994
1995static void
1996restore_all_output_modes(struct weston_compositor *compositor)
1997{
1998 struct weston_output *output;
1999
2000 wl_list_for_each(output, &compositor->output_list, link)
2001 restore_output_mode(output);
2002}
2003
2004static void
Alex Wu4539b082012-03-01 12:57:46 +08002005shell_unset_fullscreen(struct shell_surface *shsurf)
2006{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002007 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08002008 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08002009 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2010 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002011 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002012 }
Alex Wu4539b082012-03-01 12:57:46 +08002013 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2014 shsurf->fullscreen.framerate = 0;
2015 wl_list_remove(&shsurf->fullscreen.transform.link);
2016 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002017 if (shsurf->fullscreen.black_view)
2018 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2019 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002020 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002021 weston_view_set_position(shsurf->view,
2022 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002023 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002024 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002025 &shsurf->rotation.transform.link);
2026 shsurf->saved_rotation_valid = false;
2027 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002028
2029 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002030 wl_list_remove(&shsurf->view->layer_link);
2031 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08002032}
2033
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002034static void
2035shell_unset_maximized(struct shell_surface *shsurf)
2036{
2037 struct workspace *ws;
2038 /* undo all maximized things here */
2039 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002040 weston_view_set_position(shsurf->view,
2041 shsurf->saved_x,
2042 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002043
2044 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045 wl_list_insert(&shsurf->view->geometry.transformation_list,
2046 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002047 shsurf->saved_rotation_valid = false;
2048 }
2049
2050 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002051 wl_list_remove(&shsurf->view->layer_link);
2052 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002053}
2054
Pekka Paalanen98262232011-12-01 10:42:22 +02002055static int
2056reset_shell_surface_type(struct shell_surface *surface)
2057{
2058 switch (surface->type) {
2059 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08002060 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002061 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002062 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002063 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002064 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002065 case SHELL_SURFACE_NONE:
2066 case SHELL_SURFACE_TOPLEVEL:
2067 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002068 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002069 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02002070 break;
2071 }
2072
2073 surface->type = SHELL_SURFACE_NONE;
2074 return 0;
2075}
2076
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002077static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002078set_surface_type(struct shell_surface *shsurf)
2079{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002080 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002081 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002082
2083 reset_shell_surface_type(shsurf);
2084
2085 shsurf->type = shsurf->next_type;
2086 shsurf->next_type = SHELL_SURFACE_NONE;
2087
2088 switch (shsurf->type) {
2089 case SHELL_SURFACE_TOPLEVEL:
2090 break;
2091 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 if (pev)
2093 weston_view_set_position(shsurf->view,
2094 pev->geometry.x + shsurf->transient.x,
2095 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002096 break;
2097
2098 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002099 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100 shsurf->saved_x = shsurf->view->geometry.x;
2101 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002102 shsurf->saved_position_valid = true;
2103
2104 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2105 wl_list_remove(&shsurf->rotation.transform.link);
2106 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002107 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002108 shsurf->saved_rotation_valid = true;
2109 }
2110 break;
2111
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002112 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002113 weston_view_set_position(shsurf->view, shsurf->transient.x,
2114 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002115 break;
2116
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002117 default:
2118 break;
2119 }
2120}
2121
2122static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002123set_toplevel(struct shell_surface *shsurf)
2124{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002125 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002126}
2127
2128static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002129shell_surface_set_toplevel(struct wl_client *client,
2130 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002131{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002132 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002133
Tiago Vignattibc052c92012-04-19 16:18:18 +03002134 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002135}
2136
2137static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04002138set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002139 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04002140{
2141 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002142 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002143 shsurf->transient.x = x;
2144 shsurf->transient.y = y;
2145 shsurf->transient.flags = flags;
2146 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
2147}
2148
2149static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002150shell_surface_set_transient(struct wl_client *client,
2151 struct wl_resource *resource,
2152 struct wl_resource *parent_resource,
2153 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002154{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002155 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002156 struct weston_surface *parent =
2157 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002158
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002159 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002160}
2161
Tiago Vignattibe143262012-04-16 17:31:41 +03002162static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002163shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002164{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002165 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002166}
2167
2168static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002169get_output_panel_height(struct desktop_shell *shell,
2170 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08002171{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08002173 int panel_height = 0;
2174
2175 if (!output)
2176 return 0;
2177
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2179 if (view->surface->output == output) {
2180 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08002181 break;
2182 }
2183 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002184
Juan Zhao96879df2012-02-07 08:45:41 +08002185 return panel_height;
2186}
2187
2188static void
2189shell_surface_set_maximized(struct wl_client *client,
2190 struct wl_resource *resource,
2191 struct wl_resource *output_resource )
2192{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002193 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08002194 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03002195 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08002196 uint32_t edges = 0, panel_height = 0;
2197
2198 /* get the default output, if the client set it as NULL
2199 check whether the ouput is available */
2200 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002201 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002202 else if (es->output)
2203 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002204 else
2205 shsurf->output = get_default_output(es->compositor);
2206
Tiago Vignattibe143262012-04-16 17:31:41 +03002207 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01002208 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002209 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002210
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002211 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002212 shsurf->output->width,
2213 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002214
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002215 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002216}
2217
Alex Wu21858432012-04-01 20:13:08 +08002218static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002219black_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 +08002220
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002222create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002223 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002224 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002225{
2226 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002227 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002228
2229 surface = weston_surface_create(ec);
2230 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002231 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002232 return NULL;
2233 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002234 view = weston_view_create(surface);
2235 if (surface == NULL) {
2236 weston_log("no memory\n");
2237 weston_surface_destroy(surface);
2238 return NULL;
2239 }
Alex Wu4539b082012-03-01 12:57:46 +08002240
Alex Wu21858432012-04-01 20:13:08 +08002241 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002242 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002243 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002244 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002245 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002246 pixman_region32_fini(&surface->input);
2247 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002248
Jason Ekstranda7af7042013-10-12 22:38:11 -05002249 weston_view_configure(view, x, y, w, h);
2250
2251 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002252}
2253
2254/* Create black surface and append it to the associated fullscreen surface.
2255 * Handle size dismatch and positioning according to the method. */
2256static void
2257shell_configure_fullscreen(struct shell_surface *shsurf)
2258{
2259 struct weston_output *output = shsurf->fullscreen_output;
2260 struct weston_surface *surface = shsurf->surface;
2261 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002262 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002263 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002264
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002265 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2266 restore_output_mode(output);
2267
Jason Ekstranda7af7042013-10-12 22:38:11 -05002268 if (!shsurf->fullscreen.black_view)
2269 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002270 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002271 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002272 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002273 output->width,
2274 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002275
Jason Ekstranda7af7042013-10-12 22:38:11 -05002276 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2277 wl_list_insert(&shsurf->view->layer_link,
2278 &shsurf->fullscreen.black_view->layer_link);
2279 shsurf->fullscreen.black_view->surface->output = output;
2280 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002281
Jason Ekstranda7af7042013-10-12 22:38:11 -05002282 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002283 &surf_width, &surf_height);
2284
Alex Wu4539b082012-03-01 12:57:46 +08002285 switch (shsurf->fullscreen.type) {
2286 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002287 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002289 break;
2290 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002291 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002292 if (output->width == surf_width &&
2293 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002294 weston_view_set_position(shsurf->view,
2295 output->x - surf_x,
2296 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002297 break;
2298 }
2299
Alex Wu4539b082012-03-01 12:57:46 +08002300 matrix = &shsurf->fullscreen.transform.matrix;
2301 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002302
Scott Moreau1bad5db2012-08-18 01:04:05 -06002303 output_aspect = (float) output->width /
2304 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 /* XXX: Use surf_width and surf_height here? */
2306 surface_aspect = (float) surface->width /
2307 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002308 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002309 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002310 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002311 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002312 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002313 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002314
Alex Wu4539b082012-03-01 12:57:46 +08002315 weston_matrix_scale(matrix, scale, scale, 1);
2316 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002317 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002318 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002319 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2320 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002321 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002322
Alex Wu4539b082012-03-01 12:57:46 +08002323 break;
2324 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002325 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002326 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02002327 surf_width * surface->buffer_scale,
2328 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002329 shsurf->fullscreen.framerate};
2330
Hardening57388e42013-09-18 23:56:36 +02002331 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
2332 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002333 weston_view_set_position(shsurf->view,
2334 output->x - surf_x,
2335 output->y - surf_y);
2336 weston_view_configure(shsurf->fullscreen.black_view,
2337 output->x - surf_x,
2338 output->y - surf_y,
2339 output->width,
2340 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002341 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002342 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002343 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002344 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002345 }
Alex Wubd3354b2012-04-17 17:20:49 +08002346 }
Alex Wu4539b082012-03-01 12:57:46 +08002347 break;
2348 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002349 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002350 break;
2351 default:
2352 break;
2353 }
2354}
2355
2356/* make the fullscreen and black surface at the top */
2357static void
2358shell_stack_fullscreen(struct shell_surface *shsurf)
2359{
Alex Wubd3354b2012-04-17 17:20:49 +08002360 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002361 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002362
Jason Ekstranda7af7042013-10-12 22:38:11 -05002363 wl_list_remove(&shsurf->view->layer_link);
2364 wl_list_insert(&shell->fullscreen_layer.view_list,
2365 &shsurf->view->layer_link);
2366 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002367
Jason Ekstranda7af7042013-10-12 22:38:11 -05002368 if (!shsurf->fullscreen.black_view)
2369 shsurf->fullscreen.black_view =
2370 create_black_surface(shsurf->surface->compositor,
2371 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002372 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002373 output->width,
2374 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002375
Jason Ekstranda7af7042013-10-12 22:38:11 -05002376 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2377 wl_list_insert(&shsurf->view->layer_link,
2378 &shsurf->fullscreen.black_view->layer_link);
2379 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002380}
2381
2382static void
2383shell_map_fullscreen(struct shell_surface *shsurf)
2384{
Alex Wu4539b082012-03-01 12:57:46 +08002385 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002386 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002387}
2388
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002389static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002390set_fullscreen(struct shell_surface *shsurf,
2391 uint32_t method,
2392 uint32_t framerate,
2393 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002394{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002395 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002396
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002397 if (output)
2398 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002399 else if (es->output)
2400 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002401 else
2402 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002403
Alex Wu4539b082012-03-01 12:57:46 +08002404 shsurf->fullscreen_output = shsurf->output;
2405 shsurf->fullscreen.type = method;
2406 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002407 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002408
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002409 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002410 shsurf->output->width,
2411 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002412}
2413
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002414static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002415shell_surface_set_fullscreen(struct wl_client *client,
2416 struct wl_resource *resource,
2417 uint32_t method,
2418 uint32_t framerate,
2419 struct wl_resource *output_resource)
2420{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002421 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002422 struct weston_output *output;
2423
2424 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002425 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002426 else
2427 output = NULL;
2428
2429 set_fullscreen(shsurf, method, framerate, output);
2430}
2431
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002432static void
2433set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2434{
2435 /* XXX: using the same fields for transient type */
2436 shsurf->transient.x = x;
2437 shsurf->transient.y = y;
2438 shsurf->transient.flags = flags;
2439 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2440}
2441
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002442static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002443
2444static void
2445destroy_shell_seat(struct wl_listener *listener, void *data)
2446{
2447 struct shell_seat *shseat =
2448 container_of(listener,
2449 struct shell_seat, seat_destroy_listener);
2450 struct shell_surface *shsurf, *prev = NULL;
2451
2452 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002453 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002454 shseat->popup_grab.client = NULL;
2455
2456 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2457 shsurf->popup.shseat = NULL;
2458 if (prev) {
2459 wl_list_init(&prev->popup.grab_link);
2460 }
2461 prev = shsurf;
2462 }
2463 wl_list_init(&prev->popup.grab_link);
2464 }
2465
2466 wl_list_remove(&shseat->seat_destroy_listener.link);
2467 free(shseat);
2468}
2469
2470static struct shell_seat *
2471create_shell_seat(struct weston_seat *seat)
2472{
2473 struct shell_seat *shseat;
2474
2475 shseat = calloc(1, sizeof *shseat);
2476 if (!shseat) {
2477 weston_log("no memory to allocate shell seat\n");
2478 return NULL;
2479 }
2480
2481 shseat->seat = seat;
2482 wl_list_init(&shseat->popup_grab.surfaces_list);
2483
2484 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2485 wl_signal_add(&seat->destroy_signal,
2486 &shseat->seat_destroy_listener);
2487
2488 return shseat;
2489}
2490
2491static struct shell_seat *
2492get_shell_seat(struct weston_seat *seat)
2493{
2494 struct wl_listener *listener;
2495
2496 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2497 if (listener == NULL)
2498 return create_shell_seat(seat);
2499
2500 return container_of(listener,
2501 struct shell_seat, seat_destroy_listener);
2502}
2503
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002504static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002505popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002506{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002507 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002508 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002509 struct shell_seat *shseat =
2510 container_of(grab, struct shell_seat, popup_grab.grab);
2511 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002512 wl_fixed_t sx, sy;
2513
Jason Ekstranda7af7042013-10-12 22:38:11 -05002514 view = weston_compositor_pick_view(pointer->seat->compositor,
2515 pointer->x, pointer->y,
2516 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002517
Jason Ekstranda7af7042013-10-12 22:38:11 -05002518 if (view && view->surface->resource &&
2519 wl_resource_get_client(view->surface->resource) == client) {
2520 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002521 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002522 weston_pointer_set_focus(pointer, NULL,
2523 wl_fixed_from_int(0),
2524 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002525 }
2526}
2527
2528static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002529popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2530 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002531{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002532 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002533 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002534 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002535
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002536 weston_pointer_move(pointer, x, y);
2537
Neil Roberts96d790e2013-09-19 17:32:00 +01002538 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002539 weston_view_from_global_fixed(pointer->focus,
2540 pointer->x, pointer->y,
2541 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002542 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002543 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002544}
2545
2546static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002547popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002548 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002549{
2550 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002551 struct shell_seat *shseat =
2552 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002553 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002554 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002555 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002556 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002557
Neil Roberts96d790e2013-09-19 17:32:00 +01002558 resource_list = &grab->pointer->focus_resource_list;
2559 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002560 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002561 wl_resource_for_each(resource, resource_list) {
2562 wl_pointer_send_button(resource, serial,
2563 time, button, state);
2564 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002565 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002566 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002567 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002568 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002569 }
2570
Daniel Stone4dbadb12012-05-30 16:31:51 +01002571 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002572 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002573}
2574
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002575static void
2576popup_grab_cancel(struct weston_pointer_grab *grab)
2577{
2578 popup_grab_end(grab->pointer);
2579}
2580
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002581static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002582 popup_grab_focus,
2583 popup_grab_motion,
2584 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002585 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002586};
2587
2588static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002589popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002590{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002591 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002592 struct shell_seat *shseat =
2593 container_of(grab, struct shell_seat, popup_grab.grab);
2594 struct shell_surface *shsurf;
2595 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002596
2597 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002598 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002599 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002600 shseat->popup_grab.grab.interface = NULL;
2601 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002602 /* Send the popup_done event to all the popups open */
2603 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002604 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002605 shsurf->popup.shseat = NULL;
2606 if (prev) {
2607 wl_list_init(&prev->popup.grab_link);
2608 }
2609 prev = shsurf;
2610 }
2611 wl_list_init(&prev->popup.grab_link);
2612 wl_list_init(&shseat->popup_grab.surfaces_list);
2613 }
2614}
2615
2616static void
2617add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2618{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002619 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002620
2621 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002622 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002623 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002624 /* We must make sure here that this popup was opened after
2625 * a mouse press, and not just by moving around with other
2626 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002627 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002628 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002629
Rob Bradforddfe31052013-06-26 19:49:11 +01002630 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002631 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002632 } else {
2633 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002634 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002635}
2636
2637static void
2638remove_popup_grab(struct shell_surface *shsurf)
2639{
2640 struct shell_seat *shseat = shsurf->popup.shseat;
2641
2642 wl_list_remove(&shsurf->popup.grab_link);
2643 wl_list_init(&shsurf->popup.grab_link);
2644 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002645 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002646 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002647 }
2648}
2649
2650static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002651shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002652{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002653 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002654 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002655
Jason Ekstranda7af7042013-10-12 22:38:11 -05002656 shsurf->surface->output = parent_view->output;
2657 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002658
Jason Ekstranda7af7042013-10-12 22:38:11 -05002659 weston_view_set_transform_parent(shsurf->view, parent_view);
2660 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2661 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002662
Kristian Høgsberge3148752013-05-06 23:19:49 -04002663 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002664 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002665 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002666 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002667 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002668 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002669}
2670
2671static void
2672shell_surface_set_popup(struct wl_client *client,
2673 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002674 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002675 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002676 struct wl_resource *parent_resource,
2677 int32_t x, int32_t y, uint32_t flags)
2678{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002679 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002680
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002681 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002682 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002683 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002684 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002685 shsurf->popup.x = x;
2686 shsurf->popup.y = y;
2687}
2688
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002689static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002690 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002691 shell_surface_move,
2692 shell_surface_resize,
2693 shell_surface_set_toplevel,
2694 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002695 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002696 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002697 shell_surface_set_maximized,
2698 shell_surface_set_title,
2699 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002700};
2701
2702static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002703destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002704{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002705 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2706
Giulio Camuffo5085a752013-03-25 21:42:45 +01002707 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2708 remove_popup_grab(shsurf);
2709 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002710
Alex Wubd3354b2012-04-17 17:20:49 +08002711 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002712 shell_surface_is_top_fullscreen(shsurf))
2713 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002714
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 if (shsurf->fullscreen.black_view)
2716 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002717
Alex Wubd3354b2012-04-17 17:20:49 +08002718 /* As destroy_resource() use wl_list_for_each_safe(),
2719 * we can always remove the listener.
2720 */
2721 wl_list_remove(&shsurf->surface_destroy_listener.link);
2722 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002723 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002724 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002725
Jason Ekstranda7af7042013-10-12 22:38:11 -05002726 weston_view_destroy(shsurf->view);
2727
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002728 wl_list_remove(&shsurf->link);
2729 free(shsurf);
2730}
2731
2732static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002733shell_destroy_shell_surface(struct wl_resource *resource)
2734{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002735 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002736
2737 destroy_shell_surface(shsurf);
2738}
2739
2740static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002741shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002742{
2743 struct shell_surface *shsurf = container_of(listener,
2744 struct shell_surface,
2745 surface_destroy_listener);
2746
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002747 if (shsurf->resource)
2748 wl_resource_destroy(shsurf->resource);
2749 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002750 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002751}
2752
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002753static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002754shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002755
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002756static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002757get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002758{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002759 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002760 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002761 else
2762 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002763}
2764
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002765static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002766create_shell_surface(void *shell, struct weston_surface *surface,
2767 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002768{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002769 struct shell_surface *shsurf;
2770
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002771 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002772 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002773 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002774 }
2775
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002776 shsurf = calloc(1, sizeof *shsurf);
2777 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002778 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002779 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002780 }
2781
Jason Ekstranda7af7042013-10-12 22:38:11 -05002782 shsurf->view = weston_view_create(surface);
2783 if (!shsurf->view) {
2784 weston_log("no memory to allocate shell surface\n");
2785 free(shsurf);
2786 return NULL;
2787 }
2788
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002789 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002790 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002791
Tiago Vignattibc052c92012-04-19 16:18:18 +03002792 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002793 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002794 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002795 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002796 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002797 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2798 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002799 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002800 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002801 wl_list_init(&shsurf->fullscreen.transform.link);
2802
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002803 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002804 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002805 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002806 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002807
2808 /* init link so its safe to always remove it in destroy_shell_surface */
2809 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002810 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002811
Pekka Paalanen460099f2012-01-20 16:48:25 +02002812 /* empty when not in use */
2813 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002814 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002815
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002816 wl_list_init(&shsurf->workspace_transform.link);
2817
Pekka Paalanen98262232011-12-01 10:42:22 +02002818 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002819 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002820
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002821 shsurf->client = client;
2822
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002823 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002824}
2825
Jason Ekstranda7af7042013-10-12 22:38:11 -05002826static struct weston_view *
2827get_primary_view(void *shell, struct shell_surface *shsurf)
2828{
2829 return shsurf->view;
2830}
2831
Tiago Vignattibc052c92012-04-19 16:18:18 +03002832static void
2833shell_get_shell_surface(struct wl_client *client,
2834 struct wl_resource *resource,
2835 uint32_t id,
2836 struct wl_resource *surface_resource)
2837{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002838 struct weston_surface *surface =
2839 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002840 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002841 struct shell_surface *shsurf;
2842
2843 if (get_shell_surface(surface)) {
2844 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002845 WL_DISPLAY_ERROR_INVALID_OBJECT,
2846 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002847 return;
2848 }
2849
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002850 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002851 if (!shsurf) {
2852 wl_resource_post_error(surface_resource,
2853 WL_DISPLAY_ERROR_INVALID_OBJECT,
2854 "surface->configure already set");
2855 return;
2856 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002857
Jason Ekstranda85118c2013-06-27 20:17:02 -05002858 shsurf->resource =
2859 wl_resource_create(client,
2860 &wl_shell_surface_interface, 1, id);
2861 wl_resource_set_implementation(shsurf->resource,
2862 &shell_surface_implementation,
2863 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002864}
2865
2866static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002867 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002868};
2869
Kristian Høgsberg07937562011-04-12 17:25:42 -04002870static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002871shell_fade(struct desktop_shell *shell, enum fade_type type);
2872
2873static int
2874screensaver_timeout(void *data)
2875{
2876 struct desktop_shell *shell = data;
2877
2878 shell_fade(shell, FADE_OUT);
2879
2880 return 1;
2881}
2882
2883static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002884handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002885{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002886 struct desktop_shell *shell =
2887 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002888
Pekka Paalanen18027e52011-12-02 16:31:49 +02002889 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002890
2891 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002892 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002893}
2894
2895static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002896launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002897{
2898 if (shell->screensaver.binding)
2899 return;
2900
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002901 if (!shell->screensaver.path) {
2902 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002903 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002904 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002905
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002906 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002907 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002908 return;
2909 }
2910
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002911 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002912 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002913 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002914 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002915}
2916
2917static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002918terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002919{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002920 if (shell->screensaver.process.pid == 0)
2921 return;
2922
2923 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002924}
2925
2926static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002927configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002928{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002929 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002930
Giulio Camuffo184df502013-02-21 11:29:21 +01002931 if (width == 0)
2932 return;
2933
Jason Ekstranda7af7042013-10-12 22:38:11 -05002934 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2935 if (v->output == ev->output && v != ev) {
2936 weston_view_unmap(v);
2937 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002938 }
2939 }
2940
Jason Ekstranda7af7042013-10-12 22:38:11 -05002941 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002942
Jason Ekstranda7af7042013-10-12 22:38:11 -05002943 if (wl_list_empty(&ev->layer_link)) {
2944 wl_list_insert(&layer->view_list, &ev->layer_link);
2945 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002946 }
2947}
2948
2949static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002950background_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002951{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002952 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002953 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002954
Jason Ekstranda7af7042013-10-12 22:38:11 -05002955 view = container_of(es->views.next, struct weston_view, surface_link);
2956
2957 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002958}
2959
2960static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002961desktop_shell_set_background(struct wl_client *client,
2962 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002963 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002964 struct wl_resource *surface_resource)
2965{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002966 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002967 struct weston_surface *surface =
2968 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002969 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002970
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002971 if (surface->configure) {
2972 wl_resource_post_error(surface_resource,
2973 WL_DISPLAY_ERROR_INVALID_OBJECT,
2974 "surface role already assigned");
2975 return;
2976 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002977
Jason Ekstranda7af7042013-10-12 22:38:11 -05002978 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2979 weston_view_destroy(view);
2980 view = weston_view_create(surface);
2981
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002982 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002983 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002984 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002985 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002986 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002987 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002988 surface->output->width,
2989 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002990}
2991
2992static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002993panel_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 -04002994{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002995 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002996 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002997
Jason Ekstranda7af7042013-10-12 22:38:11 -05002998 view = container_of(es->views.next, struct weston_view, surface_link);
2999
3000 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003001}
3002
3003static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003004desktop_shell_set_panel(struct wl_client *client,
3005 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003006 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003007 struct wl_resource *surface_resource)
3008{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003009 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003010 struct weston_surface *surface =
3011 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003012 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003013
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003014 if (surface->configure) {
3015 wl_resource_post_error(surface_resource,
3016 WL_DISPLAY_ERROR_INVALID_OBJECT,
3017 "surface role already assigned");
3018 return;
3019 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003020
Jason Ekstranda7af7042013-10-12 22:38:11 -05003021 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3022 weston_view_destroy(view);
3023 view = weston_view_create(surface);
3024
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003025 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003026 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003027 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003028 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003029 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003030 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003031 surface->output->width,
3032 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003033}
3034
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003035static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003036lock_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 -04003037{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003038 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003039 struct weston_view *view;
3040
3041 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003042
Giulio Camuffo184df502013-02-21 11:29:21 +01003043 if (width == 0)
3044 return;
3045
Jason Ekstranda7af7042013-10-12 22:38:11 -05003046 surface->width = width;
3047 surface->height = height;
3048 view->geometry.width = width;
3049 view->geometry.height = height;
3050 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003051
3052 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003053 wl_list_insert(&shell->lock_layer.view_list,
3054 &view->layer_link);
3055 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003056 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003057 }
3058}
3059
3060static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003061handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003062{
Tiago Vignattibe143262012-04-16 17:31:41 +03003063 struct desktop_shell *shell =
3064 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003065
Martin Minarik6d118362012-06-07 18:01:59 +02003066 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003067 shell->lock_surface = NULL;
3068}
3069
3070static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003071desktop_shell_set_lock_surface(struct wl_client *client,
3072 struct wl_resource *resource,
3073 struct wl_resource *surface_resource)
3074{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003075 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003076 struct weston_surface *surface =
3077 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003078
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003079 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003080
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003081 if (!shell->locked)
3082 return;
3083
Pekka Paalanen98262232011-12-01 10:42:22 +02003084 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003085
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003086 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003087 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003088 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003089
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003090 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003091 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003092 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003093}
3094
3095static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003096resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003097{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003098 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003099
Pekka Paalanen77346a62011-11-30 16:26:35 +02003100 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003101
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003102 wl_list_remove(&shell->lock_layer.link);
3103 wl_list_insert(&shell->compositor->cursor_layer.link,
3104 &shell->fullscreen_layer.link);
3105 wl_list_insert(&shell->fullscreen_layer.link,
3106 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003107 if (shell->showing_input_panels) {
3108 wl_list_insert(&shell->panel_layer.link,
3109 &shell->input_panel_layer.link);
3110 wl_list_insert(&shell->input_panel_layer.link,
3111 &ws->layer.link);
3112 } else {
3113 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3114 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003115
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003116 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003117
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003118 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003119 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003120 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003121}
3122
3123static void
3124desktop_shell_unlock(struct wl_client *client,
3125 struct wl_resource *resource)
3126{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003127 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003128
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003129 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003130
3131 if (shell->locked)
3132 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003133}
3134
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003135static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003136desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003137 struct wl_resource *resource,
3138 struct wl_resource *surface_resource)
3139{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003140 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003141
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003142 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003143 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003144}
3145
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003146static void
3147desktop_shell_desktop_ready(struct wl_client *client,
3148 struct wl_resource *resource)
3149{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003150 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003151
3152 shell_fade_startup(shell);
3153}
3154
Kristian Høgsberg75840622011-09-06 13:48:16 -04003155static const struct desktop_shell_interface desktop_shell_implementation = {
3156 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003157 desktop_shell_set_panel,
3158 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003159 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003160 desktop_shell_set_grab_surface,
3161 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003162};
3163
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003164static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003165get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003166{
3167 struct shell_surface *shsurf;
3168
3169 shsurf = get_shell_surface(surface);
3170 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003171 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003172 return shsurf->type;
3173}
3174
Kristian Høgsberg75840622011-09-06 13:48:16 -04003175static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003176move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003177{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003178 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003179 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003180 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003181
Pekka Paalanen01388e22013-04-25 13:57:44 +03003182 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003183 if (surface == NULL)
3184 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003185
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003186 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003187 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3188 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003189 return;
3190
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003191 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003192}
3193
3194static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003195touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3196{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003197 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003198 struct weston_surface *surface;
3199 struct shell_surface *shsurf;
3200
3201 surface = weston_surface_get_main_surface(focus);
3202 if (surface == NULL)
3203 return;
3204
3205 shsurf = get_shell_surface(surface);
3206 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3207 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3208 return;
3209
3210 surface_touch_move(shsurf, (struct weston_seat *) seat);
3211}
3212
3213static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003214resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003215{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003216 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003217 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003218 uint32_t edges = 0;
3219 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003220 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003221
Pekka Paalanen01388e22013-04-25 13:57:44 +03003222 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003223 if (surface == NULL)
3224 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003225
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003226 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003227 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3228 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003229 return;
3230
Jason Ekstranda7af7042013-10-12 22:38:11 -05003231 weston_view_from_global(shsurf->view,
3232 wl_fixed_to_int(seat->pointer->grab_x),
3233 wl_fixed_to_int(seat->pointer->grab_y),
3234 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003235
Jason Ekstranda7af7042013-10-12 22:38:11 -05003236 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003237 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003238 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003239 edges |= 0;
3240 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003241 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003242
Jason Ekstranda7af7042013-10-12 22:38:11 -05003243 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003244 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003245 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003246 edges |= 0;
3247 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003248 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003249
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003250 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003251}
3252
3253static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003254surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003255 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003256{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003257 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003258 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003259 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003260 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003261
Pekka Paalanen01388e22013-04-25 13:57:44 +03003262 /* XXX: broken for windows containing sub-surfaces */
3263 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003264 if (surface == NULL)
3265 return;
3266
3267 shsurf = get_shell_surface(surface);
3268 if (!shsurf)
3269 return;
3270
Jason Ekstranda7af7042013-10-12 22:38:11 -05003271 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003272
Jason Ekstranda7af7042013-10-12 22:38:11 -05003273 if (shsurf->view->alpha > 1.0)
3274 shsurf->view->alpha = 1.0;
3275 if (shsurf->view->alpha < step)
3276 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003277
Jason Ekstranda7af7042013-10-12 22:38:11 -05003278 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003279 weston_surface_damage(surface);
3280}
3281
3282static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003283do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003284 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003285{
Daniel Stone37816df2012-05-16 18:45:18 +01003286 struct weston_seat *ws = (struct weston_seat *) seat;
3287 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003288 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003289 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003290
3291 wl_list_for_each(output, &compositor->output_list, link) {
3292 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003293 wl_fixed_to_double(seat->pointer->x),
3294 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003295 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003296 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003297 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003298 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003299 increment = -output->zoom.increment;
3300 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003301 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003302 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003303 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003304 else
3305 increment = 0;
3306
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003307 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003308
Scott Moreaue6603982012-06-11 13:07:51 -06003309 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003310 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003311 else if (output->zoom.level > output->zoom.max_level)
3312 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003313 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003314 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003315 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003316
Scott Moreaue6603982012-06-11 13:07:51 -06003317 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003318
Jason Ekstranda7af7042013-10-12 22:38:11 -05003319 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003320 }
3321 }
3322}
3323
Scott Moreauccbf29d2012-02-22 14:21:41 -07003324static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003325zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003326 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003327{
3328 do_zoom(seat, time, 0, axis, value);
3329}
3330
3331static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003332zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003333 void *data)
3334{
3335 do_zoom(seat, time, key, 0, 0);
3336}
3337
3338static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003339terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003340 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003341{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003342 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003343
Daniel Stone325fc2d2012-05-30 16:31:58 +01003344 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003345}
3346
3347static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003348lower_fullscreen_layer(struct desktop_shell *shell);
3349
3350struct alt_tab {
3351 struct desktop_shell *shell;
3352 struct weston_keyboard_grab grab;
3353
3354 struct wl_list *current;
3355
3356 struct wl_list preview_list;
3357};
3358
3359struct alt_tab_preview {
3360 struct alt_tab *alt_tab;
3361
3362 struct weston_view *view;
3363 struct weston_transform transform;
3364
3365 struct wl_listener listener;
3366
3367 struct wl_list link;
3368};
3369
3370static void
3371alt_tab_next(struct alt_tab *alt_tab)
3372{
3373 alt_tab->current = alt_tab->current->next;
3374
3375 /* Make sure we're not pointing to the list header e.g. after
3376 * cycling through the whole list. */
3377 if (alt_tab->current->next == alt_tab->preview_list.next)
3378 alt_tab->current = alt_tab->current->next;
3379}
3380
3381static void
3382alt_tab_destroy(struct alt_tab *alt_tab)
3383{
3384 struct alt_tab_preview *preview, *next;
3385 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3386
3387 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3388 preview = wl_container_of(alt_tab->current, preview, link);
3389
3390 activate(alt_tab->shell, preview->view->surface,
3391 (struct weston_seat *) keyboard->seat);
3392 }
3393
3394 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3395 wl_list_remove(&preview->link);
3396 wl_list_remove(&preview->listener.link);
3397 weston_view_destroy(preview->view);
3398 free(preview);
3399 }
3400
3401 weston_keyboard_end_grab(keyboard);
3402 if (keyboard->input_method_resource)
3403 keyboard->grab = &keyboard->input_method_grab;
3404
3405 free(alt_tab);
3406}
3407
3408static void
3409alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3410{
3411 struct alt_tab_preview *preview =
3412 container_of(listener, struct alt_tab_preview, listener);
3413 struct alt_tab *alt_tab = preview->alt_tab;
3414 int advance = 0;
3415
3416 /* If the preview that we're removing is the currently selected one,
3417 * we want to move to the next one. So we move to ->prev and then
3418 * call _next() after removing the preview. */
3419 if (alt_tab->current == &preview->link) {
3420 alt_tab->current = alt_tab->current->prev;
3421 advance = 1;
3422 }
3423
3424 wl_list_remove(&preview->listener.link);
3425 wl_list_remove(&preview->link);
3426
3427 free(preview);
3428
3429 if (advance)
3430 alt_tab_next(alt_tab);
3431
3432 /* If the last preview goes away, stop the alt-tab */
3433 if (wl_list_empty(alt_tab->current))
3434 alt_tab_destroy(alt_tab);
3435}
3436
3437static void
3438alt_tab_key(struct weston_keyboard_grab *grab,
3439 uint32_t time, uint32_t key, uint32_t state_w)
3440{
3441 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3442 enum wl_keyboard_key_state state = state_w;
3443
3444 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3445 alt_tab_next(alt_tab);
3446}
3447
3448static void
3449alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3450 uint32_t mods_depressed, uint32_t mods_latched,
3451 uint32_t mods_locked, uint32_t group)
3452{
3453 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3454 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3455
3456 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3457 alt_tab_destroy(alt_tab);
3458}
3459
3460static void
3461alt_tab_cancel(struct weston_keyboard_grab *grab)
3462{
3463 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3464
3465 alt_tab_destroy(alt_tab);
3466}
3467
3468static const struct weston_keyboard_grab_interface alt_tab_grab = {
3469 alt_tab_key,
3470 alt_tab_modifier,
3471 alt_tab_cancel,
3472};
3473
3474static int
3475view_for_alt_tab(struct weston_view *view)
3476{
3477 if (!get_shell_surface(view->surface))
3478 return 0;
3479
3480 if (get_shell_surface_type(view->surface) == SHELL_SURFACE_TRANSIENT)
3481 return 0;
3482
3483 if (view != get_default_view(view->surface))
3484 return 0;
3485
3486 return 1;
3487}
3488
3489static void
3490alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3491 void *data)
3492{
3493 struct alt_tab *alt_tab;
3494 struct desktop_shell *shell = data;
3495 struct weston_output *output = get_default_output(shell->compositor);
3496 struct workspace *ws;
3497 struct weston_view *view;
3498 int num_surfaces = 0;
3499 int x, y, side, margin;
3500
3501 wl_list_for_each(view, &shell->compositor->view_list, link) {
3502
3503 if (view_for_alt_tab(view))
3504 num_surfaces++;
3505 }
3506
3507 if (!num_surfaces)
3508 return;
3509
3510 alt_tab = malloc(sizeof *alt_tab);
3511 if (!alt_tab)
3512 return;
3513
3514 alt_tab->shell = shell;
3515 wl_list_init(&alt_tab->preview_list);
3516 alt_tab->current = &alt_tab->preview_list;
3517
3518 restore_all_output_modes(shell->compositor);
3519 lower_fullscreen_layer(alt_tab->shell);
3520
3521 alt_tab->grab.interface = &alt_tab_grab;
3522 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3523 weston_keyboard_set_focus(seat->keyboard, NULL);
3524
3525 ws = get_current_workspace(shell);
3526
3527 /* FIXME: add some visual candy e.g. prelight the selected view
3528 * and/or add a black surrounding background à la gnome-shell */
3529
3530 /* Determine the size for each preview */
3531 side = output->width / num_surfaces;
3532 if (side > 200)
3533 side = 200;
3534 margin = side / 4;
3535 side -= margin;
3536
3537 x = margin;
3538 y = (output->height - side) / 2;
3539
3540 /* Create a view for each surface */
3541 wl_list_for_each(view, &shell->compositor->view_list, link) {
3542 struct alt_tab_preview *preview;
3543 struct weston_view *v;
3544 float scale;
3545
3546 if (!view_for_alt_tab(view))
3547 continue;
3548
3549 preview = malloc(sizeof *preview);
3550 if (!preview) {
3551 alt_tab_destroy(alt_tab);
3552 return;
3553 }
3554
3555 preview->alt_tab = alt_tab;
3556
3557 preview->view = v = weston_view_create(view->surface);
3558 v->output = view->output;
3559 weston_view_restack(v, &ws->layer.view_list);
3560 weston_view_configure(v, x, y, view->geometry.width, view->geometry.height);
3561
3562 preview->listener.notify = alt_tab_handle_surface_destroy;
3563 wl_signal_add(&v->destroy_signal, &preview->listener);
3564
3565 if (view->geometry.width > view->geometry.height)
3566 scale = side / (float) view->geometry.width;
3567 else
3568 scale = side / (float) view->geometry.height;
3569
3570 wl_list_insert(&v->geometry.transformation_list,
3571 &preview->transform.link);
3572 weston_matrix_init(&preview->transform.matrix);
3573 weston_matrix_scale(&preview->transform.matrix,
3574 scale, scale, 1.0f);
3575
3576 weston_view_geometry_dirty(v);
3577 weston_compositor_schedule_repaint(v->surface->compositor);
3578
3579 /* Insert at the end of the list */
3580 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3581
3582 x += side + margin;
3583 }
3584
3585 /* Start at the second preview so a simple <alt>tab changes window.
3586 * We set `current' to the first preview and not the second because
3587 * we're going to receive a key press callback for the initial
3588 * <alt>tab which will make `current' point to the second element. */
3589 alt_tab_next(alt_tab);
3590}
3591
3592static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003593rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3594 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003595{
3596 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003597 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003598 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003599 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003600 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003601
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003602 weston_pointer_move(pointer, x, y);
3603
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003604 if (!shsurf)
3605 return;
3606
Jason Ekstranda7af7042013-10-12 22:38:11 -05003607 cx = 0.5f * shsurf->view->geometry.width;
3608 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003609
Daniel Stone37816df2012-05-16 18:45:18 +01003610 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3611 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003612 r = sqrtf(dx * dx + dy * dy);
3613
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003614 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003615 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003616
3617 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003618 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003619 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003620
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003621 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003622 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003623
3624 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003625 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003626 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003627 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003628 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003629
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003630 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003631 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003632 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003633 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003634 wl_list_init(&shsurf->rotation.transform.link);
3635 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003636 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003637 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003638
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003639 /* We need to adjust the position of the surface
3640 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003641 cposx = shsurf->view->geometry.x + cx;
3642 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003643 dposx = rotate->center.x - cposx;
3644 dposy = rotate->center.y - cposy;
3645 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003646 weston_view_set_position(shsurf->view,
3647 shsurf->view->geometry.x + dposx,
3648 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003649 }
3650
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003651 /* Repaint implies weston_surface_update_transform(), which
3652 * lazily applies the damage due to rotation update.
3653 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003654 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003655}
3656
3657static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003658rotate_grab_button(struct weston_pointer_grab *grab,
3659 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003660{
3661 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003662 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003663 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003664 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003665 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003666
Daniel Stone4dbadb12012-05-30 16:31:51 +01003667 if (pointer->button_count == 0 &&
3668 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003669 if (shsurf)
3670 weston_matrix_multiply(&shsurf->rotation.rotation,
3671 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003672 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003673 free(rotate);
3674 }
3675}
3676
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003677static void
3678rotate_grab_cancel(struct weston_pointer_grab *grab)
3679{
3680 struct rotate_grab *rotate =
3681 container_of(grab, struct rotate_grab, base.grab);
3682
3683 shell_grab_end(&rotate->base);
3684 free(rotate);
3685}
3686
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003687static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003688 noop_grab_focus,
3689 rotate_grab_motion,
3690 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003691 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003692};
3693
3694static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003695surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003696{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003697 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003698 float dx, dy;
3699 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003700
Pekka Paalanen460099f2012-01-20 16:48:25 +02003701 rotate = malloc(sizeof *rotate);
3702 if (!rotate)
3703 return;
3704
Jason Ekstranda7af7042013-10-12 22:38:11 -05003705 weston_view_to_global_float(surface->view,
3706 surface->view->geometry.width * 0.5f,
3707 surface->view->geometry.height * 0.5f,
3708 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003709
Daniel Stone37816df2012-05-16 18:45:18 +01003710 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3711 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003712 r = sqrtf(dx * dx + dy * dy);
3713 if (r > 20.0f) {
3714 struct weston_matrix inverse;
3715
3716 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003717 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003718 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003719
3720 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003721 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003722 } else {
3723 weston_matrix_init(&surface->rotation.rotation);
3724 weston_matrix_init(&rotate->rotation);
3725 }
3726
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003727 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3728 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003729}
3730
3731static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003732rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003733 void *data)
3734{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003735 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003736 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003737 struct shell_surface *surface;
3738
Pekka Paalanen01388e22013-04-25 13:57:44 +03003739 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003740 if (base_surface == NULL)
3741 return;
3742
3743 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003744 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3745 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003746 return;
3747
3748 surface_rotate(surface, seat);
3749}
3750
3751static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003752lower_fullscreen_layer(struct desktop_shell *shell)
3753{
3754 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003755 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003756
3757 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003758 wl_list_for_each_reverse_safe(view, prev,
3759 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003760 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003761 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003762}
3763
3764static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003765activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003766 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003767{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003768 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003769 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003770 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003771 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003772 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003773
Pekka Paalanen01388e22013-04-25 13:57:44 +03003774 main_surface = weston_surface_get_main_surface(es);
3775
Daniel Stone37816df2012-05-16 18:45:18 +01003776 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003777
Jonas Ådahl8538b222012-08-29 22:13:03 +02003778 state = ensure_focus_state(shell, seat);
3779 if (state == NULL)
3780 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003781
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003782 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003783 state->keyboard_focus = es;
3784 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003785 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003786
Pekka Paalanen01388e22013-04-25 13:57:44 +03003787 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003788 case SHELL_SURFACE_FULLSCREEN:
3789 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003790 shell_stack_fullscreen(get_shell_surface(main_surface));
3791 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003792 return;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003793 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003794 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003795 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003796 main_view = get_default_view(main_surface);
3797 if (main_view)
3798 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003799 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003800 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003801
3802 if (shell->focus_animation_type != ANIMATION_NONE)
3803 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Kristian Høgsberg75840622011-09-06 13:48:16 -04003804}
3805
Alex Wu21858432012-04-01 20:13:08 +08003806/* no-op func for checking black surface */
3807static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003808black_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 +08003809{
3810}
3811
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003812static bool
Alex Wu21858432012-04-01 20:13:08 +08003813is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3814{
3815 if (es->configure == black_surface_configure) {
3816 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003817 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003818 return true;
3819 }
3820 return false;
3821}
3822
Kristian Høgsberg75840622011-09-06 13:48:16 -04003823static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003824activate_binding(struct weston_seat *seat,
3825 struct desktop_shell *shell,
3826 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003827{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003828 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003829
Alex Wu9c35e6b2012-03-05 14:13:13 +08003830 if (!focus)
3831 return;
3832
Pekka Paalanen01388e22013-04-25 13:57:44 +03003833 if (is_black_surface(focus, &main_surface))
3834 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003835
Pekka Paalanen01388e22013-04-25 13:57:44 +03003836 main_surface = weston_surface_get_main_surface(focus);
3837 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003838 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003839
Neil Robertsa28c6932013-10-03 16:43:04 +01003840 activate(shell, focus, seat);
3841}
3842
3843static void
3844click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3845 void *data)
3846{
3847 if (seat->pointer->grab != &seat->pointer->default_grab)
3848 return;
3849
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003850 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003851}
3852
3853static void
3854touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3855{
3856 if (seat->touch->grab != &seat->touch->default_grab)
3857 return;
3858
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003859 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003860}
3861
3862static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003863lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003864{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003865 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003866
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003867 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003868 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003869 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003870 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003871
3872 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003873
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003874 /* Hide all surfaces by removing the fullscreen, panel and
3875 * toplevel layers. This way nothing else can show or receive
3876 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003877
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003878 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003879 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003880 if (shell->showing_input_panels)
3881 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003882 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003883 wl_list_insert(&shell->compositor->cursor_layer.link,
3884 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003885
Pekka Paalanen77346a62011-11-30 16:26:35 +02003886 launch_screensaver(shell);
3887
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003888 /* TODO: disable bindings that should not work while locked. */
3889
3890 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003891}
3892
3893static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003894unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003895{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003896 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003897 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003898 return;
3899 }
3900
3901 /* If desktop-shell client has gone away, unlock immediately. */
3902 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003903 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003904 return;
3905 }
3906
3907 if (shell->prepare_event_sent)
3908 return;
3909
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003910 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003911 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003912}
3913
3914static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003915shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003916{
3917 struct desktop_shell *shell = data;
3918
3919 shell->fade.animation = NULL;
3920
3921 switch (shell->fade.type) {
3922 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003923 weston_surface_destroy(shell->fade.view->surface);
3924 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003925 break;
3926 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003927 lock(shell);
3928 break;
3929 }
3930}
3931
Jason Ekstranda7af7042013-10-12 22:38:11 -05003932static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003933shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003934{
3935 struct weston_compositor *compositor = shell->compositor;
3936 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003937 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003938
3939 surface = weston_surface_create(compositor);
3940 if (!surface)
3941 return NULL;
3942
Jason Ekstranda7af7042013-10-12 22:38:11 -05003943 view = weston_view_create(surface);
3944 if (!view) {
3945 weston_surface_destroy(surface);
3946 return NULL;
3947 }
3948
3949 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003950 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003951 wl_list_insert(&compositor->fade_layer.view_list,
3952 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003953 pixman_region32_init(&surface->input);
3954
Jason Ekstranda7af7042013-10-12 22:38:11 -05003955 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003956}
3957
3958static void
3959shell_fade(struct desktop_shell *shell, enum fade_type type)
3960{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003961 float tint;
3962
3963 switch (type) {
3964 case FADE_IN:
3965 tint = 0.0;
3966 break;
3967 case FADE_OUT:
3968 tint = 1.0;
3969 break;
3970 default:
3971 weston_log("shell: invalid fade type\n");
3972 return;
3973 }
3974
3975 shell->fade.type = type;
3976
Jason Ekstranda7af7042013-10-12 22:38:11 -05003977 if (shell->fade.view == NULL) {
3978 shell->fade.view = shell_fade_create_surface(shell);
3979 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003980 return;
3981
Jason Ekstranda7af7042013-10-12 22:38:11 -05003982 shell->fade.view->alpha = 1.0 - tint;
3983 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003984 }
3985
3986 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003987 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003988 else
3989 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003990 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003991 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003992 shell_fade_done, shell);
3993}
3994
3995static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003996do_shell_fade_startup(void *data)
3997{
3998 struct desktop_shell *shell = data;
3999
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004000 if (shell->startup_animation_type == ANIMATION_FADE)
4001 shell_fade(shell, FADE_IN);
4002 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004003 weston_surface_destroy(shell->fade.view->surface);
4004 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004005 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004006}
4007
4008static void
4009shell_fade_startup(struct desktop_shell *shell)
4010{
4011 struct wl_event_loop *loop;
4012
4013 if (!shell->fade.startup_timer)
4014 return;
4015
4016 wl_event_source_remove(shell->fade.startup_timer);
4017 shell->fade.startup_timer = NULL;
4018
4019 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4020 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4021}
4022
4023static int
4024fade_startup_timeout(void *data)
4025{
4026 struct desktop_shell *shell = data;
4027
4028 shell_fade_startup(shell);
4029 return 0;
4030}
4031
4032static void
4033shell_fade_init(struct desktop_shell *shell)
4034{
4035 /* Make compositor output all black, and wait for the desktop-shell
4036 * client to signal it is ready, then fade in. The timer triggers a
4037 * fade-in, in case the desktop-shell client takes too long.
4038 */
4039
4040 struct wl_event_loop *loop;
4041
Jason Ekstranda7af7042013-10-12 22:38:11 -05004042 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004043 weston_log("%s: warning: fade surface already exists\n",
4044 __func__);
4045 return;
4046 }
4047
Jason Ekstranda7af7042013-10-12 22:38:11 -05004048 shell->fade.view = shell_fade_create_surface(shell);
4049 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004050 return;
4051
Jason Ekstranda7af7042013-10-12 22:38:11 -05004052 weston_view_update_transform(shell->fade.view);
4053 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004054
4055 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4056 shell->fade.startup_timer =
4057 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4058 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4059}
4060
4061static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004062idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004063{
4064 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004065 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004066
4067 shell_fade(shell, FADE_OUT);
4068 /* lock() is called from shell_fade_done() */
4069}
4070
4071static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004072wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004073{
4074 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004075 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004076
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004077 unlock(shell);
4078}
4079
4080static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004081show_input_panels(struct wl_listener *listener, void *data)
4082{
4083 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004084 container_of(listener, struct desktop_shell,
4085 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004086 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004087
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004088 shell->text_input.surface = (struct weston_surface*)data;
4089
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004090 if (shell->showing_input_panels)
4091 return;
4092
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004093 shell->showing_input_panels = true;
4094
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004095 if (!shell->locked)
4096 wl_list_insert(&shell->panel_layer.link,
4097 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004098
Jason Ekstranda7af7042013-10-12 22:38:11 -05004099 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004100 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004101 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004102 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004103 wl_list_insert(&shell->input_panel_layer.view_list,
4104 &ipsurf->view->layer_link);
4105 weston_view_geometry_dirty(ipsurf->view);
4106 weston_view_update_transform(ipsurf->view);
4107 weston_surface_damage(ipsurf->surface);
4108 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
4109 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004110 }
4111}
4112
4113static void
4114hide_input_panels(struct wl_listener *listener, void *data)
4115{
4116 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004117 container_of(listener, struct desktop_shell,
4118 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004119 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004120
Jan Arne Petersen61381972013-01-31 15:52:21 +01004121 if (!shell->showing_input_panels)
4122 return;
4123
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004124 shell->showing_input_panels = false;
4125
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004126 if (!shell->locked)
4127 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004128
Jason Ekstranda7af7042013-10-12 22:38:11 -05004129 wl_list_for_each_safe(view, next,
4130 &shell->input_panel_layer.view_list, layer_link)
4131 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004132}
4133
4134static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004135update_input_panels(struct wl_listener *listener, void *data)
4136{
4137 struct desktop_shell *shell =
4138 container_of(listener, struct desktop_shell,
4139 update_input_panel_listener);
4140
4141 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4142}
4143
4144static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004145center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004146{
Giulio Camuffob8366642013-04-25 13:57:46 +03004147 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004148 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004149
Jason Ekstranda7af7042013-10-12 22:38:11 -05004150 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004151
4152 x = output->x + (output->width - width) / 2 - surf_x / 2;
4153 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004154
Jason Ekstranda7af7042013-10-12 22:38:11 -05004155 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004156}
4157
4158static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004159weston_view_set_initial_position(struct weston_view *view,
4160 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004161{
4162 struct weston_compositor *compositor = shell->compositor;
4163 int ix = 0, iy = 0;
4164 int range_x, range_y;
4165 int dx, dy, x, y, panel_height;
4166 struct weston_output *output, *target_output = NULL;
4167 struct weston_seat *seat;
4168
4169 /* As a heuristic place the new window on the same output as the
4170 * pointer. Falling back to the output containing 0, 0.
4171 *
4172 * TODO: Do something clever for touch too?
4173 */
4174 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004175 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004176 ix = wl_fixed_to_int(seat->pointer->x);
4177 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004178 break;
4179 }
4180 }
4181
4182 wl_list_for_each(output, &compositor->output_list, link) {
4183 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4184 target_output = output;
4185 break;
4186 }
4187 }
4188
4189 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004190 weston_view_set_position(view, 10 + random() % 400,
4191 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004192 return;
4193 }
4194
4195 /* Valid range within output where the surface will still be onscreen.
4196 * If this is negative it means that the surface is bigger than
4197 * output.
4198 */
4199 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004200 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004201 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05004202 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004203
Rob Bradford4cb88c72012-08-13 15:18:44 +01004204 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004205 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004206 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004207 dx = 0;
4208
4209 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004210 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004211 else
4212 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004213
4214 x = target_output->x + dx;
4215 y = target_output->y + dy;
4216
Jason Ekstranda7af7042013-10-12 22:38:11 -05004217 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004218}
4219
4220static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004221map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004222 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004223{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004224 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004225 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01004226 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004227 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08004228 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004229 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004230
Jason Ekstranda7af7042013-10-12 22:38:11 -05004231 shsurf->view->geometry.width = width;
4232 shsurf->view->geometry.height = height;
4233 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004234
4235 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004236 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02004237 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004238 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004239 break;
Alex Wu4539b082012-03-01 12:57:46 +08004240 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004241 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08004242 shell_map_fullscreen(shsurf);
4243 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004244 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004245 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004246 panel_height = get_output_panel_height(shell, shsurf->output);
4247 surface_subsurfaces_boundingbox(shsurf->surface,
4248 &surf_x, &surf_y, NULL, NULL);
4249 weston_view_set_position(shsurf->view,
4250 shsurf->output->x - surf_x,
4251 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08004252 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004253 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004254 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004255 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004256 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004257 weston_view_set_position(shsurf->view,
4258 shsurf->view->geometry.x + sx,
4259 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004260 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004261 default:
4262 ;
4263 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004264
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02004265 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004266 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004267 case SHELL_SURFACE_POPUP:
4268 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004269 /* TODO: Handle a parent with multiple views */
4270 parent = get_default_view(shsurf->parent);
4271 if (parent) {
4272 wl_list_remove(&shsurf->view->layer_link);
4273 wl_list_insert(parent->layer_link.prev,
4274 &shsurf->view->layer_link);
4275 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004276 break;
Alex Wu4539b082012-03-01 12:57:46 +08004277 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02004278 case SHELL_SURFACE_NONE:
4279 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004280 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004281 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004282 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004283 wl_list_remove(&shsurf->view->layer_link);
4284 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004285 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004286 }
4287
Jason Ekstranda7af7042013-10-12 22:38:11 -05004288 if (shsurf->type != SHELL_SURFACE_NONE) {
4289 weston_view_update_transform(shsurf->view);
4290 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4291 shsurf->surface->output = shsurf->output;
4292 shsurf->view->output = shsurf->output;
4293 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004294 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004295
Jason Ekstranda7af7042013-10-12 22:38:11 -05004296 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004297 /* XXX: xwayland's using the same fields for transient type */
4298 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004299 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004300 if (shsurf->transient.flags ==
4301 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4302 break;
4303 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004304 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004305 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004306 if (!shell->locked) {
4307 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004308 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004309 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004310 break;
4311 default:
4312 break;
4313 }
4314
Jason Ekstranda7af7042013-10-12 22:38:11 -05004315 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004316 {
4317 switch (shell->win_animation_type) {
4318 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004319 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004320 break;
4321 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004322 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004323 break;
4324 default:
4325 break;
4326 }
4327 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004328}
4329
4330static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004331configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004332 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004333{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004334 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4335 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004336 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03004337 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004338
Pekka Paalanen77346a62011-11-30 16:26:35 +02004339 shsurf = get_shell_surface(surface);
4340 if (shsurf)
4341 surface_type = shsurf->type;
4342
Jason Ekstranda7af7042013-10-12 22:38:11 -05004343 /* TODO:
4344 * This should probably be changed to be more shell_surface
4345 * dependent
4346 */
4347 wl_list_for_each(view, &surface->views, surface_link)
4348 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004349
4350 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004351 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08004352 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004353 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004354 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004355 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004356 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004357 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4358 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004359 shsurf->view->geometry.x = shsurf->output->x - surf_x;
4360 shsurf->view->geometry.y = shsurf->output->y +
4361 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08004362 break;
Alex Wu4539b082012-03-01 12:57:46 +08004363 case SHELL_SURFACE_TOPLEVEL:
4364 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004365 default:
4366 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004367 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004368
Alex Wu4539b082012-03-01 12:57:46 +08004369 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004370 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004371 wl_list_for_each(view, &surface->views, surface_link)
4372 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004373
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004374 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004375 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004376 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004377}
4378
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004379static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004380shell_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 +03004381{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004382 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004383 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004384
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004385 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004386
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004387 if (!weston_surface_is_mapped(es) &&
4388 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004389 remove_popup_grab(shsurf);
4390 }
4391
Giulio Camuffo184df502013-02-21 11:29:21 +01004392 if (width == 0)
4393 return;
4394
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004395 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004396 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004397 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004398 type_changed = 1;
4399 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004400
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004401 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004402 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004403 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05004404 shsurf->view->geometry.width != width ||
4405 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004406 float from_x, from_y;
4407 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004408
Jason Ekstranda7af7042013-10-12 22:38:11 -05004409 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4410 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004411 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004412 shsurf->view->geometry.x + to_x - from_x,
4413 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004414 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004415 }
4416}
4417
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004418static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004419
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004420static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004421desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004422{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004423 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004424 struct desktop_shell *shell =
4425 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004426
4427 shell->child.process.pid = 0;
4428 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004429
4430 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4431 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004432 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004433 shell->child.deathstamp = time;
4434 shell->child.deathcount = 0;
4435 }
4436
4437 shell->child.deathcount++;
4438 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02004439 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004440 return;
4441 }
4442
Martin Minarik6d118362012-06-07 18:01:59 +02004443 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004444 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004445 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004446}
4447
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004448static void
4449launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004450{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004451 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05004452 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004453
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004454 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004455 &shell->child.process,
4456 shell_exe,
4457 desktop_shell_sigchld);
4458
4459 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004460 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004461}
4462
4463static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004464bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4465{
Tiago Vignattibe143262012-04-16 17:31:41 +03004466 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004467 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004468
Jason Ekstranda85118c2013-06-27 20:17:02 -05004469 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4470 if (resource)
4471 wl_resource_set_implementation(resource, &shell_implementation,
4472 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004473}
4474
Kristian Høgsberg75840622011-09-06 13:48:16 -04004475static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004476unbind_desktop_shell(struct wl_resource *resource)
4477{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004478 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004479
4480 if (shell->locked)
4481 resume_desktop(shell);
4482
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004483 shell->child.desktop_shell = NULL;
4484 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004485}
4486
4487static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004488bind_desktop_shell(struct wl_client *client,
4489 void *data, uint32_t version, uint32_t id)
4490{
Tiago Vignattibe143262012-04-16 17:31:41 +03004491 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004492 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004493
Jason Ekstranda85118c2013-06-27 20:17:02 -05004494 resource = wl_resource_create(client, &desktop_shell_interface,
4495 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004496
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004497 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004498 wl_resource_set_implementation(resource,
4499 &desktop_shell_implementation,
4500 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004501 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004502
4503 if (version < 2)
4504 shell_fade_startup(shell);
4505
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004506 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004507 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004508
4509 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4510 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004511 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004512}
4513
Pekka Paalanen6e168112011-11-24 11:34:05 +02004514static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004515screensaver_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 -04004516{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004517 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004518 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004519
Giulio Camuffo184df502013-02-21 11:29:21 +01004520 if (width == 0)
4521 return;
4522
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004523 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004524 if (!shell->locked)
4525 return;
4526
Jason Ekstranda7af7042013-10-12 22:38:11 -05004527 view = container_of(surface->views.next, struct weston_view, surface_link);
4528 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004529
Jason Ekstranda7af7042013-10-12 22:38:11 -05004530 if (wl_list_empty(&view->layer_link)) {
4531 wl_list_insert(shell->lock_layer.view_list.prev,
4532 &view->layer_link);
4533 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004534 wl_event_source_timer_update(shell->screensaver.timer,
4535 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004536 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004537 }
4538}
4539
4540static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004541screensaver_set_surface(struct wl_client *client,
4542 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004543 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004544 struct wl_resource *output_resource)
4545{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004546 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004547 struct weston_surface *surface =
4548 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004549 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004550 struct weston_view *view, *next;
4551
4552 /* Make sure we only have one view */
4553 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4554 weston_view_destroy(view);
4555 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004556
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004557 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004558 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004559 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004560}
4561
4562static const struct screensaver_interface screensaver_implementation = {
4563 screensaver_set_surface
4564};
4565
4566static void
4567unbind_screensaver(struct wl_resource *resource)
4568{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004569 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004570
Pekka Paalanen77346a62011-11-30 16:26:35 +02004571 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004572}
4573
4574static void
4575bind_screensaver(struct wl_client *client,
4576 void *data, uint32_t version, uint32_t id)
4577{
Tiago Vignattibe143262012-04-16 17:31:41 +03004578 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004579 struct wl_resource *resource;
4580
Jason Ekstranda85118c2013-06-27 20:17:02 -05004581 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004582
Pekka Paalanen77346a62011-11-30 16:26:35 +02004583 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004584 wl_resource_set_implementation(resource,
4585 &screensaver_implementation,
4586 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004587 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004588 return;
4589 }
4590
4591 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4592 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004593 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004594}
4595
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004596static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004597input_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 -04004598{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004599 struct input_panel_surface *ip_surface = surface->configure_private;
4600 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004601 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004602 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004603
Giulio Camuffo184df502013-02-21 11:29:21 +01004604 if (width == 0)
4605 return;
4606
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004607 if (!weston_surface_is_mapped(surface)) {
4608 if (!shell->showing_input_panels)
4609 return;
4610
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004611 show_surface = 1;
4612 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004613
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004614 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004615
4616 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004617 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4618 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004619 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004620 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4621 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004622 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004623
Jason Ekstranda7af7042013-10-12 22:38:11 -05004624 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004625
4626 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004627 wl_list_insert(&shell->input_panel_layer.view_list,
4628 &ip_surface->view->layer_link);
4629 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004630 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004631 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004632 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004633}
4634
4635static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004636destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004637{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004638 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4639
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004640 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004641 wl_list_remove(&input_panel_surface->link);
4642
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004643 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004644 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004645
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004646 free(input_panel_surface);
4647}
4648
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004649static struct input_panel_surface *
4650get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004651{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004652 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004653 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004654 } else {
4655 return NULL;
4656 }
4657}
4658
4659static void
4660input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4661{
4662 struct input_panel_surface *ipsurface = container_of(listener,
4663 struct input_panel_surface,
4664 surface_destroy_listener);
4665
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004666 if (ipsurface->resource) {
4667 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004668 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004669 destroy_input_panel_surface(ipsurface);
4670 }
4671}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004672
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004673static struct input_panel_surface *
4674create_input_panel_surface(struct desktop_shell *shell,
4675 struct weston_surface *surface)
4676{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004677 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004678
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004679 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4680 if (!input_panel_surface)
4681 return NULL;
4682
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004683 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004684 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004685
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004686 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004687
4688 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004689 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004690
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004691 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004692 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004693 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004694 &input_panel_surface->surface_destroy_listener);
4695
4696 wl_list_init(&input_panel_surface->link);
4697
4698 return input_panel_surface;
4699}
4700
4701static void
4702input_panel_surface_set_toplevel(struct wl_client *client,
4703 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004704 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004705 uint32_t position)
4706{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004707 struct input_panel_surface *input_panel_surface =
4708 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004709 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004710
4711 wl_list_insert(&shell->input_panel.surfaces,
4712 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004713
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004714 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004715 input_panel_surface->panel = 0;
4716}
4717
4718static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004719input_panel_surface_set_overlay_panel(struct wl_client *client,
4720 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004721{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004722 struct input_panel_surface *input_panel_surface =
4723 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004724 struct desktop_shell *shell = input_panel_surface->shell;
4725
4726 wl_list_insert(&shell->input_panel.surfaces,
4727 &input_panel_surface->link);
4728
4729 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004730}
4731
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004732static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004733 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004734 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004735};
4736
4737static void
4738destroy_input_panel_surface_resource(struct wl_resource *resource)
4739{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004740 struct input_panel_surface *ipsurf =
4741 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004742
4743 destroy_input_panel_surface(ipsurf);
4744}
4745
4746static void
4747input_panel_get_input_panel_surface(struct wl_client *client,
4748 struct wl_resource *resource,
4749 uint32_t id,
4750 struct wl_resource *surface_resource)
4751{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004752 struct weston_surface *surface =
4753 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004754 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004755 struct input_panel_surface *ipsurf;
4756
4757 if (get_input_panel_surface(surface)) {
4758 wl_resource_post_error(surface_resource,
4759 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004760 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004761 return;
4762 }
4763
4764 ipsurf = create_input_panel_surface(shell, surface);
4765 if (!ipsurf) {
4766 wl_resource_post_error(surface_resource,
4767 WL_DISPLAY_ERROR_INVALID_OBJECT,
4768 "surface->configure already set");
4769 return;
4770 }
4771
Jason Ekstranda85118c2013-06-27 20:17:02 -05004772 ipsurf->resource =
4773 wl_resource_create(client,
4774 &wl_input_panel_surface_interface, 1, id);
4775 wl_resource_set_implementation(ipsurf->resource,
4776 &input_panel_surface_implementation,
4777 ipsurf,
4778 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004779}
4780
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004781static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004782 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004783};
4784
4785static void
4786unbind_input_panel(struct wl_resource *resource)
4787{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004788 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004789
4790 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004791}
4792
4793static void
4794bind_input_panel(struct wl_client *client,
4795 void *data, uint32_t version, uint32_t id)
4796{
4797 struct desktop_shell *shell = data;
4798 struct wl_resource *resource;
4799
Jason Ekstranda85118c2013-06-27 20:17:02 -05004800 resource = wl_resource_create(client,
4801 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004802
4803 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004804 wl_resource_set_implementation(resource,
4805 &input_panel_implementation,
4806 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004807 shell->input_panel.binding = resource;
4808 return;
4809 }
4810
4811 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4812 "interface object already bound");
4813 wl_resource_destroy(resource);
4814}
4815
Kristian Høgsberg07045392012-02-19 18:52:44 -05004816struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004817 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004818 struct weston_surface *current;
4819 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004820 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004821};
4822
4823static void
4824switcher_next(struct switcher *switcher)
4825{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004826 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004827 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004828 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004829 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004830
Jason Ekstranda7af7042013-10-12 22:38:11 -05004831 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4832 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004833 case SHELL_SURFACE_TOPLEVEL:
4834 case SHELL_SURFACE_FULLSCREEN:
4835 case SHELL_SURFACE_MAXIMIZED:
4836 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004837 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004838 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004839 next = view->surface;
4840 prev = view->surface;
4841 view->alpha = 0.25;
4842 weston_view_geometry_dirty(view);
4843 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004844 break;
4845 default:
4846 break;
4847 }
Alex Wu1659daa2012-04-01 20:13:09 +08004848
Jason Ekstranda7af7042013-10-12 22:38:11 -05004849 if (is_black_surface(view->surface, NULL)) {
4850 view->alpha = 0.25;
4851 weston_view_geometry_dirty(view);
4852 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004853 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004854 }
4855
4856 if (next == NULL)
4857 next = first;
4858
Alex Wu07b26062012-03-12 16:06:01 +08004859 if (next == NULL)
4860 return;
4861
Kristian Høgsberg07045392012-02-19 18:52:44 -05004862 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004863 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004864
4865 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004866 wl_list_for_each(view, &next->views, surface_link)
4867 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004868
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004869 shsurf = get_shell_surface(switcher->current);
4870 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004871 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004872}
4873
4874static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004875switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004876{
4877 struct switcher *switcher =
4878 container_of(listener, struct switcher, listener);
4879
4880 switcher_next(switcher);
4881}
4882
4883static void
Daniel Stone351eb612012-05-31 15:27:47 -04004884switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004885{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004886 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004887 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004888 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004889
Jason Ekstranda7af7042013-10-12 22:38:11 -05004890 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004891 if (is_focus_view(view))
4892 continue;
4893
Jason Ekstranda7af7042013-10-12 22:38:11 -05004894 view->alpha = 1.0;
4895 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004896 }
4897
Alex Wu07b26062012-03-12 16:06:01 +08004898 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004899 activate(switcher->shell, switcher->current,
4900 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004901 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004902 weston_keyboard_end_grab(keyboard);
4903 if (keyboard->input_method_resource)
4904 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004905 free(switcher);
4906}
4907
4908static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004909switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004910 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004911{
4912 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004913 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004914
Daniel Stonec9785ea2012-05-30 16:31:52 +01004915 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004916 switcher_next(switcher);
4917}
4918
4919static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004920switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004921 uint32_t mods_depressed, uint32_t mods_latched,
4922 uint32_t mods_locked, uint32_t group)
4923{
4924 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004925 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004926
Daniel Stone351eb612012-05-31 15:27:47 -04004927 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4928 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004929}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004930
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004931static void
4932switcher_cancel(struct weston_keyboard_grab *grab)
4933{
4934 struct switcher *switcher = container_of(grab, struct switcher, grab);
4935
4936 switcher_destroy(switcher);
4937}
4938
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004939static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004940 switcher_key,
4941 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004942 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004943};
4944
4945static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004946switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004947 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004948{
Tiago Vignattibe143262012-04-16 17:31:41 +03004949 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004950 struct switcher *switcher;
4951
4952 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004953 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004954 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004955 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004956 wl_list_init(&switcher->listener.link);
4957
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004958 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004959 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004960 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004961 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4962 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004963 switcher_next(switcher);
4964}
4965
Daniel Stonedf8133b2013-11-19 11:37:14 +01004966struct exposay_surface {
4967 struct desktop_shell *shell;
4968 struct weston_surface *surface;
4969 struct weston_view *view;
4970 struct wl_list link;
4971
4972 int x;
4973 int y;
4974 int width;
4975 int height;
4976 double scale;
4977
4978 int row;
4979 int column;
4980
4981 /* The animations only apply a transformation for their own lifetime,
4982 * and don't have an option to indefinitely maintain the
4983 * transformation in a steady state - so, we apply our own once the
4984 * animation has finished. */
4985 struct weston_transform transform;
4986};
4987
4988static void exposay_set_state(struct desktop_shell *shell,
4989 enum exposay_target_state state,
4990 struct weston_seat *seat);
4991static void exposay_check_state(struct desktop_shell *shell);
4992
4993static void
4994exposay_in_flight_inc(struct desktop_shell *shell)
4995{
4996 shell->exposay.in_flight++;
4997}
4998
4999static void
5000exposay_in_flight_dec(struct desktop_shell *shell)
5001{
5002 if (--shell->exposay.in_flight > 0)
5003 return;
5004
5005 exposay_check_state(shell);
5006}
5007
5008static void
5009exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5010{
5011 struct exposay_surface *esurface = data;
5012
5013 wl_list_insert(&esurface->view->geometry.transformation_list,
5014 &esurface->transform.link);
5015 weston_matrix_init(&esurface->transform.matrix);
5016 weston_matrix_scale(&esurface->transform.matrix,
5017 esurface->scale, esurface->scale, 1.0f);
5018 weston_matrix_translate(&esurface->transform.matrix,
5019 esurface->x - esurface->view->geometry.x,
5020 esurface->y - esurface->view->geometry.y,
5021 0);
5022
5023 weston_view_geometry_dirty(esurface->view);
5024 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5025
5026 exposay_in_flight_dec(esurface->shell);
5027}
5028
5029static void
5030exposay_animate_in(struct exposay_surface *esurface)
5031{
5032 exposay_in_flight_inc(esurface->shell);
5033
5034 weston_move_scale_run(esurface->view,
5035 esurface->x - esurface->view->geometry.x,
5036 esurface->y - esurface->view->geometry.y,
5037 1.0, esurface->scale, 0,
5038 exposay_animate_in_done, esurface);
5039}
5040
5041static void
5042exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5043{
5044 struct exposay_surface *esurface = data;
5045 struct desktop_shell *shell = esurface->shell;
5046
5047 wl_list_remove(&esurface->link);
5048 free(esurface);
5049
5050 exposay_in_flight_dec(shell);
5051}
5052
5053static void
5054exposay_animate_out(struct exposay_surface *esurface)
5055{
5056 exposay_in_flight_inc(esurface->shell);
5057
5058 /* Remove the static transformation set up by
5059 * exposay_transform_in_done(). */
5060 wl_list_remove(&esurface->transform.link);
5061 weston_view_geometry_dirty(esurface->view);
5062
5063 weston_move_scale_run(esurface->view,
5064 esurface->x - esurface->view->geometry.x,
5065 esurface->y - esurface->view->geometry.y,
5066 1.0, esurface->scale, 1,
5067 exposay_animate_out_done, esurface);
5068}
5069
5070static void
5071exposay_highlight_surface(struct desktop_shell *shell,
5072 struct exposay_surface *esurface)
5073{
5074 struct weston_view *view = NULL;
5075
5076 if (esurface) {
5077 shell->exposay.row_current = esurface->row;
5078 shell->exposay.column_current = esurface->column;
5079 view = esurface->view;
5080 }
5081
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005082 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005083 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
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005354static void
5355exposay_cancel(struct weston_keyboard_grab *grab)
5356{
5357 struct desktop_shell *shell =
5358 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5359
5360 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5361}
5362
Daniel Stonedf8133b2013-11-19 11:37:14 +01005363static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5364 exposay_key,
5365 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005366 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005367};
5368
5369/**
5370 * Called when the transition from overview -> inactive has completed.
5371 */
5372static enum exposay_layout_state
5373exposay_set_inactive(struct desktop_shell *shell)
5374{
5375 struct weston_seat *seat = shell->exposay.seat;
5376
5377 weston_keyboard_end_grab(seat->keyboard);
5378 weston_pointer_end_grab(seat->pointer);
5379 if (seat->keyboard->input_method_resource)
5380 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5381
5382 return EXPOSAY_LAYOUT_INACTIVE;
5383}
5384
5385/**
5386 * Begins the transition from overview to inactive. */
5387static enum exposay_layout_state
5388exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5389{
5390 struct exposay_surface *esurface;
5391
5392 /* Call activate() before we start the animations to avoid
5393 * animating back the old state and then immediately transitioning
5394 * to the new. */
5395 if (switch_focus && shell->exposay.focus_current)
5396 activate(shell, shell->exposay.focus_current->surface,
5397 shell->exposay.seat);
5398 else if (shell->exposay.focus_prev)
5399 activate(shell, shell->exposay.focus_prev->surface,
5400 shell->exposay.seat);
5401
5402 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5403 exposay_animate_out(esurface);
5404 weston_compositor_schedule_repaint(shell->compositor);
5405
5406 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5407}
5408
5409static enum exposay_layout_state
5410exposay_transition_active(struct desktop_shell *shell)
5411{
5412 struct weston_seat *seat = shell->exposay.seat;
5413
5414 shell->exposay.workspace = get_current_workspace(shell);
5415 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5416 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5417 shell->exposay.clicked = NULL;
5418 wl_list_init(&shell->exposay.surface_list);
5419
5420 lower_fullscreen_layer(shell);
5421 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5422 weston_keyboard_start_grab(seat->keyboard,
5423 &shell->exposay.grab_kbd);
5424 weston_keyboard_set_focus(seat->keyboard, NULL);
5425
5426 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5427 weston_pointer_start_grab(seat->pointer,
5428 &shell->exposay.grab_ptr);
5429 weston_pointer_set_focus(seat->pointer, NULL,
5430 seat->pointer->x, seat->pointer->y);
5431
5432 return exposay_layout(shell);
5433}
5434
5435static void
5436exposay_check_state(struct desktop_shell *shell)
5437{
5438 enum exposay_layout_state state_new = shell->exposay.state_cur;
5439 int do_switch = 0;
5440
5441 /* Don't do anything whilst animations are running, just store up
5442 * target state changes and only act on them when the animations have
5443 * completed. */
5444 if (exposay_is_animating(shell))
5445 return;
5446
5447 switch (shell->exposay.state_target) {
5448 case EXPOSAY_TARGET_OVERVIEW:
5449 switch (shell->exposay.state_cur) {
5450 case EXPOSAY_LAYOUT_OVERVIEW:
5451 goto out;
5452 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5453 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5454 break;
5455 default:
5456 state_new = exposay_transition_active(shell);
5457 break;
5458 }
5459 break;
5460
5461 case EXPOSAY_TARGET_SWITCH:
5462 do_switch = 1; /* fallthrough */
5463 case EXPOSAY_TARGET_CANCEL:
5464 switch (shell->exposay.state_cur) {
5465 case EXPOSAY_LAYOUT_INACTIVE:
5466 goto out;
5467 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5468 state_new = exposay_set_inactive(shell);
5469 break;
5470 default:
5471 state_new = exposay_transition_inactive(shell, do_switch);
5472 break;
5473 }
5474
5475 break;
5476 }
5477
5478out:
5479 shell->exposay.state_cur = state_new;
5480}
5481
5482static void
5483exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5484 struct weston_seat *seat)
5485{
5486 shell->exposay.state_target = state;
5487 shell->exposay.seat = seat;
5488 exposay_check_state(shell);
5489}
5490
5491static void
5492exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5493 void *data)
5494{
5495 struct desktop_shell *shell = data;
5496
5497 if (shell->exposay.state_target == EXPOSAY_TARGET_OVERVIEW)
5498 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5499 else
5500 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
5501}
5502
Pekka Paalanen3c647232011-12-22 13:43:43 +02005503static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005504backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005505 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005506{
5507 struct weston_compositor *compositor = data;
5508 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005509 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005510
5511 /* TODO: we're limiting to simple use cases, where we assume just
5512 * control on the primary display. We'd have to extend later if we
5513 * ever get support for setting backlights on random desktop LCD
5514 * panels though */
5515 output = get_default_output(compositor);
5516 if (!output)
5517 return;
5518
5519 if (!output->set_backlight)
5520 return;
5521
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005522 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5523 backlight_new = output->backlight_current - 25;
5524 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5525 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005526
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005527 if (backlight_new < 5)
5528 backlight_new = 5;
5529 if (backlight_new > 255)
5530 backlight_new = 255;
5531
5532 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005533 output->set_backlight(output, output->backlight_current);
5534}
5535
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005536struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005537 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005538 struct weston_seat *seat;
5539 uint32_t key[2];
5540 int key_released[2];
5541};
5542
5543static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005544debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005545 uint32_t key, uint32_t state)
5546{
5547 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005548 struct weston_compositor *ec = db->seat->compositor;
5549 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005550 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005551 uint32_t serial;
5552 int send = 0, terminate = 0;
5553 int check_binding = 1;
5554 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005555 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005556
5557 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5558 /* Do not run bindings on key releases */
5559 check_binding = 0;
5560
5561 for (i = 0; i < 2; i++)
5562 if (key == db->key[i])
5563 db->key_released[i] = 1;
5564
5565 if (db->key_released[0] && db->key_released[1]) {
5566 /* All key releases been swalled so end the grab */
5567 terminate = 1;
5568 } else if (key != db->key[0] && key != db->key[1]) {
5569 /* Should not swallow release of other keys */
5570 send = 1;
5571 }
5572 } else if (key == db->key[0] && !db->key_released[0]) {
5573 /* Do not check bindings for the first press of the binding
5574 * key. This allows it to be used as a debug shortcut.
5575 * We still need to swallow this event. */
5576 check_binding = 0;
5577 } else if (db->key[1]) {
5578 /* If we already ran a binding don't process another one since
5579 * we can't keep track of all the binding keys that were
5580 * pressed in order to swallow the release events. */
5581 send = 1;
5582 check_binding = 0;
5583 }
5584
5585 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005586 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5587 key, state)) {
5588 /* We ran a binding so swallow the press and keep the
5589 * grab to swallow the released too. */
5590 send = 0;
5591 terminate = 0;
5592 db->key[1] = key;
5593 } else {
5594 /* Terminate the grab since the key pressed is not a
5595 * debug binding key. */
5596 send = 1;
5597 terminate = 1;
5598 }
5599 }
5600
5601 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005602 serial = wl_display_next_serial(display);
5603 resource_list = &grab->keyboard->focus_resource_list;
5604 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005605 wl_keyboard_send_key(resource, serial, time, key, state);
5606 }
5607 }
5608
5609 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005610 weston_keyboard_end_grab(grab->keyboard);
5611 if (grab->keyboard->input_method_resource)
5612 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005613 free(db);
5614 }
5615}
5616
5617static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005618debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005619 uint32_t mods_depressed, uint32_t mods_latched,
5620 uint32_t mods_locked, uint32_t group)
5621{
5622 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005623 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005624
Neil Roberts96d790e2013-09-19 17:32:00 +01005625 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005626
Neil Roberts96d790e2013-09-19 17:32:00 +01005627 wl_resource_for_each(resource, resource_list) {
5628 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5629 mods_latched, mods_locked, group);
5630 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005631}
5632
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005633static void
5634debug_binding_cancel(struct weston_keyboard_grab *grab)
5635{
5636 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5637
5638 weston_keyboard_end_grab(grab->keyboard);
5639 free(db);
5640}
5641
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005642struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005643 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005644 debug_binding_modifiers,
5645 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005646};
5647
5648static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005649debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005650{
5651 struct debug_binding_grab *grab;
5652
5653 grab = calloc(1, sizeof *grab);
5654 if (!grab)
5655 return;
5656
5657 grab->seat = (struct weston_seat *) seat;
5658 grab->key[0] = key;
5659 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005660 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005661}
5662
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005663static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005664force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005665 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005666{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005667 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005668 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005669 struct desktop_shell *shell = data;
5670 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005671 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005672
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005673 focus_surface = seat->keyboard->focus;
5674 if (!focus_surface)
5675 return;
5676
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005677 wl_signal_emit(&compositor->kill_signal, focus_surface);
5678
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005679 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005680 wl_client_get_credentials(client, &pid, NULL, NULL);
5681
5682 /* Skip clients that we launched ourselves (the credentials of
5683 * the socketpair is ours) */
5684 if (pid == getpid())
5685 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005686
Daniel Stone325fc2d2012-05-30 16:31:58 +01005687 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005688}
5689
5690static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005691workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005692 uint32_t key, void *data)
5693{
5694 struct desktop_shell *shell = data;
5695 unsigned int new_index = shell->workspaces.current;
5696
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005697 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005698 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005699 if (new_index != 0)
5700 new_index--;
5701
5702 change_workspace(shell, new_index);
5703}
5704
5705static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005706workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005707 uint32_t key, void *data)
5708{
5709 struct desktop_shell *shell = data;
5710 unsigned int new_index = shell->workspaces.current;
5711
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005712 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005713 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005714 if (new_index < shell->workspaces.num - 1)
5715 new_index++;
5716
5717 change_workspace(shell, new_index);
5718}
5719
5720static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005721workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005722 uint32_t key, void *data)
5723{
5724 struct desktop_shell *shell = data;
5725 unsigned int new_index;
5726
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005727 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005728 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005729 new_index = key - KEY_F1;
5730 if (new_index >= shell->workspaces.num)
5731 new_index = shell->workspaces.num - 1;
5732
5733 change_workspace(shell, new_index);
5734}
5735
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005736static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005737workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005738 uint32_t key, void *data)
5739{
5740 struct desktop_shell *shell = data;
5741 unsigned int new_index = shell->workspaces.current;
5742
5743 if (shell->locked)
5744 return;
5745
5746 if (new_index != 0)
5747 new_index--;
5748
5749 take_surface_to_workspace_by_seat(shell, seat, new_index);
5750}
5751
5752static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005753workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005754 uint32_t key, void *data)
5755{
5756 struct desktop_shell *shell = data;
5757 unsigned int new_index = shell->workspaces.current;
5758
5759 if (shell->locked)
5760 return;
5761
5762 if (new_index < shell->workspaces.num - 1)
5763 new_index++;
5764
5765 take_surface_to_workspace_by_seat(shell, seat, new_index);
5766}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005767
5768static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005769handle_output_destroy(struct wl_listener *listener, void *data)
5770{
5771 struct shell_output *output_listener =
5772 container_of(listener, struct shell_output, destroy_listener);
5773
5774 wl_list_remove(&output_listener->destroy_listener.link);
5775 wl_list_remove(&output_listener->link);
5776 free(output_listener);
5777}
5778
5779static void
5780create_shell_output(struct desktop_shell *shell,
5781 struct weston_output *output)
5782{
5783 struct shell_output *shell_output;
5784
5785 shell_output = zalloc(sizeof *shell_output);
5786 if (shell_output == NULL)
5787 return;
5788
5789 shell_output->output = output;
5790 shell_output->shell = shell;
5791 shell_output->destroy_listener.notify = handle_output_destroy;
5792 wl_signal_add(&output->destroy_signal,
5793 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005794 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005795}
5796
5797static void
5798handle_output_create(struct wl_listener *listener, void *data)
5799{
5800 struct desktop_shell *shell =
5801 container_of(listener, struct desktop_shell, output_create_listener);
5802 struct weston_output *output = (struct weston_output *)data;
5803
5804 create_shell_output(shell, output);
5805}
5806
5807static void
5808setup_output_destroy_handler(struct weston_compositor *ec,
5809 struct desktop_shell *shell)
5810{
5811 struct weston_output *output;
5812
5813 wl_list_init(&shell->output_list);
5814 wl_list_for_each(output, &ec->output_list, link)
5815 create_shell_output(shell, output);
5816
5817 shell->output_create_listener.notify = handle_output_create;
5818 wl_signal_add(&ec->output_created_signal,
5819 &shell->output_create_listener);
5820}
5821
5822static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005823shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005824{
Tiago Vignattibe143262012-04-16 17:31:41 +03005825 struct desktop_shell *shell =
5826 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005827 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005828 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005829
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005830 if (shell->child.client)
5831 wl_client_destroy(shell->child.client);
5832
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005833 wl_list_remove(&shell->idle_listener.link);
5834 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005835 wl_list_remove(&shell->show_input_panel_listener.link);
5836 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005837
Xiong Zhang6b481422013-10-23 13:58:32 +08005838 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5839 wl_list_remove(&shell_output->destroy_listener.link);
5840 wl_list_remove(&shell_output->link);
5841 free(shell_output);
5842 }
5843
5844 wl_list_remove(&shell->output_create_listener.link);
5845
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005846 wl_array_for_each(ws, &shell->workspaces.array)
5847 workspace_destroy(*ws);
5848 wl_array_release(&shell->workspaces.array);
5849
Pekka Paalanen3c647232011-12-22 13:43:43 +02005850 free(shell->screensaver.path);
5851 free(shell);
5852}
5853
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005854static void
5855shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5856{
5857 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005858 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005859
5860 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005861 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5862 MODIFIER_CTRL | MODIFIER_ALT,
5863 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01005864 weston_compositor_add_key_binding(ec, KEY_TAB,
5865 MODIFIER_ALT,
5866 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005867 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5868 click_to_activate_binding,
5869 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005870 weston_compositor_add_touch_binding(ec, 0,
5871 touch_to_activate_binding,
5872 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005873 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5874 MODIFIER_SUPER | MODIFIER_ALT,
5875 surface_opacity_binding, NULL);
5876 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5877 MODIFIER_SUPER, zoom_axis_binding,
5878 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005879
5880 /* configurable bindings */
5881 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005882 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5883 zoom_key_binding, NULL);
5884 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5885 zoom_key_binding, NULL);
5886 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5887 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005888 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005889 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5890 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005891
5892 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5893 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5894 rotate_binding, NULL);
5895
Daniel Stone325fc2d2012-05-30 16:31:58 +01005896 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5897 shell);
5898 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5899 ec);
5900 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5901 backlight_binding, ec);
5902 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5903 ec);
5904 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5905 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005906 weston_compositor_add_key_binding(ec, KEY_K, mod,
5907 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005908 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5909 workspace_up_binding, shell);
5910 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5911 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005912 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5913 workspace_move_surface_up_binding,
5914 shell);
5915 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5916 workspace_move_surface_down_binding,
5917 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005918
Daniel Stonedf8133b2013-11-19 11:37:14 +01005919 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5920
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005921 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5922 if (shell->workspaces.num > 1) {
5923 num_workspace_bindings = shell->workspaces.num;
5924 if (num_workspace_bindings > 6)
5925 num_workspace_bindings = 6;
5926 for (i = 0; i < num_workspace_bindings; i++)
5927 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5928 workspace_f_binding,
5929 shell);
5930 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005931
5932 /* Debug bindings */
5933 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5934 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005935}
5936
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005937WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005938module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005939 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005940{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005941 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005942 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005943 struct workspace **pws;
5944 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005945 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005946
Peter Huttererf3d62272013-08-08 11:57:05 +10005947 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005948 if (shell == NULL)
5949 return -1;
5950
Kristian Høgsberg75840622011-09-06 13:48:16 -04005951 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005952
5953 shell->destroy_listener.notify = shell_destroy;
5954 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005955 shell->idle_listener.notify = idle_handler;
5956 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5957 shell->wake_listener.notify = wake_handler;
5958 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005959 shell->show_input_panel_listener.notify = show_input_panels;
5960 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
5961 shell->hide_input_panel_listener.notify = hide_input_panels;
5962 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005963 shell->update_input_panel_listener.notify = update_input_panels;
5964 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06005965 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005966 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005967 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005968 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005969 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005970 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005971 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005972 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005973 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005974 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005975 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005976
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005977 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005978
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005979 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5980 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005981 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5982 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005983 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005984
5985 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005986 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005987
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005988 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005989
Daniel Stonedf8133b2013-11-19 11:37:14 +01005990 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5991 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5992
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005993 for (i = 0; i < shell->workspaces.num; i++) {
5994 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5995 if (pws == NULL)
5996 return -1;
5997
5998 *pws = workspace_create();
5999 if (*pws == NULL)
6000 return -1;
6001 }
6002 activate_workspace(shell, 0);
6003
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006004 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006005 wl_list_init(&shell->workspaces.animation.link);
6006 shell->workspaces.animation.frame = animate_workspace_change_frame;
6007
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006008 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006009 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006010 return -1;
6011
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006012 if (wl_global_create(ec->wl_display,
6013 &desktop_shell_interface, 2,
6014 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006015 return -1;
6016
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006017 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6018 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006019 return -1;
6020
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006021 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006022 shell, bind_input_panel) == NULL)
6023 return -1;
6024
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006025 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6026 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006027 return -1;
6028
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006029 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006030
Xiong Zhang6b481422013-10-23 13:58:32 +08006031 setup_output_destroy_handler(ec, shell);
6032
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006033 loop = wl_display_get_event_loop(ec->wl_display);
6034 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006035
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006036 shell->screensaver.timer =
6037 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6038
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006039 wl_list_for_each(seat, &ec->seat_list, link)
6040 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006041
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006042 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006043
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006044 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006045
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006046 return 0;
6047}