blob: a47bbe70ca18300f41365cb9c50e430500da8daa [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;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001879 if (shsurf->unresponsive) {
1880 ping_timeout_handler(shsurf);
1881 return;
1882 }
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001883
1884 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
1885 if (!shsurf->ping_timer)
1886 return;
1887
1888 shsurf->ping_timer->serial = serial;
1889 loop = wl_display_get_event_loop(compositor->wl_display);
1890 shsurf->ping_timer->source =
1891 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1892 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1893
1894 wl_shell_surface_send_ping(shsurf->resource, serial);
1895}
1896
1897static void
1898handle_shell_client_destroy(struct wl_listener *listener, void *data);
1899
1900static int
1901xdg_ping_timeout_handler(void *data)
1902{
1903 struct shell_client *sc = data;
1904 struct weston_seat *seat;
1905 struct shell_surface *shsurf;
1906
1907 /* Client is not responding */
1908 sc->unresponsive = 1;
1909
1910 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1911 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1912 continue;
1913 if (seat->pointer->focus->surface->resource == NULL)
1914 continue;
1915
1916 shsurf = get_shell_surface(seat->pointer->focus->surface);
1917 if (shsurf &&
1918 wl_resource_get_client(shsurf->resource) == sc->client)
1919 set_busy_cursor(shsurf, seat->pointer);
1920 }
1921
1922 return 1;
1923}
1924
1925static void
1926handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1927{
1928 struct weston_compositor *compositor = shsurf->shell->compositor;
1929 struct wl_listener *listener;
1930 struct wl_client *client = wl_resource_get_client(shsurf->resource);
1931 struct shell_client *sc;
1932 struct wl_event_loop *loop;
1933
1934 listener = wl_client_get_destroy_listener(client,
1935 handle_shell_client_destroy);
1936 sc = container_of(listener, struct shell_client, destroy_listener);
1937
1938 if (sc->unresponsive) {
1939 xdg_ping_timeout_handler(sc);
1940 return;
1941 }
1942
1943 sc->ping_serial = serial;
1944 loop = wl_display_get_event_loop(compositor->wl_display);
1945 if (sc->ping_timer == NULL)
1946 sc->ping_timer =
1947 wl_event_loop_add_timer(loop,
1948 xdg_ping_timeout_handler, sc);
1949 if (sc->ping_timer == NULL)
1950 return;
1951
1952 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1953
1954 xdg_shell_send_ping(sc->resource, serial);
1955}
1956
Scott Moreauff1db4a2012-04-17 19:06:18 -06001957static void
1958ping_handler(struct weston_surface *surface, uint32_t serial)
1959{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001960 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001961
1962 if (!shsurf)
1963 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001964 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001965 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001966 if (shsurf->surface == shsurf->shell->grab_surface)
1967 return;
1968
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001969 if (shell_surface_is_xdg_surface(shsurf) ||
1970 shell_surface_is_xdg_popup(shsurf))
1971 handle_xdg_ping(shsurf, serial);
1972 else if (shell_surface_is_wl_shell_surface(shsurf))
1973 handle_wl_shell_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001974}
1975
1976static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001977handle_pointer_focus(struct wl_listener *listener, void *data)
1978{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001979 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001981 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001982 uint32_t serial;
1983
Jason Ekstranda7af7042013-10-12 22:38:11 -05001984 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001985 return;
1986
Jason Ekstranda7af7042013-10-12 22:38:11 -05001987 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001988 serial = wl_display_next_serial(compositor->wl_display);
1989 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001990}
1991
1992static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001993create_pointer_focus_listener(struct weston_seat *seat)
1994{
1995 struct wl_listener *listener;
1996
Kristian Høgsberge3148752013-05-06 23:19:49 -04001997 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001998 return;
1999
2000 listener = malloc(sizeof *listener);
2001 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002002 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04002003}
2004
2005static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002006shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2007{
2008 if (--shsurf->focus_count == 0)
2009 if (shell_surface_is_xdg_surface(shsurf))
2010 xdg_surface_send_focused_unset(shsurf->resource);
2011}
2012
2013static void
2014shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2015{
2016 if (shsurf->focus_count++ == 0)
2017 if (shell_surface_is_xdg_surface(shsurf))
2018 xdg_surface_send_focused_set(shsurf->resource);
2019}
2020
2021static void
2022handle_keyboard_focus(struct wl_listener *listener, void *data)
2023{
2024 struct weston_keyboard *keyboard = data;
2025 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2026
2027 if (seat->focused_surface) {
2028 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2029 if (shsurf)
2030 shell_surface_lose_keyboard_focus(shsurf);
2031 }
2032
2033 seat->focused_surface = keyboard->focus;
2034
2035 if (seat->focused_surface) {
2036 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2037 if (shsurf)
2038 shell_surface_gain_keyboard_focus(shsurf);
2039 }
2040}
2041
2042static void
2043create_keyboard_focus_listener(struct weston_seat *seat)
2044{
2045 struct wl_listener *listener;
2046
2047 if (!seat->keyboard)
2048 return;
2049
2050 listener = malloc(sizeof *listener);
2051 listener->notify = handle_keyboard_focus;
2052 wl_signal_add(&seat->keyboard->focus_signal, listener);
2053}
2054
2055static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002056xdg_surface_set_transient_for(struct wl_client *client,
2057 struct wl_resource *resource,
2058 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06002059{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002060 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05002061 struct weston_surface *parent;
2062
2063 if (parent_resource)
2064 parent = wl_resource_get_user_data(parent_resource);
2065 else
2066 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002067
2068 shell_surface_set_parent(shsurf, parent);
2069}
2070
2071static void
2072surface_pong(struct shell_surface *shsurf, uint32_t serial)
2073{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002074 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002075
Kristian Høgsberg92374e12012-08-11 22:39:12 -04002076 if (shsurf->ping_timer == NULL)
2077 /* Just ignore unsolicited pong. */
2078 return;
2079
Scott Moreauff1db4a2012-04-17 19:06:18 -06002080 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002081 shsurf->unresponsive = 0;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002082 end_busy_cursor(ec, wl_resource_get_client(shsurf->resource));
Scott Moreauff1db4a2012-04-17 19:06:18 -06002083 }
2084}
2085
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002086static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002087shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
2088 uint32_t serial)
2089{
2090 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2091
2092 surface_pong(shsurf, serial);
2093
2094}
2095
2096static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002097set_title(struct shell_surface *shsurf, const char *title)
2098{
2099 free(shsurf->title);
2100 shsurf->title = strdup(title);
2101}
2102
2103static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002104shell_surface_set_title(struct wl_client *client,
2105 struct wl_resource *resource, const char *title)
2106{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002107 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002108
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002109 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002110}
2111
2112static void
2113shell_surface_set_class(struct wl_client *client,
2114 struct wl_resource *resource, const char *class)
2115{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002116 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002117
2118 free(shsurf->class);
2119 shsurf->class = strdup(class);
2120}
2121
Alex Wu4539b082012-03-01 12:57:46 +08002122static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002123restore_output_mode(struct weston_output *output)
2124{
Hardening57388e42013-09-18 23:56:36 +02002125 if (output->current_mode != output->original_mode ||
2126 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002127 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002128 output->original_mode,
2129 output->original_scale,
2130 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002131}
2132
2133static void
2134restore_all_output_modes(struct weston_compositor *compositor)
2135{
2136 struct weston_output *output;
2137
2138 wl_list_for_each(output, &compositor->output_list, link)
2139 restore_output_mode(output);
2140}
2141
Philip Withnallbecb77e2013-11-25 18:01:30 +00002142static int
2143get_output_panel_height(struct desktop_shell *shell,
2144 struct weston_output *output)
2145{
2146 struct weston_view *view;
2147 int panel_height = 0;
2148
2149 if (!output)
2150 return 0;
2151
2152 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2153 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002154 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002155 break;
2156 }
2157 }
2158
2159 return panel_height;
2160}
2161
Philip Withnall07926d92013-11-25 18:01:40 +00002162/* The surface will be inserted into the list immediately after the link
2163 * returned by this function (i.e. will be stacked immediately above the
2164 * returned link). */
2165static struct wl_list *
2166shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2167{
2168 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002169 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002170
2171 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002172 case SHELL_SURFACE_POPUP:
2173 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002174 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002175 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002176 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002177 /* Move the surface to its parent layer so
2178 * that surfaces which are transient for
2179 * fullscreen surfaces don't get hidden by the
2180 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002181
2182 /* TODO: Handle a parent with multiple views */
2183 parent = get_default_view(shsurf->parent);
2184 if (parent)
2185 return parent->layer_link.prev;
2186 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002187 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002188
2189 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002190 return &shsurf->shell->fullscreen_layer.view_list;
2191
Philip Withnall07926d92013-11-25 18:01:40 +00002192 case SHELL_SURFACE_NONE:
2193 default:
2194 /* Go to the fallback, below. */
2195 break;
2196 }
2197
2198 /* Move the surface to a normal workspace layer so that surfaces
2199 * which were previously fullscreen or transient are no longer
2200 * rendered on top. */
2201 ws = get_current_workspace(shsurf->shell);
2202 return &ws->layer.view_list;
2203}
2204
Philip Withnall648a4dd2013-11-25 18:01:44 +00002205static void
2206shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2207{
2208 struct shell_surface *child;
2209
2210 /* Move the child layers to the same workspace as shsurf. They will be
2211 * stacked above shsurf. */
2212 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2213 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2214 weston_view_geometry_dirty(child->view);
2215 wl_list_remove(&child->view->layer_link);
2216 wl_list_insert(shsurf->view->layer_link.prev,
2217 &child->view->layer_link);
2218 weston_view_geometry_dirty(child->view);
2219 weston_surface_damage(child->surface);
2220
2221 /* Recurse. We don’t expect this to recurse very far (if
2222 * at all) because that would imply we have transient
2223 * (or popup) children of transient surfaces, which
2224 * would be unusual. */
2225 shell_surface_update_child_surface_layers(child);
2226 }
2227 }
2228}
2229
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002230/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002231 * geometry to ensure the changes are redrawn.
2232 *
2233 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2234 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002235static void
2236shell_surface_update_layer(struct shell_surface *shsurf)
2237{
2238 struct wl_list *new_layer_link;
2239
2240 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2241
2242 if (new_layer_link == &shsurf->view->layer_link)
2243 return;
2244
2245 weston_view_geometry_dirty(shsurf->view);
2246 wl_list_remove(&shsurf->view->layer_link);
2247 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2248 weston_view_geometry_dirty(shsurf->view);
2249 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002250
2251 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002252}
2253
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002254static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002255shell_surface_set_parent(struct shell_surface *shsurf,
2256 struct weston_surface *parent)
2257{
2258 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002259
2260 wl_list_remove(&shsurf->children_link);
2261 wl_list_init(&shsurf->children_link);
2262
2263 /* Insert into the parent surface’s child list. */
2264 if (parent != NULL) {
2265 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2266 if (parent_shsurf != NULL)
2267 wl_list_insert(&parent_shsurf->children_list,
2268 &shsurf->children_link);
2269 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002270}
2271
2272static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002273shell_surface_set_output(struct shell_surface *shsurf,
2274 struct weston_output *output)
2275{
2276 struct weston_surface *es = shsurf->surface;
2277
2278 /* get the default output, if the client set it as NULL
2279 check whether the ouput is available */
2280 if (output)
2281 shsurf->output = output;
2282 else if (es->output)
2283 shsurf->output = es->output;
2284 else
2285 shsurf->output = get_default_output(es->compositor);
2286}
2287
2288static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002289surface_clear_next_states(struct shell_surface *shsurf)
2290{
2291 shsurf->next_state.maximized = false;
2292 shsurf->next_state.fullscreen = false;
2293
2294 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2295 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2296 shsurf->state_changed = true;
2297}
2298
2299static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002300set_toplevel(struct shell_surface *shsurf)
2301{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002302 shell_surface_set_parent(shsurf, NULL);
2303 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002304 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002305
2306 /* The layer_link is updated in set_surface_type(),
2307 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002308}
2309
2310static void
2311shell_surface_set_toplevel(struct wl_client *client,
2312 struct wl_resource *resource)
2313{
2314 struct shell_surface *surface = wl_resource_get_user_data(resource);
2315
2316 set_toplevel(surface);
2317}
2318
2319static void
2320set_transient(struct shell_surface *shsurf,
2321 struct weston_surface *parent, int x, int y, uint32_t flags)
2322{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002323 assert(parent != NULL);
2324
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002325 shell_surface_set_parent(shsurf, parent);
2326
2327 surface_clear_next_states(shsurf);
2328
Philip Withnallbecb77e2013-11-25 18:01:30 +00002329 shsurf->transient.x = x;
2330 shsurf->transient.y = y;
2331 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002332
Rafael Antognollied207b42013-12-03 15:35:43 -02002333 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002334 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002335 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002336
2337 /* The layer_link is updated in set_surface_type(),
2338 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002339}
2340
2341static void
2342shell_surface_set_transient(struct wl_client *client,
2343 struct wl_resource *resource,
2344 struct wl_resource *parent_resource,
2345 int x, int y, uint32_t flags)
2346{
2347 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2348 struct weston_surface *parent =
2349 wl_resource_get_user_data(parent_resource);
2350
2351 set_transient(shsurf, parent, x, y, flags);
2352}
2353
2354static void
2355set_fullscreen(struct shell_surface *shsurf,
2356 uint32_t method,
2357 uint32_t framerate,
2358 struct weston_output *output)
2359{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002360 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002361
2362 shsurf->fullscreen_output = shsurf->output;
2363 shsurf->fullscreen.type = method;
2364 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002365
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002366 shsurf->next_state.fullscreen = true;
2367 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002368 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002369
2370 shsurf->client->send_configure(shsurf->surface, 0,
2371 shsurf->output->width,
2372 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002373
2374 /* The layer_link is updated in set_surface_type(),
2375 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002376}
2377
2378static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002379weston_view_set_initial_position(struct weston_view *view,
2380 struct desktop_shell *shell);
2381
2382static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002383unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002384{
Philip Withnallf85fe842013-11-25 18:01:37 +00002385 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002386 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2387 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002388 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002389 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002390 shsurf->fullscreen_output = NULL;
2391
Alex Wu4539b082012-03-01 12:57:46 +08002392 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2393 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002394
Alex Wu4539b082012-03-01 12:57:46 +08002395 wl_list_remove(&shsurf->fullscreen.transform.link);
2396 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002397
Jason Ekstranda7af7042013-10-12 22:38:11 -05002398 if (shsurf->fullscreen.black_view)
2399 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2400 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002401
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002402 if (shsurf->saved_position_valid)
2403 weston_view_set_position(shsurf->view,
2404 shsurf->saved_x, shsurf->saved_y);
2405 else
2406 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2407
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002408 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002409 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002410 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002411 shsurf->saved_rotation_valid = false;
2412 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002413
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002414 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002415}
2416
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002417static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002418shell_surface_set_fullscreen(struct wl_client *client,
2419 struct wl_resource *resource,
2420 uint32_t method,
2421 uint32_t framerate,
2422 struct wl_resource *output_resource)
2423{
2424 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2425 struct weston_output *output;
2426
2427 if (output_resource)
2428 output = wl_resource_get_user_data(output_resource);
2429 else
2430 output = NULL;
2431
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002432 shell_surface_set_parent(shsurf, NULL);
2433
Rafael Antognolli03b16592013-12-03 15:35:42 -02002434 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002435 set_fullscreen(shsurf, method, framerate, output);
2436}
2437
2438static void
2439set_popup(struct shell_surface *shsurf,
2440 struct weston_surface *parent,
2441 struct weston_seat *seat,
2442 uint32_t serial,
2443 int32_t x,
2444 int32_t y)
2445{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002446 assert(parent != NULL);
2447
Philip Withnallbecb77e2013-11-25 18:01:30 +00002448 shsurf->popup.shseat = get_shell_seat(seat);
2449 shsurf->popup.serial = serial;
2450 shsurf->popup.x = x;
2451 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002452
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002453 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002454}
2455
2456static void
2457shell_surface_set_popup(struct wl_client *client,
2458 struct wl_resource *resource,
2459 struct wl_resource *seat_resource,
2460 uint32_t serial,
2461 struct wl_resource *parent_resource,
2462 int32_t x, int32_t y, uint32_t flags)
2463{
2464 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002465 struct weston_surface *parent =
2466 wl_resource_get_user_data(parent_resource);
2467
2468 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002469
Rafael Antognolli03b16592013-12-03 15:35:42 -02002470 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002471 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002472 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002473 wl_resource_get_user_data(seat_resource),
2474 serial, x, y);
2475}
2476
2477static void
2478set_maximized(struct shell_surface *shsurf,
2479 struct weston_output *output)
2480{
2481 struct desktop_shell *shell;
2482 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002483
Philip Withnall352e7ed2013-11-25 18:01:35 +00002484 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002485
2486 shell = shell_surface_get_shell(shsurf);
2487 panel_height = get_output_panel_height(shell, shsurf->output);
2488 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2489
2490 shsurf->client->send_configure(shsurf->surface, edges,
2491 shsurf->output->width,
2492 shsurf->output->height - panel_height);
2493
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002494 shsurf->next_state.maximized = true;
2495 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002496 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002497}
2498
2499static void
2500unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002501{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002502 /* undo all maximized things here */
2503 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002504
2505 if (shsurf->saved_position_valid)
2506 weston_view_set_position(shsurf->view,
2507 shsurf->saved_x, shsurf->saved_y);
2508 else
2509 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002510
2511 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002512 wl_list_insert(&shsurf->view->geometry.transformation_list,
2513 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002514 shsurf->saved_rotation_valid = false;
2515 }
2516
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002517 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002518}
2519
Philip Withnallbecb77e2013-11-25 18:01:30 +00002520static void
2521shell_surface_set_maximized(struct wl_client *client,
2522 struct wl_resource *resource,
2523 struct wl_resource *output_resource)
2524{
2525 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2526 struct weston_output *output;
2527
2528 if (output_resource)
2529 output = wl_resource_get_user_data(output_resource);
2530 else
2531 output = NULL;
2532
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002533 shell_surface_set_parent(shsurf, NULL);
2534
Rafael Antognolli03b16592013-12-03 15:35:42 -02002535 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002536 set_maximized(shsurf, output);
2537}
2538
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002539/* This is only ever called from set_surface_type(), so there’s no need to
2540 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002541static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002542reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002543{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002544 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002545 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002546 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002547 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002548
Pekka Paalanen98262232011-12-01 10:42:22 +02002549 return 0;
2550}
2551
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002552static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002553set_full_output(struct shell_surface *shsurf)
2554{
2555 shsurf->saved_x = shsurf->view->geometry.x;
2556 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002557 shsurf->saved_width = shsurf->surface->width;
2558 shsurf->saved_height = shsurf->surface->height;
2559 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002560 shsurf->saved_position_valid = true;
2561
2562 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2563 wl_list_remove(&shsurf->rotation.transform.link);
2564 wl_list_init(&shsurf->rotation.transform.link);
2565 weston_view_geometry_dirty(shsurf->view);
2566 shsurf->saved_rotation_valid = true;
2567 }
2568}
2569
2570static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002571set_surface_type(struct shell_surface *shsurf)
2572{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002573 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002574 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002575
Philip Withnallbecb77e2013-11-25 18:01:30 +00002576 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002577
Rafael Antognolli03b16592013-12-03 15:35:42 -02002578 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002579 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002580
2581 switch (shsurf->type) {
2582 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002583 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002584 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002585 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002586 weston_view_set_position(shsurf->view,
2587 pev->geometry.x + shsurf->transient.x,
2588 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002589 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002590 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002591
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002592 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593 weston_view_set_position(shsurf->view, shsurf->transient.x,
2594 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002595 break;
2596
Philip Withnall0f640e22013-11-25 18:01:31 +00002597 case SHELL_SURFACE_POPUP:
2598 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002599 default:
2600 break;
2601 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002602
2603 /* Update the surface’s layer. */
2604 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002605}
2606
Tiago Vignattibe143262012-04-16 17:31:41 +03002607static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002608shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002609{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002610 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002611}
2612
Alex Wu21858432012-04-01 20:13:08 +08002613static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002614black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002615
Jason Ekstranda7af7042013-10-12 22:38:11 -05002616static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002617create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002618 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002619 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002620{
2621 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002622 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002623
2624 surface = weston_surface_create(ec);
2625 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002626 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002627 return NULL;
2628 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002629 view = weston_view_create(surface);
2630 if (surface == NULL) {
2631 weston_log("no memory\n");
2632 weston_surface_destroy(surface);
2633 return NULL;
2634 }
Alex Wu4539b082012-03-01 12:57:46 +08002635
Alex Wu21858432012-04-01 20:13:08 +08002636 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002637 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002638 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002639 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002640 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002641 pixman_region32_fini(&surface->input);
2642 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002643
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002644 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002645 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002646
2647 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002648}
2649
Philip Withnalled8f1a92013-11-25 18:01:43 +00002650static void
2651shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2652{
2653 struct weston_output *output = shsurf->fullscreen_output;
2654
Rafael Antognolli03b16592013-12-03 15:35:42 -02002655 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002656
2657 if (!shsurf->fullscreen.black_view)
2658 shsurf->fullscreen.black_view =
2659 create_black_surface(shsurf->surface->compositor,
2660 shsurf->surface,
2661 output->x, output->y,
2662 output->width,
2663 output->height);
2664
2665 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2666 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2667 wl_list_insert(&shsurf->view->layer_link,
2668 &shsurf->fullscreen.black_view->layer_link);
2669 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2670 weston_surface_damage(shsurf->surface);
2671}
2672
Alex Wu4539b082012-03-01 12:57:46 +08002673/* Create black surface and append it to the associated fullscreen surface.
2674 * Handle size dismatch and positioning according to the method. */
2675static void
2676shell_configure_fullscreen(struct shell_surface *shsurf)
2677{
2678 struct weston_output *output = shsurf->fullscreen_output;
2679 struct weston_surface *surface = shsurf->surface;
2680 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002681 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002682 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002683
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002684 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2685 restore_output_mode(output);
2686
Philip Withnalled8f1a92013-11-25 18:01:43 +00002687 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002688
Jason Ekstranda7af7042013-10-12 22:38:11 -05002689 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002690 &surf_width, &surf_height);
2691
Alex Wu4539b082012-03-01 12:57:46 +08002692 switch (shsurf->fullscreen.type) {
2693 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002694 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002695 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002696 break;
2697 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002698 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002699 if (output->width == surf_width &&
2700 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002701 weston_view_set_position(shsurf->view,
2702 output->x - surf_x,
2703 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002704 break;
2705 }
2706
Alex Wu4539b082012-03-01 12:57:46 +08002707 matrix = &shsurf->fullscreen.transform.matrix;
2708 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002709
Scott Moreau1bad5db2012-08-18 01:04:05 -06002710 output_aspect = (float) output->width /
2711 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002712 /* XXX: Use surf_width and surf_height here? */
2713 surface_aspect = (float) surface->width /
2714 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002715 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002716 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002717 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002718 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002719 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002720 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002721
Alex Wu4539b082012-03-01 12:57:46 +08002722 weston_matrix_scale(matrix, scale, scale, 1);
2723 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002725 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002726 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2727 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002728 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002729
Alex Wu4539b082012-03-01 12:57:46 +08002730 break;
2731 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002732 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002733 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002734 surf_width * surface->buffer_viewport.scale,
2735 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002736 shsurf->fullscreen.framerate};
2737
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002738 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002739 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 weston_view_set_position(shsurf->view,
2741 output->x - surf_x,
2742 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002743 shsurf->fullscreen.black_view->surface->width = output->width;
2744 shsurf->fullscreen.black_view->surface->height = output->height;
2745 weston_view_set_position(shsurf->fullscreen.black_view,
2746 output->x - surf_x,
2747 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002748 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002749 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002750 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002751 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002752 }
Alex Wubd3354b2012-04-17 17:20:49 +08002753 }
Alex Wu4539b082012-03-01 12:57:46 +08002754 break;
2755 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002757 break;
2758 default:
2759 break;
2760 }
2761}
2762
Alex Wu4539b082012-03-01 12:57:46 +08002763static void
2764shell_map_fullscreen(struct shell_surface *shsurf)
2765{
Alex Wubd3354b2012-04-17 17:20:49 +08002766 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002767}
2768
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002769static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002770set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2771{
2772 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002773 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002774 shsurf->transient.x = x;
2775 shsurf->transient.y = y;
2776 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002777
2778 shell_surface_set_parent(shsurf, NULL);
2779
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002780 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002781 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002782}
2783
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002784static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002785
2786static void
2787destroy_shell_seat(struct wl_listener *listener, void *data)
2788{
2789 struct shell_seat *shseat =
2790 container_of(listener,
2791 struct shell_seat, seat_destroy_listener);
2792 struct shell_surface *shsurf, *prev = NULL;
2793
2794 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002795 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002796 shseat->popup_grab.client = NULL;
2797
2798 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2799 shsurf->popup.shseat = NULL;
2800 if (prev) {
2801 wl_list_init(&prev->popup.grab_link);
2802 }
2803 prev = shsurf;
2804 }
2805 wl_list_init(&prev->popup.grab_link);
2806 }
2807
2808 wl_list_remove(&shseat->seat_destroy_listener.link);
2809 free(shseat);
2810}
2811
2812static struct shell_seat *
2813create_shell_seat(struct weston_seat *seat)
2814{
2815 struct shell_seat *shseat;
2816
2817 shseat = calloc(1, sizeof *shseat);
2818 if (!shseat) {
2819 weston_log("no memory to allocate shell seat\n");
2820 return NULL;
2821 }
2822
2823 shseat->seat = seat;
2824 wl_list_init(&shseat->popup_grab.surfaces_list);
2825
2826 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2827 wl_signal_add(&seat->destroy_signal,
2828 &shseat->seat_destroy_listener);
2829
2830 return shseat;
2831}
2832
2833static struct shell_seat *
2834get_shell_seat(struct weston_seat *seat)
2835{
2836 struct wl_listener *listener;
2837
2838 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2839 if (listener == NULL)
2840 return create_shell_seat(seat);
2841
2842 return container_of(listener,
2843 struct shell_seat, seat_destroy_listener);
2844}
2845
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002846static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002847popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002848{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002849 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002850 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002851 struct shell_seat *shseat =
2852 container_of(grab, struct shell_seat, popup_grab.grab);
2853 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002854 wl_fixed_t sx, sy;
2855
Jason Ekstranda7af7042013-10-12 22:38:11 -05002856 view = weston_compositor_pick_view(pointer->seat->compositor,
2857 pointer->x, pointer->y,
2858 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002859
Jason Ekstranda7af7042013-10-12 22:38:11 -05002860 if (view && view->surface->resource &&
2861 wl_resource_get_client(view->surface->resource) == client) {
2862 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002863 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002864 weston_pointer_set_focus(pointer, NULL,
2865 wl_fixed_from_int(0),
2866 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002867 }
2868}
2869
2870static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002871popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2872 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002873{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002874 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002875 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002876 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002877
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002878 weston_pointer_move(pointer, x, y);
2879
Neil Roberts96d790e2013-09-19 17:32:00 +01002880 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002881 weston_view_from_global_fixed(pointer->focus,
2882 pointer->x, pointer->y,
2883 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002884 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002885 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002886}
2887
2888static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002889popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002890 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002891{
2892 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002893 struct shell_seat *shseat =
2894 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002895 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002896 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002897 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002898 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002899
Neil Roberts96d790e2013-09-19 17:32:00 +01002900 resource_list = &grab->pointer->focus_resource_list;
2901 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002902 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002903 wl_resource_for_each(resource, resource_list) {
2904 wl_pointer_send_button(resource, serial,
2905 time, button, state);
2906 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002907 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002908 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002909 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002910 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002911 }
2912
Daniel Stone4dbadb12012-05-30 16:31:51 +01002913 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002914 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002915}
2916
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002917static void
2918popup_grab_cancel(struct weston_pointer_grab *grab)
2919{
2920 popup_grab_end(grab->pointer);
2921}
2922
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002923static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002924 popup_grab_focus,
2925 popup_grab_motion,
2926 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002927 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002928};
2929
2930static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002931shell_surface_send_popup_done(struct shell_surface *shsurf)
2932{
2933 if (shell_surface_is_wl_shell_surface(shsurf))
2934 wl_shell_surface_send_popup_done(shsurf->resource);
2935 else if (shell_surface_is_xdg_popup(shsurf))
2936 xdg_popup_send_popup_done(shsurf->resource,
2937 shsurf->popup.serial);
2938}
2939
2940static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002941popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002942{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002943 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002944 struct shell_seat *shseat =
2945 container_of(grab, struct shell_seat, popup_grab.grab);
2946 struct shell_surface *shsurf;
2947 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002948
2949 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002950 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002951 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002952 shseat->popup_grab.grab.interface = NULL;
2953 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002954 /* Send the popup_done event to all the popups open */
2955 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002956 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002957 shsurf->popup.shseat = NULL;
2958 if (prev) {
2959 wl_list_init(&prev->popup.grab_link);
2960 }
2961 prev = shsurf;
2962 }
2963 wl_list_init(&prev->popup.grab_link);
2964 wl_list_init(&shseat->popup_grab.surfaces_list);
2965 }
2966}
2967
2968static void
2969add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2970{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002971 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002972
2973 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002974 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002975 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002976 /* We must make sure here that this popup was opened after
2977 * a mouse press, and not just by moving around with other
2978 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002979 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002980 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002981
Rob Bradforddfe31052013-06-26 19:49:11 +01002982 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002983 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002984 } else {
2985 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002986 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002987}
2988
2989static void
2990remove_popup_grab(struct shell_surface *shsurf)
2991{
2992 struct shell_seat *shseat = shsurf->popup.shseat;
2993
2994 wl_list_remove(&shsurf->popup.grab_link);
2995 wl_list_init(&shsurf->popup.grab_link);
2996 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002997 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002998 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002999 }
3000}
3001
3002static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003003shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003004{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003005 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003006 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003007
Jason Ekstranda7af7042013-10-12 22:38:11 -05003008 shsurf->surface->output = parent_view->output;
3009 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003010
Jason Ekstranda7af7042013-10-12 22:38:11 -05003011 weston_view_set_transform_parent(shsurf->view, parent_view);
3012 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3013 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003014
Kristian Høgsberge3148752013-05-06 23:19:49 -04003015 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003016 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003017 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003018 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003019 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003020 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003021}
3022
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003023static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003024 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003025 shell_surface_move,
3026 shell_surface_resize,
3027 shell_surface_set_toplevel,
3028 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003029 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003030 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003031 shell_surface_set_maximized,
3032 shell_surface_set_title,
3033 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003034};
3035
3036static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003037destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003038{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003039 struct shell_surface *child, *next;
3040
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003041 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3042
Giulio Camuffo5085a752013-03-25 21:42:45 +01003043 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3044 remove_popup_grab(shsurf);
3045 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003046
Alex Wubd3354b2012-04-17 17:20:49 +08003047 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003048 shell_surface_is_top_fullscreen(shsurf))
3049 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003050
Jason Ekstranda7af7042013-10-12 22:38:11 -05003051 if (shsurf->fullscreen.black_view)
3052 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003053
Alex Wubd3354b2012-04-17 17:20:49 +08003054 /* As destroy_resource() use wl_list_for_each_safe(),
3055 * we can always remove the listener.
3056 */
3057 wl_list_remove(&shsurf->surface_destroy_listener.link);
3058 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06003059 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07003060 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003061
Jason Ekstranda7af7042013-10-12 22:38:11 -05003062 weston_view_destroy(shsurf->view);
3063
Philip Withnall648a4dd2013-11-25 18:01:44 +00003064 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003065 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3066 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003067
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003068 wl_list_remove(&shsurf->link);
3069 free(shsurf);
3070}
3071
3072static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003073shell_destroy_shell_surface(struct wl_resource *resource)
3074{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003075 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003076
3077 destroy_shell_surface(shsurf);
3078}
3079
3080static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003081shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003082{
3083 struct shell_surface *shsurf = container_of(listener,
3084 struct shell_surface,
3085 surface_destroy_listener);
3086
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003087 if (shsurf->resource)
3088 wl_resource_destroy(shsurf->resource);
3089 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003090 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003091}
3092
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003093static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003094shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003095
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003096struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003097get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003098{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003099 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003100 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003101 else
3102 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003103}
3104
Rafael Antognollie2a34552013-12-03 15:35:45 -02003105static struct shell_surface *
3106create_common_surface(void *shell, struct weston_surface *surface,
3107 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003108{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003109 struct shell_surface *shsurf;
3110
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003111 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003112 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003113 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003114 }
3115
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003116 shsurf = calloc(1, sizeof *shsurf);
3117 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003118 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003119 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003120 }
3121
Jason Ekstranda7af7042013-10-12 22:38:11 -05003122 shsurf->view = weston_view_create(surface);
3123 if (!shsurf->view) {
3124 weston_log("no memory to allocate shell surface\n");
3125 free(shsurf);
3126 return NULL;
3127 }
3128
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003129 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003130 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003131
Tiago Vignattibc052c92012-04-19 16:18:18 +03003132 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003133 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003134 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003135 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003136 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003137 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003138 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3139 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003140 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003141 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003142 wl_list_init(&shsurf->fullscreen.transform.link);
3143
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003144 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003145 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003146 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003147 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003148
3149 /* init link so its safe to always remove it in destroy_shell_surface */
3150 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003151 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003152
Pekka Paalanen460099f2012-01-20 16:48:25 +02003153 /* empty when not in use */
3154 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003155 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003156
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003157 wl_list_init(&shsurf->workspace_transform.link);
3158
Philip Withnall648a4dd2013-11-25 18:01:44 +00003159 wl_list_init(&shsurf->children_link);
3160 wl_list_init(&shsurf->children_list);
3161 shsurf->parent = NULL;
3162
Pekka Paalanen98262232011-12-01 10:42:22 +02003163 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003164
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003165 shsurf->client = client;
3166
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003167 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003168}
3169
Rafael Antognollie2a34552013-12-03 15:35:45 -02003170static struct shell_surface *
3171create_shell_surface(void *shell, struct weston_surface *surface,
3172 const struct weston_shell_client *client)
3173{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003174 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003175}
3176
Jason Ekstranda7af7042013-10-12 22:38:11 -05003177static struct weston_view *
3178get_primary_view(void *shell, struct shell_surface *shsurf)
3179{
3180 return shsurf->view;
3181}
3182
Tiago Vignattibc052c92012-04-19 16:18:18 +03003183static void
3184shell_get_shell_surface(struct wl_client *client,
3185 struct wl_resource *resource,
3186 uint32_t id,
3187 struct wl_resource *surface_resource)
3188{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003189 struct weston_surface *surface =
3190 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003191 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003192 struct shell_surface *shsurf;
3193
3194 if (get_shell_surface(surface)) {
3195 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003196 WL_DISPLAY_ERROR_INVALID_OBJECT,
3197 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003198 return;
3199 }
3200
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003201 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003202 if (!shsurf) {
3203 wl_resource_post_error(surface_resource,
3204 WL_DISPLAY_ERROR_INVALID_OBJECT,
3205 "surface->configure already set");
3206 return;
3207 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003208
Jason Ekstranda85118c2013-06-27 20:17:02 -05003209 shsurf->resource =
3210 wl_resource_create(client,
3211 &wl_shell_surface_interface, 1, id);
3212 wl_resource_set_implementation(shsurf->resource,
3213 &shell_surface_implementation,
3214 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003215}
3216
Rafael Antognollie2a34552013-12-03 15:35:45 -02003217static bool
3218shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3219{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003220 /* A shell surface without a resource is created from xwayland
3221 * and is considered a wl_shell surface for now. */
3222
3223 return shsurf->resource == NULL ||
3224 wl_resource_instance_of(shsurf->resource,
3225 &wl_shell_surface_interface,
3226 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003227}
3228
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003229static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003230 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003231};
3232
Rafael Antognollie2a34552013-12-03 15:35:45 -02003233/****************************
3234 * xdg-shell implementation */
3235
3236static void
3237xdg_surface_destroy(struct wl_client *client,
3238 struct wl_resource *resource)
3239{
3240 wl_resource_destroy(resource);
3241}
3242
3243static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003244xdg_surface_set_margin(struct wl_client *client,
3245 struct wl_resource *resource,
3246 int32_t left,
3247 int32_t right,
3248 int32_t top,
3249 int32_t bottom)
3250{
3251 /* Do nothing, Weston doesn't try to constrain or place
3252 * surfaces in any special manner... */
3253}
3254
3255static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003256xdg_surface_set_app_id(struct wl_client *client,
3257 struct wl_resource *resource,
3258 const char *app_id)
3259{
3260 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3261
3262 free(shsurf->class);
3263 shsurf->class = strdup(app_id);
3264}
3265
3266static void
3267xdg_surface_set_title(struct wl_client *client,
3268 struct wl_resource *resource, const char *title)
3269{
3270 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3271
3272 set_title(shsurf, title);
3273}
3274
3275static void
3276xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3277 struct wl_resource *seat_resource, uint32_t serial)
3278{
3279 common_surface_move(resource, seat_resource, serial);
3280}
3281
3282static void
3283xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3284 struct wl_resource *seat_resource, uint32_t serial,
3285 uint32_t edges)
3286{
3287 common_surface_resize(resource, seat_resource, serial, edges);
3288}
3289
3290static void
3291xdg_surface_set_output(struct wl_client *client,
3292 struct wl_resource *resource,
3293 struct wl_resource *output_resource)
3294{
3295 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3296 struct weston_output *output;
3297
3298 if (output_resource)
3299 output = wl_resource_get_user_data(output_resource);
3300 else
3301 output = NULL;
3302
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003303 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003304}
3305
3306static void
3307xdg_surface_set_fullscreen(struct wl_client *client,
3308 struct wl_resource *resource)
3309{
3310 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3311
3312 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3313 return;
3314
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003315 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003316 set_fullscreen(shsurf,
3317 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003318 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003319}
3320
3321static void
3322xdg_surface_unset_fullscreen(struct wl_client *client,
3323 struct wl_resource *resource)
3324{
3325 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003326 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003327
3328 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3329 return;
3330
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003331 if (!shsurf->next_state.fullscreen)
3332 return;
3333
Rafael Antognollie2a34552013-12-03 15:35:45 -02003334 shsurf->next_state.fullscreen = false;
3335 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003336
3337 if (shsurf->saved_size_valid) {
3338 width = shsurf->saved_width;
3339 height = shsurf->saved_height;
3340 shsurf->saved_size_valid = false;
3341 } else {
3342 width = shsurf->surface->width;
3343 height = shsurf->surface->height;
3344 }
3345
3346 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003347}
3348
3349static void
3350xdg_surface_set_maximized(struct wl_client *client,
3351 struct wl_resource *resource)
3352{
3353 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3354
3355 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3356 return;
3357
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003358 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003359 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003360}
3361
3362static void
3363xdg_surface_unset_maximized(struct wl_client *client,
3364 struct wl_resource *resource)
3365{
3366 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003367 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003368
3369 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3370 return;
3371
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003372 if (!shsurf->next_state.maximized)
3373 return;
3374
Rafael Antognollie2a34552013-12-03 15:35:45 -02003375 shsurf->next_state.maximized = false;
3376 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003377
3378 if (shsurf->saved_size_valid) {
3379 width = shsurf->saved_width;
3380 height = shsurf->saved_height;
3381 shsurf->saved_size_valid = false;
3382 } else {
3383 width = shsurf->surface->width;
3384 height = shsurf->surface->height;
3385 }
3386
3387 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003388}
3389
3390static const struct xdg_surface_interface xdg_surface_implementation = {
3391 xdg_surface_destroy,
3392 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003393 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003394 xdg_surface_set_title,
3395 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003396 xdg_surface_move,
3397 xdg_surface_resize,
3398 xdg_surface_set_output,
3399 xdg_surface_set_fullscreen,
3400 xdg_surface_unset_fullscreen,
3401 xdg_surface_set_maximized,
3402 xdg_surface_unset_maximized,
3403 NULL /* set_minimized */
3404};
3405
3406static void
3407xdg_send_configure(struct weston_surface *surface,
3408 uint32_t edges, int32_t width, int32_t height)
3409{
3410 struct shell_surface *shsurf = get_shell_surface(surface);
3411
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003412 assert(shsurf);
3413
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003414 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003415}
3416
3417static const struct weston_shell_client xdg_client = {
3418 xdg_send_configure
3419};
3420
3421static void
3422xdg_use_unstable_version(struct wl_client *client,
3423 struct wl_resource *resource,
3424 int32_t version)
3425{
3426 if (version > 1) {
3427 wl_resource_post_error(resource,
3428 1,
3429 "xdg-shell:: version not implemented yet.");
3430 return;
3431 }
3432}
3433
3434static struct shell_surface *
3435create_xdg_surface(void *shell, struct weston_surface *surface,
3436 const struct weston_shell_client *client)
3437{
3438 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003439
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003440 shsurf = create_common_surface(shell, surface, client);
3441 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003442
3443 return shsurf;
3444}
3445
3446static void
3447xdg_get_xdg_surface(struct wl_client *client,
3448 struct wl_resource *resource,
3449 uint32_t id,
3450 struct wl_resource *surface_resource)
3451{
3452 struct weston_surface *surface =
3453 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003454 struct shell_client *sc = wl_resource_get_user_data(resource);
3455 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003456 struct shell_surface *shsurf;
3457
3458 if (get_shell_surface(surface)) {
3459 wl_resource_post_error(surface_resource,
3460 WL_DISPLAY_ERROR_INVALID_OBJECT,
3461 "desktop_shell::get_shell_surface already requested");
3462 return;
3463 }
3464
3465 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3466 if (!shsurf) {
3467 wl_resource_post_error(surface_resource,
3468 WL_DISPLAY_ERROR_INVALID_OBJECT,
3469 "surface->configure already set");
3470 return;
3471 }
3472
3473 shsurf->resource =
3474 wl_resource_create(client,
3475 &xdg_surface_interface, 1, id);
3476 wl_resource_set_implementation(shsurf->resource,
3477 &xdg_surface_implementation,
3478 shsurf, shell_destroy_shell_surface);
3479}
3480
3481static bool
3482shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3483{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003484 return shsurf->resource &&
3485 wl_resource_instance_of(shsurf->resource,
3486 &xdg_surface_interface,
3487 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003488}
3489
3490/* xdg-popup implementation */
3491
3492static void
3493xdg_popup_destroy(struct wl_client *client,
3494 struct wl_resource *resource)
3495{
3496 wl_resource_destroy(resource);
3497}
3498
Rafael Antognollie2a34552013-12-03 15:35:45 -02003499static const struct xdg_popup_interface xdg_popup_implementation = {
3500 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003501};
3502
3503static void
3504xdg_popup_send_configure(struct weston_surface *surface,
3505 uint32_t edges, int32_t width, int32_t height)
3506{
3507}
3508
3509static const struct weston_shell_client xdg_popup_client = {
3510 xdg_popup_send_configure
3511};
3512
3513static struct shell_surface *
3514create_xdg_popup(void *shell, struct weston_surface *surface,
3515 const struct weston_shell_client *client,
3516 struct weston_surface *parent,
3517 struct shell_seat *seat,
3518 uint32_t serial,
3519 int32_t x, int32_t y)
3520{
3521 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003522
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003523 shsurf = create_common_surface(shell, surface, client);
3524 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003525 shsurf->popup.shseat = seat;
3526 shsurf->popup.serial = serial;
3527 shsurf->popup.x = x;
3528 shsurf->popup.y = y;
3529 shell_surface_set_parent(shsurf, parent);
3530
3531 return shsurf;
3532}
3533
3534static void
3535xdg_get_xdg_popup(struct wl_client *client,
3536 struct wl_resource *resource,
3537 uint32_t id,
3538 struct wl_resource *surface_resource,
3539 struct wl_resource *parent_resource,
3540 struct wl_resource *seat_resource,
3541 uint32_t serial,
3542 int32_t x, int32_t y, uint32_t flags)
3543{
3544 struct weston_surface *surface =
3545 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003546 struct shell_client *sc = wl_resource_get_user_data(resource);
3547 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003548 struct shell_surface *shsurf;
3549 struct weston_surface *parent;
3550 struct shell_seat *seat;
3551
3552 if (get_shell_surface(surface)) {
3553 wl_resource_post_error(surface_resource,
3554 WL_DISPLAY_ERROR_INVALID_OBJECT,
3555 "desktop_shell::get_shell_surface already requested");
3556 return;
3557 }
3558
3559 if (!parent_resource) {
3560 wl_resource_post_error(surface_resource,
3561 WL_DISPLAY_ERROR_INVALID_OBJECT,
3562 "xdg_shell::get_xdg_popup requires a parent shell surface");
3563 }
3564
3565 parent = wl_resource_get_user_data(parent_resource);
3566 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3567
3568 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3569 parent, seat, serial, x, y);
3570 if (!shsurf) {
3571 wl_resource_post_error(surface_resource,
3572 WL_DISPLAY_ERROR_INVALID_OBJECT,
3573 "surface->configure already set");
3574 return;
3575 }
3576
3577 shsurf->resource =
3578 wl_resource_create(client,
3579 &xdg_popup_interface, 1, id);
3580 wl_resource_set_implementation(shsurf->resource,
3581 &xdg_popup_implementation,
3582 shsurf, shell_destroy_shell_surface);
3583}
3584
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003585static void
3586xdg_pong(struct wl_client *client,
3587 struct wl_resource *resource, uint32_t serial)
3588{
3589 struct shell_client *sc = wl_resource_get_user_data(resource);
3590 struct weston_compositor *ec = sc->shell->compositor;
3591
3592 if (sc->ping_serial != serial)
3593 return;
3594
3595 sc->unresponsive = 0;
3596 end_busy_cursor(ec, client);
3597
3598 if (sc->ping_timer) {
3599 wl_event_source_remove(sc->ping_timer);
3600 sc->ping_timer = NULL;
3601 }
3602}
3603
Rafael Antognollie2a34552013-12-03 15:35:45 -02003604static bool
3605shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3606{
3607 return wl_resource_instance_of(shsurf->resource,
3608 &xdg_popup_interface,
3609 &xdg_popup_implementation);
3610}
3611
3612static const struct xdg_shell_interface xdg_implementation = {
3613 xdg_use_unstable_version,
3614 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003615 xdg_get_xdg_popup,
3616 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003617};
3618
3619static int
3620xdg_shell_unversioned_dispatch(const void *implementation,
3621 void *_target, uint32_t opcode,
3622 const struct wl_message *message,
3623 union wl_argument *args)
3624{
3625 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003626 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003627
3628 if (opcode != 0) {
3629 wl_resource_post_error(resource,
3630 WL_DISPLAY_ERROR_INVALID_OBJECT,
3631 "must call use_unstable_version first");
3632 return 0;
3633 }
3634
Kristian Høgsberg239902b2014-02-11 13:50:08 -08003635#define XDG_SERVER_VERSION 2
Rafael Antognollie2a34552013-12-03 15:35:45 -02003636
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003637 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3638 "shell implementation doesn't match protocol version");
3639
Rafael Antognollie2a34552013-12-03 15:35:45 -02003640 if (args[0].i != XDG_SERVER_VERSION) {
3641 wl_resource_post_error(resource,
3642 WL_DISPLAY_ERROR_INVALID_OBJECT,
3643 "incompatible version, server is %d "
3644 "client wants %d",
3645 XDG_SERVER_VERSION, args[0].i);
3646 return 0;
3647 }
3648
3649 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003650 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003651
3652 return 1;
3653}
3654
3655/* end of xdg-shell implementation */
3656/***********************************/
3657
Kristian Høgsberg07937562011-04-12 17:25:42 -04003658static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003659shell_fade(struct desktop_shell *shell, enum fade_type type);
3660
3661static int
3662screensaver_timeout(void *data)
3663{
3664 struct desktop_shell *shell = data;
3665
3666 shell_fade(shell, FADE_OUT);
3667
3668 return 1;
3669}
3670
3671static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003672handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003673{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003674 struct desktop_shell *shell =
3675 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003676
Pekka Paalanen18027e52011-12-02 16:31:49 +02003677 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003678
3679 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003680 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003681}
3682
3683static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003684launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003685{
3686 if (shell->screensaver.binding)
3687 return;
3688
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003689 if (!shell->screensaver.path) {
3690 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003691 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003692 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003693
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003694 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003695 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003696 return;
3697 }
3698
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003699 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003700 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003701 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003702 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003703}
3704
3705static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003706terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003707{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003708 if (shell->screensaver.process.pid == 0)
3709 return;
3710
3711 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003712}
3713
3714static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003715configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003716{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003717 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003718
Jason Ekstranda7af7042013-10-12 22:38:11 -05003719 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3720 if (v->output == ev->output && v != ev) {
3721 weston_view_unmap(v);
3722 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003723 }
3724 }
3725
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003726 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003727
Jason Ekstranda7af7042013-10-12 22:38:11 -05003728 if (wl_list_empty(&ev->layer_link)) {
3729 wl_list_insert(&layer->view_list, &ev->layer_link);
3730 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003731 }
3732}
3733
3734static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003735background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003736{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003737 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003738 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003739
Jason Ekstranda7af7042013-10-12 22:38:11 -05003740 view = container_of(es->views.next, struct weston_view, surface_link);
3741
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003742 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003743}
3744
3745static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003746desktop_shell_set_background(struct wl_client *client,
3747 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003748 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003749 struct wl_resource *surface_resource)
3750{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003751 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003752 struct weston_surface *surface =
3753 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003754 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003755
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003756 if (surface->configure) {
3757 wl_resource_post_error(surface_resource,
3758 WL_DISPLAY_ERROR_INVALID_OBJECT,
3759 "surface role already assigned");
3760 return;
3761 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003762
Jason Ekstranda7af7042013-10-12 22:38:11 -05003763 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3764 weston_view_destroy(view);
3765 view = weston_view_create(surface);
3766
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003767 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003768 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003769 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003770 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003771 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003772 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003773 surface->output->width,
3774 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003775}
3776
3777static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003778panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003779{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003780 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003781 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003782
Jason Ekstranda7af7042013-10-12 22:38:11 -05003783 view = container_of(es->views.next, struct weston_view, surface_link);
3784
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003785 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003786}
3787
3788static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003789desktop_shell_set_panel(struct wl_client *client,
3790 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003791 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003792 struct wl_resource *surface_resource)
3793{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003794 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003795 struct weston_surface *surface =
3796 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003797 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003798
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003799 if (surface->configure) {
3800 wl_resource_post_error(surface_resource,
3801 WL_DISPLAY_ERROR_INVALID_OBJECT,
3802 "surface role already assigned");
3803 return;
3804 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003805
Jason Ekstranda7af7042013-10-12 22:38:11 -05003806 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3807 weston_view_destroy(view);
3808 view = weston_view_create(surface);
3809
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003810 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003811 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003812 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003813 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003814 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003815 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003816 surface->output->width,
3817 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003818}
3819
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003820static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003821lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003822{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003823 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003824 struct weston_view *view;
3825
3826 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003827
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003828 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003829 return;
3830
Jason Ekstranda7af7042013-10-12 22:38:11 -05003831 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003832
3833 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003834 wl_list_insert(&shell->lock_layer.view_list,
3835 &view->layer_link);
3836 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003837 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003838 }
3839}
3840
3841static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003842handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003843{
Tiago Vignattibe143262012-04-16 17:31:41 +03003844 struct desktop_shell *shell =
3845 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003846
Martin Minarik6d118362012-06-07 18:01:59 +02003847 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003848 shell->lock_surface = NULL;
3849}
3850
3851static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003852desktop_shell_set_lock_surface(struct wl_client *client,
3853 struct wl_resource *resource,
3854 struct wl_resource *surface_resource)
3855{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003856 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003857 struct weston_surface *surface =
3858 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003859
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003860 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003861
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003862 if (!shell->locked)
3863 return;
3864
Pekka Paalanen98262232011-12-01 10:42:22 +02003865 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003866
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003867 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003868 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003869 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003870
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003871 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003872 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003873 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003874}
3875
3876static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003877resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003878{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003879 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003880
Pekka Paalanen77346a62011-11-30 16:26:35 +02003881 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003882
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003883 wl_list_remove(&shell->lock_layer.link);
3884 wl_list_insert(&shell->compositor->cursor_layer.link,
3885 &shell->fullscreen_layer.link);
3886 wl_list_insert(&shell->fullscreen_layer.link,
3887 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003888 if (shell->showing_input_panels) {
3889 wl_list_insert(&shell->panel_layer.link,
3890 &shell->input_panel_layer.link);
3891 wl_list_insert(&shell->input_panel_layer.link,
3892 &ws->layer.link);
3893 } else {
3894 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3895 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003896
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003897 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003898
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003899 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003900 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003901 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003902}
3903
3904static void
3905desktop_shell_unlock(struct wl_client *client,
3906 struct wl_resource *resource)
3907{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003908 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003909
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003910 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003911
3912 if (shell->locked)
3913 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003914}
3915
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003916static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003917desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003918 struct wl_resource *resource,
3919 struct wl_resource *surface_resource)
3920{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003921 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003922
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003923 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003924 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003925}
3926
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003927static void
3928desktop_shell_desktop_ready(struct wl_client *client,
3929 struct wl_resource *resource)
3930{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003931 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003932
3933 shell_fade_startup(shell);
3934}
3935
Kristian Høgsberg75840622011-09-06 13:48:16 -04003936static const struct desktop_shell_interface desktop_shell_implementation = {
3937 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003938 desktop_shell_set_panel,
3939 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003940 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003941 desktop_shell_set_grab_surface,
3942 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003943};
3944
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003945static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003946get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003947{
3948 struct shell_surface *shsurf;
3949
3950 shsurf = get_shell_surface(surface);
3951 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003952 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003953 return shsurf->type;
3954}
3955
Kristian Høgsberg75840622011-09-06 13:48:16 -04003956static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003957move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003958{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003959 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003960 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003961 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003962
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003963 if (seat->pointer->focus == NULL)
3964 return;
3965
3966 focus = seat->pointer->focus->surface;
3967
Pekka Paalanen01388e22013-04-25 13:57:44 +03003968 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003969 if (surface == NULL)
3970 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003971
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003972 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003973 if (shsurf == NULL || shsurf->state.fullscreen ||
3974 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003975 return;
3976
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003977 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003978}
3979
3980static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003981maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3982{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003983 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003984 struct weston_surface *surface;
3985 struct shell_surface *shsurf;
3986
3987 surface = weston_surface_get_main_surface(focus);
3988 if (surface == NULL)
3989 return;
3990
3991 shsurf = get_shell_surface(surface);
3992 if (shsurf == NULL)
3993 return;
3994
3995 if (!shell_surface_is_xdg_surface(shsurf))
3996 return;
3997
3998 if (shsurf->state.maximized)
3999 xdg_surface_send_request_unset_maximized(shsurf->resource);
4000 else
4001 xdg_surface_send_request_set_maximized(shsurf->resource);
4002}
4003
4004static void
4005fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4006{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004007 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004008 struct weston_surface *surface;
4009 struct shell_surface *shsurf;
4010
4011 surface = weston_surface_get_main_surface(focus);
4012 if (surface == NULL)
4013 return;
4014
4015 shsurf = get_shell_surface(surface);
4016 if (shsurf == NULL)
4017 return;
4018
4019 if (!shell_surface_is_xdg_surface(shsurf))
4020 return;
4021
4022 if (shsurf->state.fullscreen)
4023 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
4024 else
4025 xdg_surface_send_request_set_fullscreen(shsurf->resource);
4026}
4027
4028static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004029touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4030{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004031 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004032 struct weston_surface *surface;
4033 struct shell_surface *shsurf;
4034
4035 surface = weston_surface_get_main_surface(focus);
4036 if (surface == NULL)
4037 return;
4038
4039 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004040 if (shsurf == NULL || shsurf->state.fullscreen ||
4041 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004042 return;
4043
4044 surface_touch_move(shsurf, (struct weston_seat *) seat);
4045}
4046
4047static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004048resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004049{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004050 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004051 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004052 uint32_t edges = 0;
4053 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004054 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004055
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004056 if (seat->pointer->focus == NULL)
4057 return;
4058
4059 focus = seat->pointer->focus->surface;
4060
Pekka Paalanen01388e22013-04-25 13:57:44 +03004061 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004062 if (surface == NULL)
4063 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004064
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004065 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004066 if (shsurf == NULL || shsurf->state.fullscreen ||
4067 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004068 return;
4069
Jason Ekstranda7af7042013-10-12 22:38:11 -05004070 weston_view_from_global(shsurf->view,
4071 wl_fixed_to_int(seat->pointer->grab_x),
4072 wl_fixed_to_int(seat->pointer->grab_y),
4073 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004074
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004075 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004076 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004077 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004078 edges |= 0;
4079 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004080 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004081
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004082 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004083 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004084 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004085 edges |= 0;
4086 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004087 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004088
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004089 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004090}
4091
4092static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004093surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004094 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004095{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004096 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004097 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004098 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004099 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004100
Pekka Paalanen01388e22013-04-25 13:57:44 +03004101 /* XXX: broken for windows containing sub-surfaces */
4102 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004103 if (surface == NULL)
4104 return;
4105
4106 shsurf = get_shell_surface(surface);
4107 if (!shsurf)
4108 return;
4109
Jason Ekstranda7af7042013-10-12 22:38:11 -05004110 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004111
Jason Ekstranda7af7042013-10-12 22:38:11 -05004112 if (shsurf->view->alpha > 1.0)
4113 shsurf->view->alpha = 1.0;
4114 if (shsurf->view->alpha < step)
4115 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004116
Jason Ekstranda7af7042013-10-12 22:38:11 -05004117 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004118 weston_surface_damage(surface);
4119}
4120
4121static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004122do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004123 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004124{
Daniel Stone37816df2012-05-16 18:45:18 +01004125 struct weston_seat *ws = (struct weston_seat *) seat;
4126 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004127 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004128 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004129
4130 wl_list_for_each(output, &compositor->output_list, link) {
4131 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004132 wl_fixed_to_double(seat->pointer->x),
4133 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004134 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004135 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004136 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004137 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004138 increment = -output->zoom.increment;
4139 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004140 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004141 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004142 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004143 else
4144 increment = 0;
4145
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004146 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004147
Scott Moreaue6603982012-06-11 13:07:51 -06004148 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004149 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004150 else if (output->zoom.level > output->zoom.max_level)
4151 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004152 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004153 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004154 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004155
Scott Moreaue6603982012-06-11 13:07:51 -06004156 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004157
Jason Ekstranda7af7042013-10-12 22:38:11 -05004158 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004159 }
4160 }
4161}
4162
Scott Moreauccbf29d2012-02-22 14:21:41 -07004163static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004164zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004165 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004166{
4167 do_zoom(seat, time, 0, axis, value);
4168}
4169
4170static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004171zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004172 void *data)
4173{
4174 do_zoom(seat, time, key, 0, 0);
4175}
4176
4177static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004178terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004179 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004180{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004181 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004182
Daniel Stone325fc2d2012-05-30 16:31:58 +01004183 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004184}
4185
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004186static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004187rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4188 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004189{
4190 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004191 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004192 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004193 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004194 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004195
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004196 weston_pointer_move(pointer, x, y);
4197
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004198 if (!shsurf)
4199 return;
4200
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004201 cx = 0.5f * shsurf->surface->width;
4202 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004203
Daniel Stone37816df2012-05-16 18:45:18 +01004204 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4205 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004206 r = sqrtf(dx * dx + dy * dy);
4207
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004208 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004209 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004210
4211 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004212 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004213 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004214
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004215 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004216 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004217
4218 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004219 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004220 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004221 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004222 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004223
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004224 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004225 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004226 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004227 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004228 wl_list_init(&shsurf->rotation.transform.link);
4229 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004230 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004231 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004232
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004233 /* We need to adjust the position of the surface
4234 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004235 cposx = shsurf->view->geometry.x + cx;
4236 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004237 dposx = rotate->center.x - cposx;
4238 dposy = rotate->center.y - cposy;
4239 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004240 weston_view_set_position(shsurf->view,
4241 shsurf->view->geometry.x + dposx,
4242 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004243 }
4244
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004245 /* Repaint implies weston_surface_update_transform(), which
4246 * lazily applies the damage due to rotation update.
4247 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004248 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004249}
4250
4251static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004252rotate_grab_button(struct weston_pointer_grab *grab,
4253 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004254{
4255 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004256 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004257 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004258 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004259 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004260
Daniel Stone4dbadb12012-05-30 16:31:51 +01004261 if (pointer->button_count == 0 &&
4262 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004263 if (shsurf)
4264 weston_matrix_multiply(&shsurf->rotation.rotation,
4265 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004266 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004267 free(rotate);
4268 }
4269}
4270
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004271static void
4272rotate_grab_cancel(struct weston_pointer_grab *grab)
4273{
4274 struct rotate_grab *rotate =
4275 container_of(grab, struct rotate_grab, base.grab);
4276
4277 shell_grab_end(&rotate->base);
4278 free(rotate);
4279}
4280
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004281static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004282 noop_grab_focus,
4283 rotate_grab_motion,
4284 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004285 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004286};
4287
4288static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004289surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004290{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004291 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004292 float dx, dy;
4293 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004294
Pekka Paalanen460099f2012-01-20 16:48:25 +02004295 rotate = malloc(sizeof *rotate);
4296 if (!rotate)
4297 return;
4298
Jason Ekstranda7af7042013-10-12 22:38:11 -05004299 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004300 surface->surface->width * 0.5f,
4301 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004302 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004303
Daniel Stone37816df2012-05-16 18:45:18 +01004304 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4305 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004306 r = sqrtf(dx * dx + dy * dy);
4307 if (r > 20.0f) {
4308 struct weston_matrix inverse;
4309
4310 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004311 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004312 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004313
4314 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004315 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004316 } else {
4317 weston_matrix_init(&surface->rotation.rotation);
4318 weston_matrix_init(&rotate->rotation);
4319 }
4320
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004321 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4322 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004323}
4324
4325static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004326rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004327 void *data)
4328{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004329 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004330 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004331 struct shell_surface *surface;
4332
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004333 if (seat->pointer->focus == NULL)
4334 return;
4335
4336 focus = seat->pointer->focus->surface;
4337
Pekka Paalanen01388e22013-04-25 13:57:44 +03004338 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004339 if (base_surface == NULL)
4340 return;
4341
4342 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004343 if (surface == NULL || surface->state.fullscreen ||
4344 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004345 return;
4346
4347 surface_rotate(surface, seat);
4348}
4349
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004350/* Move all fullscreen layers down to the current workspace in a non-reversible
4351 * manner. This should be used when implementing shell-wide overlays, such as
4352 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004353void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004354lower_fullscreen_layer(struct desktop_shell *shell)
4355{
4356 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004357 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004358
4359 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004360 wl_list_for_each_reverse_safe(view, prev,
4361 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004362 layer_link) {
4363 wl_list_remove(&view->layer_link);
4364 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4365 weston_view_damage_below(view);
4366 weston_surface_damage(view->surface);
4367 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004368}
4369
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004370void
Tiago Vignattibe143262012-04-16 17:31:41 +03004371activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004372 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004373{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004374 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004375 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004376 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004377 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004378 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004379
Pekka Paalanen01388e22013-04-25 13:57:44 +03004380 main_surface = weston_surface_get_main_surface(es);
4381
Daniel Stone37816df2012-05-16 18:45:18 +01004382 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004383
Jonas Ådahl8538b222012-08-29 22:13:03 +02004384 state = ensure_focus_state(shell, seat);
4385 if (state == NULL)
4386 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004387
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004388 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004389 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004390
Rafael Antognolli03b16592013-12-03 15:35:42 -02004391 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004392 assert(shsurf);
4393
Rafael Antognolli03b16592013-12-03 15:35:42 -02004394 if (shsurf->state.fullscreen)
4395 shell_configure_fullscreen(shsurf);
4396 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004397 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004398
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004399 /* Update the surface’s layer. This brings it to the top of the stacking
4400 * order as appropriate. */
4401 shell_surface_update_layer(shsurf);
4402
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004403 if (shell->focus_animation_type != ANIMATION_NONE) {
4404 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004405 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004406 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004407}
4408
Alex Wu21858432012-04-01 20:13:08 +08004409/* no-op func for checking black surface */
4410static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004411black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004412{
4413}
4414
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004415static bool
Alex Wu21858432012-04-01 20:13:08 +08004416is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4417{
4418 if (es->configure == black_surface_configure) {
4419 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004420 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004421 return true;
4422 }
4423 return false;
4424}
4425
Kristian Høgsberg75840622011-09-06 13:48:16 -04004426static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004427activate_binding(struct weston_seat *seat,
4428 struct desktop_shell *shell,
4429 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004430{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004431 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004432
Alex Wu9c35e6b2012-03-05 14:13:13 +08004433 if (!focus)
4434 return;
4435
Pekka Paalanen01388e22013-04-25 13:57:44 +03004436 if (is_black_surface(focus, &main_surface))
4437 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004438
Pekka Paalanen01388e22013-04-25 13:57:44 +03004439 main_surface = weston_surface_get_main_surface(focus);
4440 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004441 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004442
Neil Robertsa28c6932013-10-03 16:43:04 +01004443 activate(shell, focus, seat);
4444}
4445
4446static void
4447click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4448 void *data)
4449{
4450 if (seat->pointer->grab != &seat->pointer->default_grab)
4451 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004452 if (seat->pointer->focus == NULL)
4453 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004454
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004455 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004456}
4457
4458static void
4459touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4460{
4461 if (seat->touch->grab != &seat->touch->default_grab)
4462 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004463 if (seat->touch->focus == NULL)
4464 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004465
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004466 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004467}
4468
4469static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004470lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004471{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004472 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004473
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004474 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004475 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004476 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004477 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004478
4479 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004480
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004481 /* Hide all surfaces by removing the fullscreen, panel and
4482 * toplevel layers. This way nothing else can show or receive
4483 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004484
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004485 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004486 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004487 if (shell->showing_input_panels)
4488 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004489 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004490 wl_list_insert(&shell->compositor->cursor_layer.link,
4491 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004492
Pekka Paalanen77346a62011-11-30 16:26:35 +02004493 launch_screensaver(shell);
4494
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004495 /* TODO: disable bindings that should not work while locked. */
4496
4497 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004498}
4499
4500static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004501unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004502{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004503 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004504 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004505 return;
4506 }
4507
4508 /* If desktop-shell client has gone away, unlock immediately. */
4509 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004510 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004511 return;
4512 }
4513
4514 if (shell->prepare_event_sent)
4515 return;
4516
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004517 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004518 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004519}
4520
4521static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004522shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004523{
4524 struct desktop_shell *shell = data;
4525
4526 shell->fade.animation = NULL;
4527
4528 switch (shell->fade.type) {
4529 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004530 weston_surface_destroy(shell->fade.view->surface);
4531 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004532 break;
4533 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004534 lock(shell);
4535 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004536 default:
4537 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004538 }
4539}
4540
Jason Ekstranda7af7042013-10-12 22:38:11 -05004541static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004542shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004543{
4544 struct weston_compositor *compositor = shell->compositor;
4545 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004546 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004547
4548 surface = weston_surface_create(compositor);
4549 if (!surface)
4550 return NULL;
4551
Jason Ekstranda7af7042013-10-12 22:38:11 -05004552 view = weston_view_create(surface);
4553 if (!view) {
4554 weston_surface_destroy(surface);
4555 return NULL;
4556 }
4557
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004558 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004559 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004560 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004561 wl_list_insert(&compositor->fade_layer.view_list,
4562 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004563 pixman_region32_init(&surface->input);
4564
Jason Ekstranda7af7042013-10-12 22:38:11 -05004565 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004566}
4567
4568static void
4569shell_fade(struct desktop_shell *shell, enum fade_type type)
4570{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004571 float tint;
4572
4573 switch (type) {
4574 case FADE_IN:
4575 tint = 0.0;
4576 break;
4577 case FADE_OUT:
4578 tint = 1.0;
4579 break;
4580 default:
4581 weston_log("shell: invalid fade type\n");
4582 return;
4583 }
4584
4585 shell->fade.type = type;
4586
Jason Ekstranda7af7042013-10-12 22:38:11 -05004587 if (shell->fade.view == NULL) {
4588 shell->fade.view = shell_fade_create_surface(shell);
4589 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004590 return;
4591
Jason Ekstranda7af7042013-10-12 22:38:11 -05004592 shell->fade.view->alpha = 1.0 - tint;
4593 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004594 }
4595
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004596 if (shell->fade.view->output == NULL) {
4597 /* If the black view gets a NULL output, we lost the
4598 * last output and we'll just cancel the fade. This
4599 * happens when you close the last window under the
4600 * X11 or Wayland backends. */
4601 shell->locked = false;
4602 weston_surface_destroy(shell->fade.view->surface);
4603 shell->fade.view = NULL;
4604 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004605 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004606 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004607 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004608 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004609 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004610 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004611 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004612}
4613
4614static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004615do_shell_fade_startup(void *data)
4616{
4617 struct desktop_shell *shell = data;
4618
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004619 if (shell->startup_animation_type == ANIMATION_FADE)
4620 shell_fade(shell, FADE_IN);
4621 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004622 weston_surface_destroy(shell->fade.view->surface);
4623 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004624 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004625}
4626
4627static void
4628shell_fade_startup(struct desktop_shell *shell)
4629{
4630 struct wl_event_loop *loop;
4631
4632 if (!shell->fade.startup_timer)
4633 return;
4634
4635 wl_event_source_remove(shell->fade.startup_timer);
4636 shell->fade.startup_timer = NULL;
4637
4638 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4639 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4640}
4641
4642static int
4643fade_startup_timeout(void *data)
4644{
4645 struct desktop_shell *shell = data;
4646
4647 shell_fade_startup(shell);
4648 return 0;
4649}
4650
4651static void
4652shell_fade_init(struct desktop_shell *shell)
4653{
4654 /* Make compositor output all black, and wait for the desktop-shell
4655 * client to signal it is ready, then fade in. The timer triggers a
4656 * fade-in, in case the desktop-shell client takes too long.
4657 */
4658
4659 struct wl_event_loop *loop;
4660
Jason Ekstranda7af7042013-10-12 22:38:11 -05004661 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004662 weston_log("%s: warning: fade surface already exists\n",
4663 __func__);
4664 return;
4665 }
4666
Jason Ekstranda7af7042013-10-12 22:38:11 -05004667 shell->fade.view = shell_fade_create_surface(shell);
4668 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004669 return;
4670
Jason Ekstranda7af7042013-10-12 22:38:11 -05004671 weston_view_update_transform(shell->fade.view);
4672 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004673
4674 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4675 shell->fade.startup_timer =
4676 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4677 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4678}
4679
4680static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004681idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004682{
4683 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004684 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004685 struct weston_seat *seat;
4686
4687 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4688 if (seat->pointer)
4689 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004690
4691 shell_fade(shell, FADE_OUT);
4692 /* lock() is called from shell_fade_done() */
4693}
4694
4695static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004696wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004697{
4698 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004699 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004700
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004701 unlock(shell);
4702}
4703
4704static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004705center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004706{
Giulio Camuffob8366642013-04-25 13:57:46 +03004707 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004708 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004709
Jason Ekstranda7af7042013-10-12 22:38:11 -05004710 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004711
4712 x = output->x + (output->width - width) / 2 - surf_x / 2;
4713 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004714
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004715 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004716}
4717
4718static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004719weston_view_set_initial_position(struct weston_view *view,
4720 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004721{
4722 struct weston_compositor *compositor = shell->compositor;
4723 int ix = 0, iy = 0;
4724 int range_x, range_y;
4725 int dx, dy, x, y, panel_height;
4726 struct weston_output *output, *target_output = NULL;
4727 struct weston_seat *seat;
4728
4729 /* As a heuristic place the new window on the same output as the
4730 * pointer. Falling back to the output containing 0, 0.
4731 *
4732 * TODO: Do something clever for touch too?
4733 */
4734 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004735 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004736 ix = wl_fixed_to_int(seat->pointer->x);
4737 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004738 break;
4739 }
4740 }
4741
4742 wl_list_for_each(output, &compositor->output_list, link) {
4743 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4744 target_output = output;
4745 break;
4746 }
4747 }
4748
4749 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004750 weston_view_set_position(view, 10 + random() % 400,
4751 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004752 return;
4753 }
4754
4755 /* Valid range within output where the surface will still be onscreen.
4756 * If this is negative it means that the surface is bigger than
4757 * output.
4758 */
4759 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004760 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004761 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004762 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004763
Rob Bradford4cb88c72012-08-13 15:18:44 +01004764 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004765 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004766 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004767 dx = 0;
4768
4769 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004770 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004771 else
4772 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004773
4774 x = target_output->x + dx;
4775 y = target_output->y + dy;
4776
Jason Ekstranda7af7042013-10-12 22:38:11 -05004777 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004778}
4779
4780static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004781map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004782 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004783{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004784 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004785 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004786 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004787 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004788
Pekka Paalanen77346a62011-11-30 16:26:35 +02004789 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004790 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004791 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004792 if (shsurf->state.fullscreen) {
4793 center_on_output(shsurf->view, shsurf->fullscreen_output);
4794 shell_map_fullscreen(shsurf);
4795 } else if (shsurf->state.maximized) {
4796 /* use surface configure to set the geometry */
4797 panel_height = get_output_panel_height(shell, shsurf->output);
4798 surface_subsurfaces_boundingbox(shsurf->surface,
4799 &surf_x, &surf_y, NULL, NULL);
4800 weston_view_set_position(shsurf->view,
4801 shsurf->output->x - surf_x,
4802 shsurf->output->y +
4803 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004804 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004805 weston_view_set_initial_position(shsurf->view, shell);
4806 }
Juan Zhao96879df2012-02-07 08:45:41 +08004807 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004808 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004809 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004810 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004811 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004812 weston_view_set_position(shsurf->view,
4813 shsurf->view->geometry.x + sx,
4814 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004815 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004816 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004817 default:
4818 ;
4819 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004820
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004821 /* Surface stacking order, see also activate(). */
4822 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004823
Jason Ekstranda7af7042013-10-12 22:38:11 -05004824 if (shsurf->type != SHELL_SURFACE_NONE) {
4825 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004826 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004827 shsurf->surface->output = shsurf->output;
4828 shsurf->view->output = shsurf->output;
4829 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004830 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004831
Jason Ekstranda7af7042013-10-12 22:38:11 -05004832 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004833 /* XXX: xwayland's using the same fields for transient type */
4834 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004835 if (shsurf->transient.flags ==
4836 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4837 break;
4838 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004839 if (shsurf->state.relative &&
4840 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4841 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004842 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004843 break;
4844 wl_list_for_each(seat, &compositor->seat_list, link)
4845 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004846 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004847 case SHELL_SURFACE_POPUP:
4848 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004849 default:
4850 break;
4851 }
4852
Rafael Antognolli03b16592013-12-03 15:35:42 -02004853 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4854 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004855 {
4856 switch (shell->win_animation_type) {
4857 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004858 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004859 break;
4860 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004861 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004862 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004863 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004864 default:
4865 break;
4866 }
4867 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004868}
4869
4870static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004871configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004872 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004873{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004874 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004875 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004876 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004877
Pekka Paalanen77346a62011-11-30 16:26:35 +02004878 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004879
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004880 assert(shsurf);
4881
Rafael Antognolli03b16592013-12-03 15:35:42 -02004882 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004883 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004884 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004885 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004886 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4887 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004888 mx = shsurf->output->x - surf_x;
4889 my = shsurf->output->y +
4890 get_output_panel_height(shell,shsurf->output) - surf_y;
4891 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004892 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004893 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004894 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004895
Alex Wu4539b082012-03-01 12:57:46 +08004896 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004897 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004898 wl_list_for_each(view, &surface->views, surface_link)
4899 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004900
Rafael Antognolli03b16592013-12-03 15:35:42 -02004901 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004902 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004903 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004904}
4905
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004906static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004907shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004908{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004909 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004910 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004911 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004912
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004913 assert(shsurf);
4914
4915 shell = shsurf->shell;
4916
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004917 if (!weston_surface_is_mapped(es) &&
4918 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004919 remove_popup_grab(shsurf);
4920 }
4921
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004922 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004923 return;
4924
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004925 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004926 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004927 type_changed = 1;
4928 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004929
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004930 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004931 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004932 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004933 shsurf->last_width != es->width ||
4934 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004935 float from_x, from_y;
4936 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004937
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08004938 if (shsurf->resize_edges) {
4939 sx = 0;
4940 sy = 0;
4941 }
4942
4943 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
4944 sx = shsurf->last_width - es->width;
4945 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
4946 sy = shsurf->last_height - es->height;
4947
4948 shsurf->last_width = es->width;
4949 shsurf->last_height = es->height;
4950
Jason Ekstranda7af7042013-10-12 22:38:11 -05004951 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4952 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004953 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004954 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004955 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004956 }
4957}
4958
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004959static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004960
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004961static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004962desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004963{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004964 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004965 struct desktop_shell *shell =
4966 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004967
4968 shell->child.process.pid = 0;
4969 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004970
4971 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4972 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004973 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004974 shell->child.deathstamp = time;
4975 shell->child.deathcount = 0;
4976 }
4977
4978 shell->child.deathcount++;
4979 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004980 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004981 return;
4982 }
4983
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004984 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004985 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004986 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004987}
4988
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004989static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004990desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4991{
4992 struct desktop_shell *shell;
4993
4994 shell = container_of(listener, struct desktop_shell,
4995 child.client_destroy_listener);
4996
4997 shell->child.client = NULL;
4998}
4999
5000static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005001launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005002{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005003 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005004
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005005 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005006 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005007 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005008 desktop_shell_sigchld);
5009
5010 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005011 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005012
5013 shell->child.client_destroy_listener.notify =
5014 desktop_shell_client_destroy;
5015 wl_client_add_destroy_listener(shell->child.client,
5016 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005017}
5018
5019static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005020bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5021{
Tiago Vignattibe143262012-04-16 17:31:41 +03005022 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005023 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005024
Jason Ekstranda85118c2013-06-27 20:17:02 -05005025 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
5026 if (resource)
5027 wl_resource_set_implementation(resource, &shell_implementation,
5028 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005029}
5030
Kristian Høgsberg75840622011-09-06 13:48:16 -04005031static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005032handle_shell_client_destroy(struct wl_listener *listener, void *data)
5033{
5034 struct shell_client *sc =
5035 container_of(listener, struct shell_client, destroy_listener);
5036
5037 if (sc->ping_timer)
5038 wl_event_source_remove(sc->ping_timer);
5039 free(sc);
5040}
5041
5042static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02005043bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5044{
5045 struct desktop_shell *shell = data;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005046 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005047
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005048 sc = zalloc(sizeof *sc);
5049 if (sc == NULL) {
5050 wl_client_post_no_memory(client);
5051 return;
5052 }
5053
5054 sc->resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
5055 if (sc->resource == NULL) {
5056 free(sc);
5057 wl_client_post_no_memory(client);
5058 return;
5059 }
5060
5061 sc->client = client;
5062 sc->shell = shell;
5063 sc->destroy_listener.notify = handle_shell_client_destroy;
5064 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5065
5066 wl_resource_set_dispatcher(sc->resource,
5067 xdg_shell_unversioned_dispatch,
5068 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005069}
5070
5071static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005072unbind_desktop_shell(struct wl_resource *resource)
5073{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005074 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005075
5076 if (shell->locked)
5077 resume_desktop(shell);
5078
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005079 shell->child.desktop_shell = NULL;
5080 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005081}
5082
5083static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005084bind_desktop_shell(struct wl_client *client,
5085 void *data, uint32_t version, uint32_t id)
5086{
Tiago Vignattibe143262012-04-16 17:31:41 +03005087 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005088 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005089
Jason Ekstranda85118c2013-06-27 20:17:02 -05005090 resource = wl_resource_create(client, &desktop_shell_interface,
5091 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005092
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005093 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005094 wl_resource_set_implementation(resource,
5095 &desktop_shell_implementation,
5096 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005097 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005098
5099 if (version < 2)
5100 shell_fade_startup(shell);
5101
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005102 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005103 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005104
5105 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5106 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005107 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005108}
5109
Pekka Paalanen6e168112011-11-24 11:34:05 +02005110static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005111screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005112{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005113 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005114 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005115
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005116 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005117 return;
5118
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005119 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005120 if (!shell->locked)
5121 return;
5122
Jason Ekstranda7af7042013-10-12 22:38:11 -05005123 view = container_of(surface->views.next, struct weston_view, surface_link);
5124 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005125
Jason Ekstranda7af7042013-10-12 22:38:11 -05005126 if (wl_list_empty(&view->layer_link)) {
5127 wl_list_insert(shell->lock_layer.view_list.prev,
5128 &view->layer_link);
5129 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005130 wl_event_source_timer_update(shell->screensaver.timer,
5131 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005132 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005133 }
5134}
5135
5136static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005137screensaver_set_surface(struct wl_client *client,
5138 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005139 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005140 struct wl_resource *output_resource)
5141{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005142 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005143 struct weston_surface *surface =
5144 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005145 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005146 struct weston_view *view, *next;
5147
5148 /* Make sure we only have one view */
5149 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5150 weston_view_destroy(view);
5151 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005152
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005153 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005154 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005155 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005156}
5157
5158static const struct screensaver_interface screensaver_implementation = {
5159 screensaver_set_surface
5160};
5161
5162static void
5163unbind_screensaver(struct wl_resource *resource)
5164{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005165 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005166
Pekka Paalanen77346a62011-11-30 16:26:35 +02005167 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005168}
5169
5170static void
5171bind_screensaver(struct wl_client *client,
5172 void *data, uint32_t version, uint32_t id)
5173{
Tiago Vignattibe143262012-04-16 17:31:41 +03005174 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005175 struct wl_resource *resource;
5176
Jason Ekstranda85118c2013-06-27 20:17:02 -05005177 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005178
Pekka Paalanen77346a62011-11-30 16:26:35 +02005179 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005180 wl_resource_set_implementation(resource,
5181 &screensaver_implementation,
5182 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005183 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005184 return;
5185 }
5186
5187 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5188 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005189 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005190}
5191
Kristian Høgsberg07045392012-02-19 18:52:44 -05005192struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005193 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005194 struct weston_surface *current;
5195 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005196 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005197};
5198
5199static void
5200switcher_next(struct switcher *switcher)
5201{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005202 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005203 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005204 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005205 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005206
Jason Ekstranda7af7042013-10-12 22:38:11 -05005207 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005208 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005209 if (shsurf &&
5210 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5211 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005212 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005213 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005214 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005215 next = view->surface;
5216 prev = view->surface;
5217 view->alpha = 0.25;
5218 weston_view_geometry_dirty(view);
5219 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005220 }
Alex Wu1659daa2012-04-01 20:13:09 +08005221
Jason Ekstranda7af7042013-10-12 22:38:11 -05005222 if (is_black_surface(view->surface, NULL)) {
5223 view->alpha = 0.25;
5224 weston_view_geometry_dirty(view);
5225 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005226 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005227 }
5228
5229 if (next == NULL)
5230 next = first;
5231
Alex Wu07b26062012-03-12 16:06:01 +08005232 if (next == NULL)
5233 return;
5234
Kristian Høgsberg07045392012-02-19 18:52:44 -05005235 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005236 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005237
5238 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005239 wl_list_for_each(view, &next->views, surface_link)
5240 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005241
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005242 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005243 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005244 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005245}
5246
5247static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005248switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005249{
5250 struct switcher *switcher =
5251 container_of(listener, struct switcher, listener);
5252
5253 switcher_next(switcher);
5254}
5255
5256static void
Daniel Stone351eb612012-05-31 15:27:47 -04005257switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005258{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005259 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005260 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005261 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005262
Jason Ekstranda7af7042013-10-12 22:38:11 -05005263 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005264 if (is_focus_view(view))
5265 continue;
5266
Jason Ekstranda7af7042013-10-12 22:38:11 -05005267 view->alpha = 1.0;
5268 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005269 }
5270
Alex Wu07b26062012-03-12 16:06:01 +08005271 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005272 activate(switcher->shell, switcher->current,
5273 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005274 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005275 weston_keyboard_end_grab(keyboard);
5276 if (keyboard->input_method_resource)
5277 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005278 free(switcher);
5279}
5280
5281static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005282switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005283 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005284{
5285 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005286 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005287
Daniel Stonec9785ea2012-05-30 16:31:52 +01005288 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005289 switcher_next(switcher);
5290}
5291
5292static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005293switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005294 uint32_t mods_depressed, uint32_t mods_latched,
5295 uint32_t mods_locked, uint32_t group)
5296{
5297 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005298 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005299
Daniel Stone351eb612012-05-31 15:27:47 -04005300 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5301 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005302}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005303
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005304static void
5305switcher_cancel(struct weston_keyboard_grab *grab)
5306{
5307 struct switcher *switcher = container_of(grab, struct switcher, grab);
5308
5309 switcher_destroy(switcher);
5310}
5311
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005312static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005313 switcher_key,
5314 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005315 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005316};
5317
5318static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005319switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005320 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005321{
Tiago Vignattibe143262012-04-16 17:31:41 +03005322 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005323 struct switcher *switcher;
5324
5325 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005326 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005327 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005328 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005329 wl_list_init(&switcher->listener.link);
5330
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005331 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005332 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005333 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005334 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5335 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005336 switcher_next(switcher);
5337}
5338
Pekka Paalanen3c647232011-12-22 13:43:43 +02005339static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005340backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005341 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005342{
5343 struct weston_compositor *compositor = data;
5344 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005345 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005346
5347 /* TODO: we're limiting to simple use cases, where we assume just
5348 * control on the primary display. We'd have to extend later if we
5349 * ever get support for setting backlights on random desktop LCD
5350 * panels though */
5351 output = get_default_output(compositor);
5352 if (!output)
5353 return;
5354
5355 if (!output->set_backlight)
5356 return;
5357
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005358 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5359 backlight_new = output->backlight_current - 25;
5360 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5361 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005362
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005363 if (backlight_new < 5)
5364 backlight_new = 5;
5365 if (backlight_new > 255)
5366 backlight_new = 255;
5367
5368 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005369 output->set_backlight(output, output->backlight_current);
5370}
5371
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005372struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005373 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005374 struct weston_seat *seat;
5375 uint32_t key[2];
5376 int key_released[2];
5377};
5378
5379static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005380debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005381 uint32_t key, uint32_t state)
5382{
5383 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005384 struct weston_compositor *ec = db->seat->compositor;
5385 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005386 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005387 uint32_t serial;
5388 int send = 0, terminate = 0;
5389 int check_binding = 1;
5390 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005391 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005392
5393 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5394 /* Do not run bindings on key releases */
5395 check_binding = 0;
5396
5397 for (i = 0; i < 2; i++)
5398 if (key == db->key[i])
5399 db->key_released[i] = 1;
5400
5401 if (db->key_released[0] && db->key_released[1]) {
5402 /* All key releases been swalled so end the grab */
5403 terminate = 1;
5404 } else if (key != db->key[0] && key != db->key[1]) {
5405 /* Should not swallow release of other keys */
5406 send = 1;
5407 }
5408 } else if (key == db->key[0] && !db->key_released[0]) {
5409 /* Do not check bindings for the first press of the binding
5410 * key. This allows it to be used as a debug shortcut.
5411 * We still need to swallow this event. */
5412 check_binding = 0;
5413 } else if (db->key[1]) {
5414 /* If we already ran a binding don't process another one since
5415 * we can't keep track of all the binding keys that were
5416 * pressed in order to swallow the release events. */
5417 send = 1;
5418 check_binding = 0;
5419 }
5420
5421 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005422 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5423 key, state)) {
5424 /* We ran a binding so swallow the press and keep the
5425 * grab to swallow the released too. */
5426 send = 0;
5427 terminate = 0;
5428 db->key[1] = key;
5429 } else {
5430 /* Terminate the grab since the key pressed is not a
5431 * debug binding key. */
5432 send = 1;
5433 terminate = 1;
5434 }
5435 }
5436
5437 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005438 serial = wl_display_next_serial(display);
5439 resource_list = &grab->keyboard->focus_resource_list;
5440 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005441 wl_keyboard_send_key(resource, serial, time, key, state);
5442 }
5443 }
5444
5445 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005446 weston_keyboard_end_grab(grab->keyboard);
5447 if (grab->keyboard->input_method_resource)
5448 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005449 free(db);
5450 }
5451}
5452
5453static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005454debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005455 uint32_t mods_depressed, uint32_t mods_latched,
5456 uint32_t mods_locked, uint32_t group)
5457{
5458 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005459 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005460
Neil Roberts96d790e2013-09-19 17:32:00 +01005461 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005462
Neil Roberts96d790e2013-09-19 17:32:00 +01005463 wl_resource_for_each(resource, resource_list) {
5464 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5465 mods_latched, mods_locked, group);
5466 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005467}
5468
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005469static void
5470debug_binding_cancel(struct weston_keyboard_grab *grab)
5471{
5472 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5473
5474 weston_keyboard_end_grab(grab->keyboard);
5475 free(db);
5476}
5477
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005478struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005479 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005480 debug_binding_modifiers,
5481 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005482};
5483
5484static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005485debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005486{
5487 struct debug_binding_grab *grab;
5488
5489 grab = calloc(1, sizeof *grab);
5490 if (!grab)
5491 return;
5492
5493 grab->seat = (struct weston_seat *) seat;
5494 grab->key[0] = key;
5495 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005496 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005497}
5498
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005499static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005500force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005501 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005502{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005503 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005504 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005505 struct desktop_shell *shell = data;
5506 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005507 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005508
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005509 focus_surface = seat->keyboard->focus;
5510 if (!focus_surface)
5511 return;
5512
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005513 wl_signal_emit(&compositor->kill_signal, focus_surface);
5514
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005515 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005516 wl_client_get_credentials(client, &pid, NULL, NULL);
5517
5518 /* Skip clients that we launched ourselves (the credentials of
5519 * the socketpair is ours) */
5520 if (pid == getpid())
5521 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005522
Daniel Stone325fc2d2012-05-30 16:31:58 +01005523 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005524}
5525
5526static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005527workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005528 uint32_t key, void *data)
5529{
5530 struct desktop_shell *shell = data;
5531 unsigned int new_index = shell->workspaces.current;
5532
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005533 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005534 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005535 if (new_index != 0)
5536 new_index--;
5537
5538 change_workspace(shell, new_index);
5539}
5540
5541static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005542workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005543 uint32_t key, void *data)
5544{
5545 struct desktop_shell *shell = data;
5546 unsigned int new_index = shell->workspaces.current;
5547
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005548 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005549 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005550 if (new_index < shell->workspaces.num - 1)
5551 new_index++;
5552
5553 change_workspace(shell, new_index);
5554}
5555
5556static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005557workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005558 uint32_t key, void *data)
5559{
5560 struct desktop_shell *shell = data;
5561 unsigned int new_index;
5562
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005563 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005564 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005565 new_index = key - KEY_F1;
5566 if (new_index >= shell->workspaces.num)
5567 new_index = shell->workspaces.num - 1;
5568
5569 change_workspace(shell, new_index);
5570}
5571
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005572static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005573workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005574 uint32_t key, void *data)
5575{
5576 struct desktop_shell *shell = data;
5577 unsigned int new_index = shell->workspaces.current;
5578
5579 if (shell->locked)
5580 return;
5581
5582 if (new_index != 0)
5583 new_index--;
5584
5585 take_surface_to_workspace_by_seat(shell, seat, new_index);
5586}
5587
5588static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005589workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005590 uint32_t key, void *data)
5591{
5592 struct desktop_shell *shell = data;
5593 unsigned int new_index = shell->workspaces.current;
5594
5595 if (shell->locked)
5596 return;
5597
5598 if (new_index < shell->workspaces.num - 1)
5599 new_index++;
5600
5601 take_surface_to_workspace_by_seat(shell, seat, new_index);
5602}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005603
5604static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005605shell_reposition_view_on_output_destroy(struct weston_view *view)
5606{
5607 struct weston_output *output, *first_output;
5608 struct weston_compositor *ec = view->surface->compositor;
5609 struct shell_surface *shsurf;
5610 float x, y;
5611 int visible;
5612
5613 x = view->geometry.x;
5614 y = view->geometry.y;
5615
5616 /* At this point the destroyed output is not in the list anymore.
5617 * If the view is still visible somewhere, we leave where it is,
5618 * otherwise, move it to the first output. */
5619 visible = 0;
5620 wl_list_for_each(output, &ec->output_list, link) {
5621 if (pixman_region32_contains_point(&output->region,
5622 x, y, NULL)) {
5623 visible = 1;
5624 break;
5625 }
5626 }
5627
5628 if (!visible) {
5629 first_output = container_of(ec->output_list.next,
5630 struct weston_output, link);
5631
5632 x = first_output->x + first_output->width / 4;
5633 y = first_output->y + first_output->height / 4;
5634 }
5635
5636 weston_view_set_position(view, x, y);
5637
5638 shsurf = get_shell_surface(view->surface);
5639
5640 if (shsurf) {
5641 shsurf->saved_position_valid = false;
5642 shsurf->next_state.maximized = false;
5643 shsurf->next_state.fullscreen = false;
5644 shsurf->state_changed = true;
5645 }
5646}
5647
5648static void
5649shell_reposition_views_on_output_destroy(struct shell_output *shell_output)
5650{
5651 struct desktop_shell *shell = shell_output->shell;
5652 struct weston_output *output = shell_output->output;
5653 struct weston_layer *layer;
5654 struct weston_view *view;
5655
5656 /* Move all views in the layers owned by the shell */
5657 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5658 wl_list_for_each(view, &layer->view_list, layer_link) {
5659 if (view->output != output)
5660 continue;
5661
5662 shell_reposition_view_on_output_destroy(view);
5663 }
5664
5665 /* We don't start from the beggining of the layer list, so
5666 * make sure we don't wrap around it. */
5667 if (layer == &shell->background_layer)
5668 break;
5669 }
5670}
5671
5672static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005673handle_output_destroy(struct wl_listener *listener, void *data)
5674{
5675 struct shell_output *output_listener =
5676 container_of(listener, struct shell_output, destroy_listener);
5677
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005678 shell_reposition_views_on_output_destroy(output_listener);
5679
Xiong Zhang6b481422013-10-23 13:58:32 +08005680 wl_list_remove(&output_listener->destroy_listener.link);
5681 wl_list_remove(&output_listener->link);
5682 free(output_listener);
5683}
5684
5685static void
5686create_shell_output(struct desktop_shell *shell,
5687 struct weston_output *output)
5688{
5689 struct shell_output *shell_output;
5690
5691 shell_output = zalloc(sizeof *shell_output);
5692 if (shell_output == NULL)
5693 return;
5694
5695 shell_output->output = output;
5696 shell_output->shell = shell;
5697 shell_output->destroy_listener.notify = handle_output_destroy;
5698 wl_signal_add(&output->destroy_signal,
5699 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005700 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005701}
5702
5703static void
5704handle_output_create(struct wl_listener *listener, void *data)
5705{
5706 struct desktop_shell *shell =
5707 container_of(listener, struct desktop_shell, output_create_listener);
5708 struct weston_output *output = (struct weston_output *)data;
5709
5710 create_shell_output(shell, output);
5711}
5712
5713static void
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005714handle_output_move(struct wl_listener *listener, void *data)
5715{
5716 struct desktop_shell *shell;
5717 struct weston_output *output;
5718 struct weston_layer *layer;
5719 struct weston_view *view;
5720 float x, y;
5721
5722 shell = container_of(listener, struct desktop_shell,
5723 output_move_listener);
5724 output = data;
5725
5726 /* Move all views in the layers owned by the shell */
5727 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5728 wl_list_for_each(view, &layer->view_list, layer_link) {
5729 if (view->output != output)
5730 continue;
5731
5732 x = view->geometry.x + output->move_x;
5733 y = view->geometry.y + output->move_y;
5734 weston_view_set_position(view, x, y);
5735 }
5736
5737 /* We don't start from the beggining of the layer list, so
5738 * make sure we don't wrap around it. */
5739 if (layer == &shell->background_layer)
5740 break;
5741 }
5742}
5743
5744static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005745setup_output_destroy_handler(struct weston_compositor *ec,
5746 struct desktop_shell *shell)
5747{
5748 struct weston_output *output;
5749
5750 wl_list_init(&shell->output_list);
5751 wl_list_for_each(output, &ec->output_list, link)
5752 create_shell_output(shell, output);
5753
5754 shell->output_create_listener.notify = handle_output_create;
5755 wl_signal_add(&ec->output_created_signal,
5756 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005757
5758 shell->output_move_listener.notify = handle_output_move;
5759 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005760}
5761
5762static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005763shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005764{
Tiago Vignattibe143262012-04-16 17:31:41 +03005765 struct desktop_shell *shell =
5766 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005767 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005768 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005769
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005770 /* Force state to unlocked so we don't try to fade */
5771 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005772 if (shell->child.client)
5773 wl_client_destroy(shell->child.client);
5774
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005775 wl_list_remove(&shell->idle_listener.link);
5776 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005777
5778 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005779
Xiong Zhang6b481422013-10-23 13:58:32 +08005780 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5781 wl_list_remove(&shell_output->destroy_listener.link);
5782 wl_list_remove(&shell_output->link);
5783 free(shell_output);
5784 }
5785
5786 wl_list_remove(&shell->output_create_listener.link);
5787
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005788 wl_array_for_each(ws, &shell->workspaces.array)
5789 workspace_destroy(*ws);
5790 wl_array_release(&shell->workspaces.array);
5791
Pekka Paalanen3c647232011-12-22 13:43:43 +02005792 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005793 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005794 free(shell);
5795}
5796
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005797static void
5798shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5799{
5800 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005801 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005802
5803 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005804 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5805 MODIFIER_CTRL | MODIFIER_ALT,
5806 terminate_binding, ec);
5807 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5808 click_to_activate_binding,
5809 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005810 weston_compositor_add_touch_binding(ec, 0,
5811 touch_to_activate_binding,
5812 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005813 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5814 MODIFIER_SUPER | MODIFIER_ALT,
5815 surface_opacity_binding, NULL);
5816 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5817 MODIFIER_SUPER, zoom_axis_binding,
5818 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005819
5820 /* configurable bindings */
5821 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005822 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5823 zoom_key_binding, NULL);
5824 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5825 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005826 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5827 maximize_binding, NULL);
5828 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5829 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005830 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5831 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005832 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005833 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5834 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005835 weston_compositor_add_button_binding(ec, BTN_LEFT,
5836 mod | MODIFIER_SHIFT,
5837 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005838
5839 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5840 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5841 rotate_binding, NULL);
5842
Daniel Stone325fc2d2012-05-30 16:31:58 +01005843 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5844 shell);
5845 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5846 ec);
5847 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5848 backlight_binding, ec);
5849 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5850 ec);
5851 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5852 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005853 weston_compositor_add_key_binding(ec, KEY_K, mod,
5854 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005855 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5856 workspace_up_binding, shell);
5857 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5858 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005859 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5860 workspace_move_surface_up_binding,
5861 shell);
5862 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5863 workspace_move_surface_down_binding,
5864 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005865
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005866 if (shell->exposay_modifier)
5867 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5868 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005869
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005870 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5871 if (shell->workspaces.num > 1) {
5872 num_workspace_bindings = shell->workspaces.num;
5873 if (num_workspace_bindings > 6)
5874 num_workspace_bindings = 6;
5875 for (i = 0; i < num_workspace_bindings; i++)
5876 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5877 workspace_f_binding,
5878 shell);
5879 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005880
5881 /* Debug bindings */
5882 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5883 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005884}
5885
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005886WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005887module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005888 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005889{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005890 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005891 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005892 struct workspace **pws;
5893 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005894 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005895
Peter Huttererf3d62272013-08-08 11:57:05 +10005896 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005897 if (shell == NULL)
5898 return -1;
5899
Kristian Høgsberg75840622011-09-06 13:48:16 -04005900 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005901
5902 shell->destroy_listener.notify = shell_destroy;
5903 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005904 shell->idle_listener.notify = idle_handler;
5905 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5906 shell->wake_listener.notify = wake_handler;
5907 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005908
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005909 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005910 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005911 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005912 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005913 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005914 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005915 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005916 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005917 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005918 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005919
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005920 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5921 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005922 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5923 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005924 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005925
5926 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005927 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005928
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005929 if (input_panel_setup(shell) < 0)
5930 return -1;
5931
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005932 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005933
Daniel Stonedf8133b2013-11-19 11:37:14 +01005934 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5935 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5936
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005937 for (i = 0; i < shell->workspaces.num; i++) {
5938 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5939 if (pws == NULL)
5940 return -1;
5941
5942 *pws = workspace_create();
5943 if (*pws == NULL)
5944 return -1;
5945 }
5946 activate_workspace(shell, 0);
5947
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005948 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005949 wl_list_init(&shell->workspaces.animation.link);
5950 shell->workspaces.animation.frame = animate_workspace_change_frame;
5951
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005952 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005953 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005954 return -1;
5955
Rafael Antognollie2a34552013-12-03 15:35:45 -02005956 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5957 shell, bind_xdg_shell) == NULL)
5958 return -1;
5959
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005960 if (wl_global_create(ec->wl_display,
5961 &desktop_shell_interface, 2,
5962 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005963 return -1;
5964
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005965 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5966 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005967 return -1;
5968
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005969 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5970 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005971 return -1;
5972
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005973 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005974
Xiong Zhang6b481422013-10-23 13:58:32 +08005975 setup_output_destroy_handler(ec, shell);
5976
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005977 loop = wl_display_get_event_loop(ec->wl_display);
5978 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005979
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005980 shell->screensaver.timer =
5981 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5982
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005983 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005984 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005985 create_keyboard_focus_listener(seat);
5986 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005987
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005988 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005989
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005990 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005991
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005992 return 0;
5993}