blob: ebcf3e20bb5a21f374c060e38361ef2b6302b049 [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;
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +0100223
224 bool mod_pressed;
225 bool mod_invalid;
Daniel Stonedf8133b2013-11-19 11:37:14 +0100226 } exposay;
227
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300228 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800229 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700230 enum animation_type startup_animation_type;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100231 enum animation_type focus_animation_type;
Xiong Zhang6b481422013-10-23 13:58:32 +0800232
233 struct wl_listener output_create_listener;
234 struct wl_list output_list;
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100235
236 char *client;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400237};
238
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500239enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200240 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500241 SHELL_SURFACE_TOPLEVEL,
242 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500243 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800244 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300245 SHELL_SURFACE_POPUP,
246 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200247};
248
Scott Moreauff1db4a2012-04-17 19:06:18 -0600249struct ping_timer {
250 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600251 uint32_t serial;
252};
253
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200254struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500255 struct wl_resource *resource;
256 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200257
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500258 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500259 struct weston_view *view;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200260 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400261 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300262 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200263
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400264 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400265 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500266 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800267 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800268 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700269 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100270
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500271 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200272 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500273 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200274 } rotation;
275
276 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100277 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500278 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100279 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400280 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500281 } popup;
282
Alex Wu4539b082012-03-01 12:57:46 +0800283 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300284 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400285 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300286 } transient;
287
288 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800289 enum wl_shell_surface_fullscreen_method type;
290 struct weston_transform transform; /* matrix from x, y */
291 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800293 } fullscreen;
294
Scott Moreauff1db4a2012-04-17 19:06:18 -0600295 struct ping_timer *ping_timer;
296
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200297 struct weston_transform workspace_transform;
298
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500299 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500300 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100301 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400302
303 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200304};
305
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400307 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300308 struct shell_surface *shsurf;
309 struct wl_listener shsurf_destroy_listener;
310};
311
Rusty Lynch1084da52013-08-15 09:10:08 -0700312struct shell_touch_grab {
313 struct weston_touch_grab grab;
314 struct shell_surface *shsurf;
315 struct wl_listener shsurf_destroy_listener;
316 struct weston_touch *touch;
317};
318
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300319struct weston_move_grab {
320 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100321 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500322};
323
Rusty Lynch1084da52013-08-15 09:10:08 -0700324struct weston_touch_move_grab {
325 struct shell_touch_grab base;
326 wl_fixed_t dx, dy;
327};
328
Pekka Paalanen460099f2012-01-20 16:48:25 +0200329struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300330 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500331 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200332 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200333 float x;
334 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200335 } center;
336};
337
Giulio Camuffo5085a752013-03-25 21:42:45 +0100338struct shell_seat {
339 struct weston_seat *seat;
340 struct wl_listener seat_destroy_listener;
341
342 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400343 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100344 struct wl_list surfaces_list;
345 struct wl_client *client;
346 int32_t initial_up;
347 } popup_grab;
348};
349
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400350static void
351activate(struct desktop_shell *shell, struct weston_surface *es,
352 struct weston_seat *seat);
353
354static struct workspace *
355get_current_workspace(struct desktop_shell *shell);
356
Alex Wubd3354b2012-04-17 17:20:49 +0800357static struct shell_surface *
358get_shell_surface(struct weston_surface *surface);
359
360static struct desktop_shell *
361shell_surface_get_shell(struct shell_surface *shsurf);
362
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500363static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400364surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500365
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300366static void
367shell_fade_startup(struct desktop_shell *shell);
368
Alex Wubd3354b2012-04-17 17:20:49 +0800369static bool
370shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
371{
372 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500373 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800374
375 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100376
Jason Ekstranda7af7042013-10-12 22:38:11 -0500377 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800378 return false;
379
Jason Ekstranda7af7042013-10-12 22:38:11 -0500380 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
381 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800382 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500383 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800384}
385
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500386static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400387destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300388{
389 struct shell_grab *grab;
390
391 grab = container_of(listener, struct shell_grab,
392 shsurf_destroy_listener);
393
394 grab->shsurf = NULL;
395}
396
Jason Ekstranda7af7042013-10-12 22:38:11 -0500397static struct weston_view *
398get_default_view(struct weston_surface *surface)
399{
400 struct shell_surface *shsurf;
401 struct weston_view *view;
402
403 if (!surface || wl_list_empty(&surface->views))
404 return NULL;
405
406 shsurf = get_shell_surface(surface);
407 if (shsurf)
408 return shsurf->view;
409
410 wl_list_for_each(view, &surface->views, surface_link)
411 if (weston_view_is_mapped(view))
412 return view;
413
414 return container_of(surface->views.next, struct weston_view, surface_link);
415}
416
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300417static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400418popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400419
420static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300421shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400422 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300423 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400424 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300425 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300426{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300427 struct desktop_shell *shell = shsurf->shell;
428
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400429 popup_grab_end(pointer);
430
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300431 grab->grab.interface = interface;
432 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400433 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500434 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400435 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300436
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700437 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400438 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400439 if (shell->child.desktop_shell) {
440 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
441 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500442 weston_pointer_set_focus(pointer,
443 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400444 wl_fixed_from_int(0),
445 wl_fixed_from_int(0));
446 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300447}
448
449static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300450shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300451{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700452 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400453 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700454 grab->shsurf->grabbed = 0;
455 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300456
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700457 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300458}
459
460static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700461shell_touch_grab_start(struct shell_touch_grab *grab,
462 const struct weston_touch_grab_interface *interface,
463 struct shell_surface *shsurf,
464 struct weston_touch *touch)
465{
466 struct desktop_shell *shell = shsurf->shell;
467
468 grab->grab.interface = interface;
469 grab->shsurf = shsurf;
470 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
471 wl_signal_add(&shsurf->destroy_signal,
472 &grab->shsurf_destroy_listener);
473
474 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700475 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700476
477 weston_touch_start_grab(touch, &grab->grab);
478 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500479 weston_touch_set_focus(touch->seat,
480 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700481}
482
483static void
484shell_touch_grab_end(struct shell_touch_grab *grab)
485{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700486 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700487 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700488 grab->shsurf->grabbed = 0;
489 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700490
491 weston_touch_end_grab(grab->touch);
492}
493
494static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500495center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800496 struct weston_output *output);
497
Daniel Stone496ca172012-05-30 16:31:42 +0100498static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300499get_modifier(char *modifier)
500{
501 if (!modifier)
502 return MODIFIER_SUPER;
503
504 if (!strcmp("ctrl", modifier))
505 return MODIFIER_CTRL;
506 else if (!strcmp("alt", modifier))
507 return MODIFIER_ALT;
508 else if (!strcmp("super", modifier))
509 return MODIFIER_SUPER;
510 else
511 return MODIFIER_SUPER;
512}
513
Juan Zhaoe10d2792012-04-25 19:09:52 +0800514static enum animation_type
515get_animation_type(char *animation)
516{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800517 if (!strcmp("zoom", animation))
518 return ANIMATION_ZOOM;
519 else if (!strcmp("fade", animation))
520 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100521 else if (!strcmp("dim-layer", animation))
522 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800523 else
524 return ANIMATION_NONE;
525}
526
Alex Wu4539b082012-03-01 12:57:46 +0800527static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400528shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200529{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400530 struct weston_config_section *section;
531 int duration;
532 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200533
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400534 section = weston_config_get_section(shell->compositor->config,
535 "screensaver", NULL, NULL);
536 weston_config_section_get_string(section,
537 "path", &shell->screensaver.path, NULL);
538 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200539 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400540
541 section = weston_config_get_section(shell->compositor->config,
542 "shell", NULL, NULL);
543 weston_config_section_get_string(section,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100544 "client", &s, LIBEXECDIR "/weston-desktop-shell");
545 shell->client = s;
546 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400547 "binding-modifier", &s, "super");
548 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200549 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400550 weston_config_section_get_string(section, "animation", &s, "none");
551 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200552 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700553 weston_config_section_get_string(section,
554 "startup-animation", &s, "fade");
555 shell->startup_animation_type = get_animation_type(s);
556 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700557 if (shell->startup_animation_type == ANIMATION_ZOOM)
558 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100559 weston_config_section_get_string(section, "focus-animation", &s, "none");
560 shell->focus_animation_type = get_animation_type(s);
561 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400562 weston_config_section_get_uint(section, "num-workspaces",
563 &shell->workspaces.num,
564 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200565}
566
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100567static struct weston_output *
568get_default_output(struct weston_compositor *compositor)
569{
570 return container_of(compositor->output_list.next,
571 struct weston_output, link);
572}
573
574
575/* no-op func for checking focus surface */
576static void
577focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy,
578 int32_t width, int32_t height)
579{
580}
581
582static struct focus_surface *
583get_focus_surface(struct weston_surface *surface)
584{
585 if (surface->configure == focus_surface_configure)
586 return surface->configure_private;
587 else
588 return NULL;
589}
590
591static bool
592is_focus_surface (struct weston_surface *es)
593{
594 return (es->configure == focus_surface_configure);
595}
596
597static bool
598is_focus_view (struct weston_view *view)
599{
600 return is_focus_surface (view->surface);
601}
602
603static struct focus_surface *
604create_focus_surface(struct weston_compositor *ec,
605 struct weston_output *output)
606{
607 struct focus_surface *fsurf = NULL;
608 struct weston_surface *surface = NULL;
609
610 fsurf = malloc(sizeof *fsurf);
611 if (!fsurf)
612 return NULL;
613
614 fsurf->surface = weston_surface_create(ec);
615 surface = fsurf->surface;
616 if (surface == NULL) {
617 free(fsurf);
618 return NULL;
619 }
620
621 surface->configure = focus_surface_configure;
622 surface->output = output;
623 surface->configure_private = fsurf;
624
625 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100626 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100627
628 weston_view_configure(fsurf->view, output->x, output->y,
629 output->width, output->height);
630 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
631 pixman_region32_fini(&surface->opaque);
632 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
633 output->width, output->height);
634 pixman_region32_fini(&surface->input);
635 pixman_region32_init(&surface->input);
636
637 wl_list_init(&fsurf->workspace_transform.link);
638
639 return fsurf;
640}
641
642static void
643focus_surface_destroy(struct focus_surface *fsurf)
644{
645 weston_surface_destroy(fsurf->surface);
646 free(fsurf);
647}
648
649static void
650focus_animation_done(struct weston_view_animation *animation, void *data)
651{
652 struct workspace *ws = data;
653
654 ws->focus_animation = NULL;
655}
656
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200657static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200658focus_state_destroy(struct focus_state *state)
659{
660 wl_list_remove(&state->seat_destroy_listener.link);
661 wl_list_remove(&state->surface_destroy_listener.link);
662 free(state);
663}
664
665static void
666focus_state_seat_destroy(struct wl_listener *listener, void *data)
667{
668 struct focus_state *state = container_of(listener,
669 struct focus_state,
670 seat_destroy_listener);
671
672 wl_list_remove(&state->link);
673 focus_state_destroy(state);
674}
675
676static void
677focus_state_surface_destroy(struct wl_listener *listener, void *data)
678{
679 struct focus_state *state = container_of(listener,
680 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400681 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400682 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500683 struct weston_surface *main_surface, *next;
684 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200685
Pekka Paalanen01388e22013-04-25 13:57:44 +0300686 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
687
Kristian Høgsberge3778222012-07-31 17:29:30 -0400688 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500689 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
690 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400691 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100692 if (is_focus_view(view))
693 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400694
Jason Ekstranda7af7042013-10-12 22:38:11 -0500695 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400696 break;
697 }
698
Pekka Paalanen01388e22013-04-25 13:57:44 +0300699 /* if the focus was a sub-surface, activate its main surface */
700 if (main_surface != state->keyboard_focus)
701 next = main_surface;
702
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100703 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400704 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100705 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400706 activate(shell, next, state->seat);
707 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100708 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
709 if (state->ws->focus_animation)
710 weston_view_animation_destroy(state->ws->focus_animation);
711
712 state->ws->focus_animation = weston_fade_run(
713 state->ws->fsurf_front->view,
714 state->ws->fsurf_front->view->alpha, 0.0, 300,
715 focus_animation_done, state->ws);
716 }
717
Kristian Høgsberge3778222012-07-31 17:29:30 -0400718 wl_list_remove(&state->link);
719 focus_state_destroy(state);
720 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200721}
722
723static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400724focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200725{
Jonas Ådahl04769742012-06-13 00:01:24 +0200726 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200727
728 state = malloc(sizeof *state);
729 if (state == NULL)
730 return NULL;
731
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100732 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400733 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200734 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400735 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200736
737 state->seat_destroy_listener.notify = focus_state_seat_destroy;
738 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400739 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200740 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400741 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200742
743 return state;
744}
745
Jonas Ådahl8538b222012-08-29 22:13:03 +0200746static struct focus_state *
747ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
748{
749 struct workspace *ws = get_current_workspace(shell);
750 struct focus_state *state;
751
752 wl_list_for_each(state, &ws->focus_list, link)
753 if (state->seat == seat)
754 break;
755
756 if (&state->link == &ws->focus_list)
757 state = focus_state_create(seat, ws);
758
759 return state;
760}
761
Jonas Ådahl04769742012-06-13 00:01:24 +0200762static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400763restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200764{
765 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400766 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200767
768 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400769 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200770
Kristian Høgsberge3148752013-05-06 23:19:49 -0400771 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200772 }
773}
774
775static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200776replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
777 struct weston_seat *seat)
778{
779 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400780 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200781
782 wl_list_for_each(state, &ws->focus_list, link) {
783 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400784 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500785 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200786 return;
787 }
788 }
789}
790
791static void
792drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
793 struct weston_surface *surface)
794{
795 struct focus_state *state;
796
797 wl_list_for_each(state, &ws->focus_list, link)
798 if (state->keyboard_focus == surface)
799 state->keyboard_focus = NULL;
800}
801
802static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100803animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
804 struct weston_view *from, struct weston_view *to)
805{
806 struct weston_output *output;
807 bool focus_surface_created = false;
808
809 /* FIXME: Only support dim animation using two layers */
810 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
811 return;
812
813 output = get_default_output(shell->compositor);
814 if (ws->fsurf_front == NULL && (from || to)) {
815 ws->fsurf_front = create_focus_surface(shell->compositor, output);
816 ws->fsurf_back = create_focus_surface(shell->compositor, output);
817 ws->fsurf_front->view->alpha = 0.0;
818 ws->fsurf_back->view->alpha = 0.0;
819 focus_surface_created = true;
820 } else {
821 wl_list_remove(&ws->fsurf_front->view->layer_link);
822 wl_list_remove(&ws->fsurf_back->view->layer_link);
823 }
824
825 if (ws->focus_animation) {
826 weston_view_animation_destroy(ws->focus_animation);
827 ws->focus_animation = NULL;
828 }
829
830 if (to)
831 wl_list_insert(&to->layer_link,
832 &ws->fsurf_front->view->layer_link);
833 else if (from)
834 wl_list_insert(&ws->layer.view_list,
835 &ws->fsurf_front->view->layer_link);
836
837 if (focus_surface_created) {
838 ws->focus_animation = weston_fade_run(
839 ws->fsurf_front->view,
840 ws->fsurf_front->view->alpha, 0.6, 300,
841 focus_animation_done, ws);
842 } else if (from) {
843 wl_list_insert(&from->layer_link,
844 &ws->fsurf_back->view->layer_link);
845 ws->focus_animation = weston_stable_fade_run(
846 ws->fsurf_front->view, 0.0,
847 ws->fsurf_back->view, 0.6,
848 focus_animation_done, ws);
849 } else if (to) {
850 wl_list_insert(&ws->layer.view_list,
851 &ws->fsurf_back->view->layer_link);
852 ws->focus_animation = weston_stable_fade_run(
853 ws->fsurf_front->view, 0.0,
854 ws->fsurf_back->view, 0.6,
855 focus_animation_done, ws);
856 }
857}
858
859static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200860workspace_destroy(struct workspace *ws)
861{
Jonas Ådahl04769742012-06-13 00:01:24 +0200862 struct focus_state *state, *next;
863
864 wl_list_for_each_safe(state, next, &ws->focus_list, link)
865 focus_state_destroy(state);
866
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100867 if (ws->fsurf_front)
868 focus_surface_destroy(ws->fsurf_front);
869 if (ws->fsurf_back)
870 focus_surface_destroy(ws->fsurf_back);
871
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200872 free(ws);
873}
874
Jonas Ådahl04769742012-06-13 00:01:24 +0200875static void
876seat_destroyed(struct wl_listener *listener, void *data)
877{
878 struct weston_seat *seat = data;
879 struct focus_state *state, *next;
880 struct workspace *ws = container_of(listener,
881 struct workspace,
882 seat_destroyed_listener);
883
884 wl_list_for_each_safe(state, next, &ws->focus_list, link)
885 if (state->seat == seat)
886 wl_list_remove(&state->link);
887}
888
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200889static struct workspace *
890workspace_create(void)
891{
892 struct workspace *ws = malloc(sizeof *ws);
893 if (ws == NULL)
894 return NULL;
895
896 weston_layer_init(&ws->layer, NULL);
897
Jonas Ådahl04769742012-06-13 00:01:24 +0200898 wl_list_init(&ws->focus_list);
899 wl_list_init(&ws->seat_destroyed_listener.link);
900 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100901 ws->fsurf_front = NULL;
902 ws->fsurf_back = NULL;
903 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200904
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905 return ws;
906}
907
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200908static int
909workspace_is_empty(struct workspace *ws)
910{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912}
913
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200914static struct workspace *
915get_workspace(struct desktop_shell *shell, unsigned int index)
916{
917 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200918 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200919 pws += index;
920 return *pws;
921}
922
923static struct workspace *
924get_current_workspace(struct desktop_shell *shell)
925{
926 return get_workspace(shell, shell->workspaces.current);
927}
928
929static void
930activate_workspace(struct desktop_shell *shell, unsigned int index)
931{
932 struct workspace *ws;
933
934 ws = get_workspace(shell, index);
935 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
936
937 shell->workspaces.current = index;
938}
939
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200940static unsigned int
941get_output_height(struct weston_output *output)
942{
943 return abs(output->region.extents.y1 - output->region.extents.y2);
944}
945
946static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100947view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200948{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200949 struct weston_transform *transform;
950
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100951 if (is_focus_view(view)) {
952 struct focus_surface *fsurf = get_focus_surface(view->surface);
953 transform = &fsurf->workspace_transform;
954 } else {
955 struct shell_surface *shsurf = get_shell_surface(view->surface);
956 transform = &shsurf->workspace_transform;
957 }
958
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200959 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500960 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100961 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200962
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100963 weston_matrix_init(&transform->matrix);
964 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967}
968
969static void
970workspace_translate_out(struct workspace *ws, double fraction)
971{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200973 unsigned int height;
974 double d;
975
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
977 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978 d = height * fraction;
979
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100980 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200981 }
982}
983
984static void
985workspace_translate_in(struct workspace *ws, double fraction)
986{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200988 unsigned int height;
989 double d;
990
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
992 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993
994 if (fraction > 0)
995 d = -(height - height * fraction);
996 else
997 d = height + height * fraction;
998
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100999 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001000 }
1001}
1002
1003static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001004broadcast_current_workspace_state(struct desktop_shell *shell)
1005{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001006 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001007
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001008 wl_resource_for_each(resource, &shell->workspaces.client_list)
1009 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001010 shell->workspaces.current,
1011 shell->workspaces.num);
1012}
1013
1014static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001015reverse_workspace_change_animation(struct desktop_shell *shell,
1016 unsigned int index,
1017 struct workspace *from,
1018 struct workspace *to)
1019{
1020 shell->workspaces.current = index;
1021
1022 shell->workspaces.anim_to = to;
1023 shell->workspaces.anim_from = from;
1024 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1025 shell->workspaces.anim_timestamp = 0;
1026
Scott Moreau4272e632012-08-13 09:58:41 -06001027 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001028}
1029
1030static void
1031workspace_deactivate_transforms(struct workspace *ws)
1032{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001034 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001035
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001037 if (is_focus_view(view)) {
1038 struct focus_surface *fsurf = get_focus_surface(view->surface);
1039 transform = &fsurf->workspace_transform;
1040 } else {
1041 struct shell_surface *shsurf = get_shell_surface(view->surface);
1042 transform = &shsurf->workspace_transform;
1043 }
1044
1045 if (!wl_list_empty(&transform->link)) {
1046 wl_list_remove(&transform->link);
1047 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001048 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001049 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001050 }
1051}
1052
1053static void
1054finish_workspace_change_animation(struct desktop_shell *shell,
1055 struct workspace *from,
1056 struct workspace *to)
1057{
Scott Moreau4272e632012-08-13 09:58:41 -06001058 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001059
1060 wl_list_remove(&shell->workspaces.animation.link);
1061 workspace_deactivate_transforms(from);
1062 workspace_deactivate_transforms(to);
1063 shell->workspaces.anim_to = NULL;
1064
1065 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1066}
1067
1068static void
1069animate_workspace_change_frame(struct weston_animation *animation,
1070 struct weston_output *output, uint32_t msecs)
1071{
1072 struct desktop_shell *shell =
1073 container_of(animation, struct desktop_shell,
1074 workspaces.animation);
1075 struct workspace *from = shell->workspaces.anim_from;
1076 struct workspace *to = shell->workspaces.anim_to;
1077 uint32_t t;
1078 double x, y;
1079
1080 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1081 finish_workspace_change_animation(shell, from, to);
1082 return;
1083 }
1084
1085 if (shell->workspaces.anim_timestamp == 0) {
1086 if (shell->workspaces.anim_current == 0.0)
1087 shell->workspaces.anim_timestamp = msecs;
1088 else
1089 shell->workspaces.anim_timestamp =
1090 msecs -
1091 /* Invers of movement function 'y' below. */
1092 (asin(1.0 - shell->workspaces.anim_current) *
1093 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1094 M_2_PI);
1095 }
1096
1097 t = msecs - shell->workspaces.anim_timestamp;
1098
1099 /*
1100 * x = [0, π/2]
1101 * y(x) = sin(x)
1102 */
1103 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1104 y = sin(x);
1105
1106 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001107 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001108
1109 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1110 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1111 shell->workspaces.anim_current = y;
1112
Scott Moreau4272e632012-08-13 09:58:41 -06001113 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001115 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001116 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117}
1118
1119static void
1120animate_workspace_change(struct desktop_shell *shell,
1121 unsigned int index,
1122 struct workspace *from,
1123 struct workspace *to)
1124{
1125 struct weston_output *output;
1126
1127 int dir;
1128
1129 if (index > shell->workspaces.current)
1130 dir = -1;
1131 else
1132 dir = 1;
1133
1134 shell->workspaces.current = index;
1135
1136 shell->workspaces.anim_dir = dir;
1137 shell->workspaces.anim_from = from;
1138 shell->workspaces.anim_to = to;
1139 shell->workspaces.anim_current = 0.0;
1140 shell->workspaces.anim_timestamp = 0;
1141
1142 output = container_of(shell->compositor->output_list.next,
1143 struct weston_output, link);
1144 wl_list_insert(&output->animation_list,
1145 &shell->workspaces.animation.link);
1146
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001147 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001148
1149 workspace_translate_in(to, 0);
1150
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001151 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001152
Scott Moreau4272e632012-08-13 09:58:41 -06001153 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154}
1155
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001156static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001157update_workspace(struct desktop_shell *shell, unsigned int index,
1158 struct workspace *from, struct workspace *to)
1159{
1160 shell->workspaces.current = index;
1161 wl_list_insert(&from->layer.link, &to->layer.link);
1162 wl_list_remove(&from->layer.link);
1163}
1164
1165static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001166change_workspace(struct desktop_shell *shell, unsigned int index)
1167{
1168 struct workspace *from;
1169 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001170 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001171
1172 if (index == shell->workspaces.current)
1173 return;
1174
1175 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001176 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001177 return;
1178
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001179 from = get_current_workspace(shell);
1180 to = get_workspace(shell, index);
1181
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182 if (shell->workspaces.anim_from == to &&
1183 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001184 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001185 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001186 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001187 return;
1188 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001189
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001190 if (shell->workspaces.anim_to != NULL)
1191 finish_workspace_change_animation(shell,
1192 shell->workspaces.anim_from,
1193 shell->workspaces.anim_to);
1194
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001195 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001196
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001197 if (shell->focus_animation_type != ANIMATION_NONE) {
1198 wl_list_for_each(state, &from->focus_list, link)
1199 if (state->keyboard_focus)
1200 animate_focus_change(shell, from,
1201 get_default_view(state->keyboard_focus), NULL);
1202
1203 wl_list_for_each(state, &to->focus_list, link)
1204 if (state->keyboard_focus)
1205 animate_focus_change(shell, to,
1206 NULL, get_default_view(state->keyboard_focus));
1207 }
1208
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001209 if (workspace_is_empty(to) && workspace_is_empty(from))
1210 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001211 else
1212 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001213
1214 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001215}
1216
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001217static bool
1218workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1219{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221 struct wl_list *e;
1222
1223 if (wl_list_empty(list))
1224 return false;
1225
1226 e = list->next;
1227
1228 if (e->next != list)
1229 return false;
1230
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001232}
1233
1234static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235move_view_to_workspace(struct desktop_shell *shell,
1236 struct weston_view *view,
1237 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001238{
1239 struct workspace *from;
1240 struct workspace *to;
1241 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001242 struct weston_surface *focus;
1243
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001245
1246 if (workspace == shell->workspaces.current)
1247 return;
1248
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001249 if (workspace >= shell->workspaces.num)
1250 workspace = shell->workspaces.num - 1;
1251
Jonas Ådahle9d22502012-08-29 22:13:01 +02001252 from = get_current_workspace(shell);
1253 to = get_workspace(shell, workspace);
1254
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 wl_list_remove(&view->layer_link);
1256 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001257
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001259 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1260 if (!seat->keyboard)
1261 continue;
1262
1263 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001265 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001266 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001269}
1270
1271static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001273 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001274 unsigned int index)
1275{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001276 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001278 struct shell_surface *shsurf;
1279 struct workspace *from;
1280 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001281 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001282
Pekka Paalanen01388e22013-04-25 13:57:44 +03001283 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 view = get_default_view(surface);
1285 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001286 index == shell->workspaces.current ||
1287 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001288 return;
1289
1290 from = get_current_workspace(shell);
1291 to = get_workspace(shell, index);
1292
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 wl_list_remove(&view->layer_link);
1294 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295
Jonas Ådahle9d22502012-08-29 22:13:01 +02001296 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001297 drop_focus_state(shell, from, surface);
1298
1299 if (shell->workspaces.anim_from == to &&
1300 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001301 wl_list_remove(&to->layer.link);
1302 wl_list_insert(from->layer.link.prev, &to->layer.link);
1303
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001304 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001305 broadcast_current_workspace_state(shell);
1306
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001307 return;
1308 }
1309
1310 if (shell->workspaces.anim_to != NULL)
1311 finish_workspace_change_animation(shell,
1312 shell->workspaces.anim_from,
1313 shell->workspaces.anim_to);
1314
1315 if (workspace_is_empty(from) &&
1316 workspace_has_only(to, surface))
1317 update_workspace(shell, index, from, to);
1318 else {
1319 shsurf = get_shell_surface(surface);
1320 if (wl_list_empty(&shsurf->workspace_transform.link))
1321 wl_list_insert(&shell->workspaces.anim_sticky_list,
1322 &shsurf->workspace_transform.link);
1323
1324 animate_workspace_change(shell, index, from, to);
1325 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001326
1327 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001328
1329 state = ensure_focus_state(shell, seat);
1330 if (state != NULL)
1331 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001332}
1333
1334static void
1335workspace_manager_move_surface(struct wl_client *client,
1336 struct wl_resource *resource,
1337 struct wl_resource *surface_resource,
1338 uint32_t workspace)
1339{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001340 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001341 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001342 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001343 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001345
Pekka Paalanen01388e22013-04-25 13:57:44 +03001346 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 view = get_default_view(main_surface);
1348 if (!view)
1349 return;
1350 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001351}
1352
1353static const struct workspace_manager_interface workspace_manager_implementation = {
1354 workspace_manager_move_surface,
1355};
1356
1357static void
1358unbind_resource(struct wl_resource *resource)
1359{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001360 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001361}
1362
1363static void
1364bind_workspace_manager(struct wl_client *client,
1365 void *data, uint32_t version, uint32_t id)
1366{
1367 struct desktop_shell *shell = data;
1368 struct wl_resource *resource;
1369
Jason Ekstranda85118c2013-06-27 20:17:02 -05001370 resource = wl_resource_create(client,
1371 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001372
1373 if (resource == NULL) {
1374 weston_log("couldn't add workspace manager object");
1375 return;
1376 }
1377
Jason Ekstranda85118c2013-06-27 20:17:02 -05001378 wl_resource_set_implementation(resource,
1379 &workspace_manager_implementation,
1380 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001381 wl_list_insert(&shell->workspaces.client_list,
1382 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001383
1384 workspace_manager_send_state(resource,
1385 shell->workspaces.current,
1386 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001387}
1388
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001389static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001390touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1391 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1392{
1393}
1394
1395static void
1396touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1397{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001398 struct weston_touch_move_grab *move =
1399 (struct weston_touch_move_grab *) container_of(
1400 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001401
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001402 if (grab->touch->seat->num_tp == 0) {
1403 shell_touch_grab_end(&move->base);
1404 free(move);
1405 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001406}
1407
1408static void
1409touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1410 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1411{
1412 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1413 struct shell_surface *shsurf = move->base.shsurf;
1414 struct weston_surface *es;
1415 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1416 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1417
1418 if (!shsurf)
1419 return;
1420
1421 es = shsurf->surface;
1422
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423 weston_view_configure(shsurf->view, dx, dy,
1424 shsurf->view->geometry.width,
1425 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001426
1427 weston_compositor_schedule_repaint(es->compositor);
1428}
1429
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001430static void
1431touch_move_grab_cancel(struct weston_touch_grab *grab)
1432{
1433 struct weston_touch_move_grab *move =
1434 (struct weston_touch_move_grab *) container_of(
1435 grab, struct shell_touch_grab, grab);
1436
1437 shell_touch_grab_end(&move->base);
1438 free(move);
1439}
1440
Rusty Lynch1084da52013-08-15 09:10:08 -07001441static const struct weston_touch_grab_interface touch_move_grab_interface = {
1442 touch_move_grab_down,
1443 touch_move_grab_up,
1444 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001445 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001446};
1447
1448static int
1449surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1450{
1451 struct weston_touch_move_grab *move;
1452
1453 if (!shsurf)
1454 return -1;
1455
1456 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1457 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001458 if (shsurf->grabbed)
1459 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001460
1461 move = malloc(sizeof *move);
1462 if (!move)
1463 return -1;
1464
Jason Ekstranda7af7042013-10-12 22:38:11 -05001465 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001466 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001467 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001468 seat->touch->grab_y;
1469
1470 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1471 seat->touch);
1472
1473 return 0;
1474}
1475
1476static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001477noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001478{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001479}
1480
1481static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001482move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1483 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001484{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001485 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001486 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001487 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001488 int dx, dy;
1489
1490 weston_pointer_move(pointer, x, y);
1491 dx = wl_fixed_to_int(pointer->x + move->dx);
1492 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001493
1494 if (!shsurf)
1495 return;
1496
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 weston_view_configure(shsurf->view, dx, dy,
1498 shsurf->view->geometry.width,
1499 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001500
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001502}
1503
1504static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001505move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001506 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001507{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001508 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1509 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001510 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001511 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001512
Daniel Stone4dbadb12012-05-30 16:31:51 +01001513 if (pointer->button_count == 0 &&
1514 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001515 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001516 free(grab);
1517 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001518}
1519
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001520static void
1521move_grab_cancel(struct weston_pointer_grab *grab)
1522{
1523 struct shell_grab *shell_grab =
1524 container_of(grab, struct shell_grab, grab);
1525
1526 shell_grab_end(shell_grab);
1527 free(grab);
1528}
1529
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001530static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001531 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001532 move_grab_motion,
1533 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001534 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001535};
1536
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001537static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001538surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001539{
1540 struct weston_move_grab *move;
1541
1542 if (!shsurf)
1543 return -1;
1544
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001545 if (shsurf->grabbed)
1546 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001547 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1548 return 0;
1549
1550 move = malloc(sizeof *move);
1551 if (!move)
1552 return -1;
1553
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001555 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001557 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001558
1559 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001560 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561
1562 return 0;
1563}
1564
1565static void
1566shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1567 struct wl_resource *seat_resource, uint32_t serial)
1568{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001569 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001570 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001571 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001572
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001573 if (seat->pointer &&
1574 seat->pointer->button_count > 0 &&
1575 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001577 if ((surface == shsurf->surface) &&
1578 (surface_move(shsurf, seat) < 0))
1579 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001580 } else if (seat->touch &&
1581 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001583 if ((surface == shsurf->surface) &&
1584 (surface_touch_move(shsurf, seat) < 0))
1585 wl_resource_post_no_memory(resource);
1586 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001587}
1588
1589struct weston_resize_grab {
1590 struct shell_grab base;
1591 uint32_t edges;
1592 int32_t width, height;
1593};
1594
1595static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001596resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1597 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001598{
1599 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001600 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001601 struct shell_surface *shsurf = resize->base.shsurf;
1602 int32_t width, height;
1603 wl_fixed_t from_x, from_y;
1604 wl_fixed_t to_x, to_y;
1605
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001606 weston_pointer_move(pointer, x, y);
1607
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001608 if (!shsurf)
1609 return;
1610
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 weston_view_from_global_fixed(shsurf->view,
1612 pointer->grab_x, pointer->grab_y,
1613 &from_x, &from_y);
1614 weston_view_from_global_fixed(shsurf->view,
1615 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001616
1617 width = resize->width;
1618 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1619 width += wl_fixed_to_int(from_x - to_x);
1620 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1621 width += wl_fixed_to_int(to_x - from_x);
1622 }
1623
1624 height = resize->height;
1625 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1626 height += wl_fixed_to_int(from_y - to_y);
1627 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1628 height += wl_fixed_to_int(to_y - from_y);
1629 }
1630
1631 shsurf->client->send_configure(shsurf->surface,
1632 resize->edges, width, height);
1633}
1634
1635static void
1636send_configure(struct weston_surface *surface,
1637 uint32_t edges, int32_t width, int32_t height)
1638{
1639 struct shell_surface *shsurf = get_shell_surface(surface);
1640
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001641 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001642 edges, width, height);
1643}
1644
1645static const struct weston_shell_client shell_client = {
1646 send_configure
1647};
1648
1649static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001650resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001651 uint32_t time, uint32_t button, uint32_t state_w)
1652{
1653 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001654 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001655 enum wl_pointer_button_state state = state_w;
1656
1657 if (pointer->button_count == 0 &&
1658 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1659 shell_grab_end(&resize->base);
1660 free(grab);
1661 }
1662}
1663
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001664static void
1665resize_grab_cancel(struct weston_pointer_grab *grab)
1666{
1667 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1668
1669 shell_grab_end(&resize->base);
1670 free(grab);
1671}
1672
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001673static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001674 noop_grab_focus,
1675 resize_grab_motion,
1676 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001677 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001678};
1679
Giulio Camuffob8366642013-04-25 13:57:46 +03001680/*
1681 * Returns the bounding box of a surface and all its sub-surfaces,
1682 * in the surface coordinates system. */
1683static void
1684surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1685 int32_t *y, int32_t *w, int32_t *h) {
1686 pixman_region32_t region;
1687 pixman_box32_t *box;
1688 struct weston_subsurface *subsurface;
1689
1690 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001691 surface->width,
1692 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001693
1694 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1695 pixman_region32_union_rect(&region, &region,
1696 subsurface->position.x,
1697 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 subsurface->surface->width,
1699 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001700 }
1701
1702 box = pixman_region32_extents(&region);
1703 if (x)
1704 *x = box->x1;
1705 if (y)
1706 *y = box->y1;
1707 if (w)
1708 *w = box->x2 - box->x1;
1709 if (h)
1710 *h = box->y2 - box->y1;
1711
1712 pixman_region32_fini(&region);
1713}
1714
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715static int
1716surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001717 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001718{
1719 struct weston_resize_grab *resize;
1720
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001721 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1722 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001723 return 0;
1724
1725 if (edges == 0 || edges > 15 ||
1726 (edges & 3) == 3 || (edges & 12) == 12)
1727 return 0;
1728
1729 resize = malloc(sizeof *resize);
1730 if (!resize)
1731 return -1;
1732
1733 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001734 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1735 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001736
1737 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001738 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001739
1740 return 0;
1741}
1742
1743static void
1744shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1745 struct wl_resource *seat_resource, uint32_t serial,
1746 uint32_t edges)
1747{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001748 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001749 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001750 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001751
1752 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1753 return;
1754
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001756 if (seat->pointer->button_count == 0 ||
1757 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001758 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001759 return;
1760
Kristian Høgsberge3148752013-05-06 23:19:49 -04001761 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762 wl_resource_post_no_memory(resource);
1763}
1764
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001765static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001766end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1767
1768static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001769busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001770{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001771 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001772 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001773 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001774 wl_fixed_t sx, sy;
1775
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776 view = weston_compositor_pick_view(pointer->seat->compositor,
1777 pointer->x, pointer->y,
1778 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001779
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001781 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001782}
1783
1784static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001785busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1786 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001787{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001788 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001789}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001790
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001791static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001792busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001793 uint32_t time, uint32_t button, uint32_t state)
1794{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001795 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001796 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001797 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001798
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001799 if (shsurf && button == BTN_LEFT && state) {
1800 activate(shsurf->shell, shsurf->surface, seat);
1801 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001802 } else if (shsurf && button == BTN_RIGHT && state) {
1803 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001804 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001805 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001806}
1807
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001808static void
1809busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1810{
1811 struct shell_grab *grab = (struct shell_grab *) base;
1812
1813 shell_grab_end(grab);
1814 free(grab);
1815}
1816
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001817static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001818 busy_cursor_grab_focus,
1819 busy_cursor_grab_motion,
1820 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001821 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001822};
1823
1824static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001825set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001826{
1827 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001828
1829 grab = malloc(sizeof *grab);
1830 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001831 return;
1832
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001833 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1834 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001835}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001836
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001837static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001838end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001839{
1840 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1841
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001842 if (grab->grab.interface == &busy_cursor_grab_interface &&
1843 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001844 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001845 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001846 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001847}
1848
Scott Moreau9521d5e2012-04-19 13:06:17 -06001849static void
1850ping_timer_destroy(struct shell_surface *shsurf)
1851{
1852 if (!shsurf || !shsurf->ping_timer)
1853 return;
1854
1855 if (shsurf->ping_timer->source)
1856 wl_event_source_remove(shsurf->ping_timer->source);
1857
1858 free(shsurf->ping_timer);
1859 shsurf->ping_timer = NULL;
1860}
1861
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001862static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001863ping_timeout_handler(void *data)
1864{
1865 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001866 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001867
Scott Moreau9521d5e2012-04-19 13:06:17 -06001868 /* Client is not responding */
1869 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001870
1871 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001872 if (seat->pointer->focus &&
1873 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001874 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001875
1876 return 1;
1877}
1878
1879static void
1880ping_handler(struct weston_surface *surface, uint32_t serial)
1881{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001882 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001883 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001884 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001885
1886 if (!shsurf)
1887 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001888 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001889 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001890
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001891 if (shsurf->surface == shsurf->shell->grab_surface)
1892 return;
1893
Scott Moreauff1db4a2012-04-17 19:06:18 -06001894 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001895 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001896 if (!shsurf->ping_timer)
1897 return;
1898
1899 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001900 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1901 shsurf->ping_timer->source =
1902 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1903 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1904
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001905 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001906 }
1907}
1908
1909static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001910handle_pointer_focus(struct wl_listener *listener, void *data)
1911{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001912 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001913 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001914 struct weston_compositor *compositor;
1915 struct shell_surface *shsurf;
1916 uint32_t serial;
1917
Jason Ekstranda7af7042013-10-12 22:38:11 -05001918 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001919 return;
1920
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 compositor = view->surface->compositor;
1922 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001923
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001924 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001925 set_busy_cursor(shsurf, pointer);
1926 } else {
1927 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001929 }
1930}
1931
1932static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001933create_pointer_focus_listener(struct weston_seat *seat)
1934{
1935 struct wl_listener *listener;
1936
Kristian Høgsberge3148752013-05-06 23:19:49 -04001937 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001938 return;
1939
1940 listener = malloc(sizeof *listener);
1941 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001942 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001943}
1944
1945static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001946shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1947 uint32_t serial)
1948{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001949 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001950 struct weston_seat *seat;
1951 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001952
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001953 if (shsurf->ping_timer == NULL)
1954 /* Just ignore unsolicited pong. */
1955 return;
1956
Scott Moreauff1db4a2012-04-17 19:06:18 -06001957 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001958 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001959 wl_list_for_each(seat, &ec->seat_list, link) {
1960 if(seat->pointer)
1961 end_busy_cursor(shsurf, seat->pointer);
1962 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001963 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001964 }
1965}
1966
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001967static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001968set_title(struct shell_surface *shsurf, const char *title)
1969{
1970 free(shsurf->title);
1971 shsurf->title = strdup(title);
1972}
1973
1974static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001975shell_surface_set_title(struct wl_client *client,
1976 struct wl_resource *resource, const char *title)
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
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001980 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001981}
1982
1983static void
1984shell_surface_set_class(struct wl_client *client,
1985 struct wl_resource *resource, const char *class)
1986{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001987 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001988
1989 free(shsurf->class);
1990 shsurf->class = strdup(class);
1991}
1992
Alex Wu4539b082012-03-01 12:57:46 +08001993static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001994restore_output_mode(struct weston_output *output)
1995{
Hardening57388e42013-09-18 23:56:36 +02001996 if (output->current_mode != output->original_mode ||
1997 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001998 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001999 output->original_mode,
2000 output->original_scale,
2001 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002002}
2003
2004static void
2005restore_all_output_modes(struct weston_compositor *compositor)
2006{
2007 struct weston_output *output;
2008
2009 wl_list_for_each(output, &compositor->output_list, link)
2010 restore_output_mode(output);
2011}
2012
2013static void
Alex Wu4539b082012-03-01 12:57:46 +08002014shell_unset_fullscreen(struct shell_surface *shsurf)
2015{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002016 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08002017 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08002018 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2019 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002020 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002021 }
Alex Wu4539b082012-03-01 12:57:46 +08002022 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2023 shsurf->fullscreen.framerate = 0;
2024 wl_list_remove(&shsurf->fullscreen.transform.link);
2025 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002026 if (shsurf->fullscreen.black_view)
2027 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2028 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002029 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002030 weston_view_set_position(shsurf->view,
2031 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002032 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002033 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002034 &shsurf->rotation.transform.link);
2035 shsurf->saved_rotation_valid = false;
2036 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002037
2038 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039 wl_list_remove(&shsurf->view->layer_link);
2040 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08002041}
2042
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002043static void
2044shell_unset_maximized(struct shell_surface *shsurf)
2045{
2046 struct workspace *ws;
2047 /* undo all maximized things here */
2048 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 weston_view_set_position(shsurf->view,
2050 shsurf->saved_x,
2051 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002052
2053 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 wl_list_insert(&shsurf->view->geometry.transformation_list,
2055 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002056 shsurf->saved_rotation_valid = false;
2057 }
2058
2059 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 wl_list_remove(&shsurf->view->layer_link);
2061 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002062}
2063
Pekka Paalanen98262232011-12-01 10:42:22 +02002064static int
2065reset_shell_surface_type(struct shell_surface *surface)
2066{
2067 switch (surface->type) {
2068 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08002069 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002070 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002071 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002072 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002073 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002074 case SHELL_SURFACE_NONE:
2075 case SHELL_SURFACE_TOPLEVEL:
2076 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002077 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002078 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02002079 break;
2080 }
2081
2082 surface->type = SHELL_SURFACE_NONE;
2083 return 0;
2084}
2085
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002086static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002087set_surface_type(struct shell_surface *shsurf)
2088{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002089 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002091
2092 reset_shell_surface_type(shsurf);
2093
2094 shsurf->type = shsurf->next_type;
2095 shsurf->next_type = SHELL_SURFACE_NONE;
2096
2097 switch (shsurf->type) {
2098 case SHELL_SURFACE_TOPLEVEL:
2099 break;
2100 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002101 if (pev)
2102 weston_view_set_position(shsurf->view,
2103 pev->geometry.x + shsurf->transient.x,
2104 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002105 break;
2106
2107 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002108 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 shsurf->saved_x = shsurf->view->geometry.x;
2110 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002111 shsurf->saved_position_valid = true;
2112
2113 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2114 wl_list_remove(&shsurf->rotation.transform.link);
2115 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002117 shsurf->saved_rotation_valid = true;
2118 }
2119 break;
2120
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002121 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122 weston_view_set_position(shsurf->view, shsurf->transient.x,
2123 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002124 break;
2125
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002126 default:
2127 break;
2128 }
2129}
2130
2131static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002132set_toplevel(struct shell_surface *shsurf)
2133{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002134 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002135}
2136
2137static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002138shell_surface_set_toplevel(struct wl_client *client,
2139 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002140{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002141 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002142
Tiago Vignattibc052c92012-04-19 16:18:18 +03002143 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002144}
2145
2146static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04002147set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002148 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04002149{
2150 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002151 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002152 shsurf->transient.x = x;
2153 shsurf->transient.y = y;
2154 shsurf->transient.flags = flags;
2155 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
2156}
2157
2158static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002159shell_surface_set_transient(struct wl_client *client,
2160 struct wl_resource *resource,
2161 struct wl_resource *parent_resource,
2162 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002163{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002164 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002165 struct weston_surface *parent =
2166 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002167
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002168 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002169}
2170
Tiago Vignattibe143262012-04-16 17:31:41 +03002171static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002172shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002173{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002174 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002175}
2176
2177static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002178get_output_panel_height(struct desktop_shell *shell,
2179 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08002180{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08002182 int panel_height = 0;
2183
2184 if (!output)
2185 return 0;
2186
Jason Ekstranda7af7042013-10-12 22:38:11 -05002187 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2188 if (view->surface->output == output) {
2189 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08002190 break;
2191 }
2192 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002193
Juan Zhao96879df2012-02-07 08:45:41 +08002194 return panel_height;
2195}
2196
2197static void
2198shell_surface_set_maximized(struct wl_client *client,
2199 struct wl_resource *resource,
2200 struct wl_resource *output_resource )
2201{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002202 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08002203 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03002204 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08002205 uint32_t edges = 0, panel_height = 0;
2206
2207 /* get the default output, if the client set it as NULL
2208 check whether the ouput is available */
2209 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002210 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002211 else if (es->output)
2212 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002213 else
2214 shsurf->output = get_default_output(es->compositor);
2215
Tiago Vignattibe143262012-04-16 17:31:41 +03002216 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01002217 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002218 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002219
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002220 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002221 shsurf->output->width,
2222 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002223
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002224 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002225}
2226
Alex Wu21858432012-04-01 20:13:08 +08002227static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002228black_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 +08002229
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002231create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002232 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002233 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002234{
2235 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002236 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002237
2238 surface = weston_surface_create(ec);
2239 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002240 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002241 return NULL;
2242 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 view = weston_view_create(surface);
2244 if (surface == NULL) {
2245 weston_log("no memory\n");
2246 weston_surface_destroy(surface);
2247 return NULL;
2248 }
Alex Wu4539b082012-03-01 12:57:46 +08002249
Alex Wu21858432012-04-01 20:13:08 +08002250 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002251 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002252 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002253 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002254 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002255 pixman_region32_fini(&surface->input);
2256 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002257 surface->width = w;
2258 surface->height = h;
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002259
Jason Ekstranda7af7042013-10-12 22:38:11 -05002260 weston_view_configure(view, x, y, w, h);
2261
2262 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002263}
2264
2265/* Create black surface and append it to the associated fullscreen surface.
2266 * Handle size dismatch and positioning according to the method. */
2267static void
2268shell_configure_fullscreen(struct shell_surface *shsurf)
2269{
2270 struct weston_output *output = shsurf->fullscreen_output;
2271 struct weston_surface *surface = shsurf->surface;
2272 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002273 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002274 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002275
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002276 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2277 restore_output_mode(output);
2278
Jason Ekstranda7af7042013-10-12 22:38:11 -05002279 if (!shsurf->fullscreen.black_view)
2280 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002281 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002282 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002283 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002284 output->width,
2285 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002286
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2288 wl_list_insert(&shsurf->view->layer_link,
2289 &shsurf->fullscreen.black_view->layer_link);
2290 shsurf->fullscreen.black_view->surface->output = output;
2291 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002292
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002294 &surf_width, &surf_height);
2295
Alex Wu4539b082012-03-01 12:57:46 +08002296 switch (shsurf->fullscreen.type) {
2297 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002298 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002299 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002300 break;
2301 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002302 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002303 if (output->width == surf_width &&
2304 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 weston_view_set_position(shsurf->view,
2306 output->x - surf_x,
2307 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002308 break;
2309 }
2310
Alex Wu4539b082012-03-01 12:57:46 +08002311 matrix = &shsurf->fullscreen.transform.matrix;
2312 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002313
Scott Moreau1bad5db2012-08-18 01:04:05 -06002314 output_aspect = (float) output->width /
2315 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002316 /* XXX: Use surf_width and surf_height here? */
2317 surface_aspect = (float) surface->width /
2318 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002319 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002320 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002321 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002322 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002323 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002324 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002325
Alex Wu4539b082012-03-01 12:57:46 +08002326 weston_matrix_scale(matrix, scale, scale, 1);
2327 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002328 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002329 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002330 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2331 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002332 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002333
Alex Wu4539b082012-03-01 12:57:46 +08002334 break;
2335 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002336 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002337 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002338 surf_width * surface->buffer_viewport.scale,
2339 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002340 shsurf->fullscreen.framerate};
2341
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002342 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002343 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002344 weston_view_set_position(shsurf->view,
2345 output->x - surf_x,
2346 output->y - surf_y);
2347 weston_view_configure(shsurf->fullscreen.black_view,
2348 output->x - surf_x,
2349 output->y - surf_y,
2350 output->width,
2351 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002352 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002353 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002354 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002355 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002356 }
Alex Wubd3354b2012-04-17 17:20:49 +08002357 }
Alex Wu4539b082012-03-01 12:57:46 +08002358 break;
2359 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002360 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002361 break;
2362 default:
2363 break;
2364 }
2365}
2366
2367/* make the fullscreen and black surface at the top */
2368static void
2369shell_stack_fullscreen(struct shell_surface *shsurf)
2370{
Alex Wubd3354b2012-04-17 17:20:49 +08002371 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002372 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002373
Jason Ekstranda7af7042013-10-12 22:38:11 -05002374 wl_list_remove(&shsurf->view->layer_link);
2375 wl_list_insert(&shell->fullscreen_layer.view_list,
2376 &shsurf->view->layer_link);
2377 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002378
Jason Ekstranda7af7042013-10-12 22:38:11 -05002379 if (!shsurf->fullscreen.black_view)
2380 shsurf->fullscreen.black_view =
2381 create_black_surface(shsurf->surface->compositor,
2382 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002383 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002384 output->width,
2385 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002386
Jason Ekstranda7af7042013-10-12 22:38:11 -05002387 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2388 wl_list_insert(&shsurf->view->layer_link,
2389 &shsurf->fullscreen.black_view->layer_link);
2390 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002391}
2392
2393static void
2394shell_map_fullscreen(struct shell_surface *shsurf)
2395{
Alex Wu4539b082012-03-01 12:57:46 +08002396 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002397 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002398}
2399
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002400static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002401set_fullscreen(struct shell_surface *shsurf,
2402 uint32_t method,
2403 uint32_t framerate,
2404 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002405{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002406 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002407
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002408 if (output)
2409 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002410 else if (es->output)
2411 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002412 else
2413 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002414
Alex Wu4539b082012-03-01 12:57:46 +08002415 shsurf->fullscreen_output = shsurf->output;
2416 shsurf->fullscreen.type = method;
2417 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002418 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002419
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002420 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002421 shsurf->output->width,
2422 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002423}
2424
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002425static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002426shell_surface_set_fullscreen(struct wl_client *client,
2427 struct wl_resource *resource,
2428 uint32_t method,
2429 uint32_t framerate,
2430 struct wl_resource *output_resource)
2431{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002432 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002433 struct weston_output *output;
2434
2435 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002436 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002437 else
2438 output = NULL;
2439
2440 set_fullscreen(shsurf, method, framerate, output);
2441}
2442
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002443static void
2444set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2445{
2446 /* XXX: using the same fields for transient type */
2447 shsurf->transient.x = x;
2448 shsurf->transient.y = y;
2449 shsurf->transient.flags = flags;
2450 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2451}
2452
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002453static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002454
2455static void
2456destroy_shell_seat(struct wl_listener *listener, void *data)
2457{
2458 struct shell_seat *shseat =
2459 container_of(listener,
2460 struct shell_seat, seat_destroy_listener);
2461 struct shell_surface *shsurf, *prev = NULL;
2462
2463 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002464 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002465 shseat->popup_grab.client = NULL;
2466
2467 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2468 shsurf->popup.shseat = NULL;
2469 if (prev) {
2470 wl_list_init(&prev->popup.grab_link);
2471 }
2472 prev = shsurf;
2473 }
2474 wl_list_init(&prev->popup.grab_link);
2475 }
2476
2477 wl_list_remove(&shseat->seat_destroy_listener.link);
2478 free(shseat);
2479}
2480
2481static struct shell_seat *
2482create_shell_seat(struct weston_seat *seat)
2483{
2484 struct shell_seat *shseat;
2485
2486 shseat = calloc(1, sizeof *shseat);
2487 if (!shseat) {
2488 weston_log("no memory to allocate shell seat\n");
2489 return NULL;
2490 }
2491
2492 shseat->seat = seat;
2493 wl_list_init(&shseat->popup_grab.surfaces_list);
2494
2495 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2496 wl_signal_add(&seat->destroy_signal,
2497 &shseat->seat_destroy_listener);
2498
2499 return shseat;
2500}
2501
2502static struct shell_seat *
2503get_shell_seat(struct weston_seat *seat)
2504{
2505 struct wl_listener *listener;
2506
2507 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2508 if (listener == NULL)
2509 return create_shell_seat(seat);
2510
2511 return container_of(listener,
2512 struct shell_seat, seat_destroy_listener);
2513}
2514
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002515static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002516popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002517{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002518 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002519 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002520 struct shell_seat *shseat =
2521 container_of(grab, struct shell_seat, popup_grab.grab);
2522 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002523 wl_fixed_t sx, sy;
2524
Jason Ekstranda7af7042013-10-12 22:38:11 -05002525 view = weston_compositor_pick_view(pointer->seat->compositor,
2526 pointer->x, pointer->y,
2527 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002528
Jason Ekstranda7af7042013-10-12 22:38:11 -05002529 if (view && view->surface->resource &&
2530 wl_resource_get_client(view->surface->resource) == client) {
2531 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002532 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002533 weston_pointer_set_focus(pointer, NULL,
2534 wl_fixed_from_int(0),
2535 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002536 }
2537}
2538
2539static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002540popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2541 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002542{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002543 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002544 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002545 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002546
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002547 weston_pointer_move(pointer, x, y);
2548
Neil Roberts96d790e2013-09-19 17:32:00 +01002549 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002550 weston_view_from_global_fixed(pointer->focus,
2551 pointer->x, pointer->y,
2552 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002553 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002554 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002555}
2556
2557static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002558popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002559 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002560{
2561 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002562 struct shell_seat *shseat =
2563 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002564 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002565 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002566 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002567 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002568
Neil Roberts96d790e2013-09-19 17:32:00 +01002569 resource_list = &grab->pointer->focus_resource_list;
2570 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002571 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002572 wl_resource_for_each(resource, resource_list) {
2573 wl_pointer_send_button(resource, serial,
2574 time, button, state);
2575 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002576 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002577 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002578 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002579 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002580 }
2581
Daniel Stone4dbadb12012-05-30 16:31:51 +01002582 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002583 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002584}
2585
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002586static void
2587popup_grab_cancel(struct weston_pointer_grab *grab)
2588{
2589 popup_grab_end(grab->pointer);
2590}
2591
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002592static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002593 popup_grab_focus,
2594 popup_grab_motion,
2595 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002596 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002597};
2598
2599static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002600popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002601{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002602 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002603 struct shell_seat *shseat =
2604 container_of(grab, struct shell_seat, popup_grab.grab);
2605 struct shell_surface *shsurf;
2606 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002607
2608 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002609 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002610 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002611 shseat->popup_grab.grab.interface = NULL;
2612 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002613 /* Send the popup_done event to all the popups open */
2614 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002615 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002616 shsurf->popup.shseat = NULL;
2617 if (prev) {
2618 wl_list_init(&prev->popup.grab_link);
2619 }
2620 prev = shsurf;
2621 }
2622 wl_list_init(&prev->popup.grab_link);
2623 wl_list_init(&shseat->popup_grab.surfaces_list);
2624 }
2625}
2626
2627static void
2628add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2629{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002630 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002631
2632 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002633 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002634 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002635 /* We must make sure here that this popup was opened after
2636 * a mouse press, and not just by moving around with other
2637 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002638 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002639 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002640
Rob Bradforddfe31052013-06-26 19:49:11 +01002641 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002642 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002643 } else {
2644 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002645 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002646}
2647
2648static void
2649remove_popup_grab(struct shell_surface *shsurf)
2650{
2651 struct shell_seat *shseat = shsurf->popup.shseat;
2652
2653 wl_list_remove(&shsurf->popup.grab_link);
2654 wl_list_init(&shsurf->popup.grab_link);
2655 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002656 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002657 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002658 }
2659}
2660
2661static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002662shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002663{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002664 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002665 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002666
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 shsurf->surface->output = parent_view->output;
2668 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002669
Jason Ekstranda7af7042013-10-12 22:38:11 -05002670 weston_view_set_transform_parent(shsurf->view, parent_view);
2671 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2672 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002673
Kristian Høgsberge3148752013-05-06 23:19:49 -04002674 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002675 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002676 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002677 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002678 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002679 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002680}
2681
2682static void
2683shell_surface_set_popup(struct wl_client *client,
2684 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002685 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002686 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002687 struct wl_resource *parent_resource,
2688 int32_t x, int32_t y, uint32_t flags)
2689{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002690 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002691
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002692 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002693 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002694 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002695 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002696 shsurf->popup.x = x;
2697 shsurf->popup.y = y;
2698}
2699
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002700static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002701 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002702 shell_surface_move,
2703 shell_surface_resize,
2704 shell_surface_set_toplevel,
2705 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002706 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002707 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002708 shell_surface_set_maximized,
2709 shell_surface_set_title,
2710 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002711};
2712
2713static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002714destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002715{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002716 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2717
Giulio Camuffo5085a752013-03-25 21:42:45 +01002718 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2719 remove_popup_grab(shsurf);
2720 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002721
Alex Wubd3354b2012-04-17 17:20:49 +08002722 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002723 shell_surface_is_top_fullscreen(shsurf))
2724 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002725
Jason Ekstranda7af7042013-10-12 22:38:11 -05002726 if (shsurf->fullscreen.black_view)
2727 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002728
Alex Wubd3354b2012-04-17 17:20:49 +08002729 /* As destroy_resource() use wl_list_for_each_safe(),
2730 * we can always remove the listener.
2731 */
2732 wl_list_remove(&shsurf->surface_destroy_listener.link);
2733 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002734 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002735 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002736
Jason Ekstranda7af7042013-10-12 22:38:11 -05002737 weston_view_destroy(shsurf->view);
2738
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002739 wl_list_remove(&shsurf->link);
2740 free(shsurf);
2741}
2742
2743static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002744shell_destroy_shell_surface(struct wl_resource *resource)
2745{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002746 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002747
2748 destroy_shell_surface(shsurf);
2749}
2750
2751static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002752shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002753{
2754 struct shell_surface *shsurf = container_of(listener,
2755 struct shell_surface,
2756 surface_destroy_listener);
2757
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002758 if (shsurf->resource)
2759 wl_resource_destroy(shsurf->resource);
2760 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002761 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002762}
2763
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002764static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002765shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002766
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002767static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002768get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002769{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002770 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002771 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002772 else
2773 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002774}
2775
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002776static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002777create_shell_surface(void *shell, struct weston_surface *surface,
2778 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002779{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002780 struct shell_surface *shsurf;
2781
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002782 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002783 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002784 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002785 }
2786
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002787 shsurf = calloc(1, sizeof *shsurf);
2788 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002789 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002790 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002791 }
2792
Jason Ekstranda7af7042013-10-12 22:38:11 -05002793 shsurf->view = weston_view_create(surface);
2794 if (!shsurf->view) {
2795 weston_log("no memory to allocate shell surface\n");
2796 free(shsurf);
2797 return NULL;
2798 }
2799
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002800 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002801 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002802
Tiago Vignattibc052c92012-04-19 16:18:18 +03002803 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002804 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002805 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002806 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002807 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002808 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2809 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002810 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002811 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002812 wl_list_init(&shsurf->fullscreen.transform.link);
2813
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002814 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002815 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002816 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002817 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002818
2819 /* init link so its safe to always remove it in destroy_shell_surface */
2820 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002821 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002822
Pekka Paalanen460099f2012-01-20 16:48:25 +02002823 /* empty when not in use */
2824 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002825 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002826
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002827 wl_list_init(&shsurf->workspace_transform.link);
2828
Pekka Paalanen98262232011-12-01 10:42:22 +02002829 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002830 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002831
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002832 shsurf->client = client;
2833
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002834 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002835}
2836
Jason Ekstranda7af7042013-10-12 22:38:11 -05002837static struct weston_view *
2838get_primary_view(void *shell, struct shell_surface *shsurf)
2839{
2840 return shsurf->view;
2841}
2842
Tiago Vignattibc052c92012-04-19 16:18:18 +03002843static void
2844shell_get_shell_surface(struct wl_client *client,
2845 struct wl_resource *resource,
2846 uint32_t id,
2847 struct wl_resource *surface_resource)
2848{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002849 struct weston_surface *surface =
2850 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002851 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002852 struct shell_surface *shsurf;
2853
2854 if (get_shell_surface(surface)) {
2855 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002856 WL_DISPLAY_ERROR_INVALID_OBJECT,
2857 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002858 return;
2859 }
2860
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002861 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002862 if (!shsurf) {
2863 wl_resource_post_error(surface_resource,
2864 WL_DISPLAY_ERROR_INVALID_OBJECT,
2865 "surface->configure already set");
2866 return;
2867 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002868
Jason Ekstranda85118c2013-06-27 20:17:02 -05002869 shsurf->resource =
2870 wl_resource_create(client,
2871 &wl_shell_surface_interface, 1, id);
2872 wl_resource_set_implementation(shsurf->resource,
2873 &shell_surface_implementation,
2874 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002875}
2876
2877static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002878 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002879};
2880
Kristian Høgsberg07937562011-04-12 17:25:42 -04002881static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002882shell_fade(struct desktop_shell *shell, enum fade_type type);
2883
2884static int
2885screensaver_timeout(void *data)
2886{
2887 struct desktop_shell *shell = data;
2888
2889 shell_fade(shell, FADE_OUT);
2890
2891 return 1;
2892}
2893
2894static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002895handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002896{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002897 struct desktop_shell *shell =
2898 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002899
Pekka Paalanen18027e52011-12-02 16:31:49 +02002900 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002901
2902 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002903 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002904}
2905
2906static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002907launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002908{
2909 if (shell->screensaver.binding)
2910 return;
2911
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002912 if (!shell->screensaver.path) {
2913 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002914 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002915 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002916
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002917 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002918 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002919 return;
2920 }
2921
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002922 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002923 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002924 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002925 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002926}
2927
2928static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002929terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002930{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002931 if (shell->screensaver.process.pid == 0)
2932 return;
2933
2934 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002935}
2936
2937static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002938configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002939{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002941
Giulio Camuffo184df502013-02-21 11:29:21 +01002942 if (width == 0)
2943 return;
2944
Jason Ekstranda7af7042013-10-12 22:38:11 -05002945 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2946 if (v->output == ev->output && v != ev) {
2947 weston_view_unmap(v);
2948 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002949 }
2950 }
2951
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002953
Jason Ekstranda7af7042013-10-12 22:38:11 -05002954 if (wl_list_empty(&ev->layer_link)) {
2955 wl_list_insert(&layer->view_list, &ev->layer_link);
2956 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002957 }
2958}
2959
2960static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002961background_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 -04002962{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002963 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002964 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002965
Jason Ekstranda7af7042013-10-12 22:38:11 -05002966 view = container_of(es->views.next, struct weston_view, surface_link);
2967
2968 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002969}
2970
2971static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002972desktop_shell_set_background(struct wl_client *client,
2973 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002974 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002975 struct wl_resource *surface_resource)
2976{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002977 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002978 struct weston_surface *surface =
2979 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002980 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002981
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002982 if (surface->configure) {
2983 wl_resource_post_error(surface_resource,
2984 WL_DISPLAY_ERROR_INVALID_OBJECT,
2985 "surface role already assigned");
2986 return;
2987 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002988
Jason Ekstranda7af7042013-10-12 22:38:11 -05002989 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2990 weston_view_destroy(view);
2991 view = weston_view_create(surface);
2992
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002993 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002994 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002995 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002996 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002997 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002998 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002999 surface->output->width,
3000 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003001}
3002
3003static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003004panel_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 -04003005{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003006 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003007 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003008
Jason Ekstranda7af7042013-10-12 22:38:11 -05003009 view = container_of(es->views.next, struct weston_view, surface_link);
3010
3011 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003012}
3013
3014static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003015desktop_shell_set_panel(struct wl_client *client,
3016 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003017 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003018 struct wl_resource *surface_resource)
3019{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003020 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003021 struct weston_surface *surface =
3022 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003023 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003024
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003025 if (surface->configure) {
3026 wl_resource_post_error(surface_resource,
3027 WL_DISPLAY_ERROR_INVALID_OBJECT,
3028 "surface role already assigned");
3029 return;
3030 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003031
Jason Ekstranda7af7042013-10-12 22:38:11 -05003032 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3033 weston_view_destroy(view);
3034 view = weston_view_create(surface);
3035
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003036 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003037 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003038 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003039 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003040 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003041 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003042 surface->output->width,
3043 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003044}
3045
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003046static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003047lock_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 -04003048{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003049 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003050 struct weston_view *view;
3051
3052 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003053
Giulio Camuffo184df502013-02-21 11:29:21 +01003054 if (width == 0)
3055 return;
3056
Jason Ekstranda7af7042013-10-12 22:38:11 -05003057 surface->width = width;
3058 surface->height = height;
3059 view->geometry.width = width;
3060 view->geometry.height = height;
3061 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003062
3063 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003064 wl_list_insert(&shell->lock_layer.view_list,
3065 &view->layer_link);
3066 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003067 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003068 }
3069}
3070
3071static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003072handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003073{
Tiago Vignattibe143262012-04-16 17:31:41 +03003074 struct desktop_shell *shell =
3075 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003076
Martin Minarik6d118362012-06-07 18:01:59 +02003077 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003078 shell->lock_surface = NULL;
3079}
3080
3081static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003082desktop_shell_set_lock_surface(struct wl_client *client,
3083 struct wl_resource *resource,
3084 struct wl_resource *surface_resource)
3085{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003086 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003087 struct weston_surface *surface =
3088 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003089
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003090 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003091
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003092 if (!shell->locked)
3093 return;
3094
Pekka Paalanen98262232011-12-01 10:42:22 +02003095 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003096
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003097 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003098 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003099 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003100
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003101 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003102 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003103 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003104}
3105
3106static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003107resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003108{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003109 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003110
Pekka Paalanen77346a62011-11-30 16:26:35 +02003111 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003112
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003113 wl_list_remove(&shell->lock_layer.link);
3114 wl_list_insert(&shell->compositor->cursor_layer.link,
3115 &shell->fullscreen_layer.link);
3116 wl_list_insert(&shell->fullscreen_layer.link,
3117 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003118 if (shell->showing_input_panels) {
3119 wl_list_insert(&shell->panel_layer.link,
3120 &shell->input_panel_layer.link);
3121 wl_list_insert(&shell->input_panel_layer.link,
3122 &ws->layer.link);
3123 } else {
3124 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3125 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003126
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003127 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003128
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003129 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003130 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003131 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003132}
3133
3134static void
3135desktop_shell_unlock(struct wl_client *client,
3136 struct wl_resource *resource)
3137{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003138 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003139
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003140 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003141
3142 if (shell->locked)
3143 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003144}
3145
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003146static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003147desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003148 struct wl_resource *resource,
3149 struct wl_resource *surface_resource)
3150{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003151 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003152
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003153 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003154 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003155}
3156
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003157static void
3158desktop_shell_desktop_ready(struct wl_client *client,
3159 struct wl_resource *resource)
3160{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003161 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003162
3163 shell_fade_startup(shell);
3164}
3165
Kristian Høgsberg75840622011-09-06 13:48:16 -04003166static const struct desktop_shell_interface desktop_shell_implementation = {
3167 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003168 desktop_shell_set_panel,
3169 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003170 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003171 desktop_shell_set_grab_surface,
3172 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003173};
3174
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003175static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003176get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003177{
3178 struct shell_surface *shsurf;
3179
3180 shsurf = get_shell_surface(surface);
3181 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003182 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003183 return shsurf->type;
3184}
3185
Kristian Høgsberg75840622011-09-06 13:48:16 -04003186static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003187move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003188{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003189 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003190 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003191 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003192
Pekka Paalanen01388e22013-04-25 13:57:44 +03003193 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003194 if (surface == NULL)
3195 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003196
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003197 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003198 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3199 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003200 return;
3201
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003202 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003203}
3204
3205static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003206touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3207{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003208 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003209 struct weston_surface *surface;
3210 struct shell_surface *shsurf;
3211
3212 surface = weston_surface_get_main_surface(focus);
3213 if (surface == NULL)
3214 return;
3215
3216 shsurf = get_shell_surface(surface);
3217 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3218 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3219 return;
3220
3221 surface_touch_move(shsurf, (struct weston_seat *) seat);
3222}
3223
3224static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003225resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003226{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003227 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003228 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003229 uint32_t edges = 0;
3230 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003231 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003232
Pekka Paalanen01388e22013-04-25 13:57:44 +03003233 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003234 if (surface == NULL)
3235 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003236
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003237 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003238 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3239 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003240 return;
3241
Jason Ekstranda7af7042013-10-12 22:38:11 -05003242 weston_view_from_global(shsurf->view,
3243 wl_fixed_to_int(seat->pointer->grab_x),
3244 wl_fixed_to_int(seat->pointer->grab_y),
3245 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003246
Jason Ekstranda7af7042013-10-12 22:38:11 -05003247 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003248 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003249 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003250 edges |= 0;
3251 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003252 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003253
Jason Ekstranda7af7042013-10-12 22:38:11 -05003254 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003255 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003256 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003257 edges |= 0;
3258 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003259 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003260
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003261 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003262}
3263
3264static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003265surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003266 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003267{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003268 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003269 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003270 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003271 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003272
Pekka Paalanen01388e22013-04-25 13:57:44 +03003273 /* XXX: broken for windows containing sub-surfaces */
3274 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003275 if (surface == NULL)
3276 return;
3277
3278 shsurf = get_shell_surface(surface);
3279 if (!shsurf)
3280 return;
3281
Jason Ekstranda7af7042013-10-12 22:38:11 -05003282 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003283
Jason Ekstranda7af7042013-10-12 22:38:11 -05003284 if (shsurf->view->alpha > 1.0)
3285 shsurf->view->alpha = 1.0;
3286 if (shsurf->view->alpha < step)
3287 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003288
Jason Ekstranda7af7042013-10-12 22:38:11 -05003289 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003290 weston_surface_damage(surface);
3291}
3292
3293static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003294do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003295 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003296{
Daniel Stone37816df2012-05-16 18:45:18 +01003297 struct weston_seat *ws = (struct weston_seat *) seat;
3298 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003299 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003300 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003301
3302 wl_list_for_each(output, &compositor->output_list, link) {
3303 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003304 wl_fixed_to_double(seat->pointer->x),
3305 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003306 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003307 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003308 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003309 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003310 increment = -output->zoom.increment;
3311 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003312 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003313 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003314 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003315 else
3316 increment = 0;
3317
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003318 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003319
Scott Moreaue6603982012-06-11 13:07:51 -06003320 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003321 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003322 else if (output->zoom.level > output->zoom.max_level)
3323 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003324 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003325 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003326 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003327
Scott Moreaue6603982012-06-11 13:07:51 -06003328 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003329
Jason Ekstranda7af7042013-10-12 22:38:11 -05003330 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003331 }
3332 }
3333}
3334
Scott Moreauccbf29d2012-02-22 14:21:41 -07003335static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003336zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003337 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003338{
3339 do_zoom(seat, time, 0, axis, value);
3340}
3341
3342static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003343zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003344 void *data)
3345{
3346 do_zoom(seat, time, key, 0, 0);
3347}
3348
3349static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003350terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003351 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003352{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003353 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003354
Daniel Stone325fc2d2012-05-30 16:31:58 +01003355 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003356}
3357
3358static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003359lower_fullscreen_layer(struct desktop_shell *shell);
3360
3361struct alt_tab {
3362 struct desktop_shell *shell;
3363 struct weston_keyboard_grab grab;
3364
3365 struct wl_list *current;
3366
3367 struct wl_list preview_list;
3368};
3369
3370struct alt_tab_preview {
3371 struct alt_tab *alt_tab;
3372
3373 struct weston_view *view;
3374 struct weston_transform transform;
3375
3376 struct wl_listener listener;
3377
3378 struct wl_list link;
3379};
3380
3381static void
3382alt_tab_next(struct alt_tab *alt_tab)
3383{
3384 alt_tab->current = alt_tab->current->next;
3385
3386 /* Make sure we're not pointing to the list header e.g. after
3387 * cycling through the whole list. */
3388 if (alt_tab->current->next == alt_tab->preview_list.next)
3389 alt_tab->current = alt_tab->current->next;
3390}
3391
3392static void
3393alt_tab_destroy(struct alt_tab *alt_tab)
3394{
3395 struct alt_tab_preview *preview, *next;
3396 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3397
3398 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3399 preview = wl_container_of(alt_tab->current, preview, link);
3400
3401 activate(alt_tab->shell, preview->view->surface,
3402 (struct weston_seat *) keyboard->seat);
3403 }
3404
3405 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3406 wl_list_remove(&preview->link);
3407 wl_list_remove(&preview->listener.link);
3408 weston_view_destroy(preview->view);
3409 free(preview);
3410 }
3411
3412 weston_keyboard_end_grab(keyboard);
3413 if (keyboard->input_method_resource)
3414 keyboard->grab = &keyboard->input_method_grab;
3415
3416 free(alt_tab);
3417}
3418
3419static void
3420alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3421{
3422 struct alt_tab_preview *preview =
3423 container_of(listener, struct alt_tab_preview, listener);
3424 struct alt_tab *alt_tab = preview->alt_tab;
3425 int advance = 0;
3426
3427 /* If the preview that we're removing is the currently selected one,
3428 * we want to move to the next one. So we move to ->prev and then
3429 * call _next() after removing the preview. */
3430 if (alt_tab->current == &preview->link) {
3431 alt_tab->current = alt_tab->current->prev;
3432 advance = 1;
3433 }
3434
3435 wl_list_remove(&preview->listener.link);
3436 wl_list_remove(&preview->link);
3437
3438 free(preview);
3439
3440 if (advance)
3441 alt_tab_next(alt_tab);
3442
3443 /* If the last preview goes away, stop the alt-tab */
3444 if (wl_list_empty(alt_tab->current))
3445 alt_tab_destroy(alt_tab);
3446}
3447
3448static void
3449alt_tab_key(struct weston_keyboard_grab *grab,
3450 uint32_t time, uint32_t key, uint32_t state_w)
3451{
3452 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3453 enum wl_keyboard_key_state state = state_w;
3454
3455 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3456 alt_tab_next(alt_tab);
3457}
3458
3459static void
3460alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3461 uint32_t mods_depressed, uint32_t mods_latched,
3462 uint32_t mods_locked, uint32_t group)
3463{
3464 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3465 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3466
3467 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3468 alt_tab_destroy(alt_tab);
3469}
3470
3471static void
3472alt_tab_cancel(struct weston_keyboard_grab *grab)
3473{
3474 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3475
3476 alt_tab_destroy(alt_tab);
3477}
3478
3479static const struct weston_keyboard_grab_interface alt_tab_grab = {
3480 alt_tab_key,
3481 alt_tab_modifier,
3482 alt_tab_cancel,
3483};
3484
3485static int
3486view_for_alt_tab(struct weston_view *view)
3487{
3488 if (!get_shell_surface(view->surface))
3489 return 0;
3490
3491 if (get_shell_surface_type(view->surface) == SHELL_SURFACE_TRANSIENT)
3492 return 0;
3493
3494 if (view != get_default_view(view->surface))
3495 return 0;
3496
3497 return 1;
3498}
3499
3500static void
3501alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3502 void *data)
3503{
3504 struct alt_tab *alt_tab;
3505 struct desktop_shell *shell = data;
3506 struct weston_output *output = get_default_output(shell->compositor);
3507 struct workspace *ws;
3508 struct weston_view *view;
3509 int num_surfaces = 0;
3510 int x, y, side, margin;
3511
3512 wl_list_for_each(view, &shell->compositor->view_list, link) {
3513
3514 if (view_for_alt_tab(view))
3515 num_surfaces++;
3516 }
3517
3518 if (!num_surfaces)
3519 return;
3520
3521 alt_tab = malloc(sizeof *alt_tab);
3522 if (!alt_tab)
3523 return;
3524
3525 alt_tab->shell = shell;
3526 wl_list_init(&alt_tab->preview_list);
3527 alt_tab->current = &alt_tab->preview_list;
3528
3529 restore_all_output_modes(shell->compositor);
3530 lower_fullscreen_layer(alt_tab->shell);
3531
3532 alt_tab->grab.interface = &alt_tab_grab;
3533 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3534 weston_keyboard_set_focus(seat->keyboard, NULL);
3535
3536 ws = get_current_workspace(shell);
3537
3538 /* FIXME: add some visual candy e.g. prelight the selected view
3539 * and/or add a black surrounding background à la gnome-shell */
3540
3541 /* Determine the size for each preview */
3542 side = output->width / num_surfaces;
3543 if (side > 200)
3544 side = 200;
3545 margin = side / 4;
3546 side -= margin;
3547
3548 x = margin;
3549 y = (output->height - side) / 2;
3550
3551 /* Create a view for each surface */
3552 wl_list_for_each(view, &shell->compositor->view_list, link) {
3553 struct alt_tab_preview *preview;
3554 struct weston_view *v;
3555 float scale;
3556
3557 if (!view_for_alt_tab(view))
3558 continue;
3559
3560 preview = malloc(sizeof *preview);
3561 if (!preview) {
3562 alt_tab_destroy(alt_tab);
3563 return;
3564 }
3565
3566 preview->alt_tab = alt_tab;
3567
3568 preview->view = v = weston_view_create(view->surface);
3569 v->output = view->output;
3570 weston_view_restack(v, &ws->layer.view_list);
3571 weston_view_configure(v, x, y, view->geometry.width, view->geometry.height);
3572
3573 preview->listener.notify = alt_tab_handle_surface_destroy;
3574 wl_signal_add(&v->destroy_signal, &preview->listener);
3575
3576 if (view->geometry.width > view->geometry.height)
3577 scale = side / (float) view->geometry.width;
3578 else
3579 scale = side / (float) view->geometry.height;
3580
3581 wl_list_insert(&v->geometry.transformation_list,
3582 &preview->transform.link);
3583 weston_matrix_init(&preview->transform.matrix);
3584 weston_matrix_scale(&preview->transform.matrix,
3585 scale, scale, 1.0f);
3586
3587 weston_view_geometry_dirty(v);
3588 weston_compositor_schedule_repaint(v->surface->compositor);
3589
3590 /* Insert at the end of the list */
3591 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3592
3593 x += side + margin;
3594 }
3595
3596 /* Start at the second preview so a simple <alt>tab changes window.
3597 * We set `current' to the first preview and not the second because
3598 * we're going to receive a key press callback for the initial
3599 * <alt>tab which will make `current' point to the second element. */
3600 alt_tab_next(alt_tab);
3601}
3602
3603static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003604rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3605 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003606{
3607 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003608 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003609 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003610 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003611 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003612
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003613 weston_pointer_move(pointer, x, y);
3614
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003615 if (!shsurf)
3616 return;
3617
Jason Ekstranda7af7042013-10-12 22:38:11 -05003618 cx = 0.5f * shsurf->view->geometry.width;
3619 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003620
Daniel Stone37816df2012-05-16 18:45:18 +01003621 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3622 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003623 r = sqrtf(dx * dx + dy * dy);
3624
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003625 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003626 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003627
3628 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003629 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003630 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003631
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003632 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003633 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003634
3635 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003636 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003637 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003638 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003639 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003640
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003641 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003642 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003643 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003644 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003645 wl_list_init(&shsurf->rotation.transform.link);
3646 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003647 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003648 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003649
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003650 /* We need to adjust the position of the surface
3651 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003652 cposx = shsurf->view->geometry.x + cx;
3653 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003654 dposx = rotate->center.x - cposx;
3655 dposy = rotate->center.y - cposy;
3656 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003657 weston_view_set_position(shsurf->view,
3658 shsurf->view->geometry.x + dposx,
3659 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003660 }
3661
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003662 /* Repaint implies weston_surface_update_transform(), which
3663 * lazily applies the damage due to rotation update.
3664 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003665 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003666}
3667
3668static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003669rotate_grab_button(struct weston_pointer_grab *grab,
3670 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003671{
3672 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003673 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003674 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003675 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003676 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003677
Daniel Stone4dbadb12012-05-30 16:31:51 +01003678 if (pointer->button_count == 0 &&
3679 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003680 if (shsurf)
3681 weston_matrix_multiply(&shsurf->rotation.rotation,
3682 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003683 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003684 free(rotate);
3685 }
3686}
3687
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003688static void
3689rotate_grab_cancel(struct weston_pointer_grab *grab)
3690{
3691 struct rotate_grab *rotate =
3692 container_of(grab, struct rotate_grab, base.grab);
3693
3694 shell_grab_end(&rotate->base);
3695 free(rotate);
3696}
3697
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003698static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003699 noop_grab_focus,
3700 rotate_grab_motion,
3701 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003702 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003703};
3704
3705static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003706surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003707{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003708 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003709 float dx, dy;
3710 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003711
Pekka Paalanen460099f2012-01-20 16:48:25 +02003712 rotate = malloc(sizeof *rotate);
3713 if (!rotate)
3714 return;
3715
Jason Ekstranda7af7042013-10-12 22:38:11 -05003716 weston_view_to_global_float(surface->view,
3717 surface->view->geometry.width * 0.5f,
3718 surface->view->geometry.height * 0.5f,
3719 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003720
Daniel Stone37816df2012-05-16 18:45:18 +01003721 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3722 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003723 r = sqrtf(dx * dx + dy * dy);
3724 if (r > 20.0f) {
3725 struct weston_matrix inverse;
3726
3727 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003728 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003729 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003730
3731 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003732 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003733 } else {
3734 weston_matrix_init(&surface->rotation.rotation);
3735 weston_matrix_init(&rotate->rotation);
3736 }
3737
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003738 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3739 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003740}
3741
3742static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003743rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003744 void *data)
3745{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003746 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003747 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003748 struct shell_surface *surface;
3749
Pekka Paalanen01388e22013-04-25 13:57:44 +03003750 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003751 if (base_surface == NULL)
3752 return;
3753
3754 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003755 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3756 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003757 return;
3758
3759 surface_rotate(surface, seat);
3760}
3761
3762static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003763lower_fullscreen_layer(struct desktop_shell *shell)
3764{
3765 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003766 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003767
3768 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003769 wl_list_for_each_reverse_safe(view, prev,
3770 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003771 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003772 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003773}
3774
3775static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003776activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003777 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003778{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003779 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003780 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003781 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003782 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003783 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003784
Pekka Paalanen01388e22013-04-25 13:57:44 +03003785 main_surface = weston_surface_get_main_surface(es);
3786
Daniel Stone37816df2012-05-16 18:45:18 +01003787 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003788
Jonas Ådahl8538b222012-08-29 22:13:03 +02003789 state = ensure_focus_state(shell, seat);
3790 if (state == NULL)
3791 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003792
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003793 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003794 state->keyboard_focus = es;
3795 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003796 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003797
Pekka Paalanen01388e22013-04-25 13:57:44 +03003798 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003799 case SHELL_SURFACE_FULLSCREEN:
3800 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003801 shell_stack_fullscreen(get_shell_surface(main_surface));
3802 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003803 return;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003804 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003805 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003806 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003807 main_view = get_default_view(main_surface);
3808 if (main_view)
3809 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003810 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003811 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003812
3813 if (shell->focus_animation_type != ANIMATION_NONE)
3814 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Kristian Høgsberg75840622011-09-06 13:48:16 -04003815}
3816
Alex Wu21858432012-04-01 20:13:08 +08003817/* no-op func for checking black surface */
3818static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003819black_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 +08003820{
3821}
3822
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003823static bool
Alex Wu21858432012-04-01 20:13:08 +08003824is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3825{
3826 if (es->configure == black_surface_configure) {
3827 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003828 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003829 return true;
3830 }
3831 return false;
3832}
3833
Kristian Høgsberg75840622011-09-06 13:48:16 -04003834static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003835activate_binding(struct weston_seat *seat,
3836 struct desktop_shell *shell,
3837 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003838{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003839 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003840
Alex Wu9c35e6b2012-03-05 14:13:13 +08003841 if (!focus)
3842 return;
3843
Pekka Paalanen01388e22013-04-25 13:57:44 +03003844 if (is_black_surface(focus, &main_surface))
3845 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003846
Pekka Paalanen01388e22013-04-25 13:57:44 +03003847 main_surface = weston_surface_get_main_surface(focus);
3848 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003849 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003850
Neil Robertsa28c6932013-10-03 16:43:04 +01003851 activate(shell, focus, seat);
3852}
3853
3854static void
3855click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3856 void *data)
3857{
3858 if (seat->pointer->grab != &seat->pointer->default_grab)
3859 return;
3860
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003861 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003862}
3863
3864static void
3865touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3866{
3867 if (seat->touch->grab != &seat->touch->default_grab)
3868 return;
3869
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003870 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003871}
3872
3873static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003874lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003875{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003876 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003877
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003878 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003879 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003880 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003881 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003882
3883 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003884
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003885 /* Hide all surfaces by removing the fullscreen, panel and
3886 * toplevel layers. This way nothing else can show or receive
3887 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003888
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003889 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003890 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003891 if (shell->showing_input_panels)
3892 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003893 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003894 wl_list_insert(&shell->compositor->cursor_layer.link,
3895 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003896
Pekka Paalanen77346a62011-11-30 16:26:35 +02003897 launch_screensaver(shell);
3898
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003899 /* TODO: disable bindings that should not work while locked. */
3900
3901 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003902}
3903
3904static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003905unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003906{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003907 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003908 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003909 return;
3910 }
3911
3912 /* If desktop-shell client has gone away, unlock immediately. */
3913 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003914 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003915 return;
3916 }
3917
3918 if (shell->prepare_event_sent)
3919 return;
3920
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003921 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003922 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003923}
3924
3925static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003926shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003927{
3928 struct desktop_shell *shell = data;
3929
3930 shell->fade.animation = NULL;
3931
3932 switch (shell->fade.type) {
3933 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003934 weston_surface_destroy(shell->fade.view->surface);
3935 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003936 break;
3937 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003938 lock(shell);
3939 break;
3940 }
3941}
3942
Jason Ekstranda7af7042013-10-12 22:38:11 -05003943static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003944shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003945{
3946 struct weston_compositor *compositor = shell->compositor;
3947 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003948 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003949
3950 surface = weston_surface_create(compositor);
3951 if (!surface)
3952 return NULL;
3953
Jason Ekstranda7af7042013-10-12 22:38:11 -05003954 view = weston_view_create(surface);
3955 if (!view) {
3956 weston_surface_destroy(surface);
3957 return NULL;
3958 }
3959
3960 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003961 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003962 wl_list_insert(&compositor->fade_layer.view_list,
3963 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003964 pixman_region32_init(&surface->input);
3965
Jason Ekstranda7af7042013-10-12 22:38:11 -05003966 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003967}
3968
3969static void
3970shell_fade(struct desktop_shell *shell, enum fade_type type)
3971{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003972 float tint;
3973
3974 switch (type) {
3975 case FADE_IN:
3976 tint = 0.0;
3977 break;
3978 case FADE_OUT:
3979 tint = 1.0;
3980 break;
3981 default:
3982 weston_log("shell: invalid fade type\n");
3983 return;
3984 }
3985
3986 shell->fade.type = type;
3987
Jason Ekstranda7af7042013-10-12 22:38:11 -05003988 if (shell->fade.view == NULL) {
3989 shell->fade.view = shell_fade_create_surface(shell);
3990 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003991 return;
3992
Jason Ekstranda7af7042013-10-12 22:38:11 -05003993 shell->fade.view->alpha = 1.0 - tint;
3994 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003995 }
3996
3997 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003998 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003999 else
4000 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004001 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004002 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004003 shell_fade_done, shell);
4004}
4005
4006static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004007do_shell_fade_startup(void *data)
4008{
4009 struct desktop_shell *shell = data;
4010
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004011 if (shell->startup_animation_type == ANIMATION_FADE)
4012 shell_fade(shell, FADE_IN);
4013 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004014 weston_surface_destroy(shell->fade.view->surface);
4015 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004016 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004017}
4018
4019static void
4020shell_fade_startup(struct desktop_shell *shell)
4021{
4022 struct wl_event_loop *loop;
4023
4024 if (!shell->fade.startup_timer)
4025 return;
4026
4027 wl_event_source_remove(shell->fade.startup_timer);
4028 shell->fade.startup_timer = NULL;
4029
4030 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4031 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4032}
4033
4034static int
4035fade_startup_timeout(void *data)
4036{
4037 struct desktop_shell *shell = data;
4038
4039 shell_fade_startup(shell);
4040 return 0;
4041}
4042
4043static void
4044shell_fade_init(struct desktop_shell *shell)
4045{
4046 /* Make compositor output all black, and wait for the desktop-shell
4047 * client to signal it is ready, then fade in. The timer triggers a
4048 * fade-in, in case the desktop-shell client takes too long.
4049 */
4050
4051 struct wl_event_loop *loop;
4052
Jason Ekstranda7af7042013-10-12 22:38:11 -05004053 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004054 weston_log("%s: warning: fade surface already exists\n",
4055 __func__);
4056 return;
4057 }
4058
Jason Ekstranda7af7042013-10-12 22:38:11 -05004059 shell->fade.view = shell_fade_create_surface(shell);
4060 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004061 return;
4062
Jason Ekstranda7af7042013-10-12 22:38:11 -05004063 weston_view_update_transform(shell->fade.view);
4064 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004065
4066 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4067 shell->fade.startup_timer =
4068 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4069 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4070}
4071
4072static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004073idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004074{
4075 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004076 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004077
4078 shell_fade(shell, FADE_OUT);
4079 /* lock() is called from shell_fade_done() */
4080}
4081
4082static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004083wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004084{
4085 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004086 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004087
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004088 unlock(shell);
4089}
4090
4091static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004092show_input_panels(struct wl_listener *listener, void *data)
4093{
4094 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004095 container_of(listener, struct desktop_shell,
4096 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004097 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004098
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004099 shell->text_input.surface = (struct weston_surface*)data;
4100
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004101 if (shell->showing_input_panels)
4102 return;
4103
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004104 shell->showing_input_panels = true;
4105
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004106 if (!shell->locked)
4107 wl_list_insert(&shell->panel_layer.link,
4108 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004109
Jason Ekstranda7af7042013-10-12 22:38:11 -05004110 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004111 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004112 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004113 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004114 wl_list_insert(&shell->input_panel_layer.view_list,
4115 &ipsurf->view->layer_link);
4116 weston_view_geometry_dirty(ipsurf->view);
4117 weston_view_update_transform(ipsurf->view);
4118 weston_surface_damage(ipsurf->surface);
4119 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
4120 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004121 }
4122}
4123
4124static void
4125hide_input_panels(struct wl_listener *listener, void *data)
4126{
4127 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004128 container_of(listener, struct desktop_shell,
4129 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004130 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004131
Jan Arne Petersen61381972013-01-31 15:52:21 +01004132 if (!shell->showing_input_panels)
4133 return;
4134
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004135 shell->showing_input_panels = false;
4136
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004137 if (!shell->locked)
4138 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004139
Jason Ekstranda7af7042013-10-12 22:38:11 -05004140 wl_list_for_each_safe(view, next,
4141 &shell->input_panel_layer.view_list, layer_link)
4142 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004143}
4144
4145static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004146update_input_panels(struct wl_listener *listener, void *data)
4147{
4148 struct desktop_shell *shell =
4149 container_of(listener, struct desktop_shell,
4150 update_input_panel_listener);
4151
4152 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4153}
4154
4155static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004156center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004157{
Giulio Camuffob8366642013-04-25 13:57:46 +03004158 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004159 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004160
Jason Ekstranda7af7042013-10-12 22:38:11 -05004161 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004162
4163 x = output->x + (output->width - width) / 2 - surf_x / 2;
4164 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004165
Jason Ekstranda7af7042013-10-12 22:38:11 -05004166 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004167}
4168
4169static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004170weston_view_set_initial_position(struct weston_view *view,
4171 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004172{
4173 struct weston_compositor *compositor = shell->compositor;
4174 int ix = 0, iy = 0;
4175 int range_x, range_y;
4176 int dx, dy, x, y, panel_height;
4177 struct weston_output *output, *target_output = NULL;
4178 struct weston_seat *seat;
4179
4180 /* As a heuristic place the new window on the same output as the
4181 * pointer. Falling back to the output containing 0, 0.
4182 *
4183 * TODO: Do something clever for touch too?
4184 */
4185 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004186 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004187 ix = wl_fixed_to_int(seat->pointer->x);
4188 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004189 break;
4190 }
4191 }
4192
4193 wl_list_for_each(output, &compositor->output_list, link) {
4194 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4195 target_output = output;
4196 break;
4197 }
4198 }
4199
4200 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004201 weston_view_set_position(view, 10 + random() % 400,
4202 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004203 return;
4204 }
4205
4206 /* Valid range within output where the surface will still be onscreen.
4207 * If this is negative it means that the surface is bigger than
4208 * output.
4209 */
4210 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004211 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004212 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05004213 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004214
Rob Bradford4cb88c72012-08-13 15:18:44 +01004215 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004216 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004217 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004218 dx = 0;
4219
4220 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004221 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004222 else
4223 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004224
4225 x = target_output->x + dx;
4226 y = target_output->y + dy;
4227
Jason Ekstranda7af7042013-10-12 22:38:11 -05004228 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004229}
4230
4231static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004232map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004233 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004234{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004235 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004236 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01004237 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004238 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08004239 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004240 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004241
Jason Ekstranda7af7042013-10-12 22:38:11 -05004242 shsurf->view->geometry.width = width;
4243 shsurf->view->geometry.height = height;
4244 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004245
4246 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004247 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02004248 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004249 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004250 break;
Alex Wu4539b082012-03-01 12:57:46 +08004251 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004252 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08004253 shell_map_fullscreen(shsurf);
4254 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004255 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004256 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004257 panel_height = get_output_panel_height(shell, shsurf->output);
4258 surface_subsurfaces_boundingbox(shsurf->surface,
4259 &surf_x, &surf_y, NULL, NULL);
4260 weston_view_set_position(shsurf->view,
4261 shsurf->output->x - surf_x,
4262 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08004263 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004264 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004265 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004266 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004267 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004268 weston_view_set_position(shsurf->view,
4269 shsurf->view->geometry.x + sx,
4270 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004271 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004272 default:
4273 ;
4274 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004275
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02004276 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004277 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004278 case SHELL_SURFACE_POPUP:
4279 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004280 /* TODO: Handle a parent with multiple views */
4281 parent = get_default_view(shsurf->parent);
4282 if (parent) {
4283 wl_list_remove(&shsurf->view->layer_link);
4284 wl_list_insert(parent->layer_link.prev,
4285 &shsurf->view->layer_link);
4286 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004287 break;
Alex Wu4539b082012-03-01 12:57:46 +08004288 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02004289 case SHELL_SURFACE_NONE:
4290 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004291 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004292 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004293 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004294 wl_list_remove(&shsurf->view->layer_link);
4295 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004296 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004297 }
4298
Jason Ekstranda7af7042013-10-12 22:38:11 -05004299 if (shsurf->type != SHELL_SURFACE_NONE) {
4300 weston_view_update_transform(shsurf->view);
4301 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4302 shsurf->surface->output = shsurf->output;
4303 shsurf->view->output = shsurf->output;
4304 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004305 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004306
Jason Ekstranda7af7042013-10-12 22:38:11 -05004307 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004308 /* XXX: xwayland's using the same fields for transient type */
4309 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004310 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004311 if (shsurf->transient.flags ==
4312 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4313 break;
4314 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004315 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004316 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004317 if (!shell->locked) {
4318 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004319 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004320 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004321 break;
4322 default:
4323 break;
4324 }
4325
Jason Ekstranda7af7042013-10-12 22:38:11 -05004326 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004327 {
4328 switch (shell->win_animation_type) {
4329 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004330 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004331 break;
4332 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004333 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004334 break;
4335 default:
4336 break;
4337 }
4338 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004339}
4340
4341static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004342configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004343 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004344{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004345 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4346 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004347 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03004348 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004349
Pekka Paalanen77346a62011-11-30 16:26:35 +02004350 shsurf = get_shell_surface(surface);
4351 if (shsurf)
4352 surface_type = shsurf->type;
4353
Jason Ekstranda7af7042013-10-12 22:38:11 -05004354 /* TODO:
4355 * This should probably be changed to be more shell_surface
4356 * dependent
4357 */
4358 wl_list_for_each(view, &surface->views, surface_link)
4359 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004360
4361 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004362 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08004363 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004364 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004365 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004366 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004367 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004368 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4369 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004370 shsurf->view->geometry.x = shsurf->output->x - surf_x;
4371 shsurf->view->geometry.y = shsurf->output->y +
4372 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08004373 break;
Alex Wu4539b082012-03-01 12:57:46 +08004374 case SHELL_SURFACE_TOPLEVEL:
4375 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004376 default:
4377 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004378 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004379
Alex Wu4539b082012-03-01 12:57:46 +08004380 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004381 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004382 wl_list_for_each(view, &surface->views, surface_link)
4383 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004384
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004385 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004386 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004387 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004388}
4389
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004390static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004391shell_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 +03004392{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004393 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004394 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004395
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004396 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004397
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004398 if (!weston_surface_is_mapped(es) &&
4399 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004400 remove_popup_grab(shsurf);
4401 }
4402
Giulio Camuffo184df502013-02-21 11:29:21 +01004403 if (width == 0)
4404 return;
4405
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004406 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004407 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004408 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004409 type_changed = 1;
4410 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004411
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004412 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004413 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004414 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05004415 shsurf->view->geometry.width != width ||
4416 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004417 float from_x, from_y;
4418 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004419
Jason Ekstranda7af7042013-10-12 22:38:11 -05004420 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4421 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004422 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004423 shsurf->view->geometry.x + to_x - from_x,
4424 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004425 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004426 }
4427}
4428
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004429static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004430
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004431static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004432desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004433{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004434 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004435 struct desktop_shell *shell =
4436 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004437
4438 shell->child.process.pid = 0;
4439 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004440
4441 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4442 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004443 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004444 shell->child.deathstamp = time;
4445 shell->child.deathcount = 0;
4446 }
4447
4448 shell->child.deathcount++;
4449 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004450 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004451 return;
4452 }
4453
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004454 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004455 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004456 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004457}
4458
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004459static void
4460launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004461{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004462 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004463
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004464 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004465 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004466 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004467 desktop_shell_sigchld);
4468
4469 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004470 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004471}
4472
4473static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004474bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4475{
Tiago Vignattibe143262012-04-16 17:31:41 +03004476 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004477 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004478
Jason Ekstranda85118c2013-06-27 20:17:02 -05004479 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4480 if (resource)
4481 wl_resource_set_implementation(resource, &shell_implementation,
4482 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004483}
4484
Kristian Høgsberg75840622011-09-06 13:48:16 -04004485static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004486unbind_desktop_shell(struct wl_resource *resource)
4487{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004488 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004489
4490 if (shell->locked)
4491 resume_desktop(shell);
4492
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004493 shell->child.desktop_shell = NULL;
4494 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004495}
4496
4497static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004498bind_desktop_shell(struct wl_client *client,
4499 void *data, uint32_t version, uint32_t id)
4500{
Tiago Vignattibe143262012-04-16 17:31:41 +03004501 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004502 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004503
Jason Ekstranda85118c2013-06-27 20:17:02 -05004504 resource = wl_resource_create(client, &desktop_shell_interface,
4505 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004506
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004507 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004508 wl_resource_set_implementation(resource,
4509 &desktop_shell_implementation,
4510 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004511 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004512
4513 if (version < 2)
4514 shell_fade_startup(shell);
4515
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004516 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004517 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004518
4519 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4520 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004521 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004522}
4523
Pekka Paalanen6e168112011-11-24 11:34:05 +02004524static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004525screensaver_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 -04004526{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004527 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004528 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004529
Giulio Camuffo184df502013-02-21 11:29:21 +01004530 if (width == 0)
4531 return;
4532
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004533 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004534 if (!shell->locked)
4535 return;
4536
Jason Ekstranda7af7042013-10-12 22:38:11 -05004537 view = container_of(surface->views.next, struct weston_view, surface_link);
4538 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004539
Jason Ekstranda7af7042013-10-12 22:38:11 -05004540 if (wl_list_empty(&view->layer_link)) {
4541 wl_list_insert(shell->lock_layer.view_list.prev,
4542 &view->layer_link);
4543 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004544 wl_event_source_timer_update(shell->screensaver.timer,
4545 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004546 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004547 }
4548}
4549
4550static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004551screensaver_set_surface(struct wl_client *client,
4552 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004553 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004554 struct wl_resource *output_resource)
4555{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004556 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004557 struct weston_surface *surface =
4558 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004559 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004560 struct weston_view *view, *next;
4561
4562 /* Make sure we only have one view */
4563 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4564 weston_view_destroy(view);
4565 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004566
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004567 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004568 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004569 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004570}
4571
4572static const struct screensaver_interface screensaver_implementation = {
4573 screensaver_set_surface
4574};
4575
4576static void
4577unbind_screensaver(struct wl_resource *resource)
4578{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004579 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004580
Pekka Paalanen77346a62011-11-30 16:26:35 +02004581 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004582}
4583
4584static void
4585bind_screensaver(struct wl_client *client,
4586 void *data, uint32_t version, uint32_t id)
4587{
Tiago Vignattibe143262012-04-16 17:31:41 +03004588 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004589 struct wl_resource *resource;
4590
Jason Ekstranda85118c2013-06-27 20:17:02 -05004591 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004592
Pekka Paalanen77346a62011-11-30 16:26:35 +02004593 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004594 wl_resource_set_implementation(resource,
4595 &screensaver_implementation,
4596 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004597 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004598 return;
4599 }
4600
4601 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4602 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004603 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004604}
4605
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004606static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004607input_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 -04004608{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004609 struct input_panel_surface *ip_surface = surface->configure_private;
4610 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004611 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004612 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004613
Giulio Camuffo184df502013-02-21 11:29:21 +01004614 if (width == 0)
4615 return;
4616
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004617 if (!weston_surface_is_mapped(surface)) {
4618 if (!shell->showing_input_panels)
4619 return;
4620
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004621 show_surface = 1;
4622 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004623
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004624 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004625
4626 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004627 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4628 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004629 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004630 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4631 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004632 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004633
Jason Ekstranda7af7042013-10-12 22:38:11 -05004634 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004635
4636 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004637 wl_list_insert(&shell->input_panel_layer.view_list,
4638 &ip_surface->view->layer_link);
4639 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004640 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004641 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004642 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004643}
4644
4645static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004646destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004647{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004648 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4649
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004650 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004651 wl_list_remove(&input_panel_surface->link);
4652
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004653 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004654 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004655
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004656 free(input_panel_surface);
4657}
4658
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004659static struct input_panel_surface *
4660get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004661{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004662 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004663 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004664 } else {
4665 return NULL;
4666 }
4667}
4668
4669static void
4670input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4671{
4672 struct input_panel_surface *ipsurface = container_of(listener,
4673 struct input_panel_surface,
4674 surface_destroy_listener);
4675
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004676 if (ipsurface->resource) {
4677 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004678 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004679 destroy_input_panel_surface(ipsurface);
4680 }
4681}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004682
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004683static struct input_panel_surface *
4684create_input_panel_surface(struct desktop_shell *shell,
4685 struct weston_surface *surface)
4686{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004687 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004688
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004689 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4690 if (!input_panel_surface)
4691 return NULL;
4692
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004693 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004694 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004695
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004696 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004697
4698 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004699 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004700
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004701 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004702 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004703 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004704 &input_panel_surface->surface_destroy_listener);
4705
4706 wl_list_init(&input_panel_surface->link);
4707
4708 return input_panel_surface;
4709}
4710
4711static void
4712input_panel_surface_set_toplevel(struct wl_client *client,
4713 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004714 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004715 uint32_t position)
4716{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004717 struct input_panel_surface *input_panel_surface =
4718 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004719 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004720
4721 wl_list_insert(&shell->input_panel.surfaces,
4722 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004723
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004724 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004725 input_panel_surface->panel = 0;
4726}
4727
4728static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004729input_panel_surface_set_overlay_panel(struct wl_client *client,
4730 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004731{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004732 struct input_panel_surface *input_panel_surface =
4733 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004734 struct desktop_shell *shell = input_panel_surface->shell;
4735
4736 wl_list_insert(&shell->input_panel.surfaces,
4737 &input_panel_surface->link);
4738
4739 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004740}
4741
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004742static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004743 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004744 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004745};
4746
4747static void
4748destroy_input_panel_surface_resource(struct wl_resource *resource)
4749{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004750 struct input_panel_surface *ipsurf =
4751 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004752
4753 destroy_input_panel_surface(ipsurf);
4754}
4755
4756static void
4757input_panel_get_input_panel_surface(struct wl_client *client,
4758 struct wl_resource *resource,
4759 uint32_t id,
4760 struct wl_resource *surface_resource)
4761{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004762 struct weston_surface *surface =
4763 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004764 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004765 struct input_panel_surface *ipsurf;
4766
4767 if (get_input_panel_surface(surface)) {
4768 wl_resource_post_error(surface_resource,
4769 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004770 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004771 return;
4772 }
4773
4774 ipsurf = create_input_panel_surface(shell, surface);
4775 if (!ipsurf) {
4776 wl_resource_post_error(surface_resource,
4777 WL_DISPLAY_ERROR_INVALID_OBJECT,
4778 "surface->configure already set");
4779 return;
4780 }
4781
Jason Ekstranda85118c2013-06-27 20:17:02 -05004782 ipsurf->resource =
4783 wl_resource_create(client,
4784 &wl_input_panel_surface_interface, 1, id);
4785 wl_resource_set_implementation(ipsurf->resource,
4786 &input_panel_surface_implementation,
4787 ipsurf,
4788 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004789}
4790
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004791static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004792 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004793};
4794
4795static void
4796unbind_input_panel(struct wl_resource *resource)
4797{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004798 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004799
4800 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004801}
4802
4803static void
4804bind_input_panel(struct wl_client *client,
4805 void *data, uint32_t version, uint32_t id)
4806{
4807 struct desktop_shell *shell = data;
4808 struct wl_resource *resource;
4809
Jason Ekstranda85118c2013-06-27 20:17:02 -05004810 resource = wl_resource_create(client,
4811 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004812
4813 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004814 wl_resource_set_implementation(resource,
4815 &input_panel_implementation,
4816 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004817 shell->input_panel.binding = resource;
4818 return;
4819 }
4820
4821 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4822 "interface object already bound");
4823 wl_resource_destroy(resource);
4824}
4825
Kristian Høgsberg07045392012-02-19 18:52:44 -05004826struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004827 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004828 struct weston_surface *current;
4829 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004830 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004831};
4832
4833static void
4834switcher_next(struct switcher *switcher)
4835{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004836 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004837 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004838 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004839 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004840
Jason Ekstranda7af7042013-10-12 22:38:11 -05004841 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4842 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004843 case SHELL_SURFACE_TOPLEVEL:
4844 case SHELL_SURFACE_FULLSCREEN:
4845 case SHELL_SURFACE_MAXIMIZED:
4846 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004847 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004848 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004849 next = view->surface;
4850 prev = view->surface;
4851 view->alpha = 0.25;
4852 weston_view_geometry_dirty(view);
4853 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004854 break;
4855 default:
4856 break;
4857 }
Alex Wu1659daa2012-04-01 20:13:09 +08004858
Jason Ekstranda7af7042013-10-12 22:38:11 -05004859 if (is_black_surface(view->surface, NULL)) {
4860 view->alpha = 0.25;
4861 weston_view_geometry_dirty(view);
4862 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004863 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004864 }
4865
4866 if (next == NULL)
4867 next = first;
4868
Alex Wu07b26062012-03-12 16:06:01 +08004869 if (next == NULL)
4870 return;
4871
Kristian Høgsberg07045392012-02-19 18:52:44 -05004872 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004873 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004874
4875 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004876 wl_list_for_each(view, &next->views, surface_link)
4877 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004878
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004879 shsurf = get_shell_surface(switcher->current);
4880 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004881 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004882}
4883
4884static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004885switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004886{
4887 struct switcher *switcher =
4888 container_of(listener, struct switcher, listener);
4889
4890 switcher_next(switcher);
4891}
4892
4893static void
Daniel Stone351eb612012-05-31 15:27:47 -04004894switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004895{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004896 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004897 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004898 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004899
Jason Ekstranda7af7042013-10-12 22:38:11 -05004900 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004901 if (is_focus_view(view))
4902 continue;
4903
Jason Ekstranda7af7042013-10-12 22:38:11 -05004904 view->alpha = 1.0;
4905 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004906 }
4907
Alex Wu07b26062012-03-12 16:06:01 +08004908 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004909 activate(switcher->shell, switcher->current,
4910 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004911 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004912 weston_keyboard_end_grab(keyboard);
4913 if (keyboard->input_method_resource)
4914 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004915 free(switcher);
4916}
4917
4918static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004919switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004920 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004921{
4922 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004923 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004924
Daniel Stonec9785ea2012-05-30 16:31:52 +01004925 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004926 switcher_next(switcher);
4927}
4928
4929static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004930switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004931 uint32_t mods_depressed, uint32_t mods_latched,
4932 uint32_t mods_locked, uint32_t group)
4933{
4934 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004935 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004936
Daniel Stone351eb612012-05-31 15:27:47 -04004937 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4938 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004939}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004940
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004941static void
4942switcher_cancel(struct weston_keyboard_grab *grab)
4943{
4944 struct switcher *switcher = container_of(grab, struct switcher, grab);
4945
4946 switcher_destroy(switcher);
4947}
4948
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004949static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004950 switcher_key,
4951 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004952 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004953};
4954
4955static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004956switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004957 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004958{
Tiago Vignattibe143262012-04-16 17:31:41 +03004959 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004960 struct switcher *switcher;
4961
4962 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004963 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004964 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004965 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004966 wl_list_init(&switcher->listener.link);
4967
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004968 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004969 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004970 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004971 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4972 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004973 switcher_next(switcher);
4974}
4975
Daniel Stonedf8133b2013-11-19 11:37:14 +01004976struct exposay_surface {
4977 struct desktop_shell *shell;
4978 struct weston_surface *surface;
4979 struct weston_view *view;
4980 struct wl_list link;
4981
4982 int x;
4983 int y;
4984 int width;
4985 int height;
4986 double scale;
4987
4988 int row;
4989 int column;
4990
4991 /* The animations only apply a transformation for their own lifetime,
4992 * and don't have an option to indefinitely maintain the
4993 * transformation in a steady state - so, we apply our own once the
4994 * animation has finished. */
4995 struct weston_transform transform;
4996};
4997
4998static void exposay_set_state(struct desktop_shell *shell,
4999 enum exposay_target_state state,
5000 struct weston_seat *seat);
5001static void exposay_check_state(struct desktop_shell *shell);
5002
5003static void
5004exposay_in_flight_inc(struct desktop_shell *shell)
5005{
5006 shell->exposay.in_flight++;
5007}
5008
5009static void
5010exposay_in_flight_dec(struct desktop_shell *shell)
5011{
5012 if (--shell->exposay.in_flight > 0)
5013 return;
5014
5015 exposay_check_state(shell);
5016}
5017
5018static void
5019exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5020{
5021 struct exposay_surface *esurface = data;
5022
5023 wl_list_insert(&esurface->view->geometry.transformation_list,
5024 &esurface->transform.link);
5025 weston_matrix_init(&esurface->transform.matrix);
5026 weston_matrix_scale(&esurface->transform.matrix,
5027 esurface->scale, esurface->scale, 1.0f);
5028 weston_matrix_translate(&esurface->transform.matrix,
5029 esurface->x - esurface->view->geometry.x,
5030 esurface->y - esurface->view->geometry.y,
5031 0);
5032
5033 weston_view_geometry_dirty(esurface->view);
5034 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5035
5036 exposay_in_flight_dec(esurface->shell);
5037}
5038
5039static void
5040exposay_animate_in(struct exposay_surface *esurface)
5041{
5042 exposay_in_flight_inc(esurface->shell);
5043
5044 weston_move_scale_run(esurface->view,
5045 esurface->x - esurface->view->geometry.x,
5046 esurface->y - esurface->view->geometry.y,
5047 1.0, esurface->scale, 0,
5048 exposay_animate_in_done, esurface);
5049}
5050
5051static void
5052exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5053{
5054 struct exposay_surface *esurface = data;
5055 struct desktop_shell *shell = esurface->shell;
5056
5057 wl_list_remove(&esurface->link);
5058 free(esurface);
5059
5060 exposay_in_flight_dec(shell);
5061}
5062
5063static void
5064exposay_animate_out(struct exposay_surface *esurface)
5065{
5066 exposay_in_flight_inc(esurface->shell);
5067
5068 /* Remove the static transformation set up by
5069 * exposay_transform_in_done(). */
5070 wl_list_remove(&esurface->transform.link);
5071 weston_view_geometry_dirty(esurface->view);
5072
5073 weston_move_scale_run(esurface->view,
5074 esurface->x - esurface->view->geometry.x,
5075 esurface->y - esurface->view->geometry.y,
5076 1.0, esurface->scale, 1,
5077 exposay_animate_out_done, esurface);
5078}
5079
5080static void
5081exposay_highlight_surface(struct desktop_shell *shell,
5082 struct exposay_surface *esurface)
5083{
5084 struct weston_view *view = NULL;
5085
5086 if (esurface) {
5087 shell->exposay.row_current = esurface->row;
5088 shell->exposay.column_current = esurface->column;
5089 view = esurface->view;
5090 }
5091
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005092 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005093 shell->exposay.focus_current = view;
5094}
5095
5096static int
5097exposay_is_animating(struct desktop_shell *shell)
5098{
5099 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5100 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5101 return 0;
5102
5103 return (shell->exposay.in_flight > 0);
5104}
5105
5106static void
5107exposay_pick(struct desktop_shell *shell, int x, int y)
5108{
5109 struct exposay_surface *esurface;
5110
5111 if (exposay_is_animating(shell))
5112 return;
5113
5114 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5115 if (x < esurface->x || x > esurface->x + esurface->width)
5116 continue;
5117 if (y < esurface->y || y > esurface->y + esurface->height)
5118 continue;
5119
5120 exposay_highlight_surface(shell, esurface);
5121 return;
5122 }
5123}
5124
5125/* Pretty lame layout for now; just tries to make a square. Should take
5126 * aspect ratio into account really. Also needs to be notified of surface
5127 * addition and removal and adjust layout/animate accordingly. */
5128static enum exposay_layout_state
5129exposay_layout(struct desktop_shell *shell)
5130{
5131 struct workspace *workspace = shell->exposay.workspace;
5132 struct weston_compositor *compositor = shell->compositor;
5133 struct weston_output *output = get_default_output(compositor);
5134 struct weston_view *view;
5135 struct exposay_surface *esurface;
5136 int w, h;
5137 int i;
5138 int last_row_removed = 0;
5139
5140 wl_list_init(&shell->exposay.surface_list);
5141
5142 shell->exposay.num_surfaces = 0;
5143 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5144 if (!get_shell_surface(view->surface))
5145 continue;
5146 shell->exposay.num_surfaces++;
5147 }
5148
5149 if (shell->exposay.num_surfaces == 0) {
5150 shell->exposay.grid_size = 0;
5151 shell->exposay.hpadding_outer = 0;
5152 shell->exposay.vpadding_outer = 0;
5153 shell->exposay.padding_inner = 0;
5154 shell->exposay.surface_size = 0;
5155 return EXPOSAY_LAYOUT_OVERVIEW;
5156 }
5157
5158 /* Lay the grid out as square as possible, losing surfaces from the
5159 * bottom row if required. Start with fixed padding of a 10% margin
5160 * around the outside and 80px internal padding between surfaces, and
5161 * maximise the area made available to surfaces after this, but only
5162 * to a maximum of 1/3rd the total output size.
5163 *
5164 * If we can't make a square grid, add one extra row at the bottom
5165 * which will have a smaller number of columns.
5166 *
5167 * XXX: Surely there has to be a better way to express this maths,
5168 * right?!
5169 */
5170 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5171 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5172 shell->exposay.grid_size++;
5173 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5174
5175 shell->exposay.hpadding_outer = (output->width / 10);
5176 shell->exposay.vpadding_outer = (output->height / 10);
5177 shell->exposay.padding_inner = 80;
5178
5179 w = output->width - (shell->exposay.hpadding_outer * 2);
5180 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5181 w /= shell->exposay.grid_size;
5182
5183 h = output->height - (shell->exposay.vpadding_outer * 2);
5184 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5185 h /= shell->exposay.grid_size;
5186
5187 shell->exposay.surface_size = (w < h) ? w : h;
5188 if (shell->exposay.surface_size > (output->width / 2))
5189 shell->exposay.surface_size = output->width / 2;
5190 if (shell->exposay.surface_size > (output->height / 2))
5191 shell->exposay.surface_size = output->height / 2;
5192
5193 i = 0;
5194 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5195 int pad;
5196
5197 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5198
5199 if (!get_shell_surface(view->surface))
5200 continue;
5201
5202 esurface = malloc(sizeof(*esurface));
5203 if (!esurface) {
5204 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5205 shell->exposay.seat);
5206 break;
5207 }
5208
5209 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5210 esurface->shell = shell;
5211 esurface->view = view;
5212
5213 esurface->row = i / shell->exposay.grid_size;
5214 esurface->column = i % shell->exposay.grid_size;
5215
5216 esurface->x = shell->exposay.hpadding_outer;
5217 esurface->x += pad * esurface->column;
5218 esurface->y = shell->exposay.vpadding_outer;
5219 esurface->y += pad * esurface->row;
5220
5221 if (esurface->row == shell->exposay.grid_size - 1)
5222 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5223
5224 if (view->geometry.width > view->geometry.height)
5225 esurface->scale = shell->exposay.surface_size / (float) view->geometry.width;
5226 else
5227 esurface->scale = shell->exposay.surface_size / (float) view->geometry.height;
5228 esurface->width = view->geometry.width * esurface->scale;
5229 esurface->height = view->geometry.height * esurface->scale;
5230
5231 if (shell->exposay.focus_current == esurface->view)
5232 exposay_highlight_surface(shell, esurface);
5233
5234 exposay_animate_in(esurface);
5235
5236 i++;
5237 }
5238
5239 weston_compositor_schedule_repaint(shell->compositor);
5240
5241 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5242}
5243
5244static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005245exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5246 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005247{
5248 struct desktop_shell *shell =
5249 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5250
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005251 weston_pointer_move(grab->pointer, x, y);
5252
Daniel Stonedf8133b2013-11-19 11:37:14 +01005253 exposay_pick(shell,
5254 wl_fixed_to_int(grab->pointer->x),
5255 wl_fixed_to_int(grab->pointer->y));
5256}
5257
5258static void
5259exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5260 uint32_t state_w)
5261{
5262 struct desktop_shell *shell =
5263 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5264 struct weston_seat *seat = grab->pointer->seat;
5265 enum wl_pointer_button_state state = state_w;
5266
5267 if (button != BTN_LEFT)
5268 return;
5269
5270 /* Store the surface we clicked on, and don't do anything if we end up
5271 * releasing on a different surface. */
5272 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5273 shell->exposay.clicked = shell->exposay.focus_current;
5274 return;
5275 }
5276
5277 if (shell->exposay.focus_current == shell->exposay.clicked)
5278 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5279 else
5280 shell->exposay.clicked = NULL;
5281}
5282
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005283static void
5284exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
5285{
5286 struct desktop_shell *shell =
5287 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5288
5289 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5290}
5291
Daniel Stonedf8133b2013-11-19 11:37:14 +01005292static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5293 noop_grab_focus,
5294 exposay_motion,
5295 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005296 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005297};
5298
5299static int
5300exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5301{
5302 struct exposay_surface *esurface;
5303
5304 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5305 if (esurface->row != row || esurface->column != column)
5306 continue;
5307
5308 exposay_highlight_surface(shell, esurface);
5309 return 1;
5310 }
5311
5312 return 0;
5313}
5314
5315static void
5316exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5317 uint32_t state_w)
5318{
5319 struct weston_seat *seat = grab->keyboard->seat;
5320 struct desktop_shell *shell =
5321 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5322 enum wl_keyboard_key_state state = state_w;
5323
5324 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5325 return;
5326
5327 switch (key) {
5328 case KEY_ESC:
5329 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5330 break;
5331 case KEY_ENTER:
5332 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5333 break;
5334 case KEY_UP:
5335 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5336 shell->exposay.column_current);
5337 break;
5338 case KEY_DOWN:
5339 /* Special case for trying to move to the bottom row when it
5340 * has fewer items than all the others. */
5341 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5342 shell->exposay.column_current) &&
5343 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5344 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5345 (shell->exposay.num_surfaces %
5346 shell->exposay.grid_size) - 1);
5347 }
5348 break;
5349 case KEY_LEFT:
5350 exposay_maybe_move(shell, shell->exposay.row_current,
5351 shell->exposay.column_current - 1);
5352 break;
5353 case KEY_RIGHT:
5354 exposay_maybe_move(shell, shell->exposay.row_current,
5355 shell->exposay.column_current + 1);
5356 break;
5357 case KEY_TAB:
5358 /* Try to move right, then down (and to the leftmost column),
5359 * then if all else fails, to the top left. */
5360 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5361 shell->exposay.column_current + 1) &&
5362 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5363 exposay_maybe_move(shell, 0, 0);
5364 break;
5365 default:
5366 break;
5367 }
5368}
5369
5370static void
5371exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5372 uint32_t mods_depressed, uint32_t mods_latched,
5373 uint32_t mods_locked, uint32_t group)
5374{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005375 struct desktop_shell *shell =
5376 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5377 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5378
5379 /* We want to know when mod has been pressed and released.
5380 * FIXME: There is a problem here: if mod is pressed, then a key
5381 * is pressed and released, then mod is released, we will treat that
5382 * as if only mod had been pressed and released. */
5383 if (seat->modifier_state) {
5384 if (seat->modifier_state == shell->binding_modifier) {
5385 shell->exposay.mod_pressed = true;
5386 } else {
5387 shell->exposay.mod_invalid = true;
5388 }
5389 } else {
5390 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
5391 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5392
5393 shell->exposay.mod_invalid = false;
5394 shell->exposay.mod_pressed = false;
5395 }
5396
5397 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005398}
5399
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005400static void
5401exposay_cancel(struct weston_keyboard_grab *grab)
5402{
5403 struct desktop_shell *shell =
5404 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5405
5406 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5407}
5408
Daniel Stonedf8133b2013-11-19 11:37:14 +01005409static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5410 exposay_key,
5411 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005412 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005413};
5414
5415/**
5416 * Called when the transition from overview -> inactive has completed.
5417 */
5418static enum exposay_layout_state
5419exposay_set_inactive(struct desktop_shell *shell)
5420{
5421 struct weston_seat *seat = shell->exposay.seat;
5422
5423 weston_keyboard_end_grab(seat->keyboard);
5424 weston_pointer_end_grab(seat->pointer);
5425 if (seat->keyboard->input_method_resource)
5426 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5427
5428 return EXPOSAY_LAYOUT_INACTIVE;
5429}
5430
5431/**
5432 * Begins the transition from overview to inactive. */
5433static enum exposay_layout_state
5434exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5435{
5436 struct exposay_surface *esurface;
5437
5438 /* Call activate() before we start the animations to avoid
5439 * animating back the old state and then immediately transitioning
5440 * to the new. */
5441 if (switch_focus && shell->exposay.focus_current)
5442 activate(shell, shell->exposay.focus_current->surface,
5443 shell->exposay.seat);
5444 else if (shell->exposay.focus_prev)
5445 activate(shell, shell->exposay.focus_prev->surface,
5446 shell->exposay.seat);
5447
5448 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5449 exposay_animate_out(esurface);
5450 weston_compositor_schedule_repaint(shell->compositor);
5451
5452 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5453}
5454
5455static enum exposay_layout_state
5456exposay_transition_active(struct desktop_shell *shell)
5457{
5458 struct weston_seat *seat = shell->exposay.seat;
5459
5460 shell->exposay.workspace = get_current_workspace(shell);
5461 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5462 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5463 shell->exposay.clicked = NULL;
5464 wl_list_init(&shell->exposay.surface_list);
5465
5466 lower_fullscreen_layer(shell);
5467 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5468 weston_keyboard_start_grab(seat->keyboard,
5469 &shell->exposay.grab_kbd);
5470 weston_keyboard_set_focus(seat->keyboard, NULL);
5471
5472 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5473 weston_pointer_start_grab(seat->pointer,
5474 &shell->exposay.grab_ptr);
5475 weston_pointer_set_focus(seat->pointer, NULL,
5476 seat->pointer->x, seat->pointer->y);
5477
5478 return exposay_layout(shell);
5479}
5480
5481static void
5482exposay_check_state(struct desktop_shell *shell)
5483{
5484 enum exposay_layout_state state_new = shell->exposay.state_cur;
5485 int do_switch = 0;
5486
5487 /* Don't do anything whilst animations are running, just store up
5488 * target state changes and only act on them when the animations have
5489 * completed. */
5490 if (exposay_is_animating(shell))
5491 return;
5492
5493 switch (shell->exposay.state_target) {
5494 case EXPOSAY_TARGET_OVERVIEW:
5495 switch (shell->exposay.state_cur) {
5496 case EXPOSAY_LAYOUT_OVERVIEW:
5497 goto out;
5498 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5499 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5500 break;
5501 default:
5502 state_new = exposay_transition_active(shell);
5503 break;
5504 }
5505 break;
5506
5507 case EXPOSAY_TARGET_SWITCH:
5508 do_switch = 1; /* fallthrough */
5509 case EXPOSAY_TARGET_CANCEL:
5510 switch (shell->exposay.state_cur) {
5511 case EXPOSAY_LAYOUT_INACTIVE:
5512 goto out;
5513 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5514 state_new = exposay_set_inactive(shell);
5515 break;
5516 default:
5517 state_new = exposay_transition_inactive(shell, do_switch);
5518 break;
5519 }
5520
5521 break;
5522 }
5523
5524out:
5525 shell->exposay.state_cur = state_new;
5526}
5527
5528static void
5529exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5530 struct weston_seat *seat)
5531{
5532 shell->exposay.state_target = state;
5533 shell->exposay.seat = seat;
5534 exposay_check_state(shell);
5535}
5536
5537static void
5538exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5539 void *data)
5540{
5541 struct desktop_shell *shell = data;
5542
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005543 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005544}
5545
Pekka Paalanen3c647232011-12-22 13:43:43 +02005546static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005547backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005548 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005549{
5550 struct weston_compositor *compositor = data;
5551 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005552 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005553
5554 /* TODO: we're limiting to simple use cases, where we assume just
5555 * control on the primary display. We'd have to extend later if we
5556 * ever get support for setting backlights on random desktop LCD
5557 * panels though */
5558 output = get_default_output(compositor);
5559 if (!output)
5560 return;
5561
5562 if (!output->set_backlight)
5563 return;
5564
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005565 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5566 backlight_new = output->backlight_current - 25;
5567 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5568 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005569
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005570 if (backlight_new < 5)
5571 backlight_new = 5;
5572 if (backlight_new > 255)
5573 backlight_new = 255;
5574
5575 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005576 output->set_backlight(output, output->backlight_current);
5577}
5578
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005579struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005580 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005581 struct weston_seat *seat;
5582 uint32_t key[2];
5583 int key_released[2];
5584};
5585
5586static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005587debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005588 uint32_t key, uint32_t state)
5589{
5590 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005591 struct weston_compositor *ec = db->seat->compositor;
5592 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005593 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005594 uint32_t serial;
5595 int send = 0, terminate = 0;
5596 int check_binding = 1;
5597 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005598 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005599
5600 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5601 /* Do not run bindings on key releases */
5602 check_binding = 0;
5603
5604 for (i = 0; i < 2; i++)
5605 if (key == db->key[i])
5606 db->key_released[i] = 1;
5607
5608 if (db->key_released[0] && db->key_released[1]) {
5609 /* All key releases been swalled so end the grab */
5610 terminate = 1;
5611 } else if (key != db->key[0] && key != db->key[1]) {
5612 /* Should not swallow release of other keys */
5613 send = 1;
5614 }
5615 } else if (key == db->key[0] && !db->key_released[0]) {
5616 /* Do not check bindings for the first press of the binding
5617 * key. This allows it to be used as a debug shortcut.
5618 * We still need to swallow this event. */
5619 check_binding = 0;
5620 } else if (db->key[1]) {
5621 /* If we already ran a binding don't process another one since
5622 * we can't keep track of all the binding keys that were
5623 * pressed in order to swallow the release events. */
5624 send = 1;
5625 check_binding = 0;
5626 }
5627
5628 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005629 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5630 key, state)) {
5631 /* We ran a binding so swallow the press and keep the
5632 * grab to swallow the released too. */
5633 send = 0;
5634 terminate = 0;
5635 db->key[1] = key;
5636 } else {
5637 /* Terminate the grab since the key pressed is not a
5638 * debug binding key. */
5639 send = 1;
5640 terminate = 1;
5641 }
5642 }
5643
5644 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005645 serial = wl_display_next_serial(display);
5646 resource_list = &grab->keyboard->focus_resource_list;
5647 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005648 wl_keyboard_send_key(resource, serial, time, key, state);
5649 }
5650 }
5651
5652 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005653 weston_keyboard_end_grab(grab->keyboard);
5654 if (grab->keyboard->input_method_resource)
5655 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005656 free(db);
5657 }
5658}
5659
5660static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005661debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005662 uint32_t mods_depressed, uint32_t mods_latched,
5663 uint32_t mods_locked, uint32_t group)
5664{
5665 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005666 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005667
Neil Roberts96d790e2013-09-19 17:32:00 +01005668 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005669
Neil Roberts96d790e2013-09-19 17:32:00 +01005670 wl_resource_for_each(resource, resource_list) {
5671 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5672 mods_latched, mods_locked, group);
5673 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005674}
5675
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005676static void
5677debug_binding_cancel(struct weston_keyboard_grab *grab)
5678{
5679 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5680
5681 weston_keyboard_end_grab(grab->keyboard);
5682 free(db);
5683}
5684
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005685struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005686 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005687 debug_binding_modifiers,
5688 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005689};
5690
5691static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005692debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005693{
5694 struct debug_binding_grab *grab;
5695
5696 grab = calloc(1, sizeof *grab);
5697 if (!grab)
5698 return;
5699
5700 grab->seat = (struct weston_seat *) seat;
5701 grab->key[0] = key;
5702 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005703 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005704}
5705
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005706static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005707force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005708 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005709{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005710 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005711 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005712 struct desktop_shell *shell = data;
5713 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005714 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005715
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005716 focus_surface = seat->keyboard->focus;
5717 if (!focus_surface)
5718 return;
5719
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005720 wl_signal_emit(&compositor->kill_signal, focus_surface);
5721
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005722 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005723 wl_client_get_credentials(client, &pid, NULL, NULL);
5724
5725 /* Skip clients that we launched ourselves (the credentials of
5726 * the socketpair is ours) */
5727 if (pid == getpid())
5728 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005729
Daniel Stone325fc2d2012-05-30 16:31:58 +01005730 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005731}
5732
5733static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005734workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005735 uint32_t key, void *data)
5736{
5737 struct desktop_shell *shell = data;
5738 unsigned int new_index = shell->workspaces.current;
5739
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005740 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005741 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005742 if (new_index != 0)
5743 new_index--;
5744
5745 change_workspace(shell, new_index);
5746}
5747
5748static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005749workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005750 uint32_t key, void *data)
5751{
5752 struct desktop_shell *shell = data;
5753 unsigned int new_index = shell->workspaces.current;
5754
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005755 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005756 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005757 if (new_index < shell->workspaces.num - 1)
5758 new_index++;
5759
5760 change_workspace(shell, new_index);
5761}
5762
5763static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005764workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005765 uint32_t key, void *data)
5766{
5767 struct desktop_shell *shell = data;
5768 unsigned int new_index;
5769
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005770 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005771 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005772 new_index = key - KEY_F1;
5773 if (new_index >= shell->workspaces.num)
5774 new_index = shell->workspaces.num - 1;
5775
5776 change_workspace(shell, new_index);
5777}
5778
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005779static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005780workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005781 uint32_t key, void *data)
5782{
5783 struct desktop_shell *shell = data;
5784 unsigned int new_index = shell->workspaces.current;
5785
5786 if (shell->locked)
5787 return;
5788
5789 if (new_index != 0)
5790 new_index--;
5791
5792 take_surface_to_workspace_by_seat(shell, seat, new_index);
5793}
5794
5795static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005796workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005797 uint32_t key, void *data)
5798{
5799 struct desktop_shell *shell = data;
5800 unsigned int new_index = shell->workspaces.current;
5801
5802 if (shell->locked)
5803 return;
5804
5805 if (new_index < shell->workspaces.num - 1)
5806 new_index++;
5807
5808 take_surface_to_workspace_by_seat(shell, seat, new_index);
5809}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005810
5811static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005812handle_output_destroy(struct wl_listener *listener, void *data)
5813{
5814 struct shell_output *output_listener =
5815 container_of(listener, struct shell_output, destroy_listener);
5816
5817 wl_list_remove(&output_listener->destroy_listener.link);
5818 wl_list_remove(&output_listener->link);
5819 free(output_listener);
5820}
5821
5822static void
5823create_shell_output(struct desktop_shell *shell,
5824 struct weston_output *output)
5825{
5826 struct shell_output *shell_output;
5827
5828 shell_output = zalloc(sizeof *shell_output);
5829 if (shell_output == NULL)
5830 return;
5831
5832 shell_output->output = output;
5833 shell_output->shell = shell;
5834 shell_output->destroy_listener.notify = handle_output_destroy;
5835 wl_signal_add(&output->destroy_signal,
5836 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005837 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005838}
5839
5840static void
5841handle_output_create(struct wl_listener *listener, void *data)
5842{
5843 struct desktop_shell *shell =
5844 container_of(listener, struct desktop_shell, output_create_listener);
5845 struct weston_output *output = (struct weston_output *)data;
5846
5847 create_shell_output(shell, output);
5848}
5849
5850static void
5851setup_output_destroy_handler(struct weston_compositor *ec,
5852 struct desktop_shell *shell)
5853{
5854 struct weston_output *output;
5855
5856 wl_list_init(&shell->output_list);
5857 wl_list_for_each(output, &ec->output_list, link)
5858 create_shell_output(shell, output);
5859
5860 shell->output_create_listener.notify = handle_output_create;
5861 wl_signal_add(&ec->output_created_signal,
5862 &shell->output_create_listener);
5863}
5864
5865static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005866shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005867{
Tiago Vignattibe143262012-04-16 17:31:41 +03005868 struct desktop_shell *shell =
5869 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005870 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005871 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005872
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005873 if (shell->child.client)
5874 wl_client_destroy(shell->child.client);
5875
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005876 wl_list_remove(&shell->idle_listener.link);
5877 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005878 wl_list_remove(&shell->show_input_panel_listener.link);
5879 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005880
Xiong Zhang6b481422013-10-23 13:58:32 +08005881 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5882 wl_list_remove(&shell_output->destroy_listener.link);
5883 wl_list_remove(&shell_output->link);
5884 free(shell_output);
5885 }
5886
5887 wl_list_remove(&shell->output_create_listener.link);
5888
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005889 wl_array_for_each(ws, &shell->workspaces.array)
5890 workspace_destroy(*ws);
5891 wl_array_release(&shell->workspaces.array);
5892
Pekka Paalanen3c647232011-12-22 13:43:43 +02005893 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005894 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005895 free(shell);
5896}
5897
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005898static void
5899shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5900{
5901 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005902 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005903
5904 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005905 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5906 MODIFIER_CTRL | MODIFIER_ALT,
5907 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01005908 weston_compositor_add_key_binding(ec, KEY_TAB,
5909 MODIFIER_ALT,
5910 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005911 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5912 click_to_activate_binding,
5913 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005914 weston_compositor_add_touch_binding(ec, 0,
5915 touch_to_activate_binding,
5916 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005917 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5918 MODIFIER_SUPER | MODIFIER_ALT,
5919 surface_opacity_binding, NULL);
5920 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5921 MODIFIER_SUPER, zoom_axis_binding,
5922 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005923
5924 /* configurable bindings */
5925 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005926 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5927 zoom_key_binding, NULL);
5928 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5929 zoom_key_binding, NULL);
5930 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5931 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005932 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005933 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5934 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005935
5936 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5937 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5938 rotate_binding, NULL);
5939
Daniel Stone325fc2d2012-05-30 16:31:58 +01005940 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5941 shell);
5942 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5943 ec);
5944 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5945 backlight_binding, ec);
5946 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5947 ec);
5948 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5949 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005950 weston_compositor_add_key_binding(ec, KEY_K, mod,
5951 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005952 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5953 workspace_up_binding, shell);
5954 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5955 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005956 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5957 workspace_move_surface_up_binding,
5958 shell);
5959 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5960 workspace_move_surface_down_binding,
5961 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005962
Daniel Stonedf8133b2013-11-19 11:37:14 +01005963 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5964
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005965 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5966 if (shell->workspaces.num > 1) {
5967 num_workspace_bindings = shell->workspaces.num;
5968 if (num_workspace_bindings > 6)
5969 num_workspace_bindings = 6;
5970 for (i = 0; i < num_workspace_bindings; i++)
5971 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5972 workspace_f_binding,
5973 shell);
5974 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005975
5976 /* Debug bindings */
5977 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5978 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005979}
5980
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005981WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005982module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005983 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005984{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005985 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005986 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005987 struct workspace **pws;
5988 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005989 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005990
Peter Huttererf3d62272013-08-08 11:57:05 +10005991 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005992 if (shell == NULL)
5993 return -1;
5994
Kristian Høgsberg75840622011-09-06 13:48:16 -04005995 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005996
5997 shell->destroy_listener.notify = shell_destroy;
5998 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005999 shell->idle_listener.notify = idle_handler;
6000 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6001 shell->wake_listener.notify = wake_handler;
6002 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006003 shell->show_input_panel_listener.notify = show_input_panels;
6004 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6005 shell->hide_input_panel_listener.notify = hide_input_panels;
6006 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006007 shell->update_input_panel_listener.notify = update_input_panels;
6008 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006009 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006010 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006011 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006012 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006013 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006014 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006015 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006016 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006017 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006018 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006019 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006020
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006021 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006022
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006023 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6024 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006025 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6026 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006027 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006028
6029 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006030 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006031
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006032 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006033
Daniel Stonedf8133b2013-11-19 11:37:14 +01006034 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6035 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6036
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006037 for (i = 0; i < shell->workspaces.num; i++) {
6038 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6039 if (pws == NULL)
6040 return -1;
6041
6042 *pws = workspace_create();
6043 if (*pws == NULL)
6044 return -1;
6045 }
6046 activate_workspace(shell, 0);
6047
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006048 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006049 wl_list_init(&shell->workspaces.animation.link);
6050 shell->workspaces.animation.frame = animate_workspace_change_frame;
6051
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006052 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006053 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006054 return -1;
6055
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006056 if (wl_global_create(ec->wl_display,
6057 &desktop_shell_interface, 2,
6058 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006059 return -1;
6060
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006061 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6062 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006063 return -1;
6064
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006065 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006066 shell, bind_input_panel) == NULL)
6067 return -1;
6068
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006069 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6070 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006071 return -1;
6072
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006073 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006074
Xiong Zhang6b481422013-10-23 13:58:32 +08006075 setup_output_destroy_handler(ec, shell);
6076
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006077 loop = wl_display_get_event_loop(ec->wl_display);
6078 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006079
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006080 shell->screensaver.timer =
6081 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6082
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006083 wl_list_for_each(seat, &ec->seat_list, link)
6084 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006085
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006086 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006087
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006088 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006089
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006090 return 0;
6091}