blob: 810961f876a36e08dbd77fef0b36ff375d4b2cec [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 Paalanen974c0942014-09-05 14:45:09 +0300578 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200579
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400580 section = weston_config_get_section(shell->compositor->config,
581 "screensaver", NULL, NULL);
582 weston_config_section_get_string(section,
583 "path", &shell->screensaver.path, NULL);
584 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200585 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400586
587 section = weston_config_get_section(shell->compositor->config,
588 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300589 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
590 WESTON_SHELL_CLIENT);
591 if (ret < 0)
592 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400593 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500594 "client", &s, client);
595 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100596 shell->client = s;
597 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400598 "binding-modifier", &s, "super");
599 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200600 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800601
602 weston_config_section_get_string(section,
603 "exposay-modifier", &s, "none");
604 if (strcmp(s, "none") == 0)
605 shell->exposay_modifier = 0;
606 else
607 shell->exposay_modifier = get_modifier(s);
608 free(s);
609
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400610 weston_config_section_get_string(section, "animation", &s, "none");
611 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200612 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200613 weston_config_section_get_string(section, "close-animation", &s, "fade");
614 shell->win_close_animation_type = get_animation_type(s);
615 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700616 weston_config_section_get_string(section,
617 "startup-animation", &s, "fade");
618 shell->startup_animation_type = get_animation_type(s);
619 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700620 if (shell->startup_animation_type == ANIMATION_ZOOM)
621 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100622 weston_config_section_get_string(section, "focus-animation", &s, "none");
623 shell->focus_animation_type = get_animation_type(s);
624 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400625 weston_config_section_get_uint(section, "num-workspaces",
626 &shell->workspaces.num,
627 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200628}
629
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800630struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100631get_default_output(struct weston_compositor *compositor)
632{
633 return container_of(compositor->output_list.next,
634 struct weston_output, link);
635}
636
637
638/* no-op func for checking focus surface */
639static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600640focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100641{
642}
643
644static struct focus_surface *
645get_focus_surface(struct weston_surface *surface)
646{
647 if (surface->configure == focus_surface_configure)
648 return surface->configure_private;
649 else
650 return NULL;
651}
652
653static bool
654is_focus_surface (struct weston_surface *es)
655{
656 return (es->configure == focus_surface_configure);
657}
658
659static bool
660is_focus_view (struct weston_view *view)
661{
662 return is_focus_surface (view->surface);
663}
664
665static struct focus_surface *
666create_focus_surface(struct weston_compositor *ec,
667 struct weston_output *output)
668{
669 struct focus_surface *fsurf = NULL;
670 struct weston_surface *surface = NULL;
671
672 fsurf = malloc(sizeof *fsurf);
673 if (!fsurf)
674 return NULL;
675
676 fsurf->surface = weston_surface_create(ec);
677 surface = fsurf->surface;
678 if (surface == NULL) {
679 free(fsurf);
680 return NULL;
681 }
682
683 surface->configure = focus_surface_configure;
684 surface->output = output;
685 surface->configure_private = fsurf;
686
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800687 fsurf->view = weston_view_create(surface);
688 if (fsurf->view == NULL) {
689 weston_surface_destroy(surface);
690 free(fsurf);
691 return NULL;
692 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100693 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100694
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600695 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600696 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100697 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
698 pixman_region32_fini(&surface->opaque);
699 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
700 output->width, output->height);
701 pixman_region32_fini(&surface->input);
702 pixman_region32_init(&surface->input);
703
704 wl_list_init(&fsurf->workspace_transform.link);
705
706 return fsurf;
707}
708
709static void
710focus_surface_destroy(struct focus_surface *fsurf)
711{
712 weston_surface_destroy(fsurf->surface);
713 free(fsurf);
714}
715
716static void
717focus_animation_done(struct weston_view_animation *animation, void *data)
718{
719 struct workspace *ws = data;
720
721 ws->focus_animation = NULL;
722}
723
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200724static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200725focus_state_destroy(struct focus_state *state)
726{
727 wl_list_remove(&state->seat_destroy_listener.link);
728 wl_list_remove(&state->surface_destroy_listener.link);
729 free(state);
730}
731
732static void
733focus_state_seat_destroy(struct wl_listener *listener, void *data)
734{
735 struct focus_state *state = container_of(listener,
736 struct focus_state,
737 seat_destroy_listener);
738
739 wl_list_remove(&state->link);
740 focus_state_destroy(state);
741}
742
743static void
744focus_state_surface_destroy(struct wl_listener *listener, void *data)
745{
746 struct focus_state *state = container_of(listener,
747 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400748 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400749 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500750 struct weston_surface *main_surface, *next;
751 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200752
Pekka Paalanen01388e22013-04-25 13:57:44 +0300753 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
754
Kristian Høgsberge3778222012-07-31 17:29:30 -0400755 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300756 wl_list_for_each(view,
757 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500758 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400759 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100760 if (is_focus_view(view))
761 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400762
Jason Ekstranda7af7042013-10-12 22:38:11 -0500763 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400764 break;
765 }
766
Pekka Paalanen01388e22013-04-25 13:57:44 +0300767 /* if the focus was a sub-surface, activate its main surface */
768 if (main_surface != state->keyboard_focus)
769 next = main_surface;
770
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100771 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400772 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100773 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100774 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400775 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100776 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
777 if (state->ws->focus_animation)
778 weston_view_animation_destroy(state->ws->focus_animation);
779
780 state->ws->focus_animation = weston_fade_run(
781 state->ws->fsurf_front->view,
782 state->ws->fsurf_front->view->alpha, 0.0, 300,
783 focus_animation_done, state->ws);
784 }
785
Kristian Høgsberge3778222012-07-31 17:29:30 -0400786 wl_list_remove(&state->link);
787 focus_state_destroy(state);
788 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200789}
790
791static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400792focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200793{
Jonas Ådahl04769742012-06-13 00:01:24 +0200794 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200795
796 state = malloc(sizeof *state);
797 if (state == NULL)
798 return NULL;
799
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100800 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400801 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200802 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400803 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200804
805 state->seat_destroy_listener.notify = focus_state_seat_destroy;
806 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400807 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200808 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400809 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200810
811 return state;
812}
813
Jonas Ådahl8538b222012-08-29 22:13:03 +0200814static struct focus_state *
815ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
816{
817 struct workspace *ws = get_current_workspace(shell);
818 struct focus_state *state;
819
820 wl_list_for_each(state, &ws->focus_list, link)
821 if (state->seat == seat)
822 break;
823
824 if (&state->link == &ws->focus_list)
825 state = focus_state_create(seat, ws);
826
827 return state;
828}
829
Jonas Ådahl04769742012-06-13 00:01:24 +0200830static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800831focus_state_set_focus(struct focus_state *state,
832 struct weston_surface *surface)
833{
834 if (state->keyboard_focus) {
835 wl_list_remove(&state->surface_destroy_listener.link);
836 wl_list_init(&state->surface_destroy_listener.link);
837 }
838
839 state->keyboard_focus = surface;
840 if (surface)
841 wl_signal_add(&surface->destroy_signal,
842 &state->surface_destroy_listener);
843}
844
845static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400846restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200847{
848 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400849 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100850 struct wl_list pending_seat_list;
851 struct weston_seat *seat, *next_seat;
852
853 /* Temporarily steal the list of seats so that we can keep
854 * track of the seats we've already processed */
855 wl_list_init(&pending_seat_list);
856 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
857 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200858
859 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100860 wl_list_remove(&state->seat->link);
861 wl_list_insert(&shell->compositor->seat_list,
862 &state->seat->link);
863
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800864 if (state->seat->keyboard == NULL)
865 continue;
866
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400867 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200868
Kristian Høgsberge3148752013-05-06 23:19:49 -0400869 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200870 }
Neil Roberts4237f502014-04-09 16:33:31 +0100871
872 /* For any remaining seats that we don't have a focus state
873 * for we'll reset the keyboard focus to NULL */
874 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
875 wl_list_insert(&shell->compositor->seat_list, &seat->link);
876
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300877 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100878 continue;
879
880 weston_keyboard_set_focus(seat->keyboard, NULL);
881 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200882}
883
884static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200885replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
886 struct weston_seat *seat)
887{
888 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200889
890 wl_list_for_each(state, &ws->focus_list, link) {
891 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800892 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200893 return;
894 }
895 }
896}
897
898static void
899drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
900 struct weston_surface *surface)
901{
902 struct focus_state *state;
903
904 wl_list_for_each(state, &ws->focus_list, link)
905 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800906 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200907}
908
909static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100910animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
911 struct weston_view *from, struct weston_view *to)
912{
913 struct weston_output *output;
914 bool focus_surface_created = false;
915
916 /* FIXME: Only support dim animation using two layers */
917 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
918 return;
919
920 output = get_default_output(shell->compositor);
921 if (ws->fsurf_front == NULL && (from || to)) {
922 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800923 if (ws->fsurf_front == NULL)
924 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100925 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800926
927 ws->fsurf_back = create_focus_surface(shell->compositor, output);
928 if (ws->fsurf_back == NULL) {
929 focus_surface_destroy(ws->fsurf_front);
930 return;
931 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100932 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800933
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100934 focus_surface_created = true;
935 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300936 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
937 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100938 }
939
940 if (ws->focus_animation) {
941 weston_view_animation_destroy(ws->focus_animation);
942 ws->focus_animation = NULL;
943 }
944
945 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300946 weston_layer_entry_insert(&to->layer_link,
947 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100948 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300949 weston_layer_entry_insert(&ws->layer.view_list,
950 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100951
952 if (focus_surface_created) {
953 ws->focus_animation = weston_fade_run(
954 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200955 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100956 focus_animation_done, ws);
957 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300958 weston_layer_entry_insert(&from->layer_link,
959 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100960 ws->focus_animation = weston_stable_fade_run(
961 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200962 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100963 focus_animation_done, ws);
964 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300965 weston_layer_entry_insert(&ws->layer.view_list,
966 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100967 ws->focus_animation = weston_stable_fade_run(
968 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200969 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100970 focus_animation_done, ws);
971 }
972}
973
974static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200975workspace_destroy(struct workspace *ws)
976{
Jonas Ådahl04769742012-06-13 00:01:24 +0200977 struct focus_state *state, *next;
978
979 wl_list_for_each_safe(state, next, &ws->focus_list, link)
980 focus_state_destroy(state);
981
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100982 if (ws->fsurf_front)
983 focus_surface_destroy(ws->fsurf_front);
984 if (ws->fsurf_back)
985 focus_surface_destroy(ws->fsurf_back);
986
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200987 free(ws);
988}
989
Jonas Ådahl04769742012-06-13 00:01:24 +0200990static void
991seat_destroyed(struct wl_listener *listener, void *data)
992{
993 struct weston_seat *seat = data;
994 struct focus_state *state, *next;
995 struct workspace *ws = container_of(listener,
996 struct workspace,
997 seat_destroyed_listener);
998
999 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1000 if (state->seat == seat)
1001 wl_list_remove(&state->link);
1002}
1003
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001004static struct workspace *
1005workspace_create(void)
1006{
1007 struct workspace *ws = malloc(sizeof *ws);
1008 if (ws == NULL)
1009 return NULL;
1010
1011 weston_layer_init(&ws->layer, NULL);
1012
Jonas Ådahl04769742012-06-13 00:01:24 +02001013 wl_list_init(&ws->focus_list);
1014 wl_list_init(&ws->seat_destroyed_listener.link);
1015 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001016 ws->fsurf_front = NULL;
1017 ws->fsurf_back = NULL;
1018 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001019
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001020 return ws;
1021}
1022
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001023static int
1024workspace_is_empty(struct workspace *ws)
1025{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001026 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001027}
1028
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001029static struct workspace *
1030get_workspace(struct desktop_shell *shell, unsigned int index)
1031{
1032 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001033 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001034 pws += index;
1035 return *pws;
1036}
1037
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001038struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001039get_current_workspace(struct desktop_shell *shell)
1040{
1041 return get_workspace(shell, shell->workspaces.current);
1042}
1043
1044static void
1045activate_workspace(struct desktop_shell *shell, unsigned int index)
1046{
1047 struct workspace *ws;
1048
1049 ws = get_workspace(shell, index);
1050 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1051
1052 shell->workspaces.current = index;
1053}
1054
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055static unsigned int
1056get_output_height(struct weston_output *output)
1057{
1058 return abs(output->region.extents.y1 - output->region.extents.y2);
1059}
1060
1061static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001062view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001063{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001064 struct weston_transform *transform;
1065
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001066 if (is_focus_view(view)) {
1067 struct focus_surface *fsurf = get_focus_surface(view->surface);
1068 transform = &fsurf->workspace_transform;
1069 } else {
1070 struct shell_surface *shsurf = get_shell_surface(view->surface);
1071 transform = &shsurf->workspace_transform;
1072 }
1073
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001074 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001076 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001077
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001078 weston_matrix_init(&transform->matrix);
1079 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001081 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001082}
1083
1084static void
1085workspace_translate_out(struct workspace *ws, double fraction)
1086{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001088 unsigned int height;
1089 double d;
1090
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001091 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001092 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001093 d = height * fraction;
1094
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001095 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001096 }
1097}
1098
1099static void
1100workspace_translate_in(struct workspace *ws, double fraction)
1101{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001102 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001103 unsigned int height;
1104 double d;
1105
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001106 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001107 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001108
1109 if (fraction > 0)
1110 d = -(height - height * fraction);
1111 else
1112 d = height + height * fraction;
1113
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001114 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001115 }
1116}
1117
1118static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001119broadcast_current_workspace_state(struct desktop_shell *shell)
1120{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001121 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001122
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001123 wl_resource_for_each(resource, &shell->workspaces.client_list)
1124 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001125 shell->workspaces.current,
1126 shell->workspaces.num);
1127}
1128
1129static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130reverse_workspace_change_animation(struct desktop_shell *shell,
1131 unsigned int index,
1132 struct workspace *from,
1133 struct workspace *to)
1134{
1135 shell->workspaces.current = index;
1136
1137 shell->workspaces.anim_to = to;
1138 shell->workspaces.anim_from = from;
1139 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1140 shell->workspaces.anim_timestamp = 0;
1141
Scott Moreau4272e632012-08-13 09:58:41 -06001142 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143}
1144
1145static void
1146workspace_deactivate_transforms(struct workspace *ws)
1147{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001149 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001150
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001151 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001152 if (is_focus_view(view)) {
1153 struct focus_surface *fsurf = get_focus_surface(view->surface);
1154 transform = &fsurf->workspace_transform;
1155 } else {
1156 struct shell_surface *shsurf = get_shell_surface(view->surface);
1157 transform = &shsurf->workspace_transform;
1158 }
1159
1160 if (!wl_list_empty(&transform->link)) {
1161 wl_list_remove(&transform->link);
1162 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001163 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001165 }
1166}
1167
1168static void
1169finish_workspace_change_animation(struct desktop_shell *shell,
1170 struct workspace *from,
1171 struct workspace *to)
1172{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001173 struct weston_view *view;
1174
Scott Moreau4272e632012-08-13 09:58:41 -06001175 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001176
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001177 /* Views that extend past the bottom of the output are still
1178 * visible after the workspace animation ends but before its layer
1179 * is hidden. In that case, we need to damage below those views so
1180 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001181 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001182 weston_view_damage_below(view);
1183
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001184 wl_list_remove(&shell->workspaces.animation.link);
1185 workspace_deactivate_transforms(from);
1186 workspace_deactivate_transforms(to);
1187 shell->workspaces.anim_to = NULL;
1188
1189 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1190}
1191
1192static void
1193animate_workspace_change_frame(struct weston_animation *animation,
1194 struct weston_output *output, uint32_t msecs)
1195{
1196 struct desktop_shell *shell =
1197 container_of(animation, struct desktop_shell,
1198 workspaces.animation);
1199 struct workspace *from = shell->workspaces.anim_from;
1200 struct workspace *to = shell->workspaces.anim_to;
1201 uint32_t t;
1202 double x, y;
1203
1204 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1205 finish_workspace_change_animation(shell, from, to);
1206 return;
1207 }
1208
1209 if (shell->workspaces.anim_timestamp == 0) {
1210 if (shell->workspaces.anim_current == 0.0)
1211 shell->workspaces.anim_timestamp = msecs;
1212 else
1213 shell->workspaces.anim_timestamp =
1214 msecs -
1215 /* Invers of movement function 'y' below. */
1216 (asin(1.0 - shell->workspaces.anim_current) *
1217 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1218 M_2_PI);
1219 }
1220
1221 t = msecs - shell->workspaces.anim_timestamp;
1222
1223 /*
1224 * x = [0, π/2]
1225 * y(x) = sin(x)
1226 */
1227 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1228 y = sin(x);
1229
1230 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001231 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001232
1233 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1234 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1235 shell->workspaces.anim_current = y;
1236
Scott Moreau4272e632012-08-13 09:58:41 -06001237 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001238 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001239 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001240 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001241}
1242
1243static void
1244animate_workspace_change(struct desktop_shell *shell,
1245 unsigned int index,
1246 struct workspace *from,
1247 struct workspace *to)
1248{
1249 struct weston_output *output;
1250
1251 int dir;
1252
1253 if (index > shell->workspaces.current)
1254 dir = -1;
1255 else
1256 dir = 1;
1257
1258 shell->workspaces.current = index;
1259
1260 shell->workspaces.anim_dir = dir;
1261 shell->workspaces.anim_from = from;
1262 shell->workspaces.anim_to = to;
1263 shell->workspaces.anim_current = 0.0;
1264 shell->workspaces.anim_timestamp = 0;
1265
1266 output = container_of(shell->compositor->output_list.next,
1267 struct weston_output, link);
1268 wl_list_insert(&output->animation_list,
1269 &shell->workspaces.animation.link);
1270
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001271 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001272
1273 workspace_translate_in(to, 0);
1274
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001275 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001276
Scott Moreau4272e632012-08-13 09:58:41 -06001277 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001278}
1279
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001280static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001281update_workspace(struct desktop_shell *shell, unsigned int index,
1282 struct workspace *from, struct workspace *to)
1283{
1284 shell->workspaces.current = index;
1285 wl_list_insert(&from->layer.link, &to->layer.link);
1286 wl_list_remove(&from->layer.link);
1287}
1288
1289static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001290change_workspace(struct desktop_shell *shell, unsigned int index)
1291{
1292 struct workspace *from;
1293 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001294 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001295
1296 if (index == shell->workspaces.current)
1297 return;
1298
1299 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001300 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001301 return;
1302
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001303 from = get_current_workspace(shell);
1304 to = get_workspace(shell, index);
1305
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001306 if (shell->workspaces.anim_from == to &&
1307 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001308 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001309 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001310 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001311 return;
1312 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001313
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001314 if (shell->workspaces.anim_to != NULL)
1315 finish_workspace_change_animation(shell,
1316 shell->workspaces.anim_from,
1317 shell->workspaces.anim_to);
1318
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001319 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001320
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001321 if (shell->focus_animation_type != ANIMATION_NONE) {
1322 wl_list_for_each(state, &from->focus_list, link)
1323 if (state->keyboard_focus)
1324 animate_focus_change(shell, from,
1325 get_default_view(state->keyboard_focus), NULL);
1326
1327 wl_list_for_each(state, &to->focus_list, link)
1328 if (state->keyboard_focus)
1329 animate_focus_change(shell, to,
1330 NULL, get_default_view(state->keyboard_focus));
1331 }
1332
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001333 if (workspace_is_empty(to) && workspace_is_empty(from))
1334 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001335 else
1336 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001337
1338 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001339}
1340
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001341static bool
1342workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1343{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001344 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001345 struct wl_list *e;
1346
1347 if (wl_list_empty(list))
1348 return false;
1349
1350 e = list->next;
1351
1352 if (e->next != list)
1353 return false;
1354
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001355 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001356}
1357
1358static void
Philip Withnall659163d2013-11-25 18:01:36 +00001359move_surface_to_workspace(struct desktop_shell *shell,
1360 struct shell_surface *shsurf,
1361 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001362{
1363 struct workspace *from;
1364 struct workspace *to;
1365 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001366 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001367 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001368
1369 if (workspace == shell->workspaces.current)
1370 return;
1371
Philip Withnall659163d2013-11-25 18:01:36 +00001372 view = get_default_view(shsurf->surface);
1373 if (!view)
1374 return;
1375
1376 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1377
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001378 if (workspace >= shell->workspaces.num)
1379 workspace = shell->workspaces.num - 1;
1380
Jonas Ådahle9d22502012-08-29 22:13:01 +02001381 from = get_current_workspace(shell);
1382 to = get_workspace(shell, workspace);
1383
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001384 weston_layer_entry_remove(&view->layer_link);
1385 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001386
Philip Withnall648a4dd2013-11-25 18:01:44 +00001387 shell_surface_update_child_surface_layers(shsurf);
1388
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001390 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1391 if (!seat->keyboard)
1392 continue;
1393
1394 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001396 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001397 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001398
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001400}
1401
1402static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001403take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001404 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001405 unsigned int index)
1406{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001407 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001409 struct shell_surface *shsurf;
1410 struct workspace *from;
1411 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001412 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001413
Pekka Paalanen01388e22013-04-25 13:57:44 +03001414 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415 view = get_default_view(surface);
1416 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001417 index == shell->workspaces.current ||
1418 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001419 return;
1420
1421 from = get_current_workspace(shell);
1422 to = get_workspace(shell, index);
1423
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001424 weston_layer_entry_remove(&view->layer_link);
1425 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001426
Philip Withnall659163d2013-11-25 18:01:36 +00001427 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001428 if (shsurf != NULL)
1429 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001430
Jonas Ådahle9d22502012-08-29 22:13:01 +02001431 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001432 drop_focus_state(shell, from, surface);
1433
1434 if (shell->workspaces.anim_from == to &&
1435 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001436 wl_list_remove(&to->layer.link);
1437 wl_list_insert(from->layer.link.prev, &to->layer.link);
1438
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001439 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001440 broadcast_current_workspace_state(shell);
1441
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001442 return;
1443 }
1444
1445 if (shell->workspaces.anim_to != NULL)
1446 finish_workspace_change_animation(shell,
1447 shell->workspaces.anim_from,
1448 shell->workspaces.anim_to);
1449
1450 if (workspace_is_empty(from) &&
1451 workspace_has_only(to, surface))
1452 update_workspace(shell, index, from, to);
1453 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001454 if (shsurf != NULL &&
1455 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001456 wl_list_insert(&shell->workspaces.anim_sticky_list,
1457 &shsurf->workspace_transform.link);
1458
1459 animate_workspace_change(shell, index, from, to);
1460 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001461
1462 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001463
1464 state = ensure_focus_state(shell, seat);
1465 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001466 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001467}
1468
1469static void
1470workspace_manager_move_surface(struct wl_client *client,
1471 struct wl_resource *resource,
1472 struct wl_resource *surface_resource,
1473 uint32_t workspace)
1474{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001475 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001476 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001477 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001478 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001479 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001480
Pekka Paalanen01388e22013-04-25 13:57:44 +03001481 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001482 shell_surface = get_shell_surface(main_surface);
1483 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001485
1486 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001487}
1488
1489static const struct workspace_manager_interface workspace_manager_implementation = {
1490 workspace_manager_move_surface,
1491};
1492
1493static void
1494unbind_resource(struct wl_resource *resource)
1495{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001496 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001497}
1498
1499static void
1500bind_workspace_manager(struct wl_client *client,
1501 void *data, uint32_t version, uint32_t id)
1502{
1503 struct desktop_shell *shell = data;
1504 struct wl_resource *resource;
1505
Jason Ekstranda85118c2013-06-27 20:17:02 -05001506 resource = wl_resource_create(client,
1507 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001508
1509 if (resource == NULL) {
1510 weston_log("couldn't add workspace manager object");
1511 return;
1512 }
1513
Jason Ekstranda85118c2013-06-27 20:17:02 -05001514 wl_resource_set_implementation(resource,
1515 &workspace_manager_implementation,
1516 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001517 wl_list_insert(&shell->workspaces.client_list,
1518 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001519
1520 workspace_manager_send_state(resource,
1521 shell->workspaces.current,
1522 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001523}
1524
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001525static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001526touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1527 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1528{
1529}
1530
1531static void
1532touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1533{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001534 struct weston_touch_move_grab *move =
1535 (struct weston_touch_move_grab *) container_of(
1536 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001537
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001538 if (touch_id == 0)
1539 move->active = 0;
1540
Jonas Ådahl9484b692013-12-02 22:05:03 +01001541 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001542 shell_touch_grab_end(&move->base);
1543 free(move);
1544 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001545}
1546
1547static void
1548touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1549 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1550{
1551 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1552 struct shell_surface *shsurf = move->base.shsurf;
1553 struct weston_surface *es;
1554 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1555 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1556
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001557 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001558 return;
1559
1560 es = shsurf->surface;
1561
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001562 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001563
1564 weston_compositor_schedule_repaint(es->compositor);
1565}
1566
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001567static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001568touch_move_grab_frame(struct weston_touch_grab *grab)
1569{
1570}
1571
1572static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001573touch_move_grab_cancel(struct weston_touch_grab *grab)
1574{
1575 struct weston_touch_move_grab *move =
1576 (struct weston_touch_move_grab *) container_of(
1577 grab, struct shell_touch_grab, grab);
1578
1579 shell_touch_grab_end(&move->base);
1580 free(move);
1581}
1582
Rusty Lynch1084da52013-08-15 09:10:08 -07001583static const struct weston_touch_grab_interface touch_move_grab_interface = {
1584 touch_move_grab_down,
1585 touch_move_grab_up,
1586 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001587 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001588 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001589};
1590
1591static int
1592surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1593{
1594 struct weston_touch_move_grab *move;
1595
1596 if (!shsurf)
1597 return -1;
1598
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001599 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001600 return 0;
1601
1602 move = malloc(sizeof *move);
1603 if (!move)
1604 return -1;
1605
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001606 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001608 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001610 seat->touch->grab_y;
1611
1612 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1613 seat->touch);
1614
1615 return 0;
1616}
1617
1618static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001619noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001620{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001621}
1622
1623static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001624constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1625{
1626 struct shell_surface *shsurf = move->base.shsurf;
1627 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001628 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001629 const int safety = 50;
1630
1631 x = wl_fixed_to_int(pointer->x + move->dx);
1632 y = wl_fixed_to_int(pointer->y + move->dy);
1633
Jonny Lambd73c6942014-08-20 15:53:20 +02001634 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1635 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1636 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001637
Jonny Lambd73c6942014-08-20 15:53:20 +02001638 bottom = y + shsurf->geometry.height;
1639 if (bottom - panel_height < safety)
1640 y = panel_height + safety -
1641 shsurf->geometry.height;
1642
1643 if (move->client_initiated &&
1644 y + shsurf->geometry.y < panel_height)
1645 y = panel_height - shsurf->geometry.y;
1646 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001647
1648 *cx = x;
1649 *cy = y;
1650}
1651
1652static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001653move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1654 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001655{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001656 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001657 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001658 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001659 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001660
1661 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001662 if (!shsurf)
1663 return;
1664
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001665 constrain_position(move, &cx, &cy);
1666
1667 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001668
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001670}
1671
1672static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001673move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001674 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001675{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001676 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1677 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001678 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001679 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001680
Daniel Stone4dbadb12012-05-30 16:31:51 +01001681 if (pointer->button_count == 0 &&
1682 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001683 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001684 free(grab);
1685 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001686}
1687
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001688static void
1689move_grab_cancel(struct weston_pointer_grab *grab)
1690{
1691 struct shell_grab *shell_grab =
1692 container_of(grab, struct shell_grab, grab);
1693
1694 shell_grab_end(shell_grab);
1695 free(grab);
1696}
1697
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001698static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001699 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001700 move_grab_motion,
1701 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001702 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001703};
1704
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001705static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001706surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1707 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708{
1709 struct weston_move_grab *move;
1710
1711 if (!shsurf)
1712 return -1;
1713
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001714 if (shsurf->grabbed ||
1715 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001716 return 0;
1717
1718 move = malloc(sizeof *move);
1719 if (!move)
1720 return -1;
1721
Jason Ekstranda7af7042013-10-12 22:38:11 -05001722 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001723 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001724 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001725 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001726 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727
1728 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001729 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001730
1731 return 0;
1732}
1733
1734static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001735common_surface_move(struct wl_resource *resource,
1736 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001737{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001738 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001739 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001740 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001741
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001742 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001743 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001744 seat->pointer->button_count > 0 &&
1745 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001747 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001748 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001749 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001750 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001751 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001752 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001753 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001754 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001755 (surface_touch_move(shsurf, seat) < 0))
1756 wl_resource_post_no_memory(resource);
1757 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001758}
1759
Rafael Antognollie2a34552013-12-03 15:35:45 -02001760static void
1761shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1762 struct wl_resource *seat_resource, uint32_t serial)
1763{
1764 common_surface_move(resource, seat_resource, serial);
1765}
1766
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001767struct weston_resize_grab {
1768 struct shell_grab base;
1769 uint32_t edges;
1770 int32_t width, height;
1771};
1772
1773static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001774resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1775 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001776{
1777 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001778 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001779 struct shell_surface *shsurf = resize->base.shsurf;
1780 int32_t width, height;
1781 wl_fixed_t from_x, from_y;
1782 wl_fixed_t to_x, to_y;
1783
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001784 weston_pointer_move(pointer, x, y);
1785
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001786 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001787 return;
1788
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789 weston_view_from_global_fixed(shsurf->view,
1790 pointer->grab_x, pointer->grab_y,
1791 &from_x, &from_y);
1792 weston_view_from_global_fixed(shsurf->view,
1793 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001794
1795 width = resize->width;
1796 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1797 width += wl_fixed_to_int(from_x - to_x);
1798 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1799 width += wl_fixed_to_int(to_x - from_x);
1800 }
1801
1802 height = resize->height;
1803 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1804 height += wl_fixed_to_int(from_y - to_y);
1805 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1806 height += wl_fixed_to_int(to_y - from_y);
1807 }
1808
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001809 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001810}
1811
1812static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001813send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001814{
1815 struct shell_surface *shsurf = get_shell_surface(surface);
1816
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001817 assert(shsurf);
1818
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001819 if (shsurf->resource)
1820 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001821 shsurf->resize_edges,
1822 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001823}
1824
1825static const struct weston_shell_client shell_client = {
1826 send_configure
1827};
1828
1829static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001830resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001831 uint32_t time, uint32_t button, uint32_t state_w)
1832{
1833 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001834 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001835 enum wl_pointer_button_state state = state_w;
1836
1837 if (pointer->button_count == 0 &&
1838 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1839 shell_grab_end(&resize->base);
1840 free(grab);
1841 }
1842}
1843
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001844static void
1845resize_grab_cancel(struct weston_pointer_grab *grab)
1846{
1847 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1848
1849 shell_grab_end(&resize->base);
1850 free(grab);
1851}
1852
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001853static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001854 noop_grab_focus,
1855 resize_grab_motion,
1856 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001857 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001858};
1859
Giulio Camuffob8366642013-04-25 13:57:46 +03001860/*
1861 * Returns the bounding box of a surface and all its sub-surfaces,
1862 * in the surface coordinates system. */
1863static void
1864surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1865 int32_t *y, int32_t *w, int32_t *h) {
1866 pixman_region32_t region;
1867 pixman_box32_t *box;
1868 struct weston_subsurface *subsurface;
1869
1870 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871 surface->width,
1872 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001873
1874 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1875 pixman_region32_union_rect(&region, &region,
1876 subsurface->position.x,
1877 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001878 subsurface->surface->width,
1879 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001880 }
1881
1882 box = pixman_region32_extents(&region);
1883 if (x)
1884 *x = box->x1;
1885 if (y)
1886 *y = box->y1;
1887 if (w)
1888 *w = box->x2 - box->x1;
1889 if (h)
1890 *h = box->y2 - box->y1;
1891
1892 pixman_region32_fini(&region);
1893}
1894
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001895static int
1896surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001897 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001898{
1899 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001900 const unsigned resize_topbottom =
1901 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1902 const unsigned resize_leftright =
1903 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1904 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001905
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001906 if (shsurf->grabbed ||
1907 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001908 return 0;
1909
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001910 /* Check for invalid edge combinations. */
1911 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1912 (edges & resize_topbottom) == resize_topbottom ||
1913 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001914 return 0;
1915
1916 resize = malloc(sizeof *resize);
1917 if (!resize)
1918 return -1;
1919
1920 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001921
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001922 resize->width = shsurf->geometry.width;
1923 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001924
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001925 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001926 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001927 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001928 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001929
1930 return 0;
1931}
1932
1933static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001934common_surface_resize(struct wl_resource *resource,
1935 struct wl_resource *seat_resource, uint32_t serial,
1936 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001937{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001938 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001939 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001940 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001941
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001942 if (seat->pointer == NULL ||
1943 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001944 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001945 seat->pointer->focus == NULL)
1946 return;
1947
1948 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1949 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001950 return;
1951
Kristian Høgsberge3148752013-05-06 23:19:49 -04001952 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001953 wl_resource_post_no_memory(resource);
1954}
1955
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001956static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001957shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1958 struct wl_resource *seat_resource, uint32_t serial,
1959 uint32_t edges)
1960{
1961 common_surface_resize(resource, seat_resource, serial, edges);
1962}
1963
1964static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001965busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001966{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001967 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001968 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001969 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001970 wl_fixed_t sx, sy;
1971
Jason Ekstranda7af7042013-10-12 22:38:11 -05001972 view = weston_compositor_pick_view(pointer->seat->compositor,
1973 pointer->x, pointer->y,
1974 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001975
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001976 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1977 shell_grab_end(grab);
1978 free(grab);
1979 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001980}
1981
1982static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001983busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1984 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001985{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001986 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001987}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001988
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001989static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001990busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001991 uint32_t time, uint32_t button, uint32_t state)
1992{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001993 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001994 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001995 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001996
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001997 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001998 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001999 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002000 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002001 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002002 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002003 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002004}
2005
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002006static void
2007busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2008{
2009 struct shell_grab *grab = (struct shell_grab *) base;
2010
2011 shell_grab_end(grab);
2012 free(grab);
2013}
2014
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002015static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002016 busy_cursor_grab_focus,
2017 busy_cursor_grab_motion,
2018 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002019 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002020};
2021
2022static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002023set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002024{
2025 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002026
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002027 if (pointer->grab->interface == &busy_cursor_grab_interface)
2028 return;
2029
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002030 grab = malloc(sizeof *grab);
2031 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002032 return;
2033
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002034 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2035 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002036 /* Mark the shsurf as ungrabbed so that button binding is able
2037 * to move it. */
2038 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002039}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002040
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002041static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002042end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002043{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002044 struct shell_grab *grab;
2045 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002046
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002047 wl_list_for_each(seat, &compositor->seat_list, link) {
2048 if (seat->pointer == NULL)
2049 continue;
2050
2051 grab = (struct shell_grab *) seat->pointer->grab;
2052 if (grab->grab.interface == &busy_cursor_grab_interface &&
2053 wl_resource_get_client(grab->shsurf->resource) == client) {
2054 shell_grab_end(grab);
2055 free(grab);
2056 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002057 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002058}
2059
Scott Moreau9521d5e2012-04-19 13:06:17 -06002060static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002061handle_shell_client_destroy(struct wl_listener *listener, void *data);
2062
2063static int
2064xdg_ping_timeout_handler(void *data)
2065{
2066 struct shell_client *sc = data;
2067 struct weston_seat *seat;
2068 struct shell_surface *shsurf;
2069
2070 /* Client is not responding */
2071 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002072 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2073 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2074 continue;
2075 if (seat->pointer->focus->surface->resource == NULL)
2076 continue;
2077
2078 shsurf = get_shell_surface(seat->pointer->focus->surface);
2079 if (shsurf &&
2080 wl_resource_get_client(shsurf->resource) == sc->client)
2081 set_busy_cursor(shsurf, seat->pointer);
2082 }
2083
2084 return 1;
2085}
2086
2087static void
2088handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2089{
2090 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002091 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002092 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002093 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002094
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002095 if (sc->unresponsive) {
2096 xdg_ping_timeout_handler(sc);
2097 return;
2098 }
2099
2100 sc->ping_serial = serial;
2101 loop = wl_display_get_event_loop(compositor->wl_display);
2102 if (sc->ping_timer == NULL)
2103 sc->ping_timer =
2104 wl_event_loop_add_timer(loop,
2105 xdg_ping_timeout_handler, sc);
2106 if (sc->ping_timer == NULL)
2107 return;
2108
2109 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2110
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002111 if (shell_surface_is_xdg_surface(shsurf) ||
2112 shell_surface_is_xdg_popup(shsurf))
2113 xdg_shell_send_ping(sc->resource, serial);
2114 else if (shell_surface_is_wl_shell_surface(shsurf))
2115 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002116}
2117
Scott Moreauff1db4a2012-04-17 19:06:18 -06002118static void
2119ping_handler(struct weston_surface *surface, uint32_t serial)
2120{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002121 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002122
2123 if (!shsurf)
2124 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002125 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002126 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002127 if (shsurf->surface == shsurf->shell->grab_surface)
2128 return;
2129
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002130 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002131}
2132
2133static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002134handle_pointer_focus(struct wl_listener *listener, void *data)
2135{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002136 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002137 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002138 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002139 uint32_t serial;
2140
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002142 return;
2143
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002145 serial = wl_display_next_serial(compositor->wl_display);
2146 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002147}
2148
2149static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002150shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2151{
2152 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002153 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002154}
2155
2156static void
2157shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2158{
2159 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002160 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002161}
2162
2163static void
2164handle_keyboard_focus(struct wl_listener *listener, void *data)
2165{
2166 struct weston_keyboard *keyboard = data;
2167 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2168
2169 if (seat->focused_surface) {
2170 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2171 if (shsurf)
2172 shell_surface_lose_keyboard_focus(shsurf);
2173 }
2174
2175 seat->focused_surface = keyboard->focus;
2176
2177 if (seat->focused_surface) {
2178 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2179 if (shsurf)
2180 shell_surface_gain_keyboard_focus(shsurf);
2181 }
2182}
2183
2184static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002185shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002186{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002187 if (sc->ping_serial != serial)
2188 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002189
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002190 sc->unresponsive = 0;
2191 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002192
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002193 if (sc->ping_timer) {
2194 wl_event_source_remove(sc->ping_timer);
2195 sc->ping_timer = NULL;
2196 }
2197
2198}
2199
2200static void
2201shell_surface_pong(struct wl_client *client,
2202 struct wl_resource *resource, uint32_t serial)
2203{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002204 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2205 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002206
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002207 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002208}
2209
2210static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002211set_title(struct shell_surface *shsurf, const char *title)
2212{
2213 free(shsurf->title);
2214 shsurf->title = strdup(title);
2215}
2216
2217static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002218set_window_geometry(struct shell_surface *shsurf,
2219 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002220{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002221 shsurf->next_geometry.x = x;
2222 shsurf->next_geometry.y = y;
2223 shsurf->next_geometry.width = width;
2224 shsurf->next_geometry.height = height;
2225 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002226}
2227
2228static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002229shell_surface_set_title(struct wl_client *client,
2230 struct wl_resource *resource, const char *title)
2231{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002232 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002233
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002234 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002235}
2236
2237static void
2238shell_surface_set_class(struct wl_client *client,
2239 struct wl_resource *resource, const char *class)
2240{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002241 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002242
2243 free(shsurf->class);
2244 shsurf->class = strdup(class);
2245}
2246
Alex Wu4539b082012-03-01 12:57:46 +08002247static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002248restore_output_mode(struct weston_output *output)
2249{
Derek Foreman69c62732014-09-19 14:43:23 -05002250 if (output->original_mode ||
Hardening57388e42013-09-18 23:56:36 +02002251 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002252 weston_output_switch_mode(output,
Derek Foreman69c62732014-09-19 14:43:23 -05002253 output->native_mode,
2254 output->native_scale,
Hardening57388e42013-09-18 23:56:36 +02002255 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002256}
2257
2258static void
2259restore_all_output_modes(struct weston_compositor *compositor)
2260{
2261 struct weston_output *output;
2262
2263 wl_list_for_each(output, &compositor->output_list, link)
2264 restore_output_mode(output);
2265}
2266
Philip Withnall07926d92013-11-25 18:01:40 +00002267/* The surface will be inserted into the list immediately after the link
2268 * returned by this function (i.e. will be stacked immediately above the
2269 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002270static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002271shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2272{
2273 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002274 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002275
2276 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002277 case SHELL_SURFACE_XWAYLAND:
2278 return &shsurf->shell->fullscreen_layer.view_list;
2279
2280 case SHELL_SURFACE_NONE:
2281 return NULL;
2282
Kristian Høgsbergead52422014-01-01 13:51:52 -08002283 case SHELL_SURFACE_POPUP:
2284 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002285 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002286 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002287 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002288 /* Move the surface to its parent layer so
2289 * that surfaces which are transient for
2290 * fullscreen surfaces don't get hidden by the
2291 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002292
2293 /* TODO: Handle a parent with multiple views */
2294 parent = get_default_view(shsurf->parent);
2295 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002296 return container_of(parent->layer_link.link.prev,
2297 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002298 }
Philip Withnall07926d92013-11-25 18:01:40 +00002299
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002300 /* Move the surface to a normal workspace layer so that surfaces
2301 * which were previously fullscreen or transient are no longer
2302 * rendered on top. */
2303 ws = get_current_workspace(shsurf->shell);
2304 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002305 }
2306
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002307 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002308}
2309
Philip Withnall648a4dd2013-11-25 18:01:44 +00002310static void
2311shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2312{
2313 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002314 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002315
2316 /* Move the child layers to the same workspace as shsurf. They will be
2317 * stacked above shsurf. */
2318 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002319 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002320 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002321 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002322 prev = container_of(shsurf->view->layer_link.link.prev,
2323 struct weston_layer_entry, link);
2324 weston_layer_entry_remove(&child->view->layer_link);
2325 weston_layer_entry_insert(prev,
2326 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002327 weston_view_geometry_dirty(child->view);
2328 weston_surface_damage(child->surface);
2329
2330 /* Recurse. We don’t expect this to recurse very far (if
2331 * at all) because that would imply we have transient
2332 * (or popup) children of transient surfaces, which
2333 * would be unusual. */
2334 shell_surface_update_child_surface_layers(child);
2335 }
2336 }
2337}
2338
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002339/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002340 * geometry to ensure the changes are redrawn.
2341 *
2342 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2343 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002344static void
2345shell_surface_update_layer(struct shell_surface *shsurf)
2346{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002347 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002348
2349 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2350
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002351 if (new_layer_link == NULL)
2352 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002353 if (new_layer_link == &shsurf->view->layer_link)
2354 return;
2355
2356 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002357 weston_layer_entry_remove(&shsurf->view->layer_link);
2358 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002359 weston_view_geometry_dirty(shsurf->view);
2360 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002361
2362 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002363}
2364
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002365static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002366shell_surface_set_parent(struct shell_surface *shsurf,
2367 struct weston_surface *parent)
2368{
2369 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002370
2371 wl_list_remove(&shsurf->children_link);
2372 wl_list_init(&shsurf->children_link);
2373
2374 /* Insert into the parent surface’s child list. */
2375 if (parent != NULL) {
2376 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2377 if (parent_shsurf != NULL)
2378 wl_list_insert(&parent_shsurf->children_list,
2379 &shsurf->children_link);
2380 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002381}
2382
2383static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002384shell_surface_set_output(struct shell_surface *shsurf,
2385 struct weston_output *output)
2386{
2387 struct weston_surface *es = shsurf->surface;
2388
2389 /* get the default output, if the client set it as NULL
2390 check whether the ouput is available */
2391 if (output)
2392 shsurf->output = output;
2393 else if (es->output)
2394 shsurf->output = es->output;
2395 else
2396 shsurf->output = get_default_output(es->compositor);
2397}
2398
2399static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002400surface_clear_next_states(struct shell_surface *shsurf)
2401{
2402 shsurf->next_state.maximized = false;
2403 shsurf->next_state.fullscreen = false;
2404
2405 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2406 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2407 shsurf->state_changed = true;
2408}
2409
2410static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002411set_toplevel(struct shell_surface *shsurf)
2412{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002413 shell_surface_set_parent(shsurf, NULL);
2414 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002415 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002416
2417 /* The layer_link is updated in set_surface_type(),
2418 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002419}
2420
2421static void
2422shell_surface_set_toplevel(struct wl_client *client,
2423 struct wl_resource *resource)
2424{
2425 struct shell_surface *surface = wl_resource_get_user_data(resource);
2426
2427 set_toplevel(surface);
2428}
2429
2430static void
2431set_transient(struct shell_surface *shsurf,
2432 struct weston_surface *parent, int x, int y, uint32_t flags)
2433{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002434 assert(parent != NULL);
2435
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002436 shell_surface_set_parent(shsurf, parent);
2437
2438 surface_clear_next_states(shsurf);
2439
Philip Withnallbecb77e2013-11-25 18:01:30 +00002440 shsurf->transient.x = x;
2441 shsurf->transient.y = y;
2442 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002443
Rafael Antognollied207b42013-12-03 15:35:43 -02002444 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002445 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002446 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002447
2448 /* The layer_link is updated in set_surface_type(),
2449 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002450}
2451
2452static void
2453shell_surface_set_transient(struct wl_client *client,
2454 struct wl_resource *resource,
2455 struct wl_resource *parent_resource,
2456 int x, int y, uint32_t flags)
2457{
2458 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2459 struct weston_surface *parent =
2460 wl_resource_get_user_data(parent_resource);
2461
2462 set_transient(shsurf, parent, x, y, flags);
2463}
2464
2465static void
2466set_fullscreen(struct shell_surface *shsurf,
2467 uint32_t method,
2468 uint32_t framerate,
2469 struct weston_output *output)
2470{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002471 shell_surface_set_output(shsurf, output);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002472 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002473
2474 shsurf->fullscreen_output = shsurf->output;
2475 shsurf->fullscreen.type = method;
2476 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002477
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002478 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002479}
2480
2481static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002482weston_view_set_initial_position(struct weston_view *view,
2483 struct desktop_shell *shell);
2484
2485static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002486unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002487{
Philip Withnallf85fe842013-11-25 18:01:37 +00002488 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002489 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2490 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002491 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002492 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002493
Alex Wu4539b082012-03-01 12:57:46 +08002494 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2495 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002496
Alex Wu4539b082012-03-01 12:57:46 +08002497 wl_list_remove(&shsurf->fullscreen.transform.link);
2498 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002499
Jason Ekstranda7af7042013-10-12 22:38:11 -05002500 if (shsurf->fullscreen.black_view)
2501 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2502 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002503
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002504 if (shsurf->saved_position_valid)
2505 weston_view_set_position(shsurf->view,
2506 shsurf->saved_x, shsurf->saved_y);
2507 else
2508 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2509
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002510 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002511 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002512 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002513 shsurf->saved_rotation_valid = false;
2514 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002515
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002516 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002517}
2518
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002519static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002520shell_surface_set_fullscreen(struct wl_client *client,
2521 struct wl_resource *resource,
2522 uint32_t method,
2523 uint32_t framerate,
2524 struct wl_resource *output_resource)
2525{
2526 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2527 struct weston_output *output;
2528
2529 if (output_resource)
2530 output = wl_resource_get_user_data(output_resource);
2531 else
2532 output = NULL;
2533
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002534 shell_surface_set_parent(shsurf, NULL);
2535
Rafael Antognolli03b16592013-12-03 15:35:42 -02002536 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002537 shsurf->next_state.fullscreen = true;
2538 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002539 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002540}
2541
2542static void
2543set_popup(struct shell_surface *shsurf,
2544 struct weston_surface *parent,
2545 struct weston_seat *seat,
2546 uint32_t serial,
2547 int32_t x,
2548 int32_t y)
2549{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002550 assert(parent != NULL);
2551
Philip Withnallbecb77e2013-11-25 18:01:30 +00002552 shsurf->popup.shseat = get_shell_seat(seat);
2553 shsurf->popup.serial = serial;
2554 shsurf->popup.x = x;
2555 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002556
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002557 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002558}
2559
2560static void
2561shell_surface_set_popup(struct wl_client *client,
2562 struct wl_resource *resource,
2563 struct wl_resource *seat_resource,
2564 uint32_t serial,
2565 struct wl_resource *parent_resource,
2566 int32_t x, int32_t y, uint32_t flags)
2567{
2568 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002569 struct weston_surface *parent =
2570 wl_resource_get_user_data(parent_resource);
2571
2572 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002573
Rafael Antognolli03b16592013-12-03 15:35:42 -02002574 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002575 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002576 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002577 wl_resource_get_user_data(seat_resource),
2578 serial, x, y);
2579}
2580
2581static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002582unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002583{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002584 /* undo all maximized things here */
2585 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002586
2587 if (shsurf->saved_position_valid)
2588 weston_view_set_position(shsurf->view,
2589 shsurf->saved_x, shsurf->saved_y);
2590 else
2591 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002592
2593 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002594 wl_list_insert(&shsurf->view->geometry.transformation_list,
2595 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002596 shsurf->saved_rotation_valid = false;
2597 }
2598
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002599 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002600}
2601
Philip Withnallbecb77e2013-11-25 18:01:30 +00002602static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002603set_minimized(struct weston_surface *surface, uint32_t is_true)
2604{
2605 struct shell_surface *shsurf;
2606 struct workspace *current_ws;
2607 struct weston_seat *seat;
2608 struct weston_surface *focus;
2609 struct weston_view *view;
2610
2611 view = get_default_view(surface);
2612 if (!view)
2613 return;
2614
2615 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2616
2617 shsurf = get_shell_surface(surface);
2618 current_ws = get_current_workspace(shsurf->shell);
2619
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002620 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002621 /* hide or show, depending on the state */
2622 if (is_true) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002623 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002624
2625 drop_focus_state(shsurf->shell, current_ws, view->surface);
2626 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2627 if (!seat->keyboard)
2628 continue;
2629 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2630 if (focus == view->surface)
2631 weston_keyboard_set_focus(seat->keyboard, NULL);
2632 }
2633 }
2634 else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002635 weston_layer_entry_insert(&current_ws->layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002636
2637 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2638 if (!seat->keyboard)
2639 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002640 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002641 }
2642 }
2643
2644 shell_surface_update_child_surface_layers(shsurf);
2645
2646 weston_view_damage_below(view);
2647}
2648
2649static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002650shell_surface_set_maximized(struct wl_client *client,
2651 struct wl_resource *resource,
2652 struct wl_resource *output_resource)
2653{
2654 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2655 struct weston_output *output;
2656
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002657 surface_clear_next_states(shsurf);
2658 shsurf->next_state.maximized = true;
2659 shsurf->state_changed = true;
2660
2661 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2662 shell_surface_set_parent(shsurf, NULL);
2663
Philip Withnallbecb77e2013-11-25 18:01:30 +00002664 if (output_resource)
2665 output = wl_resource_get_user_data(output_resource);
2666 else
2667 output = NULL;
2668
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002669 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002670
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002671 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002672}
2673
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002674/* This is only ever called from set_surface_type(), so there’s no need to
2675 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002676static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002677reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002678{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002679 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002680 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002681 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002682 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002683
Pekka Paalanen98262232011-12-01 10:42:22 +02002684 return 0;
2685}
2686
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002687static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002688set_full_output(struct shell_surface *shsurf)
2689{
2690 shsurf->saved_x = shsurf->view->geometry.x;
2691 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002692 shsurf->saved_width = shsurf->surface->width;
2693 shsurf->saved_height = shsurf->surface->height;
2694 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002695 shsurf->saved_position_valid = true;
2696
2697 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2698 wl_list_remove(&shsurf->rotation.transform.link);
2699 wl_list_init(&shsurf->rotation.transform.link);
2700 weston_view_geometry_dirty(shsurf->view);
2701 shsurf->saved_rotation_valid = true;
2702 }
2703}
2704
2705static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002706set_surface_type(struct shell_surface *shsurf)
2707{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002708 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002709 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002710
Philip Withnallbecb77e2013-11-25 18:01:30 +00002711 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002712
Rafael Antognolli03b16592013-12-03 15:35:42 -02002713 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002714 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002715
2716 switch (shsurf->type) {
2717 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002718 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002719 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002720 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002721 weston_view_set_position(shsurf->view,
2722 pev->geometry.x + shsurf->transient.x,
2723 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002724 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002725 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002726
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002727 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002728 weston_view_set_position(shsurf->view, shsurf->transient.x,
2729 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002730 break;
2731
Philip Withnall0f640e22013-11-25 18:01:31 +00002732 case SHELL_SURFACE_POPUP:
2733 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002734 default:
2735 break;
2736 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002737
2738 /* Update the surface’s layer. */
2739 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002740}
2741
Tiago Vignattibe143262012-04-16 17:31:41 +03002742static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002743shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002744{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002745 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002746}
2747
Alex Wu21858432012-04-01 20:13:08 +08002748static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002749black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002750
Jason Ekstranda7af7042013-10-12 22:38:11 -05002751static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002752create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002753 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002754 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002755{
2756 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002757 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002758
2759 surface = weston_surface_create(ec);
2760 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002761 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002762 return NULL;
2763 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002764 view = weston_view_create(surface);
2765 if (surface == NULL) {
2766 weston_log("no memory\n");
2767 weston_surface_destroy(surface);
2768 return NULL;
2769 }
Alex Wu4539b082012-03-01 12:57:46 +08002770
Alex Wu21858432012-04-01 20:13:08 +08002771 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002772 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002773 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002774 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002775 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002776 pixman_region32_fini(&surface->input);
2777 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002778
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002779 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002780 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002781
2782 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002783}
2784
Philip Withnalled8f1a92013-11-25 18:01:43 +00002785static void
2786shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2787{
2788 struct weston_output *output = shsurf->fullscreen_output;
2789
Rafael Antognolli03b16592013-12-03 15:35:42 -02002790 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002791
2792 if (!shsurf->fullscreen.black_view)
2793 shsurf->fullscreen.black_view =
2794 create_black_surface(shsurf->surface->compositor,
2795 shsurf->surface,
2796 output->x, output->y,
2797 output->width,
2798 output->height);
2799
2800 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002801 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2802 weston_layer_entry_insert(&shsurf->view->layer_link,
2803 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002804 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2805 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002806
2807 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002808}
2809
Alex Wu4539b082012-03-01 12:57:46 +08002810/* Create black surface and append it to the associated fullscreen surface.
2811 * Handle size dismatch and positioning according to the method. */
2812static void
2813shell_configure_fullscreen(struct shell_surface *shsurf)
2814{
2815 struct weston_output *output = shsurf->fullscreen_output;
2816 struct weston_surface *surface = shsurf->surface;
2817 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002818 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002819 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002820
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002821 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2822 restore_output_mode(output);
2823
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002824 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002825 weston_layer_entry_remove(&shsurf->view->layer_link);
2826 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002827
Philip Withnalled8f1a92013-11-25 18:01:43 +00002828 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002829
Jason Ekstranda7af7042013-10-12 22:38:11 -05002830 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002831 &surf_width, &surf_height);
2832
Alex Wu4539b082012-03-01 12:57:46 +08002833 switch (shsurf->fullscreen.type) {
2834 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002835 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002836 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002837 break;
2838 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002839 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002840 if (output->width == surf_width &&
2841 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002842 weston_view_set_position(shsurf->view,
2843 output->x - surf_x,
2844 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002845 break;
2846 }
2847
Alex Wu4539b082012-03-01 12:57:46 +08002848 matrix = &shsurf->fullscreen.transform.matrix;
2849 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002850
Scott Moreau1bad5db2012-08-18 01:04:05 -06002851 output_aspect = (float) output->width /
2852 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002853 /* XXX: Use surf_width and surf_height here? */
2854 surface_aspect = (float) surface->width /
2855 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002856 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002857 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002858 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002859 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002860 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002861 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002862
Alex Wu4539b082012-03-01 12:57:46 +08002863 weston_matrix_scale(matrix, scale, scale, 1);
2864 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002865 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002866 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002867 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2868 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002869 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002870
Alex Wu4539b082012-03-01 12:57:46 +08002871 break;
2872 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002873 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002874 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002875 surf_width * surface->buffer_viewport.buffer.scale,
2876 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002877 shsurf->fullscreen.framerate};
2878
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002879 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002880 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002881 weston_view_set_position(shsurf->view,
2882 output->x - surf_x,
2883 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002884 shsurf->fullscreen.black_view->surface->width = output->width;
2885 shsurf->fullscreen.black_view->surface->height = output->height;
2886 weston_view_set_position(shsurf->fullscreen.black_view,
2887 output->x - surf_x,
2888 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002889 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002890 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002891 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002892 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002893 }
Alex Wubd3354b2012-04-17 17:20:49 +08002894 }
Alex Wu4539b082012-03-01 12:57:46 +08002895 break;
2896 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002897 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002898 break;
2899 default:
2900 break;
2901 }
2902}
2903
Alex Wu4539b082012-03-01 12:57:46 +08002904static void
2905shell_map_fullscreen(struct shell_surface *shsurf)
2906{
Alex Wubd3354b2012-04-17 17:20:49 +08002907 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002908}
2909
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002910static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002911set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2912{
2913 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002914 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002915 shsurf->transient.x = x;
2916 shsurf->transient.y = y;
2917 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002918
2919 shell_surface_set_parent(shsurf, NULL);
2920
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002921 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002922 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002923}
2924
Kristian Høgsberg47792412014-05-04 13:47:06 -07002925static void
2926shell_interface_set_fullscreen(struct shell_surface *shsurf,
2927 uint32_t method,
2928 uint32_t framerate,
2929 struct weston_output *output)
2930{
2931 surface_clear_next_states(shsurf);
2932 set_fullscreen(shsurf, method, framerate, output);
2933
2934 shsurf->next_state.fullscreen = true;
2935 shsurf->state_changed = true;
2936}
2937
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002938static int
2939shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2940{
2941 return surface_move(shsurf, ws, 1);
2942}
2943
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002944static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002945
2946static void
2947destroy_shell_seat(struct wl_listener *listener, void *data)
2948{
2949 struct shell_seat *shseat =
2950 container_of(listener,
2951 struct shell_seat, seat_destroy_listener);
2952 struct shell_surface *shsurf, *prev = NULL;
2953
2954 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002955 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002956 shseat->popup_grab.client = NULL;
2957
2958 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2959 shsurf->popup.shseat = NULL;
2960 if (prev) {
2961 wl_list_init(&prev->popup.grab_link);
2962 }
2963 prev = shsurf;
2964 }
2965 wl_list_init(&prev->popup.grab_link);
2966 }
2967
2968 wl_list_remove(&shseat->seat_destroy_listener.link);
2969 free(shseat);
2970}
2971
Jason Ekstrand024177c2014-04-21 19:42:58 -05002972static void
2973shell_seat_caps_changed(struct wl_listener *listener, void *data)
2974{
2975 struct shell_seat *seat;
2976
2977 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2978
2979 if (seat->seat->keyboard &&
2980 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2981 wl_signal_add(&seat->seat->keyboard->focus_signal,
2982 &seat->keyboard_focus_listener);
2983 } else if (!seat->seat->keyboard) {
2984 wl_list_init(&seat->keyboard_focus_listener.link);
2985 }
2986
2987 if (seat->seat->pointer &&
2988 wl_list_empty(&seat->pointer_focus_listener.link)) {
2989 wl_signal_add(&seat->seat->pointer->focus_signal,
2990 &seat->pointer_focus_listener);
2991 } else if (!seat->seat->pointer) {
2992 wl_list_init(&seat->pointer_focus_listener.link);
2993 }
2994}
2995
Giulio Camuffo5085a752013-03-25 21:42:45 +01002996static struct shell_seat *
2997create_shell_seat(struct weston_seat *seat)
2998{
2999 struct shell_seat *shseat;
3000
3001 shseat = calloc(1, sizeof *shseat);
3002 if (!shseat) {
3003 weston_log("no memory to allocate shell seat\n");
3004 return NULL;
3005 }
3006
3007 shseat->seat = seat;
3008 wl_list_init(&shseat->popup_grab.surfaces_list);
3009
3010 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3011 wl_signal_add(&seat->destroy_signal,
3012 &shseat->seat_destroy_listener);
3013
Jason Ekstrand024177c2014-04-21 19:42:58 -05003014 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3015 wl_list_init(&shseat->keyboard_focus_listener.link);
3016
3017 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3018 wl_list_init(&shseat->pointer_focus_listener.link);
3019
3020 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3021 wl_signal_add(&seat->updated_caps_signal,
3022 &shseat->caps_changed_listener);
3023 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3024
Giulio Camuffo5085a752013-03-25 21:42:45 +01003025 return shseat;
3026}
3027
3028static struct shell_seat *
3029get_shell_seat(struct weston_seat *seat)
3030{
3031 struct wl_listener *listener;
3032
3033 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003034 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003035
3036 return container_of(listener,
3037 struct shell_seat, seat_destroy_listener);
3038}
3039
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003040static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003041popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003042{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003043 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003044 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003045 struct shell_seat *shseat =
3046 container_of(grab, struct shell_seat, popup_grab.grab);
3047 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003048 wl_fixed_t sx, sy;
3049
Jason Ekstranda7af7042013-10-12 22:38:11 -05003050 view = weston_compositor_pick_view(pointer->seat->compositor,
3051 pointer->x, pointer->y,
3052 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003053
Jason Ekstranda7af7042013-10-12 22:38:11 -05003054 if (view && view->surface->resource &&
3055 wl_resource_get_client(view->surface->resource) == client) {
3056 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003057 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003058 weston_pointer_set_focus(pointer, NULL,
3059 wl_fixed_from_int(0),
3060 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003061 }
3062}
3063
3064static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003065popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3066 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003067{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003068 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003069 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003070 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003071
Jonas Ådahl61917c82014-08-11 22:44:02 +02003072 if (pointer->focus) {
3073 weston_view_from_global_fixed(pointer->focus, x, y,
3074 &pointer->sx, &pointer->sy);
3075 }
3076
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003077 weston_pointer_move(pointer, x, y);
3078
Neil Roberts96d790e2013-09-19 17:32:00 +01003079 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003080 weston_view_from_global_fixed(pointer->focus,
3081 pointer->x, pointer->y,
3082 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003083 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003084 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003085}
3086
3087static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003088popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003089 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003090{
3091 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003092 struct shell_seat *shseat =
3093 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003094 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003095 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003096 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003097 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003098
Neil Roberts96d790e2013-09-19 17:32:00 +01003099 resource_list = &grab->pointer->focus_resource_list;
3100 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003101 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003102 wl_resource_for_each(resource, resource_list) {
3103 wl_pointer_send_button(resource, serial,
3104 time, button, state);
3105 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003106 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003107 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003108 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003109 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003110 }
3111
Daniel Stone4dbadb12012-05-30 16:31:51 +01003112 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003113 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003114}
3115
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003116static void
3117popup_grab_cancel(struct weston_pointer_grab *grab)
3118{
3119 popup_grab_end(grab->pointer);
3120}
3121
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003122static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003123 popup_grab_focus,
3124 popup_grab_motion,
3125 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003126 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003127};
3128
3129static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003130touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3131 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3132{
3133 struct wl_resource *resource;
3134 struct shell_seat *shseat =
3135 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3136 struct wl_display *display = shseat->seat->compositor->wl_display;
3137 uint32_t serial;
3138 struct wl_list *resource_list;
3139
3140 resource_list = &grab->touch->focus_resource_list;
3141 if (!wl_list_empty(resource_list)) {
3142 serial = wl_display_get_serial(display);
3143 wl_resource_for_each(resource, resource_list) {
3144 wl_touch_send_down(resource, serial, time,
3145 grab->touch->focus->surface->resource,
3146 touch_id, sx, sy);
3147 }
3148 }
3149}
3150
3151static void
3152touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3153{
3154 struct wl_resource *resource;
3155 struct shell_seat *shseat =
3156 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3157 struct wl_display *display = shseat->seat->compositor->wl_display;
3158 uint32_t serial;
3159 struct wl_list *resource_list;
3160
3161 resource_list = &grab->touch->focus_resource_list;
3162 if (!wl_list_empty(resource_list)) {
3163 serial = wl_display_get_serial(display);
3164 wl_resource_for_each(resource, resource_list) {
3165 wl_touch_send_up(resource, serial, time, touch_id);
3166 }
3167 }
3168}
3169
3170static void
3171touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3172 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3173{
3174 struct wl_resource *resource;
3175 struct wl_list *resource_list;
3176
3177 resource_list = &grab->touch->focus_resource_list;
3178 if (!wl_list_empty(resource_list)) {
3179 wl_resource_for_each(resource, resource_list) {
3180 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3181 }
3182 }
3183}
3184
3185static void
3186touch_popup_grab_frame(struct weston_touch_grab *grab)
3187{
3188}
3189
3190static void
3191touch_popup_grab_cancel(struct weston_touch_grab *grab)
3192{
3193 touch_popup_grab_end(grab->touch);
3194}
3195
3196static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3197 touch_popup_grab_down,
3198 touch_popup_grab_up,
3199 touch_popup_grab_motion,
3200 touch_popup_grab_frame,
3201 touch_popup_grab_cancel,
3202};
3203
3204static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003205shell_surface_send_popup_done(struct shell_surface *shsurf)
3206{
3207 if (shell_surface_is_wl_shell_surface(shsurf))
3208 wl_shell_surface_send_popup_done(shsurf->resource);
3209 else if (shell_surface_is_xdg_popup(shsurf))
3210 xdg_popup_send_popup_done(shsurf->resource,
3211 shsurf->popup.serial);
3212}
3213
3214static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003215popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003216{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003217 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003218 struct shell_seat *shseat =
3219 container_of(grab, struct shell_seat, popup_grab.grab);
3220 struct shell_surface *shsurf;
3221 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003222
3223 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003224 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003225 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003226 shseat->popup_grab.grab.interface = NULL;
3227 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003228 /* Send the popup_done event to all the popups open */
3229 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003230 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003231 shsurf->popup.shseat = NULL;
3232 if (prev) {
3233 wl_list_init(&prev->popup.grab_link);
3234 }
3235 prev = shsurf;
3236 }
3237 wl_list_init(&prev->popup.grab_link);
3238 wl_list_init(&shseat->popup_grab.surfaces_list);
3239 }
3240}
3241
3242static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003243touch_popup_grab_end(struct weston_touch *touch)
3244{
3245 struct weston_touch_grab *grab = touch->grab;
3246 struct shell_seat *shseat =
3247 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3248 struct shell_surface *shsurf;
3249 struct shell_surface *prev = NULL;
3250
3251 if (touch->grab->interface == &touch_popup_grab_interface) {
3252 weston_touch_end_grab(grab->touch);
3253 shseat->popup_grab.client = NULL;
3254 shseat->popup_grab.touch_grab.interface = NULL;
3255 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3256 /* Send the popup_done event to all the popups open */
3257 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
3258 shell_surface_send_popup_done(shsurf);
3259 shsurf->popup.shseat = NULL;
3260 if (prev) {
3261 wl_list_init(&prev->popup.grab_link);
3262 }
3263 prev = shsurf;
3264 }
3265 wl_list_init(&prev->popup.grab_link);
3266 wl_list_init(&shseat->popup_grab.surfaces_list);
3267 }
3268}
3269
3270static void
3271add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat, int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003272{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003273 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003274
3275 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003276 shseat->popup_grab.type = type;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003277 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003278
3279 if (type == POINTER) {
3280 shseat->popup_grab.grab.interface = &popup_grab_interface;
3281 /* We must make sure here that this popup was opened after
3282 * a mouse press, and not just by moving around with other
3283 * popups already open. */
3284 if (shseat->seat->pointer->button_count > 0)
3285 shseat->popup_grab.initial_up = 0;
3286 } else if (type == TOUCH) {
3287 shseat->popup_grab.touch_grab.interface = &touch_popup_grab_interface;
3288 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003289
Rob Bradforddfe31052013-06-26 19:49:11 +01003290 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003291
3292 if (type == POINTER)
3293 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
3294 else if (type == TOUCH)
3295 weston_touch_start_grab(seat->touch, &shseat->popup_grab.touch_grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003296 } else {
3297 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003298 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003299}
3300
3301static void
3302remove_popup_grab(struct shell_surface *shsurf)
3303{
3304 struct shell_seat *shseat = shsurf->popup.shseat;
3305
3306 wl_list_remove(&shsurf->popup.grab_link);
3307 wl_list_init(&shsurf->popup.grab_link);
3308 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003309 if (shseat->popup_grab.type == POINTER) {
3310 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3311 shseat->popup_grab.grab.interface = NULL;
3312 } else if (shseat->popup_grab.type == TOUCH) {
3313 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3314 shseat->popup_grab.touch_grab.interface = NULL;
3315 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003316 }
3317}
3318
3319static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003320shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003321{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003322 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003323 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003324
Jason Ekstranda7af7042013-10-12 22:38:11 -05003325 shsurf->surface->output = parent_view->output;
3326 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003327
Jason Ekstranda7af7042013-10-12 22:38:11 -05003328 weston_view_set_transform_parent(shsurf->view, parent_view);
3329 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3330 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003331
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003332 if (shseat->seat->pointer &&
3333 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003334 add_popup_grab(shsurf, shseat, POINTER);
3335 } else if (shseat->seat->touch &&
3336 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
3337 add_popup_grab(shsurf, shseat, TOUCH);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003338 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003339 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003340 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003341 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003342}
3343
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003344static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003345 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003346 shell_surface_move,
3347 shell_surface_resize,
3348 shell_surface_set_toplevel,
3349 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003350 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003351 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003352 shell_surface_set_maximized,
3353 shell_surface_set_title,
3354 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003355};
3356
3357static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003358destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003359{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003360 struct shell_surface *child, *next;
3361
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003362 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3363
Giulio Camuffo5085a752013-03-25 21:42:45 +01003364 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3365 remove_popup_grab(shsurf);
3366 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003367
Alex Wubd3354b2012-04-17 17:20:49 +08003368 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003369 shell_surface_is_top_fullscreen(shsurf))
3370 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003371
Jason Ekstranda7af7042013-10-12 22:38:11 -05003372 if (shsurf->fullscreen.black_view)
3373 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003374
Alex Wubd3354b2012-04-17 17:20:49 +08003375 /* As destroy_resource() use wl_list_for_each_safe(),
3376 * we can always remove the listener.
3377 */
3378 wl_list_remove(&shsurf->surface_destroy_listener.link);
3379 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003380 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003381
Jason Ekstranda7af7042013-10-12 22:38:11 -05003382 weston_view_destroy(shsurf->view);
3383
Philip Withnall648a4dd2013-11-25 18:01:44 +00003384 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003385 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3386 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003387
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003388 wl_list_remove(&shsurf->link);
3389 free(shsurf);
3390}
3391
3392static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003393shell_destroy_shell_surface(struct wl_resource *resource)
3394{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003395 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003396
Bryan Caina46b9462014-04-04 17:41:24 -05003397 if (!wl_list_empty(&shsurf->popup.grab_link))
3398 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003399 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003400}
3401
3402static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003403shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003404{
3405 struct shell_surface *shsurf = container_of(listener,
3406 struct shell_surface,
3407 surface_destroy_listener);
3408
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003409 if (shsurf->resource)
3410 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003411
3412 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003413}
3414
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003415static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003416fade_out_done(struct weston_view_animation *animation, void *data)
3417{
3418 struct shell_surface *shsurf = data;
3419
3420 weston_surface_destroy(shsurf->surface);
3421}
3422
3423static void
3424handle_resource_destroy(struct wl_listener *listener, void *data)
3425{
3426 struct shell_surface *shsurf =
3427 container_of(listener, struct shell_surface,
3428 resource_destroy_listener);
3429
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003430 if (!weston_surface_is_mapped(shsurf->surface))
3431 return;
3432
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003433 shsurf->surface->ref_count++;
3434
3435 pixman_region32_fini(&shsurf->surface->pending.input);
3436 pixman_region32_init(&shsurf->surface->pending.input);
3437 pixman_region32_fini(&shsurf->surface->input);
3438 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003439 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3440 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3441 fade_out_done, shsurf);
3442 } else {
3443 weston_surface_destroy(shsurf->surface);
3444 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003445}
3446
3447static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003448shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003449
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003450struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003451get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003452{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003453 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003454 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003455 else
3456 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003457}
3458
Rafael Antognollie2a34552013-12-03 15:35:45 -02003459static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003460create_common_surface(struct shell_client *owner, void *shell,
3461 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003462 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003463{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003464 struct shell_surface *shsurf;
3465
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003466 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003467 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003468 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003469 }
3470
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003471 shsurf = calloc(1, sizeof *shsurf);
3472 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003473 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003474 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003475 }
3476
Jason Ekstranda7af7042013-10-12 22:38:11 -05003477 shsurf->view = weston_view_create(surface);
3478 if (!shsurf->view) {
3479 weston_log("no memory to allocate shell surface\n");
3480 free(shsurf);
3481 return NULL;
3482 }
3483
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003484 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003485 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003486
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003487 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3488 wl_resource_add_destroy_listener(surface->resource,
3489 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003490 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003491
Tiago Vignattibc052c92012-04-19 16:18:18 +03003492 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003493 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003494 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003495 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003496 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003497 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003498 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3499 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003500 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003501 wl_list_init(&shsurf->fullscreen.transform.link);
3502
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003503 shsurf->output = get_default_output(shsurf->shell->compositor);
3504
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003505 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003506 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003507 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003508 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003509
3510 /* init link so its safe to always remove it in destroy_shell_surface */
3511 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003512 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003513
Pekka Paalanen460099f2012-01-20 16:48:25 +02003514 /* empty when not in use */
3515 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003516 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003517
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003518 wl_list_init(&shsurf->workspace_transform.link);
3519
Philip Withnall648a4dd2013-11-25 18:01:44 +00003520 wl_list_init(&shsurf->children_link);
3521 wl_list_init(&shsurf->children_list);
3522 shsurf->parent = NULL;
3523
Pekka Paalanen98262232011-12-01 10:42:22 +02003524 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003525
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003526 shsurf->client = client;
3527
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003528 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003529}
3530
Rafael Antognollie2a34552013-12-03 15:35:45 -02003531static struct shell_surface *
3532create_shell_surface(void *shell, struct weston_surface *surface,
3533 const struct weston_shell_client *client)
3534{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003535 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003536}
3537
Jason Ekstranda7af7042013-10-12 22:38:11 -05003538static struct weston_view *
3539get_primary_view(void *shell, struct shell_surface *shsurf)
3540{
3541 return shsurf->view;
3542}
3543
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003544static struct weston_output *
3545get_focused_output(struct weston_compositor *compositor)
3546{
3547 struct weston_seat *seat;
3548 struct weston_output *output = NULL;
3549
3550 wl_list_for_each(seat, &compositor->seat_list, link) {
3551 /* Priority has touch focus, then pointer and
3552 * then keyboard focus. We should probably have
3553 * three for loops and check frist for touch,
3554 * then for pointer, etc. but unless somebody has some
3555 * objections, I think this is sufficient. */
3556 if (seat->touch && seat->touch->focus)
3557 output = seat->touch->focus->output;
3558 else if (seat->pointer && seat->pointer->focus)
3559 output = seat->pointer->focus->output;
3560 else if (seat->keyboard && seat->keyboard->focus)
3561 output = seat->keyboard->focus->output;
3562
3563 if (output)
3564 break;
3565 }
3566
3567 return output;
3568}
3569
Tiago Vignattibc052c92012-04-19 16:18:18 +03003570static void
3571shell_get_shell_surface(struct wl_client *client,
3572 struct wl_resource *resource,
3573 uint32_t id,
3574 struct wl_resource *surface_resource)
3575{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003576 struct weston_surface *surface =
3577 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003578 struct shell_client *sc = wl_resource_get_user_data(resource);
3579 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003580 struct shell_surface *shsurf;
3581
3582 if (get_shell_surface(surface)) {
3583 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003584 WL_DISPLAY_ERROR_INVALID_OBJECT,
3585 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003586 return;
3587 }
3588
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003589 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003590 if (!shsurf) {
3591 wl_resource_post_error(surface_resource,
3592 WL_DISPLAY_ERROR_INVALID_OBJECT,
3593 "surface->configure already set");
3594 return;
3595 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003596
Jason Ekstranda85118c2013-06-27 20:17:02 -05003597 shsurf->resource =
3598 wl_resource_create(client,
3599 &wl_shell_surface_interface, 1, id);
3600 wl_resource_set_implementation(shsurf->resource,
3601 &shell_surface_implementation,
3602 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003603}
3604
Rafael Antognollie2a34552013-12-03 15:35:45 -02003605static bool
3606shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3607{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003608 /* A shell surface without a resource is created from xwayland
3609 * and is considered a wl_shell surface for now. */
3610
3611 return shsurf->resource == NULL ||
3612 wl_resource_instance_of(shsurf->resource,
3613 &wl_shell_surface_interface,
3614 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003615}
3616
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003617static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003618 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003619};
3620
Rafael Antognollie2a34552013-12-03 15:35:45 -02003621/****************************
3622 * xdg-shell implementation */
3623
3624static void
3625xdg_surface_destroy(struct wl_client *client,
3626 struct wl_resource *resource)
3627{
3628 wl_resource_destroy(resource);
3629}
3630
3631static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003632xdg_surface_set_parent(struct wl_client *client,
3633 struct wl_resource *resource,
3634 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003635{
3636 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3637 struct weston_surface *parent;
3638
3639 if (parent_resource)
3640 parent = wl_resource_get_user_data(parent_resource);
3641 else
3642 parent = NULL;
3643
3644 shell_surface_set_parent(shsurf, parent);
3645}
3646
3647static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003648xdg_surface_set_app_id(struct wl_client *client,
3649 struct wl_resource *resource,
3650 const char *app_id)
3651{
3652 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3653
3654 free(shsurf->class);
3655 shsurf->class = strdup(app_id);
3656}
3657
3658static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003659xdg_surface_show_window_menu(struct wl_client *client,
3660 struct wl_resource *surface_resource,
3661 struct wl_resource *seat_resource,
3662 uint32_t serial,
3663 int32_t x,
3664 int32_t y)
3665{
3666 /* TODO */
3667}
3668
3669static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003670xdg_surface_set_title(struct wl_client *client,
3671 struct wl_resource *resource, const char *title)
3672{
3673 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3674
3675 set_title(shsurf, title);
3676}
3677
3678static void
3679xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3680 struct wl_resource *seat_resource, uint32_t serial)
3681{
3682 common_surface_move(resource, seat_resource, serial);
3683}
3684
3685static void
3686xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3687 struct wl_resource *seat_resource, uint32_t serial,
3688 uint32_t edges)
3689{
3690 common_surface_resize(resource, seat_resource, serial, edges);
3691}
3692
3693static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003694xdg_surface_ack_configure(struct wl_client *client,
3695 struct wl_resource *resource,
3696 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003697{
3698 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3699
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003700 if (shsurf->state_requested) {
3701 shsurf->next_state = shsurf->requested_state;
3702 shsurf->state_changed = true;
3703 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003704 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003705}
3706
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003707static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003708xdg_surface_set_window_geometry(struct wl_client *client,
3709 struct wl_resource *resource,
3710 int32_t x,
3711 int32_t y,
3712 int32_t width,
3713 int32_t height)
3714{
3715 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3716
3717 set_window_geometry(shsurf, x, y, width, height);
3718}
3719
3720static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003721xdg_surface_set_maximized(struct wl_client *client,
3722 struct wl_resource *resource)
3723{
3724 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003725 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003726
3727 shsurf->state_requested = true;
3728 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003729
3730 if (!weston_surface_is_mapped(shsurf->surface))
3731 output = get_focused_output(shsurf->surface->compositor);
3732 else
3733 output = shsurf->surface->output;
3734
3735 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003736 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003737}
3738
3739static void
3740xdg_surface_unset_maximized(struct wl_client *client,
3741 struct wl_resource *resource)
3742{
3743 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3744
3745 shsurf->state_requested = true;
3746 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003747 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003748}
3749
3750static void
3751xdg_surface_set_fullscreen(struct wl_client *client,
3752 struct wl_resource *resource,
3753 struct wl_resource *output_resource)
3754{
3755 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3756 struct weston_output *output;
3757
3758 shsurf->state_requested = true;
3759 shsurf->requested_state.fullscreen = true;
3760
3761 if (output_resource)
3762 output = wl_resource_get_user_data(output_resource);
3763 else
3764 output = NULL;
3765
Marek Chalupa052917a2014-09-02 11:35:12 +02003766 /* handle clients launching in fullscreen */
3767 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3768 /* Set the output to the one that has focus currently. */
3769 assert(shsurf->surface);
3770 output = get_focused_output(shsurf->surface->compositor);
3771 }
3772
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003773 shell_surface_set_output(shsurf, output);
3774 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003775
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003776 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003777}
3778
3779static void
3780xdg_surface_unset_fullscreen(struct wl_client *client,
3781 struct wl_resource *resource)
3782{
3783 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3784
3785 shsurf->state_requested = true;
3786 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003787 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003788}
3789
3790static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003791xdg_surface_set_minimized(struct wl_client *client,
3792 struct wl_resource *resource)
3793{
3794 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3795
3796 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3797 return;
3798
3799 /* apply compositor's own minimization logic (hide) */
3800 set_minimized(shsurf->surface, 1);
3801}
3802
Rafael Antognollie2a34552013-12-03 15:35:45 -02003803static const struct xdg_surface_interface xdg_surface_implementation = {
3804 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003805 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003806 xdg_surface_set_title,
3807 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003808 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003809 xdg_surface_move,
3810 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003811 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003812 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003813 xdg_surface_set_maximized,
3814 xdg_surface_unset_maximized,
3815 xdg_surface_set_fullscreen,
3816 xdg_surface_unset_fullscreen,
3817 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003818};
3819
3820static void
3821xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003822 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003823{
3824 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003825 uint32_t *s;
3826 struct wl_array states;
3827 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003828
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003829 assert(shsurf);
3830
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003831 if (!shsurf->resource)
3832 return;
3833
3834 wl_array_init(&states);
3835 if (shsurf->requested_state.fullscreen) {
3836 s = wl_array_add(&states, sizeof *s);
3837 *s = XDG_SURFACE_STATE_FULLSCREEN;
3838 } else if (shsurf->requested_state.maximized) {
3839 s = wl_array_add(&states, sizeof *s);
3840 *s = XDG_SURFACE_STATE_MAXIMIZED;
3841 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003842 if (shsurf->resize_edges != 0) {
3843 s = wl_array_add(&states, sizeof *s);
3844 *s = XDG_SURFACE_STATE_RESIZING;
3845 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003846 if (shsurf->focus_count > 0) {
3847 s = wl_array_add(&states, sizeof *s);
3848 *s = XDG_SURFACE_STATE_ACTIVATED;
3849 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003850
3851 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3852 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3853
3854 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003855}
3856
3857static const struct weston_shell_client xdg_client = {
3858 xdg_send_configure
3859};
3860
3861static void
3862xdg_use_unstable_version(struct wl_client *client,
3863 struct wl_resource *resource,
3864 int32_t version)
3865{
3866 if (version > 1) {
3867 wl_resource_post_error(resource,
3868 1,
3869 "xdg-shell:: version not implemented yet.");
3870 return;
3871 }
3872}
3873
3874static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003875create_xdg_surface(struct shell_client *owner, void *shell,
3876 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003877 const struct weston_shell_client *client)
3878{
3879 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003880
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003881 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03003882 if (!shsurf)
3883 return NULL;
3884
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003885 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003886
3887 return shsurf;
3888}
3889
3890static void
3891xdg_get_xdg_surface(struct wl_client *client,
3892 struct wl_resource *resource,
3893 uint32_t id,
3894 struct wl_resource *surface_resource)
3895{
3896 struct weston_surface *surface =
3897 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003898 struct shell_client *sc = wl_resource_get_user_data(resource);
3899 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003900 struct shell_surface *shsurf;
3901
3902 if (get_shell_surface(surface)) {
3903 wl_resource_post_error(surface_resource,
3904 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003905 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003906 return;
3907 }
3908
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003909 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003910 if (!shsurf) {
3911 wl_resource_post_error(surface_resource,
3912 WL_DISPLAY_ERROR_INVALID_OBJECT,
3913 "surface->configure already set");
3914 return;
3915 }
3916
3917 shsurf->resource =
3918 wl_resource_create(client,
3919 &xdg_surface_interface, 1, id);
3920 wl_resource_set_implementation(shsurf->resource,
3921 &xdg_surface_implementation,
3922 shsurf, shell_destroy_shell_surface);
3923}
3924
3925static bool
3926shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3927{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003928 return shsurf->resource &&
3929 wl_resource_instance_of(shsurf->resource,
3930 &xdg_surface_interface,
3931 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003932}
3933
3934/* xdg-popup implementation */
3935
3936static void
3937xdg_popup_destroy(struct wl_client *client,
3938 struct wl_resource *resource)
3939{
3940 wl_resource_destroy(resource);
3941}
3942
Rafael Antognollie2a34552013-12-03 15:35:45 -02003943static const struct xdg_popup_interface xdg_popup_implementation = {
3944 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003945};
3946
3947static void
3948xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003949 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003950{
3951}
3952
3953static const struct weston_shell_client xdg_popup_client = {
3954 xdg_popup_send_configure
3955};
3956
3957static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003958create_xdg_popup(struct shell_client *owner, void *shell,
3959 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003960 const struct weston_shell_client *client,
3961 struct weston_surface *parent,
3962 struct shell_seat *seat,
3963 uint32_t serial,
3964 int32_t x, int32_t y)
3965{
3966 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003967
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003968 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03003969 if (!shsurf)
3970 return NULL;
3971
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003972 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003973 shsurf->popup.shseat = seat;
3974 shsurf->popup.serial = serial;
3975 shsurf->popup.x = x;
3976 shsurf->popup.y = y;
3977 shell_surface_set_parent(shsurf, parent);
3978
3979 return shsurf;
3980}
3981
3982static void
3983xdg_get_xdg_popup(struct wl_client *client,
3984 struct wl_resource *resource,
3985 uint32_t id,
3986 struct wl_resource *surface_resource,
3987 struct wl_resource *parent_resource,
3988 struct wl_resource *seat_resource,
3989 uint32_t serial,
3990 int32_t x, int32_t y, uint32_t flags)
3991{
3992 struct weston_surface *surface =
3993 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003994 struct shell_client *sc = wl_resource_get_user_data(resource);
3995 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003996 struct shell_surface *shsurf;
3997 struct weston_surface *parent;
3998 struct shell_seat *seat;
3999
4000 if (get_shell_surface(surface)) {
4001 wl_resource_post_error(surface_resource,
4002 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04004003 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02004004 return;
4005 }
4006
4007 if (!parent_resource) {
4008 wl_resource_post_error(surface_resource,
4009 WL_DISPLAY_ERROR_INVALID_OBJECT,
4010 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004011 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004012 }
4013
4014 parent = wl_resource_get_user_data(parent_resource);
4015 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4016
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004017 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004018 parent, seat, serial, x, y);
4019 if (!shsurf) {
4020 wl_resource_post_error(surface_resource,
4021 WL_DISPLAY_ERROR_INVALID_OBJECT,
4022 "surface->configure already set");
4023 return;
4024 }
4025
4026 shsurf->resource =
4027 wl_resource_create(client,
4028 &xdg_popup_interface, 1, id);
4029 wl_resource_set_implementation(shsurf->resource,
4030 &xdg_popup_implementation,
4031 shsurf, shell_destroy_shell_surface);
4032}
4033
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004034static void
4035xdg_pong(struct wl_client *client,
4036 struct wl_resource *resource, uint32_t serial)
4037{
4038 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004039
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004040 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004041}
4042
Rafael Antognollie2a34552013-12-03 15:35:45 -02004043static bool
4044shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4045{
4046 return wl_resource_instance_of(shsurf->resource,
4047 &xdg_popup_interface,
4048 &xdg_popup_implementation);
4049}
4050
4051static const struct xdg_shell_interface xdg_implementation = {
4052 xdg_use_unstable_version,
4053 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004054 xdg_get_xdg_popup,
4055 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004056};
4057
4058static int
4059xdg_shell_unversioned_dispatch(const void *implementation,
4060 void *_target, uint32_t opcode,
4061 const struct wl_message *message,
4062 union wl_argument *args)
4063{
4064 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004065 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004066
4067 if (opcode != 0) {
4068 wl_resource_post_error(resource,
4069 WL_DISPLAY_ERROR_INVALID_OBJECT,
4070 "must call use_unstable_version first");
4071 return 0;
4072 }
4073
Pekka Paalanen71182ae2014-08-21 17:47:20 +03004074#define XDG_SERVER_VERSION 4
Rafael Antognollie2a34552013-12-03 15:35:45 -02004075
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004076 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4077 "shell implementation doesn't match protocol version");
4078
Rafael Antognollie2a34552013-12-03 15:35:45 -02004079 if (args[0].i != XDG_SERVER_VERSION) {
4080 wl_resource_post_error(resource,
4081 WL_DISPLAY_ERROR_INVALID_OBJECT,
4082 "incompatible version, server is %d "
4083 "client wants %d",
4084 XDG_SERVER_VERSION, args[0].i);
4085 return 0;
4086 }
4087
4088 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004089 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004090
4091 return 1;
4092}
4093
4094/* end of xdg-shell implementation */
4095/***********************************/
4096
Kristian Høgsberg07937562011-04-12 17:25:42 -04004097static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004098shell_fade(struct desktop_shell *shell, enum fade_type type);
4099
4100static int
4101screensaver_timeout(void *data)
4102{
4103 struct desktop_shell *shell = data;
4104
4105 shell_fade(shell, FADE_OUT);
4106
4107 return 1;
4108}
4109
4110static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004111handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02004112{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004113 struct desktop_shell *shell =
4114 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004115
Pekka Paalanen18027e52011-12-02 16:31:49 +02004116 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004117
4118 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02004119 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02004120}
4121
4122static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004123launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004124{
4125 if (shell->screensaver.binding)
4126 return;
4127
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004128 if (!shell->screensaver.path) {
4129 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004130 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004131 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004132
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004133 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02004134 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004135 return;
4136 }
4137
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004138 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004139 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004140 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004141 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004142}
4143
4144static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004145terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004146{
Pekka Paalanen18027e52011-12-02 16:31:49 +02004147 if (shell->screensaver.process.pid == 0)
4148 return;
4149
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03004150 /* Disarm the screensaver timer, otherwise it may fire when the
4151 * compositor is not in the idle state. In that case, the screen will
4152 * be locked, but the wake_signal won't fire on user input, making the
4153 * system unresponsive. */
4154 wl_event_source_timer_update(shell->screensaver.timer, 0);
4155
Pekka Paalanen18027e52011-12-02 16:31:49 +02004156 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004157}
4158
4159static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004160configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004161{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004162 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004163
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004164 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004165 if (v->output == ev->output && v != ev) {
4166 weston_view_unmap(v);
4167 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004168 }
4169 }
4170
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004171 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004172
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004173 if (wl_list_empty(&ev->layer_link.link)) {
4174 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004175 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004176 }
4177}
4178
4179static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004180background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004181{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004182 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004183 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004184
Jason Ekstranda7af7042013-10-12 22:38:11 -05004185 view = container_of(es->views.next, struct weston_view, surface_link);
4186
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004187 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004188}
4189
4190static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004191desktop_shell_set_background(struct wl_client *client,
4192 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004193 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004194 struct wl_resource *surface_resource)
4195{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004196 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004197 struct weston_surface *surface =
4198 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004199 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004200
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004201 if (surface->configure) {
4202 wl_resource_post_error(surface_resource,
4203 WL_DISPLAY_ERROR_INVALID_OBJECT,
4204 "surface role already assigned");
4205 return;
4206 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004207
Jason Ekstranda7af7042013-10-12 22:38:11 -05004208 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4209 weston_view_destroy(view);
4210 view = weston_view_create(surface);
4211
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004212 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004213 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004214 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004215 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004216 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004217 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004218 surface->output->width,
4219 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004220}
4221
4222static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004223panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004224{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004225 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004226 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004227
Jason Ekstranda7af7042013-10-12 22:38:11 -05004228 view = container_of(es->views.next, struct weston_view, surface_link);
4229
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004230 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004231}
4232
4233static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004234desktop_shell_set_panel(struct wl_client *client,
4235 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004236 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004237 struct wl_resource *surface_resource)
4238{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004239 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004240 struct weston_surface *surface =
4241 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004242 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004243
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004244 if (surface->configure) {
4245 wl_resource_post_error(surface_resource,
4246 WL_DISPLAY_ERROR_INVALID_OBJECT,
4247 "surface role already assigned");
4248 return;
4249 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004250
Jason Ekstranda7af7042013-10-12 22:38:11 -05004251 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4252 weston_view_destroy(view);
4253 view = weston_view_create(surface);
4254
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004255 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004256 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004257 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004258 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004259 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004260 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004261 surface->output->width,
4262 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004263}
4264
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004265static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004266lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004267{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004268 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004269 struct weston_view *view;
4270
4271 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004272
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004273 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004274 return;
4275
Jason Ekstranda7af7042013-10-12 22:38:11 -05004276 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004277
4278 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004279 weston_layer_entry_insert(&shell->lock_layer.view_list,
4280 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004281 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004282 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004283 }
4284}
4285
4286static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004287handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004288{
Tiago Vignattibe143262012-04-16 17:31:41 +03004289 struct desktop_shell *shell =
4290 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004291
Martin Minarik6d118362012-06-07 18:01:59 +02004292 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004293 shell->lock_surface = NULL;
4294}
4295
4296static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004297desktop_shell_set_lock_surface(struct wl_client *client,
4298 struct wl_resource *resource,
4299 struct wl_resource *surface_resource)
4300{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004301 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004302 struct weston_surface *surface =
4303 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004304
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004305 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004306
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004307 if (!shell->locked)
4308 return;
4309
Pekka Paalanen98262232011-12-01 10:42:22 +02004310 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004311
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004312 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004313 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004314 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004315
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004316 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004317 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004318 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004319}
4320
4321static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004322resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004323{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004324 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004325
Pekka Paalanen77346a62011-11-30 16:26:35 +02004326 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004327
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004328 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004329 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004330 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004331 &shell->input_panel_layer.link);
4332 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004333 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004334 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004335 wl_list_insert(&shell->compositor->cursor_layer.link,
4336 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004337 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004338 wl_list_insert(&shell->fullscreen_layer.link,
4339 &shell->panel_layer.link);
4340 wl_list_insert(&shell->panel_layer.link,
4341 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004342
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004343 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004344
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004345 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004346 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004347 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004348}
4349
4350static void
4351desktop_shell_unlock(struct wl_client *client,
4352 struct wl_resource *resource)
4353{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004354 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004355
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004356 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004357
4358 if (shell->locked)
4359 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004360}
4361
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004362static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004363desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004364 struct wl_resource *resource,
4365 struct wl_resource *surface_resource)
4366{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004367 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004368
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004369 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004370 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004371}
4372
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004373static void
4374desktop_shell_desktop_ready(struct wl_client *client,
4375 struct wl_resource *resource)
4376{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004377 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004378
4379 shell_fade_startup(shell);
4380}
4381
Jonny Lamb765760d2014-08-20 15:53:19 +02004382static void
4383desktop_shell_set_panel_position(struct wl_client *client,
4384 struct wl_resource *resource,
4385 uint32_t position)
4386{
4387 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4388
4389 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4390 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4391 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4392 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4393 wl_resource_post_error(resource,
4394 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4395 "bad position argument");
4396 return;
4397 }
4398
4399 shell->panel_position = position;
4400}
4401
Kristian Høgsberg75840622011-09-06 13:48:16 -04004402static const struct desktop_shell_interface desktop_shell_implementation = {
4403 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004404 desktop_shell_set_panel,
4405 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004406 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004407 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004408 desktop_shell_desktop_ready,
4409 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004410};
4411
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004412static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004413get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004414{
4415 struct shell_surface *shsurf;
4416
4417 shsurf = get_shell_surface(surface);
4418 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004419 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004420 return shsurf->type;
4421}
4422
Kristian Høgsberg75840622011-09-06 13:48:16 -04004423static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004424move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004425{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004426 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004427 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004428 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004429
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004430 if (seat->pointer->focus == NULL)
4431 return;
4432
4433 focus = seat->pointer->focus->surface;
4434
Pekka Paalanen01388e22013-04-25 13:57:44 +03004435 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004436 if (surface == NULL)
4437 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004438
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004439 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004440 if (shsurf == NULL || shsurf->state.fullscreen ||
4441 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004442 return;
4443
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004444 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004445}
4446
4447static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004448maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4449{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004450 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004451 struct weston_surface *surface;
4452 struct shell_surface *shsurf;
4453
4454 surface = weston_surface_get_main_surface(focus);
4455 if (surface == NULL)
4456 return;
4457
4458 shsurf = get_shell_surface(surface);
4459 if (shsurf == NULL)
4460 return;
4461
4462 if (!shell_surface_is_xdg_surface(shsurf))
4463 return;
4464
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004465 shsurf->state_requested = true;
4466 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004467 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004468}
4469
4470static void
4471fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4472{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004473 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004474 struct weston_surface *surface;
4475 struct shell_surface *shsurf;
4476
4477 surface = weston_surface_get_main_surface(focus);
4478 if (surface == NULL)
4479 return;
4480
4481 shsurf = get_shell_surface(surface);
4482 if (shsurf == NULL)
4483 return;
4484
4485 if (!shell_surface_is_xdg_surface(shsurf))
4486 return;
4487
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004488 shsurf->state_requested = true;
4489 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004490 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004491 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004492}
4493
4494static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004495touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4496{
Derek Foreman362656b2014-09-04 10:23:05 -05004497 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004498 struct weston_surface *surface;
4499 struct shell_surface *shsurf;
4500
Derek Foreman362656b2014-09-04 10:23:05 -05004501 if (seat->touch->focus == NULL)
4502 return;
4503
4504 focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004505 surface = weston_surface_get_main_surface(focus);
4506 if (surface == NULL)
4507 return;
4508
4509 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004510 if (shsurf == NULL || shsurf->state.fullscreen ||
4511 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004512 return;
4513
4514 surface_touch_move(shsurf, (struct weston_seat *) seat);
4515}
4516
4517static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004518resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004519{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004520 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004521 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004522 uint32_t edges = 0;
4523 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004524 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004525
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004526 if (seat->pointer->focus == NULL)
4527 return;
4528
4529 focus = seat->pointer->focus->surface;
4530
Pekka Paalanen01388e22013-04-25 13:57:44 +03004531 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004532 if (surface == NULL)
4533 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004534
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004535 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004536 if (shsurf == NULL || shsurf->state.fullscreen ||
4537 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004538 return;
4539
Jason Ekstranda7af7042013-10-12 22:38:11 -05004540 weston_view_from_global(shsurf->view,
4541 wl_fixed_to_int(seat->pointer->grab_x),
4542 wl_fixed_to_int(seat->pointer->grab_y),
4543 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004544
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004545 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004546 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004547 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004548 edges |= 0;
4549 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004550 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004551
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004552 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004553 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004554 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004555 edges |= 0;
4556 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004557 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004558
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004559 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004560}
4561
4562static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004563surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004564 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004565{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004566 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004567 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004568 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004569 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004570
Pekka Paalanen01388e22013-04-25 13:57:44 +03004571 /* XXX: broken for windows containing sub-surfaces */
4572 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004573 if (surface == NULL)
4574 return;
4575
4576 shsurf = get_shell_surface(surface);
4577 if (!shsurf)
4578 return;
4579
Jason Ekstranda7af7042013-10-12 22:38:11 -05004580 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004581
Jason Ekstranda7af7042013-10-12 22:38:11 -05004582 if (shsurf->view->alpha > 1.0)
4583 shsurf->view->alpha = 1.0;
4584 if (shsurf->view->alpha < step)
4585 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004586
Jason Ekstranda7af7042013-10-12 22:38:11 -05004587 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004588 weston_surface_damage(surface);
4589}
4590
4591static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004592do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004593 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004594{
Daniel Stone37816df2012-05-16 18:45:18 +01004595 struct weston_seat *ws = (struct weston_seat *) seat;
4596 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004597 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004598 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004599
4600 wl_list_for_each(output, &compositor->output_list, link) {
4601 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004602 wl_fixed_to_double(seat->pointer->x),
4603 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004604 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004605 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004606 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004607 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004608 increment = -output->zoom.increment;
4609 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004610 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004611 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004612 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004613 else
4614 increment = 0;
4615
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004616 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004617
Scott Moreaue6603982012-06-11 13:07:51 -06004618 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004619 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004620 else if (output->zoom.level > output->zoom.max_level)
4621 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004622 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004623 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004624 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004625
Scott Moreaue6603982012-06-11 13:07:51 -06004626 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004627
Jason Ekstranda7af7042013-10-12 22:38:11 -05004628 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004629 }
4630 }
4631}
4632
Scott Moreauccbf29d2012-02-22 14:21:41 -07004633static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004634zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004635 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004636{
4637 do_zoom(seat, time, 0, axis, value);
4638}
4639
4640static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004641zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004642 void *data)
4643{
4644 do_zoom(seat, time, key, 0, 0);
4645}
4646
4647static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004648terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004649 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004650{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004651 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004652
Daniel Stone325fc2d2012-05-30 16:31:58 +01004653 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004654}
4655
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004656static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004657rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4658 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004659{
4660 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004661 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004662 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004663 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004664 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004665
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004666 weston_pointer_move(pointer, x, y);
4667
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004668 if (!shsurf)
4669 return;
4670
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004671 cx = 0.5f * shsurf->surface->width;
4672 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004673
Daniel Stone37816df2012-05-16 18:45:18 +01004674 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4675 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004676 r = sqrtf(dx * dx + dy * dy);
4677
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004678 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004679 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004680
4681 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004682 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004683 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004684
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004685 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004686 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004687
4688 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004689 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004690 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004691 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004692 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004693
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004694 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004695 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004696 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004697 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004698 wl_list_init(&shsurf->rotation.transform.link);
4699 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004700 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004701 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004702
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004703 /* We need to adjust the position of the surface
4704 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004705 cposx = shsurf->view->geometry.x + cx;
4706 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004707 dposx = rotate->center.x - cposx;
4708 dposy = rotate->center.y - cposy;
4709 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004710 weston_view_set_position(shsurf->view,
4711 shsurf->view->geometry.x + dposx,
4712 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004713 }
4714
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004715 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004716 * lazily applies the damage due to rotation update.
4717 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004718 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004719}
4720
4721static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004722rotate_grab_button(struct weston_pointer_grab *grab,
4723 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004724{
4725 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004726 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004727 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004728 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004729 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004730
Daniel Stone4dbadb12012-05-30 16:31:51 +01004731 if (pointer->button_count == 0 &&
4732 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004733 if (shsurf)
4734 weston_matrix_multiply(&shsurf->rotation.rotation,
4735 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004736 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004737 free(rotate);
4738 }
4739}
4740
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004741static void
4742rotate_grab_cancel(struct weston_pointer_grab *grab)
4743{
4744 struct rotate_grab *rotate =
4745 container_of(grab, struct rotate_grab, base.grab);
4746
4747 shell_grab_end(&rotate->base);
4748 free(rotate);
4749}
4750
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004751static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004752 noop_grab_focus,
4753 rotate_grab_motion,
4754 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004755 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004756};
4757
4758static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004759surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004760{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004761 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004762 float dx, dy;
4763 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004764
Pekka Paalanen460099f2012-01-20 16:48:25 +02004765 rotate = malloc(sizeof *rotate);
4766 if (!rotate)
4767 return;
4768
Jason Ekstranda7af7042013-10-12 22:38:11 -05004769 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004770 surface->surface->width * 0.5f,
4771 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004772 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004773
Daniel Stone37816df2012-05-16 18:45:18 +01004774 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4775 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004776 r = sqrtf(dx * dx + dy * dy);
4777 if (r > 20.0f) {
4778 struct weston_matrix inverse;
4779
4780 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004781 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004782 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004783
4784 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004785 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004786 } else {
4787 weston_matrix_init(&surface->rotation.rotation);
4788 weston_matrix_init(&rotate->rotation);
4789 }
4790
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004791 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4792 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004793}
4794
4795static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004796rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004797 void *data)
4798{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004799 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004800 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004801 struct shell_surface *surface;
4802
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004803 if (seat->pointer->focus == NULL)
4804 return;
4805
4806 focus = seat->pointer->focus->surface;
4807
Pekka Paalanen01388e22013-04-25 13:57:44 +03004808 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004809 if (base_surface == NULL)
4810 return;
4811
4812 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004813 if (surface == NULL || surface->state.fullscreen ||
4814 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004815 return;
4816
4817 surface_rotate(surface, seat);
4818}
4819
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004820/* Move all fullscreen layers down to the current workspace and hide their
4821 * black views. The surfaces' state is set to both fullscreen and lowered,
4822 * and this is reversed when such a surface is re-configured, see
4823 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4824 *
4825 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004826 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004827void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004828lower_fullscreen_layer(struct desktop_shell *shell)
4829{
4830 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004831 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004832
4833 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004834 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004835 &shell->fullscreen_layer.view_list.link,
4836 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004837 struct shell_surface *shsurf = get_shell_surface(view->surface);
4838
4839 if (!shsurf)
4840 continue;
4841
4842 /* We can have a non-fullscreen popup for a fullscreen surface
4843 * in the fullscreen layer. */
4844 if (shsurf->state.fullscreen) {
4845 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004846 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4847 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004848 weston_view_damage_below(shsurf->fullscreen.black_view);
4849
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004850 }
4851
4852 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004853 weston_layer_entry_remove(&view->layer_link);
4854 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004855 weston_view_damage_below(view);
4856 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004857
4858 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004859 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004860}
4861
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004862void
Tiago Vignattibe143262012-04-16 17:31:41 +03004863activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004864 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004865{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004866 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004867 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004868 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004869 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004870 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004871
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004872 lower_fullscreen_layer(shell);
4873
Pekka Paalanen01388e22013-04-25 13:57:44 +03004874 main_surface = weston_surface_get_main_surface(es);
4875
Daniel Stone37816df2012-05-16 18:45:18 +01004876 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004877
Jonas Ådahl8538b222012-08-29 22:13:03 +02004878 state = ensure_focus_state(shell, seat);
4879 if (state == NULL)
4880 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004881
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004882 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004883 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004884
Rafael Antognolli03b16592013-12-03 15:35:42 -02004885 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004886 assert(shsurf);
4887
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004888 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004889 shell_configure_fullscreen(shsurf);
4890 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004891 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004892
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004893 /* Update the surface’s layer. This brings it to the top of the stacking
4894 * order as appropriate. */
4895 shell_surface_update_layer(shsurf);
4896
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004897 if (shell->focus_animation_type != ANIMATION_NONE) {
4898 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004899 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004900 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004901}
4902
Alex Wu21858432012-04-01 20:13:08 +08004903/* no-op func for checking black surface */
4904static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004905black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004906{
4907}
4908
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004909static bool
Alex Wu21858432012-04-01 20:13:08 +08004910is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4911{
4912 if (es->configure == black_surface_configure) {
4913 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004914 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004915 return true;
4916 }
4917 return false;
4918}
4919
Kristian Høgsberg75840622011-09-06 13:48:16 -04004920static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004921activate_binding(struct weston_seat *seat,
4922 struct desktop_shell *shell,
4923 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004924{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004925 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004926
Alex Wu9c35e6b2012-03-05 14:13:13 +08004927 if (!focus)
4928 return;
4929
Pekka Paalanen01388e22013-04-25 13:57:44 +03004930 if (is_black_surface(focus, &main_surface))
4931 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004932
Pekka Paalanen01388e22013-04-25 13:57:44 +03004933 main_surface = weston_surface_get_main_surface(focus);
4934 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004935 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004936
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004937 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004938}
4939
4940static void
4941click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4942 void *data)
4943{
4944 if (seat->pointer->grab != &seat->pointer->default_grab)
4945 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004946 if (seat->pointer->focus == NULL)
4947 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004948
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004949 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004950}
4951
4952static void
4953touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4954{
4955 if (seat->touch->grab != &seat->touch->default_grab)
4956 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004957 if (seat->touch->focus == NULL)
4958 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004959
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004960 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004961}
4962
4963static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004964unfocus_all_seats(struct desktop_shell *shell)
4965{
4966 struct weston_seat *seat, *next;
4967
4968 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4969 if (seat->keyboard == NULL)
4970 continue;
4971
4972 weston_keyboard_set_focus(seat->keyboard, NULL);
4973 }
4974}
4975
4976static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004977lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004978{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004979 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004980
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004981 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004982 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004983 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004984 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004985
4986 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004987
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004988 /* Hide all surfaces by removing the fullscreen, panel and
4989 * toplevel layers. This way nothing else can show or receive
4990 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004991
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004992 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004993 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004994 if (shell->showing_input_panels)
4995 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004996 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004997 wl_list_insert(&shell->compositor->cursor_layer.link,
4998 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004999
Pekka Paalanen77346a62011-11-30 16:26:35 +02005000 launch_screensaver(shell);
5001
Neil Robertsb4a91702014-04-09 16:33:32 +01005002 /* Remove the keyboard focus on all seats. This will be
5003 * restored to the workspace's saved state via
5004 * restore_focus_state when the compositor is unlocked */
5005 unfocus_all_seats(shell);
5006
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005007 /* TODO: disable bindings that should not work while locked. */
5008
5009 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005010}
5011
5012static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005013unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005014{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005015 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005016 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005017 return;
5018 }
5019
5020 /* If desktop-shell client has gone away, unlock immediately. */
5021 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005022 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005023 return;
5024 }
5025
5026 if (shell->prepare_event_sent)
5027 return;
5028
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005029 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005030 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005031}
5032
5033static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005034shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005035{
5036 struct desktop_shell *shell = data;
5037
5038 shell->fade.animation = NULL;
5039
5040 switch (shell->fade.type) {
5041 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005042 weston_surface_destroy(shell->fade.view->surface);
5043 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005044 break;
5045 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005046 lock(shell);
5047 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005048 default:
5049 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005050 }
5051}
5052
Jason Ekstranda7af7042013-10-12 22:38:11 -05005053static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005054shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005055{
5056 struct weston_compositor *compositor = shell->compositor;
5057 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005058 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005059
5060 surface = weston_surface_create(compositor);
5061 if (!surface)
5062 return NULL;
5063
Jason Ekstranda7af7042013-10-12 22:38:11 -05005064 view = weston_view_create(surface);
5065 if (!view) {
5066 weston_surface_destroy(surface);
5067 return NULL;
5068 }
5069
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005070 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005071 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005072 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005073 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5074 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005075 pixman_region32_init(&surface->input);
5076
Jason Ekstranda7af7042013-10-12 22:38:11 -05005077 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005078}
5079
5080static void
5081shell_fade(struct desktop_shell *shell, enum fade_type type)
5082{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005083 float tint;
5084
5085 switch (type) {
5086 case FADE_IN:
5087 tint = 0.0;
5088 break;
5089 case FADE_OUT:
5090 tint = 1.0;
5091 break;
5092 default:
5093 weston_log("shell: invalid fade type\n");
5094 return;
5095 }
5096
5097 shell->fade.type = type;
5098
Jason Ekstranda7af7042013-10-12 22:38:11 -05005099 if (shell->fade.view == NULL) {
5100 shell->fade.view = shell_fade_create_surface(shell);
5101 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005102 return;
5103
Jason Ekstranda7af7042013-10-12 22:38:11 -05005104 shell->fade.view->alpha = 1.0 - tint;
5105 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005106 }
5107
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005108 if (shell->fade.view->output == NULL) {
5109 /* If the black view gets a NULL output, we lost the
5110 * last output and we'll just cancel the fade. This
5111 * happens when you close the last window under the
5112 * X11 or Wayland backends. */
5113 shell->locked = false;
5114 weston_surface_destroy(shell->fade.view->surface);
5115 shell->fade.view = NULL;
5116 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005117 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005118 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005119 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005120 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005121 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005122 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005123 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005124}
5125
5126static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005127do_shell_fade_startup(void *data)
5128{
5129 struct desktop_shell *shell = data;
5130
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005131 if (shell->startup_animation_type == ANIMATION_FADE)
5132 shell_fade(shell, FADE_IN);
5133 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005134 weston_surface_destroy(shell->fade.view->surface);
5135 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005136 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005137}
5138
5139static void
5140shell_fade_startup(struct desktop_shell *shell)
5141{
5142 struct wl_event_loop *loop;
5143
5144 if (!shell->fade.startup_timer)
5145 return;
5146
5147 wl_event_source_remove(shell->fade.startup_timer);
5148 shell->fade.startup_timer = NULL;
5149
5150 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5151 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5152}
5153
5154static int
5155fade_startup_timeout(void *data)
5156{
5157 struct desktop_shell *shell = data;
5158
5159 shell_fade_startup(shell);
5160 return 0;
5161}
5162
5163static void
5164shell_fade_init(struct desktop_shell *shell)
5165{
5166 /* Make compositor output all black, and wait for the desktop-shell
5167 * client to signal it is ready, then fade in. The timer triggers a
5168 * fade-in, in case the desktop-shell client takes too long.
5169 */
5170
5171 struct wl_event_loop *loop;
5172
Jason Ekstranda7af7042013-10-12 22:38:11 -05005173 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005174 weston_log("%s: warning: fade surface already exists\n",
5175 __func__);
5176 return;
5177 }
5178
Jason Ekstranda7af7042013-10-12 22:38:11 -05005179 shell->fade.view = shell_fade_create_surface(shell);
5180 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005181 return;
5182
Jason Ekstranda7af7042013-10-12 22:38:11 -05005183 weston_view_update_transform(shell->fade.view);
5184 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005185
5186 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5187 shell->fade.startup_timer =
5188 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5189 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5190}
5191
5192static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005193idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005194{
5195 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005196 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005197 struct weston_seat *seat;
5198
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005199 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005200 if (seat->pointer)
5201 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005202 if (seat->touch)
5203 touch_popup_grab_end(seat->touch);
5204 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005205
5206 shell_fade(shell, FADE_OUT);
5207 /* lock() is called from shell_fade_done() */
5208}
5209
5210static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005211wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005212{
5213 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005214 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005215
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005216 unlock(shell);
5217}
5218
5219static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005220center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005221{
Giulio Camuffob8366642013-04-25 13:57:46 +03005222 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005223 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005224
Jason Ekstranda7af7042013-10-12 22:38:11 -05005225 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005226
5227 x = output->x + (output->width - width) / 2 - surf_x / 2;
5228 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005229
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005230 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005231}
5232
5233static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005234weston_view_set_initial_position(struct weston_view *view,
5235 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005236{
5237 struct weston_compositor *compositor = shell->compositor;
5238 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005239 int32_t range_x, range_y;
5240 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005241 struct weston_output *output, *target_output = NULL;
5242 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005243 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005244
5245 /* As a heuristic place the new window on the same output as the
5246 * pointer. Falling back to the output containing 0, 0.
5247 *
5248 * TODO: Do something clever for touch too?
5249 */
5250 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005251 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005252 ix = wl_fixed_to_int(seat->pointer->x);
5253 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005254 break;
5255 }
5256 }
5257
5258 wl_list_for_each(output, &compositor->output_list, link) {
5259 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5260 target_output = output;
5261 break;
5262 }
5263 }
5264
5265 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005266 weston_view_set_position(view, 10 + random() % 400,
5267 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005268 return;
5269 }
5270
5271 /* Valid range within output where the surface will still be onscreen.
5272 * If this is negative it means that the surface is bigger than
5273 * output.
5274 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005275 get_output_work_area(shell, target_output, &area);
5276
5277 dx = area.x;
5278 dy = area.y;
5279 range_x = area.width - view->surface->width;
5280 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005281
Rob Bradford4cb88c72012-08-13 15:18:44 +01005282 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005283 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005284
5285 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005286 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005287
5288 x = target_output->x + dx;
5289 y = target_output->y + dy;
5290
Jason Ekstranda7af7042013-10-12 22:38:11 -05005291 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005292}
5293
5294static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005295set_maximized_position(struct desktop_shell *shell,
5296 struct shell_surface *shsurf)
5297{
5298 int32_t surf_x, surf_y;
5299 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005300 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005301
Jonny Lambd73c6942014-08-20 15:53:20 +02005302 get_output_work_area(shell, shsurf->output, &area);
5303 surface_subsurfaces_boundingbox(shsurf->surface,
5304 &surf_x, &surf_y, NULL, NULL);
Marek Chalupa8b771af2014-09-01 17:20:33 +02005305 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005306
5307 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005308 e->x1 + area.x - surf_x,
5309 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005310}
5311
5312static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005313map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005314 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005315{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005316 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005317 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005318
Pekka Paalanen77346a62011-11-30 16:26:35 +02005319 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005320 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005321 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005322 if (shsurf->state.fullscreen) {
5323 center_on_output(shsurf->view, shsurf->fullscreen_output);
5324 shell_map_fullscreen(shsurf);
5325 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005326 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005327 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005328 weston_view_set_initial_position(shsurf->view, shell);
5329 }
Juan Zhao96879df2012-02-07 08:45:41 +08005330 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005331 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005332 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005333 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005334 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005335 weston_view_set_position(shsurf->view,
5336 shsurf->view->geometry.x + sx,
5337 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005338 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005339 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005340 default:
5341 ;
5342 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005343
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005344 /* Surface stacking order, see also activate(). */
5345 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005346
Jason Ekstranda7af7042013-10-12 22:38:11 -05005347 if (shsurf->type != SHELL_SURFACE_NONE) {
5348 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005349 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005350 shsurf->surface->output = shsurf->output;
5351 shsurf->view->output = shsurf->output;
5352 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005353 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005354
Jason Ekstranda7af7042013-10-12 22:38:11 -05005355 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005356 /* XXX: xwayland's using the same fields for transient type */
5357 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005358 if (shsurf->transient.flags ==
5359 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5360 break;
5361 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005362 if (shsurf->state.relative &&
5363 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5364 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005365 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005366 break;
5367 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005368 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005369 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005370 case SHELL_SURFACE_POPUP:
5371 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005372 default:
5373 break;
5374 }
5375
Rafael Antognolli03b16592013-12-03 15:35:42 -02005376 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5377 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005378 {
5379 switch (shell->win_animation_type) {
5380 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005381 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005382 break;
5383 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005384 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005385 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005386 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005387 default:
5388 break;
5389 }
5390 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005391}
5392
5393static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005394configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005395 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005396{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005397 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005398 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005399
Pekka Paalanen77346a62011-11-30 16:26:35 +02005400 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005401
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005402 assert(shsurf);
5403
Rafael Antognolli03b16592013-12-03 15:35:42 -02005404 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005405 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005406 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005407 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005408 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005409 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005410 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005411
Alex Wu4539b082012-03-01 12:57:46 +08005412 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005413 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005414 wl_list_for_each(view, &surface->views, surface_link)
5415 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005416
Rafael Antognolli03b16592013-12-03 15:35:42 -02005417 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005418 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005419 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005420}
5421
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005422static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005423shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005424{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005425 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005426 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005427 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005428
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005429 assert(shsurf);
5430
5431 shell = shsurf->shell;
5432
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005433 if (!weston_surface_is_mapped(es) &&
5434 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005435 remove_popup_grab(shsurf);
5436 }
5437
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005438 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005439 return;
5440
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005441 if (shsurf->has_next_geometry) {
5442 shsurf->geometry = shsurf->next_geometry;
5443 shsurf->has_next_geometry = false;
5444 shsurf->has_set_geometry = true;
5445 } else if (!shsurf->has_set_geometry) {
5446 surface_subsurfaces_boundingbox(shsurf->surface,
5447 &shsurf->geometry.x,
5448 &shsurf->geometry.y,
5449 &shsurf->geometry.width,
5450 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005451 }
5452
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005453 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005454 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005455 type_changed = 1;
5456 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005457
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005458 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005459 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005460 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005461 shsurf->last_width != es->width ||
5462 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005463 float from_x, from_y;
5464 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005465
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005466 if (shsurf->resize_edges) {
5467 sx = 0;
5468 sy = 0;
5469 }
5470
5471 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5472 sx = shsurf->last_width - es->width;
5473 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5474 sy = shsurf->last_height - es->height;
5475
5476 shsurf->last_width = es->width;
5477 shsurf->last_height = es->height;
5478
Jason Ekstranda7af7042013-10-12 22:38:11 -05005479 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5480 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005481 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005482 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005483 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005484 }
5485}
5486
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005487static bool
5488check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5489{
5490 struct timespec now;
5491
5492 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5493 return false;
5494
5495 /*
5496 * If the shell helper client dies before the session has been
5497 * up for roughly 30 seconds, better just make Weston shut down,
5498 * because the user likely has no way to interact with the desktop
5499 * anyway.
5500 */
5501 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5502 weston_log("Error: %s apparently cannot run at all.\n",
5503 shell->client);
5504 weston_log_continue(STAMP_SPACE "Quitting...");
5505 wl_display_terminate(shell->compositor->wl_display);
5506
5507 return true;
5508 }
5509
5510 return false;
5511}
5512
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005513static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005514
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005515static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005516respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005517{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005518 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005519
5520 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5521 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005522 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005523 shell->child.deathstamp = time;
5524 shell->child.deathcount = 0;
5525 }
5526
5527 shell->child.deathcount++;
5528 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005529 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005530 return;
5531 }
5532
Pekka Paalanen826dc142014-08-27 12:11:53 +03005533 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005534 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005535}
5536
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005537static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005538desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5539{
5540 struct desktop_shell *shell;
5541
5542 shell = container_of(listener, struct desktop_shell,
5543 child.client_destroy_listener);
5544
Pekka Paalanen826dc142014-08-27 12:11:53 +03005545 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005546 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005547 /*
5548 * unbind_desktop_shell() will reset shell->child.desktop_shell
5549 * before the respawned process has a chance to create a new
5550 * desktop_shell object, because we are being called from the
5551 * wl_client destructor which destroys all wl_resources before
5552 * returning.
5553 */
5554
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005555 if (!check_desktop_shell_crash_too_early(shell))
5556 respawn_desktop_shell_process(shell);
5557
Pekka Paalanen826dc142014-08-27 12:11:53 +03005558 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005559}
5560
5561static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005562launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005563{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005564 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005565
Pekka Paalanen826dc142014-08-27 12:11:53 +03005566 shell->child.client = weston_client_start(shell->compositor,
5567 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005568
Arnaud Vrac42631192014-08-25 20:56:46 +02005569 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005570 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005571 return;
5572 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005573
5574 shell->child.client_destroy_listener.notify =
5575 desktop_shell_client_destroy;
5576 wl_client_add_destroy_listener(shell->child.client,
5577 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005578}
5579
5580static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005581handle_shell_client_destroy(struct wl_listener *listener, void *data)
5582{
5583 struct shell_client *sc =
5584 container_of(listener, struct shell_client, destroy_listener);
5585
5586 if (sc->ping_timer)
5587 wl_event_source_remove(sc->ping_timer);
5588 free(sc);
5589}
5590
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005591static struct shell_client *
5592shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5593 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005594{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005595 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005596
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005597 sc = zalloc(sizeof *sc);
5598 if (sc == NULL) {
5599 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005600 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005601 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005602
5603 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005604 if (sc->resource == NULL) {
5605 free(sc);
5606 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005607 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005608 }
5609
5610 sc->client = client;
5611 sc->shell = shell;
5612 sc->destroy_listener.notify = handle_shell_client_destroy;
5613 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5614
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005615 return sc;
5616}
5617
5618static void
5619bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5620{
5621 struct desktop_shell *shell = data;
5622 struct shell_client *sc;
5623
5624 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5625 if (sc)
5626 wl_resource_set_implementation(sc->resource,
5627 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005628 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005629}
5630
5631static void
5632bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5633{
5634 struct desktop_shell *shell = data;
5635 struct shell_client *sc;
5636
5637 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5638 if (sc)
5639 wl_resource_set_dispatcher(sc->resource,
5640 xdg_shell_unversioned_dispatch,
5641 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005642}
5643
5644static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005645unbind_desktop_shell(struct wl_resource *resource)
5646{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005647 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005648
5649 if (shell->locked)
5650 resume_desktop(shell);
5651
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005652 shell->child.desktop_shell = NULL;
5653 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005654}
5655
5656static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005657bind_desktop_shell(struct wl_client *client,
5658 void *data, uint32_t version, uint32_t id)
5659{
Tiago Vignattibe143262012-04-16 17:31:41 +03005660 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005661 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005662
Jason Ekstranda85118c2013-06-27 20:17:02 -05005663 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005664 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005665
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005666 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005667 wl_resource_set_implementation(resource,
5668 &desktop_shell_implementation,
5669 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005670 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005671
5672 if (version < 2)
5673 shell_fade_startup(shell);
5674
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005675 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005676 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005677
5678 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5679 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005680}
5681
Pekka Paalanen6e168112011-11-24 11:34:05 +02005682static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005683screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005684{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005685 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005686 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005687 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005688
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005689 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005690 return;
5691
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005692 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005693 if (!shell->locked)
5694 return;
5695
Jason Ekstranda7af7042013-10-12 22:38:11 -05005696 view = container_of(surface->views.next, struct weston_view, surface_link);
5697 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005698
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005699 if (wl_list_empty(&view->layer_link.link)) {
5700 prev = container_of(shell->lock_layer.view_list.link.prev,
5701 struct weston_layer_entry, link);
5702 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005703 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005704 wl_event_source_timer_update(shell->screensaver.timer,
5705 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005706 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005707 }
5708}
5709
5710static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005711screensaver_set_surface(struct wl_client *client,
5712 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005713 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005714 struct wl_resource *output_resource)
5715{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005716 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005717 struct weston_surface *surface =
5718 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005719 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005720 struct weston_view *view, *next;
5721
5722 /* Make sure we only have one view */
5723 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5724 weston_view_destroy(view);
5725 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005726
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005727 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005728 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005729 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005730}
5731
5732static const struct screensaver_interface screensaver_implementation = {
5733 screensaver_set_surface
5734};
5735
5736static void
5737unbind_screensaver(struct wl_resource *resource)
5738{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005739 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005740
Pekka Paalanen77346a62011-11-30 16:26:35 +02005741 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005742}
5743
5744static void
5745bind_screensaver(struct wl_client *client,
5746 void *data, uint32_t version, uint32_t id)
5747{
Tiago Vignattibe143262012-04-16 17:31:41 +03005748 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005749 struct wl_resource *resource;
5750
Jason Ekstranda85118c2013-06-27 20:17:02 -05005751 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005752
Pekka Paalanen77346a62011-11-30 16:26:35 +02005753 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005754 wl_resource_set_implementation(resource,
5755 &screensaver_implementation,
5756 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005757 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005758 return;
5759 }
5760
5761 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5762 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005763}
5764
Kristian Høgsberg07045392012-02-19 18:52:44 -05005765struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005766 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005767 struct weston_surface *current;
5768 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005769 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005770 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005771};
5772
5773static void
5774switcher_next(struct switcher *switcher)
5775{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005776 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005777 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005778 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005779 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005780
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005781 /* temporary re-display minimized surfaces */
5782 struct weston_view *tmp;
5783 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005784 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5785 weston_layer_entry_remove(&view->layer_link);
5786 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005787 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5788 *minimized = view;
5789 }
5790
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005791 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005792 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005793 if (shsurf &&
5794 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5795 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005796 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005797 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005798 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005799 next = view->surface;
5800 prev = view->surface;
5801 view->alpha = 0.25;
5802 weston_view_geometry_dirty(view);
5803 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005804 }
Alex Wu1659daa2012-04-01 20:13:09 +08005805
Jason Ekstranda7af7042013-10-12 22:38:11 -05005806 if (is_black_surface(view->surface, NULL)) {
5807 view->alpha = 0.25;
5808 weston_view_geometry_dirty(view);
5809 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005810 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005811 }
5812
5813 if (next == NULL)
5814 next = first;
5815
Alex Wu07b26062012-03-12 16:06:01 +08005816 if (next == NULL)
5817 return;
5818
Kristian Høgsberg07045392012-02-19 18:52:44 -05005819 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005820 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005821
5822 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005823 wl_list_for_each(view, &next->views, surface_link)
5824 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005825
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005826 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005827 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005828 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005829}
5830
5831static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005832switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005833{
5834 struct switcher *switcher =
5835 container_of(listener, struct switcher, listener);
5836
5837 switcher_next(switcher);
5838}
5839
5840static void
Daniel Stone351eb612012-05-31 15:27:47 -04005841switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005842{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005843 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005844 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005845 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005846
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005847 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005848 if (is_focus_view(view))
5849 continue;
5850
Jason Ekstranda7af7042013-10-12 22:38:11 -05005851 view->alpha = 1.0;
5852 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005853 }
5854
Alex Wu07b26062012-03-12 16:06:01 +08005855 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005856 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005857 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005858 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005859 weston_keyboard_end_grab(keyboard);
5860 if (keyboard->input_method_resource)
5861 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005862
5863 /* re-hide surfaces that were temporary shown during the switch */
5864 struct weston_view **minimized;
5865 wl_array_for_each(minimized, &switcher->minimized_array) {
5866 /* with the exception of the current selected */
5867 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005868 weston_layer_entry_remove(&(*minimized)->layer_link);
5869 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005870 weston_view_damage_below(*minimized);
5871 }
5872 }
5873 wl_array_release(&switcher->minimized_array);
5874
Kristian Høgsberg07045392012-02-19 18:52:44 -05005875 free(switcher);
5876}
5877
5878static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005879switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005880 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005881{
5882 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005883 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005884
Daniel Stonec9785ea2012-05-30 16:31:52 +01005885 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005886 switcher_next(switcher);
5887}
5888
5889static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005890switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005891 uint32_t mods_depressed, uint32_t mods_latched,
5892 uint32_t mods_locked, uint32_t group)
5893{
5894 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005895 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005896
Daniel Stone351eb612012-05-31 15:27:47 -04005897 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5898 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005899}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005900
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005901static void
5902switcher_cancel(struct weston_keyboard_grab *grab)
5903{
5904 struct switcher *switcher = container_of(grab, struct switcher, grab);
5905
5906 switcher_destroy(switcher);
5907}
5908
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005909static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005910 switcher_key,
5911 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005912 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005913};
5914
5915static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005916switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005917 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005918{
Tiago Vignattibe143262012-04-16 17:31:41 +03005919 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005920 struct switcher *switcher;
5921
5922 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005923 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005924 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005925 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005926 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005927 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005928
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005929 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005930 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005931 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005932 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5933 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005934 switcher_next(switcher);
5935}
5936
Pekka Paalanen3c647232011-12-22 13:43:43 +02005937static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005938backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005939 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005940{
5941 struct weston_compositor *compositor = data;
5942 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005943 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005944
5945 /* TODO: we're limiting to simple use cases, where we assume just
5946 * control on the primary display. We'd have to extend later if we
5947 * ever get support for setting backlights on random desktop LCD
5948 * panels though */
5949 output = get_default_output(compositor);
5950 if (!output)
5951 return;
5952
5953 if (!output->set_backlight)
5954 return;
5955
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005956 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5957 backlight_new = output->backlight_current - 25;
5958 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5959 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005960
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005961 if (backlight_new < 5)
5962 backlight_new = 5;
5963 if (backlight_new > 255)
5964 backlight_new = 255;
5965
5966 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005967 output->set_backlight(output, output->backlight_current);
5968}
5969
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005970struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005971 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005972 struct weston_seat *seat;
5973 uint32_t key[2];
5974 int key_released[2];
5975};
5976
5977static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005978debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005979 uint32_t key, uint32_t state)
5980{
5981 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005982 struct weston_compositor *ec = db->seat->compositor;
5983 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005984 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005985 uint32_t serial;
5986 int send = 0, terminate = 0;
5987 int check_binding = 1;
5988 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005989 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005990
5991 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5992 /* Do not run bindings on key releases */
5993 check_binding = 0;
5994
5995 for (i = 0; i < 2; i++)
5996 if (key == db->key[i])
5997 db->key_released[i] = 1;
5998
5999 if (db->key_released[0] && db->key_released[1]) {
6000 /* All key releases been swalled so end the grab */
6001 terminate = 1;
6002 } else if (key != db->key[0] && key != db->key[1]) {
6003 /* Should not swallow release of other keys */
6004 send = 1;
6005 }
6006 } else if (key == db->key[0] && !db->key_released[0]) {
6007 /* Do not check bindings for the first press of the binding
6008 * key. This allows it to be used as a debug shortcut.
6009 * We still need to swallow this event. */
6010 check_binding = 0;
6011 } else if (db->key[1]) {
6012 /* If we already ran a binding don't process another one since
6013 * we can't keep track of all the binding keys that were
6014 * pressed in order to swallow the release events. */
6015 send = 1;
6016 check_binding = 0;
6017 }
6018
6019 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006020 if (weston_compositor_run_debug_binding(ec, db->seat, time,
6021 key, state)) {
6022 /* We ran a binding so swallow the press and keep the
6023 * grab to swallow the released too. */
6024 send = 0;
6025 terminate = 0;
6026 db->key[1] = key;
6027 } else {
6028 /* Terminate the grab since the key pressed is not a
6029 * debug binding key. */
6030 send = 1;
6031 terminate = 1;
6032 }
6033 }
6034
6035 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01006036 serial = wl_display_next_serial(display);
6037 resource_list = &grab->keyboard->focus_resource_list;
6038 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006039 wl_keyboard_send_key(resource, serial, time, key, state);
6040 }
6041 }
6042
6043 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006044 weston_keyboard_end_grab(grab->keyboard);
6045 if (grab->keyboard->input_method_resource)
6046 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006047 free(db);
6048 }
6049}
6050
6051static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006052debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006053 uint32_t mods_depressed, uint32_t mods_latched,
6054 uint32_t mods_locked, uint32_t group)
6055{
6056 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01006057 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006058
Neil Roberts96d790e2013-09-19 17:32:00 +01006059 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006060
Neil Roberts96d790e2013-09-19 17:32:00 +01006061 wl_resource_for_each(resource, resource_list) {
6062 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
6063 mods_latched, mods_locked, group);
6064 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006065}
6066
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006067static void
6068debug_binding_cancel(struct weston_keyboard_grab *grab)
6069{
6070 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
6071
6072 weston_keyboard_end_grab(grab->keyboard);
6073 free(db);
6074}
6075
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006076struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006077 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006078 debug_binding_modifiers,
6079 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006080};
6081
6082static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006083debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006084{
6085 struct debug_binding_grab *grab;
6086
6087 grab = calloc(1, sizeof *grab);
6088 if (!grab)
6089 return;
6090
6091 grab->seat = (struct weston_seat *) seat;
6092 grab->key[0] = key;
6093 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006094 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006095}
6096
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006097static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006098force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006099 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006100{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006101 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006102 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006103 struct desktop_shell *shell = data;
6104 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006105 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006106
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006107 focus_surface = seat->keyboard->focus;
6108 if (!focus_surface)
6109 return;
6110
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006111 wl_signal_emit(&compositor->kill_signal, focus_surface);
6112
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006113 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006114 wl_client_get_credentials(client, &pid, NULL, NULL);
6115
6116 /* Skip clients that we launched ourselves (the credentials of
6117 * the socketpair is ours) */
6118 if (pid == getpid())
6119 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006120
Daniel Stone325fc2d2012-05-30 16:31:58 +01006121 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006122}
6123
6124static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006125workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006126 uint32_t key, void *data)
6127{
6128 struct desktop_shell *shell = data;
6129 unsigned int new_index = shell->workspaces.current;
6130
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006131 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006132 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006133 if (new_index != 0)
6134 new_index--;
6135
6136 change_workspace(shell, new_index);
6137}
6138
6139static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006140workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006141 uint32_t key, void *data)
6142{
6143 struct desktop_shell *shell = data;
6144 unsigned int new_index = shell->workspaces.current;
6145
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006146 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006147 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006148 if (new_index < shell->workspaces.num - 1)
6149 new_index++;
6150
6151 change_workspace(shell, new_index);
6152}
6153
6154static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006155workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006156 uint32_t key, void *data)
6157{
6158 struct desktop_shell *shell = data;
6159 unsigned int new_index;
6160
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006161 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006162 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006163 new_index = key - KEY_F1;
6164 if (new_index >= shell->workspaces.num)
6165 new_index = shell->workspaces.num - 1;
6166
6167 change_workspace(shell, new_index);
6168}
6169
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006170static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006171workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006172 uint32_t key, void *data)
6173{
6174 struct desktop_shell *shell = data;
6175 unsigned int new_index = shell->workspaces.current;
6176
6177 if (shell->locked)
6178 return;
6179
6180 if (new_index != 0)
6181 new_index--;
6182
6183 take_surface_to_workspace_by_seat(shell, seat, new_index);
6184}
6185
6186static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006187workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006188 uint32_t key, void *data)
6189{
6190 struct desktop_shell *shell = data;
6191 unsigned int new_index = shell->workspaces.current;
6192
6193 if (shell->locked)
6194 return;
6195
6196 if (new_index < shell->workspaces.num - 1)
6197 new_index++;
6198
6199 take_surface_to_workspace_by_seat(shell, seat, new_index);
6200}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006201
6202static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006203shell_reposition_view_on_output_destroy(struct weston_view *view)
6204{
6205 struct weston_output *output, *first_output;
6206 struct weston_compositor *ec = view->surface->compositor;
6207 struct shell_surface *shsurf;
6208 float x, y;
6209 int visible;
6210
6211 x = view->geometry.x;
6212 y = view->geometry.y;
6213
6214 /* At this point the destroyed output is not in the list anymore.
6215 * If the view is still visible somewhere, we leave where it is,
6216 * otherwise, move it to the first output. */
6217 visible = 0;
6218 wl_list_for_each(output, &ec->output_list, link) {
6219 if (pixman_region32_contains_point(&output->region,
6220 x, y, NULL)) {
6221 visible = 1;
6222 break;
6223 }
6224 }
6225
6226 if (!visible) {
6227 first_output = container_of(ec->output_list.next,
6228 struct weston_output, link);
6229
6230 x = first_output->x + first_output->width / 4;
6231 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006232
6233 weston_view_set_position(view, x, y);
6234 } else {
6235 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006236 }
6237
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006238
6239 shsurf = get_shell_surface(view->surface);
6240
6241 if (shsurf) {
6242 shsurf->saved_position_valid = false;
6243 shsurf->next_state.maximized = false;
6244 shsurf->next_state.fullscreen = false;
6245 shsurf->state_changed = true;
6246 }
6247}
6248
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006249void
6250shell_for_each_layer(struct desktop_shell *shell,
6251 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006252{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006253 struct workspace **ws;
6254
6255 func(shell, &shell->fullscreen_layer, data);
6256 func(shell, &shell->panel_layer, data);
6257 func(shell, &shell->background_layer, data);
6258 func(shell, &shell->lock_layer, data);
6259 func(shell, &shell->input_panel_layer, data);
6260
6261 wl_array_for_each(ws, &shell->workspaces.array)
6262 func(shell, &(*ws)->layer, data);
6263}
6264
6265static void
6266shell_output_destroy_move_layer(struct desktop_shell *shell,
6267 struct weston_layer *layer,
6268 void *data)
6269{
6270 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006271 struct weston_view *view;
6272
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006273 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006274 if (view->output != output)
6275 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006276
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006277 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006278 }
6279}
6280
6281static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006282handle_output_destroy(struct wl_listener *listener, void *data)
6283{
6284 struct shell_output *output_listener =
6285 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006286 struct weston_output *output = output_listener->output;
6287 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006288
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006289 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006290
Xiong Zhang6b481422013-10-23 13:58:32 +08006291 wl_list_remove(&output_listener->destroy_listener.link);
6292 wl_list_remove(&output_listener->link);
6293 free(output_listener);
6294}
6295
6296static void
6297create_shell_output(struct desktop_shell *shell,
6298 struct weston_output *output)
6299{
6300 struct shell_output *shell_output;
6301
6302 shell_output = zalloc(sizeof *shell_output);
6303 if (shell_output == NULL)
6304 return;
6305
6306 shell_output->output = output;
6307 shell_output->shell = shell;
6308 shell_output->destroy_listener.notify = handle_output_destroy;
6309 wl_signal_add(&output->destroy_signal,
6310 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006311 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006312}
6313
6314static void
6315handle_output_create(struct wl_listener *listener, void *data)
6316{
6317 struct desktop_shell *shell =
6318 container_of(listener, struct desktop_shell, output_create_listener);
6319 struct weston_output *output = (struct weston_output *)data;
6320
6321 create_shell_output(shell, output);
6322}
6323
6324static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006325handle_output_move_layer(struct desktop_shell *shell,
6326 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006327{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006328 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006329 struct weston_view *view;
6330 float x, y;
6331
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006332 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006333 if (view->output != output)
6334 continue;
6335
6336 x = view->geometry.x + output->move_x;
6337 y = view->geometry.y + output->move_y;
6338 weston_view_set_position(view, x, y);
6339 }
6340}
6341
6342static void
6343handle_output_move(struct wl_listener *listener, void *data)
6344{
6345 struct desktop_shell *shell;
6346
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006347 shell = container_of(listener, struct desktop_shell,
6348 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006349
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006350 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006351}
6352
6353static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006354setup_output_destroy_handler(struct weston_compositor *ec,
6355 struct desktop_shell *shell)
6356{
6357 struct weston_output *output;
6358
6359 wl_list_init(&shell->output_list);
6360 wl_list_for_each(output, &ec->output_list, link)
6361 create_shell_output(shell, output);
6362
6363 shell->output_create_listener.notify = handle_output_create;
6364 wl_signal_add(&ec->output_created_signal,
6365 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006366
6367 shell->output_move_listener.notify = handle_output_move;
6368 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006369}
6370
6371static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006372shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006373{
Tiago Vignattibe143262012-04-16 17:31:41 +03006374 struct desktop_shell *shell =
6375 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006376 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006377 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006378
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006379 /* Force state to unlocked so we don't try to fade */
6380 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006381
6382 if (shell->child.client) {
6383 /* disable respawn */
6384 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006385 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006386 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006387
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006388 wl_list_remove(&shell->idle_listener.link);
6389 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006390
6391 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006392
Xiong Zhang6b481422013-10-23 13:58:32 +08006393 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6394 wl_list_remove(&shell_output->destroy_listener.link);
6395 wl_list_remove(&shell_output->link);
6396 free(shell_output);
6397 }
6398
6399 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006400 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006401
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006402 wl_array_for_each(ws, &shell->workspaces.array)
6403 workspace_destroy(*ws);
6404 wl_array_release(&shell->workspaces.array);
6405
Pekka Paalanen3c647232011-12-22 13:43:43 +02006406 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006407 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006408 free(shell);
6409}
6410
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006411static void
6412shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6413{
6414 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006415 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006416
6417 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006418 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6419 MODIFIER_CTRL | MODIFIER_ALT,
6420 terminate_binding, ec);
6421 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6422 click_to_activate_binding,
6423 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006424 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6425 click_to_activate_binding,
6426 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006427 weston_compositor_add_touch_binding(ec, 0,
6428 touch_to_activate_binding,
6429 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006430 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6431 MODIFIER_SUPER | MODIFIER_ALT,
6432 surface_opacity_binding, NULL);
6433 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6434 MODIFIER_SUPER, zoom_axis_binding,
6435 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006436
6437 /* configurable bindings */
6438 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006439 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6440 zoom_key_binding, NULL);
6441 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6442 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006443 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6444 maximize_binding, NULL);
6445 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6446 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006447 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6448 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006449 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006450 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6451 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006452 weston_compositor_add_button_binding(ec, BTN_LEFT,
6453 mod | MODIFIER_SHIFT,
6454 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006455
6456 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6457 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6458 rotate_binding, NULL);
6459
Daniel Stone325fc2d2012-05-30 16:31:58 +01006460 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6461 shell);
6462 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6463 ec);
6464 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6465 backlight_binding, ec);
6466 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6467 ec);
6468 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6469 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006470 weston_compositor_add_key_binding(ec, KEY_K, mod,
6471 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006472 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6473 workspace_up_binding, shell);
6474 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6475 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006476 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6477 workspace_move_surface_up_binding,
6478 shell);
6479 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6480 workspace_move_surface_down_binding,
6481 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006482
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006483 if (shell->exposay_modifier)
6484 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6485 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006486
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006487 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6488 if (shell->workspaces.num > 1) {
6489 num_workspace_bindings = shell->workspaces.num;
6490 if (num_workspace_bindings > 6)
6491 num_workspace_bindings = 6;
6492 for (i = 0; i < num_workspace_bindings; i++)
6493 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6494 workspace_f_binding,
6495 shell);
6496 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006497
6498 /* Debug bindings */
6499 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6500 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006501}
6502
Jason Ekstrand024177c2014-04-21 19:42:58 -05006503static void
6504handle_seat_created(struct wl_listener *listener, void *data)
6505{
6506 struct weston_seat *seat = data;
6507
6508 create_shell_seat(seat);
6509}
6510
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006511WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006512module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006513 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006514{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006515 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006516 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006517 struct workspace **pws;
6518 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006519 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006520
Peter Huttererf3d62272013-08-08 11:57:05 +10006521 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006522 if (shell == NULL)
6523 return -1;
6524
Kristian Høgsberg75840622011-09-06 13:48:16 -04006525 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006526
6527 shell->destroy_listener.notify = shell_destroy;
6528 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006529 shell->idle_listener.notify = idle_handler;
6530 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6531 shell->wake_listener.notify = wake_handler;
6532 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006533
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006534 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006535 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006536 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006537 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006538 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006539 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006540 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006541 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006542 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006543 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006544 ec->shell_interface.set_window_geometry = set_window_geometry;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006545
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006546 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6547 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006548 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6549 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006550 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006551
6552 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006553 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006554
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006555 if (input_panel_setup(shell) < 0)
6556 return -1;
6557
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006558 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006559
Daniel Stonedf8133b2013-11-19 11:37:14 +01006560 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6561 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6562
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006563 for (i = 0; i < shell->workspaces.num; i++) {
6564 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6565 if (pws == NULL)
6566 return -1;
6567
6568 *pws = workspace_create();
6569 if (*pws == NULL)
6570 return -1;
6571 }
6572 activate_workspace(shell, 0);
6573
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006574 weston_layer_init(&shell->minimized_layer, NULL);
6575
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006576 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006577 wl_list_init(&shell->workspaces.animation.link);
6578 shell->workspaces.animation.frame = animate_workspace_change_frame;
6579
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006580 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006581 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006582 return -1;
6583
Rafael Antognollie2a34552013-12-03 15:35:45 -02006584 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6585 shell, bind_xdg_shell) == NULL)
6586 return -1;
6587
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006588 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006589 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006590 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006591 return -1;
6592
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006593 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6594 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006595 return -1;
6596
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006597 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6598 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006599 return -1;
6600
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006601 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006602
Jonny Lamb765760d2014-08-20 15:53:19 +02006603 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6604
Xiong Zhang6b481422013-10-23 13:58:32 +08006605 setup_output_destroy_handler(ec, shell);
6606
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006607 loop = wl_display_get_event_loop(ec->wl_display);
6608 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006609
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006610 shell->screensaver.timer =
6611 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6612
Jason Ekstrand024177c2014-04-21 19:42:58 -05006613 wl_list_for_each(seat, &ec->seat_list, link)
6614 handle_seat_created(NULL, seat);
6615 shell->seat_create_listener.notify = handle_seat_created;
6616 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006617
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006618 screenshooter_create(ec);
6619
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006620 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006621
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006622 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006623
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006624 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6625
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006626 return 0;
6627}