blob: b2bc74aad865fd2a5f26f41613bbcf4395affa66 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020029#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050030#include <string.h>
31#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040032#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020033#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020034#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020035#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040036#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010040#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020041#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020042#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Juan Zhaoe10d2792012-04-25 19:09:52 +080047enum animation_type {
48 ANIMATION_NONE,
49
50 ANIMATION_ZOOM,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010051 ANIMATION_FADE,
52 ANIMATION_DIM_LAYER,
Juan Zhaoe10d2792012-04-25 19:09:52 +080053};
54
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020055enum fade_type {
56 FADE_IN,
57 FADE_OUT
58};
59
Daniel Stonedf8133b2013-11-19 11:37:14 +010060enum exposay_target_state {
61 EXPOSAY_TARGET_OVERVIEW, /* show all windows */
62 EXPOSAY_TARGET_CANCEL, /* return to normal, same focus */
63 EXPOSAY_TARGET_SWITCH, /* return to normal, switch focus */
64};
65
66enum exposay_layout_state {
67 EXPOSAY_LAYOUT_INACTIVE = 0, /* normal desktop */
68 EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE, /* in transition to normal */
69 EXPOSAY_LAYOUT_OVERVIEW, /* show all windows */
70 EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW, /* in transition to all windows */
71};
72
Jonas Ådahl04769742012-06-13 00:01:24 +020073struct focus_state {
74 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040075 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020076 struct weston_surface *keyboard_focus;
77 struct wl_list link;
78 struct wl_listener seat_destroy_listener;
79 struct wl_listener surface_destroy_listener;
80};
81
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010082struct focus_surface {
83 struct weston_surface *surface;
84 struct weston_view *view;
85 struct weston_transform workspace_transform;
86};
87
Jonas Ådahle3cddce2012-06-13 00:01:22 +020088struct workspace {
89 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020090
91 struct wl_list focus_list;
92 struct wl_listener seat_destroyed_listener;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010093
94 struct focus_surface *fsurf_front;
95 struct focus_surface *fsurf_back;
96 struct weston_view_animation *focus_animation;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020097};
98
Philipp Brüschweiler88013572012-08-06 13:44:42 +020099struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -0500100 struct wl_resource *resource;
101 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100102
103 struct desktop_shell *shell;
104
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200105 struct wl_list link;
106 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500107 struct weston_view *view;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100108 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200109
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +0200110 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200111 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200112};
113
Xiong Zhang6b481422013-10-23 13:58:32 +0800114struct shell_output {
115 struct desktop_shell *shell;
116 struct weston_output *output;
117 struct wl_listener destroy_listener;
118 struct wl_list link;
119};
120
Tiago Vignattibe143262012-04-16 17:31:41 +0300121struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500122 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400123
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200124 struct wl_listener idle_listener;
125 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400126 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200127 struct wl_listener show_input_panel_listener;
128 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200129 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200130
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500131 struct weston_layer fullscreen_layer;
132 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500133 struct weston_layer background_layer;
134 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200135 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500136
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400137 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300138 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400139
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200140 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500141 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200142 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200143 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200144
145 unsigned deathcount;
146 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200147 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200148
149 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200150 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200151 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200152
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200153 struct {
154 struct weston_surface *surface;
155 pixman_box32_t cursor_rectangle;
156 } text_input;
157
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400158 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500159 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160
Pekka Paalanen77346a62011-11-30 16:26:35 +0200161 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200162 struct wl_array array;
163 unsigned int current;
164 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200165
Jonas Ådahle9d22502012-08-29 22:13:01 +0200166 struct wl_list client_list;
167
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200168 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200169 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200170 int anim_dir;
171 uint32_t anim_timestamp;
172 double anim_current;
173 struct workspace *anim_from;
174 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200175 } workspaces;
176
177 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200178 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200179 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200180 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500181 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200182 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200183 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500184
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200185 struct {
186 struct wl_resource *binding;
187 struct wl_list surfaces;
188 } input_panel;
189
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200190 struct {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500191 struct weston_view *view;
192 struct weston_view_animation *animation;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200193 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300194 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200195 } fade;
196
Daniel Stonedf8133b2013-11-19 11:37:14 +0100197 struct exposay {
198 /* XXX: Make these exposay_surfaces. */
199 struct weston_view *focus_prev;
200 struct weston_view *focus_current;
201 struct weston_view *clicked;
202 struct workspace *workspace;
203 struct weston_seat *seat;
204 struct wl_list surface_list;
205
206 struct weston_keyboard_grab grab_kbd;
207 struct weston_pointer_grab grab_ptr;
208
209 enum exposay_target_state state_target;
210 enum exposay_layout_state state_cur;
211 int in_flight; /* number of animations still running */
212
213 int num_surfaces;
214 int grid_size;
215 int surface_size;
216
217 int hpadding_outer;
218 int vpadding_outer;
219 int padding_inner;
220
221 int row_current;
222 int column_current;
223 } exposay;
224
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300225 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800226 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700227 enum animation_type startup_animation_type;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100228 enum animation_type focus_animation_type;
Xiong Zhang6b481422013-10-23 13:58:32 +0800229
230 struct wl_listener output_create_listener;
231 struct wl_list output_list;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400232};
233
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500234enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200235 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500236 SHELL_SURFACE_TOPLEVEL,
237 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500238 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800239 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300240 SHELL_SURFACE_POPUP,
241 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200242};
243
Scott Moreauff1db4a2012-04-17 19:06:18 -0600244struct ping_timer {
245 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600246 uint32_t serial;
247};
248
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200249struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500250 struct wl_resource *resource;
251 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200252
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500253 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500254 struct weston_view *view;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200255 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400256 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300257 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200258
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400259 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400260 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500261 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800262 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800263 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700264 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100265
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500266 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200267 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500268 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200269 } rotation;
270
271 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100272 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500273 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100274 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400275 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500276 } popup;
277
Alex Wu4539b082012-03-01 12:57:46 +0800278 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300279 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400280 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300281 } transient;
282
283 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800284 enum wl_shell_surface_fullscreen_method type;
285 struct weston_transform transform; /* matrix from x, y */
286 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500287 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800288 } fullscreen;
289
Scott Moreauff1db4a2012-04-17 19:06:18 -0600290 struct ping_timer *ping_timer;
291
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200292 struct weston_transform workspace_transform;
293
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500294 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500295 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100296 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400297
298 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200299};
300
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300301struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400302 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300303 struct shell_surface *shsurf;
304 struct wl_listener shsurf_destroy_listener;
305};
306
Rusty Lynch1084da52013-08-15 09:10:08 -0700307struct shell_touch_grab {
308 struct weston_touch_grab grab;
309 struct shell_surface *shsurf;
310 struct wl_listener shsurf_destroy_listener;
311 struct weston_touch *touch;
312};
313
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300314struct weston_move_grab {
315 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100316 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500317};
318
Rusty Lynch1084da52013-08-15 09:10:08 -0700319struct weston_touch_move_grab {
320 struct shell_touch_grab base;
321 wl_fixed_t dx, dy;
322};
323
Pekka Paalanen460099f2012-01-20 16:48:25 +0200324struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300325 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500326 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200327 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200328 float x;
329 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200330 } center;
331};
332
Giulio Camuffo5085a752013-03-25 21:42:45 +0100333struct shell_seat {
334 struct weston_seat *seat;
335 struct wl_listener seat_destroy_listener;
336
337 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400338 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100339 struct wl_list surfaces_list;
340 struct wl_client *client;
341 int32_t initial_up;
342 } popup_grab;
343};
344
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400345static void
346activate(struct desktop_shell *shell, struct weston_surface *es,
347 struct weston_seat *seat);
348
349static struct workspace *
350get_current_workspace(struct desktop_shell *shell);
351
Alex Wubd3354b2012-04-17 17:20:49 +0800352static struct shell_surface *
353get_shell_surface(struct weston_surface *surface);
354
355static struct desktop_shell *
356shell_surface_get_shell(struct shell_surface *shsurf);
357
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500358static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400359surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500360
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300361static void
362shell_fade_startup(struct desktop_shell *shell);
363
Alex Wubd3354b2012-04-17 17:20:49 +0800364static bool
365shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
366{
367 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800369
370 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100371
Jason Ekstranda7af7042013-10-12 22:38:11 -0500372 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800373 return false;
374
Jason Ekstranda7af7042013-10-12 22:38:11 -0500375 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
376 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800377 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500378 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800379}
380
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500381static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400382destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300383{
384 struct shell_grab *grab;
385
386 grab = container_of(listener, struct shell_grab,
387 shsurf_destroy_listener);
388
389 grab->shsurf = NULL;
390}
391
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392static struct weston_view *
393get_default_view(struct weston_surface *surface)
394{
395 struct shell_surface *shsurf;
396 struct weston_view *view;
397
398 if (!surface || wl_list_empty(&surface->views))
399 return NULL;
400
401 shsurf = get_shell_surface(surface);
402 if (shsurf)
403 return shsurf->view;
404
405 wl_list_for_each(view, &surface->views, surface_link)
406 if (weston_view_is_mapped(view))
407 return view;
408
409 return container_of(surface->views.next, struct weston_view, surface_link);
410}
411
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300412static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400413popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400414
415static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300416shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400417 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300418 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400419 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300420 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300421{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300422 struct desktop_shell *shell = shsurf->shell;
423
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400424 popup_grab_end(pointer);
425
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300426 grab->grab.interface = interface;
427 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400428 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500429 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400430 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300431
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700432 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400433 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400434 if (shell->child.desktop_shell) {
435 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
436 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500437 weston_pointer_set_focus(pointer,
438 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400439 wl_fixed_from_int(0),
440 wl_fixed_from_int(0));
441 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300442}
443
444static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300445shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300446{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700447 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400448 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700449 grab->shsurf->grabbed = 0;
450 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300451
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700452 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300453}
454
455static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700456shell_touch_grab_start(struct shell_touch_grab *grab,
457 const struct weston_touch_grab_interface *interface,
458 struct shell_surface *shsurf,
459 struct weston_touch *touch)
460{
461 struct desktop_shell *shell = shsurf->shell;
462
463 grab->grab.interface = interface;
464 grab->shsurf = shsurf;
465 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
466 wl_signal_add(&shsurf->destroy_signal,
467 &grab->shsurf_destroy_listener);
468
469 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700470 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700471
472 weston_touch_start_grab(touch, &grab->grab);
473 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500474 weston_touch_set_focus(touch->seat,
475 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700476}
477
478static void
479shell_touch_grab_end(struct shell_touch_grab *grab)
480{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700481 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700482 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700483 grab->shsurf->grabbed = 0;
484 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700485
486 weston_touch_end_grab(grab->touch);
487}
488
489static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500490center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800491 struct weston_output *output);
492
Daniel Stone496ca172012-05-30 16:31:42 +0100493static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300494get_modifier(char *modifier)
495{
496 if (!modifier)
497 return MODIFIER_SUPER;
498
499 if (!strcmp("ctrl", modifier))
500 return MODIFIER_CTRL;
501 else if (!strcmp("alt", modifier))
502 return MODIFIER_ALT;
503 else if (!strcmp("super", modifier))
504 return MODIFIER_SUPER;
505 else
506 return MODIFIER_SUPER;
507}
508
Juan Zhaoe10d2792012-04-25 19:09:52 +0800509static enum animation_type
510get_animation_type(char *animation)
511{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800512 if (!strcmp("zoom", animation))
513 return ANIMATION_ZOOM;
514 else if (!strcmp("fade", animation))
515 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100516 else if (!strcmp("dim-layer", animation))
517 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800518 else
519 return ANIMATION_NONE;
520}
521
Alex Wu4539b082012-03-01 12:57:46 +0800522static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400523shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200524{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400525 struct weston_config_section *section;
526 int duration;
527 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200528
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400529 section = weston_config_get_section(shell->compositor->config,
530 "screensaver", NULL, NULL);
531 weston_config_section_get_string(section,
532 "path", &shell->screensaver.path, NULL);
533 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200534 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400535
536 section = weston_config_get_section(shell->compositor->config,
537 "shell", NULL, NULL);
538 weston_config_section_get_string(section,
539 "binding-modifier", &s, "super");
540 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200541 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400542 weston_config_section_get_string(section, "animation", &s, "none");
543 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200544 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700545 weston_config_section_get_string(section,
546 "startup-animation", &s, "fade");
547 shell->startup_animation_type = get_animation_type(s);
548 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700549 if (shell->startup_animation_type == ANIMATION_ZOOM)
550 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100551 weston_config_section_get_string(section, "focus-animation", &s, "none");
552 shell->focus_animation_type = get_animation_type(s);
553 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400554 weston_config_section_get_uint(section, "num-workspaces",
555 &shell->workspaces.num,
556 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200557}
558
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100559static struct weston_output *
560get_default_output(struct weston_compositor *compositor)
561{
562 return container_of(compositor->output_list.next,
563 struct weston_output, link);
564}
565
566
567/* no-op func for checking focus surface */
568static void
569focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy,
570 int32_t width, int32_t height)
571{
572}
573
574static struct focus_surface *
575get_focus_surface(struct weston_surface *surface)
576{
577 if (surface->configure == focus_surface_configure)
578 return surface->configure_private;
579 else
580 return NULL;
581}
582
583static bool
584is_focus_surface (struct weston_surface *es)
585{
586 return (es->configure == focus_surface_configure);
587}
588
589static bool
590is_focus_view (struct weston_view *view)
591{
592 return is_focus_surface (view->surface);
593}
594
595static struct focus_surface *
596create_focus_surface(struct weston_compositor *ec,
597 struct weston_output *output)
598{
599 struct focus_surface *fsurf = NULL;
600 struct weston_surface *surface = NULL;
601
602 fsurf = malloc(sizeof *fsurf);
603 if (!fsurf)
604 return NULL;
605
606 fsurf->surface = weston_surface_create(ec);
607 surface = fsurf->surface;
608 if (surface == NULL) {
609 free(fsurf);
610 return NULL;
611 }
612
613 surface->configure = focus_surface_configure;
614 surface->output = output;
615 surface->configure_private = fsurf;
616
617 fsurf->view = weston_view_create (surface);
618
619 weston_view_configure(fsurf->view, output->x, output->y,
620 output->width, output->height);
621 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
622 pixman_region32_fini(&surface->opaque);
623 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
624 output->width, output->height);
625 pixman_region32_fini(&surface->input);
626 pixman_region32_init(&surface->input);
627
628 wl_list_init(&fsurf->workspace_transform.link);
629
630 return fsurf;
631}
632
633static void
634focus_surface_destroy(struct focus_surface *fsurf)
635{
636 weston_surface_destroy(fsurf->surface);
637 free(fsurf);
638}
639
640static void
641focus_animation_done(struct weston_view_animation *animation, void *data)
642{
643 struct workspace *ws = data;
644
645 ws->focus_animation = NULL;
646}
647
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200648static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200649focus_state_destroy(struct focus_state *state)
650{
651 wl_list_remove(&state->seat_destroy_listener.link);
652 wl_list_remove(&state->surface_destroy_listener.link);
653 free(state);
654}
655
656static void
657focus_state_seat_destroy(struct wl_listener *listener, void *data)
658{
659 struct focus_state *state = container_of(listener,
660 struct focus_state,
661 seat_destroy_listener);
662
663 wl_list_remove(&state->link);
664 focus_state_destroy(state);
665}
666
667static void
668focus_state_surface_destroy(struct wl_listener *listener, void *data)
669{
670 struct focus_state *state = container_of(listener,
671 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400672 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400673 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500674 struct weston_surface *main_surface, *next;
675 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200676
Pekka Paalanen01388e22013-04-25 13:57:44 +0300677 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
678
Kristian Høgsberge3778222012-07-31 17:29:30 -0400679 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500680 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
681 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400682 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100683 if (is_focus_view(view))
684 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400685
Jason Ekstranda7af7042013-10-12 22:38:11 -0500686 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400687 break;
688 }
689
Pekka Paalanen01388e22013-04-25 13:57:44 +0300690 /* if the focus was a sub-surface, activate its main surface */
691 if (main_surface != state->keyboard_focus)
692 next = main_surface;
693
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100694 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400695 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100696 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400697 activate(shell, next, state->seat);
698 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100699 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
700 if (state->ws->focus_animation)
701 weston_view_animation_destroy(state->ws->focus_animation);
702
703 state->ws->focus_animation = weston_fade_run(
704 state->ws->fsurf_front->view,
705 state->ws->fsurf_front->view->alpha, 0.0, 300,
706 focus_animation_done, state->ws);
707 }
708
Kristian Høgsberge3778222012-07-31 17:29:30 -0400709 wl_list_remove(&state->link);
710 focus_state_destroy(state);
711 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200712}
713
714static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400715focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200716{
Jonas Ådahl04769742012-06-13 00:01:24 +0200717 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200718
719 state = malloc(sizeof *state);
720 if (state == NULL)
721 return NULL;
722
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100723 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400724 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200725 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400726 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200727
728 state->seat_destroy_listener.notify = focus_state_seat_destroy;
729 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400730 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200731 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400732 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200733
734 return state;
735}
736
Jonas Ådahl8538b222012-08-29 22:13:03 +0200737static struct focus_state *
738ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
739{
740 struct workspace *ws = get_current_workspace(shell);
741 struct focus_state *state;
742
743 wl_list_for_each(state, &ws->focus_list, link)
744 if (state->seat == seat)
745 break;
746
747 if (&state->link == &ws->focus_list)
748 state = focus_state_create(seat, ws);
749
750 return state;
751}
752
Jonas Ådahl04769742012-06-13 00:01:24 +0200753static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400754restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200755{
756 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400757 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200758
759 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400760 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200761
Kristian Høgsberge3148752013-05-06 23:19:49 -0400762 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200763 }
764}
765
766static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200767replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
768 struct weston_seat *seat)
769{
770 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400771 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200772
773 wl_list_for_each(state, &ws->focus_list, link) {
774 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400775 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500776 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200777 return;
778 }
779 }
780}
781
782static void
783drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
784 struct weston_surface *surface)
785{
786 struct focus_state *state;
787
788 wl_list_for_each(state, &ws->focus_list, link)
789 if (state->keyboard_focus == surface)
790 state->keyboard_focus = NULL;
791}
792
793static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100794animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
795 struct weston_view *from, struct weston_view *to)
796{
797 struct weston_output *output;
798 bool focus_surface_created = false;
799
800 /* FIXME: Only support dim animation using two layers */
801 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
802 return;
803
804 output = get_default_output(shell->compositor);
805 if (ws->fsurf_front == NULL && (from || to)) {
806 ws->fsurf_front = create_focus_surface(shell->compositor, output);
807 ws->fsurf_back = create_focus_surface(shell->compositor, output);
808 ws->fsurf_front->view->alpha = 0.0;
809 ws->fsurf_back->view->alpha = 0.0;
810 focus_surface_created = true;
811 } else {
812 wl_list_remove(&ws->fsurf_front->view->layer_link);
813 wl_list_remove(&ws->fsurf_back->view->layer_link);
814 }
815
816 if (ws->focus_animation) {
817 weston_view_animation_destroy(ws->focus_animation);
818 ws->focus_animation = NULL;
819 }
820
821 if (to)
822 wl_list_insert(&to->layer_link,
823 &ws->fsurf_front->view->layer_link);
824 else if (from)
825 wl_list_insert(&ws->layer.view_list,
826 &ws->fsurf_front->view->layer_link);
827
828 if (focus_surface_created) {
829 ws->focus_animation = weston_fade_run(
830 ws->fsurf_front->view,
831 ws->fsurf_front->view->alpha, 0.6, 300,
832 focus_animation_done, ws);
833 } else if (from) {
834 wl_list_insert(&from->layer_link,
835 &ws->fsurf_back->view->layer_link);
836 ws->focus_animation = weston_stable_fade_run(
837 ws->fsurf_front->view, 0.0,
838 ws->fsurf_back->view, 0.6,
839 focus_animation_done, ws);
840 } else if (to) {
841 wl_list_insert(&ws->layer.view_list,
842 &ws->fsurf_back->view->layer_link);
843 ws->focus_animation = weston_stable_fade_run(
844 ws->fsurf_front->view, 0.0,
845 ws->fsurf_back->view, 0.6,
846 focus_animation_done, ws);
847 }
848}
849
850static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200851workspace_destroy(struct workspace *ws)
852{
Jonas Ådahl04769742012-06-13 00:01:24 +0200853 struct focus_state *state, *next;
854
855 wl_list_for_each_safe(state, next, &ws->focus_list, link)
856 focus_state_destroy(state);
857
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100858 if (ws->fsurf_front)
859 focus_surface_destroy(ws->fsurf_front);
860 if (ws->fsurf_back)
861 focus_surface_destroy(ws->fsurf_back);
862
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200863 free(ws);
864}
865
Jonas Ådahl04769742012-06-13 00:01:24 +0200866static void
867seat_destroyed(struct wl_listener *listener, void *data)
868{
869 struct weston_seat *seat = data;
870 struct focus_state *state, *next;
871 struct workspace *ws = container_of(listener,
872 struct workspace,
873 seat_destroyed_listener);
874
875 wl_list_for_each_safe(state, next, &ws->focus_list, link)
876 if (state->seat == seat)
877 wl_list_remove(&state->link);
878}
879
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200880static struct workspace *
881workspace_create(void)
882{
883 struct workspace *ws = malloc(sizeof *ws);
884 if (ws == NULL)
885 return NULL;
886
887 weston_layer_init(&ws->layer, NULL);
888
Jonas Ådahl04769742012-06-13 00:01:24 +0200889 wl_list_init(&ws->focus_list);
890 wl_list_init(&ws->seat_destroyed_listener.link);
891 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100892 ws->fsurf_front = NULL;
893 ws->fsurf_back = NULL;
894 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200895
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200896 return ws;
897}
898
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200899static int
900workspace_is_empty(struct workspace *ws)
901{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500902 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200903}
904
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905static struct workspace *
906get_workspace(struct desktop_shell *shell, unsigned int index)
907{
908 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200909 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200910 pws += index;
911 return *pws;
912}
913
914static struct workspace *
915get_current_workspace(struct desktop_shell *shell)
916{
917 return get_workspace(shell, shell->workspaces.current);
918}
919
920static void
921activate_workspace(struct desktop_shell *shell, unsigned int index)
922{
923 struct workspace *ws;
924
925 ws = get_workspace(shell, index);
926 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
927
928 shell->workspaces.current = index;
929}
930
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200931static unsigned int
932get_output_height(struct weston_output *output)
933{
934 return abs(output->region.extents.y1 - output->region.extents.y2);
935}
936
937static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100938view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200939{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200940 struct weston_transform *transform;
941
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100942 if (is_focus_view(view)) {
943 struct focus_surface *fsurf = get_focus_surface(view->surface);
944 transform = &fsurf->workspace_transform;
945 } else {
946 struct shell_surface *shsurf = get_shell_surface(view->surface);
947 transform = &shsurf->workspace_transform;
948 }
949
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200950 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100952 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200953
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100954 weston_matrix_init(&transform->matrix);
955 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200956 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200958}
959
960static void
961workspace_translate_out(struct workspace *ws, double fraction)
962{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964 unsigned int height;
965 double d;
966
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
968 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200969 d = height * fraction;
970
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100971 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200972 }
973}
974
975static void
976workspace_translate_in(struct workspace *ws, double fraction)
977{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500978 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200979 unsigned int height;
980 double d;
981
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
983 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984
985 if (fraction > 0)
986 d = -(height - height * fraction);
987 else
988 d = height + height * fraction;
989
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100990 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991 }
992}
993
994static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200995broadcast_current_workspace_state(struct desktop_shell *shell)
996{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700997 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200998
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700999 wl_resource_for_each(resource, &shell->workspaces.client_list)
1000 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001001 shell->workspaces.current,
1002 shell->workspaces.num);
1003}
1004
1005static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001006reverse_workspace_change_animation(struct desktop_shell *shell,
1007 unsigned int index,
1008 struct workspace *from,
1009 struct workspace *to)
1010{
1011 shell->workspaces.current = index;
1012
1013 shell->workspaces.anim_to = to;
1014 shell->workspaces.anim_from = from;
1015 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1016 shell->workspaces.anim_timestamp = 0;
1017
Scott Moreau4272e632012-08-13 09:58:41 -06001018 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001019}
1020
1021static void
1022workspace_deactivate_transforms(struct workspace *ws)
1023{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001025 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001026
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001028 if (is_focus_view(view)) {
1029 struct focus_surface *fsurf = get_focus_surface(view->surface);
1030 transform = &fsurf->workspace_transform;
1031 } else {
1032 struct shell_surface *shsurf = get_shell_surface(view->surface);
1033 transform = &shsurf->workspace_transform;
1034 }
1035
1036 if (!wl_list_empty(&transform->link)) {
1037 wl_list_remove(&transform->link);
1038 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001039 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001041 }
1042}
1043
1044static void
1045finish_workspace_change_animation(struct desktop_shell *shell,
1046 struct workspace *from,
1047 struct workspace *to)
1048{
Scott Moreau4272e632012-08-13 09:58:41 -06001049 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001050
1051 wl_list_remove(&shell->workspaces.animation.link);
1052 workspace_deactivate_transforms(from);
1053 workspace_deactivate_transforms(to);
1054 shell->workspaces.anim_to = NULL;
1055
1056 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1057}
1058
1059static void
1060animate_workspace_change_frame(struct weston_animation *animation,
1061 struct weston_output *output, uint32_t msecs)
1062{
1063 struct desktop_shell *shell =
1064 container_of(animation, struct desktop_shell,
1065 workspaces.animation);
1066 struct workspace *from = shell->workspaces.anim_from;
1067 struct workspace *to = shell->workspaces.anim_to;
1068 uint32_t t;
1069 double x, y;
1070
1071 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1072 finish_workspace_change_animation(shell, from, to);
1073 return;
1074 }
1075
1076 if (shell->workspaces.anim_timestamp == 0) {
1077 if (shell->workspaces.anim_current == 0.0)
1078 shell->workspaces.anim_timestamp = msecs;
1079 else
1080 shell->workspaces.anim_timestamp =
1081 msecs -
1082 /* Invers of movement function 'y' below. */
1083 (asin(1.0 - shell->workspaces.anim_current) *
1084 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1085 M_2_PI);
1086 }
1087
1088 t = msecs - shell->workspaces.anim_timestamp;
1089
1090 /*
1091 * x = [0, π/2]
1092 * y(x) = sin(x)
1093 */
1094 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1095 y = sin(x);
1096
1097 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001098 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001099
1100 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1101 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1102 shell->workspaces.anim_current = y;
1103
Scott Moreau4272e632012-08-13 09:58:41 -06001104 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001106 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001108}
1109
1110static void
1111animate_workspace_change(struct desktop_shell *shell,
1112 unsigned int index,
1113 struct workspace *from,
1114 struct workspace *to)
1115{
1116 struct weston_output *output;
1117
1118 int dir;
1119
1120 if (index > shell->workspaces.current)
1121 dir = -1;
1122 else
1123 dir = 1;
1124
1125 shell->workspaces.current = index;
1126
1127 shell->workspaces.anim_dir = dir;
1128 shell->workspaces.anim_from = from;
1129 shell->workspaces.anim_to = to;
1130 shell->workspaces.anim_current = 0.0;
1131 shell->workspaces.anim_timestamp = 0;
1132
1133 output = container_of(shell->compositor->output_list.next,
1134 struct weston_output, link);
1135 wl_list_insert(&output->animation_list,
1136 &shell->workspaces.animation.link);
1137
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001138 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001139
1140 workspace_translate_in(to, 0);
1141
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001142 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001143
Scott Moreau4272e632012-08-13 09:58:41 -06001144 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001145}
1146
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001147static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001148update_workspace(struct desktop_shell *shell, unsigned int index,
1149 struct workspace *from, struct workspace *to)
1150{
1151 shell->workspaces.current = index;
1152 wl_list_insert(&from->layer.link, &to->layer.link);
1153 wl_list_remove(&from->layer.link);
1154}
1155
1156static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001157change_workspace(struct desktop_shell *shell, unsigned int index)
1158{
1159 struct workspace *from;
1160 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001161 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001162
1163 if (index == shell->workspaces.current)
1164 return;
1165
1166 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001168 return;
1169
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001170 from = get_current_workspace(shell);
1171 to = get_workspace(shell, index);
1172
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001173 if (shell->workspaces.anim_from == to &&
1174 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001175 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001176 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001177 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001178 return;
1179 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001180
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001181 if (shell->workspaces.anim_to != NULL)
1182 finish_workspace_change_animation(shell,
1183 shell->workspaces.anim_from,
1184 shell->workspaces.anim_to);
1185
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001186 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001187
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001188 if (shell->focus_animation_type != ANIMATION_NONE) {
1189 wl_list_for_each(state, &from->focus_list, link)
1190 if (state->keyboard_focus)
1191 animate_focus_change(shell, from,
1192 get_default_view(state->keyboard_focus), NULL);
1193
1194 wl_list_for_each(state, &to->focus_list, link)
1195 if (state->keyboard_focus)
1196 animate_focus_change(shell, to,
1197 NULL, get_default_view(state->keyboard_focus));
1198 }
1199
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001200 if (workspace_is_empty(to) && workspace_is_empty(from))
1201 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001202 else
1203 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001204
1205 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001206}
1207
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001208static bool
1209workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1210{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001211 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001212 struct wl_list *e;
1213
1214 if (wl_list_empty(list))
1215 return false;
1216
1217 e = list->next;
1218
1219 if (e->next != list)
1220 return false;
1221
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001223}
1224
1225static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226move_view_to_workspace(struct desktop_shell *shell,
1227 struct weston_view *view,
1228 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001229{
1230 struct workspace *from;
1231 struct workspace *to;
1232 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001233 struct weston_surface *focus;
1234
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001236
1237 if (workspace == shell->workspaces.current)
1238 return;
1239
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001240 if (workspace >= shell->workspaces.num)
1241 workspace = shell->workspaces.num - 1;
1242
Jonas Ådahle9d22502012-08-29 22:13:01 +02001243 from = get_current_workspace(shell);
1244 to = get_workspace(shell, workspace);
1245
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 wl_list_remove(&view->layer_link);
1247 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001248
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001250 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1251 if (!seat->keyboard)
1252 continue;
1253
1254 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001256 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001257 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001258
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001260}
1261
1262static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001263take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001264 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001265 unsigned int index)
1266{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001267 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001269 struct shell_surface *shsurf;
1270 struct workspace *from;
1271 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001272 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001273
Pekka Paalanen01388e22013-04-25 13:57:44 +03001274 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 view = get_default_view(surface);
1276 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001277 index == shell->workspaces.current ||
1278 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001279 return;
1280
1281 from = get_current_workspace(shell);
1282 to = get_workspace(shell, index);
1283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 wl_list_remove(&view->layer_link);
1285 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001286
Jonas Ådahle9d22502012-08-29 22:13:01 +02001287 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001288 drop_focus_state(shell, from, surface);
1289
1290 if (shell->workspaces.anim_from == to &&
1291 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001292 wl_list_remove(&to->layer.link);
1293 wl_list_insert(from->layer.link.prev, &to->layer.link);
1294
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001296 broadcast_current_workspace_state(shell);
1297
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001298 return;
1299 }
1300
1301 if (shell->workspaces.anim_to != NULL)
1302 finish_workspace_change_animation(shell,
1303 shell->workspaces.anim_from,
1304 shell->workspaces.anim_to);
1305
1306 if (workspace_is_empty(from) &&
1307 workspace_has_only(to, surface))
1308 update_workspace(shell, index, from, to);
1309 else {
1310 shsurf = get_shell_surface(surface);
1311 if (wl_list_empty(&shsurf->workspace_transform.link))
1312 wl_list_insert(&shell->workspaces.anim_sticky_list,
1313 &shsurf->workspace_transform.link);
1314
1315 animate_workspace_change(shell, index, from, to);
1316 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001317
1318 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001319
1320 state = ensure_focus_state(shell, seat);
1321 if (state != NULL)
1322 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323}
1324
1325static void
1326workspace_manager_move_surface(struct wl_client *client,
1327 struct wl_resource *resource,
1328 struct wl_resource *surface_resource,
1329 uint32_t workspace)
1330{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001331 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001332 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001333 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001334 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001336
Pekka Paalanen01388e22013-04-25 13:57:44 +03001337 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 view = get_default_view(main_surface);
1339 if (!view)
1340 return;
1341 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001342}
1343
1344static const struct workspace_manager_interface workspace_manager_implementation = {
1345 workspace_manager_move_surface,
1346};
1347
1348static void
1349unbind_resource(struct wl_resource *resource)
1350{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001351 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001352}
1353
1354static void
1355bind_workspace_manager(struct wl_client *client,
1356 void *data, uint32_t version, uint32_t id)
1357{
1358 struct desktop_shell *shell = data;
1359 struct wl_resource *resource;
1360
Jason Ekstranda85118c2013-06-27 20:17:02 -05001361 resource = wl_resource_create(client,
1362 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001363
1364 if (resource == NULL) {
1365 weston_log("couldn't add workspace manager object");
1366 return;
1367 }
1368
Jason Ekstranda85118c2013-06-27 20:17:02 -05001369 wl_resource_set_implementation(resource,
1370 &workspace_manager_implementation,
1371 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001372 wl_list_insert(&shell->workspaces.client_list,
1373 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001374
1375 workspace_manager_send_state(resource,
1376 shell->workspaces.current,
1377 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001378}
1379
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001380static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001381touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1382 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1383{
1384}
1385
1386static void
1387touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1388{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001389 struct weston_touch_move_grab *move =
1390 (struct weston_touch_move_grab *) container_of(
1391 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001392
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001393 if (grab->touch->seat->num_tp == 0) {
1394 shell_touch_grab_end(&move->base);
1395 free(move);
1396 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001397}
1398
1399static void
1400touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1401 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1402{
1403 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1404 struct shell_surface *shsurf = move->base.shsurf;
1405 struct weston_surface *es;
1406 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1407 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1408
1409 if (!shsurf)
1410 return;
1411
1412 es = shsurf->surface;
1413
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 weston_view_configure(shsurf->view, dx, dy,
1415 shsurf->view->geometry.width,
1416 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001417
1418 weston_compositor_schedule_repaint(es->compositor);
1419}
1420
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001421static void
1422touch_move_grab_cancel(struct weston_touch_grab *grab)
1423{
1424 struct weston_touch_move_grab *move =
1425 (struct weston_touch_move_grab *) container_of(
1426 grab, struct shell_touch_grab, grab);
1427
1428 shell_touch_grab_end(&move->base);
1429 free(move);
1430}
1431
Rusty Lynch1084da52013-08-15 09:10:08 -07001432static const struct weston_touch_grab_interface touch_move_grab_interface = {
1433 touch_move_grab_down,
1434 touch_move_grab_up,
1435 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001436 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001437};
1438
1439static int
1440surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1441{
1442 struct weston_touch_move_grab *move;
1443
1444 if (!shsurf)
1445 return -1;
1446
1447 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1448 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001449 if (shsurf->grabbed)
1450 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001451
1452 move = malloc(sizeof *move);
1453 if (!move)
1454 return -1;
1455
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001457 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001459 seat->touch->grab_y;
1460
1461 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1462 seat->touch);
1463
1464 return 0;
1465}
1466
1467static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001468noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001469{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001470}
1471
1472static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001473move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1474 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001475{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001476 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001477 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001478 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001479 int dx, dy;
1480
1481 weston_pointer_move(pointer, x, y);
1482 dx = wl_fixed_to_int(pointer->x + move->dx);
1483 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001484
1485 if (!shsurf)
1486 return;
1487
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488 weston_view_configure(shsurf->view, dx, dy,
1489 shsurf->view->geometry.width,
1490 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001491
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001493}
1494
1495static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001496move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001497 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001498{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001499 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1500 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001501 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001502 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001503
Daniel Stone4dbadb12012-05-30 16:31:51 +01001504 if (pointer->button_count == 0 &&
1505 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001506 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001507 free(grab);
1508 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001509}
1510
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001511static void
1512move_grab_cancel(struct weston_pointer_grab *grab)
1513{
1514 struct shell_grab *shell_grab =
1515 container_of(grab, struct shell_grab, grab);
1516
1517 shell_grab_end(shell_grab);
1518 free(grab);
1519}
1520
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001521static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001522 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001523 move_grab_motion,
1524 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001525 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001526};
1527
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001528static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001529surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001530{
1531 struct weston_move_grab *move;
1532
1533 if (!shsurf)
1534 return -1;
1535
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001536 if (shsurf->grabbed)
1537 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001538 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1539 return 0;
1540
1541 move = malloc(sizeof *move);
1542 if (!move)
1543 return -1;
1544
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001546 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001548 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001549
1550 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001551 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001552
1553 return 0;
1554}
1555
1556static void
1557shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1558 struct wl_resource *seat_resource, uint32_t serial)
1559{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001560 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001561 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001562 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001563
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001564 if (seat->pointer &&
1565 seat->pointer->button_count > 0 &&
1566 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001567 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001568 if ((surface == shsurf->surface) &&
1569 (surface_move(shsurf, seat) < 0))
1570 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001571 } else if (seat->touch &&
1572 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001574 if ((surface == shsurf->surface) &&
1575 (surface_touch_move(shsurf, seat) < 0))
1576 wl_resource_post_no_memory(resource);
1577 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001578}
1579
1580struct weston_resize_grab {
1581 struct shell_grab base;
1582 uint32_t edges;
1583 int32_t width, height;
1584};
1585
1586static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001587resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1588 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001589{
1590 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001591 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001592 struct shell_surface *shsurf = resize->base.shsurf;
1593 int32_t width, height;
1594 wl_fixed_t from_x, from_y;
1595 wl_fixed_t to_x, to_y;
1596
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001597 weston_pointer_move(pointer, x, y);
1598
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001599 if (!shsurf)
1600 return;
1601
Jason Ekstranda7af7042013-10-12 22:38:11 -05001602 weston_view_from_global_fixed(shsurf->view,
1603 pointer->grab_x, pointer->grab_y,
1604 &from_x, &from_y);
1605 weston_view_from_global_fixed(shsurf->view,
1606 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001607
1608 width = resize->width;
1609 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1610 width += wl_fixed_to_int(from_x - to_x);
1611 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1612 width += wl_fixed_to_int(to_x - from_x);
1613 }
1614
1615 height = resize->height;
1616 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1617 height += wl_fixed_to_int(from_y - to_y);
1618 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1619 height += wl_fixed_to_int(to_y - from_y);
1620 }
1621
1622 shsurf->client->send_configure(shsurf->surface,
1623 resize->edges, width, height);
1624}
1625
1626static void
1627send_configure(struct weston_surface *surface,
1628 uint32_t edges, int32_t width, int32_t height)
1629{
1630 struct shell_surface *shsurf = get_shell_surface(surface);
1631
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001632 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633 edges, width, height);
1634}
1635
1636static const struct weston_shell_client shell_client = {
1637 send_configure
1638};
1639
1640static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001641resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001642 uint32_t time, uint32_t button, uint32_t state_w)
1643{
1644 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001645 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001646 enum wl_pointer_button_state state = state_w;
1647
1648 if (pointer->button_count == 0 &&
1649 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1650 shell_grab_end(&resize->base);
1651 free(grab);
1652 }
1653}
1654
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001655static void
1656resize_grab_cancel(struct weston_pointer_grab *grab)
1657{
1658 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1659
1660 shell_grab_end(&resize->base);
1661 free(grab);
1662}
1663
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001664static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001665 noop_grab_focus,
1666 resize_grab_motion,
1667 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001668 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001669};
1670
Giulio Camuffob8366642013-04-25 13:57:46 +03001671/*
1672 * Returns the bounding box of a surface and all its sub-surfaces,
1673 * in the surface coordinates system. */
1674static void
1675surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1676 int32_t *y, int32_t *w, int32_t *h) {
1677 pixman_region32_t region;
1678 pixman_box32_t *box;
1679 struct weston_subsurface *subsurface;
1680
1681 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 surface->width,
1683 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001684
1685 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1686 pixman_region32_union_rect(&region, &region,
1687 subsurface->position.x,
1688 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001689 subsurface->surface->width,
1690 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001691 }
1692
1693 box = pixman_region32_extents(&region);
1694 if (x)
1695 *x = box->x1;
1696 if (y)
1697 *y = box->y1;
1698 if (w)
1699 *w = box->x2 - box->x1;
1700 if (h)
1701 *h = box->y2 - box->y1;
1702
1703 pixman_region32_fini(&region);
1704}
1705
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001706static int
1707surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001708 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709{
1710 struct weston_resize_grab *resize;
1711
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001712 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1713 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001714 return 0;
1715
1716 if (edges == 0 || edges > 15 ||
1717 (edges & 3) == 3 || (edges & 12) == 12)
1718 return 0;
1719
1720 resize = malloc(sizeof *resize);
1721 if (!resize)
1722 return -1;
1723
1724 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001725 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1726 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727
1728 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001729 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001730
1731 return 0;
1732}
1733
1734static void
1735shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1736 struct wl_resource *seat_resource, uint32_t serial,
1737 uint32_t edges)
1738{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001739 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001740 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001741 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001742
1743 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1744 return;
1745
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001747 if (seat->pointer->button_count == 0 ||
1748 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001749 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001750 return;
1751
Kristian Høgsberge3148752013-05-06 23:19:49 -04001752 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001753 wl_resource_post_no_memory(resource);
1754}
1755
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001756static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001757end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1758
1759static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001760busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001761{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001762 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001763 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001764 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001765 wl_fixed_t sx, sy;
1766
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 view = weston_compositor_pick_view(pointer->seat->compositor,
1768 pointer->x, pointer->y,
1769 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001770
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001772 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001773}
1774
1775static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001776busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1777 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001778{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001779 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001780}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001781
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001782static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001783busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001784 uint32_t time, uint32_t button, uint32_t state)
1785{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001786 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001787 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001788 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001789
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001790 if (shsurf && button == BTN_LEFT && state) {
1791 activate(shsurf->shell, shsurf->surface, seat);
1792 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001793 } else if (shsurf && button == BTN_RIGHT && state) {
1794 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001795 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001796 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001797}
1798
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001799static void
1800busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1801{
1802 struct shell_grab *grab = (struct shell_grab *) base;
1803
1804 shell_grab_end(grab);
1805 free(grab);
1806}
1807
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001808static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001809 busy_cursor_grab_focus,
1810 busy_cursor_grab_motion,
1811 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001812 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001813};
1814
1815static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001816set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001817{
1818 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001819
1820 grab = malloc(sizeof *grab);
1821 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001822 return;
1823
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001824 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1825 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001826}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001827
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001828static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001829end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001830{
1831 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1832
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001833 if (grab->grab.interface == &busy_cursor_grab_interface &&
1834 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001835 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001836 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001837 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001838}
1839
Scott Moreau9521d5e2012-04-19 13:06:17 -06001840static void
1841ping_timer_destroy(struct shell_surface *shsurf)
1842{
1843 if (!shsurf || !shsurf->ping_timer)
1844 return;
1845
1846 if (shsurf->ping_timer->source)
1847 wl_event_source_remove(shsurf->ping_timer->source);
1848
1849 free(shsurf->ping_timer);
1850 shsurf->ping_timer = NULL;
1851}
1852
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001853static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001854ping_timeout_handler(void *data)
1855{
1856 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001857 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001858
Scott Moreau9521d5e2012-04-19 13:06:17 -06001859 /* Client is not responding */
1860 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001861
1862 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001863 if (seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001864 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001865
1866 return 1;
1867}
1868
1869static void
1870ping_handler(struct weston_surface *surface, uint32_t serial)
1871{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001872 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001873 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001874 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001875
1876 if (!shsurf)
1877 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001878 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001879 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001880
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001881 if (shsurf->surface == shsurf->shell->grab_surface)
1882 return;
1883
Scott Moreauff1db4a2012-04-17 19:06:18 -06001884 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001885 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001886 if (!shsurf->ping_timer)
1887 return;
1888
1889 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001890 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1891 shsurf->ping_timer->source =
1892 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1893 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1894
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001895 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001896 }
1897}
1898
1899static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001900handle_pointer_focus(struct wl_listener *listener, void *data)
1901{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001902 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001903 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001904 struct weston_compositor *compositor;
1905 struct shell_surface *shsurf;
1906 uint32_t serial;
1907
Jason Ekstranda7af7042013-10-12 22:38:11 -05001908 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001909 return;
1910
Jason Ekstranda7af7042013-10-12 22:38:11 -05001911 compositor = view->surface->compositor;
1912 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001913
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001914 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001915 set_busy_cursor(shsurf, pointer);
1916 } else {
1917 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001918 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001919 }
1920}
1921
1922static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001923create_pointer_focus_listener(struct weston_seat *seat)
1924{
1925 struct wl_listener *listener;
1926
Kristian Høgsberge3148752013-05-06 23:19:49 -04001927 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001928 return;
1929
1930 listener = malloc(sizeof *listener);
1931 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001932 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001933}
1934
1935static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001936shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1937 uint32_t serial)
1938{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001939 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001940 struct weston_seat *seat;
1941 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001942
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001943 if (shsurf->ping_timer == NULL)
1944 /* Just ignore unsolicited pong. */
1945 return;
1946
Scott Moreauff1db4a2012-04-17 19:06:18 -06001947 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001948 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001949 wl_list_for_each(seat, &ec->seat_list, link) {
1950 if(seat->pointer)
1951 end_busy_cursor(shsurf, seat->pointer);
1952 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001953 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001954 }
1955}
1956
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001957static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001958set_title(struct shell_surface *shsurf, const char *title)
1959{
1960 free(shsurf->title);
1961 shsurf->title = strdup(title);
1962}
1963
1964static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001965shell_surface_set_title(struct wl_client *client,
1966 struct wl_resource *resource, const char *title)
1967{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001968 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001969
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001970 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001971}
1972
1973static void
1974shell_surface_set_class(struct wl_client *client,
1975 struct wl_resource *resource, const char *class)
1976{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001977 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001978
1979 free(shsurf->class);
1980 shsurf->class = strdup(class);
1981}
1982
Alex Wu4539b082012-03-01 12:57:46 +08001983static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001984restore_output_mode(struct weston_output *output)
1985{
Hardening57388e42013-09-18 23:56:36 +02001986 if (output->current_mode != output->original_mode ||
1987 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001988 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001989 output->original_mode,
1990 output->original_scale,
1991 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001992}
1993
1994static void
1995restore_all_output_modes(struct weston_compositor *compositor)
1996{
1997 struct weston_output *output;
1998
1999 wl_list_for_each(output, &compositor->output_list, link)
2000 restore_output_mode(output);
2001}
2002
2003static void
Alex Wu4539b082012-03-01 12:57:46 +08002004shell_unset_fullscreen(struct shell_surface *shsurf)
2005{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002006 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08002007 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08002008 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2009 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002010 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002011 }
Alex Wu4539b082012-03-01 12:57:46 +08002012 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2013 shsurf->fullscreen.framerate = 0;
2014 wl_list_remove(&shsurf->fullscreen.transform.link);
2015 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016 if (shsurf->fullscreen.black_view)
2017 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2018 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002019 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020 weston_view_set_position(shsurf->view,
2021 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002022 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002024 &shsurf->rotation.transform.link);
2025 shsurf->saved_rotation_valid = false;
2026 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002027
2028 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002029 wl_list_remove(&shsurf->view->layer_link);
2030 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08002031}
2032
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002033static void
2034shell_unset_maximized(struct shell_surface *shsurf)
2035{
2036 struct workspace *ws;
2037 /* undo all maximized things here */
2038 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039 weston_view_set_position(shsurf->view,
2040 shsurf->saved_x,
2041 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002042
2043 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044 wl_list_insert(&shsurf->view->geometry.transformation_list,
2045 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002046 shsurf->saved_rotation_valid = false;
2047 }
2048
2049 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002050 wl_list_remove(&shsurf->view->layer_link);
2051 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002052}
2053
Pekka Paalanen98262232011-12-01 10:42:22 +02002054static int
2055reset_shell_surface_type(struct shell_surface *surface)
2056{
2057 switch (surface->type) {
2058 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08002059 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002060 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002061 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002062 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002063 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002064 case SHELL_SURFACE_NONE:
2065 case SHELL_SURFACE_TOPLEVEL:
2066 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002067 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002068 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02002069 break;
2070 }
2071
2072 surface->type = SHELL_SURFACE_NONE;
2073 return 0;
2074}
2075
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002076static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002077set_surface_type(struct shell_surface *shsurf)
2078{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002079 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002081
2082 reset_shell_surface_type(shsurf);
2083
2084 shsurf->type = shsurf->next_type;
2085 shsurf->next_type = SHELL_SURFACE_NONE;
2086
2087 switch (shsurf->type) {
2088 case SHELL_SURFACE_TOPLEVEL:
2089 break;
2090 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002091 if (pev)
2092 weston_view_set_position(shsurf->view,
2093 pev->geometry.x + shsurf->transient.x,
2094 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002095 break;
2096
2097 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002098 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002099 shsurf->saved_x = shsurf->view->geometry.x;
2100 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002101 shsurf->saved_position_valid = true;
2102
2103 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2104 wl_list_remove(&shsurf->rotation.transform.link);
2105 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002107 shsurf->saved_rotation_valid = true;
2108 }
2109 break;
2110
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002111 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112 weston_view_set_position(shsurf->view, shsurf->transient.x,
2113 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002114 break;
2115
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002116 default:
2117 break;
2118 }
2119}
2120
2121static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002122set_toplevel(struct shell_surface *shsurf)
2123{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002124 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002125}
2126
2127static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002128shell_surface_set_toplevel(struct wl_client *client,
2129 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002130{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002131 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002132
Tiago Vignattibc052c92012-04-19 16:18:18 +03002133 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002134}
2135
2136static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04002137set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002138 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04002139{
2140 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002141 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002142 shsurf->transient.x = x;
2143 shsurf->transient.y = y;
2144 shsurf->transient.flags = flags;
2145 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
2146}
2147
2148static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002149shell_surface_set_transient(struct wl_client *client,
2150 struct wl_resource *resource,
2151 struct wl_resource *parent_resource,
2152 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002153{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002154 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002155 struct weston_surface *parent =
2156 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002157
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002158 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002159}
2160
Tiago Vignattibe143262012-04-16 17:31:41 +03002161static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002162shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002163{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002164 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002165}
2166
2167static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002168get_output_panel_height(struct desktop_shell *shell,
2169 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08002170{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08002172 int panel_height = 0;
2173
2174 if (!output)
2175 return 0;
2176
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2178 if (view->surface->output == output) {
2179 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08002180 break;
2181 }
2182 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002183
Juan Zhao96879df2012-02-07 08:45:41 +08002184 return panel_height;
2185}
2186
2187static void
2188shell_surface_set_maximized(struct wl_client *client,
2189 struct wl_resource *resource,
2190 struct wl_resource *output_resource )
2191{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002192 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08002193 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03002194 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08002195 uint32_t edges = 0, panel_height = 0;
2196
2197 /* get the default output, if the client set it as NULL
2198 check whether the ouput is available */
2199 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002200 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002201 else if (es->output)
2202 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002203 else
2204 shsurf->output = get_default_output(es->compositor);
2205
Tiago Vignattibe143262012-04-16 17:31:41 +03002206 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01002207 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002208 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002209
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002210 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002211 shsurf->output->width,
2212 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002213
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002214 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002215}
2216
Alex Wu21858432012-04-01 20:13:08 +08002217static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002218black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height);
Alex Wu21858432012-04-01 20:13:08 +08002219
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002221create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002222 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002223 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002224{
2225 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002227
2228 surface = weston_surface_create(ec);
2229 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002230 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002231 return NULL;
2232 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002233 view = weston_view_create(surface);
2234 if (surface == NULL) {
2235 weston_log("no memory\n");
2236 weston_surface_destroy(surface);
2237 return NULL;
2238 }
Alex Wu4539b082012-03-01 12:57:46 +08002239
Alex Wu21858432012-04-01 20:13:08 +08002240 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002241 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002242 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002243 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002244 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002245 pixman_region32_fini(&surface->input);
2246 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002247
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248 weston_view_configure(view, x, y, w, h);
2249
2250 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002251}
2252
2253/* Create black surface and append it to the associated fullscreen surface.
2254 * Handle size dismatch and positioning according to the method. */
2255static void
2256shell_configure_fullscreen(struct shell_surface *shsurf)
2257{
2258 struct weston_output *output = shsurf->fullscreen_output;
2259 struct weston_surface *surface = shsurf->surface;
2260 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002261 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002262 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002263
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002264 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2265 restore_output_mode(output);
2266
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267 if (!shsurf->fullscreen.black_view)
2268 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002269 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002270 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002271 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002272 output->width,
2273 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002274
Jason Ekstranda7af7042013-10-12 22:38:11 -05002275 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2276 wl_list_insert(&shsurf->view->layer_link,
2277 &shsurf->fullscreen.black_view->layer_link);
2278 shsurf->fullscreen.black_view->surface->output = output;
2279 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002280
Jason Ekstranda7af7042013-10-12 22:38:11 -05002281 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002282 &surf_width, &surf_height);
2283
Alex Wu4539b082012-03-01 12:57:46 +08002284 switch (shsurf->fullscreen.type) {
2285 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002286 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002288 break;
2289 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002290 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002291 if (output->width == surf_width &&
2292 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 weston_view_set_position(shsurf->view,
2294 output->x - surf_x,
2295 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002296 break;
2297 }
2298
Alex Wu4539b082012-03-01 12:57:46 +08002299 matrix = &shsurf->fullscreen.transform.matrix;
2300 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002301
Scott Moreau1bad5db2012-08-18 01:04:05 -06002302 output_aspect = (float) output->width /
2303 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002304 /* XXX: Use surf_width and surf_height here? */
2305 surface_aspect = (float) surface->width /
2306 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002307 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002308 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002309 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002310 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002311 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002312 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002313
Alex Wu4539b082012-03-01 12:57:46 +08002314 weston_matrix_scale(matrix, scale, scale, 1);
2315 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002316 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002317 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002318 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2319 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002321
Alex Wu4539b082012-03-01 12:57:46 +08002322 break;
2323 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002324 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002325 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02002326 surf_width * surface->buffer_scale,
2327 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002328 shsurf->fullscreen.framerate};
2329
Hardening57388e42013-09-18 23:56:36 +02002330 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
2331 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002332 weston_view_set_position(shsurf->view,
2333 output->x - surf_x,
2334 output->y - surf_y);
2335 weston_view_configure(shsurf->fullscreen.black_view,
2336 output->x - surf_x,
2337 output->y - surf_y,
2338 output->width,
2339 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002340 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002341 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002342 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002343 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002344 }
Alex Wubd3354b2012-04-17 17:20:49 +08002345 }
Alex Wu4539b082012-03-01 12:57:46 +08002346 break;
2347 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002348 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002349 break;
2350 default:
2351 break;
2352 }
2353}
2354
2355/* make the fullscreen and black surface at the top */
2356static void
2357shell_stack_fullscreen(struct shell_surface *shsurf)
2358{
Alex Wubd3354b2012-04-17 17:20:49 +08002359 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002360 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002361
Jason Ekstranda7af7042013-10-12 22:38:11 -05002362 wl_list_remove(&shsurf->view->layer_link);
2363 wl_list_insert(&shell->fullscreen_layer.view_list,
2364 &shsurf->view->layer_link);
2365 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002366
Jason Ekstranda7af7042013-10-12 22:38:11 -05002367 if (!shsurf->fullscreen.black_view)
2368 shsurf->fullscreen.black_view =
2369 create_black_surface(shsurf->surface->compositor,
2370 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002371 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002372 output->width,
2373 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002374
Jason Ekstranda7af7042013-10-12 22:38:11 -05002375 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2376 wl_list_insert(&shsurf->view->layer_link,
2377 &shsurf->fullscreen.black_view->layer_link);
2378 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002379}
2380
2381static void
2382shell_map_fullscreen(struct shell_surface *shsurf)
2383{
Alex Wu4539b082012-03-01 12:57:46 +08002384 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002385 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002386}
2387
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002388static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002389set_fullscreen(struct shell_surface *shsurf,
2390 uint32_t method,
2391 uint32_t framerate,
2392 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002393{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002394 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002395
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002396 if (output)
2397 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002398 else if (es->output)
2399 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002400 else
2401 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002402
Alex Wu4539b082012-03-01 12:57:46 +08002403 shsurf->fullscreen_output = shsurf->output;
2404 shsurf->fullscreen.type = method;
2405 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002406 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002407
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002408 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002409 shsurf->output->width,
2410 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002411}
2412
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002413static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002414shell_surface_set_fullscreen(struct wl_client *client,
2415 struct wl_resource *resource,
2416 uint32_t method,
2417 uint32_t framerate,
2418 struct wl_resource *output_resource)
2419{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002420 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002421 struct weston_output *output;
2422
2423 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002424 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002425 else
2426 output = NULL;
2427
2428 set_fullscreen(shsurf, method, framerate, output);
2429}
2430
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002431static void
2432set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2433{
2434 /* XXX: using the same fields for transient type */
2435 shsurf->transient.x = x;
2436 shsurf->transient.y = y;
2437 shsurf->transient.flags = flags;
2438 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2439}
2440
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002441static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002442
2443static void
2444destroy_shell_seat(struct wl_listener *listener, void *data)
2445{
2446 struct shell_seat *shseat =
2447 container_of(listener,
2448 struct shell_seat, seat_destroy_listener);
2449 struct shell_surface *shsurf, *prev = NULL;
2450
2451 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002452 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002453 shseat->popup_grab.client = NULL;
2454
2455 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2456 shsurf->popup.shseat = NULL;
2457 if (prev) {
2458 wl_list_init(&prev->popup.grab_link);
2459 }
2460 prev = shsurf;
2461 }
2462 wl_list_init(&prev->popup.grab_link);
2463 }
2464
2465 wl_list_remove(&shseat->seat_destroy_listener.link);
2466 free(shseat);
2467}
2468
2469static struct shell_seat *
2470create_shell_seat(struct weston_seat *seat)
2471{
2472 struct shell_seat *shseat;
2473
2474 shseat = calloc(1, sizeof *shseat);
2475 if (!shseat) {
2476 weston_log("no memory to allocate shell seat\n");
2477 return NULL;
2478 }
2479
2480 shseat->seat = seat;
2481 wl_list_init(&shseat->popup_grab.surfaces_list);
2482
2483 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2484 wl_signal_add(&seat->destroy_signal,
2485 &shseat->seat_destroy_listener);
2486
2487 return shseat;
2488}
2489
2490static struct shell_seat *
2491get_shell_seat(struct weston_seat *seat)
2492{
2493 struct wl_listener *listener;
2494
2495 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2496 if (listener == NULL)
2497 return create_shell_seat(seat);
2498
2499 return container_of(listener,
2500 struct shell_seat, seat_destroy_listener);
2501}
2502
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002503static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002504popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002505{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002506 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002508 struct shell_seat *shseat =
2509 container_of(grab, struct shell_seat, popup_grab.grab);
2510 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002511 wl_fixed_t sx, sy;
2512
Jason Ekstranda7af7042013-10-12 22:38:11 -05002513 view = weston_compositor_pick_view(pointer->seat->compositor,
2514 pointer->x, pointer->y,
2515 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002516
Jason Ekstranda7af7042013-10-12 22:38:11 -05002517 if (view && view->surface->resource &&
2518 wl_resource_get_client(view->surface->resource) == client) {
2519 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002520 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002521 weston_pointer_set_focus(pointer, NULL,
2522 wl_fixed_from_int(0),
2523 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002524 }
2525}
2526
2527static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002528popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2529 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002530{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002531 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002532 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002533 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002534
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002535 weston_pointer_move(pointer, x, y);
2536
Neil Roberts96d790e2013-09-19 17:32:00 +01002537 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002538 weston_view_from_global_fixed(pointer->focus,
2539 pointer->x, pointer->y,
2540 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002541 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002542 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002543}
2544
2545static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002546popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002547 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002548{
2549 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002550 struct shell_seat *shseat =
2551 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002552 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002553 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002554 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002555 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002556
Neil Roberts96d790e2013-09-19 17:32:00 +01002557 resource_list = &grab->pointer->focus_resource_list;
2558 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002559 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002560 wl_resource_for_each(resource, resource_list) {
2561 wl_pointer_send_button(resource, serial,
2562 time, button, state);
2563 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002564 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002565 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002566 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002567 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002568 }
2569
Daniel Stone4dbadb12012-05-30 16:31:51 +01002570 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002571 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002572}
2573
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002574static void
2575popup_grab_cancel(struct weston_pointer_grab *grab)
2576{
2577 popup_grab_end(grab->pointer);
2578}
2579
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002580static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002581 popup_grab_focus,
2582 popup_grab_motion,
2583 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002584 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002585};
2586
2587static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002588popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002589{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002590 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002591 struct shell_seat *shseat =
2592 container_of(grab, struct shell_seat, popup_grab.grab);
2593 struct shell_surface *shsurf;
2594 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002595
2596 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002597 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002598 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002599 shseat->popup_grab.grab.interface = NULL;
2600 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002601 /* Send the popup_done event to all the popups open */
2602 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002603 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002604 shsurf->popup.shseat = NULL;
2605 if (prev) {
2606 wl_list_init(&prev->popup.grab_link);
2607 }
2608 prev = shsurf;
2609 }
2610 wl_list_init(&prev->popup.grab_link);
2611 wl_list_init(&shseat->popup_grab.surfaces_list);
2612 }
2613}
2614
2615static void
2616add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2617{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002618 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002619
2620 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002621 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002622 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002623 /* We must make sure here that this popup was opened after
2624 * a mouse press, and not just by moving around with other
2625 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002626 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002627 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002628
Rob Bradforddfe31052013-06-26 19:49:11 +01002629 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002630 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002631 } else {
2632 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002633 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002634}
2635
2636static void
2637remove_popup_grab(struct shell_surface *shsurf)
2638{
2639 struct shell_seat *shseat = shsurf->popup.shseat;
2640
2641 wl_list_remove(&shsurf->popup.grab_link);
2642 wl_list_init(&shsurf->popup.grab_link);
2643 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002644 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002645 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002646 }
2647}
2648
2649static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002650shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002651{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002652 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002653 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002654
Jason Ekstranda7af7042013-10-12 22:38:11 -05002655 shsurf->surface->output = parent_view->output;
2656 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002657
Jason Ekstranda7af7042013-10-12 22:38:11 -05002658 weston_view_set_transform_parent(shsurf->view, parent_view);
2659 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2660 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002661
Kristian Høgsberge3148752013-05-06 23:19:49 -04002662 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002663 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002664 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002665 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002666 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002667 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002668}
2669
2670static void
2671shell_surface_set_popup(struct wl_client *client,
2672 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002673 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002674 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002675 struct wl_resource *parent_resource,
2676 int32_t x, int32_t y, uint32_t flags)
2677{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002678 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002679
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002680 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002681 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002682 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002683 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002684 shsurf->popup.x = x;
2685 shsurf->popup.y = y;
2686}
2687
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002688static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002689 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002690 shell_surface_move,
2691 shell_surface_resize,
2692 shell_surface_set_toplevel,
2693 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002694 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002695 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002696 shell_surface_set_maximized,
2697 shell_surface_set_title,
2698 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002699};
2700
2701static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002702destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002703{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002704 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2705
Giulio Camuffo5085a752013-03-25 21:42:45 +01002706 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2707 remove_popup_grab(shsurf);
2708 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002709
Alex Wubd3354b2012-04-17 17:20:49 +08002710 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002711 shell_surface_is_top_fullscreen(shsurf))
2712 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002713
Jason Ekstranda7af7042013-10-12 22:38:11 -05002714 if (shsurf->fullscreen.black_view)
2715 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002716
Alex Wubd3354b2012-04-17 17:20:49 +08002717 /* As destroy_resource() use wl_list_for_each_safe(),
2718 * we can always remove the listener.
2719 */
2720 wl_list_remove(&shsurf->surface_destroy_listener.link);
2721 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002722 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002723 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002724
Jason Ekstranda7af7042013-10-12 22:38:11 -05002725 weston_view_destroy(shsurf->view);
2726
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002727 wl_list_remove(&shsurf->link);
2728 free(shsurf);
2729}
2730
2731static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002732shell_destroy_shell_surface(struct wl_resource *resource)
2733{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002734 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002735
2736 destroy_shell_surface(shsurf);
2737}
2738
2739static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002740shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002741{
2742 struct shell_surface *shsurf = container_of(listener,
2743 struct shell_surface,
2744 surface_destroy_listener);
2745
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002746 if (shsurf->resource)
2747 wl_resource_destroy(shsurf->resource);
2748 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002749 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002750}
2751
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002752static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002753shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002754
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002755static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002756get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002757{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002758 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002759 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002760 else
2761 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002762}
2763
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002764static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002765create_shell_surface(void *shell, struct weston_surface *surface,
2766 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002767{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002768 struct shell_surface *shsurf;
2769
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002770 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002771 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002772 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002773 }
2774
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002775 shsurf = calloc(1, sizeof *shsurf);
2776 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002777 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002778 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002779 }
2780
Jason Ekstranda7af7042013-10-12 22:38:11 -05002781 shsurf->view = weston_view_create(surface);
2782 if (!shsurf->view) {
2783 weston_log("no memory to allocate shell surface\n");
2784 free(shsurf);
2785 return NULL;
2786 }
2787
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002788 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002789 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002790
Tiago Vignattibc052c92012-04-19 16:18:18 +03002791 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002792 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002793 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002794 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002795 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002796 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2797 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002798 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002799 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002800 wl_list_init(&shsurf->fullscreen.transform.link);
2801
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002802 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002803 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002804 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002805 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002806
2807 /* init link so its safe to always remove it in destroy_shell_surface */
2808 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002809 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002810
Pekka Paalanen460099f2012-01-20 16:48:25 +02002811 /* empty when not in use */
2812 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002813 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002814
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002815 wl_list_init(&shsurf->workspace_transform.link);
2816
Pekka Paalanen98262232011-12-01 10:42:22 +02002817 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002818 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002819
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002820 shsurf->client = client;
2821
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002822 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002823}
2824
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825static struct weston_view *
2826get_primary_view(void *shell, struct shell_surface *shsurf)
2827{
2828 return shsurf->view;
2829}
2830
Tiago Vignattibc052c92012-04-19 16:18:18 +03002831static void
2832shell_get_shell_surface(struct wl_client *client,
2833 struct wl_resource *resource,
2834 uint32_t id,
2835 struct wl_resource *surface_resource)
2836{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002837 struct weston_surface *surface =
2838 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002839 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002840 struct shell_surface *shsurf;
2841
2842 if (get_shell_surface(surface)) {
2843 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002844 WL_DISPLAY_ERROR_INVALID_OBJECT,
2845 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002846 return;
2847 }
2848
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002849 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002850 if (!shsurf) {
2851 wl_resource_post_error(surface_resource,
2852 WL_DISPLAY_ERROR_INVALID_OBJECT,
2853 "surface->configure already set");
2854 return;
2855 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002856
Jason Ekstranda85118c2013-06-27 20:17:02 -05002857 shsurf->resource =
2858 wl_resource_create(client,
2859 &wl_shell_surface_interface, 1, id);
2860 wl_resource_set_implementation(shsurf->resource,
2861 &shell_surface_implementation,
2862 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002863}
2864
2865static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002866 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002867};
2868
Kristian Høgsberg07937562011-04-12 17:25:42 -04002869static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002870shell_fade(struct desktop_shell *shell, enum fade_type type);
2871
2872static int
2873screensaver_timeout(void *data)
2874{
2875 struct desktop_shell *shell = data;
2876
2877 shell_fade(shell, FADE_OUT);
2878
2879 return 1;
2880}
2881
2882static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002883handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002884{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002885 struct desktop_shell *shell =
2886 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002887
Pekka Paalanen18027e52011-12-02 16:31:49 +02002888 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002889
2890 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002891 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002892}
2893
2894static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002895launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002896{
2897 if (shell->screensaver.binding)
2898 return;
2899
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002900 if (!shell->screensaver.path) {
2901 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002902 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002903 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002904
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002905 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002906 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002907 return;
2908 }
2909
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002910 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002911 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002912 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002913 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002914}
2915
2916static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002917terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002918{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002919 if (shell->screensaver.process.pid == 0)
2920 return;
2921
2922 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002923}
2924
2925static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002926configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002927{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002929
Giulio Camuffo184df502013-02-21 11:29:21 +01002930 if (width == 0)
2931 return;
2932
Jason Ekstranda7af7042013-10-12 22:38:11 -05002933 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2934 if (v->output == ev->output && v != ev) {
2935 weston_view_unmap(v);
2936 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002937 }
2938 }
2939
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002941
Jason Ekstranda7af7042013-10-12 22:38:11 -05002942 if (wl_list_empty(&ev->layer_link)) {
2943 wl_list_insert(&layer->view_list, &ev->layer_link);
2944 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002945 }
2946}
2947
2948static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002949background_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002950{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002951 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002953
Jason Ekstranda7af7042013-10-12 22:38:11 -05002954 view = container_of(es->views.next, struct weston_view, surface_link);
2955
2956 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002957}
2958
2959static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002960desktop_shell_set_background(struct wl_client *client,
2961 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002962 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002963 struct wl_resource *surface_resource)
2964{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002965 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002966 struct weston_surface *surface =
2967 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002968 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002969
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002970 if (surface->configure) {
2971 wl_resource_post_error(surface_resource,
2972 WL_DISPLAY_ERROR_INVALID_OBJECT,
2973 "surface role already assigned");
2974 return;
2975 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002976
Jason Ekstranda7af7042013-10-12 22:38:11 -05002977 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2978 weston_view_destroy(view);
2979 view = weston_view_create(surface);
2980
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002981 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002982 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002983 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002984 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002985 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002986 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002987 surface->output->width,
2988 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002989}
2990
2991static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002992panel_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002993{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002994 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002995 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002996
Jason Ekstranda7af7042013-10-12 22:38:11 -05002997 view = container_of(es->views.next, struct weston_view, surface_link);
2998
2999 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003000}
3001
3002static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003003desktop_shell_set_panel(struct wl_client *client,
3004 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003005 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003006 struct wl_resource *surface_resource)
3007{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003008 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003009 struct weston_surface *surface =
3010 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003011 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003012
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003013 if (surface->configure) {
3014 wl_resource_post_error(surface_resource,
3015 WL_DISPLAY_ERROR_INVALID_OBJECT,
3016 "surface role already assigned");
3017 return;
3018 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003019
Jason Ekstranda7af7042013-10-12 22:38:11 -05003020 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3021 weston_view_destroy(view);
3022 view = weston_view_create(surface);
3023
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003024 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003025 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003026 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003027 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003028 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003029 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003030 surface->output->width,
3031 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003032}
3033
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003034static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003035lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003036{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003037 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003038 struct weston_view *view;
3039
3040 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003041
Giulio Camuffo184df502013-02-21 11:29:21 +01003042 if (width == 0)
3043 return;
3044
Jason Ekstranda7af7042013-10-12 22:38:11 -05003045 surface->width = width;
3046 surface->height = height;
3047 view->geometry.width = width;
3048 view->geometry.height = height;
3049 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003050
3051 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003052 wl_list_insert(&shell->lock_layer.view_list,
3053 &view->layer_link);
3054 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003055 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003056 }
3057}
3058
3059static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003060handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003061{
Tiago Vignattibe143262012-04-16 17:31:41 +03003062 struct desktop_shell *shell =
3063 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003064
Martin Minarik6d118362012-06-07 18:01:59 +02003065 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003066 shell->lock_surface = NULL;
3067}
3068
3069static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003070desktop_shell_set_lock_surface(struct wl_client *client,
3071 struct wl_resource *resource,
3072 struct wl_resource *surface_resource)
3073{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003074 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003075 struct weston_surface *surface =
3076 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003077
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003078 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003079
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003080 if (!shell->locked)
3081 return;
3082
Pekka Paalanen98262232011-12-01 10:42:22 +02003083 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003084
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003085 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003086 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003087 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003088
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003089 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003090 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003091 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003092}
3093
3094static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003095resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003096{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003097 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003098
Pekka Paalanen77346a62011-11-30 16:26:35 +02003099 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003100
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003101 wl_list_remove(&shell->lock_layer.link);
3102 wl_list_insert(&shell->compositor->cursor_layer.link,
3103 &shell->fullscreen_layer.link);
3104 wl_list_insert(&shell->fullscreen_layer.link,
3105 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003106 if (shell->showing_input_panels) {
3107 wl_list_insert(&shell->panel_layer.link,
3108 &shell->input_panel_layer.link);
3109 wl_list_insert(&shell->input_panel_layer.link,
3110 &ws->layer.link);
3111 } else {
3112 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3113 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003114
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003115 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003116
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003117 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003118 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003119 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003120}
3121
3122static void
3123desktop_shell_unlock(struct wl_client *client,
3124 struct wl_resource *resource)
3125{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003126 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003127
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003128 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003129
3130 if (shell->locked)
3131 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003132}
3133
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003134static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003135desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003136 struct wl_resource *resource,
3137 struct wl_resource *surface_resource)
3138{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003139 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003140
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003141 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003142 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003143}
3144
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003145static void
3146desktop_shell_desktop_ready(struct wl_client *client,
3147 struct wl_resource *resource)
3148{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003149 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003150
3151 shell_fade_startup(shell);
3152}
3153
Kristian Høgsberg75840622011-09-06 13:48:16 -04003154static const struct desktop_shell_interface desktop_shell_implementation = {
3155 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003156 desktop_shell_set_panel,
3157 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003158 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003159 desktop_shell_set_grab_surface,
3160 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003161};
3162
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003163static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003164get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003165{
3166 struct shell_surface *shsurf;
3167
3168 shsurf = get_shell_surface(surface);
3169 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003170 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003171 return shsurf->type;
3172}
3173
Kristian Høgsberg75840622011-09-06 13:48:16 -04003174static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003175move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003176{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003177 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003178 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003179 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003180
Pekka Paalanen01388e22013-04-25 13:57:44 +03003181 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003182 if (surface == NULL)
3183 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003184
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003185 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003186 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3187 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003188 return;
3189
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003190 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003191}
3192
3193static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003194touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3195{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003196 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003197 struct weston_surface *surface;
3198 struct shell_surface *shsurf;
3199
3200 surface = weston_surface_get_main_surface(focus);
3201 if (surface == NULL)
3202 return;
3203
3204 shsurf = get_shell_surface(surface);
3205 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3206 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3207 return;
3208
3209 surface_touch_move(shsurf, (struct weston_seat *) seat);
3210}
3211
3212static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003213resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003214{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003215 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003216 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003217 uint32_t edges = 0;
3218 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003219 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003220
Pekka Paalanen01388e22013-04-25 13:57:44 +03003221 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003222 if (surface == NULL)
3223 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003224
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003225 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003226 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3227 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003228 return;
3229
Jason Ekstranda7af7042013-10-12 22:38:11 -05003230 weston_view_from_global(shsurf->view,
3231 wl_fixed_to_int(seat->pointer->grab_x),
3232 wl_fixed_to_int(seat->pointer->grab_y),
3233 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003234
Jason Ekstranda7af7042013-10-12 22:38:11 -05003235 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003236 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003237 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003238 edges |= 0;
3239 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003240 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003241
Jason Ekstranda7af7042013-10-12 22:38:11 -05003242 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003243 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003244 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003245 edges |= 0;
3246 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003247 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003248
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003249 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003250}
3251
3252static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003253surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003254 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003255{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003256 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003257 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003258 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003259 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003260
Pekka Paalanen01388e22013-04-25 13:57:44 +03003261 /* XXX: broken for windows containing sub-surfaces */
3262 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003263 if (surface == NULL)
3264 return;
3265
3266 shsurf = get_shell_surface(surface);
3267 if (!shsurf)
3268 return;
3269
Jason Ekstranda7af7042013-10-12 22:38:11 -05003270 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003271
Jason Ekstranda7af7042013-10-12 22:38:11 -05003272 if (shsurf->view->alpha > 1.0)
3273 shsurf->view->alpha = 1.0;
3274 if (shsurf->view->alpha < step)
3275 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003276
Jason Ekstranda7af7042013-10-12 22:38:11 -05003277 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003278 weston_surface_damage(surface);
3279}
3280
3281static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003282do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003283 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003284{
Daniel Stone37816df2012-05-16 18:45:18 +01003285 struct weston_seat *ws = (struct weston_seat *) seat;
3286 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003287 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003288 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003289
3290 wl_list_for_each(output, &compositor->output_list, link) {
3291 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003292 wl_fixed_to_double(seat->pointer->x),
3293 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003294 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003295 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003296 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003297 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003298 increment = -output->zoom.increment;
3299 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003300 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003301 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003302 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003303 else
3304 increment = 0;
3305
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003306 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003307
Scott Moreaue6603982012-06-11 13:07:51 -06003308 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003309 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003310 else if (output->zoom.level > output->zoom.max_level)
3311 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003312 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003313 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003314 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003315
Scott Moreaue6603982012-06-11 13:07:51 -06003316 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003317
Jason Ekstranda7af7042013-10-12 22:38:11 -05003318 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003319 }
3320 }
3321}
3322
Scott Moreauccbf29d2012-02-22 14:21:41 -07003323static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003324zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003325 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003326{
3327 do_zoom(seat, time, 0, axis, value);
3328}
3329
3330static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003331zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003332 void *data)
3333{
3334 do_zoom(seat, time, key, 0, 0);
3335}
3336
3337static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003338terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003339 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003340{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003341 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003342
Daniel Stone325fc2d2012-05-30 16:31:58 +01003343 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003344}
3345
3346static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003347rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3348 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003349{
3350 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003351 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003352 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003353 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003354 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003355
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003356 weston_pointer_move(pointer, x, y);
3357
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003358 if (!shsurf)
3359 return;
3360
Jason Ekstranda7af7042013-10-12 22:38:11 -05003361 cx = 0.5f * shsurf->view->geometry.width;
3362 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003363
Daniel Stone37816df2012-05-16 18:45:18 +01003364 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3365 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003366 r = sqrtf(dx * dx + dy * dy);
3367
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003368 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003369 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003370
3371 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003372 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003373 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003374
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003375 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003376 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003377
3378 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003379 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003380 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003381 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003382 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003383
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003384 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003385 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003386 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003387 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003388 wl_list_init(&shsurf->rotation.transform.link);
3389 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003390 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003391 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003392
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003393 /* We need to adjust the position of the surface
3394 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003395 cposx = shsurf->view->geometry.x + cx;
3396 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003397 dposx = rotate->center.x - cposx;
3398 dposy = rotate->center.y - cposy;
3399 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003400 weston_view_set_position(shsurf->view,
3401 shsurf->view->geometry.x + dposx,
3402 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003403 }
3404
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003405 /* Repaint implies weston_surface_update_transform(), which
3406 * lazily applies the damage due to rotation update.
3407 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003408 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003409}
3410
3411static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003412rotate_grab_button(struct weston_pointer_grab *grab,
3413 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003414{
3415 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003416 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003417 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003418 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003419 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003420
Daniel Stone4dbadb12012-05-30 16:31:51 +01003421 if (pointer->button_count == 0 &&
3422 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003423 if (shsurf)
3424 weston_matrix_multiply(&shsurf->rotation.rotation,
3425 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003426 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003427 free(rotate);
3428 }
3429}
3430
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003431static void
3432rotate_grab_cancel(struct weston_pointer_grab *grab)
3433{
3434 struct rotate_grab *rotate =
3435 container_of(grab, struct rotate_grab, base.grab);
3436
3437 shell_grab_end(&rotate->base);
3438 free(rotate);
3439}
3440
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003441static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003442 noop_grab_focus,
3443 rotate_grab_motion,
3444 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003445 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003446};
3447
3448static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003449surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003450{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003451 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003452 float dx, dy;
3453 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003454
Pekka Paalanen460099f2012-01-20 16:48:25 +02003455 rotate = malloc(sizeof *rotate);
3456 if (!rotate)
3457 return;
3458
Jason Ekstranda7af7042013-10-12 22:38:11 -05003459 weston_view_to_global_float(surface->view,
3460 surface->view->geometry.width * 0.5f,
3461 surface->view->geometry.height * 0.5f,
3462 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003463
Daniel Stone37816df2012-05-16 18:45:18 +01003464 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3465 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003466 r = sqrtf(dx * dx + dy * dy);
3467 if (r > 20.0f) {
3468 struct weston_matrix inverse;
3469
3470 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003471 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003472 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003473
3474 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003475 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003476 } else {
3477 weston_matrix_init(&surface->rotation.rotation);
3478 weston_matrix_init(&rotate->rotation);
3479 }
3480
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003481 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3482 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003483}
3484
3485static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003486rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003487 void *data)
3488{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003489 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003490 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003491 struct shell_surface *surface;
3492
Pekka Paalanen01388e22013-04-25 13:57:44 +03003493 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003494 if (base_surface == NULL)
3495 return;
3496
3497 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003498 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3499 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003500 return;
3501
3502 surface_rotate(surface, seat);
3503}
3504
3505static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003506lower_fullscreen_layer(struct desktop_shell *shell)
3507{
3508 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003509 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003510
3511 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003512 wl_list_for_each_reverse_safe(view, prev,
3513 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003514 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003515 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003516}
3517
3518static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003519activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003520 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003521{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003522 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003523 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003524 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003525 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003526 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003527
Pekka Paalanen01388e22013-04-25 13:57:44 +03003528 main_surface = weston_surface_get_main_surface(es);
3529
Daniel Stone37816df2012-05-16 18:45:18 +01003530 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003531
Jonas Ådahl8538b222012-08-29 22:13:03 +02003532 state = ensure_focus_state(shell, seat);
3533 if (state == NULL)
3534 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003535
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003536 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003537 state->keyboard_focus = es;
3538 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003539 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003540
Pekka Paalanen01388e22013-04-25 13:57:44 +03003541 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003542 case SHELL_SURFACE_FULLSCREEN:
3543 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003544 shell_stack_fullscreen(get_shell_surface(main_surface));
3545 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003546 return;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003547 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003548 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003549 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003550 main_view = get_default_view(main_surface);
3551 if (main_view)
3552 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003553 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003554 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003555
3556 if (shell->focus_animation_type != ANIMATION_NONE)
3557 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Kristian Høgsberg75840622011-09-06 13:48:16 -04003558}
3559
Alex Wu21858432012-04-01 20:13:08 +08003560/* no-op func for checking black surface */
3561static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003562black_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 +08003563{
3564}
3565
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003566static bool
Alex Wu21858432012-04-01 20:13:08 +08003567is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3568{
3569 if (es->configure == black_surface_configure) {
3570 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003571 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003572 return true;
3573 }
3574 return false;
3575}
3576
Kristian Høgsberg75840622011-09-06 13:48:16 -04003577static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003578activate_binding(struct weston_seat *seat,
3579 struct desktop_shell *shell,
3580 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003581{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003582 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003583
Alex Wu9c35e6b2012-03-05 14:13:13 +08003584 if (!focus)
3585 return;
3586
Pekka Paalanen01388e22013-04-25 13:57:44 +03003587 if (is_black_surface(focus, &main_surface))
3588 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003589
Pekka Paalanen01388e22013-04-25 13:57:44 +03003590 main_surface = weston_surface_get_main_surface(focus);
3591 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003592 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003593
Neil Robertsa28c6932013-10-03 16:43:04 +01003594 activate(shell, focus, seat);
3595}
3596
3597static void
3598click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3599 void *data)
3600{
3601 if (seat->pointer->grab != &seat->pointer->default_grab)
3602 return;
3603
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003604 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003605}
3606
3607static void
3608touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3609{
3610 if (seat->touch->grab != &seat->touch->default_grab)
3611 return;
3612
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003613 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003614}
3615
3616static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003617lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003618{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003619 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003620
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003621 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003622 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003623 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003624 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003625
3626 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003627
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003628 /* Hide all surfaces by removing the fullscreen, panel and
3629 * toplevel layers. This way nothing else can show or receive
3630 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003631
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003632 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003633 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003634 if (shell->showing_input_panels)
3635 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003636 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003637 wl_list_insert(&shell->compositor->cursor_layer.link,
3638 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003639
Pekka Paalanen77346a62011-11-30 16:26:35 +02003640 launch_screensaver(shell);
3641
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003642 /* TODO: disable bindings that should not work while locked. */
3643
3644 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003645}
3646
3647static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003648unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003649{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003650 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003651 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003652 return;
3653 }
3654
3655 /* If desktop-shell client has gone away, unlock immediately. */
3656 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003657 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003658 return;
3659 }
3660
3661 if (shell->prepare_event_sent)
3662 return;
3663
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003664 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003665 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003666}
3667
3668static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003669shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003670{
3671 struct desktop_shell *shell = data;
3672
3673 shell->fade.animation = NULL;
3674
3675 switch (shell->fade.type) {
3676 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003677 weston_surface_destroy(shell->fade.view->surface);
3678 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003679 break;
3680 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003681 lock(shell);
3682 break;
3683 }
3684}
3685
Jason Ekstranda7af7042013-10-12 22:38:11 -05003686static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003687shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003688{
3689 struct weston_compositor *compositor = shell->compositor;
3690 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003691 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003692
3693 surface = weston_surface_create(compositor);
3694 if (!surface)
3695 return NULL;
3696
Jason Ekstranda7af7042013-10-12 22:38:11 -05003697 view = weston_view_create(surface);
3698 if (!view) {
3699 weston_surface_destroy(surface);
3700 return NULL;
3701 }
3702
3703 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003704 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003705 wl_list_insert(&compositor->fade_layer.view_list,
3706 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003707 pixman_region32_init(&surface->input);
3708
Jason Ekstranda7af7042013-10-12 22:38:11 -05003709 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003710}
3711
3712static void
3713shell_fade(struct desktop_shell *shell, enum fade_type type)
3714{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003715 float tint;
3716
3717 switch (type) {
3718 case FADE_IN:
3719 tint = 0.0;
3720 break;
3721 case FADE_OUT:
3722 tint = 1.0;
3723 break;
3724 default:
3725 weston_log("shell: invalid fade type\n");
3726 return;
3727 }
3728
3729 shell->fade.type = type;
3730
Jason Ekstranda7af7042013-10-12 22:38:11 -05003731 if (shell->fade.view == NULL) {
3732 shell->fade.view = shell_fade_create_surface(shell);
3733 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003734 return;
3735
Jason Ekstranda7af7042013-10-12 22:38:11 -05003736 shell->fade.view->alpha = 1.0 - tint;
3737 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003738 }
3739
3740 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003741 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003742 else
3743 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003744 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003745 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003746 shell_fade_done, shell);
3747}
3748
3749static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003750do_shell_fade_startup(void *data)
3751{
3752 struct desktop_shell *shell = data;
3753
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003754 if (shell->startup_animation_type == ANIMATION_FADE)
3755 shell_fade(shell, FADE_IN);
3756 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003757 weston_surface_destroy(shell->fade.view->surface);
3758 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003759 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003760}
3761
3762static void
3763shell_fade_startup(struct desktop_shell *shell)
3764{
3765 struct wl_event_loop *loop;
3766
3767 if (!shell->fade.startup_timer)
3768 return;
3769
3770 wl_event_source_remove(shell->fade.startup_timer);
3771 shell->fade.startup_timer = NULL;
3772
3773 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3774 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3775}
3776
3777static int
3778fade_startup_timeout(void *data)
3779{
3780 struct desktop_shell *shell = data;
3781
3782 shell_fade_startup(shell);
3783 return 0;
3784}
3785
3786static void
3787shell_fade_init(struct desktop_shell *shell)
3788{
3789 /* Make compositor output all black, and wait for the desktop-shell
3790 * client to signal it is ready, then fade in. The timer triggers a
3791 * fade-in, in case the desktop-shell client takes too long.
3792 */
3793
3794 struct wl_event_loop *loop;
3795
Jason Ekstranda7af7042013-10-12 22:38:11 -05003796 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003797 weston_log("%s: warning: fade surface already exists\n",
3798 __func__);
3799 return;
3800 }
3801
Jason Ekstranda7af7042013-10-12 22:38:11 -05003802 shell->fade.view = shell_fade_create_surface(shell);
3803 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003804 return;
3805
Jason Ekstranda7af7042013-10-12 22:38:11 -05003806 weston_view_update_transform(shell->fade.view);
3807 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003808
3809 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3810 shell->fade.startup_timer =
3811 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3812 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3813}
3814
3815static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003816idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003817{
3818 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003819 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003820
3821 shell_fade(shell, FADE_OUT);
3822 /* lock() is called from shell_fade_done() */
3823}
3824
3825static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003826wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003827{
3828 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003829 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003830
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003831 unlock(shell);
3832}
3833
3834static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003835show_input_panels(struct wl_listener *listener, void *data)
3836{
3837 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003838 container_of(listener, struct desktop_shell,
3839 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003840 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003841
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003842 shell->text_input.surface = (struct weston_surface*)data;
3843
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003844 if (shell->showing_input_panels)
3845 return;
3846
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003847 shell->showing_input_panels = true;
3848
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003849 if (!shell->locked)
3850 wl_list_insert(&shell->panel_layer.link,
3851 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003852
Jason Ekstranda7af7042013-10-12 22:38:11 -05003853 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003854 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003855 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003856 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003857 wl_list_insert(&shell->input_panel_layer.view_list,
3858 &ipsurf->view->layer_link);
3859 weston_view_geometry_dirty(ipsurf->view);
3860 weston_view_update_transform(ipsurf->view);
3861 weston_surface_damage(ipsurf->surface);
3862 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
3863 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003864 }
3865}
3866
3867static void
3868hide_input_panels(struct wl_listener *listener, void *data)
3869{
3870 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003871 container_of(listener, struct desktop_shell,
3872 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003873 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003874
Jan Arne Petersen61381972013-01-31 15:52:21 +01003875 if (!shell->showing_input_panels)
3876 return;
3877
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003878 shell->showing_input_panels = false;
3879
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003880 if (!shell->locked)
3881 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003882
Jason Ekstranda7af7042013-10-12 22:38:11 -05003883 wl_list_for_each_safe(view, next,
3884 &shell->input_panel_layer.view_list, layer_link)
3885 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003886}
3887
3888static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003889update_input_panels(struct wl_listener *listener, void *data)
3890{
3891 struct desktop_shell *shell =
3892 container_of(listener, struct desktop_shell,
3893 update_input_panel_listener);
3894
3895 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3896}
3897
3898static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003899center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003900{
Giulio Camuffob8366642013-04-25 13:57:46 +03003901 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003902 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003903
Jason Ekstranda7af7042013-10-12 22:38:11 -05003904 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03003905
3906 x = output->x + (output->width - width) / 2 - surf_x / 2;
3907 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003908
Jason Ekstranda7af7042013-10-12 22:38:11 -05003909 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003910}
3911
3912static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003913weston_view_set_initial_position(struct weston_view *view,
3914 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003915{
3916 struct weston_compositor *compositor = shell->compositor;
3917 int ix = 0, iy = 0;
3918 int range_x, range_y;
3919 int dx, dy, x, y, panel_height;
3920 struct weston_output *output, *target_output = NULL;
3921 struct weston_seat *seat;
3922
3923 /* As a heuristic place the new window on the same output as the
3924 * pointer. Falling back to the output containing 0, 0.
3925 *
3926 * TODO: Do something clever for touch too?
3927 */
3928 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003929 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003930 ix = wl_fixed_to_int(seat->pointer->x);
3931 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003932 break;
3933 }
3934 }
3935
3936 wl_list_for_each(output, &compositor->output_list, link) {
3937 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3938 target_output = output;
3939 break;
3940 }
3941 }
3942
3943 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003944 weston_view_set_position(view, 10 + random() % 400,
3945 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003946 return;
3947 }
3948
3949 /* Valid range within output where the surface will still be onscreen.
3950 * If this is negative it means that the surface is bigger than
3951 * output.
3952 */
3953 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003954 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003955 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05003956 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003957
Rob Bradford4cb88c72012-08-13 15:18:44 +01003958 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003959 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003960 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003961 dx = 0;
3962
3963 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003964 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003965 else
3966 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003967
3968 x = target_output->x + dx;
3969 y = target_output->y + dy;
3970
Jason Ekstranda7af7042013-10-12 22:38:11 -05003971 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003972}
3973
3974static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003975map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003976 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003977{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003978 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003979 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003980 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003981 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003982 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003983 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003984
Jason Ekstranda7af7042013-10-12 22:38:11 -05003985 shsurf->view->geometry.width = width;
3986 shsurf->view->geometry.height = height;
3987 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003988
3989 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003990 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02003991 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003992 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003993 break;
Alex Wu4539b082012-03-01 12:57:46 +08003994 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003995 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003996 shell_map_fullscreen(shsurf);
3997 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003998 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003999 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004000 panel_height = get_output_panel_height(shell, shsurf->output);
4001 surface_subsurfaces_boundingbox(shsurf->surface,
4002 &surf_x, &surf_y, NULL, NULL);
4003 weston_view_set_position(shsurf->view,
4004 shsurf->output->x - surf_x,
4005 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08004006 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004007 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004008 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004009 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004010 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004011 weston_view_set_position(shsurf->view,
4012 shsurf->view->geometry.x + sx,
4013 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004014 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004015 default:
4016 ;
4017 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004018
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02004019 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004020 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004021 case SHELL_SURFACE_POPUP:
4022 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004023 /* TODO: Handle a parent with multiple views */
4024 parent = get_default_view(shsurf->parent);
4025 if (parent) {
4026 wl_list_remove(&shsurf->view->layer_link);
4027 wl_list_insert(parent->layer_link.prev,
4028 &shsurf->view->layer_link);
4029 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004030 break;
Alex Wu4539b082012-03-01 12:57:46 +08004031 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02004032 case SHELL_SURFACE_NONE:
4033 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004034 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004035 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004036 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004037 wl_list_remove(&shsurf->view->layer_link);
4038 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004039 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004040 }
4041
Jason Ekstranda7af7042013-10-12 22:38:11 -05004042 if (shsurf->type != SHELL_SURFACE_NONE) {
4043 weston_view_update_transform(shsurf->view);
4044 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4045 shsurf->surface->output = shsurf->output;
4046 shsurf->view->output = shsurf->output;
4047 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004048 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004049
Jason Ekstranda7af7042013-10-12 22:38:11 -05004050 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004051 /* XXX: xwayland's using the same fields for transient type */
4052 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004053 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004054 if (shsurf->transient.flags ==
4055 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4056 break;
4057 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004058 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004059 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004060 if (!shell->locked) {
4061 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004062 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004063 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004064 break;
4065 default:
4066 break;
4067 }
4068
Jason Ekstranda7af7042013-10-12 22:38:11 -05004069 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004070 {
4071 switch (shell->win_animation_type) {
4072 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004073 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004074 break;
4075 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004076 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004077 break;
4078 default:
4079 break;
4080 }
4081 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004082}
4083
4084static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004085configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004086 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004087{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004088 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4089 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004090 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03004091 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004092
Pekka Paalanen77346a62011-11-30 16:26:35 +02004093 shsurf = get_shell_surface(surface);
4094 if (shsurf)
4095 surface_type = shsurf->type;
4096
Jason Ekstranda7af7042013-10-12 22:38:11 -05004097 /* TODO:
4098 * This should probably be changed to be more shell_surface
4099 * dependent
4100 */
4101 wl_list_for_each(view, &surface->views, surface_link)
4102 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004103
4104 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004105 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08004106 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004107 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004108 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004109 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004110 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004111 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4112 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004113 shsurf->view->geometry.x = shsurf->output->x - surf_x;
4114 shsurf->view->geometry.y = shsurf->output->y +
4115 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08004116 break;
Alex Wu4539b082012-03-01 12:57:46 +08004117 case SHELL_SURFACE_TOPLEVEL:
4118 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004119 default:
4120 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004121 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004122
Alex Wu4539b082012-03-01 12:57:46 +08004123 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004124 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004125 wl_list_for_each(view, &surface->views, surface_link)
4126 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004127
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004128 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004129 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004130 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004131}
4132
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004133static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004134shell_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 +03004135{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004136 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004137 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004138
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004139 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004140
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004141 if (!weston_surface_is_mapped(es) &&
4142 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004143 remove_popup_grab(shsurf);
4144 }
4145
Giulio Camuffo184df502013-02-21 11:29:21 +01004146 if (width == 0)
4147 return;
4148
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004149 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004150 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004151 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004152 type_changed = 1;
4153 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004154
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004155 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004156 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004157 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05004158 shsurf->view->geometry.width != width ||
4159 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004160 float from_x, from_y;
4161 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004162
Jason Ekstranda7af7042013-10-12 22:38:11 -05004163 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4164 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004165 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004166 shsurf->view->geometry.x + to_x - from_x,
4167 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004168 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004169 }
4170}
4171
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004172static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004173
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004174static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004175desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004176{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004177 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004178 struct desktop_shell *shell =
4179 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004180
4181 shell->child.process.pid = 0;
4182 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004183
4184 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4185 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004186 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004187 shell->child.deathstamp = time;
4188 shell->child.deathcount = 0;
4189 }
4190
4191 shell->child.deathcount++;
4192 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02004193 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004194 return;
4195 }
4196
Martin Minarik6d118362012-06-07 18:01:59 +02004197 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004198 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004199 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004200}
4201
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004202static void
4203launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004204{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004205 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05004206 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004207
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004208 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004209 &shell->child.process,
4210 shell_exe,
4211 desktop_shell_sigchld);
4212
4213 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004214 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004215}
4216
4217static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004218bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4219{
Tiago Vignattibe143262012-04-16 17:31:41 +03004220 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004221 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004222
Jason Ekstranda85118c2013-06-27 20:17:02 -05004223 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4224 if (resource)
4225 wl_resource_set_implementation(resource, &shell_implementation,
4226 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004227}
4228
Kristian Høgsberg75840622011-09-06 13:48:16 -04004229static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004230unbind_desktop_shell(struct wl_resource *resource)
4231{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004232 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004233
4234 if (shell->locked)
4235 resume_desktop(shell);
4236
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004237 shell->child.desktop_shell = NULL;
4238 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004239}
4240
4241static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004242bind_desktop_shell(struct wl_client *client,
4243 void *data, uint32_t version, uint32_t id)
4244{
Tiago Vignattibe143262012-04-16 17:31:41 +03004245 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004246 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004247
Jason Ekstranda85118c2013-06-27 20:17:02 -05004248 resource = wl_resource_create(client, &desktop_shell_interface,
4249 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004250
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004251 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004252 wl_resource_set_implementation(resource,
4253 &desktop_shell_implementation,
4254 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004255 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004256
4257 if (version < 2)
4258 shell_fade_startup(shell);
4259
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004260 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004261 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004262
4263 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4264 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004265 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004266}
4267
Pekka Paalanen6e168112011-11-24 11:34:05 +02004268static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004269screensaver_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 -04004270{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004271 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004272 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004273
Giulio Camuffo184df502013-02-21 11:29:21 +01004274 if (width == 0)
4275 return;
4276
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004277 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004278 if (!shell->locked)
4279 return;
4280
Jason Ekstranda7af7042013-10-12 22:38:11 -05004281 view = container_of(surface->views.next, struct weston_view, surface_link);
4282 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004283
Jason Ekstranda7af7042013-10-12 22:38:11 -05004284 if (wl_list_empty(&view->layer_link)) {
4285 wl_list_insert(shell->lock_layer.view_list.prev,
4286 &view->layer_link);
4287 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004288 wl_event_source_timer_update(shell->screensaver.timer,
4289 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004290 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004291 }
4292}
4293
4294static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004295screensaver_set_surface(struct wl_client *client,
4296 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004297 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004298 struct wl_resource *output_resource)
4299{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004300 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004301 struct weston_surface *surface =
4302 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004303 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004304 struct weston_view *view, *next;
4305
4306 /* Make sure we only have one view */
4307 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4308 weston_view_destroy(view);
4309 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004310
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004311 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004312 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004313 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004314}
4315
4316static const struct screensaver_interface screensaver_implementation = {
4317 screensaver_set_surface
4318};
4319
4320static void
4321unbind_screensaver(struct wl_resource *resource)
4322{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004323 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004324
Pekka Paalanen77346a62011-11-30 16:26:35 +02004325 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004326}
4327
4328static void
4329bind_screensaver(struct wl_client *client,
4330 void *data, uint32_t version, uint32_t id)
4331{
Tiago Vignattibe143262012-04-16 17:31:41 +03004332 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004333 struct wl_resource *resource;
4334
Jason Ekstranda85118c2013-06-27 20:17:02 -05004335 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004336
Pekka Paalanen77346a62011-11-30 16:26:35 +02004337 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004338 wl_resource_set_implementation(resource,
4339 &screensaver_implementation,
4340 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004341 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004342 return;
4343 }
4344
4345 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4346 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004347 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004348}
4349
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004350static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004351input_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 -04004352{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004353 struct input_panel_surface *ip_surface = surface->configure_private;
4354 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004355 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004356 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004357
Giulio Camuffo184df502013-02-21 11:29:21 +01004358 if (width == 0)
4359 return;
4360
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004361 if (!weston_surface_is_mapped(surface)) {
4362 if (!shell->showing_input_panels)
4363 return;
4364
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004365 show_surface = 1;
4366 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004367
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004368 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004369
4370 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004371 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4372 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004373 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004374 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4375 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004376 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004377
Jason Ekstranda7af7042013-10-12 22:38:11 -05004378 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004379
4380 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004381 wl_list_insert(&shell->input_panel_layer.view_list,
4382 &ip_surface->view->layer_link);
4383 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004384 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004385 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004386 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004387}
4388
4389static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004390destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004391{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004392 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4393
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004394 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004395 wl_list_remove(&input_panel_surface->link);
4396
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004397 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004398 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004399
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004400 free(input_panel_surface);
4401}
4402
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004403static struct input_panel_surface *
4404get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004405{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004406 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004407 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004408 } else {
4409 return NULL;
4410 }
4411}
4412
4413static void
4414input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4415{
4416 struct input_panel_surface *ipsurface = container_of(listener,
4417 struct input_panel_surface,
4418 surface_destroy_listener);
4419
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004420 if (ipsurface->resource) {
4421 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004422 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004423 destroy_input_panel_surface(ipsurface);
4424 }
4425}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004426
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004427static struct input_panel_surface *
4428create_input_panel_surface(struct desktop_shell *shell,
4429 struct weston_surface *surface)
4430{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004431 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004432
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004433 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4434 if (!input_panel_surface)
4435 return NULL;
4436
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004437 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004438 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004439
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004440 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004441
4442 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004443 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004444
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004445 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004446 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004447 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004448 &input_panel_surface->surface_destroy_listener);
4449
4450 wl_list_init(&input_panel_surface->link);
4451
4452 return input_panel_surface;
4453}
4454
4455static void
4456input_panel_surface_set_toplevel(struct wl_client *client,
4457 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004458 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004459 uint32_t position)
4460{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004461 struct input_panel_surface *input_panel_surface =
4462 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004463 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004464
4465 wl_list_insert(&shell->input_panel.surfaces,
4466 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004467
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004468 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004469 input_panel_surface->panel = 0;
4470}
4471
4472static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004473input_panel_surface_set_overlay_panel(struct wl_client *client,
4474 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004475{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004476 struct input_panel_surface *input_panel_surface =
4477 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004478 struct desktop_shell *shell = input_panel_surface->shell;
4479
4480 wl_list_insert(&shell->input_panel.surfaces,
4481 &input_panel_surface->link);
4482
4483 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004484}
4485
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004486static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004487 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004488 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004489};
4490
4491static void
4492destroy_input_panel_surface_resource(struct wl_resource *resource)
4493{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004494 struct input_panel_surface *ipsurf =
4495 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004496
4497 destroy_input_panel_surface(ipsurf);
4498}
4499
4500static void
4501input_panel_get_input_panel_surface(struct wl_client *client,
4502 struct wl_resource *resource,
4503 uint32_t id,
4504 struct wl_resource *surface_resource)
4505{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004506 struct weston_surface *surface =
4507 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004508 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004509 struct input_panel_surface *ipsurf;
4510
4511 if (get_input_panel_surface(surface)) {
4512 wl_resource_post_error(surface_resource,
4513 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004514 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004515 return;
4516 }
4517
4518 ipsurf = create_input_panel_surface(shell, surface);
4519 if (!ipsurf) {
4520 wl_resource_post_error(surface_resource,
4521 WL_DISPLAY_ERROR_INVALID_OBJECT,
4522 "surface->configure already set");
4523 return;
4524 }
4525
Jason Ekstranda85118c2013-06-27 20:17:02 -05004526 ipsurf->resource =
4527 wl_resource_create(client,
4528 &wl_input_panel_surface_interface, 1, id);
4529 wl_resource_set_implementation(ipsurf->resource,
4530 &input_panel_surface_implementation,
4531 ipsurf,
4532 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004533}
4534
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004535static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004536 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004537};
4538
4539static void
4540unbind_input_panel(struct wl_resource *resource)
4541{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004542 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004543
4544 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004545}
4546
4547static void
4548bind_input_panel(struct wl_client *client,
4549 void *data, uint32_t version, uint32_t id)
4550{
4551 struct desktop_shell *shell = data;
4552 struct wl_resource *resource;
4553
Jason Ekstranda85118c2013-06-27 20:17:02 -05004554 resource = wl_resource_create(client,
4555 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004556
4557 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004558 wl_resource_set_implementation(resource,
4559 &input_panel_implementation,
4560 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004561 shell->input_panel.binding = resource;
4562 return;
4563 }
4564
4565 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4566 "interface object already bound");
4567 wl_resource_destroy(resource);
4568}
4569
Kristian Høgsberg07045392012-02-19 18:52:44 -05004570struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004571 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004572 struct weston_surface *current;
4573 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004574 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004575};
4576
4577static void
4578switcher_next(struct switcher *switcher)
4579{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004580 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004581 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004582 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004583 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004584
Jason Ekstranda7af7042013-10-12 22:38:11 -05004585 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4586 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004587 case SHELL_SURFACE_TOPLEVEL:
4588 case SHELL_SURFACE_FULLSCREEN:
4589 case SHELL_SURFACE_MAXIMIZED:
4590 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004591 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004592 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004593 next = view->surface;
4594 prev = view->surface;
4595 view->alpha = 0.25;
4596 weston_view_geometry_dirty(view);
4597 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004598 break;
4599 default:
4600 break;
4601 }
Alex Wu1659daa2012-04-01 20:13:09 +08004602
Jason Ekstranda7af7042013-10-12 22:38:11 -05004603 if (is_black_surface(view->surface, NULL)) {
4604 view->alpha = 0.25;
4605 weston_view_geometry_dirty(view);
4606 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004607 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004608 }
4609
4610 if (next == NULL)
4611 next = first;
4612
Alex Wu07b26062012-03-12 16:06:01 +08004613 if (next == NULL)
4614 return;
4615
Kristian Høgsberg07045392012-02-19 18:52:44 -05004616 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004617 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004618
4619 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004620 wl_list_for_each(view, &next->views, surface_link)
4621 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004622
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004623 shsurf = get_shell_surface(switcher->current);
4624 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004625 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004626}
4627
4628static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004629switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004630{
4631 struct switcher *switcher =
4632 container_of(listener, struct switcher, listener);
4633
4634 switcher_next(switcher);
4635}
4636
4637static void
Daniel Stone351eb612012-05-31 15:27:47 -04004638switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004639{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004640 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004641 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004642 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004643
Jason Ekstranda7af7042013-10-12 22:38:11 -05004644 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004645 if (is_focus_view(view))
4646 continue;
4647
Jason Ekstranda7af7042013-10-12 22:38:11 -05004648 view->alpha = 1.0;
4649 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004650 }
4651
Alex Wu07b26062012-03-12 16:06:01 +08004652 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004653 activate(switcher->shell, switcher->current,
4654 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004655 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004656 weston_keyboard_end_grab(keyboard);
4657 if (keyboard->input_method_resource)
4658 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004659 free(switcher);
4660}
4661
4662static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004663switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004664 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004665{
4666 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004667 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004668
Daniel Stonec9785ea2012-05-30 16:31:52 +01004669 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004670 switcher_next(switcher);
4671}
4672
4673static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004674switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004675 uint32_t mods_depressed, uint32_t mods_latched,
4676 uint32_t mods_locked, uint32_t group)
4677{
4678 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004679 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004680
Daniel Stone351eb612012-05-31 15:27:47 -04004681 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4682 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004683}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004684
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004685static void
4686switcher_cancel(struct weston_keyboard_grab *grab)
4687{
4688 struct switcher *switcher = container_of(grab, struct switcher, grab);
4689
4690 switcher_destroy(switcher);
4691}
4692
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004693static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004694 switcher_key,
4695 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004696 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004697};
4698
4699static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004700switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004701 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004702{
Tiago Vignattibe143262012-04-16 17:31:41 +03004703 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004704 struct switcher *switcher;
4705
4706 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004707 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004708 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004709 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004710 wl_list_init(&switcher->listener.link);
4711
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004712 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004713 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004714 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004715 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4716 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004717 switcher_next(switcher);
4718}
4719
Daniel Stonedf8133b2013-11-19 11:37:14 +01004720struct exposay_surface {
4721 struct desktop_shell *shell;
4722 struct weston_surface *surface;
4723 struct weston_view *view;
4724 struct wl_list link;
4725
4726 int x;
4727 int y;
4728 int width;
4729 int height;
4730 double scale;
4731
4732 int row;
4733 int column;
4734
4735 /* The animations only apply a transformation for their own lifetime,
4736 * and don't have an option to indefinitely maintain the
4737 * transformation in a steady state - so, we apply our own once the
4738 * animation has finished. */
4739 struct weston_transform transform;
4740};
4741
4742static void exposay_set_state(struct desktop_shell *shell,
4743 enum exposay_target_state state,
4744 struct weston_seat *seat);
4745static void exposay_check_state(struct desktop_shell *shell);
4746
4747static void
4748exposay_in_flight_inc(struct desktop_shell *shell)
4749{
4750 shell->exposay.in_flight++;
4751}
4752
4753static void
4754exposay_in_flight_dec(struct desktop_shell *shell)
4755{
4756 if (--shell->exposay.in_flight > 0)
4757 return;
4758
4759 exposay_check_state(shell);
4760}
4761
4762static void
4763exposay_animate_in_done(struct weston_view_animation *animation, void *data)
4764{
4765 struct exposay_surface *esurface = data;
4766
4767 wl_list_insert(&esurface->view->geometry.transformation_list,
4768 &esurface->transform.link);
4769 weston_matrix_init(&esurface->transform.matrix);
4770 weston_matrix_scale(&esurface->transform.matrix,
4771 esurface->scale, esurface->scale, 1.0f);
4772 weston_matrix_translate(&esurface->transform.matrix,
4773 esurface->x - esurface->view->geometry.x,
4774 esurface->y - esurface->view->geometry.y,
4775 0);
4776
4777 weston_view_geometry_dirty(esurface->view);
4778 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
4779
4780 exposay_in_flight_dec(esurface->shell);
4781}
4782
4783static void
4784exposay_animate_in(struct exposay_surface *esurface)
4785{
4786 exposay_in_flight_inc(esurface->shell);
4787
4788 weston_move_scale_run(esurface->view,
4789 esurface->x - esurface->view->geometry.x,
4790 esurface->y - esurface->view->geometry.y,
4791 1.0, esurface->scale, 0,
4792 exposay_animate_in_done, esurface);
4793}
4794
4795static void
4796exposay_animate_out_done(struct weston_view_animation *animation, void *data)
4797{
4798 struct exposay_surface *esurface = data;
4799 struct desktop_shell *shell = esurface->shell;
4800
4801 wl_list_remove(&esurface->link);
4802 free(esurface);
4803
4804 exposay_in_flight_dec(shell);
4805}
4806
4807static void
4808exposay_animate_out(struct exposay_surface *esurface)
4809{
4810 exposay_in_flight_inc(esurface->shell);
4811
4812 /* Remove the static transformation set up by
4813 * exposay_transform_in_done(). */
4814 wl_list_remove(&esurface->transform.link);
4815 weston_view_geometry_dirty(esurface->view);
4816
4817 weston_move_scale_run(esurface->view,
4818 esurface->x - esurface->view->geometry.x,
4819 esurface->y - esurface->view->geometry.y,
4820 1.0, esurface->scale, 1,
4821 exposay_animate_out_done, esurface);
4822}
4823
4824static void
4825exposay_highlight_surface(struct desktop_shell *shell,
4826 struct exposay_surface *esurface)
4827{
4828 struct weston_view *view = NULL;
4829
4830 if (esurface) {
4831 shell->exposay.row_current = esurface->row;
4832 shell->exposay.column_current = esurface->column;
4833 view = esurface->view;
4834 }
4835
4836 animate_focus_change(shell, shell->exposay.workspace,
4837 shell->exposay.focus_current, view);
4838 shell->exposay.focus_current = view;
4839}
4840
4841static int
4842exposay_is_animating(struct desktop_shell *shell)
4843{
4844 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
4845 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
4846 return 0;
4847
4848 return (shell->exposay.in_flight > 0);
4849}
4850
4851static void
4852exposay_pick(struct desktop_shell *shell, int x, int y)
4853{
4854 struct exposay_surface *esurface;
4855
4856 if (exposay_is_animating(shell))
4857 return;
4858
4859 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
4860 if (x < esurface->x || x > esurface->x + esurface->width)
4861 continue;
4862 if (y < esurface->y || y > esurface->y + esurface->height)
4863 continue;
4864
4865 exposay_highlight_surface(shell, esurface);
4866 return;
4867 }
4868}
4869
4870/* Pretty lame layout for now; just tries to make a square. Should take
4871 * aspect ratio into account really. Also needs to be notified of surface
4872 * addition and removal and adjust layout/animate accordingly. */
4873static enum exposay_layout_state
4874exposay_layout(struct desktop_shell *shell)
4875{
4876 struct workspace *workspace = shell->exposay.workspace;
4877 struct weston_compositor *compositor = shell->compositor;
4878 struct weston_output *output = get_default_output(compositor);
4879 struct weston_view *view;
4880 struct exposay_surface *esurface;
4881 int w, h;
4882 int i;
4883 int last_row_removed = 0;
4884
4885 wl_list_init(&shell->exposay.surface_list);
4886
4887 shell->exposay.num_surfaces = 0;
4888 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
4889 if (!get_shell_surface(view->surface))
4890 continue;
4891 shell->exposay.num_surfaces++;
4892 }
4893
4894 if (shell->exposay.num_surfaces == 0) {
4895 shell->exposay.grid_size = 0;
4896 shell->exposay.hpadding_outer = 0;
4897 shell->exposay.vpadding_outer = 0;
4898 shell->exposay.padding_inner = 0;
4899 shell->exposay.surface_size = 0;
4900 return EXPOSAY_LAYOUT_OVERVIEW;
4901 }
4902
4903 /* Lay the grid out as square as possible, losing surfaces from the
4904 * bottom row if required. Start with fixed padding of a 10% margin
4905 * around the outside and 80px internal padding between surfaces, and
4906 * maximise the area made available to surfaces after this, but only
4907 * to a maximum of 1/3rd the total output size.
4908 *
4909 * If we can't make a square grid, add one extra row at the bottom
4910 * which will have a smaller number of columns.
4911 *
4912 * XXX: Surely there has to be a better way to express this maths,
4913 * right?!
4914 */
4915 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
4916 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
4917 shell->exposay.grid_size++;
4918 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
4919
4920 shell->exposay.hpadding_outer = (output->width / 10);
4921 shell->exposay.vpadding_outer = (output->height / 10);
4922 shell->exposay.padding_inner = 80;
4923
4924 w = output->width - (shell->exposay.hpadding_outer * 2);
4925 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
4926 w /= shell->exposay.grid_size;
4927
4928 h = output->height - (shell->exposay.vpadding_outer * 2);
4929 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
4930 h /= shell->exposay.grid_size;
4931
4932 shell->exposay.surface_size = (w < h) ? w : h;
4933 if (shell->exposay.surface_size > (output->width / 2))
4934 shell->exposay.surface_size = output->width / 2;
4935 if (shell->exposay.surface_size > (output->height / 2))
4936 shell->exposay.surface_size = output->height / 2;
4937
4938 i = 0;
4939 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
4940 int pad;
4941
4942 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
4943
4944 if (!get_shell_surface(view->surface))
4945 continue;
4946
4947 esurface = malloc(sizeof(*esurface));
4948 if (!esurface) {
4949 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
4950 shell->exposay.seat);
4951 break;
4952 }
4953
4954 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
4955 esurface->shell = shell;
4956 esurface->view = view;
4957
4958 esurface->row = i / shell->exposay.grid_size;
4959 esurface->column = i % shell->exposay.grid_size;
4960
4961 esurface->x = shell->exposay.hpadding_outer;
4962 esurface->x += pad * esurface->column;
4963 esurface->y = shell->exposay.vpadding_outer;
4964 esurface->y += pad * esurface->row;
4965
4966 if (esurface->row == shell->exposay.grid_size - 1)
4967 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
4968
4969 if (view->geometry.width > view->geometry.height)
4970 esurface->scale = shell->exposay.surface_size / (float) view->geometry.width;
4971 else
4972 esurface->scale = shell->exposay.surface_size / (float) view->geometry.height;
4973 esurface->width = view->geometry.width * esurface->scale;
4974 esurface->height = view->geometry.height * esurface->scale;
4975
4976 if (shell->exposay.focus_current == esurface->view)
4977 exposay_highlight_surface(shell, esurface);
4978
4979 exposay_animate_in(esurface);
4980
4981 i++;
4982 }
4983
4984 weston_compositor_schedule_repaint(shell->compositor);
4985
4986 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
4987}
4988
4989static void
4990exposay_motion(struct weston_pointer_grab *grab, uint32_t time)
4991{
4992 struct desktop_shell *shell =
4993 container_of(grab, struct desktop_shell, exposay.grab_ptr);
4994
4995 exposay_pick(shell,
4996 wl_fixed_to_int(grab->pointer->x),
4997 wl_fixed_to_int(grab->pointer->y));
4998}
4999
5000static void
5001exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5002 uint32_t state_w)
5003{
5004 struct desktop_shell *shell =
5005 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5006 struct weston_seat *seat = grab->pointer->seat;
5007 enum wl_pointer_button_state state = state_w;
5008
5009 if (button != BTN_LEFT)
5010 return;
5011
5012 /* Store the surface we clicked on, and don't do anything if we end up
5013 * releasing on a different surface. */
5014 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5015 shell->exposay.clicked = shell->exposay.focus_current;
5016 return;
5017 }
5018
5019 if (shell->exposay.focus_current == shell->exposay.clicked)
5020 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5021 else
5022 shell->exposay.clicked = NULL;
5023}
5024
5025static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5026 noop_grab_focus,
5027 exposay_motion,
5028 exposay_button,
5029};
5030
5031static int
5032exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5033{
5034 struct exposay_surface *esurface;
5035
5036 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5037 if (esurface->row != row || esurface->column != column)
5038 continue;
5039
5040 exposay_highlight_surface(shell, esurface);
5041 return 1;
5042 }
5043
5044 return 0;
5045}
5046
5047static void
5048exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5049 uint32_t state_w)
5050{
5051 struct weston_seat *seat = grab->keyboard->seat;
5052 struct desktop_shell *shell =
5053 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5054 enum wl_keyboard_key_state state = state_w;
5055
5056 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5057 return;
5058
5059 switch (key) {
5060 case KEY_ESC:
5061 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5062 break;
5063 case KEY_ENTER:
5064 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5065 break;
5066 case KEY_UP:
5067 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5068 shell->exposay.column_current);
5069 break;
5070 case KEY_DOWN:
5071 /* Special case for trying to move to the bottom row when it
5072 * has fewer items than all the others. */
5073 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5074 shell->exposay.column_current) &&
5075 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5076 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5077 (shell->exposay.num_surfaces %
5078 shell->exposay.grid_size) - 1);
5079 }
5080 break;
5081 case KEY_LEFT:
5082 exposay_maybe_move(shell, shell->exposay.row_current,
5083 shell->exposay.column_current - 1);
5084 break;
5085 case KEY_RIGHT:
5086 exposay_maybe_move(shell, shell->exposay.row_current,
5087 shell->exposay.column_current + 1);
5088 break;
5089 case KEY_TAB:
5090 /* Try to move right, then down (and to the leftmost column),
5091 * then if all else fails, to the top left. */
5092 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5093 shell->exposay.column_current + 1) &&
5094 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5095 exposay_maybe_move(shell, 0, 0);
5096 break;
5097 default:
5098 break;
5099 }
5100}
5101
5102static void
5103exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5104 uint32_t mods_depressed, uint32_t mods_latched,
5105 uint32_t mods_locked, uint32_t group)
5106{
5107}
5108
5109static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5110 exposay_key,
5111 exposay_modifier,
5112};
5113
5114/**
5115 * Called when the transition from overview -> inactive has completed.
5116 */
5117static enum exposay_layout_state
5118exposay_set_inactive(struct desktop_shell *shell)
5119{
5120 struct weston_seat *seat = shell->exposay.seat;
5121
5122 weston_keyboard_end_grab(seat->keyboard);
5123 weston_pointer_end_grab(seat->pointer);
5124 if (seat->keyboard->input_method_resource)
5125 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5126
5127 return EXPOSAY_LAYOUT_INACTIVE;
5128}
5129
5130/**
5131 * Begins the transition from overview to inactive. */
5132static enum exposay_layout_state
5133exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5134{
5135 struct exposay_surface *esurface;
5136
5137 /* Call activate() before we start the animations to avoid
5138 * animating back the old state and then immediately transitioning
5139 * to the new. */
5140 if (switch_focus && shell->exposay.focus_current)
5141 activate(shell, shell->exposay.focus_current->surface,
5142 shell->exposay.seat);
5143 else if (shell->exposay.focus_prev)
5144 activate(shell, shell->exposay.focus_prev->surface,
5145 shell->exposay.seat);
5146
5147 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5148 exposay_animate_out(esurface);
5149 weston_compositor_schedule_repaint(shell->compositor);
5150
5151 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5152}
5153
5154static enum exposay_layout_state
5155exposay_transition_active(struct desktop_shell *shell)
5156{
5157 struct weston_seat *seat = shell->exposay.seat;
5158
5159 shell->exposay.workspace = get_current_workspace(shell);
5160 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5161 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5162 shell->exposay.clicked = NULL;
5163 wl_list_init(&shell->exposay.surface_list);
5164
5165 lower_fullscreen_layer(shell);
5166 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5167 weston_keyboard_start_grab(seat->keyboard,
5168 &shell->exposay.grab_kbd);
5169 weston_keyboard_set_focus(seat->keyboard, NULL);
5170
5171 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5172 weston_pointer_start_grab(seat->pointer,
5173 &shell->exposay.grab_ptr);
5174 weston_pointer_set_focus(seat->pointer, NULL,
5175 seat->pointer->x, seat->pointer->y);
5176
5177 return exposay_layout(shell);
5178}
5179
5180static void
5181exposay_check_state(struct desktop_shell *shell)
5182{
5183 enum exposay_layout_state state_new = shell->exposay.state_cur;
5184 int do_switch = 0;
5185
5186 /* Don't do anything whilst animations are running, just store up
5187 * target state changes and only act on them when the animations have
5188 * completed. */
5189 if (exposay_is_animating(shell))
5190 return;
5191
5192 switch (shell->exposay.state_target) {
5193 case EXPOSAY_TARGET_OVERVIEW:
5194 switch (shell->exposay.state_cur) {
5195 case EXPOSAY_LAYOUT_OVERVIEW:
5196 goto out;
5197 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5198 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5199 break;
5200 default:
5201 state_new = exposay_transition_active(shell);
5202 break;
5203 }
5204 break;
5205
5206 case EXPOSAY_TARGET_SWITCH:
5207 do_switch = 1; /* fallthrough */
5208 case EXPOSAY_TARGET_CANCEL:
5209 switch (shell->exposay.state_cur) {
5210 case EXPOSAY_LAYOUT_INACTIVE:
5211 goto out;
5212 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5213 state_new = exposay_set_inactive(shell);
5214 break;
5215 default:
5216 state_new = exposay_transition_inactive(shell, do_switch);
5217 break;
5218 }
5219
5220 break;
5221 }
5222
5223out:
5224 shell->exposay.state_cur = state_new;
5225}
5226
5227static void
5228exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5229 struct weston_seat *seat)
5230{
5231 shell->exposay.state_target = state;
5232 shell->exposay.seat = seat;
5233 exposay_check_state(shell);
5234}
5235
5236static void
5237exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5238 void *data)
5239{
5240 struct desktop_shell *shell = data;
5241
5242 if (shell->exposay.state_target == EXPOSAY_TARGET_OVERVIEW)
5243 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5244 else
5245 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
5246}
5247
Pekka Paalanen3c647232011-12-22 13:43:43 +02005248static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005249backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005250 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005251{
5252 struct weston_compositor *compositor = data;
5253 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005254 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005255
5256 /* TODO: we're limiting to simple use cases, where we assume just
5257 * control on the primary display. We'd have to extend later if we
5258 * ever get support for setting backlights on random desktop LCD
5259 * panels though */
5260 output = get_default_output(compositor);
5261 if (!output)
5262 return;
5263
5264 if (!output->set_backlight)
5265 return;
5266
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005267 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5268 backlight_new = output->backlight_current - 25;
5269 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5270 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005271
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005272 if (backlight_new < 5)
5273 backlight_new = 5;
5274 if (backlight_new > 255)
5275 backlight_new = 255;
5276
5277 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005278 output->set_backlight(output, output->backlight_current);
5279}
5280
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005281struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005282 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005283 struct weston_seat *seat;
5284 uint32_t key[2];
5285 int key_released[2];
5286};
5287
5288static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005289debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005290 uint32_t key, uint32_t state)
5291{
5292 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005293 struct weston_compositor *ec = db->seat->compositor;
5294 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005295 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005296 uint32_t serial;
5297 int send = 0, terminate = 0;
5298 int check_binding = 1;
5299 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005300 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005301
5302 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5303 /* Do not run bindings on key releases */
5304 check_binding = 0;
5305
5306 for (i = 0; i < 2; i++)
5307 if (key == db->key[i])
5308 db->key_released[i] = 1;
5309
5310 if (db->key_released[0] && db->key_released[1]) {
5311 /* All key releases been swalled so end the grab */
5312 terminate = 1;
5313 } else if (key != db->key[0] && key != db->key[1]) {
5314 /* Should not swallow release of other keys */
5315 send = 1;
5316 }
5317 } else if (key == db->key[0] && !db->key_released[0]) {
5318 /* Do not check bindings for the first press of the binding
5319 * key. This allows it to be used as a debug shortcut.
5320 * We still need to swallow this event. */
5321 check_binding = 0;
5322 } else if (db->key[1]) {
5323 /* If we already ran a binding don't process another one since
5324 * we can't keep track of all the binding keys that were
5325 * pressed in order to swallow the release events. */
5326 send = 1;
5327 check_binding = 0;
5328 }
5329
5330 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005331 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5332 key, state)) {
5333 /* We ran a binding so swallow the press and keep the
5334 * grab to swallow the released too. */
5335 send = 0;
5336 terminate = 0;
5337 db->key[1] = key;
5338 } else {
5339 /* Terminate the grab since the key pressed is not a
5340 * debug binding key. */
5341 send = 1;
5342 terminate = 1;
5343 }
5344 }
5345
5346 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005347 serial = wl_display_next_serial(display);
5348 resource_list = &grab->keyboard->focus_resource_list;
5349 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005350 wl_keyboard_send_key(resource, serial, time, key, state);
5351 }
5352 }
5353
5354 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005355 weston_keyboard_end_grab(grab->keyboard);
5356 if (grab->keyboard->input_method_resource)
5357 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005358 free(db);
5359 }
5360}
5361
5362static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005363debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005364 uint32_t mods_depressed, uint32_t mods_latched,
5365 uint32_t mods_locked, uint32_t group)
5366{
5367 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005368 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005369
Neil Roberts96d790e2013-09-19 17:32:00 +01005370 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005371
Neil Roberts96d790e2013-09-19 17:32:00 +01005372 wl_resource_for_each(resource, resource_list) {
5373 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5374 mods_latched, mods_locked, group);
5375 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005376}
5377
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005378static void
5379debug_binding_cancel(struct weston_keyboard_grab *grab)
5380{
5381 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5382
5383 weston_keyboard_end_grab(grab->keyboard);
5384 free(db);
5385}
5386
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005387struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005388 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005389 debug_binding_modifiers,
5390 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005391};
5392
5393static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005394debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005395{
5396 struct debug_binding_grab *grab;
5397
5398 grab = calloc(1, sizeof *grab);
5399 if (!grab)
5400 return;
5401
5402 grab->seat = (struct weston_seat *) seat;
5403 grab->key[0] = key;
5404 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005405 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005406}
5407
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005408static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005409force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005410 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005411{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005412 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005413 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005414 struct desktop_shell *shell = data;
5415 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005416 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005417
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005418 focus_surface = seat->keyboard->focus;
5419 if (!focus_surface)
5420 return;
5421
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005422 wl_signal_emit(&compositor->kill_signal, focus_surface);
5423
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005424 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005425 wl_client_get_credentials(client, &pid, NULL, NULL);
5426
5427 /* Skip clients that we launched ourselves (the credentials of
5428 * the socketpair is ours) */
5429 if (pid == getpid())
5430 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005431
Daniel Stone325fc2d2012-05-30 16:31:58 +01005432 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005433}
5434
5435static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005436workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005437 uint32_t key, void *data)
5438{
5439 struct desktop_shell *shell = data;
5440 unsigned int new_index = shell->workspaces.current;
5441
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005442 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005443 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005444 if (new_index != 0)
5445 new_index--;
5446
5447 change_workspace(shell, new_index);
5448}
5449
5450static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005451workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005452 uint32_t key, void *data)
5453{
5454 struct desktop_shell *shell = data;
5455 unsigned int new_index = shell->workspaces.current;
5456
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005457 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005458 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005459 if (new_index < shell->workspaces.num - 1)
5460 new_index++;
5461
5462 change_workspace(shell, new_index);
5463}
5464
5465static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005466workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005467 uint32_t key, void *data)
5468{
5469 struct desktop_shell *shell = data;
5470 unsigned int new_index;
5471
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005472 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005473 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005474 new_index = key - KEY_F1;
5475 if (new_index >= shell->workspaces.num)
5476 new_index = shell->workspaces.num - 1;
5477
5478 change_workspace(shell, new_index);
5479}
5480
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005481static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005482workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005483 uint32_t key, void *data)
5484{
5485 struct desktop_shell *shell = data;
5486 unsigned int new_index = shell->workspaces.current;
5487
5488 if (shell->locked)
5489 return;
5490
5491 if (new_index != 0)
5492 new_index--;
5493
5494 take_surface_to_workspace_by_seat(shell, seat, new_index);
5495}
5496
5497static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005498workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005499 uint32_t key, void *data)
5500{
5501 struct desktop_shell *shell = data;
5502 unsigned int new_index = shell->workspaces.current;
5503
5504 if (shell->locked)
5505 return;
5506
5507 if (new_index < shell->workspaces.num - 1)
5508 new_index++;
5509
5510 take_surface_to_workspace_by_seat(shell, seat, new_index);
5511}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005512
5513static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005514handle_output_destroy(struct wl_listener *listener, void *data)
5515{
5516 struct shell_output *output_listener =
5517 container_of(listener, struct shell_output, destroy_listener);
5518
5519 wl_list_remove(&output_listener->destroy_listener.link);
5520 wl_list_remove(&output_listener->link);
5521 free(output_listener);
5522}
5523
5524static void
5525create_shell_output(struct desktop_shell *shell,
5526 struct weston_output *output)
5527{
5528 struct shell_output *shell_output;
5529
5530 shell_output = zalloc(sizeof *shell_output);
5531 if (shell_output == NULL)
5532 return;
5533
5534 shell_output->output = output;
5535 shell_output->shell = shell;
5536 shell_output->destroy_listener.notify = handle_output_destroy;
5537 wl_signal_add(&output->destroy_signal,
5538 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005539 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005540}
5541
5542static void
5543handle_output_create(struct wl_listener *listener, void *data)
5544{
5545 struct desktop_shell *shell =
5546 container_of(listener, struct desktop_shell, output_create_listener);
5547 struct weston_output *output = (struct weston_output *)data;
5548
5549 create_shell_output(shell, output);
5550}
5551
5552static void
5553setup_output_destroy_handler(struct weston_compositor *ec,
5554 struct desktop_shell *shell)
5555{
5556 struct weston_output *output;
5557
5558 wl_list_init(&shell->output_list);
5559 wl_list_for_each(output, &ec->output_list, link)
5560 create_shell_output(shell, output);
5561
5562 shell->output_create_listener.notify = handle_output_create;
5563 wl_signal_add(&ec->output_created_signal,
5564 &shell->output_create_listener);
5565}
5566
5567static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005568shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005569{
Tiago Vignattibe143262012-04-16 17:31:41 +03005570 struct desktop_shell *shell =
5571 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005572 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005573 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005574
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005575 if (shell->child.client)
5576 wl_client_destroy(shell->child.client);
5577
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005578 wl_list_remove(&shell->idle_listener.link);
5579 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005580 wl_list_remove(&shell->show_input_panel_listener.link);
5581 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005582
Xiong Zhang6b481422013-10-23 13:58:32 +08005583 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5584 wl_list_remove(&shell_output->destroy_listener.link);
5585 wl_list_remove(&shell_output->link);
5586 free(shell_output);
5587 }
5588
5589 wl_list_remove(&shell->output_create_listener.link);
5590
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005591 wl_array_for_each(ws, &shell->workspaces.array)
5592 workspace_destroy(*ws);
5593 wl_array_release(&shell->workspaces.array);
5594
Pekka Paalanen3c647232011-12-22 13:43:43 +02005595 free(shell->screensaver.path);
5596 free(shell);
5597}
5598
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005599static void
5600shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5601{
5602 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005603 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005604
5605 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005606 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5607 MODIFIER_CTRL | MODIFIER_ALT,
5608 terminate_binding, ec);
5609 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5610 click_to_activate_binding,
5611 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005612 weston_compositor_add_touch_binding(ec, 0,
5613 touch_to_activate_binding,
5614 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005615 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5616 MODIFIER_SUPER | MODIFIER_ALT,
5617 surface_opacity_binding, NULL);
5618 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5619 MODIFIER_SUPER, zoom_axis_binding,
5620 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005621
5622 /* configurable bindings */
5623 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005624 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5625 zoom_key_binding, NULL);
5626 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5627 zoom_key_binding, NULL);
5628 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5629 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005630 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005631 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5632 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005633
5634 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5635 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5636 rotate_binding, NULL);
5637
Daniel Stone325fc2d2012-05-30 16:31:58 +01005638 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5639 shell);
5640 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5641 ec);
5642 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5643 backlight_binding, ec);
5644 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5645 ec);
5646 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5647 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005648 weston_compositor_add_key_binding(ec, KEY_K, mod,
5649 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005650 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5651 workspace_up_binding, shell);
5652 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5653 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005654 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5655 workspace_move_surface_up_binding,
5656 shell);
5657 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5658 workspace_move_surface_down_binding,
5659 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005660
Daniel Stonedf8133b2013-11-19 11:37:14 +01005661 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5662
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005663 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5664 if (shell->workspaces.num > 1) {
5665 num_workspace_bindings = shell->workspaces.num;
5666 if (num_workspace_bindings > 6)
5667 num_workspace_bindings = 6;
5668 for (i = 0; i < num_workspace_bindings; i++)
5669 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5670 workspace_f_binding,
5671 shell);
5672 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005673
5674 /* Debug bindings */
5675 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5676 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005677}
5678
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005679WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005680module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005681 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005682{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005683 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005684 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005685 struct workspace **pws;
5686 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005687 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005688
Peter Huttererf3d62272013-08-08 11:57:05 +10005689 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005690 if (shell == NULL)
5691 return -1;
5692
Kristian Høgsberg75840622011-09-06 13:48:16 -04005693 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005694
5695 shell->destroy_listener.notify = shell_destroy;
5696 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005697 shell->idle_listener.notify = idle_handler;
5698 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5699 shell->wake_listener.notify = wake_handler;
5700 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005701 shell->show_input_panel_listener.notify = show_input_panels;
5702 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
5703 shell->hide_input_panel_listener.notify = hide_input_panels;
5704 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005705 shell->update_input_panel_listener.notify = update_input_panels;
5706 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06005707 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005708 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005709 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005710 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005711 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005712 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005713 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005714 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005715 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005716 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005717 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005718
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005719 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005720
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005721 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5722 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005723 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5724 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005725 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005726
5727 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005728 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005729
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005730 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005731
Daniel Stonedf8133b2013-11-19 11:37:14 +01005732 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5733 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5734
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005735 for (i = 0; i < shell->workspaces.num; i++) {
5736 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5737 if (pws == NULL)
5738 return -1;
5739
5740 *pws = workspace_create();
5741 if (*pws == NULL)
5742 return -1;
5743 }
5744 activate_workspace(shell, 0);
5745
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005746 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005747 wl_list_init(&shell->workspaces.animation.link);
5748 shell->workspaces.animation.frame = animate_workspace_change_frame;
5749
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005750 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005751 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005752 return -1;
5753
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005754 if (wl_global_create(ec->wl_display,
5755 &desktop_shell_interface, 2,
5756 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005757 return -1;
5758
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005759 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5760 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005761 return -1;
5762
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005763 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005764 shell, bind_input_panel) == NULL)
5765 return -1;
5766
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005767 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5768 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005769 return -1;
5770
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005771 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005772
Xiong Zhang6b481422013-10-23 13:58:32 +08005773 setup_output_destroy_handler(ec, shell);
5774
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005775 loop = wl_display_get_event_loop(ec->wl_display);
5776 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005777
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005778 shell->screensaver.timer =
5779 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5780
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005781 wl_list_for_each(seat, &ec->seat_list, link)
5782 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005783
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005784 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005785
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005786 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005787
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005788 return 0;
5789}