blob: 2a82a1d38c891b07fc318a5fddeed8e2eae124f0 [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>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Xiong Zhang6b481422013-10-23 13:58:32 +080059struct shell_output {
60 struct desktop_shell *shell;
61 struct weston_output *output;
62 struct wl_listener destroy_listener;
63 struct wl_list link;
64};
65
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050066enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020067 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050068 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030069 SHELL_SURFACE_POPUP,
70 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020071};
72
Scott Moreauff1db4a2012-04-17 19:06:18 -060073struct ping_timer {
74 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -060075 uint32_t serial;
76};
77
Philip Withnall648a4dd2013-11-25 18:01:44 +000078/*
79 * Surface stacking and ordering.
80 *
81 * This is handled using several linked lists of surfaces, organised into
82 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
83 * above all of the surfaces in the layer below. The set of layers is static and
84 * in the following order (top-most first):
85 * • Lock layer (only ever displayed on its own)
86 * • Cursor layer
87 * • Fullscreen layer
88 * • Panel layer
89 * • Input panel layer
90 * • Workspace layers
91 * • Background layer
92 *
93 * The list of layers may be manipulated to remove whole layers of surfaces from
94 * display. For example, when locking the screen, all layers except the lock
95 * layer are removed.
96 *
97 * A surface’s layer is modified on configuring the surface, in
98 * set_surface_type() (which is only called when the surface’s type change is
99 * _committed_). If a surface’s type changes (e.g. when making a window
100 * fullscreen) its layer changes too.
101 *
102 * In order to allow popup and transient surfaces to be correctly stacked above
103 * their parent surfaces, each surface tracks both its parent surface, and a
104 * linked list of its children. When a surface’s layer is updated, so are the
105 * layers of its children. Note that child surfaces are *not* the same as
106 * subsurfaces — child/parent surfaces are purely for maintaining stacking
107 * order.
108 *
109 * The children_link list of siblings of a surface (i.e. those surfaces which
110 * have the same parent) only contains weston_surfaces which have a
111 * shell_surface. Stacking is not implemented for non-shell_surface
112 * weston_surfaces. This means that the following implication does *not* hold:
113 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
114 */
115
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200116struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500117 struct wl_resource *resource;
118 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200119
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500120 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500121 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600122 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200123 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400124 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000125 struct wl_list children_list; /* child surfaces of this one */
126 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300127 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200128
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800129 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400130 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500131 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200132 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800133 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200134 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800135 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700136 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800137 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100138
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200140 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500141 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200142 } rotation;
143
144 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100145 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500146 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100147 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400148 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500149 } popup;
150
Alex Wu4539b082012-03-01 12:57:46 +0800151 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300152 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400153 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300154 } transient;
155
156 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800157 enum wl_shell_surface_fullscreen_method type;
158 struct weston_transform transform; /* matrix from x, y */
159 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500160 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800161 } fullscreen;
162
Scott Moreauff1db4a2012-04-17 19:06:18 -0600163 struct ping_timer *ping_timer;
164
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200165 struct weston_transform workspace_transform;
166
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500167 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500168 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200169 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100170 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400171
172 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200173
174 struct {
175 bool maximized;
176 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200177 bool relative;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200178 } state, next_state; /* surface states */
179 bool state_changed;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500180
181 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200182};
183
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300184struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400185 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300186 struct shell_surface *shsurf;
187 struct wl_listener shsurf_destroy_listener;
188};
189
Rusty Lynch1084da52013-08-15 09:10:08 -0700190struct shell_touch_grab {
191 struct weston_touch_grab grab;
192 struct shell_surface *shsurf;
193 struct wl_listener shsurf_destroy_listener;
194 struct weston_touch *touch;
195};
196
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300197struct weston_move_grab {
198 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100199 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500200};
201
Rusty Lynch1084da52013-08-15 09:10:08 -0700202struct weston_touch_move_grab {
203 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800204 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700205 wl_fixed_t dx, dy;
206};
207
Pekka Paalanen460099f2012-01-20 16:48:25 +0200208struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300209 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500210 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200212 float x;
213 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200214 } center;
215};
216
Giulio Camuffo5085a752013-03-25 21:42:45 +0100217struct shell_seat {
218 struct weston_seat *seat;
219 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500220 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100221
222 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400223 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100224 struct wl_list surfaces_list;
225 struct wl_client *client;
226 int32_t initial_up;
227 } popup_grab;
228};
229
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800230struct shell_client {
231 struct wl_resource *resource;
232 struct wl_client *client;
233 struct desktop_shell *shell;
234 struct wl_listener destroy_listener;
235 struct wl_event_source *ping_timer;
236 uint32_t ping_serial;
237 int unresponsive;
238};
239
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100240void
241set_alpha_if_fullscreen(struct shell_surface *shsurf)
242{
243 if (shsurf && shsurf->state.fullscreen)
244 shsurf->fullscreen.black_view->alpha = 0.25;
245}
246
Alex Wubd3354b2012-04-17 17:20:49 +0800247static struct desktop_shell *
248shell_surface_get_shell(struct shell_surface *shsurf);
249
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500250static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400251surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500252
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300253static void
254shell_fade_startup(struct desktop_shell *shell);
255
Philip Withnallbecb77e2013-11-25 18:01:30 +0000256static struct shell_seat *
257get_shell_seat(struct weston_seat *seat);
258
Philip Withnall648a4dd2013-11-25 18:01:44 +0000259static void
260shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
261
Alex Wubd3354b2012-04-17 17:20:49 +0800262static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200263shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
264
265static bool
266shell_surface_is_xdg_surface(struct shell_surface *shsurf);
267
268static bool
269shell_surface_is_xdg_popup(struct shell_surface *shsurf);
270
271static void
272shell_surface_set_parent(struct shell_surface *shsurf,
273 struct weston_surface *parent);
274
275static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800276shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
277{
278 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800280
281 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100282
Jason Ekstranda7af7042013-10-12 22:38:11 -0500283 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800284 return false;
285
Jason Ekstranda7af7042013-10-12 22:38:11 -0500286 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
287 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800288 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800290}
291
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500292static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400293destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300294{
295 struct shell_grab *grab;
296
297 grab = container_of(listener, struct shell_grab,
298 shsurf_destroy_listener);
299
300 grab->shsurf = NULL;
301}
302
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800303struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304get_default_view(struct weston_surface *surface)
305{
306 struct shell_surface *shsurf;
307 struct weston_view *view;
308
309 if (!surface || wl_list_empty(&surface->views))
310 return NULL;
311
312 shsurf = get_shell_surface(surface);
313 if (shsurf)
314 return shsurf->view;
315
316 wl_list_for_each(view, &surface->views, surface_link)
317 if (weston_view_is_mapped(view))
318 return view;
319
320 return container_of(surface->views.next, struct weston_view, surface_link);
321}
322
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300323static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400324popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400325
326static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300327shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400328 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300329 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400330 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300331 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300333 struct desktop_shell *shell = shsurf->shell;
334
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400335 popup_grab_end(pointer);
336
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300337 grab->grab.interface = interface;
338 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400339 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500340 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400341 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300342
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700343 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400344 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400345 if (shell->child.desktop_shell) {
346 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
347 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500348 weston_pointer_set_focus(pointer,
349 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400350 wl_fixed_from_int(0),
351 wl_fixed_from_int(0));
352 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300353}
354
355static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300356shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300357{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700358 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400359 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700360 grab->shsurf->grabbed = 0;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800361 grab->shsurf->resize_edges = 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700362 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300363
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700364 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300365}
366
367static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700368shell_touch_grab_start(struct shell_touch_grab *grab,
369 const struct weston_touch_grab_interface *interface,
370 struct shell_surface *shsurf,
371 struct weston_touch *touch)
372{
373 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800374
Rusty Lynch1084da52013-08-15 09:10:08 -0700375 grab->grab.interface = interface;
376 grab->shsurf = shsurf;
377 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
378 wl_signal_add(&shsurf->destroy_signal,
379 &grab->shsurf_destroy_listener);
380
381 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700382 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700383
384 weston_touch_start_grab(touch, &grab->grab);
385 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500386 weston_touch_set_focus(touch->seat,
387 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700388}
389
390static void
391shell_touch_grab_end(struct shell_touch_grab *grab)
392{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700393 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700394 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700395 grab->shsurf->grabbed = 0;
396 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700397
398 weston_touch_end_grab(grab->touch);
399}
400
401static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500402center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800403 struct weston_output *output);
404
Daniel Stone496ca172012-05-30 16:31:42 +0100405static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300406get_modifier(char *modifier)
407{
408 if (!modifier)
409 return MODIFIER_SUPER;
410
411 if (!strcmp("ctrl", modifier))
412 return MODIFIER_CTRL;
413 else if (!strcmp("alt", modifier))
414 return MODIFIER_ALT;
415 else if (!strcmp("super", modifier))
416 return MODIFIER_SUPER;
417 else
418 return MODIFIER_SUPER;
419}
420
Juan Zhaoe10d2792012-04-25 19:09:52 +0800421static enum animation_type
422get_animation_type(char *animation)
423{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800424 if (!animation)
425 return ANIMATION_NONE;
426
Juan Zhaoe10d2792012-04-25 19:09:52 +0800427 if (!strcmp("zoom", animation))
428 return ANIMATION_ZOOM;
429 else if (!strcmp("fade", animation))
430 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100431 else if (!strcmp("dim-layer", animation))
432 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800433 else
434 return ANIMATION_NONE;
435}
436
Alex Wu4539b082012-03-01 12:57:46 +0800437static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400438shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200439{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400440 struct weston_config_section *section;
441 int duration;
442 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200443
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400444 section = weston_config_get_section(shell->compositor->config,
445 "screensaver", NULL, NULL);
446 weston_config_section_get_string(section,
447 "path", &shell->screensaver.path, NULL);
448 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200449 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400450
451 section = weston_config_get_section(shell->compositor->config,
452 "shell", NULL, NULL);
453 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100454 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100455 shell->client = s;
456 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400457 "binding-modifier", &s, "super");
458 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200459 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800460
461 weston_config_section_get_string(section,
462 "exposay-modifier", &s, "none");
463 if (strcmp(s, "none") == 0)
464 shell->exposay_modifier = 0;
465 else
466 shell->exposay_modifier = get_modifier(s);
467 free(s);
468
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400469 weston_config_section_get_string(section, "animation", &s, "none");
470 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200471 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700472 weston_config_section_get_string(section,
473 "startup-animation", &s, "fade");
474 shell->startup_animation_type = get_animation_type(s);
475 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700476 if (shell->startup_animation_type == ANIMATION_ZOOM)
477 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100478 weston_config_section_get_string(section, "focus-animation", &s, "none");
479 shell->focus_animation_type = get_animation_type(s);
480 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400481 weston_config_section_get_uint(section, "num-workspaces",
482 &shell->workspaces.num,
483 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200484}
485
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800486struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100487get_default_output(struct weston_compositor *compositor)
488{
489 return container_of(compositor->output_list.next,
490 struct weston_output, link);
491}
492
493
494/* no-op func for checking focus surface */
495static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600496focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100497{
498}
499
500static struct focus_surface *
501get_focus_surface(struct weston_surface *surface)
502{
503 if (surface->configure == focus_surface_configure)
504 return surface->configure_private;
505 else
506 return NULL;
507}
508
509static bool
510is_focus_surface (struct weston_surface *es)
511{
512 return (es->configure == focus_surface_configure);
513}
514
515static bool
516is_focus_view (struct weston_view *view)
517{
518 return is_focus_surface (view->surface);
519}
520
521static struct focus_surface *
522create_focus_surface(struct weston_compositor *ec,
523 struct weston_output *output)
524{
525 struct focus_surface *fsurf = NULL;
526 struct weston_surface *surface = NULL;
527
528 fsurf = malloc(sizeof *fsurf);
529 if (!fsurf)
530 return NULL;
531
532 fsurf->surface = weston_surface_create(ec);
533 surface = fsurf->surface;
534 if (surface == NULL) {
535 free(fsurf);
536 return NULL;
537 }
538
539 surface->configure = focus_surface_configure;
540 surface->output = output;
541 surface->configure_private = fsurf;
542
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800543 fsurf->view = weston_view_create(surface);
544 if (fsurf->view == NULL) {
545 weston_surface_destroy(surface);
546 free(fsurf);
547 return NULL;
548 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100549 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100550
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600551 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600552 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100553 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
554 pixman_region32_fini(&surface->opaque);
555 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
556 output->width, output->height);
557 pixman_region32_fini(&surface->input);
558 pixman_region32_init(&surface->input);
559
560 wl_list_init(&fsurf->workspace_transform.link);
561
562 return fsurf;
563}
564
565static void
566focus_surface_destroy(struct focus_surface *fsurf)
567{
568 weston_surface_destroy(fsurf->surface);
569 free(fsurf);
570}
571
572static void
573focus_animation_done(struct weston_view_animation *animation, void *data)
574{
575 struct workspace *ws = data;
576
577 ws->focus_animation = NULL;
578}
579
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200580static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200581focus_state_destroy(struct focus_state *state)
582{
583 wl_list_remove(&state->seat_destroy_listener.link);
584 wl_list_remove(&state->surface_destroy_listener.link);
585 free(state);
586}
587
588static void
589focus_state_seat_destroy(struct wl_listener *listener, void *data)
590{
591 struct focus_state *state = container_of(listener,
592 struct focus_state,
593 seat_destroy_listener);
594
595 wl_list_remove(&state->link);
596 focus_state_destroy(state);
597}
598
599static void
600focus_state_surface_destroy(struct wl_listener *listener, void *data)
601{
602 struct focus_state *state = container_of(listener,
603 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400604 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400605 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500606 struct weston_surface *main_surface, *next;
607 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200608
Pekka Paalanen01388e22013-04-25 13:57:44 +0300609 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
610
Kristian Høgsberge3778222012-07-31 17:29:30 -0400611 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500612 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
613 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400614 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100615 if (is_focus_view(view))
616 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400617
Jason Ekstranda7af7042013-10-12 22:38:11 -0500618 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400619 break;
620 }
621
Pekka Paalanen01388e22013-04-25 13:57:44 +0300622 /* if the focus was a sub-surface, activate its main surface */
623 if (main_surface != state->keyboard_focus)
624 next = main_surface;
625
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100626 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400627 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100628 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400629 activate(shell, next, state->seat);
630 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100631 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
632 if (state->ws->focus_animation)
633 weston_view_animation_destroy(state->ws->focus_animation);
634
635 state->ws->focus_animation = weston_fade_run(
636 state->ws->fsurf_front->view,
637 state->ws->fsurf_front->view->alpha, 0.0, 300,
638 focus_animation_done, state->ws);
639 }
640
Kristian Høgsberge3778222012-07-31 17:29:30 -0400641 wl_list_remove(&state->link);
642 focus_state_destroy(state);
643 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200644}
645
646static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400647focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200648{
Jonas Ådahl04769742012-06-13 00:01:24 +0200649 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200650
651 state = malloc(sizeof *state);
652 if (state == NULL)
653 return NULL;
654
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100655 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400656 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200657 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400658 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200659
660 state->seat_destroy_listener.notify = focus_state_seat_destroy;
661 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400662 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200663 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400664 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200665
666 return state;
667}
668
Jonas Ådahl8538b222012-08-29 22:13:03 +0200669static struct focus_state *
670ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
671{
672 struct workspace *ws = get_current_workspace(shell);
673 struct focus_state *state;
674
675 wl_list_for_each(state, &ws->focus_list, link)
676 if (state->seat == seat)
677 break;
678
679 if (&state->link == &ws->focus_list)
680 state = focus_state_create(seat, ws);
681
682 return state;
683}
684
Jonas Ådahl04769742012-06-13 00:01:24 +0200685static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800686focus_state_set_focus(struct focus_state *state,
687 struct weston_surface *surface)
688{
689 if (state->keyboard_focus) {
690 wl_list_remove(&state->surface_destroy_listener.link);
691 wl_list_init(&state->surface_destroy_listener.link);
692 }
693
694 state->keyboard_focus = surface;
695 if (surface)
696 wl_signal_add(&surface->destroy_signal,
697 &state->surface_destroy_listener);
698}
699
700static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400701restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200702{
703 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400704 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200705
706 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800707 if (state->seat->keyboard == NULL)
708 continue;
709
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400710 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200711
Kristian Høgsberge3148752013-05-06 23:19:49 -0400712 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200713 }
714}
715
716static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200717replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
718 struct weston_seat *seat)
719{
720 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200721
722 wl_list_for_each(state, &ws->focus_list, link) {
723 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800724 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200725 return;
726 }
727 }
728}
729
730static void
731drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
732 struct weston_surface *surface)
733{
734 struct focus_state *state;
735
736 wl_list_for_each(state, &ws->focus_list, link)
737 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800738 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200739}
740
741static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100742animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
743 struct weston_view *from, struct weston_view *to)
744{
745 struct weston_output *output;
746 bool focus_surface_created = false;
747
748 /* FIXME: Only support dim animation using two layers */
749 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
750 return;
751
752 output = get_default_output(shell->compositor);
753 if (ws->fsurf_front == NULL && (from || to)) {
754 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800755 if (ws->fsurf_front == NULL)
756 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100757 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800758
759 ws->fsurf_back = create_focus_surface(shell->compositor, output);
760 if (ws->fsurf_back == NULL) {
761 focus_surface_destroy(ws->fsurf_front);
762 return;
763 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100764 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800765
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100766 focus_surface_created = true;
767 } else {
768 wl_list_remove(&ws->fsurf_front->view->layer_link);
769 wl_list_remove(&ws->fsurf_back->view->layer_link);
770 }
771
772 if (ws->focus_animation) {
773 weston_view_animation_destroy(ws->focus_animation);
774 ws->focus_animation = NULL;
775 }
776
777 if (to)
778 wl_list_insert(&to->layer_link,
779 &ws->fsurf_front->view->layer_link);
780 else if (from)
781 wl_list_insert(&ws->layer.view_list,
782 &ws->fsurf_front->view->layer_link);
783
784 if (focus_surface_created) {
785 ws->focus_animation = weston_fade_run(
786 ws->fsurf_front->view,
787 ws->fsurf_front->view->alpha, 0.6, 300,
788 focus_animation_done, ws);
789 } else if (from) {
790 wl_list_insert(&from->layer_link,
791 &ws->fsurf_back->view->layer_link);
792 ws->focus_animation = weston_stable_fade_run(
793 ws->fsurf_front->view, 0.0,
794 ws->fsurf_back->view, 0.6,
795 focus_animation_done, ws);
796 } else if (to) {
797 wl_list_insert(&ws->layer.view_list,
798 &ws->fsurf_back->view->layer_link);
799 ws->focus_animation = weston_stable_fade_run(
800 ws->fsurf_front->view, 0.0,
801 ws->fsurf_back->view, 0.6,
802 focus_animation_done, ws);
803 }
804}
805
806static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200807workspace_destroy(struct workspace *ws)
808{
Jonas Ådahl04769742012-06-13 00:01:24 +0200809 struct focus_state *state, *next;
810
811 wl_list_for_each_safe(state, next, &ws->focus_list, link)
812 focus_state_destroy(state);
813
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100814 if (ws->fsurf_front)
815 focus_surface_destroy(ws->fsurf_front);
816 if (ws->fsurf_back)
817 focus_surface_destroy(ws->fsurf_back);
818
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200819 free(ws);
820}
821
Jonas Ådahl04769742012-06-13 00:01:24 +0200822static void
823seat_destroyed(struct wl_listener *listener, void *data)
824{
825 struct weston_seat *seat = data;
826 struct focus_state *state, *next;
827 struct workspace *ws = container_of(listener,
828 struct workspace,
829 seat_destroyed_listener);
830
831 wl_list_for_each_safe(state, next, &ws->focus_list, link)
832 if (state->seat == seat)
833 wl_list_remove(&state->link);
834}
835
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200836static struct workspace *
837workspace_create(void)
838{
839 struct workspace *ws = malloc(sizeof *ws);
840 if (ws == NULL)
841 return NULL;
842
843 weston_layer_init(&ws->layer, NULL);
844
Jonas Ådahl04769742012-06-13 00:01:24 +0200845 wl_list_init(&ws->focus_list);
846 wl_list_init(&ws->seat_destroyed_listener.link);
847 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100848 ws->fsurf_front = NULL;
849 ws->fsurf_back = NULL;
850 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200851
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200852 return ws;
853}
854
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200855static int
856workspace_is_empty(struct workspace *ws)
857{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500858 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200859}
860
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200861static struct workspace *
862get_workspace(struct desktop_shell *shell, unsigned int index)
863{
864 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200865 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200866 pws += index;
867 return *pws;
868}
869
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800870struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200871get_current_workspace(struct desktop_shell *shell)
872{
873 return get_workspace(shell, shell->workspaces.current);
874}
875
876static void
877activate_workspace(struct desktop_shell *shell, unsigned int index)
878{
879 struct workspace *ws;
880
881 ws = get_workspace(shell, index);
882 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
883
884 shell->workspaces.current = index;
885}
886
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200887static unsigned int
888get_output_height(struct weston_output *output)
889{
890 return abs(output->region.extents.y1 - output->region.extents.y2);
891}
892
893static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100894view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200895{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200896 struct weston_transform *transform;
897
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100898 if (is_focus_view(view)) {
899 struct focus_surface *fsurf = get_focus_surface(view->surface);
900 transform = &fsurf->workspace_transform;
901 } else {
902 struct shell_surface *shsurf = get_shell_surface(view->surface);
903 transform = &shsurf->workspace_transform;
904 }
905
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200906 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100908 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200909
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100910 weston_matrix_init(&transform->matrix);
911 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200914}
915
916static void
917workspace_translate_out(struct workspace *ws, double fraction)
918{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200920 unsigned int height;
921 double d;
922
Jason Ekstranda7af7042013-10-12 22:38:11 -0500923 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
924 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200925 d = height * fraction;
926
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100927 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200928 }
929}
930
931static void
932workspace_translate_in(struct workspace *ws, double fraction)
933{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200935 unsigned int height;
936 double d;
937
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
939 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200940
941 if (fraction > 0)
942 d = -(height - height * fraction);
943 else
944 d = height + height * fraction;
945
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100946 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200947 }
948}
949
950static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200951broadcast_current_workspace_state(struct desktop_shell *shell)
952{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700953 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200954
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700955 wl_resource_for_each(resource, &shell->workspaces.client_list)
956 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200957 shell->workspaces.current,
958 shell->workspaces.num);
959}
960
961static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200962reverse_workspace_change_animation(struct desktop_shell *shell,
963 unsigned int index,
964 struct workspace *from,
965 struct workspace *to)
966{
967 shell->workspaces.current = index;
968
969 shell->workspaces.anim_to = to;
970 shell->workspaces.anim_from = from;
971 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
972 shell->workspaces.anim_timestamp = 0;
973
Scott Moreau4272e632012-08-13 09:58:41 -0600974 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200975}
976
977static void
978workspace_deactivate_transforms(struct workspace *ws)
979{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500980 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100981 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200982
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100984 if (is_focus_view(view)) {
985 struct focus_surface *fsurf = get_focus_surface(view->surface);
986 transform = &fsurf->workspace_transform;
987 } else {
988 struct shell_surface *shsurf = get_shell_surface(view->surface);
989 transform = &shsurf->workspace_transform;
990 }
991
992 if (!wl_list_empty(&transform->link)) {
993 wl_list_remove(&transform->link);
994 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200995 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997 }
998}
999
1000static void
1001finish_workspace_change_animation(struct desktop_shell *shell,
1002 struct workspace *from,
1003 struct workspace *to)
1004{
Scott Moreau4272e632012-08-13 09:58:41 -06001005 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001006
1007 wl_list_remove(&shell->workspaces.animation.link);
1008 workspace_deactivate_transforms(from);
1009 workspace_deactivate_transforms(to);
1010 shell->workspaces.anim_to = NULL;
1011
1012 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1013}
1014
1015static void
1016animate_workspace_change_frame(struct weston_animation *animation,
1017 struct weston_output *output, uint32_t msecs)
1018{
1019 struct desktop_shell *shell =
1020 container_of(animation, struct desktop_shell,
1021 workspaces.animation);
1022 struct workspace *from = shell->workspaces.anim_from;
1023 struct workspace *to = shell->workspaces.anim_to;
1024 uint32_t t;
1025 double x, y;
1026
1027 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1028 finish_workspace_change_animation(shell, from, to);
1029 return;
1030 }
1031
1032 if (shell->workspaces.anim_timestamp == 0) {
1033 if (shell->workspaces.anim_current == 0.0)
1034 shell->workspaces.anim_timestamp = msecs;
1035 else
1036 shell->workspaces.anim_timestamp =
1037 msecs -
1038 /* Invers of movement function 'y' below. */
1039 (asin(1.0 - shell->workspaces.anim_current) *
1040 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1041 M_2_PI);
1042 }
1043
1044 t = msecs - shell->workspaces.anim_timestamp;
1045
1046 /*
1047 * x = [0, π/2]
1048 * y(x) = sin(x)
1049 */
1050 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1051 y = sin(x);
1052
1053 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001054 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055
1056 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1057 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1058 shell->workspaces.anim_current = y;
1059
Scott Moreau4272e632012-08-13 09:58:41 -06001060 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001061 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001062 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001063 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001064}
1065
1066static void
1067animate_workspace_change(struct desktop_shell *shell,
1068 unsigned int index,
1069 struct workspace *from,
1070 struct workspace *to)
1071{
1072 struct weston_output *output;
1073
1074 int dir;
1075
1076 if (index > shell->workspaces.current)
1077 dir = -1;
1078 else
1079 dir = 1;
1080
1081 shell->workspaces.current = index;
1082
1083 shell->workspaces.anim_dir = dir;
1084 shell->workspaces.anim_from = from;
1085 shell->workspaces.anim_to = to;
1086 shell->workspaces.anim_current = 0.0;
1087 shell->workspaces.anim_timestamp = 0;
1088
1089 output = container_of(shell->compositor->output_list.next,
1090 struct weston_output, link);
1091 wl_list_insert(&output->animation_list,
1092 &shell->workspaces.animation.link);
1093
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001094 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001095
1096 workspace_translate_in(to, 0);
1097
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001098 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001099
Scott Moreau4272e632012-08-13 09:58:41 -06001100 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001101}
1102
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001103static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001104update_workspace(struct desktop_shell *shell, unsigned int index,
1105 struct workspace *from, struct workspace *to)
1106{
1107 shell->workspaces.current = index;
1108 wl_list_insert(&from->layer.link, &to->layer.link);
1109 wl_list_remove(&from->layer.link);
1110}
1111
1112static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001113change_workspace(struct desktop_shell *shell, unsigned int index)
1114{
1115 struct workspace *from;
1116 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001117 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001118
1119 if (index == shell->workspaces.current)
1120 return;
1121
1122 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001124 return;
1125
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001126 from = get_current_workspace(shell);
1127 to = get_workspace(shell, index);
1128
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129 if (shell->workspaces.anim_from == to &&
1130 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001131 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001132 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001133 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001134 return;
1135 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001136
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001137 if (shell->workspaces.anim_to != NULL)
1138 finish_workspace_change_animation(shell,
1139 shell->workspaces.anim_from,
1140 shell->workspaces.anim_to);
1141
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001142 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001143
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001144 if (shell->focus_animation_type != ANIMATION_NONE) {
1145 wl_list_for_each(state, &from->focus_list, link)
1146 if (state->keyboard_focus)
1147 animate_focus_change(shell, from,
1148 get_default_view(state->keyboard_focus), NULL);
1149
1150 wl_list_for_each(state, &to->focus_list, link)
1151 if (state->keyboard_focus)
1152 animate_focus_change(shell, to,
1153 NULL, get_default_view(state->keyboard_focus));
1154 }
1155
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001156 if (workspace_is_empty(to) && workspace_is_empty(from))
1157 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001158 else
1159 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001160
1161 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001162}
1163
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001164static bool
1165workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1166{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001168 struct wl_list *e;
1169
1170 if (wl_list_empty(list))
1171 return false;
1172
1173 e = list->next;
1174
1175 if (e->next != list)
1176 return false;
1177
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001179}
1180
1181static void
Philip Withnall659163d2013-11-25 18:01:36 +00001182move_surface_to_workspace(struct desktop_shell *shell,
1183 struct shell_surface *shsurf,
1184 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001185{
1186 struct workspace *from;
1187 struct workspace *to;
1188 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001189 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001190 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001191
1192 if (workspace == shell->workspaces.current)
1193 return;
1194
Philip Withnall659163d2013-11-25 18:01:36 +00001195 view = get_default_view(shsurf->surface);
1196 if (!view)
1197 return;
1198
1199 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1200
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001201 if (workspace >= shell->workspaces.num)
1202 workspace = shell->workspaces.num - 1;
1203
Jonas Ådahle9d22502012-08-29 22:13:01 +02001204 from = get_current_workspace(shell);
1205 to = get_workspace(shell, workspace);
1206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 wl_list_remove(&view->layer_link);
1208 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001209
Philip Withnall648a4dd2013-11-25 18:01:44 +00001210 shell_surface_update_child_surface_layers(shsurf);
1211
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001213 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1214 if (!seat->keyboard)
1215 continue;
1216
1217 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001219 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001220 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001221
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001223}
1224
1225static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001226take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001227 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001228 unsigned int index)
1229{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001230 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001232 struct shell_surface *shsurf;
1233 struct workspace *from;
1234 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001235 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001236
Pekka Paalanen01388e22013-04-25 13:57:44 +03001237 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238 view = get_default_view(surface);
1239 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001240 index == shell->workspaces.current ||
1241 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001242 return;
1243
1244 from = get_current_workspace(shell);
1245 to = get_workspace(shell, index);
1246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 wl_list_remove(&view->layer_link);
1248 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001249
Philip Withnall659163d2013-11-25 18:01:36 +00001250 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001251 if (shsurf != NULL)
1252 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001253
Jonas Ådahle9d22502012-08-29 22:13:01 +02001254 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001255 drop_focus_state(shell, from, surface);
1256
1257 if (shell->workspaces.anim_from == to &&
1258 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001259 wl_list_remove(&to->layer.link);
1260 wl_list_insert(from->layer.link.prev, &to->layer.link);
1261
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001262 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001263 broadcast_current_workspace_state(shell);
1264
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001265 return;
1266 }
1267
1268 if (shell->workspaces.anim_to != NULL)
1269 finish_workspace_change_animation(shell,
1270 shell->workspaces.anim_from,
1271 shell->workspaces.anim_to);
1272
1273 if (workspace_is_empty(from) &&
1274 workspace_has_only(to, surface))
1275 update_workspace(shell, index, from, to);
1276 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001277 if (shsurf != NULL &&
1278 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001279 wl_list_insert(&shell->workspaces.anim_sticky_list,
1280 &shsurf->workspace_transform.link);
1281
1282 animate_workspace_change(shell, index, from, to);
1283 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001284
1285 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001286
1287 state = ensure_focus_state(shell, seat);
1288 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001289 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001290}
1291
1292static void
1293workspace_manager_move_surface(struct wl_client *client,
1294 struct wl_resource *resource,
1295 struct wl_resource *surface_resource,
1296 uint32_t workspace)
1297{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001298 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001299 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001300 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001301 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001302 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001303
Pekka Paalanen01388e22013-04-25 13:57:44 +03001304 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001305 shell_surface = get_shell_surface(main_surface);
1306 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001308
1309 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001310}
1311
1312static const struct workspace_manager_interface workspace_manager_implementation = {
1313 workspace_manager_move_surface,
1314};
1315
1316static void
1317unbind_resource(struct wl_resource *resource)
1318{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001319 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001320}
1321
1322static void
1323bind_workspace_manager(struct wl_client *client,
1324 void *data, uint32_t version, uint32_t id)
1325{
1326 struct desktop_shell *shell = data;
1327 struct wl_resource *resource;
1328
Jason Ekstranda85118c2013-06-27 20:17:02 -05001329 resource = wl_resource_create(client,
1330 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001331
1332 if (resource == NULL) {
1333 weston_log("couldn't add workspace manager object");
1334 return;
1335 }
1336
Jason Ekstranda85118c2013-06-27 20:17:02 -05001337 wl_resource_set_implementation(resource,
1338 &workspace_manager_implementation,
1339 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001340 wl_list_insert(&shell->workspaces.client_list,
1341 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001342
1343 workspace_manager_send_state(resource,
1344 shell->workspaces.current,
1345 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001346}
1347
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001348static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001349touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1350 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1351{
1352}
1353
1354static void
1355touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1356{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001357 struct weston_touch_move_grab *move =
1358 (struct weston_touch_move_grab *) container_of(
1359 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001360
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001361 if (touch_id == 0)
1362 move->active = 0;
1363
Jonas Ådahl9484b692013-12-02 22:05:03 +01001364 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001365 shell_touch_grab_end(&move->base);
1366 free(move);
1367 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001368}
1369
1370static void
1371touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1372 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1373{
1374 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1375 struct shell_surface *shsurf = move->base.shsurf;
1376 struct weston_surface *es;
1377 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1378 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1379
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001380 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001381 return;
1382
1383 es = shsurf->surface;
1384
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001385 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001386
1387 weston_compositor_schedule_repaint(es->compositor);
1388}
1389
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001390static void
1391touch_move_grab_cancel(struct weston_touch_grab *grab)
1392{
1393 struct weston_touch_move_grab *move =
1394 (struct weston_touch_move_grab *) container_of(
1395 grab, struct shell_touch_grab, grab);
1396
1397 shell_touch_grab_end(&move->base);
1398 free(move);
1399}
1400
Rusty Lynch1084da52013-08-15 09:10:08 -07001401static const struct weston_touch_grab_interface touch_move_grab_interface = {
1402 touch_move_grab_down,
1403 touch_move_grab_up,
1404 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001405 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001406};
1407
1408static int
1409surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1410{
1411 struct weston_touch_move_grab *move;
1412
1413 if (!shsurf)
1414 return -1;
1415
Rafael Antognolli03b16592013-12-03 15:35:42 -02001416 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001417 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001418 if (shsurf->grabbed)
1419 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001420
1421 move = malloc(sizeof *move);
1422 if (!move)
1423 return -1;
1424
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001425 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001427 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001429 seat->touch->grab_y;
1430
1431 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1432 seat->touch);
1433
1434 return 0;
1435}
1436
1437static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001438noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001439{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001440}
1441
1442static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001443move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1444 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001445{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001446 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001447 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001448 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001449 int dx, dy;
1450
1451 weston_pointer_move(pointer, x, y);
1452 dx = wl_fixed_to_int(pointer->x + move->dx);
1453 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001454
1455 if (!shsurf)
1456 return;
1457
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001458 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001459
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001461}
1462
1463static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001464move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001465 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001466{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001467 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1468 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001469 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001470 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001471
Daniel Stone4dbadb12012-05-30 16:31:51 +01001472 if (pointer->button_count == 0 &&
1473 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001474 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001475 free(grab);
1476 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001477}
1478
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001479static void
1480move_grab_cancel(struct weston_pointer_grab *grab)
1481{
1482 struct shell_grab *shell_grab =
1483 container_of(grab, struct shell_grab, grab);
1484
1485 shell_grab_end(shell_grab);
1486 free(grab);
1487}
1488
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001489static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001490 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001491 move_grab_motion,
1492 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001493 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001494};
1495
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001496static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001497surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001498{
1499 struct weston_move_grab *move;
1500
1501 if (!shsurf)
1502 return -1;
1503
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001504 if (shsurf->grabbed)
1505 return 0;
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001506 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001507 return 0;
1508
1509 move = malloc(sizeof *move);
1510 if (!move)
1511 return -1;
1512
Jason Ekstranda7af7042013-10-12 22:38:11 -05001513 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001514 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001516 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001517
1518 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001519 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001520
1521 return 0;
1522}
1523
1524static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001525common_surface_move(struct wl_resource *resource,
1526 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001527{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001528 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001529 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001530 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001531
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001532 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001533 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001534 seat->pointer->button_count > 0 &&
1535 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001537 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001538 (surface_move(shsurf, seat) < 0))
1539 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001540 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001541 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001542 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001544 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001545 (surface_touch_move(shsurf, seat) < 0))
1546 wl_resource_post_no_memory(resource);
1547 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001548}
1549
Rafael Antognollie2a34552013-12-03 15:35:45 -02001550static void
1551shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1552 struct wl_resource *seat_resource, uint32_t serial)
1553{
1554 common_surface_move(resource, seat_resource, serial);
1555}
1556
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001557struct weston_resize_grab {
1558 struct shell_grab base;
1559 uint32_t edges;
1560 int32_t width, height;
1561};
1562
1563static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001564resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1565 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001566{
1567 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001568 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001569 struct shell_surface *shsurf = resize->base.shsurf;
1570 int32_t width, height;
1571 wl_fixed_t from_x, from_y;
1572 wl_fixed_t to_x, to_y;
1573
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001574 weston_pointer_move(pointer, x, y);
1575
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001576 if (!shsurf)
1577 return;
1578
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 weston_view_from_global_fixed(shsurf->view,
1580 pointer->grab_x, pointer->grab_y,
1581 &from_x, &from_y);
1582 weston_view_from_global_fixed(shsurf->view,
1583 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584
1585 width = resize->width;
1586 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1587 width += wl_fixed_to_int(from_x - to_x);
1588 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1589 width += wl_fixed_to_int(to_x - from_x);
1590 }
1591
1592 height = resize->height;
1593 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1594 height += wl_fixed_to_int(from_y - to_y);
1595 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1596 height += wl_fixed_to_int(to_y - from_y);
1597 }
1598
1599 shsurf->client->send_configure(shsurf->surface,
1600 resize->edges, width, height);
1601}
1602
1603static void
1604send_configure(struct weston_surface *surface,
1605 uint32_t edges, int32_t width, int32_t height)
1606{
1607 struct shell_surface *shsurf = get_shell_surface(surface);
1608
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001609 assert(shsurf);
1610
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001611 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001612 edges, width, height);
1613}
1614
1615static const struct weston_shell_client shell_client = {
1616 send_configure
1617};
1618
1619static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001620resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621 uint32_t time, uint32_t button, uint32_t state_w)
1622{
1623 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001624 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001625 enum wl_pointer_button_state state = state_w;
1626
1627 if (pointer->button_count == 0 &&
1628 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1629 shell_grab_end(&resize->base);
1630 free(grab);
1631 }
1632}
1633
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001634static void
1635resize_grab_cancel(struct weston_pointer_grab *grab)
1636{
1637 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1638
1639 shell_grab_end(&resize->base);
1640 free(grab);
1641}
1642
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001643static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644 noop_grab_focus,
1645 resize_grab_motion,
1646 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001647 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001648};
1649
Giulio Camuffob8366642013-04-25 13:57:46 +03001650/*
1651 * Returns the bounding box of a surface and all its sub-surfaces,
1652 * in the surface coordinates system. */
1653static void
1654surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1655 int32_t *y, int32_t *w, int32_t *h) {
1656 pixman_region32_t region;
1657 pixman_box32_t *box;
1658 struct weston_subsurface *subsurface;
1659
1660 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001661 surface->width,
1662 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001663
1664 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1665 pixman_region32_union_rect(&region, &region,
1666 subsurface->position.x,
1667 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001668 subsurface->surface->width,
1669 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001670 }
1671
1672 box = pixman_region32_extents(&region);
1673 if (x)
1674 *x = box->x1;
1675 if (y)
1676 *y = box->y1;
1677 if (w)
1678 *w = box->x2 - box->x1;
1679 if (h)
1680 *h = box->y2 - box->y1;
1681
1682 pixman_region32_fini(&region);
1683}
1684
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001685static int
1686surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001687 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688{
1689 struct weston_resize_grab *resize;
1690
Rafael Antognolli03b16592013-12-03 15:35:42 -02001691 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001692 return 0;
1693
1694 if (edges == 0 || edges > 15 ||
1695 (edges & 3) == 3 || (edges & 12) == 12)
1696 return 0;
1697
1698 resize = malloc(sizeof *resize);
1699 if (!resize)
1700 return -1;
1701
1702 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001703 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1704 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001705
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001706 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001707 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001708 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709
1710 return 0;
1711}
1712
1713static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001714common_surface_resize(struct wl_resource *resource,
1715 struct wl_resource *seat_resource, uint32_t serial,
1716 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001717{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001718 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001719 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001720 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001721
Rafael Antognolli03b16592013-12-03 15:35:42 -02001722 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001723 return;
1724
Kristian Høgsberge3148752013-05-06 23:19:49 -04001725 if (seat->pointer->button_count == 0 ||
1726 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001727 seat->pointer->focus == NULL)
1728 return;
1729
1730 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1731 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732 return;
1733
Kristian Høgsberge3148752013-05-06 23:19:49 -04001734 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001735 wl_resource_post_no_memory(resource);
1736}
1737
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001738static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001739shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1740 struct wl_resource *seat_resource, uint32_t serial,
1741 uint32_t edges)
1742{
1743 common_surface_resize(resource, seat_resource, serial, edges);
1744}
1745
1746static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001747busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001748{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001749 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001750 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001751 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001752 wl_fixed_t sx, sy;
1753
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754 view = weston_compositor_pick_view(pointer->seat->compositor,
1755 pointer->x, pointer->y,
1756 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001757
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001758 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1759 shell_grab_end(grab);
1760 free(grab);
1761 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001762}
1763
1764static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001765busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1766 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001767{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001768 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001769}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001770
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001771static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001772busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001773 uint32_t time, uint32_t button, uint32_t state)
1774{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001775 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001776 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001777 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001778
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001779 if (shsurf && button == BTN_LEFT && state) {
1780 activate(shsurf->shell, shsurf->surface, seat);
1781 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001782 } else if (shsurf && button == BTN_RIGHT && state) {
1783 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001784 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001785 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001786}
1787
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001788static void
1789busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1790{
1791 struct shell_grab *grab = (struct shell_grab *) base;
1792
1793 shell_grab_end(grab);
1794 free(grab);
1795}
1796
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001797static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001798 busy_cursor_grab_focus,
1799 busy_cursor_grab_motion,
1800 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001801 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001802};
1803
1804static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001805set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001806{
1807 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001808
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001809 if (pointer->grab->interface == &busy_cursor_grab_interface)
1810 return;
1811
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001812 grab = malloc(sizeof *grab);
1813 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001814 return;
1815
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001816 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1817 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001818}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001819
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001820static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001821end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001822{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001823 struct shell_grab *grab;
1824 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001826 wl_list_for_each(seat, &compositor->seat_list, link) {
1827 if (seat->pointer == NULL)
1828 continue;
1829
1830 grab = (struct shell_grab *) seat->pointer->grab;
1831 if (grab->grab.interface == &busy_cursor_grab_interface &&
1832 wl_resource_get_client(grab->shsurf->resource) == client) {
1833 shell_grab_end(grab);
1834 free(grab);
1835 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001836 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001837}
1838
Scott Moreau9521d5e2012-04-19 13:06:17 -06001839static void
1840ping_timer_destroy(struct shell_surface *shsurf)
1841{
1842 if (!shsurf || !shsurf->ping_timer)
1843 return;
1844
1845 if (shsurf->ping_timer->source)
1846 wl_event_source_remove(shsurf->ping_timer->source);
1847
1848 free(shsurf->ping_timer);
1849 shsurf->ping_timer = NULL;
1850}
1851
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001852static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001853ping_timeout_handler(void *data)
1854{
1855 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001856 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001857
Scott Moreau9521d5e2012-04-19 13:06:17 -06001858 /* Client is not responding */
1859 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001860
1861 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsberg8fe412d2014-01-17 16:39:17 -08001862 if (seat->pointer && seat->pointer->focus &&
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001863 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
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001869static const int ping_timeout = 200;
1870
1871static void
1872handle_wl_shell_ping(struct shell_surface *shsurf, uint32_t serial)
1873{
1874 struct wl_event_loop *loop;
1875 struct weston_compositor *compositor = shsurf->shell->compositor;
1876
1877 if (shsurf->ping_timer)
1878 return;
1879
1880 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
1881 if (!shsurf->ping_timer)
1882 return;
1883
1884 shsurf->ping_timer->serial = serial;
1885 loop = wl_display_get_event_loop(compositor->wl_display);
1886 shsurf->ping_timer->source =
1887 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1888 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1889
1890 wl_shell_surface_send_ping(shsurf->resource, serial);
1891}
1892
1893static void
1894handle_shell_client_destroy(struct wl_listener *listener, void *data);
1895
1896static int
1897xdg_ping_timeout_handler(void *data)
1898{
1899 struct shell_client *sc = data;
1900 struct weston_seat *seat;
1901 struct shell_surface *shsurf;
1902
1903 /* Client is not responding */
1904 sc->unresponsive = 1;
1905
1906 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1907 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1908 continue;
1909 if (seat->pointer->focus->surface->resource == NULL)
1910 continue;
1911
1912 shsurf = get_shell_surface(seat->pointer->focus->surface);
1913 if (shsurf &&
1914 wl_resource_get_client(shsurf->resource) == sc->client)
1915 set_busy_cursor(shsurf, seat->pointer);
1916 }
1917
1918 return 1;
1919}
1920
1921static void
1922handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1923{
1924 struct weston_compositor *compositor = shsurf->shell->compositor;
1925 struct wl_listener *listener;
1926 struct wl_client *client = wl_resource_get_client(shsurf->resource);
1927 struct shell_client *sc;
1928 struct wl_event_loop *loop;
1929
1930 listener = wl_client_get_destroy_listener(client,
1931 handle_shell_client_destroy);
1932 sc = container_of(listener, struct shell_client, destroy_listener);
1933
1934 if (sc->unresponsive) {
1935 xdg_ping_timeout_handler(sc);
1936 return;
1937 }
1938
1939 sc->ping_serial = serial;
1940 loop = wl_display_get_event_loop(compositor->wl_display);
1941 if (sc->ping_timer == NULL)
1942 sc->ping_timer =
1943 wl_event_loop_add_timer(loop,
1944 xdg_ping_timeout_handler, sc);
1945 if (sc->ping_timer == NULL)
1946 return;
1947
1948 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1949
1950 xdg_shell_send_ping(sc->resource, serial);
1951}
1952
Scott Moreauff1db4a2012-04-17 19:06:18 -06001953static void
1954ping_handler(struct weston_surface *surface, uint32_t serial)
1955{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001956 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001957
1958 if (!shsurf)
1959 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001960 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001961 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001962 if (shsurf->surface == shsurf->shell->grab_surface)
1963 return;
1964
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001965 if (shell_surface_is_xdg_surface(shsurf) ||
1966 shell_surface_is_xdg_popup(shsurf))
1967 handle_xdg_ping(shsurf, serial);
1968 else if (shell_surface_is_wl_shell_surface(shsurf))
1969 handle_wl_shell_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001970}
1971
1972static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001973handle_pointer_focus(struct wl_listener *listener, void *data)
1974{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001975 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001976 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001977 struct weston_compositor *compositor;
1978 struct shell_surface *shsurf;
1979 uint32_t serial;
1980
Jason Ekstranda7af7042013-10-12 22:38:11 -05001981 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001982 return;
1983
Jason Ekstranda7af7042013-10-12 22:38:11 -05001984 compositor = view->surface->compositor;
1985 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001986
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001987 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001988 set_busy_cursor(shsurf, pointer);
1989 } else {
1990 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001991 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001992 }
1993}
1994
1995static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001996create_pointer_focus_listener(struct weston_seat *seat)
1997{
1998 struct wl_listener *listener;
1999
Kristian Høgsberge3148752013-05-06 23:19:49 -04002000 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04002001 return;
2002
2003 listener = malloc(sizeof *listener);
2004 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002005 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04002006}
2007
2008static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002009shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2010{
2011 if (--shsurf->focus_count == 0)
2012 if (shell_surface_is_xdg_surface(shsurf))
2013 xdg_surface_send_focused_unset(shsurf->resource);
2014}
2015
2016static void
2017shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2018{
2019 if (shsurf->focus_count++ == 0)
2020 if (shell_surface_is_xdg_surface(shsurf))
2021 xdg_surface_send_focused_set(shsurf->resource);
2022}
2023
2024static void
2025handle_keyboard_focus(struct wl_listener *listener, void *data)
2026{
2027 struct weston_keyboard *keyboard = data;
2028 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2029
2030 if (seat->focused_surface) {
2031 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2032 if (shsurf)
2033 shell_surface_lose_keyboard_focus(shsurf);
2034 }
2035
2036 seat->focused_surface = keyboard->focus;
2037
2038 if (seat->focused_surface) {
2039 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2040 if (shsurf)
2041 shell_surface_gain_keyboard_focus(shsurf);
2042 }
2043}
2044
2045static void
2046create_keyboard_focus_listener(struct weston_seat *seat)
2047{
2048 struct wl_listener *listener;
2049
2050 if (!seat->keyboard)
2051 return;
2052
2053 listener = malloc(sizeof *listener);
2054 listener->notify = handle_keyboard_focus;
2055 wl_signal_add(&seat->keyboard->focus_signal, listener);
2056}
2057
2058static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002059xdg_surface_set_transient_for(struct wl_client *client,
2060 struct wl_resource *resource,
2061 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06002062{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002063 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05002064 struct weston_surface *parent;
2065
2066 if (parent_resource)
2067 parent = wl_resource_get_user_data(parent_resource);
2068 else
2069 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002070
2071 shell_surface_set_parent(shsurf, parent);
2072}
2073
2074static void
2075surface_pong(struct shell_surface *shsurf, uint32_t serial)
2076{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002077 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002078
Kristian Høgsberg92374e12012-08-11 22:39:12 -04002079 if (shsurf->ping_timer == NULL)
2080 /* Just ignore unsolicited pong. */
2081 return;
2082
Scott Moreauff1db4a2012-04-17 19:06:18 -06002083 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002084 shsurf->unresponsive = 0;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002085 end_busy_cursor(ec, wl_resource_get_client(shsurf->resource));
Scott Moreauff1db4a2012-04-17 19:06:18 -06002086 }
2087}
2088
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002089static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002090shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
2091 uint32_t serial)
2092{
2093 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2094
2095 surface_pong(shsurf, serial);
2096
2097}
2098
2099static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002100set_title(struct shell_surface *shsurf, const char *title)
2101{
2102 free(shsurf->title);
2103 shsurf->title = strdup(title);
2104}
2105
2106static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002107shell_surface_set_title(struct wl_client *client,
2108 struct wl_resource *resource, const char *title)
2109{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002110 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002111
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002112 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002113}
2114
2115static void
2116shell_surface_set_class(struct wl_client *client,
2117 struct wl_resource *resource, const char *class)
2118{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002119 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002120
2121 free(shsurf->class);
2122 shsurf->class = strdup(class);
2123}
2124
Alex Wu4539b082012-03-01 12:57:46 +08002125static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002126restore_output_mode(struct weston_output *output)
2127{
Hardening57388e42013-09-18 23:56:36 +02002128 if (output->current_mode != output->original_mode ||
2129 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002130 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002131 output->original_mode,
2132 output->original_scale,
2133 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002134}
2135
2136static void
2137restore_all_output_modes(struct weston_compositor *compositor)
2138{
2139 struct weston_output *output;
2140
2141 wl_list_for_each(output, &compositor->output_list, link)
2142 restore_output_mode(output);
2143}
2144
Philip Withnallbecb77e2013-11-25 18:01:30 +00002145static int
2146get_output_panel_height(struct desktop_shell *shell,
2147 struct weston_output *output)
2148{
2149 struct weston_view *view;
2150 int panel_height = 0;
2151
2152 if (!output)
2153 return 0;
2154
2155 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2156 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002157 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002158 break;
2159 }
2160 }
2161
2162 return panel_height;
2163}
2164
Philip Withnall07926d92013-11-25 18:01:40 +00002165/* The surface will be inserted into the list immediately after the link
2166 * returned by this function (i.e. will be stacked immediately above the
2167 * returned link). */
2168static struct wl_list *
2169shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2170{
2171 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002172 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002173
2174 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002175 case SHELL_SURFACE_POPUP:
2176 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002177 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002178 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002179 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002180 /* Move the surface to its parent layer so
2181 * that surfaces which are transient for
2182 * fullscreen surfaces don't get hidden by the
2183 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002184
2185 /* TODO: Handle a parent with multiple views */
2186 parent = get_default_view(shsurf->parent);
2187 if (parent)
2188 return parent->layer_link.prev;
2189 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002190 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002191
2192 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002193 return &shsurf->shell->fullscreen_layer.view_list;
2194
Philip Withnall07926d92013-11-25 18:01:40 +00002195 case SHELL_SURFACE_NONE:
2196 default:
2197 /* Go to the fallback, below. */
2198 break;
2199 }
2200
2201 /* Move the surface to a normal workspace layer so that surfaces
2202 * which were previously fullscreen or transient are no longer
2203 * rendered on top. */
2204 ws = get_current_workspace(shsurf->shell);
2205 return &ws->layer.view_list;
2206}
2207
Philip Withnall648a4dd2013-11-25 18:01:44 +00002208static void
2209shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2210{
2211 struct shell_surface *child;
2212
2213 /* Move the child layers to the same workspace as shsurf. They will be
2214 * stacked above shsurf. */
2215 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2216 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2217 weston_view_geometry_dirty(child->view);
2218 wl_list_remove(&child->view->layer_link);
2219 wl_list_insert(shsurf->view->layer_link.prev,
2220 &child->view->layer_link);
2221 weston_view_geometry_dirty(child->view);
2222 weston_surface_damage(child->surface);
2223
2224 /* Recurse. We don’t expect this to recurse very far (if
2225 * at all) because that would imply we have transient
2226 * (or popup) children of transient surfaces, which
2227 * would be unusual. */
2228 shell_surface_update_child_surface_layers(child);
2229 }
2230 }
2231}
2232
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002233/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002234 * geometry to ensure the changes are redrawn.
2235 *
2236 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2237 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002238static void
2239shell_surface_update_layer(struct shell_surface *shsurf)
2240{
2241 struct wl_list *new_layer_link;
2242
2243 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2244
2245 if (new_layer_link == &shsurf->view->layer_link)
2246 return;
2247
2248 weston_view_geometry_dirty(shsurf->view);
2249 wl_list_remove(&shsurf->view->layer_link);
2250 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2251 weston_view_geometry_dirty(shsurf->view);
2252 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002253
2254 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002255}
2256
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002257static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002258shell_surface_set_parent(struct shell_surface *shsurf,
2259 struct weston_surface *parent)
2260{
2261 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002262
2263 wl_list_remove(&shsurf->children_link);
2264 wl_list_init(&shsurf->children_link);
2265
2266 /* Insert into the parent surface’s child list. */
2267 if (parent != NULL) {
2268 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2269 if (parent_shsurf != NULL)
2270 wl_list_insert(&parent_shsurf->children_list,
2271 &shsurf->children_link);
2272 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002273}
2274
2275static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002276shell_surface_set_output(struct shell_surface *shsurf,
2277 struct weston_output *output)
2278{
2279 struct weston_surface *es = shsurf->surface;
2280
2281 /* get the default output, if the client set it as NULL
2282 check whether the ouput is available */
2283 if (output)
2284 shsurf->output = output;
2285 else if (es->output)
2286 shsurf->output = es->output;
2287 else
2288 shsurf->output = get_default_output(es->compositor);
2289}
2290
2291static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002292surface_clear_next_states(struct shell_surface *shsurf)
2293{
2294 shsurf->next_state.maximized = false;
2295 shsurf->next_state.fullscreen = false;
2296
2297 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2298 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2299 shsurf->state_changed = true;
2300}
2301
2302static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002303set_toplevel(struct shell_surface *shsurf)
2304{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002305 shell_surface_set_parent(shsurf, NULL);
2306 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002307 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002308
2309 /* The layer_link is updated in set_surface_type(),
2310 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002311}
2312
2313static void
2314shell_surface_set_toplevel(struct wl_client *client,
2315 struct wl_resource *resource)
2316{
2317 struct shell_surface *surface = wl_resource_get_user_data(resource);
2318
2319 set_toplevel(surface);
2320}
2321
2322static void
2323set_transient(struct shell_surface *shsurf,
2324 struct weston_surface *parent, int x, int y, uint32_t flags)
2325{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002326 assert(parent != NULL);
2327
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002328 shell_surface_set_parent(shsurf, parent);
2329
2330 surface_clear_next_states(shsurf);
2331
Philip Withnallbecb77e2013-11-25 18:01:30 +00002332 shsurf->transient.x = x;
2333 shsurf->transient.y = y;
2334 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002335
Rafael Antognollied207b42013-12-03 15:35:43 -02002336 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002337 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002338 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002339
2340 /* The layer_link is updated in set_surface_type(),
2341 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002342}
2343
2344static void
2345shell_surface_set_transient(struct wl_client *client,
2346 struct wl_resource *resource,
2347 struct wl_resource *parent_resource,
2348 int x, int y, uint32_t flags)
2349{
2350 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2351 struct weston_surface *parent =
2352 wl_resource_get_user_data(parent_resource);
2353
2354 set_transient(shsurf, parent, x, y, flags);
2355}
2356
2357static void
2358set_fullscreen(struct shell_surface *shsurf,
2359 uint32_t method,
2360 uint32_t framerate,
2361 struct weston_output *output)
2362{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002363 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002364
2365 shsurf->fullscreen_output = shsurf->output;
2366 shsurf->fullscreen.type = method;
2367 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002368
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002369 shsurf->next_state.fullscreen = true;
2370 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002371 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002372
2373 shsurf->client->send_configure(shsurf->surface, 0,
2374 shsurf->output->width,
2375 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002376
2377 /* The layer_link is updated in set_surface_type(),
2378 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002379}
2380
2381static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002382weston_view_set_initial_position(struct weston_view *view,
2383 struct desktop_shell *shell);
2384
2385static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002386unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002387{
Philip Withnallf85fe842013-11-25 18:01:37 +00002388 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002389 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2390 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002391 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002392 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002393 shsurf->fullscreen_output = NULL;
2394
Alex Wu4539b082012-03-01 12:57:46 +08002395 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2396 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002397
Alex Wu4539b082012-03-01 12:57:46 +08002398 wl_list_remove(&shsurf->fullscreen.transform.link);
2399 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002400
Jason Ekstranda7af7042013-10-12 22:38:11 -05002401 if (shsurf->fullscreen.black_view)
2402 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2403 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002404
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002405 if (shsurf->saved_position_valid)
2406 weston_view_set_position(shsurf->view,
2407 shsurf->saved_x, shsurf->saved_y);
2408 else
2409 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2410
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002411 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002412 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002413 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002414 shsurf->saved_rotation_valid = false;
2415 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002416
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002417 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002418}
2419
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002420static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002421shell_surface_set_fullscreen(struct wl_client *client,
2422 struct wl_resource *resource,
2423 uint32_t method,
2424 uint32_t framerate,
2425 struct wl_resource *output_resource)
2426{
2427 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2428 struct weston_output *output;
2429
2430 if (output_resource)
2431 output = wl_resource_get_user_data(output_resource);
2432 else
2433 output = NULL;
2434
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002435 shell_surface_set_parent(shsurf, NULL);
2436
Rafael Antognolli03b16592013-12-03 15:35:42 -02002437 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002438 set_fullscreen(shsurf, method, framerate, output);
2439}
2440
2441static void
2442set_popup(struct shell_surface *shsurf,
2443 struct weston_surface *parent,
2444 struct weston_seat *seat,
2445 uint32_t serial,
2446 int32_t x,
2447 int32_t y)
2448{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002449 assert(parent != NULL);
2450
Philip Withnallbecb77e2013-11-25 18:01:30 +00002451 shsurf->popup.shseat = get_shell_seat(seat);
2452 shsurf->popup.serial = serial;
2453 shsurf->popup.x = x;
2454 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002455
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002456 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002457}
2458
2459static void
2460shell_surface_set_popup(struct wl_client *client,
2461 struct wl_resource *resource,
2462 struct wl_resource *seat_resource,
2463 uint32_t serial,
2464 struct wl_resource *parent_resource,
2465 int32_t x, int32_t y, uint32_t flags)
2466{
2467 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002468 struct weston_surface *parent =
2469 wl_resource_get_user_data(parent_resource);
2470
2471 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002472
Rafael Antognolli03b16592013-12-03 15:35:42 -02002473 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002474 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002475 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002476 wl_resource_get_user_data(seat_resource),
2477 serial, x, y);
2478}
2479
2480static void
2481set_maximized(struct shell_surface *shsurf,
2482 struct weston_output *output)
2483{
2484 struct desktop_shell *shell;
2485 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002486
Philip Withnall352e7ed2013-11-25 18:01:35 +00002487 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002488
2489 shell = shell_surface_get_shell(shsurf);
2490 panel_height = get_output_panel_height(shell, shsurf->output);
2491 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2492
2493 shsurf->client->send_configure(shsurf->surface, edges,
2494 shsurf->output->width,
2495 shsurf->output->height - panel_height);
2496
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002497 shsurf->next_state.maximized = true;
2498 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002499 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002500}
2501
2502static void
2503unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002504{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002505 /* undo all maximized things here */
2506 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002507
2508 if (shsurf->saved_position_valid)
2509 weston_view_set_position(shsurf->view,
2510 shsurf->saved_x, shsurf->saved_y);
2511 else
2512 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002513
2514 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002515 wl_list_insert(&shsurf->view->geometry.transformation_list,
2516 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002517 shsurf->saved_rotation_valid = false;
2518 }
2519
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002520 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002521}
2522
Philip Withnallbecb77e2013-11-25 18:01:30 +00002523static void
2524shell_surface_set_maximized(struct wl_client *client,
2525 struct wl_resource *resource,
2526 struct wl_resource *output_resource)
2527{
2528 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2529 struct weston_output *output;
2530
2531 if (output_resource)
2532 output = wl_resource_get_user_data(output_resource);
2533 else
2534 output = NULL;
2535
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002536 shell_surface_set_parent(shsurf, NULL);
2537
Rafael Antognolli03b16592013-12-03 15:35:42 -02002538 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002539 set_maximized(shsurf, output);
2540}
2541
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002542/* This is only ever called from set_surface_type(), so there’s no need to
2543 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002544static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002545reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002546{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002547 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002548 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002549 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002550 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002551
Pekka Paalanen98262232011-12-01 10:42:22 +02002552 return 0;
2553}
2554
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002555static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002556set_full_output(struct shell_surface *shsurf)
2557{
2558 shsurf->saved_x = shsurf->view->geometry.x;
2559 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002560 shsurf->saved_width = shsurf->surface->width;
2561 shsurf->saved_height = shsurf->surface->height;
2562 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002563 shsurf->saved_position_valid = true;
2564
2565 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2566 wl_list_remove(&shsurf->rotation.transform.link);
2567 wl_list_init(&shsurf->rotation.transform.link);
2568 weston_view_geometry_dirty(shsurf->view);
2569 shsurf->saved_rotation_valid = true;
2570 }
2571}
2572
2573static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002574set_surface_type(struct shell_surface *shsurf)
2575{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002576 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002577 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002578
Philip Withnallbecb77e2013-11-25 18:01:30 +00002579 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002580
Rafael Antognolli03b16592013-12-03 15:35:42 -02002581 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002582 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002583
2584 switch (shsurf->type) {
2585 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002586 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002587 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002588 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002589 weston_view_set_position(shsurf->view,
2590 pev->geometry.x + shsurf->transient.x,
2591 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002592 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002593 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002594
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002595 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002596 weston_view_set_position(shsurf->view, shsurf->transient.x,
2597 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002598 break;
2599
Philip Withnall0f640e22013-11-25 18:01:31 +00002600 case SHELL_SURFACE_POPUP:
2601 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002602 default:
2603 break;
2604 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002605
2606 /* Update the surface’s layer. */
2607 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002608}
2609
Tiago Vignattibe143262012-04-16 17:31:41 +03002610static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002611shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002612{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002613 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002614}
2615
Alex Wu21858432012-04-01 20:13:08 +08002616static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002617black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002618
Jason Ekstranda7af7042013-10-12 22:38:11 -05002619static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002620create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002621 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002622 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002623{
2624 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002625 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002626
2627 surface = weston_surface_create(ec);
2628 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002629 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002630 return NULL;
2631 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002632 view = weston_view_create(surface);
2633 if (surface == NULL) {
2634 weston_log("no memory\n");
2635 weston_surface_destroy(surface);
2636 return NULL;
2637 }
Alex Wu4539b082012-03-01 12:57:46 +08002638
Alex Wu21858432012-04-01 20:13:08 +08002639 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002640 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002641 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002642 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002643 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002644 pixman_region32_fini(&surface->input);
2645 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002646
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002647 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002648 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002649
2650 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002651}
2652
Philip Withnalled8f1a92013-11-25 18:01:43 +00002653static void
2654shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2655{
2656 struct weston_output *output = shsurf->fullscreen_output;
2657
Rafael Antognolli03b16592013-12-03 15:35:42 -02002658 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002659
2660 if (!shsurf->fullscreen.black_view)
2661 shsurf->fullscreen.black_view =
2662 create_black_surface(shsurf->surface->compositor,
2663 shsurf->surface,
2664 output->x, output->y,
2665 output->width,
2666 output->height);
2667
2668 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2669 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2670 wl_list_insert(&shsurf->view->layer_link,
2671 &shsurf->fullscreen.black_view->layer_link);
2672 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2673 weston_surface_damage(shsurf->surface);
2674}
2675
Alex Wu4539b082012-03-01 12:57:46 +08002676/* Create black surface and append it to the associated fullscreen surface.
2677 * Handle size dismatch and positioning according to the method. */
2678static void
2679shell_configure_fullscreen(struct shell_surface *shsurf)
2680{
2681 struct weston_output *output = shsurf->fullscreen_output;
2682 struct weston_surface *surface = shsurf->surface;
2683 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002684 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002685 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002686
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002687 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2688 restore_output_mode(output);
2689
Philip Withnalled8f1a92013-11-25 18:01:43 +00002690 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002691
Jason Ekstranda7af7042013-10-12 22:38:11 -05002692 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002693 &surf_width, &surf_height);
2694
Alex Wu4539b082012-03-01 12:57:46 +08002695 switch (shsurf->fullscreen.type) {
2696 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002697 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002698 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002699 break;
2700 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002701 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002702 if (output->width == surf_width &&
2703 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002704 weston_view_set_position(shsurf->view,
2705 output->x - surf_x,
2706 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002707 break;
2708 }
2709
Alex Wu4539b082012-03-01 12:57:46 +08002710 matrix = &shsurf->fullscreen.transform.matrix;
2711 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002712
Scott Moreau1bad5db2012-08-18 01:04:05 -06002713 output_aspect = (float) output->width /
2714 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 /* XXX: Use surf_width and surf_height here? */
2716 surface_aspect = (float) surface->width /
2717 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002718 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002719 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002720 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002721 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002722 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002723 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002724
Alex Wu4539b082012-03-01 12:57:46 +08002725 weston_matrix_scale(matrix, scale, scale, 1);
2726 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002727 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002728 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002729 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2730 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002731 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002732
Alex Wu4539b082012-03-01 12:57:46 +08002733 break;
2734 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002735 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002736 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002737 surf_width * surface->buffer_viewport.scale,
2738 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002739 shsurf->fullscreen.framerate};
2740
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002741 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002742 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002743 weston_view_set_position(shsurf->view,
2744 output->x - surf_x,
2745 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002746 shsurf->fullscreen.black_view->surface->width = output->width;
2747 shsurf->fullscreen.black_view->surface->height = output->height;
2748 weston_view_set_position(shsurf->fullscreen.black_view,
2749 output->x - surf_x,
2750 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002751 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002752 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002753 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002754 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002755 }
Alex Wubd3354b2012-04-17 17:20:49 +08002756 }
Alex Wu4539b082012-03-01 12:57:46 +08002757 break;
2758 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002759 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002760 break;
2761 default:
2762 break;
2763 }
2764}
2765
Alex Wu4539b082012-03-01 12:57:46 +08002766static void
2767shell_map_fullscreen(struct shell_surface *shsurf)
2768{
Alex Wubd3354b2012-04-17 17:20:49 +08002769 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002770}
2771
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002772static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002773set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2774{
2775 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002776 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002777 shsurf->transient.x = x;
2778 shsurf->transient.y = y;
2779 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002780
2781 shell_surface_set_parent(shsurf, NULL);
2782
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002783 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002784 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002785}
2786
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002787static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002788
2789static void
2790destroy_shell_seat(struct wl_listener *listener, void *data)
2791{
2792 struct shell_seat *shseat =
2793 container_of(listener,
2794 struct shell_seat, seat_destroy_listener);
2795 struct shell_surface *shsurf, *prev = NULL;
2796
2797 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002798 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002799 shseat->popup_grab.client = NULL;
2800
2801 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2802 shsurf->popup.shseat = NULL;
2803 if (prev) {
2804 wl_list_init(&prev->popup.grab_link);
2805 }
2806 prev = shsurf;
2807 }
2808 wl_list_init(&prev->popup.grab_link);
2809 }
2810
2811 wl_list_remove(&shseat->seat_destroy_listener.link);
2812 free(shseat);
2813}
2814
2815static struct shell_seat *
2816create_shell_seat(struct weston_seat *seat)
2817{
2818 struct shell_seat *shseat;
2819
2820 shseat = calloc(1, sizeof *shseat);
2821 if (!shseat) {
2822 weston_log("no memory to allocate shell seat\n");
2823 return NULL;
2824 }
2825
2826 shseat->seat = seat;
2827 wl_list_init(&shseat->popup_grab.surfaces_list);
2828
2829 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2830 wl_signal_add(&seat->destroy_signal,
2831 &shseat->seat_destroy_listener);
2832
2833 return shseat;
2834}
2835
2836static struct shell_seat *
2837get_shell_seat(struct weston_seat *seat)
2838{
2839 struct wl_listener *listener;
2840
2841 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2842 if (listener == NULL)
2843 return create_shell_seat(seat);
2844
2845 return container_of(listener,
2846 struct shell_seat, seat_destroy_listener);
2847}
2848
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002849static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002850popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002851{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002852 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002853 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002854 struct shell_seat *shseat =
2855 container_of(grab, struct shell_seat, popup_grab.grab);
2856 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002857 wl_fixed_t sx, sy;
2858
Jason Ekstranda7af7042013-10-12 22:38:11 -05002859 view = weston_compositor_pick_view(pointer->seat->compositor,
2860 pointer->x, pointer->y,
2861 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002862
Jason Ekstranda7af7042013-10-12 22:38:11 -05002863 if (view && view->surface->resource &&
2864 wl_resource_get_client(view->surface->resource) == client) {
2865 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002866 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002867 weston_pointer_set_focus(pointer, NULL,
2868 wl_fixed_from_int(0),
2869 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002870 }
2871}
2872
2873static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002874popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2875 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002876{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002877 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002878 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002879 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002880
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002881 weston_pointer_move(pointer, x, y);
2882
Neil Roberts96d790e2013-09-19 17:32:00 +01002883 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002884 weston_view_from_global_fixed(pointer->focus,
2885 pointer->x, pointer->y,
2886 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002887 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002888 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002889}
2890
2891static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002892popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002893 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002894{
2895 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002896 struct shell_seat *shseat =
2897 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002898 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002899 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002900 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002901 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002902
Neil Roberts96d790e2013-09-19 17:32:00 +01002903 resource_list = &grab->pointer->focus_resource_list;
2904 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002905 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002906 wl_resource_for_each(resource, resource_list) {
2907 wl_pointer_send_button(resource, serial,
2908 time, button, state);
2909 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002910 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002911 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002912 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002913 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002914 }
2915
Daniel Stone4dbadb12012-05-30 16:31:51 +01002916 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002917 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002918}
2919
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002920static void
2921popup_grab_cancel(struct weston_pointer_grab *grab)
2922{
2923 popup_grab_end(grab->pointer);
2924}
2925
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002926static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002927 popup_grab_focus,
2928 popup_grab_motion,
2929 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002930 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002931};
2932
2933static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002934shell_surface_send_popup_done(struct shell_surface *shsurf)
2935{
2936 if (shell_surface_is_wl_shell_surface(shsurf))
2937 wl_shell_surface_send_popup_done(shsurf->resource);
2938 else if (shell_surface_is_xdg_popup(shsurf))
2939 xdg_popup_send_popup_done(shsurf->resource,
2940 shsurf->popup.serial);
2941}
2942
2943static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002944popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002945{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002946 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002947 struct shell_seat *shseat =
2948 container_of(grab, struct shell_seat, popup_grab.grab);
2949 struct shell_surface *shsurf;
2950 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002951
2952 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002953 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002954 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002955 shseat->popup_grab.grab.interface = NULL;
2956 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002957 /* Send the popup_done event to all the popups open */
2958 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002959 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002960 shsurf->popup.shseat = NULL;
2961 if (prev) {
2962 wl_list_init(&prev->popup.grab_link);
2963 }
2964 prev = shsurf;
2965 }
2966 wl_list_init(&prev->popup.grab_link);
2967 wl_list_init(&shseat->popup_grab.surfaces_list);
2968 }
2969}
2970
2971static void
2972add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2973{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002974 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002975
2976 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002977 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002978 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002979 /* We must make sure here that this popup was opened after
2980 * a mouse press, and not just by moving around with other
2981 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002982 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002983 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002984
Rob Bradforddfe31052013-06-26 19:49:11 +01002985 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002986 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002987 } else {
2988 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002989 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002990}
2991
2992static void
2993remove_popup_grab(struct shell_surface *shsurf)
2994{
2995 struct shell_seat *shseat = shsurf->popup.shseat;
2996
2997 wl_list_remove(&shsurf->popup.grab_link);
2998 wl_list_init(&shsurf->popup.grab_link);
2999 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003000 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003001 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003002 }
3003}
3004
3005static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003006shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003007{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003008 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003009 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003010
Jason Ekstranda7af7042013-10-12 22:38:11 -05003011 shsurf->surface->output = parent_view->output;
3012 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003013
Jason Ekstranda7af7042013-10-12 22:38:11 -05003014 weston_view_set_transform_parent(shsurf->view, parent_view);
3015 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3016 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003017
Kristian Høgsberge3148752013-05-06 23:19:49 -04003018 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003019 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003020 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003021 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003022 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003023 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003024}
3025
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003026static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003027 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003028 shell_surface_move,
3029 shell_surface_resize,
3030 shell_surface_set_toplevel,
3031 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003032 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003033 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003034 shell_surface_set_maximized,
3035 shell_surface_set_title,
3036 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003037};
3038
3039static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003040destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003041{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003042 struct shell_surface *child, *next;
3043
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003044 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3045
Giulio Camuffo5085a752013-03-25 21:42:45 +01003046 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3047 remove_popup_grab(shsurf);
3048 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003049
Alex Wubd3354b2012-04-17 17:20:49 +08003050 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003051 shell_surface_is_top_fullscreen(shsurf))
3052 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003053
Jason Ekstranda7af7042013-10-12 22:38:11 -05003054 if (shsurf->fullscreen.black_view)
3055 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003056
Alex Wubd3354b2012-04-17 17:20:49 +08003057 /* As destroy_resource() use wl_list_for_each_safe(),
3058 * we can always remove the listener.
3059 */
3060 wl_list_remove(&shsurf->surface_destroy_listener.link);
3061 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06003062 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07003063 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003064
Jason Ekstranda7af7042013-10-12 22:38:11 -05003065 weston_view_destroy(shsurf->view);
3066
Philip Withnall648a4dd2013-11-25 18:01:44 +00003067 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003068 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3069 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003070
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003071 wl_list_remove(&shsurf->link);
3072 free(shsurf);
3073}
3074
3075static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003076shell_destroy_shell_surface(struct wl_resource *resource)
3077{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003078 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003079
3080 destroy_shell_surface(shsurf);
3081}
3082
3083static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003084shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003085{
3086 struct shell_surface *shsurf = container_of(listener,
3087 struct shell_surface,
3088 surface_destroy_listener);
3089
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003090 if (shsurf->resource)
3091 wl_resource_destroy(shsurf->resource);
3092 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003093 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003094}
3095
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003096static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003097shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003098
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003099struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003100get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003101{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003102 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003103 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003104 else
3105 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003106}
3107
Rafael Antognollie2a34552013-12-03 15:35:45 -02003108static struct shell_surface *
3109create_common_surface(void *shell, struct weston_surface *surface,
3110 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003111{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003112 struct shell_surface *shsurf;
3113
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003114 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003115 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003116 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003117 }
3118
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003119 shsurf = calloc(1, sizeof *shsurf);
3120 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003121 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003122 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003123 }
3124
Jason Ekstranda7af7042013-10-12 22:38:11 -05003125 shsurf->view = weston_view_create(surface);
3126 if (!shsurf->view) {
3127 weston_log("no memory to allocate shell surface\n");
3128 free(shsurf);
3129 return NULL;
3130 }
3131
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003132 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003133 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003134
Tiago Vignattibc052c92012-04-19 16:18:18 +03003135 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003136 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003137 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003138 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003139 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003140 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003141 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3142 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003143 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003144 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003145 wl_list_init(&shsurf->fullscreen.transform.link);
3146
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003147 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003148 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003149 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003150 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003151
3152 /* init link so its safe to always remove it in destroy_shell_surface */
3153 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003154 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003155
Pekka Paalanen460099f2012-01-20 16:48:25 +02003156 /* empty when not in use */
3157 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003158 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003159
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003160 wl_list_init(&shsurf->workspace_transform.link);
3161
Philip Withnall648a4dd2013-11-25 18:01:44 +00003162 wl_list_init(&shsurf->children_link);
3163 wl_list_init(&shsurf->children_list);
3164 shsurf->parent = NULL;
3165
Pekka Paalanen98262232011-12-01 10:42:22 +02003166 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003167
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003168 shsurf->client = client;
3169
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003170 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003171}
3172
Rafael Antognollie2a34552013-12-03 15:35:45 -02003173static struct shell_surface *
3174create_shell_surface(void *shell, struct weston_surface *surface,
3175 const struct weston_shell_client *client)
3176{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003177 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003178}
3179
Jason Ekstranda7af7042013-10-12 22:38:11 -05003180static struct weston_view *
3181get_primary_view(void *shell, struct shell_surface *shsurf)
3182{
3183 return shsurf->view;
3184}
3185
Tiago Vignattibc052c92012-04-19 16:18:18 +03003186static void
3187shell_get_shell_surface(struct wl_client *client,
3188 struct wl_resource *resource,
3189 uint32_t id,
3190 struct wl_resource *surface_resource)
3191{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003192 struct weston_surface *surface =
3193 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003194 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003195 struct shell_surface *shsurf;
3196
3197 if (get_shell_surface(surface)) {
3198 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003199 WL_DISPLAY_ERROR_INVALID_OBJECT,
3200 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003201 return;
3202 }
3203
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003204 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003205 if (!shsurf) {
3206 wl_resource_post_error(surface_resource,
3207 WL_DISPLAY_ERROR_INVALID_OBJECT,
3208 "surface->configure already set");
3209 return;
3210 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003211
Jason Ekstranda85118c2013-06-27 20:17:02 -05003212 shsurf->resource =
3213 wl_resource_create(client,
3214 &wl_shell_surface_interface, 1, id);
3215 wl_resource_set_implementation(shsurf->resource,
3216 &shell_surface_implementation,
3217 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003218}
3219
Rafael Antognollie2a34552013-12-03 15:35:45 -02003220static bool
3221shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3222{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003223 /* A shell surface without a resource is created from xwayland
3224 * and is considered a wl_shell surface for now. */
3225
3226 return shsurf->resource == NULL ||
3227 wl_resource_instance_of(shsurf->resource,
3228 &wl_shell_surface_interface,
3229 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003230}
3231
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003232static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003233 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003234};
3235
Rafael Antognollie2a34552013-12-03 15:35:45 -02003236/****************************
3237 * xdg-shell implementation */
3238
3239static void
3240xdg_surface_destroy(struct wl_client *client,
3241 struct wl_resource *resource)
3242{
3243 wl_resource_destroy(resource);
3244}
3245
3246static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003247xdg_surface_set_margin(struct wl_client *client,
3248 struct wl_resource *resource,
3249 int32_t left,
3250 int32_t right,
3251 int32_t top,
3252 int32_t bottom)
3253{
3254 /* Do nothing, Weston doesn't try to constrain or place
3255 * surfaces in any special manner... */
3256}
3257
3258static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003259xdg_surface_set_app_id(struct wl_client *client,
3260 struct wl_resource *resource,
3261 const char *app_id)
3262{
3263 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3264
3265 free(shsurf->class);
3266 shsurf->class = strdup(app_id);
3267}
3268
3269static void
3270xdg_surface_set_title(struct wl_client *client,
3271 struct wl_resource *resource, const char *title)
3272{
3273 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3274
3275 set_title(shsurf, title);
3276}
3277
3278static void
3279xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3280 struct wl_resource *seat_resource, uint32_t serial)
3281{
3282 common_surface_move(resource, seat_resource, serial);
3283}
3284
3285static void
3286xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3287 struct wl_resource *seat_resource, uint32_t serial,
3288 uint32_t edges)
3289{
3290 common_surface_resize(resource, seat_resource, serial, edges);
3291}
3292
3293static void
3294xdg_surface_set_output(struct wl_client *client,
3295 struct wl_resource *resource,
3296 struct wl_resource *output_resource)
3297{
3298 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3299 struct weston_output *output;
3300
3301 if (output_resource)
3302 output = wl_resource_get_user_data(output_resource);
3303 else
3304 output = NULL;
3305
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003306 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003307}
3308
3309static void
3310xdg_surface_set_fullscreen(struct wl_client *client,
3311 struct wl_resource *resource)
3312{
3313 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3314
3315 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3316 return;
3317
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003318 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003319 set_fullscreen(shsurf,
3320 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003321 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003322}
3323
3324static void
3325xdg_surface_unset_fullscreen(struct wl_client *client,
3326 struct wl_resource *resource)
3327{
3328 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003329 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003330
3331 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3332 return;
3333
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003334 if (!shsurf->next_state.fullscreen)
3335 return;
3336
Rafael Antognollie2a34552013-12-03 15:35:45 -02003337 shsurf->next_state.fullscreen = false;
3338 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003339
3340 if (shsurf->saved_size_valid) {
3341 width = shsurf->saved_width;
3342 height = shsurf->saved_height;
3343 shsurf->saved_size_valid = false;
3344 } else {
3345 width = shsurf->surface->width;
3346 height = shsurf->surface->height;
3347 }
3348
3349 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003350}
3351
3352static void
3353xdg_surface_set_maximized(struct wl_client *client,
3354 struct wl_resource *resource)
3355{
3356 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3357
3358 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3359 return;
3360
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003361 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003362 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003363}
3364
3365static void
3366xdg_surface_unset_maximized(struct wl_client *client,
3367 struct wl_resource *resource)
3368{
3369 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003370 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003371
3372 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3373 return;
3374
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003375 if (!shsurf->next_state.maximized)
3376 return;
3377
Rafael Antognollie2a34552013-12-03 15:35:45 -02003378 shsurf->next_state.maximized = false;
3379 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003380
3381 if (shsurf->saved_size_valid) {
3382 width = shsurf->saved_width;
3383 height = shsurf->saved_height;
3384 shsurf->saved_size_valid = false;
3385 } else {
3386 width = shsurf->surface->width;
3387 height = shsurf->surface->height;
3388 }
3389
3390 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003391}
3392
3393static const struct xdg_surface_interface xdg_surface_implementation = {
3394 xdg_surface_destroy,
3395 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003396 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003397 xdg_surface_set_title,
3398 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003399 xdg_surface_move,
3400 xdg_surface_resize,
3401 xdg_surface_set_output,
3402 xdg_surface_set_fullscreen,
3403 xdg_surface_unset_fullscreen,
3404 xdg_surface_set_maximized,
3405 xdg_surface_unset_maximized,
3406 NULL /* set_minimized */
3407};
3408
3409static void
3410xdg_send_configure(struct weston_surface *surface,
3411 uint32_t edges, int32_t width, int32_t height)
3412{
3413 struct shell_surface *shsurf = get_shell_surface(surface);
3414
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003415 assert(shsurf);
3416
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003417 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003418}
3419
3420static const struct weston_shell_client xdg_client = {
3421 xdg_send_configure
3422};
3423
3424static void
3425xdg_use_unstable_version(struct wl_client *client,
3426 struct wl_resource *resource,
3427 int32_t version)
3428{
3429 if (version > 1) {
3430 wl_resource_post_error(resource,
3431 1,
3432 "xdg-shell:: version not implemented yet.");
3433 return;
3434 }
3435}
3436
3437static struct shell_surface *
3438create_xdg_surface(void *shell, struct weston_surface *surface,
3439 const struct weston_shell_client *client)
3440{
3441 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003442
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003443 shsurf = create_common_surface(shell, surface, client);
3444 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003445
3446 return shsurf;
3447}
3448
3449static void
3450xdg_get_xdg_surface(struct wl_client *client,
3451 struct wl_resource *resource,
3452 uint32_t id,
3453 struct wl_resource *surface_resource)
3454{
3455 struct weston_surface *surface =
3456 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003457 struct shell_client *sc = wl_resource_get_user_data(resource);
3458 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003459 struct shell_surface *shsurf;
3460
3461 if (get_shell_surface(surface)) {
3462 wl_resource_post_error(surface_resource,
3463 WL_DISPLAY_ERROR_INVALID_OBJECT,
3464 "desktop_shell::get_shell_surface already requested");
3465 return;
3466 }
3467
3468 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3469 if (!shsurf) {
3470 wl_resource_post_error(surface_resource,
3471 WL_DISPLAY_ERROR_INVALID_OBJECT,
3472 "surface->configure already set");
3473 return;
3474 }
3475
3476 shsurf->resource =
3477 wl_resource_create(client,
3478 &xdg_surface_interface, 1, id);
3479 wl_resource_set_implementation(shsurf->resource,
3480 &xdg_surface_implementation,
3481 shsurf, shell_destroy_shell_surface);
3482}
3483
3484static bool
3485shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3486{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003487 return shsurf->resource &&
3488 wl_resource_instance_of(shsurf->resource,
3489 &xdg_surface_interface,
3490 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003491}
3492
3493/* xdg-popup implementation */
3494
3495static void
3496xdg_popup_destroy(struct wl_client *client,
3497 struct wl_resource *resource)
3498{
3499 wl_resource_destroy(resource);
3500}
3501
Rafael Antognollie2a34552013-12-03 15:35:45 -02003502static const struct xdg_popup_interface xdg_popup_implementation = {
3503 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003504};
3505
3506static void
3507xdg_popup_send_configure(struct weston_surface *surface,
3508 uint32_t edges, int32_t width, int32_t height)
3509{
3510}
3511
3512static const struct weston_shell_client xdg_popup_client = {
3513 xdg_popup_send_configure
3514};
3515
3516static struct shell_surface *
3517create_xdg_popup(void *shell, struct weston_surface *surface,
3518 const struct weston_shell_client *client,
3519 struct weston_surface *parent,
3520 struct shell_seat *seat,
3521 uint32_t serial,
3522 int32_t x, int32_t y)
3523{
3524 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003525
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003526 shsurf = create_common_surface(shell, surface, client);
3527 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003528 shsurf->popup.shseat = seat;
3529 shsurf->popup.serial = serial;
3530 shsurf->popup.x = x;
3531 shsurf->popup.y = y;
3532 shell_surface_set_parent(shsurf, parent);
3533
3534 return shsurf;
3535}
3536
3537static void
3538xdg_get_xdg_popup(struct wl_client *client,
3539 struct wl_resource *resource,
3540 uint32_t id,
3541 struct wl_resource *surface_resource,
3542 struct wl_resource *parent_resource,
3543 struct wl_resource *seat_resource,
3544 uint32_t serial,
3545 int32_t x, int32_t y, uint32_t flags)
3546{
3547 struct weston_surface *surface =
3548 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003549 struct shell_client *sc = wl_resource_get_user_data(resource);
3550 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003551 struct shell_surface *shsurf;
3552 struct weston_surface *parent;
3553 struct shell_seat *seat;
3554
3555 if (get_shell_surface(surface)) {
3556 wl_resource_post_error(surface_resource,
3557 WL_DISPLAY_ERROR_INVALID_OBJECT,
3558 "desktop_shell::get_shell_surface already requested");
3559 return;
3560 }
3561
3562 if (!parent_resource) {
3563 wl_resource_post_error(surface_resource,
3564 WL_DISPLAY_ERROR_INVALID_OBJECT,
3565 "xdg_shell::get_xdg_popup requires a parent shell surface");
3566 }
3567
3568 parent = wl_resource_get_user_data(parent_resource);
3569 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3570
3571 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3572 parent, seat, serial, x, y);
3573 if (!shsurf) {
3574 wl_resource_post_error(surface_resource,
3575 WL_DISPLAY_ERROR_INVALID_OBJECT,
3576 "surface->configure already set");
3577 return;
3578 }
3579
3580 shsurf->resource =
3581 wl_resource_create(client,
3582 &xdg_popup_interface, 1, id);
3583 wl_resource_set_implementation(shsurf->resource,
3584 &xdg_popup_implementation,
3585 shsurf, shell_destroy_shell_surface);
3586}
3587
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003588static void
3589xdg_pong(struct wl_client *client,
3590 struct wl_resource *resource, uint32_t serial)
3591{
3592 struct shell_client *sc = wl_resource_get_user_data(resource);
3593 struct weston_compositor *ec = sc->shell->compositor;
3594
3595 if (sc->ping_serial != serial)
3596 return;
3597
3598 sc->unresponsive = 0;
3599 end_busy_cursor(ec, client);
3600
3601 if (sc->ping_timer) {
3602 wl_event_source_remove(sc->ping_timer);
3603 sc->ping_timer = NULL;
3604 }
3605}
3606
Rafael Antognollie2a34552013-12-03 15:35:45 -02003607static bool
3608shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3609{
3610 return wl_resource_instance_of(shsurf->resource,
3611 &xdg_popup_interface,
3612 &xdg_popup_implementation);
3613}
3614
3615static const struct xdg_shell_interface xdg_implementation = {
3616 xdg_use_unstable_version,
3617 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003618 xdg_get_xdg_popup,
3619 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003620};
3621
3622static int
3623xdg_shell_unversioned_dispatch(const void *implementation,
3624 void *_target, uint32_t opcode,
3625 const struct wl_message *message,
3626 union wl_argument *args)
3627{
3628 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003629 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003630
3631 if (opcode != 0) {
3632 wl_resource_post_error(resource,
3633 WL_DISPLAY_ERROR_INVALID_OBJECT,
3634 "must call use_unstable_version first");
3635 return 0;
3636 }
3637
3638#define XDG_SERVER_VERSION 1
3639
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003640 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3641 "shell implementation doesn't match protocol version");
3642
Rafael Antognollie2a34552013-12-03 15:35:45 -02003643 if (args[0].i != XDG_SERVER_VERSION) {
3644 wl_resource_post_error(resource,
3645 WL_DISPLAY_ERROR_INVALID_OBJECT,
3646 "incompatible version, server is %d "
3647 "client wants %d",
3648 XDG_SERVER_VERSION, args[0].i);
3649 return 0;
3650 }
3651
3652 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003653 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003654
3655 return 1;
3656}
3657
3658/* end of xdg-shell implementation */
3659/***********************************/
3660
Kristian Høgsberg07937562011-04-12 17:25:42 -04003661static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003662shell_fade(struct desktop_shell *shell, enum fade_type type);
3663
3664static int
3665screensaver_timeout(void *data)
3666{
3667 struct desktop_shell *shell = data;
3668
3669 shell_fade(shell, FADE_OUT);
3670
3671 return 1;
3672}
3673
3674static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003675handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003676{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003677 struct desktop_shell *shell =
3678 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003679
Pekka Paalanen18027e52011-12-02 16:31:49 +02003680 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003681
3682 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003683 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003684}
3685
3686static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003687launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003688{
3689 if (shell->screensaver.binding)
3690 return;
3691
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003692 if (!shell->screensaver.path) {
3693 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003694 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003695 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003696
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003697 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003698 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003699 return;
3700 }
3701
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003702 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003703 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003704 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003705 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003706}
3707
3708static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003709terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003710{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003711 if (shell->screensaver.process.pid == 0)
3712 return;
3713
3714 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003715}
3716
3717static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003718configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003719{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003720 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003721
Jason Ekstranda7af7042013-10-12 22:38:11 -05003722 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3723 if (v->output == ev->output && v != ev) {
3724 weston_view_unmap(v);
3725 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003726 }
3727 }
3728
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003729 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003730
Jason Ekstranda7af7042013-10-12 22:38:11 -05003731 if (wl_list_empty(&ev->layer_link)) {
3732 wl_list_insert(&layer->view_list, &ev->layer_link);
3733 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003734 }
3735}
3736
3737static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003738background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003739{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003740 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003741 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003742
Jason Ekstranda7af7042013-10-12 22:38:11 -05003743 view = container_of(es->views.next, struct weston_view, surface_link);
3744
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003745 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003746}
3747
3748static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003749desktop_shell_set_background(struct wl_client *client,
3750 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003751 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003752 struct wl_resource *surface_resource)
3753{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003754 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003755 struct weston_surface *surface =
3756 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003757 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003758
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003759 if (surface->configure) {
3760 wl_resource_post_error(surface_resource,
3761 WL_DISPLAY_ERROR_INVALID_OBJECT,
3762 "surface role already assigned");
3763 return;
3764 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003765
Jason Ekstranda7af7042013-10-12 22:38:11 -05003766 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3767 weston_view_destroy(view);
3768 view = weston_view_create(surface);
3769
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003770 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003771 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003772 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003773 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003774 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003775 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003776 surface->output->width,
3777 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003778}
3779
3780static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003781panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003782{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003783 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003784 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003785
Jason Ekstranda7af7042013-10-12 22:38:11 -05003786 view = container_of(es->views.next, struct weston_view, surface_link);
3787
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003788 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003789}
3790
3791static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003792desktop_shell_set_panel(struct wl_client *client,
3793 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003794 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003795 struct wl_resource *surface_resource)
3796{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003797 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003798 struct weston_surface *surface =
3799 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003800 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003801
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003802 if (surface->configure) {
3803 wl_resource_post_error(surface_resource,
3804 WL_DISPLAY_ERROR_INVALID_OBJECT,
3805 "surface role already assigned");
3806 return;
3807 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003808
Jason Ekstranda7af7042013-10-12 22:38:11 -05003809 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3810 weston_view_destroy(view);
3811 view = weston_view_create(surface);
3812
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003813 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003814 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003815 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003816 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003817 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003818 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003819 surface->output->width,
3820 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003821}
3822
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003823static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003824lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003825{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003826 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003827 struct weston_view *view;
3828
3829 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003830
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003831 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003832 return;
3833
Jason Ekstranda7af7042013-10-12 22:38:11 -05003834 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003835
3836 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003837 wl_list_insert(&shell->lock_layer.view_list,
3838 &view->layer_link);
3839 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003840 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003841 }
3842}
3843
3844static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003845handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003846{
Tiago Vignattibe143262012-04-16 17:31:41 +03003847 struct desktop_shell *shell =
3848 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003849
Martin Minarik6d118362012-06-07 18:01:59 +02003850 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003851 shell->lock_surface = NULL;
3852}
3853
3854static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003855desktop_shell_set_lock_surface(struct wl_client *client,
3856 struct wl_resource *resource,
3857 struct wl_resource *surface_resource)
3858{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003859 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003860 struct weston_surface *surface =
3861 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003862
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003863 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003864
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003865 if (!shell->locked)
3866 return;
3867
Pekka Paalanen98262232011-12-01 10:42:22 +02003868 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003869
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003870 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003871 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003872 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003873
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003874 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003875 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003876 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003877}
3878
3879static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003880resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003881{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003882 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003883
Pekka Paalanen77346a62011-11-30 16:26:35 +02003884 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003885
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003886 wl_list_remove(&shell->lock_layer.link);
3887 wl_list_insert(&shell->compositor->cursor_layer.link,
3888 &shell->fullscreen_layer.link);
3889 wl_list_insert(&shell->fullscreen_layer.link,
3890 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003891 if (shell->showing_input_panels) {
3892 wl_list_insert(&shell->panel_layer.link,
3893 &shell->input_panel_layer.link);
3894 wl_list_insert(&shell->input_panel_layer.link,
3895 &ws->layer.link);
3896 } else {
3897 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3898 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003899
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003900 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003901
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003902 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003903 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003904 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003905}
3906
3907static void
3908desktop_shell_unlock(struct wl_client *client,
3909 struct wl_resource *resource)
3910{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003911 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003912
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003913 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003914
3915 if (shell->locked)
3916 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003917}
3918
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003919static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003920desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003921 struct wl_resource *resource,
3922 struct wl_resource *surface_resource)
3923{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003924 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003925
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003926 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003927 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003928}
3929
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003930static void
3931desktop_shell_desktop_ready(struct wl_client *client,
3932 struct wl_resource *resource)
3933{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003934 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003935
3936 shell_fade_startup(shell);
3937}
3938
Kristian Høgsberg75840622011-09-06 13:48:16 -04003939static const struct desktop_shell_interface desktop_shell_implementation = {
3940 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003941 desktop_shell_set_panel,
3942 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003943 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003944 desktop_shell_set_grab_surface,
3945 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003946};
3947
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003948static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003949get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003950{
3951 struct shell_surface *shsurf;
3952
3953 shsurf = get_shell_surface(surface);
3954 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003955 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003956 return shsurf->type;
3957}
3958
Kristian Høgsberg75840622011-09-06 13:48:16 -04003959static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003960move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003961{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003962 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003963 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003964 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003965
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003966 if (seat->pointer->focus == NULL)
3967 return;
3968
3969 focus = seat->pointer->focus->surface;
3970
Pekka Paalanen01388e22013-04-25 13:57:44 +03003971 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003972 if (surface == NULL)
3973 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003974
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003975 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003976 if (shsurf == NULL || shsurf->state.fullscreen ||
3977 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003978 return;
3979
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003980 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003981}
3982
3983static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003984maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3985{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003986 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003987 struct weston_surface *surface;
3988 struct shell_surface *shsurf;
3989
3990 surface = weston_surface_get_main_surface(focus);
3991 if (surface == NULL)
3992 return;
3993
3994 shsurf = get_shell_surface(surface);
3995 if (shsurf == NULL)
3996 return;
3997
3998 if (!shell_surface_is_xdg_surface(shsurf))
3999 return;
4000
4001 if (shsurf->state.maximized)
4002 xdg_surface_send_request_unset_maximized(shsurf->resource);
4003 else
4004 xdg_surface_send_request_set_maximized(shsurf->resource);
4005}
4006
4007static void
4008fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4009{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004010 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004011 struct weston_surface *surface;
4012 struct shell_surface *shsurf;
4013
4014 surface = weston_surface_get_main_surface(focus);
4015 if (surface == NULL)
4016 return;
4017
4018 shsurf = get_shell_surface(surface);
4019 if (shsurf == NULL)
4020 return;
4021
4022 if (!shell_surface_is_xdg_surface(shsurf))
4023 return;
4024
4025 if (shsurf->state.fullscreen)
4026 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
4027 else
4028 xdg_surface_send_request_set_fullscreen(shsurf->resource);
4029}
4030
4031static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004032touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4033{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004034 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004035 struct weston_surface *surface;
4036 struct shell_surface *shsurf;
4037
4038 surface = weston_surface_get_main_surface(focus);
4039 if (surface == NULL)
4040 return;
4041
4042 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004043 if (shsurf == NULL || shsurf->state.fullscreen ||
4044 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004045 return;
4046
4047 surface_touch_move(shsurf, (struct weston_seat *) seat);
4048}
4049
4050static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004051resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004052{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004053 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004054 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004055 uint32_t edges = 0;
4056 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004057 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004058
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004059 if (seat->pointer->focus == NULL)
4060 return;
4061
4062 focus = seat->pointer->focus->surface;
4063
Pekka Paalanen01388e22013-04-25 13:57:44 +03004064 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004065 if (surface == NULL)
4066 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004067
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004068 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004069 if (shsurf == NULL || shsurf->state.fullscreen ||
4070 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004071 return;
4072
Jason Ekstranda7af7042013-10-12 22:38:11 -05004073 weston_view_from_global(shsurf->view,
4074 wl_fixed_to_int(seat->pointer->grab_x),
4075 wl_fixed_to_int(seat->pointer->grab_y),
4076 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004077
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004078 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004079 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004080 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004081 edges |= 0;
4082 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004083 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004084
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004085 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004086 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004087 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004088 edges |= 0;
4089 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004090 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004091
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004092 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004093}
4094
4095static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004096surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004097 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004098{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004099 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004100 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004101 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004102 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004103
Pekka Paalanen01388e22013-04-25 13:57:44 +03004104 /* XXX: broken for windows containing sub-surfaces */
4105 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004106 if (surface == NULL)
4107 return;
4108
4109 shsurf = get_shell_surface(surface);
4110 if (!shsurf)
4111 return;
4112
Jason Ekstranda7af7042013-10-12 22:38:11 -05004113 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004114
Jason Ekstranda7af7042013-10-12 22:38:11 -05004115 if (shsurf->view->alpha > 1.0)
4116 shsurf->view->alpha = 1.0;
4117 if (shsurf->view->alpha < step)
4118 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004119
Jason Ekstranda7af7042013-10-12 22:38:11 -05004120 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004121 weston_surface_damage(surface);
4122}
4123
4124static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004125do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004126 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004127{
Daniel Stone37816df2012-05-16 18:45:18 +01004128 struct weston_seat *ws = (struct weston_seat *) seat;
4129 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004130 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004131 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004132
4133 wl_list_for_each(output, &compositor->output_list, link) {
4134 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004135 wl_fixed_to_double(seat->pointer->x),
4136 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004137 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004138 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004139 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004140 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004141 increment = -output->zoom.increment;
4142 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004143 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004144 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004145 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004146 else
4147 increment = 0;
4148
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004149 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004150
Scott Moreaue6603982012-06-11 13:07:51 -06004151 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004152 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004153 else if (output->zoom.level > output->zoom.max_level)
4154 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004155 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004156 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004157 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004158
Scott Moreaue6603982012-06-11 13:07:51 -06004159 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004160
Jason Ekstranda7af7042013-10-12 22:38:11 -05004161 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004162 }
4163 }
4164}
4165
Scott Moreauccbf29d2012-02-22 14:21:41 -07004166static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004167zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004168 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004169{
4170 do_zoom(seat, time, 0, axis, value);
4171}
4172
4173static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004174zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004175 void *data)
4176{
4177 do_zoom(seat, time, key, 0, 0);
4178}
4179
4180static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004181terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004182 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004183{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004184 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004185
Daniel Stone325fc2d2012-05-30 16:31:58 +01004186 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004187}
4188
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004189static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004190rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4191 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004192{
4193 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004194 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004195 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004196 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004197 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004198
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004199 weston_pointer_move(pointer, x, y);
4200
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004201 if (!shsurf)
4202 return;
4203
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004204 cx = 0.5f * shsurf->surface->width;
4205 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004206
Daniel Stone37816df2012-05-16 18:45:18 +01004207 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4208 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004209 r = sqrtf(dx * dx + dy * dy);
4210
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004211 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004212 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004213
4214 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004215 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004216 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004217
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004218 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004219 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004220
4221 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004222 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004223 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004224 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004225 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004226
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004227 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004228 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004229 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004230 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004231 wl_list_init(&shsurf->rotation.transform.link);
4232 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004233 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004234 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004235
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004236 /* We need to adjust the position of the surface
4237 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004238 cposx = shsurf->view->geometry.x + cx;
4239 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004240 dposx = rotate->center.x - cposx;
4241 dposy = rotate->center.y - cposy;
4242 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004243 weston_view_set_position(shsurf->view,
4244 shsurf->view->geometry.x + dposx,
4245 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004246 }
4247
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004248 /* Repaint implies weston_surface_update_transform(), which
4249 * lazily applies the damage due to rotation update.
4250 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004251 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004252}
4253
4254static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004255rotate_grab_button(struct weston_pointer_grab *grab,
4256 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004257{
4258 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004259 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004260 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004261 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004262 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004263
Daniel Stone4dbadb12012-05-30 16:31:51 +01004264 if (pointer->button_count == 0 &&
4265 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004266 if (shsurf)
4267 weston_matrix_multiply(&shsurf->rotation.rotation,
4268 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004269 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004270 free(rotate);
4271 }
4272}
4273
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004274static void
4275rotate_grab_cancel(struct weston_pointer_grab *grab)
4276{
4277 struct rotate_grab *rotate =
4278 container_of(grab, struct rotate_grab, base.grab);
4279
4280 shell_grab_end(&rotate->base);
4281 free(rotate);
4282}
4283
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004284static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004285 noop_grab_focus,
4286 rotate_grab_motion,
4287 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004288 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004289};
4290
4291static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004292surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004293{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004294 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004295 float dx, dy;
4296 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004297
Pekka Paalanen460099f2012-01-20 16:48:25 +02004298 rotate = malloc(sizeof *rotate);
4299 if (!rotate)
4300 return;
4301
Jason Ekstranda7af7042013-10-12 22:38:11 -05004302 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004303 surface->surface->width * 0.5f,
4304 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004305 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004306
Daniel Stone37816df2012-05-16 18:45:18 +01004307 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4308 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004309 r = sqrtf(dx * dx + dy * dy);
4310 if (r > 20.0f) {
4311 struct weston_matrix inverse;
4312
4313 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004314 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004315 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004316
4317 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004318 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004319 } else {
4320 weston_matrix_init(&surface->rotation.rotation);
4321 weston_matrix_init(&rotate->rotation);
4322 }
4323
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004324 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4325 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004326}
4327
4328static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004329rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004330 void *data)
4331{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004332 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004333 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004334 struct shell_surface *surface;
4335
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004336 if (seat->pointer->focus == NULL)
4337 return;
4338
4339 focus = seat->pointer->focus->surface;
4340
Pekka Paalanen01388e22013-04-25 13:57:44 +03004341 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004342 if (base_surface == NULL)
4343 return;
4344
4345 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004346 if (surface == NULL || surface->state.fullscreen ||
4347 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004348 return;
4349
4350 surface_rotate(surface, seat);
4351}
4352
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004353/* Move all fullscreen layers down to the current workspace in a non-reversible
4354 * manner. This should be used when implementing shell-wide overlays, such as
4355 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004356void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004357lower_fullscreen_layer(struct desktop_shell *shell)
4358{
4359 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004360 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004361
4362 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004363 wl_list_for_each_reverse_safe(view, prev,
4364 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004365 layer_link) {
4366 wl_list_remove(&view->layer_link);
4367 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4368 weston_view_damage_below(view);
4369 weston_surface_damage(view->surface);
4370 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004371}
4372
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004373void
Tiago Vignattibe143262012-04-16 17:31:41 +03004374activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004375 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004376{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004377 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004378 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004379 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004380 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004381 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004382
Pekka Paalanen01388e22013-04-25 13:57:44 +03004383 main_surface = weston_surface_get_main_surface(es);
4384
Daniel Stone37816df2012-05-16 18:45:18 +01004385 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004386
Jonas Ådahl8538b222012-08-29 22:13:03 +02004387 state = ensure_focus_state(shell, seat);
4388 if (state == NULL)
4389 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004390
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004391 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004392 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004393
Rafael Antognolli03b16592013-12-03 15:35:42 -02004394 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004395 assert(shsurf);
4396
Rafael Antognolli03b16592013-12-03 15:35:42 -02004397 if (shsurf->state.fullscreen)
4398 shell_configure_fullscreen(shsurf);
4399 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004400 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004401
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004402 /* Update the surface’s layer. This brings it to the top of the stacking
4403 * order as appropriate. */
4404 shell_surface_update_layer(shsurf);
4405
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004406 if (shell->focus_animation_type != ANIMATION_NONE) {
4407 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004408 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004409 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004410}
4411
Alex Wu21858432012-04-01 20:13:08 +08004412/* no-op func for checking black surface */
4413static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004414black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004415{
4416}
4417
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004418static bool
Alex Wu21858432012-04-01 20:13:08 +08004419is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4420{
4421 if (es->configure == black_surface_configure) {
4422 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004423 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004424 return true;
4425 }
4426 return false;
4427}
4428
Kristian Høgsberg75840622011-09-06 13:48:16 -04004429static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004430activate_binding(struct weston_seat *seat,
4431 struct desktop_shell *shell,
4432 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004433{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004434 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004435
Alex Wu9c35e6b2012-03-05 14:13:13 +08004436 if (!focus)
4437 return;
4438
Pekka Paalanen01388e22013-04-25 13:57:44 +03004439 if (is_black_surface(focus, &main_surface))
4440 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004441
Pekka Paalanen01388e22013-04-25 13:57:44 +03004442 main_surface = weston_surface_get_main_surface(focus);
4443 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004444 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004445
Neil Robertsa28c6932013-10-03 16:43:04 +01004446 activate(shell, focus, seat);
4447}
4448
4449static void
4450click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4451 void *data)
4452{
4453 if (seat->pointer->grab != &seat->pointer->default_grab)
4454 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004455 if (seat->pointer->focus == NULL)
4456 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004457
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004458 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004459}
4460
4461static void
4462touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4463{
4464 if (seat->touch->grab != &seat->touch->default_grab)
4465 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004466 if (seat->touch->focus == NULL)
4467 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004468
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004469 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004470}
4471
4472static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004473lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004474{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004475 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004476
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004477 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004478 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004479 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004480 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004481
4482 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004483
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004484 /* Hide all surfaces by removing the fullscreen, panel and
4485 * toplevel layers. This way nothing else can show or receive
4486 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004487
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004488 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004489 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004490 if (shell->showing_input_panels)
4491 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004492 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004493 wl_list_insert(&shell->compositor->cursor_layer.link,
4494 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004495
Pekka Paalanen77346a62011-11-30 16:26:35 +02004496 launch_screensaver(shell);
4497
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004498 /* TODO: disable bindings that should not work while locked. */
4499
4500 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004501}
4502
4503static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004504unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004505{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004506 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004507 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004508 return;
4509 }
4510
4511 /* If desktop-shell client has gone away, unlock immediately. */
4512 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004513 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004514 return;
4515 }
4516
4517 if (shell->prepare_event_sent)
4518 return;
4519
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004520 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004521 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004522}
4523
4524static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004525shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004526{
4527 struct desktop_shell *shell = data;
4528
4529 shell->fade.animation = NULL;
4530
4531 switch (shell->fade.type) {
4532 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004533 weston_surface_destroy(shell->fade.view->surface);
4534 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004535 break;
4536 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004537 lock(shell);
4538 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004539 default:
4540 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004541 }
4542}
4543
Jason Ekstranda7af7042013-10-12 22:38:11 -05004544static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004545shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004546{
4547 struct weston_compositor *compositor = shell->compositor;
4548 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004549 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004550
4551 surface = weston_surface_create(compositor);
4552 if (!surface)
4553 return NULL;
4554
Jason Ekstranda7af7042013-10-12 22:38:11 -05004555 view = weston_view_create(surface);
4556 if (!view) {
4557 weston_surface_destroy(surface);
4558 return NULL;
4559 }
4560
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004561 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004562 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004563 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004564 wl_list_insert(&compositor->fade_layer.view_list,
4565 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004566 pixman_region32_init(&surface->input);
4567
Jason Ekstranda7af7042013-10-12 22:38:11 -05004568 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004569}
4570
4571static void
4572shell_fade(struct desktop_shell *shell, enum fade_type type)
4573{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004574 float tint;
4575
4576 switch (type) {
4577 case FADE_IN:
4578 tint = 0.0;
4579 break;
4580 case FADE_OUT:
4581 tint = 1.0;
4582 break;
4583 default:
4584 weston_log("shell: invalid fade type\n");
4585 return;
4586 }
4587
4588 shell->fade.type = type;
4589
Jason Ekstranda7af7042013-10-12 22:38:11 -05004590 if (shell->fade.view == NULL) {
4591 shell->fade.view = shell_fade_create_surface(shell);
4592 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004593 return;
4594
Jason Ekstranda7af7042013-10-12 22:38:11 -05004595 shell->fade.view->alpha = 1.0 - tint;
4596 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004597 }
4598
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004599 if (shell->fade.view->output == NULL) {
4600 /* If the black view gets a NULL output, we lost the
4601 * last output and we'll just cancel the fade. This
4602 * happens when you close the last window under the
4603 * X11 or Wayland backends. */
4604 shell->locked = false;
4605 weston_surface_destroy(shell->fade.view->surface);
4606 shell->fade.view = NULL;
4607 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004608 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004609 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004610 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004611 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004612 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004613 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004614 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004615}
4616
4617static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004618do_shell_fade_startup(void *data)
4619{
4620 struct desktop_shell *shell = data;
4621
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004622 if (shell->startup_animation_type == ANIMATION_FADE)
4623 shell_fade(shell, FADE_IN);
4624 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004625 weston_surface_destroy(shell->fade.view->surface);
4626 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004627 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004628}
4629
4630static void
4631shell_fade_startup(struct desktop_shell *shell)
4632{
4633 struct wl_event_loop *loop;
4634
4635 if (!shell->fade.startup_timer)
4636 return;
4637
4638 wl_event_source_remove(shell->fade.startup_timer);
4639 shell->fade.startup_timer = NULL;
4640
4641 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4642 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4643}
4644
4645static int
4646fade_startup_timeout(void *data)
4647{
4648 struct desktop_shell *shell = data;
4649
4650 shell_fade_startup(shell);
4651 return 0;
4652}
4653
4654static void
4655shell_fade_init(struct desktop_shell *shell)
4656{
4657 /* Make compositor output all black, and wait for the desktop-shell
4658 * client to signal it is ready, then fade in. The timer triggers a
4659 * fade-in, in case the desktop-shell client takes too long.
4660 */
4661
4662 struct wl_event_loop *loop;
4663
Jason Ekstranda7af7042013-10-12 22:38:11 -05004664 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004665 weston_log("%s: warning: fade surface already exists\n",
4666 __func__);
4667 return;
4668 }
4669
Jason Ekstranda7af7042013-10-12 22:38:11 -05004670 shell->fade.view = shell_fade_create_surface(shell);
4671 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004672 return;
4673
Jason Ekstranda7af7042013-10-12 22:38:11 -05004674 weston_view_update_transform(shell->fade.view);
4675 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004676
4677 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4678 shell->fade.startup_timer =
4679 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4680 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4681}
4682
4683static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004684idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004685{
4686 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004687 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004688 struct weston_seat *seat;
4689
4690 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4691 if (seat->pointer)
4692 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004693
4694 shell_fade(shell, FADE_OUT);
4695 /* lock() is called from shell_fade_done() */
4696}
4697
4698static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004699wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004700{
4701 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004702 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004703
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004704 unlock(shell);
4705}
4706
4707static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004708center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004709{
Giulio Camuffob8366642013-04-25 13:57:46 +03004710 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004711 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004712
Jason Ekstranda7af7042013-10-12 22:38:11 -05004713 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004714
4715 x = output->x + (output->width - width) / 2 - surf_x / 2;
4716 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004717
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004718 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004719}
4720
4721static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004722weston_view_set_initial_position(struct weston_view *view,
4723 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004724{
4725 struct weston_compositor *compositor = shell->compositor;
4726 int ix = 0, iy = 0;
4727 int range_x, range_y;
4728 int dx, dy, x, y, panel_height;
4729 struct weston_output *output, *target_output = NULL;
4730 struct weston_seat *seat;
4731
4732 /* As a heuristic place the new window on the same output as the
4733 * pointer. Falling back to the output containing 0, 0.
4734 *
4735 * TODO: Do something clever for touch too?
4736 */
4737 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004738 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004739 ix = wl_fixed_to_int(seat->pointer->x);
4740 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004741 break;
4742 }
4743 }
4744
4745 wl_list_for_each(output, &compositor->output_list, link) {
4746 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4747 target_output = output;
4748 break;
4749 }
4750 }
4751
4752 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004753 weston_view_set_position(view, 10 + random() % 400,
4754 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004755 return;
4756 }
4757
4758 /* Valid range within output where the surface will still be onscreen.
4759 * If this is negative it means that the surface is bigger than
4760 * output.
4761 */
4762 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004763 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004764 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004765 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004766
Rob Bradford4cb88c72012-08-13 15:18:44 +01004767 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004768 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004769 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004770 dx = 0;
4771
4772 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004773 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004774 else
4775 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004776
4777 x = target_output->x + dx;
4778 y = target_output->y + dy;
4779
Jason Ekstranda7af7042013-10-12 22:38:11 -05004780 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004781}
4782
4783static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004784map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004785 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004786{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004787 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004788 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004789 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004790 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004791
Pekka Paalanen77346a62011-11-30 16:26:35 +02004792 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004793 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004794 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004795 if (shsurf->state.fullscreen) {
4796 center_on_output(shsurf->view, shsurf->fullscreen_output);
4797 shell_map_fullscreen(shsurf);
4798 } else if (shsurf->state.maximized) {
4799 /* use surface configure to set the geometry */
4800 panel_height = get_output_panel_height(shell, shsurf->output);
4801 surface_subsurfaces_boundingbox(shsurf->surface,
4802 &surf_x, &surf_y, NULL, NULL);
4803 weston_view_set_position(shsurf->view,
4804 shsurf->output->x - surf_x,
4805 shsurf->output->y +
4806 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004807 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004808 weston_view_set_initial_position(shsurf->view, shell);
4809 }
Juan Zhao96879df2012-02-07 08:45:41 +08004810 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004811 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004812 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004813 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004814 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004815 weston_view_set_position(shsurf->view,
4816 shsurf->view->geometry.x + sx,
4817 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004818 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004819 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004820 default:
4821 ;
4822 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004823
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004824 /* Surface stacking order, see also activate(). */
4825 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004826
Jason Ekstranda7af7042013-10-12 22:38:11 -05004827 if (shsurf->type != SHELL_SURFACE_NONE) {
4828 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004829 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004830 shsurf->surface->output = shsurf->output;
4831 shsurf->view->output = shsurf->output;
4832 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004833 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004834
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004836 /* XXX: xwayland's using the same fields for transient type */
4837 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004838 if (shsurf->transient.flags ==
4839 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4840 break;
4841 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004842 if (shsurf->state.relative &&
4843 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4844 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004845 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004846 break;
4847 wl_list_for_each(seat, &compositor->seat_list, link)
4848 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004849 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004850 case SHELL_SURFACE_POPUP:
4851 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004852 default:
4853 break;
4854 }
4855
Rafael Antognolli03b16592013-12-03 15:35:42 -02004856 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4857 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004858 {
4859 switch (shell->win_animation_type) {
4860 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004861 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004862 break;
4863 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004864 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004865 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004866 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004867 default:
4868 break;
4869 }
4870 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004871}
4872
4873static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004874configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004875 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004876{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004877 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004878 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004879 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004880
Pekka Paalanen77346a62011-11-30 16:26:35 +02004881 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004882
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004883 assert(shsurf);
4884
Rafael Antognolli03b16592013-12-03 15:35:42 -02004885 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004886 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004887 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004888 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004889 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4890 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004891 mx = shsurf->output->x - surf_x;
4892 my = shsurf->output->y +
4893 get_output_panel_height(shell,shsurf->output) - surf_y;
4894 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004895 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004896 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004897 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004898
Alex Wu4539b082012-03-01 12:57:46 +08004899 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004900 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004901 wl_list_for_each(view, &surface->views, surface_link)
4902 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004903
Rafael Antognolli03b16592013-12-03 15:35:42 -02004904 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004905 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004906 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004907}
4908
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004909static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004910shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004911{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004912 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004913 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004914 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004915
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004916 assert(shsurf);
4917
4918 shell = shsurf->shell;
4919
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004920 if (!weston_surface_is_mapped(es) &&
4921 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004922 remove_popup_grab(shsurf);
4923 }
4924
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004925 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004926 return;
4927
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004928 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004929 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004930 type_changed = 1;
4931 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004932
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004933 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004934 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004935 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004936 shsurf->last_width != es->width ||
4937 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004938 float from_x, from_y;
4939 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004940
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08004941 if (shsurf->resize_edges) {
4942 sx = 0;
4943 sy = 0;
4944 }
4945
4946 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
4947 sx = shsurf->last_width - es->width;
4948 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
4949 sy = shsurf->last_height - es->height;
4950
4951 shsurf->last_width = es->width;
4952 shsurf->last_height = es->height;
4953
Jason Ekstranda7af7042013-10-12 22:38:11 -05004954 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4955 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004956 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004957 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004958 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004959 }
4960}
4961
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004962static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004963
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004964static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004965desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004966{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004967 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004968 struct desktop_shell *shell =
4969 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004970
4971 shell->child.process.pid = 0;
4972 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004973
4974 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4975 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004976 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004977 shell->child.deathstamp = time;
4978 shell->child.deathcount = 0;
4979 }
4980
4981 shell->child.deathcount++;
4982 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004983 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004984 return;
4985 }
4986
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004987 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004988 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004989 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004990}
4991
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004992static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004993desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4994{
4995 struct desktop_shell *shell;
4996
4997 shell = container_of(listener, struct desktop_shell,
4998 child.client_destroy_listener);
4999
5000 shell->child.client = NULL;
5001}
5002
5003static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005004launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005005{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005006 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005007
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005008 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005009 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005010 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005011 desktop_shell_sigchld);
5012
5013 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005014 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005015
5016 shell->child.client_destroy_listener.notify =
5017 desktop_shell_client_destroy;
5018 wl_client_add_destroy_listener(shell->child.client,
5019 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005020}
5021
5022static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005023bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5024{
Tiago Vignattibe143262012-04-16 17:31:41 +03005025 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005026 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005027
Jason Ekstranda85118c2013-06-27 20:17:02 -05005028 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
5029 if (resource)
5030 wl_resource_set_implementation(resource, &shell_implementation,
5031 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005032}
5033
Kristian Høgsberg75840622011-09-06 13:48:16 -04005034static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005035handle_shell_client_destroy(struct wl_listener *listener, void *data)
5036{
5037 struct shell_client *sc =
5038 container_of(listener, struct shell_client, destroy_listener);
5039
5040 if (sc->ping_timer)
5041 wl_event_source_remove(sc->ping_timer);
5042 free(sc);
5043}
5044
5045static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02005046bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5047{
5048 struct desktop_shell *shell = data;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005049 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005050
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005051 sc = zalloc(sizeof *sc);
5052 if (sc == NULL) {
5053 wl_client_post_no_memory(client);
5054 return;
5055 }
5056
5057 sc->resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
5058 if (sc->resource == NULL) {
5059 free(sc);
5060 wl_client_post_no_memory(client);
5061 return;
5062 }
5063
5064 sc->client = client;
5065 sc->shell = shell;
5066 sc->destroy_listener.notify = handle_shell_client_destroy;
5067 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5068
5069 wl_resource_set_dispatcher(sc->resource,
5070 xdg_shell_unversioned_dispatch,
5071 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005072}
5073
5074static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005075unbind_desktop_shell(struct wl_resource *resource)
5076{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005077 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005078
5079 if (shell->locked)
5080 resume_desktop(shell);
5081
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005082 shell->child.desktop_shell = NULL;
5083 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005084}
5085
5086static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005087bind_desktop_shell(struct wl_client *client,
5088 void *data, uint32_t version, uint32_t id)
5089{
Tiago Vignattibe143262012-04-16 17:31:41 +03005090 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005091 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005092
Jason Ekstranda85118c2013-06-27 20:17:02 -05005093 resource = wl_resource_create(client, &desktop_shell_interface,
5094 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005095
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005096 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005097 wl_resource_set_implementation(resource,
5098 &desktop_shell_implementation,
5099 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005100 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005101
5102 if (version < 2)
5103 shell_fade_startup(shell);
5104
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005105 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005106 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005107
5108 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5109 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005110 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005111}
5112
Pekka Paalanen6e168112011-11-24 11:34:05 +02005113static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005114screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005115{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005116 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005117 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005118
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005119 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005120 return;
5121
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005122 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005123 if (!shell->locked)
5124 return;
5125
Jason Ekstranda7af7042013-10-12 22:38:11 -05005126 view = container_of(surface->views.next, struct weston_view, surface_link);
5127 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005128
Jason Ekstranda7af7042013-10-12 22:38:11 -05005129 if (wl_list_empty(&view->layer_link)) {
5130 wl_list_insert(shell->lock_layer.view_list.prev,
5131 &view->layer_link);
5132 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005133 wl_event_source_timer_update(shell->screensaver.timer,
5134 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005135 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005136 }
5137}
5138
5139static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005140screensaver_set_surface(struct wl_client *client,
5141 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005142 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005143 struct wl_resource *output_resource)
5144{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005145 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005146 struct weston_surface *surface =
5147 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005148 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005149 struct weston_view *view, *next;
5150
5151 /* Make sure we only have one view */
5152 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5153 weston_view_destroy(view);
5154 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005155
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005156 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005157 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005158 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005159}
5160
5161static const struct screensaver_interface screensaver_implementation = {
5162 screensaver_set_surface
5163};
5164
5165static void
5166unbind_screensaver(struct wl_resource *resource)
5167{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005168 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005169
Pekka Paalanen77346a62011-11-30 16:26:35 +02005170 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005171}
5172
5173static void
5174bind_screensaver(struct wl_client *client,
5175 void *data, uint32_t version, uint32_t id)
5176{
Tiago Vignattibe143262012-04-16 17:31:41 +03005177 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005178 struct wl_resource *resource;
5179
Jason Ekstranda85118c2013-06-27 20:17:02 -05005180 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005181
Pekka Paalanen77346a62011-11-30 16:26:35 +02005182 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005183 wl_resource_set_implementation(resource,
5184 &screensaver_implementation,
5185 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005186 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005187 return;
5188 }
5189
5190 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5191 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005192 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005193}
5194
Kristian Høgsberg07045392012-02-19 18:52:44 -05005195struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005196 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005197 struct weston_surface *current;
5198 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005199 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005200};
5201
5202static void
5203switcher_next(struct switcher *switcher)
5204{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005205 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005206 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005207 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005208 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005209
Jason Ekstranda7af7042013-10-12 22:38:11 -05005210 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005211 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005212 if (shsurf &&
5213 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5214 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005215 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005216 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005217 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005218 next = view->surface;
5219 prev = view->surface;
5220 view->alpha = 0.25;
5221 weston_view_geometry_dirty(view);
5222 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005223 }
Alex Wu1659daa2012-04-01 20:13:09 +08005224
Jason Ekstranda7af7042013-10-12 22:38:11 -05005225 if (is_black_surface(view->surface, NULL)) {
5226 view->alpha = 0.25;
5227 weston_view_geometry_dirty(view);
5228 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005229 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005230 }
5231
5232 if (next == NULL)
5233 next = first;
5234
Alex Wu07b26062012-03-12 16:06:01 +08005235 if (next == NULL)
5236 return;
5237
Kristian Høgsberg07045392012-02-19 18:52:44 -05005238 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005239 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005240
5241 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005242 wl_list_for_each(view, &next->views, surface_link)
5243 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005244
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005245 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005246 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005247 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005248}
5249
5250static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005251switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005252{
5253 struct switcher *switcher =
5254 container_of(listener, struct switcher, listener);
5255
5256 switcher_next(switcher);
5257}
5258
5259static void
Daniel Stone351eb612012-05-31 15:27:47 -04005260switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005261{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005262 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005263 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005264 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005265
Jason Ekstranda7af7042013-10-12 22:38:11 -05005266 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005267 if (is_focus_view(view))
5268 continue;
5269
Jason Ekstranda7af7042013-10-12 22:38:11 -05005270 view->alpha = 1.0;
5271 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005272 }
5273
Alex Wu07b26062012-03-12 16:06:01 +08005274 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005275 activate(switcher->shell, switcher->current,
5276 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005277 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005278 weston_keyboard_end_grab(keyboard);
5279 if (keyboard->input_method_resource)
5280 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005281 free(switcher);
5282}
5283
5284static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005285switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005286 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005287{
5288 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005289 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005290
Daniel Stonec9785ea2012-05-30 16:31:52 +01005291 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005292 switcher_next(switcher);
5293}
5294
5295static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005296switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005297 uint32_t mods_depressed, uint32_t mods_latched,
5298 uint32_t mods_locked, uint32_t group)
5299{
5300 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005301 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005302
Daniel Stone351eb612012-05-31 15:27:47 -04005303 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5304 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005305}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005306
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005307static void
5308switcher_cancel(struct weston_keyboard_grab *grab)
5309{
5310 struct switcher *switcher = container_of(grab, struct switcher, grab);
5311
5312 switcher_destroy(switcher);
5313}
5314
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005315static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005316 switcher_key,
5317 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005318 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005319};
5320
5321static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005322switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005323 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005324{
Tiago Vignattibe143262012-04-16 17:31:41 +03005325 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005326 struct switcher *switcher;
5327
5328 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005329 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005330 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005331 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005332 wl_list_init(&switcher->listener.link);
5333
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005334 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005335 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005336 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005337 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5338 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005339 switcher_next(switcher);
5340}
5341
Pekka Paalanen3c647232011-12-22 13:43:43 +02005342static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005343backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005344 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005345{
5346 struct weston_compositor *compositor = data;
5347 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005348 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005349
5350 /* TODO: we're limiting to simple use cases, where we assume just
5351 * control on the primary display. We'd have to extend later if we
5352 * ever get support for setting backlights on random desktop LCD
5353 * panels though */
5354 output = get_default_output(compositor);
5355 if (!output)
5356 return;
5357
5358 if (!output->set_backlight)
5359 return;
5360
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005361 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5362 backlight_new = output->backlight_current - 25;
5363 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5364 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005365
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005366 if (backlight_new < 5)
5367 backlight_new = 5;
5368 if (backlight_new > 255)
5369 backlight_new = 255;
5370
5371 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005372 output->set_backlight(output, output->backlight_current);
5373}
5374
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005375struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005376 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005377 struct weston_seat *seat;
5378 uint32_t key[2];
5379 int key_released[2];
5380};
5381
5382static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005383debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005384 uint32_t key, uint32_t state)
5385{
5386 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005387 struct weston_compositor *ec = db->seat->compositor;
5388 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005389 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005390 uint32_t serial;
5391 int send = 0, terminate = 0;
5392 int check_binding = 1;
5393 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005394 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005395
5396 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5397 /* Do not run bindings on key releases */
5398 check_binding = 0;
5399
5400 for (i = 0; i < 2; i++)
5401 if (key == db->key[i])
5402 db->key_released[i] = 1;
5403
5404 if (db->key_released[0] && db->key_released[1]) {
5405 /* All key releases been swalled so end the grab */
5406 terminate = 1;
5407 } else if (key != db->key[0] && key != db->key[1]) {
5408 /* Should not swallow release of other keys */
5409 send = 1;
5410 }
5411 } else if (key == db->key[0] && !db->key_released[0]) {
5412 /* Do not check bindings for the first press of the binding
5413 * key. This allows it to be used as a debug shortcut.
5414 * We still need to swallow this event. */
5415 check_binding = 0;
5416 } else if (db->key[1]) {
5417 /* If we already ran a binding don't process another one since
5418 * we can't keep track of all the binding keys that were
5419 * pressed in order to swallow the release events. */
5420 send = 1;
5421 check_binding = 0;
5422 }
5423
5424 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005425 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5426 key, state)) {
5427 /* We ran a binding so swallow the press and keep the
5428 * grab to swallow the released too. */
5429 send = 0;
5430 terminate = 0;
5431 db->key[1] = key;
5432 } else {
5433 /* Terminate the grab since the key pressed is not a
5434 * debug binding key. */
5435 send = 1;
5436 terminate = 1;
5437 }
5438 }
5439
5440 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005441 serial = wl_display_next_serial(display);
5442 resource_list = &grab->keyboard->focus_resource_list;
5443 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005444 wl_keyboard_send_key(resource, serial, time, key, state);
5445 }
5446 }
5447
5448 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005449 weston_keyboard_end_grab(grab->keyboard);
5450 if (grab->keyboard->input_method_resource)
5451 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005452 free(db);
5453 }
5454}
5455
5456static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005457debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005458 uint32_t mods_depressed, uint32_t mods_latched,
5459 uint32_t mods_locked, uint32_t group)
5460{
5461 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005462 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005463
Neil Roberts96d790e2013-09-19 17:32:00 +01005464 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005465
Neil Roberts96d790e2013-09-19 17:32:00 +01005466 wl_resource_for_each(resource, resource_list) {
5467 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5468 mods_latched, mods_locked, group);
5469 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005470}
5471
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005472static void
5473debug_binding_cancel(struct weston_keyboard_grab *grab)
5474{
5475 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5476
5477 weston_keyboard_end_grab(grab->keyboard);
5478 free(db);
5479}
5480
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005481struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005482 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005483 debug_binding_modifiers,
5484 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005485};
5486
5487static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005488debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005489{
5490 struct debug_binding_grab *grab;
5491
5492 grab = calloc(1, sizeof *grab);
5493 if (!grab)
5494 return;
5495
5496 grab->seat = (struct weston_seat *) seat;
5497 grab->key[0] = key;
5498 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005499 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005500}
5501
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005502static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005503force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005504 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005505{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005506 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005507 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005508 struct desktop_shell *shell = data;
5509 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005510 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005511
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005512 focus_surface = seat->keyboard->focus;
5513 if (!focus_surface)
5514 return;
5515
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005516 wl_signal_emit(&compositor->kill_signal, focus_surface);
5517
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005518 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005519 wl_client_get_credentials(client, &pid, NULL, NULL);
5520
5521 /* Skip clients that we launched ourselves (the credentials of
5522 * the socketpair is ours) */
5523 if (pid == getpid())
5524 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005525
Daniel Stone325fc2d2012-05-30 16:31:58 +01005526 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005527}
5528
5529static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005530workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005531 uint32_t key, void *data)
5532{
5533 struct desktop_shell *shell = data;
5534 unsigned int new_index = shell->workspaces.current;
5535
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005536 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005537 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005538 if (new_index != 0)
5539 new_index--;
5540
5541 change_workspace(shell, new_index);
5542}
5543
5544static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005545workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005546 uint32_t key, void *data)
5547{
5548 struct desktop_shell *shell = data;
5549 unsigned int new_index = shell->workspaces.current;
5550
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005551 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005552 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005553 if (new_index < shell->workspaces.num - 1)
5554 new_index++;
5555
5556 change_workspace(shell, new_index);
5557}
5558
5559static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005560workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005561 uint32_t key, void *data)
5562{
5563 struct desktop_shell *shell = data;
5564 unsigned int new_index;
5565
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005566 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005567 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005568 new_index = key - KEY_F1;
5569 if (new_index >= shell->workspaces.num)
5570 new_index = shell->workspaces.num - 1;
5571
5572 change_workspace(shell, new_index);
5573}
5574
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005575static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005576workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005577 uint32_t key, void *data)
5578{
5579 struct desktop_shell *shell = data;
5580 unsigned int new_index = shell->workspaces.current;
5581
5582 if (shell->locked)
5583 return;
5584
5585 if (new_index != 0)
5586 new_index--;
5587
5588 take_surface_to_workspace_by_seat(shell, seat, new_index);
5589}
5590
5591static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005592workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005593 uint32_t key, void *data)
5594{
5595 struct desktop_shell *shell = data;
5596 unsigned int new_index = shell->workspaces.current;
5597
5598 if (shell->locked)
5599 return;
5600
5601 if (new_index < shell->workspaces.num - 1)
5602 new_index++;
5603
5604 take_surface_to_workspace_by_seat(shell, seat, new_index);
5605}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005606
5607static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005608shell_reposition_view_on_output_destroy(struct weston_view *view)
5609{
5610 struct weston_output *output, *first_output;
5611 struct weston_compositor *ec = view->surface->compositor;
5612 struct shell_surface *shsurf;
5613 float x, y;
5614 int visible;
5615
5616 x = view->geometry.x;
5617 y = view->geometry.y;
5618
5619 /* At this point the destroyed output is not in the list anymore.
5620 * If the view is still visible somewhere, we leave where it is,
5621 * otherwise, move it to the first output. */
5622 visible = 0;
5623 wl_list_for_each(output, &ec->output_list, link) {
5624 if (pixman_region32_contains_point(&output->region,
5625 x, y, NULL)) {
5626 visible = 1;
5627 break;
5628 }
5629 }
5630
5631 if (!visible) {
5632 first_output = container_of(ec->output_list.next,
5633 struct weston_output, link);
5634
5635 x = first_output->x + first_output->width / 4;
5636 y = first_output->y + first_output->height / 4;
5637 }
5638
5639 weston_view_set_position(view, x, y);
5640
5641 shsurf = get_shell_surface(view->surface);
5642
5643 if (shsurf) {
5644 shsurf->saved_position_valid = false;
5645 shsurf->next_state.maximized = false;
5646 shsurf->next_state.fullscreen = false;
5647 shsurf->state_changed = true;
5648 }
5649}
5650
5651static void
5652shell_reposition_views_on_output_destroy(struct shell_output *shell_output)
5653{
5654 struct desktop_shell *shell = shell_output->shell;
5655 struct weston_output *output = shell_output->output;
5656 struct weston_layer *layer;
5657 struct weston_view *view;
5658
5659 /* Move all views in the layers owned by the shell */
5660 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5661 wl_list_for_each(view, &layer->view_list, layer_link) {
5662 if (view->output != output)
5663 continue;
5664
5665 shell_reposition_view_on_output_destroy(view);
5666 }
5667
5668 /* We don't start from the beggining of the layer list, so
5669 * make sure we don't wrap around it. */
5670 if (layer == &shell->background_layer)
5671 break;
5672 }
5673}
5674
5675static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005676handle_output_destroy(struct wl_listener *listener, void *data)
5677{
5678 struct shell_output *output_listener =
5679 container_of(listener, struct shell_output, destroy_listener);
5680
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005681 shell_reposition_views_on_output_destroy(output_listener);
5682
Xiong Zhang6b481422013-10-23 13:58:32 +08005683 wl_list_remove(&output_listener->destroy_listener.link);
5684 wl_list_remove(&output_listener->link);
5685 free(output_listener);
5686}
5687
5688static void
5689create_shell_output(struct desktop_shell *shell,
5690 struct weston_output *output)
5691{
5692 struct shell_output *shell_output;
5693
5694 shell_output = zalloc(sizeof *shell_output);
5695 if (shell_output == NULL)
5696 return;
5697
5698 shell_output->output = output;
5699 shell_output->shell = shell;
5700 shell_output->destroy_listener.notify = handle_output_destroy;
5701 wl_signal_add(&output->destroy_signal,
5702 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005703 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005704}
5705
5706static void
5707handle_output_create(struct wl_listener *listener, void *data)
5708{
5709 struct desktop_shell *shell =
5710 container_of(listener, struct desktop_shell, output_create_listener);
5711 struct weston_output *output = (struct weston_output *)data;
5712
5713 create_shell_output(shell, output);
5714}
5715
5716static void
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005717handle_output_move(struct wl_listener *listener, void *data)
5718{
5719 struct desktop_shell *shell;
5720 struct weston_output *output;
5721 struct weston_layer *layer;
5722 struct weston_view *view;
5723 float x, y;
5724
5725 shell = container_of(listener, struct desktop_shell,
5726 output_move_listener);
5727 output = data;
5728
5729 /* Move all views in the layers owned by the shell */
5730 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5731 wl_list_for_each(view, &layer->view_list, layer_link) {
5732 if (view->output != output)
5733 continue;
5734
5735 x = view->geometry.x + output->move_x;
5736 y = view->geometry.y + output->move_y;
5737 weston_view_set_position(view, x, y);
5738 }
5739
5740 /* We don't start from the beggining of the layer list, so
5741 * make sure we don't wrap around it. */
5742 if (layer == &shell->background_layer)
5743 break;
5744 }
5745}
5746
5747static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005748setup_output_destroy_handler(struct weston_compositor *ec,
5749 struct desktop_shell *shell)
5750{
5751 struct weston_output *output;
5752
5753 wl_list_init(&shell->output_list);
5754 wl_list_for_each(output, &ec->output_list, link)
5755 create_shell_output(shell, output);
5756
5757 shell->output_create_listener.notify = handle_output_create;
5758 wl_signal_add(&ec->output_created_signal,
5759 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005760
5761 shell->output_move_listener.notify = handle_output_move;
5762 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005763}
5764
5765static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005766shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005767{
Tiago Vignattibe143262012-04-16 17:31:41 +03005768 struct desktop_shell *shell =
5769 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005770 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005771 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005772
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005773 /* Force state to unlocked so we don't try to fade */
5774 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005775 if (shell->child.client)
5776 wl_client_destroy(shell->child.client);
5777
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005778 wl_list_remove(&shell->idle_listener.link);
5779 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005780
5781 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005782
Xiong Zhang6b481422013-10-23 13:58:32 +08005783 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5784 wl_list_remove(&shell_output->destroy_listener.link);
5785 wl_list_remove(&shell_output->link);
5786 free(shell_output);
5787 }
5788
5789 wl_list_remove(&shell->output_create_listener.link);
5790
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005791 wl_array_for_each(ws, &shell->workspaces.array)
5792 workspace_destroy(*ws);
5793 wl_array_release(&shell->workspaces.array);
5794
Pekka Paalanen3c647232011-12-22 13:43:43 +02005795 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005796 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005797 free(shell);
5798}
5799
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005800static void
5801shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5802{
5803 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005804 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005805
5806 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005807 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5808 MODIFIER_CTRL | MODIFIER_ALT,
5809 terminate_binding, ec);
5810 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5811 click_to_activate_binding,
5812 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005813 weston_compositor_add_touch_binding(ec, 0,
5814 touch_to_activate_binding,
5815 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005816 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5817 MODIFIER_SUPER | MODIFIER_ALT,
5818 surface_opacity_binding, NULL);
5819 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5820 MODIFIER_SUPER, zoom_axis_binding,
5821 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005822
5823 /* configurable bindings */
5824 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005825 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5826 zoom_key_binding, NULL);
5827 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5828 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005829 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5830 maximize_binding, NULL);
5831 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5832 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005833 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5834 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005835 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005836 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5837 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005838 weston_compositor_add_button_binding(ec, BTN_LEFT,
5839 mod | MODIFIER_SHIFT,
5840 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005841
5842 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5843 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5844 rotate_binding, NULL);
5845
Daniel Stone325fc2d2012-05-30 16:31:58 +01005846 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5847 shell);
5848 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5849 ec);
5850 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5851 backlight_binding, ec);
5852 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5853 ec);
5854 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5855 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005856 weston_compositor_add_key_binding(ec, KEY_K, mod,
5857 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005858 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5859 workspace_up_binding, shell);
5860 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5861 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005862 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5863 workspace_move_surface_up_binding,
5864 shell);
5865 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5866 workspace_move_surface_down_binding,
5867 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005868
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005869 if (shell->exposay_modifier)
5870 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5871 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005872
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005873 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5874 if (shell->workspaces.num > 1) {
5875 num_workspace_bindings = shell->workspaces.num;
5876 if (num_workspace_bindings > 6)
5877 num_workspace_bindings = 6;
5878 for (i = 0; i < num_workspace_bindings; i++)
5879 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5880 workspace_f_binding,
5881 shell);
5882 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005883
5884 /* Debug bindings */
5885 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5886 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005887}
5888
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005889WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005890module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005891 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005892{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005893 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005894 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005895 struct workspace **pws;
5896 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005897 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005898
Peter Huttererf3d62272013-08-08 11:57:05 +10005899 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005900 if (shell == NULL)
5901 return -1;
5902
Kristian Høgsberg75840622011-09-06 13:48:16 -04005903 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005904
5905 shell->destroy_listener.notify = shell_destroy;
5906 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005907 shell->idle_listener.notify = idle_handler;
5908 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5909 shell->wake_listener.notify = wake_handler;
5910 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005911
Scott Moreauff1db4a2012-04-17 19:06:18 -06005912 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005913 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005914 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005915 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005916 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005917 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005918 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005919 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005920 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005921 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005922 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005923
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005924 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5925 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005926 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5927 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005928 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005929
5930 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005931 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005932
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005933 if (input_panel_setup(shell) < 0)
5934 return -1;
5935
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005936 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005937
Daniel Stonedf8133b2013-11-19 11:37:14 +01005938 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5939 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5940
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005941 for (i = 0; i < shell->workspaces.num; i++) {
5942 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5943 if (pws == NULL)
5944 return -1;
5945
5946 *pws = workspace_create();
5947 if (*pws == NULL)
5948 return -1;
5949 }
5950 activate_workspace(shell, 0);
5951
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005952 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005953 wl_list_init(&shell->workspaces.animation.link);
5954 shell->workspaces.animation.frame = animate_workspace_change_frame;
5955
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005956 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005957 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005958 return -1;
5959
Rafael Antognollie2a34552013-12-03 15:35:45 -02005960 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5961 shell, bind_xdg_shell) == NULL)
5962 return -1;
5963
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005964 if (wl_global_create(ec->wl_display,
5965 &desktop_shell_interface, 2,
5966 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005967 return -1;
5968
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005969 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5970 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005971 return -1;
5972
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005973 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5974 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005975 return -1;
5976
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005977 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005978
Xiong Zhang6b481422013-10-23 13:58:32 +08005979 setup_output_destroy_handler(ec, shell);
5980
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005981 loop = wl_display_get_event_loop(ec->wl_display);
5982 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005983
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005984 shell->screensaver.timer =
5985 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5986
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005987 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005988 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005989 create_keyboard_focus_listener(seat);
5990 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005991
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005992 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005993
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005994 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005995
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005996 return 0;
5997}