blob: bf52b93c1c7966c245eaa8bcc5d216a7ee7a5b5b [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
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050059enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020060 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030062 SHELL_SURFACE_POPUP,
63 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020064};
65
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050066struct shell_client;
67
Philip Withnall648a4dd2013-11-25 18:01:44 +000068/*
69 * Surface stacking and ordering.
70 *
71 * This is handled using several linked lists of surfaces, organised into
72 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
73 * above all of the surfaces in the layer below. The set of layers is static and
74 * in the following order (top-most first):
75 * • Lock layer (only ever displayed on its own)
76 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010077 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000078 * • Fullscreen layer
79 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Workspace layers
81 * • Background layer
82 *
83 * The list of layers may be manipulated to remove whole layers of surfaces from
84 * display. For example, when locking the screen, all layers except the lock
85 * layer are removed.
86 *
87 * A surface’s layer is modified on configuring the surface, in
88 * set_surface_type() (which is only called when the surface’s type change is
89 * _committed_). If a surface’s type changes (e.g. when making a window
90 * fullscreen) its layer changes too.
91 *
92 * In order to allow popup and transient surfaces to be correctly stacked above
93 * their parent surfaces, each surface tracks both its parent surface, and a
94 * linked list of its children. When a surface’s layer is updated, so are the
95 * layers of its children. Note that child surfaces are *not* the same as
96 * subsurfaces — child/parent surfaces are purely for maintaining stacking
97 * order.
98 *
99 * The children_link list of siblings of a surface (i.e. those surfaces which
100 * have the same parent) only contains weston_surfaces which have a
101 * shell_surface. Stacking is not implemented for non-shell_surface
102 * weston_surfaces. This means that the following implication does *not* hold:
103 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
104 */
105
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200106struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500107 struct wl_resource *resource;
108 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500109 struct shell_client *owner;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200110
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500111 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500112 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600113 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700115 struct wl_listener resource_destroy_listener;
116
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400117 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000118 struct wl_list children_list; /* child surfaces of this one */
119 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300120 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200121
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800122 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400123 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500124 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200125 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800126 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200127 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800128 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700129 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800130 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100131
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200133 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500134 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200135 } rotation;
136
137 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100138 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100140 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400141 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 } popup;
143
Alex Wu4539b082012-03-01 12:57:46 +0800144 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300145 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400146 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 } transient;
148
149 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800150 enum wl_shell_surface_fullscreen_method type;
151 struct weston_transform transform; /* matrix from x, y */
152 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800154 } fullscreen;
155
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200156 struct weston_transform workspace_transform;
157
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500158 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400161
162 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200163
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700164 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200165 bool maximized;
166 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200167 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100168 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500169 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200170 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500171 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500172
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700173 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400174 int32_t x, y, width, height;
175 } geometry, next_geometry;
176 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700177
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500178 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179};
180
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300181struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400182 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300183 struct shell_surface *shsurf;
184 struct wl_listener shsurf_destroy_listener;
185};
186
Rusty Lynch1084da52013-08-15 09:10:08 -0700187struct shell_touch_grab {
188 struct weston_touch_grab grab;
189 struct shell_surface *shsurf;
190 struct wl_listener shsurf_destroy_listener;
191 struct weston_touch *touch;
192};
193
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300194struct weston_move_grab {
195 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100196 wl_fixed_t dx, dy;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700197 int client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500198};
199
Rusty Lynch1084da52013-08-15 09:10:08 -0700200struct weston_touch_move_grab {
201 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800202 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700203 wl_fixed_t dx, dy;
204};
205
Pekka Paalanen460099f2012-01-20 16:48:25 +0200206struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300207 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500208 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200209 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200210 float x;
211 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200212 } center;
213};
214
Giulio Camuffo5085a752013-03-25 21:42:45 +0100215struct shell_seat {
216 struct weston_seat *seat;
217 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500218 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100219
Jason Ekstrand024177c2014-04-21 19:42:58 -0500220 struct wl_listener caps_changed_listener;
221 struct wl_listener pointer_focus_listener;
222 struct wl_listener keyboard_focus_listener;
223
Giulio Camuffo5085a752013-03-25 21:42:45 +0100224 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400225 struct weston_pointer_grab grab;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200226 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100227 struct wl_list surfaces_list;
228 struct wl_client *client;
229 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200230 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100231 } popup_grab;
232};
233
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800234struct shell_client {
235 struct wl_resource *resource;
236 struct wl_client *client;
237 struct desktop_shell *shell;
238 struct wl_listener destroy_listener;
239 struct wl_event_source *ping_timer;
240 uint32_t ping_serial;
241 int unresponsive;
242};
243
Alex Wubd3354b2012-04-17 17:20:49 +0800244static struct desktop_shell *
245shell_surface_get_shell(struct shell_surface *shsurf);
246
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500247static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400248surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500249
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300250static void
251shell_fade_startup(struct desktop_shell *shell);
252
Philip Withnallbecb77e2013-11-25 18:01:30 +0000253static struct shell_seat *
254get_shell_seat(struct weston_seat *seat);
255
Jonny Lambd73c6942014-08-20 15:53:20 +0200256static void
257get_output_panel_size(struct desktop_shell *shell,
258 struct weston_output *output,
259 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700260
Philip Withnall648a4dd2013-11-25 18:01:44 +0000261static void
262shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
263
Alex Wubd3354b2012-04-17 17:20:49 +0800264static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200265shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
266
267static bool
268shell_surface_is_xdg_surface(struct shell_surface *shsurf);
269
270static bool
271shell_surface_is_xdg_popup(struct shell_surface *shsurf);
272
273static void
274shell_surface_set_parent(struct shell_surface *shsurf,
275 struct weston_surface *parent);
276
277static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800278shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
279{
280 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800282
283 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100284
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300285 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800286 return false;
287
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300288 top_fs_ev = container_of(shell->fullscreen_layer.view_list.link.next,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 struct weston_view,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300290 layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500291 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800292}
293
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500294static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400295destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300296{
297 struct shell_grab *grab;
298
299 grab = container_of(listener, struct shell_grab,
300 shsurf_destroy_listener);
301
302 grab->shsurf = NULL;
303}
304
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800305struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500306get_default_view(struct weston_surface *surface)
307{
308 struct shell_surface *shsurf;
309 struct weston_view *view;
310
311 if (!surface || wl_list_empty(&surface->views))
312 return NULL;
313
314 shsurf = get_shell_surface(surface);
315 if (shsurf)
316 return shsurf->view;
317
318 wl_list_for_each(view, &surface->views, surface_link)
319 if (weston_view_is_mapped(view))
320 return view;
321
322 return container_of(surface->views.next, struct weston_view, surface_link);
323}
324
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300325static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400326popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200327static void
328touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400329
330static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300331shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400332 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300333 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400334 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300335 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300336{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300337 struct desktop_shell *shell = shsurf->shell;
338
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400339 popup_grab_end(pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200340 if (pointer->seat->touch)
341 touch_popup_grab_end(pointer->seat->touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400342
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300343 grab->grab.interface = interface;
344 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400345 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500346 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400347 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300348
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700349 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400350 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400351 if (shell->child.desktop_shell) {
352 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
353 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500354 weston_pointer_set_focus(pointer,
355 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400356 wl_fixed_from_int(0),
357 wl_fixed_from_int(0));
358 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300359}
360
Jonny Lambd73c6942014-08-20 15:53:20 +0200361static void
362get_output_panel_size(struct desktop_shell *shell,
363 struct weston_output *output,
364 int *width,
365 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700366{
367 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200368
369 *width = 0;
370 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700371
372 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200373 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700374
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300375 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200376 float x, y;
377
378 if (view->surface->output != output)
379 continue;
380
381 switch (shell->panel_position) {
382 case DESKTOP_SHELL_PANEL_POSITION_TOP:
383 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
384
385 weston_view_to_global_float(view,
386 view->surface->width, 0,
387 &x, &y);
388
389 *width = (int) x;
390 *height = view->surface->height + (int) y;
391 return;
392
393 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
394 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
395 weston_view_to_global_float(view,
396 0, view->surface->height,
397 &x, &y);
398
399 *width = view->surface->width + (int) x;
400 *height = (int) y;
401 return;
402
403 default:
404 /* we've already set width and height to
405 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700406 break;
407 }
408 }
409
Jonny Lambd73c6942014-08-20 15:53:20 +0200410 /* the correct view wasn't found */
411}
412
413static void
414get_output_work_area(struct desktop_shell *shell,
415 struct weston_output *output,
416 pixman_rectangle32_t *area)
417{
418 int32_t panel_width = 0, panel_height = 0;
419
420 area->x = 0;
421 area->y = 0;
422
423 get_output_panel_size(shell, output, &panel_width, &panel_height);
424
425 switch (shell->panel_position) {
426 case DESKTOP_SHELL_PANEL_POSITION_TOP:
427 default:
428 area->y = panel_height;
429 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
430 area->width = output->width;
431 area->height = output->height - panel_height;
432 break;
433 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
434 area->x = panel_width;
435 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
436 area->width = output->width - panel_width;
437 area->height = output->height;
438 break;
439 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700440}
441
442static void
443send_configure_for_surface(struct shell_surface *shsurf)
444{
445 int32_t width, height;
446 struct surface_state *state;
447
448 if (shsurf->state_requested)
449 state = &shsurf->requested_state;
450 else if (shsurf->state_changed)
451 state = &shsurf->next_state;
452 else
453 state = &shsurf->state;
454
455 if (state->fullscreen) {
456 width = shsurf->output->width;
457 height = shsurf->output->height;
458 } else if (state->maximized) {
459 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200460 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700461
462 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200463 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700464
Jonny Lambd73c6942014-08-20 15:53:20 +0200465 width = area.width;
466 height = area.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700467 } else {
468 width = 0;
469 height = 0;
470 }
471
472 shsurf->client->send_configure(shsurf->surface, width, height);
473}
474
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300475static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400476shell_surface_state_changed(struct shell_surface *shsurf)
477{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700478 if (shell_surface_is_xdg_surface(shsurf))
479 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400480}
481
482static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300483shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300484{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700485 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400486 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700487 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400488
489 if (grab->shsurf->resize_edges) {
490 grab->shsurf->resize_edges = 0;
491 shell_surface_state_changed(grab->shsurf);
492 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700493 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300494
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700495 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300496}
497
498static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700499shell_touch_grab_start(struct shell_touch_grab *grab,
500 const struct weston_touch_grab_interface *interface,
501 struct shell_surface *shsurf,
502 struct weston_touch *touch)
503{
504 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800505
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200506 touch_popup_grab_end(touch);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700507 if (touch->seat->pointer)
508 popup_grab_end(touch->seat->pointer);
509
Rusty Lynch1084da52013-08-15 09:10:08 -0700510 grab->grab.interface = interface;
511 grab->shsurf = shsurf;
512 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
513 wl_signal_add(&shsurf->destroy_signal,
514 &grab->shsurf_destroy_listener);
515
516 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700517 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700518
519 weston_touch_start_grab(touch, &grab->grab);
520 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500521 weston_touch_set_focus(touch->seat,
522 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700523}
524
525static void
526shell_touch_grab_end(struct shell_touch_grab *grab)
527{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700528 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700529 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700530 grab->shsurf->grabbed = 0;
531 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700532
533 weston_touch_end_grab(grab->touch);
534}
535
536static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500537center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800538 struct weston_output *output);
539
Daniel Stone496ca172012-05-30 16:31:42 +0100540static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300541get_modifier(char *modifier)
542{
543 if (!modifier)
544 return MODIFIER_SUPER;
545
546 if (!strcmp("ctrl", modifier))
547 return MODIFIER_CTRL;
548 else if (!strcmp("alt", modifier))
549 return MODIFIER_ALT;
550 else if (!strcmp("super", modifier))
551 return MODIFIER_SUPER;
552 else
553 return MODIFIER_SUPER;
554}
555
Juan Zhaoe10d2792012-04-25 19:09:52 +0800556static enum animation_type
557get_animation_type(char *animation)
558{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800559 if (!animation)
560 return ANIMATION_NONE;
561
Juan Zhaoe10d2792012-04-25 19:09:52 +0800562 if (!strcmp("zoom", animation))
563 return ANIMATION_ZOOM;
564 else if (!strcmp("fade", animation))
565 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100566 else if (!strcmp("dim-layer", animation))
567 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800568 else
569 return ANIMATION_NONE;
570}
571
Alex Wu4539b082012-03-01 12:57:46 +0800572static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400573shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200574{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400575 struct weston_config_section *section;
576 int duration;
Derek Foremanc7210432014-08-21 11:32:38 -0500577 char *s, *client;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200578
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400579 section = weston_config_get_section(shell->compositor->config,
580 "screensaver", NULL, NULL);
581 weston_config_section_get_string(section,
582 "path", &shell->screensaver.path, NULL);
583 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200584 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400585
586 section = weston_config_get_section(shell->compositor->config,
587 "shell", NULL, NULL);
Derek Foremanc7210432014-08-21 11:32:38 -0500588 asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
589 WESTON_SHELL_CLIENT);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400590 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500591 "client", &s, client);
592 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100593 shell->client = s;
594 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400595 "binding-modifier", &s, "super");
596 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200597 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800598
599 weston_config_section_get_string(section,
600 "exposay-modifier", &s, "none");
601 if (strcmp(s, "none") == 0)
602 shell->exposay_modifier = 0;
603 else
604 shell->exposay_modifier = get_modifier(s);
605 free(s);
606
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400607 weston_config_section_get_string(section, "animation", &s, "none");
608 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200609 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200610 weston_config_section_get_string(section, "close-animation", &s, "fade");
611 shell->win_close_animation_type = get_animation_type(s);
612 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700613 weston_config_section_get_string(section,
614 "startup-animation", &s, "fade");
615 shell->startup_animation_type = get_animation_type(s);
616 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700617 if (shell->startup_animation_type == ANIMATION_ZOOM)
618 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100619 weston_config_section_get_string(section, "focus-animation", &s, "none");
620 shell->focus_animation_type = get_animation_type(s);
621 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400622 weston_config_section_get_uint(section, "num-workspaces",
623 &shell->workspaces.num,
624 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200625}
626
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800627struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100628get_default_output(struct weston_compositor *compositor)
629{
630 return container_of(compositor->output_list.next,
631 struct weston_output, link);
632}
633
634
635/* no-op func for checking focus surface */
636static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600637focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100638{
639}
640
641static struct focus_surface *
642get_focus_surface(struct weston_surface *surface)
643{
644 if (surface->configure == focus_surface_configure)
645 return surface->configure_private;
646 else
647 return NULL;
648}
649
650static bool
651is_focus_surface (struct weston_surface *es)
652{
653 return (es->configure == focus_surface_configure);
654}
655
656static bool
657is_focus_view (struct weston_view *view)
658{
659 return is_focus_surface (view->surface);
660}
661
662static struct focus_surface *
663create_focus_surface(struct weston_compositor *ec,
664 struct weston_output *output)
665{
666 struct focus_surface *fsurf = NULL;
667 struct weston_surface *surface = NULL;
668
669 fsurf = malloc(sizeof *fsurf);
670 if (!fsurf)
671 return NULL;
672
673 fsurf->surface = weston_surface_create(ec);
674 surface = fsurf->surface;
675 if (surface == NULL) {
676 free(fsurf);
677 return NULL;
678 }
679
680 surface->configure = focus_surface_configure;
681 surface->output = output;
682 surface->configure_private = fsurf;
683
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800684 fsurf->view = weston_view_create(surface);
685 if (fsurf->view == NULL) {
686 weston_surface_destroy(surface);
687 free(fsurf);
688 return NULL;
689 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100690 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100691
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600692 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600693 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100694 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
695 pixman_region32_fini(&surface->opaque);
696 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
697 output->width, output->height);
698 pixman_region32_fini(&surface->input);
699 pixman_region32_init(&surface->input);
700
701 wl_list_init(&fsurf->workspace_transform.link);
702
703 return fsurf;
704}
705
706static void
707focus_surface_destroy(struct focus_surface *fsurf)
708{
709 weston_surface_destroy(fsurf->surface);
710 free(fsurf);
711}
712
713static void
714focus_animation_done(struct weston_view_animation *animation, void *data)
715{
716 struct workspace *ws = data;
717
718 ws->focus_animation = NULL;
719}
720
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200721static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200722focus_state_destroy(struct focus_state *state)
723{
724 wl_list_remove(&state->seat_destroy_listener.link);
725 wl_list_remove(&state->surface_destroy_listener.link);
726 free(state);
727}
728
729static void
730focus_state_seat_destroy(struct wl_listener *listener, void *data)
731{
732 struct focus_state *state = container_of(listener,
733 struct focus_state,
734 seat_destroy_listener);
735
736 wl_list_remove(&state->link);
737 focus_state_destroy(state);
738}
739
740static void
741focus_state_surface_destroy(struct wl_listener *listener, void *data)
742{
743 struct focus_state *state = container_of(listener,
744 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400745 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400746 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500747 struct weston_surface *main_surface, *next;
748 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200749
Pekka Paalanen01388e22013-04-25 13:57:44 +0300750 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
751
Kristian Høgsberge3778222012-07-31 17:29:30 -0400752 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300753 wl_list_for_each(view,
754 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500755 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400756 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100757 if (is_focus_view(view))
758 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400759
Jason Ekstranda7af7042013-10-12 22:38:11 -0500760 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400761 break;
762 }
763
Pekka Paalanen01388e22013-04-25 13:57:44 +0300764 /* if the focus was a sub-surface, activate its main surface */
765 if (main_surface != state->keyboard_focus)
766 next = main_surface;
767
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100768 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400769 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100770 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100771 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400772 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100773 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
774 if (state->ws->focus_animation)
775 weston_view_animation_destroy(state->ws->focus_animation);
776
777 state->ws->focus_animation = weston_fade_run(
778 state->ws->fsurf_front->view,
779 state->ws->fsurf_front->view->alpha, 0.0, 300,
780 focus_animation_done, state->ws);
781 }
782
Kristian Høgsberge3778222012-07-31 17:29:30 -0400783 wl_list_remove(&state->link);
784 focus_state_destroy(state);
785 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200786}
787
788static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400789focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200790{
Jonas Ådahl04769742012-06-13 00:01:24 +0200791 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200792
793 state = malloc(sizeof *state);
794 if (state == NULL)
795 return NULL;
796
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100797 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400798 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200799 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400800 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200801
802 state->seat_destroy_listener.notify = focus_state_seat_destroy;
803 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400804 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200805 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400806 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200807
808 return state;
809}
810
Jonas Ådahl8538b222012-08-29 22:13:03 +0200811static struct focus_state *
812ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
813{
814 struct workspace *ws = get_current_workspace(shell);
815 struct focus_state *state;
816
817 wl_list_for_each(state, &ws->focus_list, link)
818 if (state->seat == seat)
819 break;
820
821 if (&state->link == &ws->focus_list)
822 state = focus_state_create(seat, ws);
823
824 return state;
825}
826
Jonas Ådahl04769742012-06-13 00:01:24 +0200827static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800828focus_state_set_focus(struct focus_state *state,
829 struct weston_surface *surface)
830{
831 if (state->keyboard_focus) {
832 wl_list_remove(&state->surface_destroy_listener.link);
833 wl_list_init(&state->surface_destroy_listener.link);
834 }
835
836 state->keyboard_focus = surface;
837 if (surface)
838 wl_signal_add(&surface->destroy_signal,
839 &state->surface_destroy_listener);
840}
841
842static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400843restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200844{
845 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400846 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100847 struct wl_list pending_seat_list;
848 struct weston_seat *seat, *next_seat;
849
850 /* Temporarily steal the list of seats so that we can keep
851 * track of the seats we've already processed */
852 wl_list_init(&pending_seat_list);
853 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
854 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200855
856 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100857 wl_list_remove(&state->seat->link);
858 wl_list_insert(&shell->compositor->seat_list,
859 &state->seat->link);
860
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800861 if (state->seat->keyboard == NULL)
862 continue;
863
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400864 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200865
Kristian Høgsberge3148752013-05-06 23:19:49 -0400866 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200867 }
Neil Roberts4237f502014-04-09 16:33:31 +0100868
869 /* For any remaining seats that we don't have a focus state
870 * for we'll reset the keyboard focus to NULL */
871 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
872 wl_list_insert(&shell->compositor->seat_list, &seat->link);
873
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300874 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100875 continue;
876
877 weston_keyboard_set_focus(seat->keyboard, NULL);
878 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200879}
880
881static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200882replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
883 struct weston_seat *seat)
884{
885 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200886
887 wl_list_for_each(state, &ws->focus_list, link) {
888 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800889 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200890 return;
891 }
892 }
893}
894
895static void
896drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
897 struct weston_surface *surface)
898{
899 struct focus_state *state;
900
901 wl_list_for_each(state, &ws->focus_list, link)
902 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800903 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200904}
905
906static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100907animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
908 struct weston_view *from, struct weston_view *to)
909{
910 struct weston_output *output;
911 bool focus_surface_created = false;
912
913 /* FIXME: Only support dim animation using two layers */
914 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
915 return;
916
917 output = get_default_output(shell->compositor);
918 if (ws->fsurf_front == NULL && (from || to)) {
919 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800920 if (ws->fsurf_front == NULL)
921 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100922 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800923
924 ws->fsurf_back = create_focus_surface(shell->compositor, output);
925 if (ws->fsurf_back == NULL) {
926 focus_surface_destroy(ws->fsurf_front);
927 return;
928 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100929 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800930
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100931 focus_surface_created = true;
932 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300933 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
934 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100935 }
936
937 if (ws->focus_animation) {
938 weston_view_animation_destroy(ws->focus_animation);
939 ws->focus_animation = NULL;
940 }
941
942 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300943 weston_layer_entry_insert(&to->layer_link,
944 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100945 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300946 weston_layer_entry_insert(&ws->layer.view_list,
947 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100948
949 if (focus_surface_created) {
950 ws->focus_animation = weston_fade_run(
951 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200952 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100953 focus_animation_done, ws);
954 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300955 weston_layer_entry_insert(&from->layer_link,
956 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100957 ws->focus_animation = weston_stable_fade_run(
958 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200959 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100960 focus_animation_done, ws);
961 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300962 weston_layer_entry_insert(&ws->layer.view_list,
963 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100964 ws->focus_animation = weston_stable_fade_run(
965 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200966 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100967 focus_animation_done, ws);
968 }
969}
970
971static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200972workspace_destroy(struct workspace *ws)
973{
Jonas Ådahl04769742012-06-13 00:01:24 +0200974 struct focus_state *state, *next;
975
976 wl_list_for_each_safe(state, next, &ws->focus_list, link)
977 focus_state_destroy(state);
978
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100979 if (ws->fsurf_front)
980 focus_surface_destroy(ws->fsurf_front);
981 if (ws->fsurf_back)
982 focus_surface_destroy(ws->fsurf_back);
983
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200984 free(ws);
985}
986
Jonas Ådahl04769742012-06-13 00:01:24 +0200987static void
988seat_destroyed(struct wl_listener *listener, void *data)
989{
990 struct weston_seat *seat = data;
991 struct focus_state *state, *next;
992 struct workspace *ws = container_of(listener,
993 struct workspace,
994 seat_destroyed_listener);
995
996 wl_list_for_each_safe(state, next, &ws->focus_list, link)
997 if (state->seat == seat)
998 wl_list_remove(&state->link);
999}
1000
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001001static struct workspace *
1002workspace_create(void)
1003{
1004 struct workspace *ws = malloc(sizeof *ws);
1005 if (ws == NULL)
1006 return NULL;
1007
1008 weston_layer_init(&ws->layer, NULL);
1009
Jonas Ådahl04769742012-06-13 00:01:24 +02001010 wl_list_init(&ws->focus_list);
1011 wl_list_init(&ws->seat_destroyed_listener.link);
1012 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001013 ws->fsurf_front = NULL;
1014 ws->fsurf_back = NULL;
1015 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001016
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001017 return ws;
1018}
1019
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001020static int
1021workspace_is_empty(struct workspace *ws)
1022{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001023 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001024}
1025
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001026static struct workspace *
1027get_workspace(struct desktop_shell *shell, unsigned int index)
1028{
1029 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001030 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001031 pws += index;
1032 return *pws;
1033}
1034
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001035struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001036get_current_workspace(struct desktop_shell *shell)
1037{
1038 return get_workspace(shell, shell->workspaces.current);
1039}
1040
1041static void
1042activate_workspace(struct desktop_shell *shell, unsigned int index)
1043{
1044 struct workspace *ws;
1045
1046 ws = get_workspace(shell, index);
1047 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1048
1049 shell->workspaces.current = index;
1050}
1051
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001052static unsigned int
1053get_output_height(struct weston_output *output)
1054{
1055 return abs(output->region.extents.y1 - output->region.extents.y2);
1056}
1057
1058static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001059view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001060{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001061 struct weston_transform *transform;
1062
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001063 if (is_focus_view(view)) {
1064 struct focus_surface *fsurf = get_focus_surface(view->surface);
1065 transform = &fsurf->workspace_transform;
1066 } else {
1067 struct shell_surface *shsurf = get_shell_surface(view->surface);
1068 transform = &shsurf->workspace_transform;
1069 }
1070
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001071 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001072 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001073 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001074
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001075 weston_matrix_init(&transform->matrix);
1076 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001077 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001078 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001079}
1080
1081static void
1082workspace_translate_out(struct workspace *ws, double fraction)
1083{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001085 unsigned int height;
1086 double d;
1087
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001088 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001090 d = height * fraction;
1091
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001092 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001093 }
1094}
1095
1096static void
1097workspace_translate_in(struct workspace *ws, double fraction)
1098{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001099 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001100 unsigned int height;
1101 double d;
1102
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001103 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001104 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105
1106 if (fraction > 0)
1107 d = -(height - height * fraction);
1108 else
1109 d = height + height * fraction;
1110
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001111 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001112 }
1113}
1114
1115static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001116broadcast_current_workspace_state(struct desktop_shell *shell)
1117{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001118 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001119
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001120 wl_resource_for_each(resource, &shell->workspaces.client_list)
1121 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001122 shell->workspaces.current,
1123 shell->workspaces.num);
1124}
1125
1126static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127reverse_workspace_change_animation(struct desktop_shell *shell,
1128 unsigned int index,
1129 struct workspace *from,
1130 struct workspace *to)
1131{
1132 shell->workspaces.current = index;
1133
1134 shell->workspaces.anim_to = to;
1135 shell->workspaces.anim_from = from;
1136 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1137 shell->workspaces.anim_timestamp = 0;
1138
Scott Moreau4272e632012-08-13 09:58:41 -06001139 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001140}
1141
1142static void
1143workspace_deactivate_transforms(struct workspace *ws)
1144{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001146 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001147
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001148 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001149 if (is_focus_view(view)) {
1150 struct focus_surface *fsurf = get_focus_surface(view->surface);
1151 transform = &fsurf->workspace_transform;
1152 } else {
1153 struct shell_surface *shsurf = get_shell_surface(view->surface);
1154 transform = &shsurf->workspace_transform;
1155 }
1156
1157 if (!wl_list_empty(&transform->link)) {
1158 wl_list_remove(&transform->link);
1159 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001160 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001162 }
1163}
1164
1165static void
1166finish_workspace_change_animation(struct desktop_shell *shell,
1167 struct workspace *from,
1168 struct workspace *to)
1169{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001170 struct weston_view *view;
1171
Scott Moreau4272e632012-08-13 09:58:41 -06001172 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001173
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001174 /* Views that extend past the bottom of the output are still
1175 * visible after the workspace animation ends but before its layer
1176 * is hidden. In that case, we need to damage below those views so
1177 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001178 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001179 weston_view_damage_below(view);
1180
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001181 wl_list_remove(&shell->workspaces.animation.link);
1182 workspace_deactivate_transforms(from);
1183 workspace_deactivate_transforms(to);
1184 shell->workspaces.anim_to = NULL;
1185
1186 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1187}
1188
1189static void
1190animate_workspace_change_frame(struct weston_animation *animation,
1191 struct weston_output *output, uint32_t msecs)
1192{
1193 struct desktop_shell *shell =
1194 container_of(animation, struct desktop_shell,
1195 workspaces.animation);
1196 struct workspace *from = shell->workspaces.anim_from;
1197 struct workspace *to = shell->workspaces.anim_to;
1198 uint32_t t;
1199 double x, y;
1200
1201 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1202 finish_workspace_change_animation(shell, from, to);
1203 return;
1204 }
1205
1206 if (shell->workspaces.anim_timestamp == 0) {
1207 if (shell->workspaces.anim_current == 0.0)
1208 shell->workspaces.anim_timestamp = msecs;
1209 else
1210 shell->workspaces.anim_timestamp =
1211 msecs -
1212 /* Invers of movement function 'y' below. */
1213 (asin(1.0 - shell->workspaces.anim_current) *
1214 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1215 M_2_PI);
1216 }
1217
1218 t = msecs - shell->workspaces.anim_timestamp;
1219
1220 /*
1221 * x = [0, π/2]
1222 * y(x) = sin(x)
1223 */
1224 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1225 y = sin(x);
1226
1227 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001228 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001229
1230 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1231 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1232 shell->workspaces.anim_current = y;
1233
Scott Moreau4272e632012-08-13 09:58:41 -06001234 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001235 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001236 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001237 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001238}
1239
1240static void
1241animate_workspace_change(struct desktop_shell *shell,
1242 unsigned int index,
1243 struct workspace *from,
1244 struct workspace *to)
1245{
1246 struct weston_output *output;
1247
1248 int dir;
1249
1250 if (index > shell->workspaces.current)
1251 dir = -1;
1252 else
1253 dir = 1;
1254
1255 shell->workspaces.current = index;
1256
1257 shell->workspaces.anim_dir = dir;
1258 shell->workspaces.anim_from = from;
1259 shell->workspaces.anim_to = to;
1260 shell->workspaces.anim_current = 0.0;
1261 shell->workspaces.anim_timestamp = 0;
1262
1263 output = container_of(shell->compositor->output_list.next,
1264 struct weston_output, link);
1265 wl_list_insert(&output->animation_list,
1266 &shell->workspaces.animation.link);
1267
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001268 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001269
1270 workspace_translate_in(to, 0);
1271
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001272 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001273
Scott Moreau4272e632012-08-13 09:58:41 -06001274 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001275}
1276
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001277static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001278update_workspace(struct desktop_shell *shell, unsigned int index,
1279 struct workspace *from, struct workspace *to)
1280{
1281 shell->workspaces.current = index;
1282 wl_list_insert(&from->layer.link, &to->layer.link);
1283 wl_list_remove(&from->layer.link);
1284}
1285
1286static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001287change_workspace(struct desktop_shell *shell, unsigned int index)
1288{
1289 struct workspace *from;
1290 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001291 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001292
1293 if (index == shell->workspaces.current)
1294 return;
1295
1296 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001297 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001298 return;
1299
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001300 from = get_current_workspace(shell);
1301 to = get_workspace(shell, index);
1302
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001303 if (shell->workspaces.anim_from == to &&
1304 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001305 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001306 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001307 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001308 return;
1309 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001310
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001311 if (shell->workspaces.anim_to != NULL)
1312 finish_workspace_change_animation(shell,
1313 shell->workspaces.anim_from,
1314 shell->workspaces.anim_to);
1315
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001316 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001317
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001318 if (shell->focus_animation_type != ANIMATION_NONE) {
1319 wl_list_for_each(state, &from->focus_list, link)
1320 if (state->keyboard_focus)
1321 animate_focus_change(shell, from,
1322 get_default_view(state->keyboard_focus), NULL);
1323
1324 wl_list_for_each(state, &to->focus_list, link)
1325 if (state->keyboard_focus)
1326 animate_focus_change(shell, to,
1327 NULL, get_default_view(state->keyboard_focus));
1328 }
1329
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001330 if (workspace_is_empty(to) && workspace_is_empty(from))
1331 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001332 else
1333 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001334
1335 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001336}
1337
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001338static bool
1339workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1340{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001341 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001342 struct wl_list *e;
1343
1344 if (wl_list_empty(list))
1345 return false;
1346
1347 e = list->next;
1348
1349 if (e->next != list)
1350 return false;
1351
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001352 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001353}
1354
1355static void
Philip Withnall659163d2013-11-25 18:01:36 +00001356move_surface_to_workspace(struct desktop_shell *shell,
1357 struct shell_surface *shsurf,
1358 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001359{
1360 struct workspace *from;
1361 struct workspace *to;
1362 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001363 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001364 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001365
1366 if (workspace == shell->workspaces.current)
1367 return;
1368
Philip Withnall659163d2013-11-25 18:01:36 +00001369 view = get_default_view(shsurf->surface);
1370 if (!view)
1371 return;
1372
1373 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1374
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001375 if (workspace >= shell->workspaces.num)
1376 workspace = shell->workspaces.num - 1;
1377
Jonas Ådahle9d22502012-08-29 22:13:01 +02001378 from = get_current_workspace(shell);
1379 to = get_workspace(shell, workspace);
1380
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001381 weston_layer_entry_remove(&view->layer_link);
1382 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001383
Philip Withnall648a4dd2013-11-25 18:01:44 +00001384 shell_surface_update_child_surface_layers(shsurf);
1385
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001387 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1388 if (!seat->keyboard)
1389 continue;
1390
1391 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001393 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001394 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001395
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001397}
1398
1399static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001400take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001401 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001402 unsigned int index)
1403{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001404 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001406 struct shell_surface *shsurf;
1407 struct workspace *from;
1408 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001409 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001410
Pekka Paalanen01388e22013-04-25 13:57:44 +03001411 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412 view = get_default_view(surface);
1413 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001414 index == shell->workspaces.current ||
1415 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001416 return;
1417
1418 from = get_current_workspace(shell);
1419 to = get_workspace(shell, index);
1420
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001421 weston_layer_entry_remove(&view->layer_link);
1422 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001423
Philip Withnall659163d2013-11-25 18:01:36 +00001424 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001425 if (shsurf != NULL)
1426 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001427
Jonas Ådahle9d22502012-08-29 22:13:01 +02001428 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001429 drop_focus_state(shell, from, surface);
1430
1431 if (shell->workspaces.anim_from == to &&
1432 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001433 wl_list_remove(&to->layer.link);
1434 wl_list_insert(from->layer.link.prev, &to->layer.link);
1435
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001436 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001437 broadcast_current_workspace_state(shell);
1438
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001439 return;
1440 }
1441
1442 if (shell->workspaces.anim_to != NULL)
1443 finish_workspace_change_animation(shell,
1444 shell->workspaces.anim_from,
1445 shell->workspaces.anim_to);
1446
1447 if (workspace_is_empty(from) &&
1448 workspace_has_only(to, surface))
1449 update_workspace(shell, index, from, to);
1450 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001451 if (shsurf != NULL &&
1452 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001453 wl_list_insert(&shell->workspaces.anim_sticky_list,
1454 &shsurf->workspace_transform.link);
1455
1456 animate_workspace_change(shell, index, from, to);
1457 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001458
1459 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001460
1461 state = ensure_focus_state(shell, seat);
1462 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001463 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001464}
1465
1466static void
1467workspace_manager_move_surface(struct wl_client *client,
1468 struct wl_resource *resource,
1469 struct wl_resource *surface_resource,
1470 uint32_t workspace)
1471{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001472 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001473 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001474 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001475 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001476 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001477
Pekka Paalanen01388e22013-04-25 13:57:44 +03001478 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001479 shell_surface = get_shell_surface(main_surface);
1480 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001482
1483 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001484}
1485
1486static const struct workspace_manager_interface workspace_manager_implementation = {
1487 workspace_manager_move_surface,
1488};
1489
1490static void
1491unbind_resource(struct wl_resource *resource)
1492{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001493 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001494}
1495
1496static void
1497bind_workspace_manager(struct wl_client *client,
1498 void *data, uint32_t version, uint32_t id)
1499{
1500 struct desktop_shell *shell = data;
1501 struct wl_resource *resource;
1502
Jason Ekstranda85118c2013-06-27 20:17:02 -05001503 resource = wl_resource_create(client,
1504 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001505
1506 if (resource == NULL) {
1507 weston_log("couldn't add workspace manager object");
1508 return;
1509 }
1510
Jason Ekstranda85118c2013-06-27 20:17:02 -05001511 wl_resource_set_implementation(resource,
1512 &workspace_manager_implementation,
1513 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001514 wl_list_insert(&shell->workspaces.client_list,
1515 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001516
1517 workspace_manager_send_state(resource,
1518 shell->workspaces.current,
1519 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001520}
1521
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001522static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001523touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1524 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1525{
1526}
1527
1528static void
1529touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1530{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001531 struct weston_touch_move_grab *move =
1532 (struct weston_touch_move_grab *) container_of(
1533 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001534
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001535 if (touch_id == 0)
1536 move->active = 0;
1537
Jonas Ådahl9484b692013-12-02 22:05:03 +01001538 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001539 shell_touch_grab_end(&move->base);
1540 free(move);
1541 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001542}
1543
1544static void
1545touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1546 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1547{
1548 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1549 struct shell_surface *shsurf = move->base.shsurf;
1550 struct weston_surface *es;
1551 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1552 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1553
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001554 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001555 return;
1556
1557 es = shsurf->surface;
1558
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001559 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001560
1561 weston_compositor_schedule_repaint(es->compositor);
1562}
1563
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001564static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001565touch_move_grab_frame(struct weston_touch_grab *grab)
1566{
1567}
1568
1569static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001570touch_move_grab_cancel(struct weston_touch_grab *grab)
1571{
1572 struct weston_touch_move_grab *move =
1573 (struct weston_touch_move_grab *) container_of(
1574 grab, struct shell_touch_grab, grab);
1575
1576 shell_touch_grab_end(&move->base);
1577 free(move);
1578}
1579
Rusty Lynch1084da52013-08-15 09:10:08 -07001580static const struct weston_touch_grab_interface touch_move_grab_interface = {
1581 touch_move_grab_down,
1582 touch_move_grab_up,
1583 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001584 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001585 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001586};
1587
1588static int
1589surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1590{
1591 struct weston_touch_move_grab *move;
1592
1593 if (!shsurf)
1594 return -1;
1595
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001596 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001597 return 0;
1598
1599 move = malloc(sizeof *move);
1600 if (!move)
1601 return -1;
1602
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001603 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001604 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001605 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001607 seat->touch->grab_y;
1608
1609 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1610 seat->touch);
1611
1612 return 0;
1613}
1614
1615static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001616noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001617{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001618}
1619
1620static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001621constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1622{
1623 struct shell_surface *shsurf = move->base.shsurf;
1624 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001625 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001626 const int safety = 50;
1627
1628 x = wl_fixed_to_int(pointer->x + move->dx);
1629 y = wl_fixed_to_int(pointer->y + move->dy);
1630
Jonny Lambd73c6942014-08-20 15:53:20 +02001631 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1632 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1633 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001634
Jonny Lambd73c6942014-08-20 15:53:20 +02001635 bottom = y + shsurf->geometry.height;
1636 if (bottom - panel_height < safety)
1637 y = panel_height + safety -
1638 shsurf->geometry.height;
1639
1640 if (move->client_initiated &&
1641 y + shsurf->geometry.y < panel_height)
1642 y = panel_height - shsurf->geometry.y;
1643 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001644
1645 *cx = x;
1646 *cy = y;
1647}
1648
1649static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001650move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1651 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001652{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001653 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001654 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001655 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001656 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001657
1658 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001659 if (!shsurf)
1660 return;
1661
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001662 constrain_position(move, &cx, &cy);
1663
1664 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001665
Jason Ekstranda7af7042013-10-12 22:38:11 -05001666 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001667}
1668
1669static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001670move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001671 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001672{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001673 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1674 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001675 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001676 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001677
Daniel Stone4dbadb12012-05-30 16:31:51 +01001678 if (pointer->button_count == 0 &&
1679 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001680 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001681 free(grab);
1682 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001683}
1684
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001685static void
1686move_grab_cancel(struct weston_pointer_grab *grab)
1687{
1688 struct shell_grab *shell_grab =
1689 container_of(grab, struct shell_grab, grab);
1690
1691 shell_grab_end(shell_grab);
1692 free(grab);
1693}
1694
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001695static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001696 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001697 move_grab_motion,
1698 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001699 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001700};
1701
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001702static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001703surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1704 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001705{
1706 struct weston_move_grab *move;
1707
1708 if (!shsurf)
1709 return -1;
1710
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001711 if (shsurf->grabbed ||
1712 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713 return 0;
1714
1715 move = malloc(sizeof *move);
1716 if (!move)
1717 return -1;
1718
Jason Ekstranda7af7042013-10-12 22:38:11 -05001719 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001720 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001721 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001722 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001723 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001724
1725 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001726 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727
1728 return 0;
1729}
1730
1731static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001732common_surface_move(struct wl_resource *resource,
1733 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001734{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001735 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001736 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001737 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001738
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001739 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001740 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001741 seat->pointer->button_count > 0 &&
1742 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001744 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001745 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001746 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001747 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001748 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001749 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001751 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001752 (surface_touch_move(shsurf, seat) < 0))
1753 wl_resource_post_no_memory(resource);
1754 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001755}
1756
Rafael Antognollie2a34552013-12-03 15:35:45 -02001757static void
1758shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1759 struct wl_resource *seat_resource, uint32_t serial)
1760{
1761 common_surface_move(resource, seat_resource, serial);
1762}
1763
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001764struct weston_resize_grab {
1765 struct shell_grab base;
1766 uint32_t edges;
1767 int32_t width, height;
1768};
1769
1770static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001771resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1772 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001773{
1774 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001775 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001776 struct shell_surface *shsurf = resize->base.shsurf;
1777 int32_t width, height;
1778 wl_fixed_t from_x, from_y;
1779 wl_fixed_t to_x, to_y;
1780
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001781 weston_pointer_move(pointer, x, y);
1782
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001783 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001784 return;
1785
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 weston_view_from_global_fixed(shsurf->view,
1787 pointer->grab_x, pointer->grab_y,
1788 &from_x, &from_y);
1789 weston_view_from_global_fixed(shsurf->view,
1790 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001791
1792 width = resize->width;
1793 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1794 width += wl_fixed_to_int(from_x - to_x);
1795 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1796 width += wl_fixed_to_int(to_x - from_x);
1797 }
1798
1799 height = resize->height;
1800 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1801 height += wl_fixed_to_int(from_y - to_y);
1802 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1803 height += wl_fixed_to_int(to_y - from_y);
1804 }
1805
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001806 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001807}
1808
1809static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001810send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001811{
1812 struct shell_surface *shsurf = get_shell_surface(surface);
1813
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001814 assert(shsurf);
1815
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001816 if (shsurf->resource)
1817 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001818 shsurf->resize_edges,
1819 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001820}
1821
1822static const struct weston_shell_client shell_client = {
1823 send_configure
1824};
1825
1826static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001827resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001828 uint32_t time, uint32_t button, uint32_t state_w)
1829{
1830 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001831 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001832 enum wl_pointer_button_state state = state_w;
1833
1834 if (pointer->button_count == 0 &&
1835 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1836 shell_grab_end(&resize->base);
1837 free(grab);
1838 }
1839}
1840
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001841static void
1842resize_grab_cancel(struct weston_pointer_grab *grab)
1843{
1844 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1845
1846 shell_grab_end(&resize->base);
1847 free(grab);
1848}
1849
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001850static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001851 noop_grab_focus,
1852 resize_grab_motion,
1853 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001854 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001855};
1856
Giulio Camuffob8366642013-04-25 13:57:46 +03001857/*
1858 * Returns the bounding box of a surface and all its sub-surfaces,
1859 * in the surface coordinates system. */
1860static void
1861surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1862 int32_t *y, int32_t *w, int32_t *h) {
1863 pixman_region32_t region;
1864 pixman_box32_t *box;
1865 struct weston_subsurface *subsurface;
1866
1867 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001868 surface->width,
1869 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001870
1871 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1872 pixman_region32_union_rect(&region, &region,
1873 subsurface->position.x,
1874 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001875 subsurface->surface->width,
1876 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001877 }
1878
1879 box = pixman_region32_extents(&region);
1880 if (x)
1881 *x = box->x1;
1882 if (y)
1883 *y = box->y1;
1884 if (w)
1885 *w = box->x2 - box->x1;
1886 if (h)
1887 *h = box->y2 - box->y1;
1888
1889 pixman_region32_fini(&region);
1890}
1891
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001892static int
1893surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001894 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001895{
1896 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001897 const unsigned resize_topbottom =
1898 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1899 const unsigned resize_leftright =
1900 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1901 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001902
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001903 if (shsurf->grabbed ||
1904 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001905 return 0;
1906
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001907 /* Check for invalid edge combinations. */
1908 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1909 (edges & resize_topbottom) == resize_topbottom ||
1910 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001911 return 0;
1912
1913 resize = malloc(sizeof *resize);
1914 if (!resize)
1915 return -1;
1916
1917 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001918
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001919 resize->width = shsurf->geometry.width;
1920 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001921
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001922 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001923 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001924 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001925 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001926
1927 return 0;
1928}
1929
1930static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001931common_surface_resize(struct wl_resource *resource,
1932 struct wl_resource *seat_resource, uint32_t serial,
1933 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001934{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001935 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001936 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001937 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001938
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001939 if (seat->pointer == NULL ||
1940 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001941 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001942 seat->pointer->focus == NULL)
1943 return;
1944
1945 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1946 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001947 return;
1948
Kristian Høgsberge3148752013-05-06 23:19:49 -04001949 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001950 wl_resource_post_no_memory(resource);
1951}
1952
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001953static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001954shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1955 struct wl_resource *seat_resource, uint32_t serial,
1956 uint32_t edges)
1957{
1958 common_surface_resize(resource, seat_resource, serial, edges);
1959}
1960
1961static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001962busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001963{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001964 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001965 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001966 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001967 wl_fixed_t sx, sy;
1968
Jason Ekstranda7af7042013-10-12 22:38:11 -05001969 view = weston_compositor_pick_view(pointer->seat->compositor,
1970 pointer->x, pointer->y,
1971 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001972
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001973 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1974 shell_grab_end(grab);
1975 free(grab);
1976 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001977}
1978
1979static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001980busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1981 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001982{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001983 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001984}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001985
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001986static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001987busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001988 uint32_t time, uint32_t button, uint32_t state)
1989{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001990 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001991 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001992 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001993
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001994 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001995 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001996 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001997 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001998 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001999 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002000 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002001}
2002
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002003static void
2004busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2005{
2006 struct shell_grab *grab = (struct shell_grab *) base;
2007
2008 shell_grab_end(grab);
2009 free(grab);
2010}
2011
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002012static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002013 busy_cursor_grab_focus,
2014 busy_cursor_grab_motion,
2015 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002016 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002017};
2018
2019static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002020set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002021{
2022 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002023
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002024 if (pointer->grab->interface == &busy_cursor_grab_interface)
2025 return;
2026
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002027 grab = malloc(sizeof *grab);
2028 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002029 return;
2030
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002031 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2032 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002033 /* Mark the shsurf as ungrabbed so that button binding is able
2034 * to move it. */
2035 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002036}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002037
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002038static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002039end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002040{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002041 struct shell_grab *grab;
2042 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002043
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002044 wl_list_for_each(seat, &compositor->seat_list, link) {
2045 if (seat->pointer == NULL)
2046 continue;
2047
2048 grab = (struct shell_grab *) seat->pointer->grab;
2049 if (grab->grab.interface == &busy_cursor_grab_interface &&
2050 wl_resource_get_client(grab->shsurf->resource) == client) {
2051 shell_grab_end(grab);
2052 free(grab);
2053 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002054 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002055}
2056
Scott Moreau9521d5e2012-04-19 13:06:17 -06002057static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002058handle_shell_client_destroy(struct wl_listener *listener, void *data);
2059
2060static int
2061xdg_ping_timeout_handler(void *data)
2062{
2063 struct shell_client *sc = data;
2064 struct weston_seat *seat;
2065 struct shell_surface *shsurf;
2066
2067 /* Client is not responding */
2068 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002069 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2070 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2071 continue;
2072 if (seat->pointer->focus->surface->resource == NULL)
2073 continue;
2074
2075 shsurf = get_shell_surface(seat->pointer->focus->surface);
2076 if (shsurf &&
2077 wl_resource_get_client(shsurf->resource) == sc->client)
2078 set_busy_cursor(shsurf, seat->pointer);
2079 }
2080
2081 return 1;
2082}
2083
2084static void
2085handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2086{
2087 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002088 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002089 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002090 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002091
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002092 if (sc->unresponsive) {
2093 xdg_ping_timeout_handler(sc);
2094 return;
2095 }
2096
2097 sc->ping_serial = serial;
2098 loop = wl_display_get_event_loop(compositor->wl_display);
2099 if (sc->ping_timer == NULL)
2100 sc->ping_timer =
2101 wl_event_loop_add_timer(loop,
2102 xdg_ping_timeout_handler, sc);
2103 if (sc->ping_timer == NULL)
2104 return;
2105
2106 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2107
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002108 if (shell_surface_is_xdg_surface(shsurf) ||
2109 shell_surface_is_xdg_popup(shsurf))
2110 xdg_shell_send_ping(sc->resource, serial);
2111 else if (shell_surface_is_wl_shell_surface(shsurf))
2112 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002113}
2114
Scott Moreauff1db4a2012-04-17 19:06:18 -06002115static void
2116ping_handler(struct weston_surface *surface, uint32_t serial)
2117{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002118 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002119
2120 if (!shsurf)
2121 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002122 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002123 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002124 if (shsurf->surface == shsurf->shell->grab_surface)
2125 return;
2126
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002127 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002128}
2129
2130static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002131handle_pointer_focus(struct wl_listener *listener, void *data)
2132{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002133 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002134 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002135 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002136 uint32_t serial;
2137
Jason Ekstranda7af7042013-10-12 22:38:11 -05002138 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002139 return;
2140
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002142 serial = wl_display_next_serial(compositor->wl_display);
2143 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002144}
2145
2146static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002147shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2148{
2149 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002150 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002151}
2152
2153static void
2154shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2155{
2156 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002157 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002158}
2159
2160static void
2161handle_keyboard_focus(struct wl_listener *listener, void *data)
2162{
2163 struct weston_keyboard *keyboard = data;
2164 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2165
2166 if (seat->focused_surface) {
2167 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2168 if (shsurf)
2169 shell_surface_lose_keyboard_focus(shsurf);
2170 }
2171
2172 seat->focused_surface = keyboard->focus;
2173
2174 if (seat->focused_surface) {
2175 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2176 if (shsurf)
2177 shell_surface_gain_keyboard_focus(shsurf);
2178 }
2179}
2180
2181static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002182shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002183{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002184 if (sc->ping_serial != serial)
2185 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002186
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002187 sc->unresponsive = 0;
2188 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002189
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002190 if (sc->ping_timer) {
2191 wl_event_source_remove(sc->ping_timer);
2192 sc->ping_timer = NULL;
2193 }
2194
2195}
2196
2197static void
2198shell_surface_pong(struct wl_client *client,
2199 struct wl_resource *resource, uint32_t serial)
2200{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002201 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2202 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002203
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002204 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002205}
2206
2207static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002208set_title(struct shell_surface *shsurf, const char *title)
2209{
2210 free(shsurf->title);
2211 shsurf->title = strdup(title);
2212}
2213
2214static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002215set_window_geometry(struct shell_surface *shsurf,
2216 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002217{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002218 shsurf->next_geometry.x = x;
2219 shsurf->next_geometry.y = y;
2220 shsurf->next_geometry.width = width;
2221 shsurf->next_geometry.height = height;
2222 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002223}
2224
2225static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002226shell_surface_set_title(struct wl_client *client,
2227 struct wl_resource *resource, const char *title)
2228{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002229 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002230
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002231 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002232}
2233
2234static void
2235shell_surface_set_class(struct wl_client *client,
2236 struct wl_resource *resource, const char *class)
2237{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002238 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002239
2240 free(shsurf->class);
2241 shsurf->class = strdup(class);
2242}
2243
Alex Wu4539b082012-03-01 12:57:46 +08002244static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002245restore_output_mode(struct weston_output *output)
2246{
Hardening57388e42013-09-18 23:56:36 +02002247 if (output->current_mode != output->original_mode ||
2248 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002249 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002250 output->original_mode,
2251 output->original_scale,
2252 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002253}
2254
2255static void
2256restore_all_output_modes(struct weston_compositor *compositor)
2257{
2258 struct weston_output *output;
2259
2260 wl_list_for_each(output, &compositor->output_list, link)
2261 restore_output_mode(output);
2262}
2263
Philip Withnall07926d92013-11-25 18:01:40 +00002264/* The surface will be inserted into the list immediately after the link
2265 * returned by this function (i.e. will be stacked immediately above the
2266 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002267static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002268shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2269{
2270 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002271 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002272
2273 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002274 case SHELL_SURFACE_XWAYLAND:
2275 return &shsurf->shell->fullscreen_layer.view_list;
2276
2277 case SHELL_SURFACE_NONE:
2278 return NULL;
2279
Kristian Høgsbergead52422014-01-01 13:51:52 -08002280 case SHELL_SURFACE_POPUP:
2281 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002282 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002283 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002284 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002285 /* Move the surface to its parent layer so
2286 * that surfaces which are transient for
2287 * fullscreen surfaces don't get hidden by the
2288 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002289
2290 /* TODO: Handle a parent with multiple views */
2291 parent = get_default_view(shsurf->parent);
2292 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002293 return container_of(parent->layer_link.link.prev,
2294 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002295 }
Philip Withnall07926d92013-11-25 18:01:40 +00002296
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002297 /* Move the surface to a normal workspace layer so that surfaces
2298 * which were previously fullscreen or transient are no longer
2299 * rendered on top. */
2300 ws = get_current_workspace(shsurf->shell);
2301 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002302 }
2303
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002304 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002305}
2306
Philip Withnall648a4dd2013-11-25 18:01:44 +00002307static void
2308shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2309{
2310 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002311 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002312
2313 /* Move the child layers to the same workspace as shsurf. They will be
2314 * stacked above shsurf. */
2315 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002316 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002317 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002318 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002319 prev = container_of(shsurf->view->layer_link.link.prev,
2320 struct weston_layer_entry, link);
2321 weston_layer_entry_remove(&child->view->layer_link);
2322 weston_layer_entry_insert(prev,
2323 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002324 weston_view_geometry_dirty(child->view);
2325 weston_surface_damage(child->surface);
2326
2327 /* Recurse. We don’t expect this to recurse very far (if
2328 * at all) because that would imply we have transient
2329 * (or popup) children of transient surfaces, which
2330 * would be unusual. */
2331 shell_surface_update_child_surface_layers(child);
2332 }
2333 }
2334}
2335
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002336/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002337 * geometry to ensure the changes are redrawn.
2338 *
2339 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2340 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002341static void
2342shell_surface_update_layer(struct shell_surface *shsurf)
2343{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002344 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002345
2346 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2347
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002348 if (new_layer_link == NULL)
2349 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002350 if (new_layer_link == &shsurf->view->layer_link)
2351 return;
2352
2353 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002354 weston_layer_entry_remove(&shsurf->view->layer_link);
2355 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002356 weston_view_geometry_dirty(shsurf->view);
2357 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002358
2359 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002360}
2361
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002362static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002363shell_surface_set_parent(struct shell_surface *shsurf,
2364 struct weston_surface *parent)
2365{
2366 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002367
2368 wl_list_remove(&shsurf->children_link);
2369 wl_list_init(&shsurf->children_link);
2370
2371 /* Insert into the parent surface’s child list. */
2372 if (parent != NULL) {
2373 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2374 if (parent_shsurf != NULL)
2375 wl_list_insert(&parent_shsurf->children_list,
2376 &shsurf->children_link);
2377 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002378}
2379
2380static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002381shell_surface_set_output(struct shell_surface *shsurf,
2382 struct weston_output *output)
2383{
2384 struct weston_surface *es = shsurf->surface;
2385
2386 /* get the default output, if the client set it as NULL
2387 check whether the ouput is available */
2388 if (output)
2389 shsurf->output = output;
2390 else if (es->output)
2391 shsurf->output = es->output;
2392 else
2393 shsurf->output = get_default_output(es->compositor);
2394}
2395
2396static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002397surface_clear_next_states(struct shell_surface *shsurf)
2398{
2399 shsurf->next_state.maximized = false;
2400 shsurf->next_state.fullscreen = false;
2401
2402 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2403 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2404 shsurf->state_changed = true;
2405}
2406
2407static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002408set_toplevel(struct shell_surface *shsurf)
2409{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002410 shell_surface_set_parent(shsurf, NULL);
2411 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002412 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002413
2414 /* The layer_link is updated in set_surface_type(),
2415 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002416}
2417
2418static void
2419shell_surface_set_toplevel(struct wl_client *client,
2420 struct wl_resource *resource)
2421{
2422 struct shell_surface *surface = wl_resource_get_user_data(resource);
2423
2424 set_toplevel(surface);
2425}
2426
2427static void
2428set_transient(struct shell_surface *shsurf,
2429 struct weston_surface *parent, int x, int y, uint32_t flags)
2430{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002431 assert(parent != NULL);
2432
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002433 shell_surface_set_parent(shsurf, parent);
2434
2435 surface_clear_next_states(shsurf);
2436
Philip Withnallbecb77e2013-11-25 18:01:30 +00002437 shsurf->transient.x = x;
2438 shsurf->transient.y = y;
2439 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002440
Rafael Antognollied207b42013-12-03 15:35:43 -02002441 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002442 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002443 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002444
2445 /* The layer_link is updated in set_surface_type(),
2446 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002447}
2448
2449static void
2450shell_surface_set_transient(struct wl_client *client,
2451 struct wl_resource *resource,
2452 struct wl_resource *parent_resource,
2453 int x, int y, uint32_t flags)
2454{
2455 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2456 struct weston_surface *parent =
2457 wl_resource_get_user_data(parent_resource);
2458
2459 set_transient(shsurf, parent, x, y, flags);
2460}
2461
2462static void
2463set_fullscreen(struct shell_surface *shsurf,
2464 uint32_t method,
2465 uint32_t framerate,
2466 struct weston_output *output)
2467{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002468 shell_surface_set_output(shsurf, output);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002469 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002470
2471 shsurf->fullscreen_output = shsurf->output;
2472 shsurf->fullscreen.type = method;
2473 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002474
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002475 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002476}
2477
2478static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002479weston_view_set_initial_position(struct weston_view *view,
2480 struct desktop_shell *shell);
2481
2482static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002483unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002484{
Philip Withnallf85fe842013-11-25 18:01:37 +00002485 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002486 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2487 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002488 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002489 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002490
Alex Wu4539b082012-03-01 12:57:46 +08002491 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2492 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002493
Alex Wu4539b082012-03-01 12:57:46 +08002494 wl_list_remove(&shsurf->fullscreen.transform.link);
2495 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002496
Jason Ekstranda7af7042013-10-12 22:38:11 -05002497 if (shsurf->fullscreen.black_view)
2498 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2499 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002500
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002501 if (shsurf->saved_position_valid)
2502 weston_view_set_position(shsurf->view,
2503 shsurf->saved_x, shsurf->saved_y);
2504 else
2505 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2506
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002507 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002508 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002509 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002510 shsurf->saved_rotation_valid = false;
2511 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002512
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002513 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002514}
2515
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002516static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002517shell_surface_set_fullscreen(struct wl_client *client,
2518 struct wl_resource *resource,
2519 uint32_t method,
2520 uint32_t framerate,
2521 struct wl_resource *output_resource)
2522{
2523 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2524 struct weston_output *output;
2525
2526 if (output_resource)
2527 output = wl_resource_get_user_data(output_resource);
2528 else
2529 output = NULL;
2530
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002531 shell_surface_set_parent(shsurf, NULL);
2532
Rafael Antognolli03b16592013-12-03 15:35:42 -02002533 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002534 shsurf->next_state.fullscreen = true;
2535 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002536 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002537}
2538
2539static void
2540set_popup(struct shell_surface *shsurf,
2541 struct weston_surface *parent,
2542 struct weston_seat *seat,
2543 uint32_t serial,
2544 int32_t x,
2545 int32_t y)
2546{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002547 assert(parent != NULL);
2548
Philip Withnallbecb77e2013-11-25 18:01:30 +00002549 shsurf->popup.shseat = get_shell_seat(seat);
2550 shsurf->popup.serial = serial;
2551 shsurf->popup.x = x;
2552 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002553
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002554 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002555}
2556
2557static void
2558shell_surface_set_popup(struct wl_client *client,
2559 struct wl_resource *resource,
2560 struct wl_resource *seat_resource,
2561 uint32_t serial,
2562 struct wl_resource *parent_resource,
2563 int32_t x, int32_t y, uint32_t flags)
2564{
2565 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002566 struct weston_surface *parent =
2567 wl_resource_get_user_data(parent_resource);
2568
2569 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002570
Rafael Antognolli03b16592013-12-03 15:35:42 -02002571 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002572 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002573 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002574 wl_resource_get_user_data(seat_resource),
2575 serial, x, y);
2576}
2577
2578static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002579unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002580{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002581 /* undo all maximized things here */
2582 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002583
2584 if (shsurf->saved_position_valid)
2585 weston_view_set_position(shsurf->view,
2586 shsurf->saved_x, shsurf->saved_y);
2587 else
2588 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002589
2590 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002591 wl_list_insert(&shsurf->view->geometry.transformation_list,
2592 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002593 shsurf->saved_rotation_valid = false;
2594 }
2595
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002596 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002597}
2598
Philip Withnallbecb77e2013-11-25 18:01:30 +00002599static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002600set_minimized(struct weston_surface *surface, uint32_t is_true)
2601{
2602 struct shell_surface *shsurf;
2603 struct workspace *current_ws;
2604 struct weston_seat *seat;
2605 struct weston_surface *focus;
2606 struct weston_view *view;
2607
2608 view = get_default_view(surface);
2609 if (!view)
2610 return;
2611
2612 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2613
2614 shsurf = get_shell_surface(surface);
2615 current_ws = get_current_workspace(shsurf->shell);
2616
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002617 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002618 /* hide or show, depending on the state */
2619 if (is_true) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002620 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002621
2622 drop_focus_state(shsurf->shell, current_ws, view->surface);
2623 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2624 if (!seat->keyboard)
2625 continue;
2626 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2627 if (focus == view->surface)
2628 weston_keyboard_set_focus(seat->keyboard, NULL);
2629 }
2630 }
2631 else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002632 weston_layer_entry_insert(&current_ws->layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002633
2634 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2635 if (!seat->keyboard)
2636 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002637 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002638 }
2639 }
2640
2641 shell_surface_update_child_surface_layers(shsurf);
2642
2643 weston_view_damage_below(view);
2644}
2645
2646static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002647shell_surface_set_maximized(struct wl_client *client,
2648 struct wl_resource *resource,
2649 struct wl_resource *output_resource)
2650{
2651 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2652 struct weston_output *output;
2653
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002654 surface_clear_next_states(shsurf);
2655 shsurf->next_state.maximized = true;
2656 shsurf->state_changed = true;
2657
2658 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2659 shell_surface_set_parent(shsurf, NULL);
2660
Philip Withnallbecb77e2013-11-25 18:01:30 +00002661 if (output_resource)
2662 output = wl_resource_get_user_data(output_resource);
2663 else
2664 output = NULL;
2665
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002666 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002667
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002668 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002669}
2670
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002671/* This is only ever called from set_surface_type(), so there’s no need to
2672 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002673static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002674reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002675{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002676 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002677 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002678 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002679 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002680
Pekka Paalanen98262232011-12-01 10:42:22 +02002681 return 0;
2682}
2683
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002684static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002685set_full_output(struct shell_surface *shsurf)
2686{
2687 shsurf->saved_x = shsurf->view->geometry.x;
2688 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002689 shsurf->saved_width = shsurf->surface->width;
2690 shsurf->saved_height = shsurf->surface->height;
2691 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002692 shsurf->saved_position_valid = true;
2693
2694 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2695 wl_list_remove(&shsurf->rotation.transform.link);
2696 wl_list_init(&shsurf->rotation.transform.link);
2697 weston_view_geometry_dirty(shsurf->view);
2698 shsurf->saved_rotation_valid = true;
2699 }
2700}
2701
2702static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002703set_surface_type(struct shell_surface *shsurf)
2704{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002705 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002706 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002707
Philip Withnallbecb77e2013-11-25 18:01:30 +00002708 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002709
Rafael Antognolli03b16592013-12-03 15:35:42 -02002710 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002711 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002712
2713 switch (shsurf->type) {
2714 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002715 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002716 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002717 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002718 weston_view_set_position(shsurf->view,
2719 pev->geometry.x + shsurf->transient.x,
2720 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002721 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002722 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002723
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002724 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002725 weston_view_set_position(shsurf->view, shsurf->transient.x,
2726 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002727 break;
2728
Philip Withnall0f640e22013-11-25 18:01:31 +00002729 case SHELL_SURFACE_POPUP:
2730 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002731 default:
2732 break;
2733 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002734
2735 /* Update the surface’s layer. */
2736 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002737}
2738
Tiago Vignattibe143262012-04-16 17:31:41 +03002739static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002740shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002741{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002742 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002743}
2744
Alex Wu21858432012-04-01 20:13:08 +08002745static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002746black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002747
Jason Ekstranda7af7042013-10-12 22:38:11 -05002748static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002749create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002750 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002751 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002752{
2753 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002754 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002755
2756 surface = weston_surface_create(ec);
2757 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002758 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002759 return NULL;
2760 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002761 view = weston_view_create(surface);
2762 if (surface == NULL) {
2763 weston_log("no memory\n");
2764 weston_surface_destroy(surface);
2765 return NULL;
2766 }
Alex Wu4539b082012-03-01 12:57:46 +08002767
Alex Wu21858432012-04-01 20:13:08 +08002768 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002769 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002770 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002771 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002772 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002773 pixman_region32_fini(&surface->input);
2774 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002775
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002776 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002777 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002778
2779 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002780}
2781
Philip Withnalled8f1a92013-11-25 18:01:43 +00002782static void
2783shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2784{
2785 struct weston_output *output = shsurf->fullscreen_output;
2786
Rafael Antognolli03b16592013-12-03 15:35:42 -02002787 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002788
2789 if (!shsurf->fullscreen.black_view)
2790 shsurf->fullscreen.black_view =
2791 create_black_surface(shsurf->surface->compositor,
2792 shsurf->surface,
2793 output->x, output->y,
2794 output->width,
2795 output->height);
2796
2797 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002798 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2799 weston_layer_entry_insert(&shsurf->view->layer_link,
2800 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002801 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2802 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002803
2804 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002805}
2806
Alex Wu4539b082012-03-01 12:57:46 +08002807/* Create black surface and append it to the associated fullscreen surface.
2808 * Handle size dismatch and positioning according to the method. */
2809static void
2810shell_configure_fullscreen(struct shell_surface *shsurf)
2811{
2812 struct weston_output *output = shsurf->fullscreen_output;
2813 struct weston_surface *surface = shsurf->surface;
2814 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002815 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002816 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002817
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002818 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2819 restore_output_mode(output);
2820
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002821 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002822 weston_layer_entry_remove(&shsurf->view->layer_link);
2823 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002824
Philip Withnalled8f1a92013-11-25 18:01:43 +00002825 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002826
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002828 &surf_width, &surf_height);
2829
Alex Wu4539b082012-03-01 12:57:46 +08002830 switch (shsurf->fullscreen.type) {
2831 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002832 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002833 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002834 break;
2835 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002836 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002837 if (output->width == surf_width &&
2838 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002839 weston_view_set_position(shsurf->view,
2840 output->x - surf_x,
2841 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002842 break;
2843 }
2844
Alex Wu4539b082012-03-01 12:57:46 +08002845 matrix = &shsurf->fullscreen.transform.matrix;
2846 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002847
Scott Moreau1bad5db2012-08-18 01:04:05 -06002848 output_aspect = (float) output->width /
2849 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002850 /* XXX: Use surf_width and surf_height here? */
2851 surface_aspect = (float) surface->width /
2852 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002853 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002854 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002855 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002856 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002857 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002858 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002859
Alex Wu4539b082012-03-01 12:57:46 +08002860 weston_matrix_scale(matrix, scale, scale, 1);
2861 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002862 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002863 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002864 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2865 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002866 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002867
Alex Wu4539b082012-03-01 12:57:46 +08002868 break;
2869 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002870 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002871 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002872 surf_width * surface->buffer_viewport.buffer.scale,
2873 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002874 shsurf->fullscreen.framerate};
2875
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002876 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002877 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002878 weston_view_set_position(shsurf->view,
2879 output->x - surf_x,
2880 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002881 shsurf->fullscreen.black_view->surface->width = output->width;
2882 shsurf->fullscreen.black_view->surface->height = output->height;
2883 weston_view_set_position(shsurf->fullscreen.black_view,
2884 output->x - surf_x,
2885 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002886 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002887 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002888 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002889 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002890 }
Alex Wubd3354b2012-04-17 17:20:49 +08002891 }
Alex Wu4539b082012-03-01 12:57:46 +08002892 break;
2893 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002894 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002895 break;
2896 default:
2897 break;
2898 }
2899}
2900
Alex Wu4539b082012-03-01 12:57:46 +08002901static void
2902shell_map_fullscreen(struct shell_surface *shsurf)
2903{
Alex Wubd3354b2012-04-17 17:20:49 +08002904 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002905}
2906
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002907static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002908set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2909{
2910 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002911 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002912 shsurf->transient.x = x;
2913 shsurf->transient.y = y;
2914 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002915
2916 shell_surface_set_parent(shsurf, NULL);
2917
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002918 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002919 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002920}
2921
Kristian Høgsberg47792412014-05-04 13:47:06 -07002922static void
2923shell_interface_set_fullscreen(struct shell_surface *shsurf,
2924 uint32_t method,
2925 uint32_t framerate,
2926 struct weston_output *output)
2927{
2928 surface_clear_next_states(shsurf);
2929 set_fullscreen(shsurf, method, framerate, output);
2930
2931 shsurf->next_state.fullscreen = true;
2932 shsurf->state_changed = true;
2933}
2934
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002935static int
2936shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2937{
2938 return surface_move(shsurf, ws, 1);
2939}
2940
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002941static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002942
2943static void
2944destroy_shell_seat(struct wl_listener *listener, void *data)
2945{
2946 struct shell_seat *shseat =
2947 container_of(listener,
2948 struct shell_seat, seat_destroy_listener);
2949 struct shell_surface *shsurf, *prev = NULL;
2950
2951 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002952 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002953 shseat->popup_grab.client = NULL;
2954
2955 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2956 shsurf->popup.shseat = NULL;
2957 if (prev) {
2958 wl_list_init(&prev->popup.grab_link);
2959 }
2960 prev = shsurf;
2961 }
2962 wl_list_init(&prev->popup.grab_link);
2963 }
2964
2965 wl_list_remove(&shseat->seat_destroy_listener.link);
2966 free(shseat);
2967}
2968
Jason Ekstrand024177c2014-04-21 19:42:58 -05002969static void
2970shell_seat_caps_changed(struct wl_listener *listener, void *data)
2971{
2972 struct shell_seat *seat;
2973
2974 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2975
2976 if (seat->seat->keyboard &&
2977 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2978 wl_signal_add(&seat->seat->keyboard->focus_signal,
2979 &seat->keyboard_focus_listener);
2980 } else if (!seat->seat->keyboard) {
2981 wl_list_init(&seat->keyboard_focus_listener.link);
2982 }
2983
2984 if (seat->seat->pointer &&
2985 wl_list_empty(&seat->pointer_focus_listener.link)) {
2986 wl_signal_add(&seat->seat->pointer->focus_signal,
2987 &seat->pointer_focus_listener);
2988 } else if (!seat->seat->pointer) {
2989 wl_list_init(&seat->pointer_focus_listener.link);
2990 }
2991}
2992
Giulio Camuffo5085a752013-03-25 21:42:45 +01002993static struct shell_seat *
2994create_shell_seat(struct weston_seat *seat)
2995{
2996 struct shell_seat *shseat;
2997
2998 shseat = calloc(1, sizeof *shseat);
2999 if (!shseat) {
3000 weston_log("no memory to allocate shell seat\n");
3001 return NULL;
3002 }
3003
3004 shseat->seat = seat;
3005 wl_list_init(&shseat->popup_grab.surfaces_list);
3006
3007 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3008 wl_signal_add(&seat->destroy_signal,
3009 &shseat->seat_destroy_listener);
3010
Jason Ekstrand024177c2014-04-21 19:42:58 -05003011 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3012 wl_list_init(&shseat->keyboard_focus_listener.link);
3013
3014 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3015 wl_list_init(&shseat->pointer_focus_listener.link);
3016
3017 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3018 wl_signal_add(&seat->updated_caps_signal,
3019 &shseat->caps_changed_listener);
3020 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3021
Giulio Camuffo5085a752013-03-25 21:42:45 +01003022 return shseat;
3023}
3024
3025static struct shell_seat *
3026get_shell_seat(struct weston_seat *seat)
3027{
3028 struct wl_listener *listener;
3029
3030 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003031 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003032
3033 return container_of(listener,
3034 struct shell_seat, seat_destroy_listener);
3035}
3036
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003037static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003038popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003039{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003040 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003041 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003042 struct shell_seat *shseat =
3043 container_of(grab, struct shell_seat, popup_grab.grab);
3044 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003045 wl_fixed_t sx, sy;
3046
Jason Ekstranda7af7042013-10-12 22:38:11 -05003047 view = weston_compositor_pick_view(pointer->seat->compositor,
3048 pointer->x, pointer->y,
3049 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003050
Jason Ekstranda7af7042013-10-12 22:38:11 -05003051 if (view && view->surface->resource &&
3052 wl_resource_get_client(view->surface->resource) == client) {
3053 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003054 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003055 weston_pointer_set_focus(pointer, NULL,
3056 wl_fixed_from_int(0),
3057 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003058 }
3059}
3060
3061static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003062popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3063 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003064{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003065 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003066 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003067 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003068
Jonas Ådahl61917c82014-08-11 22:44:02 +02003069 if (pointer->focus) {
3070 weston_view_from_global_fixed(pointer->focus, x, y,
3071 &pointer->sx, &pointer->sy);
3072 }
3073
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003074 weston_pointer_move(pointer, x, y);
3075
Neil Roberts96d790e2013-09-19 17:32:00 +01003076 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003077 weston_view_from_global_fixed(pointer->focus,
3078 pointer->x, pointer->y,
3079 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003080 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003081 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003082}
3083
3084static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003085popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003086 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003087{
3088 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003089 struct shell_seat *shseat =
3090 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003091 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003092 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003093 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003094 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003095
Neil Roberts96d790e2013-09-19 17:32:00 +01003096 resource_list = &grab->pointer->focus_resource_list;
3097 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003098 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003099 wl_resource_for_each(resource, resource_list) {
3100 wl_pointer_send_button(resource, serial,
3101 time, button, state);
3102 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003103 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003104 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003105 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003106 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003107 }
3108
Daniel Stone4dbadb12012-05-30 16:31:51 +01003109 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003110 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003111}
3112
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003113static void
3114popup_grab_cancel(struct weston_pointer_grab *grab)
3115{
3116 popup_grab_end(grab->pointer);
3117}
3118
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003119static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003120 popup_grab_focus,
3121 popup_grab_motion,
3122 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003123 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003124};
3125
3126static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003127touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3128 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3129{
3130 struct wl_resource *resource;
3131 struct shell_seat *shseat =
3132 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3133 struct wl_display *display = shseat->seat->compositor->wl_display;
3134 uint32_t serial;
3135 struct wl_list *resource_list;
3136
3137 resource_list = &grab->touch->focus_resource_list;
3138 if (!wl_list_empty(resource_list)) {
3139 serial = wl_display_get_serial(display);
3140 wl_resource_for_each(resource, resource_list) {
3141 wl_touch_send_down(resource, serial, time,
3142 grab->touch->focus->surface->resource,
3143 touch_id, sx, sy);
3144 }
3145 }
3146}
3147
3148static void
3149touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3150{
3151 struct wl_resource *resource;
3152 struct shell_seat *shseat =
3153 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3154 struct wl_display *display = shseat->seat->compositor->wl_display;
3155 uint32_t serial;
3156 struct wl_list *resource_list;
3157
3158 resource_list = &grab->touch->focus_resource_list;
3159 if (!wl_list_empty(resource_list)) {
3160 serial = wl_display_get_serial(display);
3161 wl_resource_for_each(resource, resource_list) {
3162 wl_touch_send_up(resource, serial, time, touch_id);
3163 }
3164 }
3165}
3166
3167static void
3168touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3169 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3170{
3171 struct wl_resource *resource;
3172 struct wl_list *resource_list;
3173
3174 resource_list = &grab->touch->focus_resource_list;
3175 if (!wl_list_empty(resource_list)) {
3176 wl_resource_for_each(resource, resource_list) {
3177 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3178 }
3179 }
3180}
3181
3182static void
3183touch_popup_grab_frame(struct weston_touch_grab *grab)
3184{
3185}
3186
3187static void
3188touch_popup_grab_cancel(struct weston_touch_grab *grab)
3189{
3190 touch_popup_grab_end(grab->touch);
3191}
3192
3193static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3194 touch_popup_grab_down,
3195 touch_popup_grab_up,
3196 touch_popup_grab_motion,
3197 touch_popup_grab_frame,
3198 touch_popup_grab_cancel,
3199};
3200
3201static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003202shell_surface_send_popup_done(struct shell_surface *shsurf)
3203{
3204 if (shell_surface_is_wl_shell_surface(shsurf))
3205 wl_shell_surface_send_popup_done(shsurf->resource);
3206 else if (shell_surface_is_xdg_popup(shsurf))
3207 xdg_popup_send_popup_done(shsurf->resource,
3208 shsurf->popup.serial);
3209}
3210
3211static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003212popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003213{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003214 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003215 struct shell_seat *shseat =
3216 container_of(grab, struct shell_seat, popup_grab.grab);
3217 struct shell_surface *shsurf;
3218 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003219
3220 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003221 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003222 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003223 shseat->popup_grab.grab.interface = NULL;
3224 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003225 /* Send the popup_done event to all the popups open */
3226 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003227 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003228 shsurf->popup.shseat = NULL;
3229 if (prev) {
3230 wl_list_init(&prev->popup.grab_link);
3231 }
3232 prev = shsurf;
3233 }
3234 wl_list_init(&prev->popup.grab_link);
3235 wl_list_init(&shseat->popup_grab.surfaces_list);
3236 }
3237}
3238
3239static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003240touch_popup_grab_end(struct weston_touch *touch)
3241{
3242 struct weston_touch_grab *grab = touch->grab;
3243 struct shell_seat *shseat =
3244 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3245 struct shell_surface *shsurf;
3246 struct shell_surface *prev = NULL;
3247
3248 if (touch->grab->interface == &touch_popup_grab_interface) {
3249 weston_touch_end_grab(grab->touch);
3250 shseat->popup_grab.client = NULL;
3251 shseat->popup_grab.touch_grab.interface = NULL;
3252 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3253 /* Send the popup_done event to all the popups open */
3254 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
3255 shell_surface_send_popup_done(shsurf);
3256 shsurf->popup.shseat = NULL;
3257 if (prev) {
3258 wl_list_init(&prev->popup.grab_link);
3259 }
3260 prev = shsurf;
3261 }
3262 wl_list_init(&prev->popup.grab_link);
3263 wl_list_init(&shseat->popup_grab.surfaces_list);
3264 }
3265}
3266
3267static void
3268add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat, int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003269{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003270 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003271
3272 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003273 shseat->popup_grab.type = type;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003274 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003275
3276 if (type == POINTER) {
3277 shseat->popup_grab.grab.interface = &popup_grab_interface;
3278 /* We must make sure here that this popup was opened after
3279 * a mouse press, and not just by moving around with other
3280 * popups already open. */
3281 if (shseat->seat->pointer->button_count > 0)
3282 shseat->popup_grab.initial_up = 0;
3283 } else if (type == TOUCH) {
3284 shseat->popup_grab.touch_grab.interface = &touch_popup_grab_interface;
3285 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003286
Rob Bradforddfe31052013-06-26 19:49:11 +01003287 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003288
3289 if (type == POINTER)
3290 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
3291 else if (type == TOUCH)
3292 weston_touch_start_grab(seat->touch, &shseat->popup_grab.touch_grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003293 } else {
3294 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003295 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003296}
3297
3298static void
3299remove_popup_grab(struct shell_surface *shsurf)
3300{
3301 struct shell_seat *shseat = shsurf->popup.shseat;
3302
3303 wl_list_remove(&shsurf->popup.grab_link);
3304 wl_list_init(&shsurf->popup.grab_link);
3305 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003306 if (shseat->popup_grab.type == POINTER) {
3307 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3308 shseat->popup_grab.grab.interface = NULL;
3309 } else if (shseat->popup_grab.type == TOUCH) {
3310 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3311 shseat->popup_grab.touch_grab.interface = NULL;
3312 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003313 }
3314}
3315
3316static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003317shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003318{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003319 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003320 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003321
Jason Ekstranda7af7042013-10-12 22:38:11 -05003322 shsurf->surface->output = parent_view->output;
3323 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003324
Jason Ekstranda7af7042013-10-12 22:38:11 -05003325 weston_view_set_transform_parent(shsurf->view, parent_view);
3326 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3327 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003328
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003329 if (shseat->seat->pointer &&
3330 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003331 add_popup_grab(shsurf, shseat, POINTER);
3332 } else if (shseat->seat->touch &&
3333 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
3334 add_popup_grab(shsurf, shseat, TOUCH);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003335 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003336 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003337 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003338 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003339}
3340
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003341static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003342 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003343 shell_surface_move,
3344 shell_surface_resize,
3345 shell_surface_set_toplevel,
3346 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003347 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003348 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003349 shell_surface_set_maximized,
3350 shell_surface_set_title,
3351 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003352};
3353
3354static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003355destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003356{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003357 struct shell_surface *child, *next;
3358
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003359 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3360
Giulio Camuffo5085a752013-03-25 21:42:45 +01003361 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3362 remove_popup_grab(shsurf);
3363 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003364
Alex Wubd3354b2012-04-17 17:20:49 +08003365 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003366 shell_surface_is_top_fullscreen(shsurf))
3367 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003368
Jason Ekstranda7af7042013-10-12 22:38:11 -05003369 if (shsurf->fullscreen.black_view)
3370 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003371
Alex Wubd3354b2012-04-17 17:20:49 +08003372 /* As destroy_resource() use wl_list_for_each_safe(),
3373 * we can always remove the listener.
3374 */
3375 wl_list_remove(&shsurf->surface_destroy_listener.link);
3376 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003377 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003378
Jason Ekstranda7af7042013-10-12 22:38:11 -05003379 weston_view_destroy(shsurf->view);
3380
Philip Withnall648a4dd2013-11-25 18:01:44 +00003381 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003382 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3383 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003384
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003385 wl_list_remove(&shsurf->link);
3386 free(shsurf);
3387}
3388
3389static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003390shell_destroy_shell_surface(struct wl_resource *resource)
3391{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003392 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003393
Bryan Caina46b9462014-04-04 17:41:24 -05003394 if (!wl_list_empty(&shsurf->popup.grab_link))
3395 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003396 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003397}
3398
3399static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003400shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003401{
3402 struct shell_surface *shsurf = container_of(listener,
3403 struct shell_surface,
3404 surface_destroy_listener);
3405
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003406 if (shsurf->resource)
3407 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003408
3409 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003410}
3411
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003412static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003413fade_out_done(struct weston_view_animation *animation, void *data)
3414{
3415 struct shell_surface *shsurf = data;
3416
3417 weston_surface_destroy(shsurf->surface);
3418}
3419
3420static void
3421handle_resource_destroy(struct wl_listener *listener, void *data)
3422{
3423 struct shell_surface *shsurf =
3424 container_of(listener, struct shell_surface,
3425 resource_destroy_listener);
3426
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003427 if (!weston_surface_is_mapped(shsurf->surface))
3428 return;
3429
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003430 shsurf->surface->ref_count++;
3431
3432 pixman_region32_fini(&shsurf->surface->pending.input);
3433 pixman_region32_init(&shsurf->surface->pending.input);
3434 pixman_region32_fini(&shsurf->surface->input);
3435 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003436 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3437 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3438 fade_out_done, shsurf);
3439 } else {
3440 weston_surface_destroy(shsurf->surface);
3441 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003442}
3443
3444static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003445shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003446
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003447struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003448get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003449{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003450 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003451 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003452 else
3453 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003454}
3455
Rafael Antognollie2a34552013-12-03 15:35:45 -02003456static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003457create_common_surface(struct shell_client *owner, void *shell,
3458 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003459 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003460{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003461 struct shell_surface *shsurf;
3462
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003463 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003464 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003465 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003466 }
3467
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003468 shsurf = calloc(1, sizeof *shsurf);
3469 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003470 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003471 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003472 }
3473
Jason Ekstranda7af7042013-10-12 22:38:11 -05003474 shsurf->view = weston_view_create(surface);
3475 if (!shsurf->view) {
3476 weston_log("no memory to allocate shell surface\n");
3477 free(shsurf);
3478 return NULL;
3479 }
3480
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003481 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003482 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003483
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003484 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3485 wl_resource_add_destroy_listener(surface->resource,
3486 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003487 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003488
Tiago Vignattibc052c92012-04-19 16:18:18 +03003489 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003490 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003491 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003492 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003493 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003494 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003495 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3496 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003497 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003498 wl_list_init(&shsurf->fullscreen.transform.link);
3499
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003500 shsurf->output = get_default_output(shsurf->shell->compositor);
3501
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003502 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003503 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003504 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003505 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003506
3507 /* init link so its safe to always remove it in destroy_shell_surface */
3508 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003509 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003510
Pekka Paalanen460099f2012-01-20 16:48:25 +02003511 /* empty when not in use */
3512 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003513 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003514
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003515 wl_list_init(&shsurf->workspace_transform.link);
3516
Philip Withnall648a4dd2013-11-25 18:01:44 +00003517 wl_list_init(&shsurf->children_link);
3518 wl_list_init(&shsurf->children_list);
3519 shsurf->parent = NULL;
3520
Pekka Paalanen98262232011-12-01 10:42:22 +02003521 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003522
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003523 shsurf->client = client;
3524
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003525 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003526}
3527
Rafael Antognollie2a34552013-12-03 15:35:45 -02003528static struct shell_surface *
3529create_shell_surface(void *shell, struct weston_surface *surface,
3530 const struct weston_shell_client *client)
3531{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003532 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003533}
3534
Jason Ekstranda7af7042013-10-12 22:38:11 -05003535static struct weston_view *
3536get_primary_view(void *shell, struct shell_surface *shsurf)
3537{
3538 return shsurf->view;
3539}
3540
Tiago Vignattibc052c92012-04-19 16:18:18 +03003541static void
3542shell_get_shell_surface(struct wl_client *client,
3543 struct wl_resource *resource,
3544 uint32_t id,
3545 struct wl_resource *surface_resource)
3546{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003547 struct weston_surface *surface =
3548 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003549 struct shell_client *sc = wl_resource_get_user_data(resource);
3550 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003551 struct shell_surface *shsurf;
3552
3553 if (get_shell_surface(surface)) {
3554 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003555 WL_DISPLAY_ERROR_INVALID_OBJECT,
3556 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003557 return;
3558 }
3559
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003560 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003561 if (!shsurf) {
3562 wl_resource_post_error(surface_resource,
3563 WL_DISPLAY_ERROR_INVALID_OBJECT,
3564 "surface->configure already set");
3565 return;
3566 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003567
Jason Ekstranda85118c2013-06-27 20:17:02 -05003568 shsurf->resource =
3569 wl_resource_create(client,
3570 &wl_shell_surface_interface, 1, id);
3571 wl_resource_set_implementation(shsurf->resource,
3572 &shell_surface_implementation,
3573 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003574}
3575
Rafael Antognollie2a34552013-12-03 15:35:45 -02003576static bool
3577shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3578{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003579 /* A shell surface without a resource is created from xwayland
3580 * and is considered a wl_shell surface for now. */
3581
3582 return shsurf->resource == NULL ||
3583 wl_resource_instance_of(shsurf->resource,
3584 &wl_shell_surface_interface,
3585 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003586}
3587
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003588static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003589 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003590};
3591
Rafael Antognollie2a34552013-12-03 15:35:45 -02003592/****************************
3593 * xdg-shell implementation */
3594
3595static void
3596xdg_surface_destroy(struct wl_client *client,
3597 struct wl_resource *resource)
3598{
3599 wl_resource_destroy(resource);
3600}
3601
3602static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003603xdg_surface_set_parent(struct wl_client *client,
3604 struct wl_resource *resource,
3605 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003606{
3607 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3608 struct weston_surface *parent;
3609
3610 if (parent_resource)
3611 parent = wl_resource_get_user_data(parent_resource);
3612 else
3613 parent = NULL;
3614
3615 shell_surface_set_parent(shsurf, parent);
3616}
3617
3618static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003619xdg_surface_set_app_id(struct wl_client *client,
3620 struct wl_resource *resource,
3621 const char *app_id)
3622{
3623 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3624
3625 free(shsurf->class);
3626 shsurf->class = strdup(app_id);
3627}
3628
3629static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003630xdg_surface_show_window_menu(struct wl_client *client,
3631 struct wl_resource *surface_resource,
3632 struct wl_resource *seat_resource,
3633 uint32_t serial,
3634 int32_t x,
3635 int32_t y)
3636{
3637 /* TODO */
3638}
3639
3640static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003641xdg_surface_set_title(struct wl_client *client,
3642 struct wl_resource *resource, const char *title)
3643{
3644 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3645
3646 set_title(shsurf, title);
3647}
3648
3649static void
3650xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3651 struct wl_resource *seat_resource, uint32_t serial)
3652{
3653 common_surface_move(resource, seat_resource, serial);
3654}
3655
3656static void
3657xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3658 struct wl_resource *seat_resource, uint32_t serial,
3659 uint32_t edges)
3660{
3661 common_surface_resize(resource, seat_resource, serial, edges);
3662}
3663
3664static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003665xdg_surface_ack_configure(struct wl_client *client,
3666 struct wl_resource *resource,
3667 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003668{
3669 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3670
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003671 if (shsurf->state_requested) {
3672 shsurf->next_state = shsurf->requested_state;
3673 shsurf->state_changed = true;
3674 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003675 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003676}
3677
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003678static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003679xdg_surface_set_window_geometry(struct wl_client *client,
3680 struct wl_resource *resource,
3681 int32_t x,
3682 int32_t y,
3683 int32_t width,
3684 int32_t height)
3685{
3686 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3687
3688 set_window_geometry(shsurf, x, y, width, height);
3689}
3690
3691static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003692xdg_surface_set_maximized(struct wl_client *client,
3693 struct wl_resource *resource)
3694{
3695 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3696
3697 shsurf->state_requested = true;
3698 shsurf->requested_state.maximized = true;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003699 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003700}
3701
3702static void
3703xdg_surface_unset_maximized(struct wl_client *client,
3704 struct wl_resource *resource)
3705{
3706 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3707
3708 shsurf->state_requested = true;
3709 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003710 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003711}
3712
3713static void
3714xdg_surface_set_fullscreen(struct wl_client *client,
3715 struct wl_resource *resource,
3716 struct wl_resource *output_resource)
3717{
3718 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3719 struct weston_output *output;
3720
3721 shsurf->state_requested = true;
3722 shsurf->requested_state.fullscreen = true;
3723
3724 if (output_resource)
3725 output = wl_resource_get_user_data(output_resource);
3726 else
3727 output = NULL;
3728
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003729 shell_surface_set_output(shsurf, output);
3730 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003731
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003732 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003733}
3734
3735static void
3736xdg_surface_unset_fullscreen(struct wl_client *client,
3737 struct wl_resource *resource)
3738{
3739 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3740
3741 shsurf->state_requested = true;
3742 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003743 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003744}
3745
3746static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003747xdg_surface_set_minimized(struct wl_client *client,
3748 struct wl_resource *resource)
3749{
3750 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3751
3752 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3753 return;
3754
3755 /* apply compositor's own minimization logic (hide) */
3756 set_minimized(shsurf->surface, 1);
3757}
3758
Rafael Antognollie2a34552013-12-03 15:35:45 -02003759static const struct xdg_surface_interface xdg_surface_implementation = {
3760 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003761 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003762 xdg_surface_set_title,
3763 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003764 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003765 xdg_surface_move,
3766 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003767 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003768 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003769 xdg_surface_set_maximized,
3770 xdg_surface_unset_maximized,
3771 xdg_surface_set_fullscreen,
3772 xdg_surface_unset_fullscreen,
3773 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003774};
3775
3776static void
3777xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003778 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003779{
3780 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003781 uint32_t *s;
3782 struct wl_array states;
3783 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003784
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003785 assert(shsurf);
3786
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003787 if (!shsurf->resource)
3788 return;
3789
3790 wl_array_init(&states);
3791 if (shsurf->requested_state.fullscreen) {
3792 s = wl_array_add(&states, sizeof *s);
3793 *s = XDG_SURFACE_STATE_FULLSCREEN;
3794 } else if (shsurf->requested_state.maximized) {
3795 s = wl_array_add(&states, sizeof *s);
3796 *s = XDG_SURFACE_STATE_MAXIMIZED;
3797 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003798 if (shsurf->resize_edges != 0) {
3799 s = wl_array_add(&states, sizeof *s);
3800 *s = XDG_SURFACE_STATE_RESIZING;
3801 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003802 if (shsurf->focus_count > 0) {
3803 s = wl_array_add(&states, sizeof *s);
3804 *s = XDG_SURFACE_STATE_ACTIVATED;
3805 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003806
3807 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3808 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3809
3810 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003811}
3812
3813static const struct weston_shell_client xdg_client = {
3814 xdg_send_configure
3815};
3816
3817static void
3818xdg_use_unstable_version(struct wl_client *client,
3819 struct wl_resource *resource,
3820 int32_t version)
3821{
3822 if (version > 1) {
3823 wl_resource_post_error(resource,
3824 1,
3825 "xdg-shell:: version not implemented yet.");
3826 return;
3827 }
3828}
3829
3830static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003831create_xdg_surface(struct shell_client *owner, void *shell,
3832 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003833 const struct weston_shell_client *client)
3834{
3835 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003836
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003837 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003838 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003839
3840 return shsurf;
3841}
3842
3843static void
3844xdg_get_xdg_surface(struct wl_client *client,
3845 struct wl_resource *resource,
3846 uint32_t id,
3847 struct wl_resource *surface_resource)
3848{
3849 struct weston_surface *surface =
3850 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003851 struct shell_client *sc = wl_resource_get_user_data(resource);
3852 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003853 struct shell_surface *shsurf;
3854
3855 if (get_shell_surface(surface)) {
3856 wl_resource_post_error(surface_resource,
3857 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003858 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003859 return;
3860 }
3861
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003862 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003863 if (!shsurf) {
3864 wl_resource_post_error(surface_resource,
3865 WL_DISPLAY_ERROR_INVALID_OBJECT,
3866 "surface->configure already set");
3867 return;
3868 }
3869
3870 shsurf->resource =
3871 wl_resource_create(client,
3872 &xdg_surface_interface, 1, id);
3873 wl_resource_set_implementation(shsurf->resource,
3874 &xdg_surface_implementation,
3875 shsurf, shell_destroy_shell_surface);
3876}
3877
3878static bool
3879shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3880{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003881 return shsurf->resource &&
3882 wl_resource_instance_of(shsurf->resource,
3883 &xdg_surface_interface,
3884 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003885}
3886
3887/* xdg-popup implementation */
3888
3889static void
3890xdg_popup_destroy(struct wl_client *client,
3891 struct wl_resource *resource)
3892{
3893 wl_resource_destroy(resource);
3894}
3895
Rafael Antognollie2a34552013-12-03 15:35:45 -02003896static const struct xdg_popup_interface xdg_popup_implementation = {
3897 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003898};
3899
3900static void
3901xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003902 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003903{
3904}
3905
3906static const struct weston_shell_client xdg_popup_client = {
3907 xdg_popup_send_configure
3908};
3909
3910static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003911create_xdg_popup(struct shell_client *owner, void *shell,
3912 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003913 const struct weston_shell_client *client,
3914 struct weston_surface *parent,
3915 struct shell_seat *seat,
3916 uint32_t serial,
3917 int32_t x, int32_t y)
3918{
3919 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003920
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003921 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003922 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003923 shsurf->popup.shseat = seat;
3924 shsurf->popup.serial = serial;
3925 shsurf->popup.x = x;
3926 shsurf->popup.y = y;
3927 shell_surface_set_parent(shsurf, parent);
3928
3929 return shsurf;
3930}
3931
3932static void
3933xdg_get_xdg_popup(struct wl_client *client,
3934 struct wl_resource *resource,
3935 uint32_t id,
3936 struct wl_resource *surface_resource,
3937 struct wl_resource *parent_resource,
3938 struct wl_resource *seat_resource,
3939 uint32_t serial,
3940 int32_t x, int32_t y, uint32_t flags)
3941{
3942 struct weston_surface *surface =
3943 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003944 struct shell_client *sc = wl_resource_get_user_data(resource);
3945 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003946 struct shell_surface *shsurf;
3947 struct weston_surface *parent;
3948 struct shell_seat *seat;
3949
3950 if (get_shell_surface(surface)) {
3951 wl_resource_post_error(surface_resource,
3952 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003953 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003954 return;
3955 }
3956
3957 if (!parent_resource) {
3958 wl_resource_post_error(surface_resource,
3959 WL_DISPLAY_ERROR_INVALID_OBJECT,
3960 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04003961 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003962 }
3963
3964 parent = wl_resource_get_user_data(parent_resource);
3965 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3966
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003967 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003968 parent, seat, serial, x, y);
3969 if (!shsurf) {
3970 wl_resource_post_error(surface_resource,
3971 WL_DISPLAY_ERROR_INVALID_OBJECT,
3972 "surface->configure already set");
3973 return;
3974 }
3975
3976 shsurf->resource =
3977 wl_resource_create(client,
3978 &xdg_popup_interface, 1, id);
3979 wl_resource_set_implementation(shsurf->resource,
3980 &xdg_popup_implementation,
3981 shsurf, shell_destroy_shell_surface);
3982}
3983
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003984static void
3985xdg_pong(struct wl_client *client,
3986 struct wl_resource *resource, uint32_t serial)
3987{
3988 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003989
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003990 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003991}
3992
Rafael Antognollie2a34552013-12-03 15:35:45 -02003993static bool
3994shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3995{
3996 return wl_resource_instance_of(shsurf->resource,
3997 &xdg_popup_interface,
3998 &xdg_popup_implementation);
3999}
4000
4001static const struct xdg_shell_interface xdg_implementation = {
4002 xdg_use_unstable_version,
4003 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004004 xdg_get_xdg_popup,
4005 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004006};
4007
4008static int
4009xdg_shell_unversioned_dispatch(const void *implementation,
4010 void *_target, uint32_t opcode,
4011 const struct wl_message *message,
4012 union wl_argument *args)
4013{
4014 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004015 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004016
4017 if (opcode != 0) {
4018 wl_resource_post_error(resource,
4019 WL_DISPLAY_ERROR_INVALID_OBJECT,
4020 "must call use_unstable_version first");
4021 return 0;
4022 }
4023
Pekka Paalanen71182ae2014-08-21 17:47:20 +03004024#define XDG_SERVER_VERSION 4
Rafael Antognollie2a34552013-12-03 15:35:45 -02004025
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004026 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4027 "shell implementation doesn't match protocol version");
4028
Rafael Antognollie2a34552013-12-03 15:35:45 -02004029 if (args[0].i != XDG_SERVER_VERSION) {
4030 wl_resource_post_error(resource,
4031 WL_DISPLAY_ERROR_INVALID_OBJECT,
4032 "incompatible version, server is %d "
4033 "client wants %d",
4034 XDG_SERVER_VERSION, args[0].i);
4035 return 0;
4036 }
4037
4038 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004039 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004040
4041 return 1;
4042}
4043
4044/* end of xdg-shell implementation */
4045/***********************************/
4046
Kristian Høgsberg07937562011-04-12 17:25:42 -04004047static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004048shell_fade(struct desktop_shell *shell, enum fade_type type);
4049
4050static int
4051screensaver_timeout(void *data)
4052{
4053 struct desktop_shell *shell = data;
4054
4055 shell_fade(shell, FADE_OUT);
4056
4057 return 1;
4058}
4059
4060static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004061handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02004062{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004063 struct desktop_shell *shell =
4064 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004065
Pekka Paalanen18027e52011-12-02 16:31:49 +02004066 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004067
4068 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02004069 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02004070}
4071
4072static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004073launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004074{
4075 if (shell->screensaver.binding)
4076 return;
4077
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004078 if (!shell->screensaver.path) {
4079 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004080 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004081 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004082
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004083 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02004084 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004085 return;
4086 }
4087
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004088 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004089 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004090 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004091 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004092}
4093
4094static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004095terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004096{
Pekka Paalanen18027e52011-12-02 16:31:49 +02004097 if (shell->screensaver.process.pid == 0)
4098 return;
4099
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03004100 /* Disarm the screensaver timer, otherwise it may fire when the
4101 * compositor is not in the idle state. In that case, the screen will
4102 * be locked, but the wake_signal won't fire on user input, making the
4103 * system unresponsive. */
4104 wl_event_source_timer_update(shell->screensaver.timer, 0);
4105
Pekka Paalanen18027e52011-12-02 16:31:49 +02004106 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004107}
4108
4109static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004110configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004111{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004112 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004113
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004114 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004115 if (v->output == ev->output && v != ev) {
4116 weston_view_unmap(v);
4117 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004118 }
4119 }
4120
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004121 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004122
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004123 if (wl_list_empty(&ev->layer_link.link)) {
4124 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004125 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004126 }
4127}
4128
4129static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004130background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004131{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004132 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004133 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004134
Jason Ekstranda7af7042013-10-12 22:38:11 -05004135 view = container_of(es->views.next, struct weston_view, surface_link);
4136
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004137 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004138}
4139
4140static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004141desktop_shell_set_background(struct wl_client *client,
4142 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004143 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004144 struct wl_resource *surface_resource)
4145{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004146 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004147 struct weston_surface *surface =
4148 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004149 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004150
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004151 if (surface->configure) {
4152 wl_resource_post_error(surface_resource,
4153 WL_DISPLAY_ERROR_INVALID_OBJECT,
4154 "surface role already assigned");
4155 return;
4156 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004157
Jason Ekstranda7af7042013-10-12 22:38:11 -05004158 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4159 weston_view_destroy(view);
4160 view = weston_view_create(surface);
4161
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004162 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004163 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004164 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004165 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004166 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004167 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004168 surface->output->width,
4169 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004170}
4171
4172static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004173panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004174{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004175 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004176 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004177
Jason Ekstranda7af7042013-10-12 22:38:11 -05004178 view = container_of(es->views.next, struct weston_view, surface_link);
4179
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004180 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004181}
4182
4183static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004184desktop_shell_set_panel(struct wl_client *client,
4185 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004186 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004187 struct wl_resource *surface_resource)
4188{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004189 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004190 struct weston_surface *surface =
4191 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004192 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004193
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004194 if (surface->configure) {
4195 wl_resource_post_error(surface_resource,
4196 WL_DISPLAY_ERROR_INVALID_OBJECT,
4197 "surface role already assigned");
4198 return;
4199 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004200
Jason Ekstranda7af7042013-10-12 22:38:11 -05004201 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4202 weston_view_destroy(view);
4203 view = weston_view_create(surface);
4204
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004205 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004206 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004207 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004208 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004209 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004210 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004211 surface->output->width,
4212 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004213}
4214
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004215static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004216lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004217{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004218 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004219 struct weston_view *view;
4220
4221 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004222
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004223 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004224 return;
4225
Jason Ekstranda7af7042013-10-12 22:38:11 -05004226 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004227
4228 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004229 weston_layer_entry_insert(&shell->lock_layer.view_list,
4230 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004231 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004232 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004233 }
4234}
4235
4236static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004237handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004238{
Tiago Vignattibe143262012-04-16 17:31:41 +03004239 struct desktop_shell *shell =
4240 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004241
Martin Minarik6d118362012-06-07 18:01:59 +02004242 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004243 shell->lock_surface = NULL;
4244}
4245
4246static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004247desktop_shell_set_lock_surface(struct wl_client *client,
4248 struct wl_resource *resource,
4249 struct wl_resource *surface_resource)
4250{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004251 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004252 struct weston_surface *surface =
4253 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004254
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004255 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004256
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004257 if (!shell->locked)
4258 return;
4259
Pekka Paalanen98262232011-12-01 10:42:22 +02004260 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004261
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004262 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004263 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004264 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004265
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004266 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004267 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004268 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004269}
4270
4271static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004272resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004273{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004274 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004275
Pekka Paalanen77346a62011-11-30 16:26:35 +02004276 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004277
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004278 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004279 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004280 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004281 &shell->input_panel_layer.link);
4282 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004283 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004284 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004285 wl_list_insert(&shell->compositor->cursor_layer.link,
4286 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004287 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004288 wl_list_insert(&shell->fullscreen_layer.link,
4289 &shell->panel_layer.link);
4290 wl_list_insert(&shell->panel_layer.link,
4291 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004292
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004293 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004294
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004295 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004296 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004297 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004298}
4299
4300static void
4301desktop_shell_unlock(struct wl_client *client,
4302 struct wl_resource *resource)
4303{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004304 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004305
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004306 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004307
4308 if (shell->locked)
4309 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004310}
4311
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004312static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004313desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004314 struct wl_resource *resource,
4315 struct wl_resource *surface_resource)
4316{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004317 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004318
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004319 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004320 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004321}
4322
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004323static void
4324desktop_shell_desktop_ready(struct wl_client *client,
4325 struct wl_resource *resource)
4326{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004327 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004328
4329 shell_fade_startup(shell);
4330}
4331
Jonny Lamb765760d2014-08-20 15:53:19 +02004332static void
4333desktop_shell_set_panel_position(struct wl_client *client,
4334 struct wl_resource *resource,
4335 uint32_t position)
4336{
4337 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4338
4339 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4340 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4341 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4342 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4343 wl_resource_post_error(resource,
4344 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4345 "bad position argument");
4346 return;
4347 }
4348
4349 shell->panel_position = position;
4350}
4351
Kristian Høgsberg75840622011-09-06 13:48:16 -04004352static const struct desktop_shell_interface desktop_shell_implementation = {
4353 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004354 desktop_shell_set_panel,
4355 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004356 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004357 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004358 desktop_shell_desktop_ready,
4359 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004360};
4361
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004362static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004363get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004364{
4365 struct shell_surface *shsurf;
4366
4367 shsurf = get_shell_surface(surface);
4368 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004369 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004370 return shsurf->type;
4371}
4372
Kristian Høgsberg75840622011-09-06 13:48:16 -04004373static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004374move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004375{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004376 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004377 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004378 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004379
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004380 if (seat->pointer->focus == NULL)
4381 return;
4382
4383 focus = seat->pointer->focus->surface;
4384
Pekka Paalanen01388e22013-04-25 13:57:44 +03004385 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004386 if (surface == NULL)
4387 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004388
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004389 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004390 if (shsurf == NULL || shsurf->state.fullscreen ||
4391 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004392 return;
4393
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004394 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004395}
4396
4397static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004398maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4399{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004400 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004401 struct weston_surface *surface;
4402 struct shell_surface *shsurf;
4403
4404 surface = weston_surface_get_main_surface(focus);
4405 if (surface == NULL)
4406 return;
4407
4408 shsurf = get_shell_surface(surface);
4409 if (shsurf == NULL)
4410 return;
4411
4412 if (!shell_surface_is_xdg_surface(shsurf))
4413 return;
4414
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004415 shsurf->state_requested = true;
4416 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004417 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004418}
4419
4420static void
4421fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4422{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004423 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004424 struct weston_surface *surface;
4425 struct shell_surface *shsurf;
4426
4427 surface = weston_surface_get_main_surface(focus);
4428 if (surface == NULL)
4429 return;
4430
4431 shsurf = get_shell_surface(surface);
4432 if (shsurf == NULL)
4433 return;
4434
4435 if (!shell_surface_is_xdg_surface(shsurf))
4436 return;
4437
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004438 shsurf->state_requested = true;
4439 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004440 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004441 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004442}
4443
4444static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004445touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4446{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004447 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004448 struct weston_surface *surface;
4449 struct shell_surface *shsurf;
4450
4451 surface = weston_surface_get_main_surface(focus);
4452 if (surface == NULL)
4453 return;
4454
4455 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004456 if (shsurf == NULL || shsurf->state.fullscreen ||
4457 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004458 return;
4459
4460 surface_touch_move(shsurf, (struct weston_seat *) seat);
4461}
4462
4463static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004464resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004465{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004466 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004467 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004468 uint32_t edges = 0;
4469 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004470 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004471
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004472 if (seat->pointer->focus == NULL)
4473 return;
4474
4475 focus = seat->pointer->focus->surface;
4476
Pekka Paalanen01388e22013-04-25 13:57:44 +03004477 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004478 if (surface == NULL)
4479 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004480
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004481 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004482 if (shsurf == NULL || shsurf->state.fullscreen ||
4483 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004484 return;
4485
Jason Ekstranda7af7042013-10-12 22:38:11 -05004486 weston_view_from_global(shsurf->view,
4487 wl_fixed_to_int(seat->pointer->grab_x),
4488 wl_fixed_to_int(seat->pointer->grab_y),
4489 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004490
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004491 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004492 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004493 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004494 edges |= 0;
4495 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004496 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004497
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004498 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004499 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004500 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004501 edges |= 0;
4502 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004503 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004504
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004505 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004506}
4507
4508static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004509surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004510 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004511{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004512 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004513 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004514 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004515 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004516
Pekka Paalanen01388e22013-04-25 13:57:44 +03004517 /* XXX: broken for windows containing sub-surfaces */
4518 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004519 if (surface == NULL)
4520 return;
4521
4522 shsurf = get_shell_surface(surface);
4523 if (!shsurf)
4524 return;
4525
Jason Ekstranda7af7042013-10-12 22:38:11 -05004526 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004527
Jason Ekstranda7af7042013-10-12 22:38:11 -05004528 if (shsurf->view->alpha > 1.0)
4529 shsurf->view->alpha = 1.0;
4530 if (shsurf->view->alpha < step)
4531 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004532
Jason Ekstranda7af7042013-10-12 22:38:11 -05004533 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004534 weston_surface_damage(surface);
4535}
4536
4537static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004538do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004539 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004540{
Daniel Stone37816df2012-05-16 18:45:18 +01004541 struct weston_seat *ws = (struct weston_seat *) seat;
4542 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004543 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004544 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004545
4546 wl_list_for_each(output, &compositor->output_list, link) {
4547 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004548 wl_fixed_to_double(seat->pointer->x),
4549 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004550 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004551 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004552 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004553 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004554 increment = -output->zoom.increment;
4555 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004556 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004557 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004558 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004559 else
4560 increment = 0;
4561
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004562 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004563
Scott Moreaue6603982012-06-11 13:07:51 -06004564 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004565 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004566 else if (output->zoom.level > output->zoom.max_level)
4567 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004568 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004569 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004570 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004571
Scott Moreaue6603982012-06-11 13:07:51 -06004572 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004573
Jason Ekstranda7af7042013-10-12 22:38:11 -05004574 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004575 }
4576 }
4577}
4578
Scott Moreauccbf29d2012-02-22 14:21:41 -07004579static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004580zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004581 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004582{
4583 do_zoom(seat, time, 0, axis, value);
4584}
4585
4586static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004587zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004588 void *data)
4589{
4590 do_zoom(seat, time, key, 0, 0);
4591}
4592
4593static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004594terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004595 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004596{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004597 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004598
Daniel Stone325fc2d2012-05-30 16:31:58 +01004599 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004600}
4601
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004602static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004603rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4604 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004605{
4606 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004607 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004608 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004609 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004610 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004611
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004612 weston_pointer_move(pointer, x, y);
4613
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004614 if (!shsurf)
4615 return;
4616
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004617 cx = 0.5f * shsurf->surface->width;
4618 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004619
Daniel Stone37816df2012-05-16 18:45:18 +01004620 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4621 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004622 r = sqrtf(dx * dx + dy * dy);
4623
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004624 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004625 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004626
4627 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004628 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004629 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004630
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004631 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004632 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004633
4634 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004635 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004636 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004637 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004638 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004639
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004640 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004641 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004642 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004643 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004644 wl_list_init(&shsurf->rotation.transform.link);
4645 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004646 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004647 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004648
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004649 /* We need to adjust the position of the surface
4650 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004651 cposx = shsurf->view->geometry.x + cx;
4652 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004653 dposx = rotate->center.x - cposx;
4654 dposy = rotate->center.y - cposy;
4655 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004656 weston_view_set_position(shsurf->view,
4657 shsurf->view->geometry.x + dposx,
4658 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004659 }
4660
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004661 /* Repaint implies weston_surface_update_transform(), which
4662 * lazily applies the damage due to rotation update.
4663 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004664 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004665}
4666
4667static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004668rotate_grab_button(struct weston_pointer_grab *grab,
4669 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004670{
4671 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004672 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004673 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004674 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004675 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004676
Daniel Stone4dbadb12012-05-30 16:31:51 +01004677 if (pointer->button_count == 0 &&
4678 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004679 if (shsurf)
4680 weston_matrix_multiply(&shsurf->rotation.rotation,
4681 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004682 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004683 free(rotate);
4684 }
4685}
4686
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004687static void
4688rotate_grab_cancel(struct weston_pointer_grab *grab)
4689{
4690 struct rotate_grab *rotate =
4691 container_of(grab, struct rotate_grab, base.grab);
4692
4693 shell_grab_end(&rotate->base);
4694 free(rotate);
4695}
4696
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004697static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004698 noop_grab_focus,
4699 rotate_grab_motion,
4700 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004701 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004702};
4703
4704static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004705surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004706{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004707 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004708 float dx, dy;
4709 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004710
Pekka Paalanen460099f2012-01-20 16:48:25 +02004711 rotate = malloc(sizeof *rotate);
4712 if (!rotate)
4713 return;
4714
Jason Ekstranda7af7042013-10-12 22:38:11 -05004715 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004716 surface->surface->width * 0.5f,
4717 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004718 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004719
Daniel Stone37816df2012-05-16 18:45:18 +01004720 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4721 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004722 r = sqrtf(dx * dx + dy * dy);
4723 if (r > 20.0f) {
4724 struct weston_matrix inverse;
4725
4726 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004727 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004728 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004729
4730 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004731 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004732 } else {
4733 weston_matrix_init(&surface->rotation.rotation);
4734 weston_matrix_init(&rotate->rotation);
4735 }
4736
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004737 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4738 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004739}
4740
4741static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004742rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004743 void *data)
4744{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004745 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004746 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004747 struct shell_surface *surface;
4748
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004749 if (seat->pointer->focus == NULL)
4750 return;
4751
4752 focus = seat->pointer->focus->surface;
4753
Pekka Paalanen01388e22013-04-25 13:57:44 +03004754 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004755 if (base_surface == NULL)
4756 return;
4757
4758 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004759 if (surface == NULL || surface->state.fullscreen ||
4760 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004761 return;
4762
4763 surface_rotate(surface, seat);
4764}
4765
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004766/* Move all fullscreen layers down to the current workspace and hide their
4767 * black views. The surfaces' state is set to both fullscreen and lowered,
4768 * and this is reversed when such a surface is re-configured, see
4769 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4770 *
4771 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004772 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004773void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004774lower_fullscreen_layer(struct desktop_shell *shell)
4775{
4776 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004777 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004778
4779 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004780 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004781 &shell->fullscreen_layer.view_list.link,
4782 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004783 struct shell_surface *shsurf = get_shell_surface(view->surface);
4784
4785 if (!shsurf)
4786 continue;
4787
4788 /* We can have a non-fullscreen popup for a fullscreen surface
4789 * in the fullscreen layer. */
4790 if (shsurf->state.fullscreen) {
4791 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004792 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4793 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004794 weston_view_damage_below(shsurf->fullscreen.black_view);
4795
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004796 }
4797
4798 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004799 weston_layer_entry_remove(&view->layer_link);
4800 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004801 weston_view_damage_below(view);
4802 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004803
4804 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004805 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004806}
4807
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004808void
Tiago Vignattibe143262012-04-16 17:31:41 +03004809activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004810 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004811{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004812 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004813 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004814 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004815 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004816 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004817
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004818 lower_fullscreen_layer(shell);
4819
Pekka Paalanen01388e22013-04-25 13:57:44 +03004820 main_surface = weston_surface_get_main_surface(es);
4821
Daniel Stone37816df2012-05-16 18:45:18 +01004822 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004823
Jonas Ådahl8538b222012-08-29 22:13:03 +02004824 state = ensure_focus_state(shell, seat);
4825 if (state == NULL)
4826 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004827
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004828 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004829 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004830
Rafael Antognolli03b16592013-12-03 15:35:42 -02004831 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004832 assert(shsurf);
4833
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004834 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004835 shell_configure_fullscreen(shsurf);
4836 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004837 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004838
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004839 /* Update the surface’s layer. This brings it to the top of the stacking
4840 * order as appropriate. */
4841 shell_surface_update_layer(shsurf);
4842
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004843 if (shell->focus_animation_type != ANIMATION_NONE) {
4844 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004845 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004846 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004847}
4848
Alex Wu21858432012-04-01 20:13:08 +08004849/* no-op func for checking black surface */
4850static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004851black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004852{
4853}
4854
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004855static bool
Alex Wu21858432012-04-01 20:13:08 +08004856is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4857{
4858 if (es->configure == black_surface_configure) {
4859 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004860 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004861 return true;
4862 }
4863 return false;
4864}
4865
Kristian Høgsberg75840622011-09-06 13:48:16 -04004866static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004867activate_binding(struct weston_seat *seat,
4868 struct desktop_shell *shell,
4869 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004870{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004871 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004872
Alex Wu9c35e6b2012-03-05 14:13:13 +08004873 if (!focus)
4874 return;
4875
Pekka Paalanen01388e22013-04-25 13:57:44 +03004876 if (is_black_surface(focus, &main_surface))
4877 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004878
Pekka Paalanen01388e22013-04-25 13:57:44 +03004879 main_surface = weston_surface_get_main_surface(focus);
4880 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004881 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004882
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004883 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004884}
4885
4886static void
4887click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4888 void *data)
4889{
4890 if (seat->pointer->grab != &seat->pointer->default_grab)
4891 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004892 if (seat->pointer->focus == NULL)
4893 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004894
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004895 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004896}
4897
4898static void
4899touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4900{
4901 if (seat->touch->grab != &seat->touch->default_grab)
4902 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004903 if (seat->touch->focus == NULL)
4904 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004905
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004906 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004907}
4908
4909static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004910unfocus_all_seats(struct desktop_shell *shell)
4911{
4912 struct weston_seat *seat, *next;
4913
4914 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4915 if (seat->keyboard == NULL)
4916 continue;
4917
4918 weston_keyboard_set_focus(seat->keyboard, NULL);
4919 }
4920}
4921
4922static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004923lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004924{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004925 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004926
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004927 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004928 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004929 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004930 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004931
4932 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004933
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004934 /* Hide all surfaces by removing the fullscreen, panel and
4935 * toplevel layers. This way nothing else can show or receive
4936 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004937
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004938 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004939 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004940 if (shell->showing_input_panels)
4941 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004942 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004943 wl_list_insert(&shell->compositor->cursor_layer.link,
4944 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004945
Pekka Paalanen77346a62011-11-30 16:26:35 +02004946 launch_screensaver(shell);
4947
Neil Robertsb4a91702014-04-09 16:33:32 +01004948 /* Remove the keyboard focus on all seats. This will be
4949 * restored to the workspace's saved state via
4950 * restore_focus_state when the compositor is unlocked */
4951 unfocus_all_seats(shell);
4952
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004953 /* TODO: disable bindings that should not work while locked. */
4954
4955 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004956}
4957
4958static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004959unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004960{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004961 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004962 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004963 return;
4964 }
4965
4966 /* If desktop-shell client has gone away, unlock immediately. */
4967 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004968 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004969 return;
4970 }
4971
4972 if (shell->prepare_event_sent)
4973 return;
4974
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004975 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004976 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004977}
4978
4979static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004980shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004981{
4982 struct desktop_shell *shell = data;
4983
4984 shell->fade.animation = NULL;
4985
4986 switch (shell->fade.type) {
4987 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004988 weston_surface_destroy(shell->fade.view->surface);
4989 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004990 break;
4991 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004992 lock(shell);
4993 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004994 default:
4995 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004996 }
4997}
4998
Jason Ekstranda7af7042013-10-12 22:38:11 -05004999static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005000shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005001{
5002 struct weston_compositor *compositor = shell->compositor;
5003 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005004 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005005
5006 surface = weston_surface_create(compositor);
5007 if (!surface)
5008 return NULL;
5009
Jason Ekstranda7af7042013-10-12 22:38:11 -05005010 view = weston_view_create(surface);
5011 if (!view) {
5012 weston_surface_destroy(surface);
5013 return NULL;
5014 }
5015
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005016 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005017 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005018 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005019 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5020 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005021 pixman_region32_init(&surface->input);
5022
Jason Ekstranda7af7042013-10-12 22:38:11 -05005023 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005024}
5025
5026static void
5027shell_fade(struct desktop_shell *shell, enum fade_type type)
5028{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005029 float tint;
5030
5031 switch (type) {
5032 case FADE_IN:
5033 tint = 0.0;
5034 break;
5035 case FADE_OUT:
5036 tint = 1.0;
5037 break;
5038 default:
5039 weston_log("shell: invalid fade type\n");
5040 return;
5041 }
5042
5043 shell->fade.type = type;
5044
Jason Ekstranda7af7042013-10-12 22:38:11 -05005045 if (shell->fade.view == NULL) {
5046 shell->fade.view = shell_fade_create_surface(shell);
5047 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005048 return;
5049
Jason Ekstranda7af7042013-10-12 22:38:11 -05005050 shell->fade.view->alpha = 1.0 - tint;
5051 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005052 }
5053
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005054 if (shell->fade.view->output == NULL) {
5055 /* If the black view gets a NULL output, we lost the
5056 * last output and we'll just cancel the fade. This
5057 * happens when you close the last window under the
5058 * X11 or Wayland backends. */
5059 shell->locked = false;
5060 weston_surface_destroy(shell->fade.view->surface);
5061 shell->fade.view = NULL;
5062 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005063 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005064 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005065 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005066 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005067 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005068 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005069 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005070}
5071
5072static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005073do_shell_fade_startup(void *data)
5074{
5075 struct desktop_shell *shell = data;
5076
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005077 if (shell->startup_animation_type == ANIMATION_FADE)
5078 shell_fade(shell, FADE_IN);
5079 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005080 weston_surface_destroy(shell->fade.view->surface);
5081 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005082 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005083}
5084
5085static void
5086shell_fade_startup(struct desktop_shell *shell)
5087{
5088 struct wl_event_loop *loop;
5089
5090 if (!shell->fade.startup_timer)
5091 return;
5092
5093 wl_event_source_remove(shell->fade.startup_timer);
5094 shell->fade.startup_timer = NULL;
5095
5096 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5097 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5098}
5099
5100static int
5101fade_startup_timeout(void *data)
5102{
5103 struct desktop_shell *shell = data;
5104
5105 shell_fade_startup(shell);
5106 return 0;
5107}
5108
5109static void
5110shell_fade_init(struct desktop_shell *shell)
5111{
5112 /* Make compositor output all black, and wait for the desktop-shell
5113 * client to signal it is ready, then fade in. The timer triggers a
5114 * fade-in, in case the desktop-shell client takes too long.
5115 */
5116
5117 struct wl_event_loop *loop;
5118
Jason Ekstranda7af7042013-10-12 22:38:11 -05005119 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005120 weston_log("%s: warning: fade surface already exists\n",
5121 __func__);
5122 return;
5123 }
5124
Jason Ekstranda7af7042013-10-12 22:38:11 -05005125 shell->fade.view = shell_fade_create_surface(shell);
5126 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005127 return;
5128
Jason Ekstranda7af7042013-10-12 22:38:11 -05005129 weston_view_update_transform(shell->fade.view);
5130 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005131
5132 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5133 shell->fade.startup_timer =
5134 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5135 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5136}
5137
5138static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005139idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005140{
5141 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005142 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005143 struct weston_seat *seat;
5144
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005145 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005146 if (seat->pointer)
5147 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005148 if (seat->touch)
5149 touch_popup_grab_end(seat->touch);
5150 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005151
5152 shell_fade(shell, FADE_OUT);
5153 /* lock() is called from shell_fade_done() */
5154}
5155
5156static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005157wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005158{
5159 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005160 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005161
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005162 unlock(shell);
5163}
5164
5165static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005166center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005167{
Giulio Camuffob8366642013-04-25 13:57:46 +03005168 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005169 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005170
Jason Ekstranda7af7042013-10-12 22:38:11 -05005171 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005172
5173 x = output->x + (output->width - width) / 2 - surf_x / 2;
5174 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005175
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005176 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005177}
5178
5179static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005180weston_view_set_initial_position(struct weston_view *view,
5181 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005182{
5183 struct weston_compositor *compositor = shell->compositor;
5184 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005185 int32_t range_x, range_y;
5186 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005187 struct weston_output *output, *target_output = NULL;
5188 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005189 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005190
5191 /* As a heuristic place the new window on the same output as the
5192 * pointer. Falling back to the output containing 0, 0.
5193 *
5194 * TODO: Do something clever for touch too?
5195 */
5196 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005197 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005198 ix = wl_fixed_to_int(seat->pointer->x);
5199 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005200 break;
5201 }
5202 }
5203
5204 wl_list_for_each(output, &compositor->output_list, link) {
5205 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5206 target_output = output;
5207 break;
5208 }
5209 }
5210
5211 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005212 weston_view_set_position(view, 10 + random() % 400,
5213 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005214 return;
5215 }
5216
5217 /* Valid range within output where the surface will still be onscreen.
5218 * If this is negative it means that the surface is bigger than
5219 * output.
5220 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005221 get_output_work_area(shell, target_output, &area);
5222
5223 dx = area.x;
5224 dy = area.y;
5225 range_x = area.width - view->surface->width;
5226 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005227
Rob Bradford4cb88c72012-08-13 15:18:44 +01005228 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005229 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005230
5231 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005232 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005233
5234 x = target_output->x + dx;
5235 y = target_output->y + dy;
5236
Jason Ekstranda7af7042013-10-12 22:38:11 -05005237 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005238}
5239
5240static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005241set_maximized_position(struct desktop_shell *shell,
5242 struct shell_surface *shsurf)
5243{
5244 int32_t surf_x, surf_y;
5245 pixman_rectangle32_t area;
5246
5247 /* use surface configure to set the geometry */
5248 get_output_work_area(shell, shsurf->output, &area);
5249 surface_subsurfaces_boundingbox(shsurf->surface,
5250 &surf_x, &surf_y, NULL, NULL);
5251
5252 weston_view_set_position(shsurf->view,
5253 area.x - surf_x,
5254 area.y - surf_y);
5255}
5256
5257static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005258map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005259 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005260{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005261 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005262 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005263
Pekka Paalanen77346a62011-11-30 16:26:35 +02005264 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005265 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005266 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005267 if (shsurf->state.fullscreen) {
5268 center_on_output(shsurf->view, shsurf->fullscreen_output);
5269 shell_map_fullscreen(shsurf);
5270 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005271 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005272 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005273 weston_view_set_initial_position(shsurf->view, shell);
5274 }
Juan Zhao96879df2012-02-07 08:45:41 +08005275 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005276 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005277 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005278 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005279 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005280 weston_view_set_position(shsurf->view,
5281 shsurf->view->geometry.x + sx,
5282 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005283 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005284 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005285 default:
5286 ;
5287 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005288
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005289 /* Surface stacking order, see also activate(). */
5290 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005291
Jason Ekstranda7af7042013-10-12 22:38:11 -05005292 if (shsurf->type != SHELL_SURFACE_NONE) {
5293 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005294 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005295 shsurf->surface->output = shsurf->output;
5296 shsurf->view->output = shsurf->output;
5297 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005298 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005299
Jason Ekstranda7af7042013-10-12 22:38:11 -05005300 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005301 /* XXX: xwayland's using the same fields for transient type */
5302 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005303 if (shsurf->transient.flags ==
5304 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5305 break;
5306 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005307 if (shsurf->state.relative &&
5308 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5309 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005310 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005311 break;
5312 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005313 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005314 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005315 case SHELL_SURFACE_POPUP:
5316 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005317 default:
5318 break;
5319 }
5320
Rafael Antognolli03b16592013-12-03 15:35:42 -02005321 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5322 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005323 {
5324 switch (shell->win_animation_type) {
5325 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005326 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005327 break;
5328 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005329 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005330 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005331 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005332 default:
5333 break;
5334 }
5335 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005336}
5337
5338static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005339configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005340 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005341{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005342 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005343 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005344
Pekka Paalanen77346a62011-11-30 16:26:35 +02005345 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005346
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005347 assert(shsurf);
5348
Rafael Antognolli03b16592013-12-03 15:35:42 -02005349 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005350 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005351 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005352 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005353 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005354 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005355 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005356
Alex Wu4539b082012-03-01 12:57:46 +08005357 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005358 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005359 wl_list_for_each(view, &surface->views, surface_link)
5360 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005361
Rafael Antognolli03b16592013-12-03 15:35:42 -02005362 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005363 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005364 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005365}
5366
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005367static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005368shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005369{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005370 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005371 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005372 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005373
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005374 assert(shsurf);
5375
5376 shell = shsurf->shell;
5377
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005378 if (!weston_surface_is_mapped(es) &&
5379 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005380 remove_popup_grab(shsurf);
5381 }
5382
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005383 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005384 return;
5385
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005386 if (shsurf->has_next_geometry) {
5387 shsurf->geometry = shsurf->next_geometry;
5388 shsurf->has_next_geometry = false;
5389 shsurf->has_set_geometry = true;
5390 } else if (!shsurf->has_set_geometry) {
5391 surface_subsurfaces_boundingbox(shsurf->surface,
5392 &shsurf->geometry.x,
5393 &shsurf->geometry.y,
5394 &shsurf->geometry.width,
5395 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005396 }
5397
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005398 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005399 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005400 type_changed = 1;
5401 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005402
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005403 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005404 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005405 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005406 shsurf->last_width != es->width ||
5407 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005408 float from_x, from_y;
5409 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005410
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005411 if (shsurf->resize_edges) {
5412 sx = 0;
5413 sy = 0;
5414 }
5415
5416 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5417 sx = shsurf->last_width - es->width;
5418 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5419 sy = shsurf->last_height - es->height;
5420
5421 shsurf->last_width = es->width;
5422 shsurf->last_height = es->height;
5423
Jason Ekstranda7af7042013-10-12 22:38:11 -05005424 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5425 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005426 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005427 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005428 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005429 }
5430}
5431
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005432static bool
5433check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5434{
5435 struct timespec now;
5436
5437 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5438 return false;
5439
5440 /*
5441 * If the shell helper client dies before the session has been
5442 * up for roughly 30 seconds, better just make Weston shut down,
5443 * because the user likely has no way to interact with the desktop
5444 * anyway.
5445 */
5446 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5447 weston_log("Error: %s apparently cannot run at all.\n",
5448 shell->client);
5449 weston_log_continue(STAMP_SPACE "Quitting...");
5450 wl_display_terminate(shell->compositor->wl_display);
5451
5452 return true;
5453 }
5454
5455 return false;
5456}
5457
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005458static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005459
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005460static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005461respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005462{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005463 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005464
5465 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5466 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005467 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005468 shell->child.deathstamp = time;
5469 shell->child.deathcount = 0;
5470 }
5471
5472 shell->child.deathcount++;
5473 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005474 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005475 return;
5476 }
5477
Pekka Paalanen826dc142014-08-27 12:11:53 +03005478 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005479 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005480}
5481
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005482static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005483desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5484{
5485 struct desktop_shell *shell;
5486
5487 shell = container_of(listener, struct desktop_shell,
5488 child.client_destroy_listener);
5489
Pekka Paalanen826dc142014-08-27 12:11:53 +03005490 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005491 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005492 /*
5493 * unbind_desktop_shell() will reset shell->child.desktop_shell
5494 * before the respawned process has a chance to create a new
5495 * desktop_shell object, because we are being called from the
5496 * wl_client destructor which destroys all wl_resources before
5497 * returning.
5498 */
5499
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005500 if (!check_desktop_shell_crash_too_early(shell))
5501 respawn_desktop_shell_process(shell);
5502
Pekka Paalanen826dc142014-08-27 12:11:53 +03005503 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005504}
5505
5506static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005507launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005508{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005509 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005510
Pekka Paalanen826dc142014-08-27 12:11:53 +03005511 shell->child.client = weston_client_start(shell->compositor,
5512 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005513
Arnaud Vrac42631192014-08-25 20:56:46 +02005514 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005515 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005516 return;
5517 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005518
5519 shell->child.client_destroy_listener.notify =
5520 desktop_shell_client_destroy;
5521 wl_client_add_destroy_listener(shell->child.client,
5522 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005523}
5524
5525static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005526handle_shell_client_destroy(struct wl_listener *listener, void *data)
5527{
5528 struct shell_client *sc =
5529 container_of(listener, struct shell_client, destroy_listener);
5530
5531 if (sc->ping_timer)
5532 wl_event_source_remove(sc->ping_timer);
5533 free(sc);
5534}
5535
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005536static struct shell_client *
5537shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5538 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005539{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005540 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005541
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005542 sc = zalloc(sizeof *sc);
5543 if (sc == NULL) {
5544 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005545 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005546 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005547
5548 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005549 if (sc->resource == NULL) {
5550 free(sc);
5551 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005552 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005553 }
5554
5555 sc->client = client;
5556 sc->shell = shell;
5557 sc->destroy_listener.notify = handle_shell_client_destroy;
5558 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5559
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005560 return sc;
5561}
5562
5563static void
5564bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5565{
5566 struct desktop_shell *shell = data;
5567 struct shell_client *sc;
5568
5569 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5570 if (sc)
5571 wl_resource_set_implementation(sc->resource,
5572 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005573 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005574}
5575
5576static void
5577bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5578{
5579 struct desktop_shell *shell = data;
5580 struct shell_client *sc;
5581
5582 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5583 if (sc)
5584 wl_resource_set_dispatcher(sc->resource,
5585 xdg_shell_unversioned_dispatch,
5586 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005587}
5588
5589static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005590unbind_desktop_shell(struct wl_resource *resource)
5591{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005592 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005593
5594 if (shell->locked)
5595 resume_desktop(shell);
5596
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005597 shell->child.desktop_shell = NULL;
5598 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005599}
5600
5601static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005602bind_desktop_shell(struct wl_client *client,
5603 void *data, uint32_t version, uint32_t id)
5604{
Tiago Vignattibe143262012-04-16 17:31:41 +03005605 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005606 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005607
Jason Ekstranda85118c2013-06-27 20:17:02 -05005608 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005609 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005610
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005611 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005612 wl_resource_set_implementation(resource,
5613 &desktop_shell_implementation,
5614 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005615 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005616
5617 if (version < 2)
5618 shell_fade_startup(shell);
5619
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005620 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005621 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005622
5623 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5624 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005625}
5626
Pekka Paalanen6e168112011-11-24 11:34:05 +02005627static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005628screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005629{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005630 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005631 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005632 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005633
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005634 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005635 return;
5636
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005637 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005638 if (!shell->locked)
5639 return;
5640
Jason Ekstranda7af7042013-10-12 22:38:11 -05005641 view = container_of(surface->views.next, struct weston_view, surface_link);
5642 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005643
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005644 if (wl_list_empty(&view->layer_link.link)) {
5645 prev = container_of(shell->lock_layer.view_list.link.prev,
5646 struct weston_layer_entry, link);
5647 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005648 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005649 wl_event_source_timer_update(shell->screensaver.timer,
5650 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005651 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005652 }
5653}
5654
5655static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005656screensaver_set_surface(struct wl_client *client,
5657 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005658 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005659 struct wl_resource *output_resource)
5660{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005661 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005662 struct weston_surface *surface =
5663 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005664 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005665 struct weston_view *view, *next;
5666
5667 /* Make sure we only have one view */
5668 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5669 weston_view_destroy(view);
5670 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005671
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005672 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005673 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005674 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005675}
5676
5677static const struct screensaver_interface screensaver_implementation = {
5678 screensaver_set_surface
5679};
5680
5681static void
5682unbind_screensaver(struct wl_resource *resource)
5683{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005684 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005685
Pekka Paalanen77346a62011-11-30 16:26:35 +02005686 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005687}
5688
5689static void
5690bind_screensaver(struct wl_client *client,
5691 void *data, uint32_t version, uint32_t id)
5692{
Tiago Vignattibe143262012-04-16 17:31:41 +03005693 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005694 struct wl_resource *resource;
5695
Jason Ekstranda85118c2013-06-27 20:17:02 -05005696 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005697
Pekka Paalanen77346a62011-11-30 16:26:35 +02005698 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005699 wl_resource_set_implementation(resource,
5700 &screensaver_implementation,
5701 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005702 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005703 return;
5704 }
5705
5706 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5707 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005708}
5709
Kristian Høgsberg07045392012-02-19 18:52:44 -05005710struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005711 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005712 struct weston_surface *current;
5713 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005714 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005715 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005716};
5717
5718static void
5719switcher_next(struct switcher *switcher)
5720{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005721 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005722 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005723 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005724 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005725
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005726 /* temporary re-display minimized surfaces */
5727 struct weston_view *tmp;
5728 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005729 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5730 weston_layer_entry_remove(&view->layer_link);
5731 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005732 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5733 *minimized = view;
5734 }
5735
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005736 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005737 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005738 if (shsurf &&
5739 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5740 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005741 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005742 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005743 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005744 next = view->surface;
5745 prev = view->surface;
5746 view->alpha = 0.25;
5747 weston_view_geometry_dirty(view);
5748 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005749 }
Alex Wu1659daa2012-04-01 20:13:09 +08005750
Jason Ekstranda7af7042013-10-12 22:38:11 -05005751 if (is_black_surface(view->surface, NULL)) {
5752 view->alpha = 0.25;
5753 weston_view_geometry_dirty(view);
5754 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005755 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005756 }
5757
5758 if (next == NULL)
5759 next = first;
5760
Alex Wu07b26062012-03-12 16:06:01 +08005761 if (next == NULL)
5762 return;
5763
Kristian Høgsberg07045392012-02-19 18:52:44 -05005764 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005765 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005766
5767 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005768 wl_list_for_each(view, &next->views, surface_link)
5769 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005770
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005771 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005772 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005773 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005774}
5775
5776static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005777switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005778{
5779 struct switcher *switcher =
5780 container_of(listener, struct switcher, listener);
5781
5782 switcher_next(switcher);
5783}
5784
5785static void
Daniel Stone351eb612012-05-31 15:27:47 -04005786switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005787{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005788 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005789 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005790 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005791
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005792 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005793 if (is_focus_view(view))
5794 continue;
5795
Jason Ekstranda7af7042013-10-12 22:38:11 -05005796 view->alpha = 1.0;
5797 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005798 }
5799
Alex Wu07b26062012-03-12 16:06:01 +08005800 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005801 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005802 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005803 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005804 weston_keyboard_end_grab(keyboard);
5805 if (keyboard->input_method_resource)
5806 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005807
5808 /* re-hide surfaces that were temporary shown during the switch */
5809 struct weston_view **minimized;
5810 wl_array_for_each(minimized, &switcher->minimized_array) {
5811 /* with the exception of the current selected */
5812 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005813 weston_layer_entry_remove(&(*minimized)->layer_link);
5814 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005815 weston_view_damage_below(*minimized);
5816 }
5817 }
5818 wl_array_release(&switcher->minimized_array);
5819
Kristian Høgsberg07045392012-02-19 18:52:44 -05005820 free(switcher);
5821}
5822
5823static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005824switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005825 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005826{
5827 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005828 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005829
Daniel Stonec9785ea2012-05-30 16:31:52 +01005830 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005831 switcher_next(switcher);
5832}
5833
5834static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005835switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005836 uint32_t mods_depressed, uint32_t mods_latched,
5837 uint32_t mods_locked, uint32_t group)
5838{
5839 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005840 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005841
Daniel Stone351eb612012-05-31 15:27:47 -04005842 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5843 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005844}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005845
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005846static void
5847switcher_cancel(struct weston_keyboard_grab *grab)
5848{
5849 struct switcher *switcher = container_of(grab, struct switcher, grab);
5850
5851 switcher_destroy(switcher);
5852}
5853
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005854static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005855 switcher_key,
5856 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005857 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005858};
5859
5860static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005861switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005862 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005863{
Tiago Vignattibe143262012-04-16 17:31:41 +03005864 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005865 struct switcher *switcher;
5866
5867 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005868 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005869 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005870 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005871 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005872 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005873
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005874 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005875 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005876 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005877 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5878 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005879 switcher_next(switcher);
5880}
5881
Pekka Paalanen3c647232011-12-22 13:43:43 +02005882static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005883backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005884 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005885{
5886 struct weston_compositor *compositor = data;
5887 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005888 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005889
5890 /* TODO: we're limiting to simple use cases, where we assume just
5891 * control on the primary display. We'd have to extend later if we
5892 * ever get support for setting backlights on random desktop LCD
5893 * panels though */
5894 output = get_default_output(compositor);
5895 if (!output)
5896 return;
5897
5898 if (!output->set_backlight)
5899 return;
5900
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005901 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5902 backlight_new = output->backlight_current - 25;
5903 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5904 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005905
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005906 if (backlight_new < 5)
5907 backlight_new = 5;
5908 if (backlight_new > 255)
5909 backlight_new = 255;
5910
5911 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005912 output->set_backlight(output, output->backlight_current);
5913}
5914
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005915struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005916 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005917 struct weston_seat *seat;
5918 uint32_t key[2];
5919 int key_released[2];
5920};
5921
5922static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005923debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005924 uint32_t key, uint32_t state)
5925{
5926 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005927 struct weston_compositor *ec = db->seat->compositor;
5928 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005929 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005930 uint32_t serial;
5931 int send = 0, terminate = 0;
5932 int check_binding = 1;
5933 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005934 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005935
5936 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5937 /* Do not run bindings on key releases */
5938 check_binding = 0;
5939
5940 for (i = 0; i < 2; i++)
5941 if (key == db->key[i])
5942 db->key_released[i] = 1;
5943
5944 if (db->key_released[0] && db->key_released[1]) {
5945 /* All key releases been swalled so end the grab */
5946 terminate = 1;
5947 } else if (key != db->key[0] && key != db->key[1]) {
5948 /* Should not swallow release of other keys */
5949 send = 1;
5950 }
5951 } else if (key == db->key[0] && !db->key_released[0]) {
5952 /* Do not check bindings for the first press of the binding
5953 * key. This allows it to be used as a debug shortcut.
5954 * We still need to swallow this event. */
5955 check_binding = 0;
5956 } else if (db->key[1]) {
5957 /* If we already ran a binding don't process another one since
5958 * we can't keep track of all the binding keys that were
5959 * pressed in order to swallow the release events. */
5960 send = 1;
5961 check_binding = 0;
5962 }
5963
5964 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005965 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5966 key, state)) {
5967 /* We ran a binding so swallow the press and keep the
5968 * grab to swallow the released too. */
5969 send = 0;
5970 terminate = 0;
5971 db->key[1] = key;
5972 } else {
5973 /* Terminate the grab since the key pressed is not a
5974 * debug binding key. */
5975 send = 1;
5976 terminate = 1;
5977 }
5978 }
5979
5980 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005981 serial = wl_display_next_serial(display);
5982 resource_list = &grab->keyboard->focus_resource_list;
5983 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005984 wl_keyboard_send_key(resource, serial, time, key, state);
5985 }
5986 }
5987
5988 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005989 weston_keyboard_end_grab(grab->keyboard);
5990 if (grab->keyboard->input_method_resource)
5991 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005992 free(db);
5993 }
5994}
5995
5996static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005997debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005998 uint32_t mods_depressed, uint32_t mods_latched,
5999 uint32_t mods_locked, uint32_t group)
6000{
6001 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01006002 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006003
Neil Roberts96d790e2013-09-19 17:32:00 +01006004 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006005
Neil Roberts96d790e2013-09-19 17:32:00 +01006006 wl_resource_for_each(resource, resource_list) {
6007 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
6008 mods_latched, mods_locked, group);
6009 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006010}
6011
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006012static void
6013debug_binding_cancel(struct weston_keyboard_grab *grab)
6014{
6015 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
6016
6017 weston_keyboard_end_grab(grab->keyboard);
6018 free(db);
6019}
6020
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006021struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006022 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006023 debug_binding_modifiers,
6024 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006025};
6026
6027static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006028debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006029{
6030 struct debug_binding_grab *grab;
6031
6032 grab = calloc(1, sizeof *grab);
6033 if (!grab)
6034 return;
6035
6036 grab->seat = (struct weston_seat *) seat;
6037 grab->key[0] = key;
6038 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006039 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006040}
6041
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006042static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006043force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006044 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006045{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006046 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006047 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006048 struct desktop_shell *shell = data;
6049 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006050 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006051
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006052 focus_surface = seat->keyboard->focus;
6053 if (!focus_surface)
6054 return;
6055
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006056 wl_signal_emit(&compositor->kill_signal, focus_surface);
6057
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006058 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006059 wl_client_get_credentials(client, &pid, NULL, NULL);
6060
6061 /* Skip clients that we launched ourselves (the credentials of
6062 * the socketpair is ours) */
6063 if (pid == getpid())
6064 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006065
Daniel Stone325fc2d2012-05-30 16:31:58 +01006066 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006067}
6068
6069static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006070workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006071 uint32_t key, void *data)
6072{
6073 struct desktop_shell *shell = data;
6074 unsigned int new_index = shell->workspaces.current;
6075
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006076 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006077 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006078 if (new_index != 0)
6079 new_index--;
6080
6081 change_workspace(shell, new_index);
6082}
6083
6084static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006085workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006086 uint32_t key, void *data)
6087{
6088 struct desktop_shell *shell = data;
6089 unsigned int new_index = shell->workspaces.current;
6090
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006091 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006092 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006093 if (new_index < shell->workspaces.num - 1)
6094 new_index++;
6095
6096 change_workspace(shell, new_index);
6097}
6098
6099static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006100workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006101 uint32_t key, void *data)
6102{
6103 struct desktop_shell *shell = data;
6104 unsigned int new_index;
6105
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006106 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006107 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006108 new_index = key - KEY_F1;
6109 if (new_index >= shell->workspaces.num)
6110 new_index = shell->workspaces.num - 1;
6111
6112 change_workspace(shell, new_index);
6113}
6114
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006115static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006116workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006117 uint32_t key, void *data)
6118{
6119 struct desktop_shell *shell = data;
6120 unsigned int new_index = shell->workspaces.current;
6121
6122 if (shell->locked)
6123 return;
6124
6125 if (new_index != 0)
6126 new_index--;
6127
6128 take_surface_to_workspace_by_seat(shell, seat, new_index);
6129}
6130
6131static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006132workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006133 uint32_t key, void *data)
6134{
6135 struct desktop_shell *shell = data;
6136 unsigned int new_index = shell->workspaces.current;
6137
6138 if (shell->locked)
6139 return;
6140
6141 if (new_index < shell->workspaces.num - 1)
6142 new_index++;
6143
6144 take_surface_to_workspace_by_seat(shell, seat, new_index);
6145}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006146
6147static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006148shell_reposition_view_on_output_destroy(struct weston_view *view)
6149{
6150 struct weston_output *output, *first_output;
6151 struct weston_compositor *ec = view->surface->compositor;
6152 struct shell_surface *shsurf;
6153 float x, y;
6154 int visible;
6155
6156 x = view->geometry.x;
6157 y = view->geometry.y;
6158
6159 /* At this point the destroyed output is not in the list anymore.
6160 * If the view is still visible somewhere, we leave where it is,
6161 * otherwise, move it to the first output. */
6162 visible = 0;
6163 wl_list_for_each(output, &ec->output_list, link) {
6164 if (pixman_region32_contains_point(&output->region,
6165 x, y, NULL)) {
6166 visible = 1;
6167 break;
6168 }
6169 }
6170
6171 if (!visible) {
6172 first_output = container_of(ec->output_list.next,
6173 struct weston_output, link);
6174
6175 x = first_output->x + first_output->width / 4;
6176 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006177
6178 weston_view_set_position(view, x, y);
6179 } else {
6180 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006181 }
6182
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006183
6184 shsurf = get_shell_surface(view->surface);
6185
6186 if (shsurf) {
6187 shsurf->saved_position_valid = false;
6188 shsurf->next_state.maximized = false;
6189 shsurf->next_state.fullscreen = false;
6190 shsurf->state_changed = true;
6191 }
6192}
6193
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006194void
6195shell_for_each_layer(struct desktop_shell *shell,
6196 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006197{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006198 struct workspace **ws;
6199
6200 func(shell, &shell->fullscreen_layer, data);
6201 func(shell, &shell->panel_layer, data);
6202 func(shell, &shell->background_layer, data);
6203 func(shell, &shell->lock_layer, data);
6204 func(shell, &shell->input_panel_layer, data);
6205
6206 wl_array_for_each(ws, &shell->workspaces.array)
6207 func(shell, &(*ws)->layer, data);
6208}
6209
6210static void
6211shell_output_destroy_move_layer(struct desktop_shell *shell,
6212 struct weston_layer *layer,
6213 void *data)
6214{
6215 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006216 struct weston_view *view;
6217
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006218 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006219 if (view->output != output)
6220 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006221
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006222 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006223 }
6224}
6225
6226static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006227handle_output_destroy(struct wl_listener *listener, void *data)
6228{
6229 struct shell_output *output_listener =
6230 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006231 struct weston_output *output = output_listener->output;
6232 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006233
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006234 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006235
Xiong Zhang6b481422013-10-23 13:58:32 +08006236 wl_list_remove(&output_listener->destroy_listener.link);
6237 wl_list_remove(&output_listener->link);
6238 free(output_listener);
6239}
6240
6241static void
6242create_shell_output(struct desktop_shell *shell,
6243 struct weston_output *output)
6244{
6245 struct shell_output *shell_output;
6246
6247 shell_output = zalloc(sizeof *shell_output);
6248 if (shell_output == NULL)
6249 return;
6250
6251 shell_output->output = output;
6252 shell_output->shell = shell;
6253 shell_output->destroy_listener.notify = handle_output_destroy;
6254 wl_signal_add(&output->destroy_signal,
6255 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006256 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006257}
6258
6259static void
6260handle_output_create(struct wl_listener *listener, void *data)
6261{
6262 struct desktop_shell *shell =
6263 container_of(listener, struct desktop_shell, output_create_listener);
6264 struct weston_output *output = (struct weston_output *)data;
6265
6266 create_shell_output(shell, output);
6267}
6268
6269static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006270handle_output_move_layer(struct desktop_shell *shell,
6271 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006272{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006273 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006274 struct weston_view *view;
6275 float x, y;
6276
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006277 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006278 if (view->output != output)
6279 continue;
6280
6281 x = view->geometry.x + output->move_x;
6282 y = view->geometry.y + output->move_y;
6283 weston_view_set_position(view, x, y);
6284 }
6285}
6286
6287static void
6288handle_output_move(struct wl_listener *listener, void *data)
6289{
6290 struct desktop_shell *shell;
6291
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006292 shell = container_of(listener, struct desktop_shell,
6293 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006294
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006295 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006296}
6297
6298static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006299setup_output_destroy_handler(struct weston_compositor *ec,
6300 struct desktop_shell *shell)
6301{
6302 struct weston_output *output;
6303
6304 wl_list_init(&shell->output_list);
6305 wl_list_for_each(output, &ec->output_list, link)
6306 create_shell_output(shell, output);
6307
6308 shell->output_create_listener.notify = handle_output_create;
6309 wl_signal_add(&ec->output_created_signal,
6310 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006311
6312 shell->output_move_listener.notify = handle_output_move;
6313 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006314}
6315
6316static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006317shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006318{
Tiago Vignattibe143262012-04-16 17:31:41 +03006319 struct desktop_shell *shell =
6320 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006321 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006322 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006323
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006324 /* Force state to unlocked so we don't try to fade */
6325 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006326
6327 if (shell->child.client) {
6328 /* disable respawn */
6329 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006330 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006331 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006332
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006333 wl_list_remove(&shell->idle_listener.link);
6334 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006335
6336 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006337
Xiong Zhang6b481422013-10-23 13:58:32 +08006338 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6339 wl_list_remove(&shell_output->destroy_listener.link);
6340 wl_list_remove(&shell_output->link);
6341 free(shell_output);
6342 }
6343
6344 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006345 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006346
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006347 wl_array_for_each(ws, &shell->workspaces.array)
6348 workspace_destroy(*ws);
6349 wl_array_release(&shell->workspaces.array);
6350
Pekka Paalanen3c647232011-12-22 13:43:43 +02006351 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006352 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006353 free(shell);
6354}
6355
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006356static void
6357shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6358{
6359 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006360 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006361
6362 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006363 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6364 MODIFIER_CTRL | MODIFIER_ALT,
6365 terminate_binding, ec);
6366 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6367 click_to_activate_binding,
6368 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006369 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6370 click_to_activate_binding,
6371 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006372 weston_compositor_add_touch_binding(ec, 0,
6373 touch_to_activate_binding,
6374 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006375 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6376 MODIFIER_SUPER | MODIFIER_ALT,
6377 surface_opacity_binding, NULL);
6378 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6379 MODIFIER_SUPER, zoom_axis_binding,
6380 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006381
6382 /* configurable bindings */
6383 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006384 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6385 zoom_key_binding, NULL);
6386 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6387 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006388 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6389 maximize_binding, NULL);
6390 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6391 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006392 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6393 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006394 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006395 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6396 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006397 weston_compositor_add_button_binding(ec, BTN_LEFT,
6398 mod | MODIFIER_SHIFT,
6399 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006400
6401 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6402 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6403 rotate_binding, NULL);
6404
Daniel Stone325fc2d2012-05-30 16:31:58 +01006405 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6406 shell);
6407 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6408 ec);
6409 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6410 backlight_binding, ec);
6411 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6412 ec);
6413 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6414 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006415 weston_compositor_add_key_binding(ec, KEY_K, mod,
6416 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006417 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6418 workspace_up_binding, shell);
6419 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6420 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006421 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6422 workspace_move_surface_up_binding,
6423 shell);
6424 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6425 workspace_move_surface_down_binding,
6426 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006427
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006428 if (shell->exposay_modifier)
6429 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6430 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006431
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006432 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6433 if (shell->workspaces.num > 1) {
6434 num_workspace_bindings = shell->workspaces.num;
6435 if (num_workspace_bindings > 6)
6436 num_workspace_bindings = 6;
6437 for (i = 0; i < num_workspace_bindings; i++)
6438 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6439 workspace_f_binding,
6440 shell);
6441 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006442
6443 /* Debug bindings */
6444 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6445 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006446}
6447
Jason Ekstrand024177c2014-04-21 19:42:58 -05006448static void
6449handle_seat_created(struct wl_listener *listener, void *data)
6450{
6451 struct weston_seat *seat = data;
6452
6453 create_shell_seat(seat);
6454}
6455
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006456WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006457module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006458 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006459{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006460 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006461 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006462 struct workspace **pws;
6463 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006464 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006465
Peter Huttererf3d62272013-08-08 11:57:05 +10006466 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006467 if (shell == NULL)
6468 return -1;
6469
Kristian Høgsberg75840622011-09-06 13:48:16 -04006470 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006471
6472 shell->destroy_listener.notify = shell_destroy;
6473 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006474 shell->idle_listener.notify = idle_handler;
6475 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6476 shell->wake_listener.notify = wake_handler;
6477 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006478
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006479 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006480 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006481 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006482 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006483 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006484 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006485 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006486 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006487 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006488 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006489 ec->shell_interface.set_window_geometry = set_window_geometry;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006490
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006491 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6492 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006493 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6494 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006495 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006496
6497 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006498 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006499
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006500 if (input_panel_setup(shell) < 0)
6501 return -1;
6502
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006503 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006504
Daniel Stonedf8133b2013-11-19 11:37:14 +01006505 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6506 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6507
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006508 for (i = 0; i < shell->workspaces.num; i++) {
6509 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6510 if (pws == NULL)
6511 return -1;
6512
6513 *pws = workspace_create();
6514 if (*pws == NULL)
6515 return -1;
6516 }
6517 activate_workspace(shell, 0);
6518
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006519 weston_layer_init(&shell->minimized_layer, NULL);
6520
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006521 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006522 wl_list_init(&shell->workspaces.animation.link);
6523 shell->workspaces.animation.frame = animate_workspace_change_frame;
6524
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006525 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006526 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006527 return -1;
6528
Rafael Antognollie2a34552013-12-03 15:35:45 -02006529 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6530 shell, bind_xdg_shell) == NULL)
6531 return -1;
6532
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006533 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006534 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006535 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006536 return -1;
6537
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006538 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6539 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006540 return -1;
6541
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006542 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6543 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006544 return -1;
6545
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006546 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006547
Jonny Lamb765760d2014-08-20 15:53:19 +02006548 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6549
Xiong Zhang6b481422013-10-23 13:58:32 +08006550 setup_output_destroy_handler(ec, shell);
6551
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006552 loop = wl_display_get_event_loop(ec->wl_display);
6553 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006554
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006555 shell->screensaver.timer =
6556 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6557
Jason Ekstrand024177c2014-04-21 19:42:58 -05006558 wl_list_for_each(seat, &ec->seat_list, link)
6559 handle_seat_created(NULL, seat);
6560 shell->seat_create_listener.notify = handle_seat_created;
6561 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006562
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006563 screenshooter_create(ec);
6564
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006565 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006566
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006567 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006568
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006569 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6570
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006571 return 0;
6572}