blob: 9eda814c8a412fd6d52d04c51e4261525a6171d1 [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;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200160 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100161 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400162
163 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200164
165 struct {
166 bool maximized;
167 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200168 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100169 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500170 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200171 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500172 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500173
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700174 struct {
175 int left, right, top, bottom;
176 } margin;
177
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
220 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400221 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100222 struct wl_list surfaces_list;
223 struct wl_client *client;
224 int32_t initial_up;
225 } popup_grab;
226};
227
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800228struct shell_client {
229 struct wl_resource *resource;
230 struct wl_client *client;
231 struct desktop_shell *shell;
232 struct wl_listener destroy_listener;
233 struct wl_event_source *ping_timer;
234 uint32_t ping_serial;
235 int unresponsive;
236};
237
Alex Wubd3354b2012-04-17 17:20:49 +0800238static struct desktop_shell *
239shell_surface_get_shell(struct shell_surface *shsurf);
240
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500241static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400242surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500243
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300244static void
245shell_fade_startup(struct desktop_shell *shell);
246
Philip Withnallbecb77e2013-11-25 18:01:30 +0000247static struct shell_seat *
248get_shell_seat(struct weston_seat *seat);
249
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700250static int
251get_output_panel_height(struct desktop_shell *shell,
252 struct weston_output *output);
253
Philip Withnall648a4dd2013-11-25 18:01:44 +0000254static void
255shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
256
Alex Wubd3354b2012-04-17 17:20:49 +0800257static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200258shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
259
260static bool
261shell_surface_is_xdg_surface(struct shell_surface *shsurf);
262
263static bool
264shell_surface_is_xdg_popup(struct shell_surface *shsurf);
265
266static void
267shell_surface_set_parent(struct shell_surface *shsurf,
268 struct weston_surface *parent);
269
270static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800271shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
272{
273 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500274 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800275
276 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100277
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800279 return false;
280
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
282 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800283 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500284 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800285}
286
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500287static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400288destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300289{
290 struct shell_grab *grab;
291
292 grab = container_of(listener, struct shell_grab,
293 shsurf_destroy_listener);
294
295 grab->shsurf = NULL;
296}
297
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800298struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500299get_default_view(struct weston_surface *surface)
300{
301 struct shell_surface *shsurf;
302 struct weston_view *view;
303
304 if (!surface || wl_list_empty(&surface->views))
305 return NULL;
306
307 shsurf = get_shell_surface(surface);
308 if (shsurf)
309 return shsurf->view;
310
311 wl_list_for_each(view, &surface->views, surface_link)
312 if (weston_view_is_mapped(view))
313 return view;
314
315 return container_of(surface->views.next, struct weston_view, surface_link);
316}
317
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300318static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400319popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400320
321static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300322shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400323 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300324 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400325 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300326 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300327{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300328 struct desktop_shell *shell = shsurf->shell;
329
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400330 popup_grab_end(pointer);
331
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332 grab->grab.interface = interface;
333 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400334 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500335 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400336 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300337
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700338 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400339 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400340 if (shell->child.desktop_shell) {
341 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
342 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500343 weston_pointer_set_focus(pointer,
344 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400345 wl_fixed_from_int(0),
346 wl_fixed_from_int(0));
347 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300348}
349
350static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300351shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300352{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700353 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400354 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700355 grab->shsurf->grabbed = 0;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800356 grab->shsurf->resize_edges = 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700357 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300358
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700359 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300360}
361
362static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700363shell_touch_grab_start(struct shell_touch_grab *grab,
364 const struct weston_touch_grab_interface *interface,
365 struct shell_surface *shsurf,
366 struct weston_touch *touch)
367{
368 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800369
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700370 if (touch->seat->pointer)
371 popup_grab_end(touch->seat->pointer);
372
Rusty Lynch1084da52013-08-15 09:10:08 -0700373 grab->grab.interface = interface;
374 grab->shsurf = shsurf;
375 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
376 wl_signal_add(&shsurf->destroy_signal,
377 &grab->shsurf_destroy_listener);
378
379 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700380 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700381
382 weston_touch_start_grab(touch, &grab->grab);
383 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500384 weston_touch_set_focus(touch->seat,
385 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700386}
387
388static void
389shell_touch_grab_end(struct shell_touch_grab *grab)
390{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700391 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700392 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700393 grab->shsurf->grabbed = 0;
394 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700395
396 weston_touch_end_grab(grab->touch);
397}
398
399static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500400center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800401 struct weston_output *output);
402
Daniel Stone496ca172012-05-30 16:31:42 +0100403static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300404get_modifier(char *modifier)
405{
406 if (!modifier)
407 return MODIFIER_SUPER;
408
409 if (!strcmp("ctrl", modifier))
410 return MODIFIER_CTRL;
411 else if (!strcmp("alt", modifier))
412 return MODIFIER_ALT;
413 else if (!strcmp("super", modifier))
414 return MODIFIER_SUPER;
415 else
416 return MODIFIER_SUPER;
417}
418
Juan Zhaoe10d2792012-04-25 19:09:52 +0800419static enum animation_type
420get_animation_type(char *animation)
421{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800422 if (!animation)
423 return ANIMATION_NONE;
424
Juan Zhaoe10d2792012-04-25 19:09:52 +0800425 if (!strcmp("zoom", animation))
426 return ANIMATION_ZOOM;
427 else if (!strcmp("fade", animation))
428 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100429 else if (!strcmp("dim-layer", animation))
430 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800431 else
432 return ANIMATION_NONE;
433}
434
Alex Wu4539b082012-03-01 12:57:46 +0800435static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400436shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200437{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400438 struct weston_config_section *section;
439 int duration;
440 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200441
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400442 section = weston_config_get_section(shell->compositor->config,
443 "screensaver", NULL, NULL);
444 weston_config_section_get_string(section,
445 "path", &shell->screensaver.path, NULL);
446 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200447 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400448
449 section = weston_config_get_section(shell->compositor->config,
450 "shell", NULL, NULL);
451 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100452 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100453 shell->client = s;
454 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400455 "binding-modifier", &s, "super");
456 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200457 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800458
459 weston_config_section_get_string(section,
460 "exposay-modifier", &s, "none");
461 if (strcmp(s, "none") == 0)
462 shell->exposay_modifier = 0;
463 else
464 shell->exposay_modifier = get_modifier(s);
465 free(s);
466
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400467 weston_config_section_get_string(section, "animation", &s, "none");
468 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200469 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700470 weston_config_section_get_string(section,
471 "startup-animation", &s, "fade");
472 shell->startup_animation_type = get_animation_type(s);
473 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700474 if (shell->startup_animation_type == ANIMATION_ZOOM)
475 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100476 weston_config_section_get_string(section, "focus-animation", &s, "none");
477 shell->focus_animation_type = get_animation_type(s);
478 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400479 weston_config_section_get_uint(section, "num-workspaces",
480 &shell->workspaces.num,
481 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200482}
483
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800484struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100485get_default_output(struct weston_compositor *compositor)
486{
487 return container_of(compositor->output_list.next,
488 struct weston_output, link);
489}
490
491
492/* no-op func for checking focus surface */
493static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600494focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100495{
496}
497
498static struct focus_surface *
499get_focus_surface(struct weston_surface *surface)
500{
501 if (surface->configure == focus_surface_configure)
502 return surface->configure_private;
503 else
504 return NULL;
505}
506
507static bool
508is_focus_surface (struct weston_surface *es)
509{
510 return (es->configure == focus_surface_configure);
511}
512
513static bool
514is_focus_view (struct weston_view *view)
515{
516 return is_focus_surface (view->surface);
517}
518
519static struct focus_surface *
520create_focus_surface(struct weston_compositor *ec,
521 struct weston_output *output)
522{
523 struct focus_surface *fsurf = NULL;
524 struct weston_surface *surface = NULL;
525
526 fsurf = malloc(sizeof *fsurf);
527 if (!fsurf)
528 return NULL;
529
530 fsurf->surface = weston_surface_create(ec);
531 surface = fsurf->surface;
532 if (surface == NULL) {
533 free(fsurf);
534 return NULL;
535 }
536
537 surface->configure = focus_surface_configure;
538 surface->output = output;
539 surface->configure_private = fsurf;
540
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800541 fsurf->view = weston_view_create(surface);
542 if (fsurf->view == NULL) {
543 weston_surface_destroy(surface);
544 free(fsurf);
545 return NULL;
546 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100547 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100548
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600549 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600550 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100551 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
552 pixman_region32_fini(&surface->opaque);
553 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
554 output->width, output->height);
555 pixman_region32_fini(&surface->input);
556 pixman_region32_init(&surface->input);
557
558 wl_list_init(&fsurf->workspace_transform.link);
559
560 return fsurf;
561}
562
563static void
564focus_surface_destroy(struct focus_surface *fsurf)
565{
566 weston_surface_destroy(fsurf->surface);
567 free(fsurf);
568}
569
570static void
571focus_animation_done(struct weston_view_animation *animation, void *data)
572{
573 struct workspace *ws = data;
574
575 ws->focus_animation = NULL;
576}
577
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200578static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200579focus_state_destroy(struct focus_state *state)
580{
581 wl_list_remove(&state->seat_destroy_listener.link);
582 wl_list_remove(&state->surface_destroy_listener.link);
583 free(state);
584}
585
586static void
587focus_state_seat_destroy(struct wl_listener *listener, void *data)
588{
589 struct focus_state *state = container_of(listener,
590 struct focus_state,
591 seat_destroy_listener);
592
593 wl_list_remove(&state->link);
594 focus_state_destroy(state);
595}
596
597static void
598focus_state_surface_destroy(struct wl_listener *listener, void *data)
599{
600 struct focus_state *state = container_of(listener,
601 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400602 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400603 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500604 struct weston_surface *main_surface, *next;
605 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200606
Pekka Paalanen01388e22013-04-25 13:57:44 +0300607 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
608
Kristian Høgsberge3778222012-07-31 17:29:30 -0400609 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500610 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
611 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400612 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100613 if (is_focus_view(view))
614 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400615
Jason Ekstranda7af7042013-10-12 22:38:11 -0500616 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400617 break;
618 }
619
Pekka Paalanen01388e22013-04-25 13:57:44 +0300620 /* if the focus was a sub-surface, activate its main surface */
621 if (main_surface != state->keyboard_focus)
622 next = main_surface;
623
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100624 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400625 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100626 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100627 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400628 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100629 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
630 if (state->ws->focus_animation)
631 weston_view_animation_destroy(state->ws->focus_animation);
632
633 state->ws->focus_animation = weston_fade_run(
634 state->ws->fsurf_front->view,
635 state->ws->fsurf_front->view->alpha, 0.0, 300,
636 focus_animation_done, state->ws);
637 }
638
Kristian Høgsberge3778222012-07-31 17:29:30 -0400639 wl_list_remove(&state->link);
640 focus_state_destroy(state);
641 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200642}
643
644static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400645focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200646{
Jonas Ådahl04769742012-06-13 00:01:24 +0200647 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200648
649 state = malloc(sizeof *state);
650 if (state == NULL)
651 return NULL;
652
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100653 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400654 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200655 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400656 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200657
658 state->seat_destroy_listener.notify = focus_state_seat_destroy;
659 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400660 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200661 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400662 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200663
664 return state;
665}
666
Jonas Ådahl8538b222012-08-29 22:13:03 +0200667static struct focus_state *
668ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
669{
670 struct workspace *ws = get_current_workspace(shell);
671 struct focus_state *state;
672
673 wl_list_for_each(state, &ws->focus_list, link)
674 if (state->seat == seat)
675 break;
676
677 if (&state->link == &ws->focus_list)
678 state = focus_state_create(seat, ws);
679
680 return state;
681}
682
Jonas Ådahl04769742012-06-13 00:01:24 +0200683static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800684focus_state_set_focus(struct focus_state *state,
685 struct weston_surface *surface)
686{
687 if (state->keyboard_focus) {
688 wl_list_remove(&state->surface_destroy_listener.link);
689 wl_list_init(&state->surface_destroy_listener.link);
690 }
691
692 state->keyboard_focus = surface;
693 if (surface)
694 wl_signal_add(&surface->destroy_signal,
695 &state->surface_destroy_listener);
696}
697
698static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400699restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200700{
701 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400702 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100703 struct wl_list pending_seat_list;
704 struct weston_seat *seat, *next_seat;
705
706 /* Temporarily steal the list of seats so that we can keep
707 * track of the seats we've already processed */
708 wl_list_init(&pending_seat_list);
709 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
710 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200711
712 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100713 wl_list_remove(&state->seat->link);
714 wl_list_insert(&shell->compositor->seat_list,
715 &state->seat->link);
716
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800717 if (state->seat->keyboard == NULL)
718 continue;
719
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400720 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200721
Kristian Høgsberge3148752013-05-06 23:19:49 -0400722 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200723 }
Neil Roberts4237f502014-04-09 16:33:31 +0100724
725 /* For any remaining seats that we don't have a focus state
726 * for we'll reset the keyboard focus to NULL */
727 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
728 wl_list_insert(&shell->compositor->seat_list, &seat->link);
729
730 if (state->seat->keyboard == NULL)
731 continue;
732
733 weston_keyboard_set_focus(seat->keyboard, NULL);
734 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200735}
736
737static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200738replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
739 struct weston_seat *seat)
740{
741 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200742
743 wl_list_for_each(state, &ws->focus_list, link) {
744 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800745 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200746 return;
747 }
748 }
749}
750
751static void
752drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
753 struct weston_surface *surface)
754{
755 struct focus_state *state;
756
757 wl_list_for_each(state, &ws->focus_list, link)
758 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800759 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200760}
761
762static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100763animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
764 struct weston_view *from, struct weston_view *to)
765{
766 struct weston_output *output;
767 bool focus_surface_created = false;
768
769 /* FIXME: Only support dim animation using two layers */
770 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
771 return;
772
773 output = get_default_output(shell->compositor);
774 if (ws->fsurf_front == NULL && (from || to)) {
775 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800776 if (ws->fsurf_front == NULL)
777 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100778 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800779
780 ws->fsurf_back = create_focus_surface(shell->compositor, output);
781 if (ws->fsurf_back == NULL) {
782 focus_surface_destroy(ws->fsurf_front);
783 return;
784 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100785 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800786
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100787 focus_surface_created = true;
788 } else {
789 wl_list_remove(&ws->fsurf_front->view->layer_link);
790 wl_list_remove(&ws->fsurf_back->view->layer_link);
791 }
792
793 if (ws->focus_animation) {
794 weston_view_animation_destroy(ws->focus_animation);
795 ws->focus_animation = NULL;
796 }
797
798 if (to)
799 wl_list_insert(&to->layer_link,
800 &ws->fsurf_front->view->layer_link);
801 else if (from)
802 wl_list_insert(&ws->layer.view_list,
803 &ws->fsurf_front->view->layer_link);
804
805 if (focus_surface_created) {
806 ws->focus_animation = weston_fade_run(
807 ws->fsurf_front->view,
808 ws->fsurf_front->view->alpha, 0.6, 300,
809 focus_animation_done, ws);
810 } else if (from) {
811 wl_list_insert(&from->layer_link,
812 &ws->fsurf_back->view->layer_link);
813 ws->focus_animation = weston_stable_fade_run(
814 ws->fsurf_front->view, 0.0,
815 ws->fsurf_back->view, 0.6,
816 focus_animation_done, ws);
817 } else if (to) {
818 wl_list_insert(&ws->layer.view_list,
819 &ws->fsurf_back->view->layer_link);
820 ws->focus_animation = weston_stable_fade_run(
821 ws->fsurf_front->view, 0.0,
822 ws->fsurf_back->view, 0.6,
823 focus_animation_done, ws);
824 }
825}
826
827static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200828workspace_destroy(struct workspace *ws)
829{
Jonas Ådahl04769742012-06-13 00:01:24 +0200830 struct focus_state *state, *next;
831
832 wl_list_for_each_safe(state, next, &ws->focus_list, link)
833 focus_state_destroy(state);
834
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100835 if (ws->fsurf_front)
836 focus_surface_destroy(ws->fsurf_front);
837 if (ws->fsurf_back)
838 focus_surface_destroy(ws->fsurf_back);
839
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200840 free(ws);
841}
842
Jonas Ådahl04769742012-06-13 00:01:24 +0200843static void
844seat_destroyed(struct wl_listener *listener, void *data)
845{
846 struct weston_seat *seat = data;
847 struct focus_state *state, *next;
848 struct workspace *ws = container_of(listener,
849 struct workspace,
850 seat_destroyed_listener);
851
852 wl_list_for_each_safe(state, next, &ws->focus_list, link)
853 if (state->seat == seat)
854 wl_list_remove(&state->link);
855}
856
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200857static struct workspace *
858workspace_create(void)
859{
860 struct workspace *ws = malloc(sizeof *ws);
861 if (ws == NULL)
862 return NULL;
863
864 weston_layer_init(&ws->layer, NULL);
865
Jonas Ådahl04769742012-06-13 00:01:24 +0200866 wl_list_init(&ws->focus_list);
867 wl_list_init(&ws->seat_destroyed_listener.link);
868 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100869 ws->fsurf_front = NULL;
870 ws->fsurf_back = NULL;
871 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200872
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200873 return ws;
874}
875
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200876static int
877workspace_is_empty(struct workspace *ws)
878{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200880}
881
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200882static struct workspace *
883get_workspace(struct desktop_shell *shell, unsigned int index)
884{
885 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200886 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200887 pws += index;
888 return *pws;
889}
890
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800891struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200892get_current_workspace(struct desktop_shell *shell)
893{
894 return get_workspace(shell, shell->workspaces.current);
895}
896
897static void
898activate_workspace(struct desktop_shell *shell, unsigned int index)
899{
900 struct workspace *ws;
901
902 ws = get_workspace(shell, index);
903 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
904
905 shell->workspaces.current = index;
906}
907
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200908static unsigned int
909get_output_height(struct weston_output *output)
910{
911 return abs(output->region.extents.y1 - output->region.extents.y2);
912}
913
914static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100915view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200916{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200917 struct weston_transform *transform;
918
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100919 if (is_focus_view(view)) {
920 struct focus_surface *fsurf = get_focus_surface(view->surface);
921 transform = &fsurf->workspace_transform;
922 } else {
923 struct shell_surface *shsurf = get_shell_surface(view->surface);
924 transform = &shsurf->workspace_transform;
925 }
926
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200927 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500928 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100929 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200930
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100931 weston_matrix_init(&transform->matrix);
932 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200933 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200935}
936
937static void
938workspace_translate_out(struct workspace *ws, double fraction)
939{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200941 unsigned int height;
942 double d;
943
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
945 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200946 d = height * fraction;
947
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100948 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200949 }
950}
951
952static void
953workspace_translate_in(struct workspace *ws, double fraction)
954{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200956 unsigned int height;
957 double d;
958
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
960 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200961
962 if (fraction > 0)
963 d = -(height - height * fraction);
964 else
965 d = height + height * fraction;
966
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100967 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200968 }
969}
970
971static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200972broadcast_current_workspace_state(struct desktop_shell *shell)
973{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700974 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200975
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700976 wl_resource_for_each(resource, &shell->workspaces.client_list)
977 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200978 shell->workspaces.current,
979 shell->workspaces.num);
980}
981
982static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200983reverse_workspace_change_animation(struct desktop_shell *shell,
984 unsigned int index,
985 struct workspace *from,
986 struct workspace *to)
987{
988 shell->workspaces.current = index;
989
990 shell->workspaces.anim_to = to;
991 shell->workspaces.anim_from = from;
992 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
993 shell->workspaces.anim_timestamp = 0;
994
Scott Moreau4272e632012-08-13 09:58:41 -0600995 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996}
997
998static void
999workspace_deactivate_transforms(struct workspace *ws)
1000{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001002 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001003
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001005 if (is_focus_view(view)) {
1006 struct focus_surface *fsurf = get_focus_surface(view->surface);
1007 transform = &fsurf->workspace_transform;
1008 } else {
1009 struct shell_surface *shsurf = get_shell_surface(view->surface);
1010 transform = &shsurf->workspace_transform;
1011 }
1012
1013 if (!wl_list_empty(&transform->link)) {
1014 wl_list_remove(&transform->link);
1015 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001016 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001018 }
1019}
1020
1021static void
1022finish_workspace_change_animation(struct desktop_shell *shell,
1023 struct workspace *from,
1024 struct workspace *to)
1025{
Scott Moreau4272e632012-08-13 09:58:41 -06001026 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001027
1028 wl_list_remove(&shell->workspaces.animation.link);
1029 workspace_deactivate_transforms(from);
1030 workspace_deactivate_transforms(to);
1031 shell->workspaces.anim_to = NULL;
1032
1033 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1034}
1035
1036static void
1037animate_workspace_change_frame(struct weston_animation *animation,
1038 struct weston_output *output, uint32_t msecs)
1039{
1040 struct desktop_shell *shell =
1041 container_of(animation, struct desktop_shell,
1042 workspaces.animation);
1043 struct workspace *from = shell->workspaces.anim_from;
1044 struct workspace *to = shell->workspaces.anim_to;
1045 uint32_t t;
1046 double x, y;
1047
1048 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1049 finish_workspace_change_animation(shell, from, to);
1050 return;
1051 }
1052
1053 if (shell->workspaces.anim_timestamp == 0) {
1054 if (shell->workspaces.anim_current == 0.0)
1055 shell->workspaces.anim_timestamp = msecs;
1056 else
1057 shell->workspaces.anim_timestamp =
1058 msecs -
1059 /* Invers of movement function 'y' below. */
1060 (asin(1.0 - shell->workspaces.anim_current) *
1061 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1062 M_2_PI);
1063 }
1064
1065 t = msecs - shell->workspaces.anim_timestamp;
1066
1067 /*
1068 * x = [0, π/2]
1069 * y(x) = sin(x)
1070 */
1071 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1072 y = sin(x);
1073
1074 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001075 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001076
1077 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1078 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1079 shell->workspaces.anim_current = y;
1080
Scott Moreau4272e632012-08-13 09:58:41 -06001081 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001082 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001083 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001084 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001085}
1086
1087static void
1088animate_workspace_change(struct desktop_shell *shell,
1089 unsigned int index,
1090 struct workspace *from,
1091 struct workspace *to)
1092{
1093 struct weston_output *output;
1094
1095 int dir;
1096
1097 if (index > shell->workspaces.current)
1098 dir = -1;
1099 else
1100 dir = 1;
1101
1102 shell->workspaces.current = index;
1103
1104 shell->workspaces.anim_dir = dir;
1105 shell->workspaces.anim_from = from;
1106 shell->workspaces.anim_to = to;
1107 shell->workspaces.anim_current = 0.0;
1108 shell->workspaces.anim_timestamp = 0;
1109
1110 output = container_of(shell->compositor->output_list.next,
1111 struct weston_output, link);
1112 wl_list_insert(&output->animation_list,
1113 &shell->workspaces.animation.link);
1114
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001115 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001116
1117 workspace_translate_in(to, 0);
1118
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001119 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001120
Scott Moreau4272e632012-08-13 09:58:41 -06001121 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122}
1123
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001124static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001125update_workspace(struct desktop_shell *shell, unsigned int index,
1126 struct workspace *from, struct workspace *to)
1127{
1128 shell->workspaces.current = index;
1129 wl_list_insert(&from->layer.link, &to->layer.link);
1130 wl_list_remove(&from->layer.link);
1131}
1132
1133static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001134change_workspace(struct desktop_shell *shell, unsigned int index)
1135{
1136 struct workspace *from;
1137 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001138 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001139
1140 if (index == shell->workspaces.current)
1141 return;
1142
1143 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001145 return;
1146
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001147 from = get_current_workspace(shell);
1148 to = get_workspace(shell, index);
1149
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001150 if (shell->workspaces.anim_from == to &&
1151 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001152 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001153 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001154 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155 return;
1156 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001157
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001158 if (shell->workspaces.anim_to != NULL)
1159 finish_workspace_change_animation(shell,
1160 shell->workspaces.anim_from,
1161 shell->workspaces.anim_to);
1162
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001163 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001164
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001165 if (shell->focus_animation_type != ANIMATION_NONE) {
1166 wl_list_for_each(state, &from->focus_list, link)
1167 if (state->keyboard_focus)
1168 animate_focus_change(shell, from,
1169 get_default_view(state->keyboard_focus), NULL);
1170
1171 wl_list_for_each(state, &to->focus_list, link)
1172 if (state->keyboard_focus)
1173 animate_focus_change(shell, to,
1174 NULL, get_default_view(state->keyboard_focus));
1175 }
1176
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001177 if (workspace_is_empty(to) && workspace_is_empty(from))
1178 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001179 else
1180 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001181
1182 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001183}
1184
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001185static bool
1186workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1187{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001189 struct wl_list *e;
1190
1191 if (wl_list_empty(list))
1192 return false;
1193
1194 e = list->next;
1195
1196 if (e->next != list)
1197 return false;
1198
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001200}
1201
1202static void
Philip Withnall659163d2013-11-25 18:01:36 +00001203move_surface_to_workspace(struct desktop_shell *shell,
1204 struct shell_surface *shsurf,
1205 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001206{
1207 struct workspace *from;
1208 struct workspace *to;
1209 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001210 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001211 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001212
1213 if (workspace == shell->workspaces.current)
1214 return;
1215
Philip Withnall659163d2013-11-25 18:01:36 +00001216 view = get_default_view(shsurf->surface);
1217 if (!view)
1218 return;
1219
1220 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1221
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001222 if (workspace >= shell->workspaces.num)
1223 workspace = shell->workspaces.num - 1;
1224
Jonas Ådahle9d22502012-08-29 22:13:01 +02001225 from = get_current_workspace(shell);
1226 to = get_workspace(shell, workspace);
1227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 wl_list_remove(&view->layer_link);
1229 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001230
Philip Withnall648a4dd2013-11-25 18:01:44 +00001231 shell_surface_update_child_surface_layers(shsurf);
1232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001234 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1235 if (!seat->keyboard)
1236 continue;
1237
1238 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001240 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001241 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001242
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001244}
1245
1246static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001247take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001248 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001249 unsigned int index)
1250{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001251 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001253 struct shell_surface *shsurf;
1254 struct workspace *from;
1255 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001256 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257
Pekka Paalanen01388e22013-04-25 13:57:44 +03001258 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 view = get_default_view(surface);
1260 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001261 index == shell->workspaces.current ||
1262 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001263 return;
1264
1265 from = get_current_workspace(shell);
1266 to = get_workspace(shell, index);
1267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 wl_list_remove(&view->layer_link);
1269 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270
Philip Withnall659163d2013-11-25 18:01:36 +00001271 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001272 if (shsurf != NULL)
1273 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001274
Jonas Ådahle9d22502012-08-29 22:13:01 +02001275 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001276 drop_focus_state(shell, from, surface);
1277
1278 if (shell->workspaces.anim_from == to &&
1279 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001280 wl_list_remove(&to->layer.link);
1281 wl_list_insert(from->layer.link.prev, &to->layer.link);
1282
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001283 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001284 broadcast_current_workspace_state(shell);
1285
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001286 return;
1287 }
1288
1289 if (shell->workspaces.anim_to != NULL)
1290 finish_workspace_change_animation(shell,
1291 shell->workspaces.anim_from,
1292 shell->workspaces.anim_to);
1293
1294 if (workspace_is_empty(from) &&
1295 workspace_has_only(to, surface))
1296 update_workspace(shell, index, from, to);
1297 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001298 if (shsurf != NULL &&
1299 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001300 wl_list_insert(&shell->workspaces.anim_sticky_list,
1301 &shsurf->workspace_transform.link);
1302
1303 animate_workspace_change(shell, index, from, to);
1304 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001305
1306 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001307
1308 state = ensure_focus_state(shell, seat);
1309 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001310 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001311}
1312
1313static void
1314workspace_manager_move_surface(struct wl_client *client,
1315 struct wl_resource *resource,
1316 struct wl_resource *surface_resource,
1317 uint32_t workspace)
1318{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001319 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001320 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001321 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001322 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001323 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001324
Pekka Paalanen01388e22013-04-25 13:57:44 +03001325 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001326 shell_surface = get_shell_surface(main_surface);
1327 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001329
1330 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001331}
1332
1333static const struct workspace_manager_interface workspace_manager_implementation = {
1334 workspace_manager_move_surface,
1335};
1336
1337static void
1338unbind_resource(struct wl_resource *resource)
1339{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001340 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001341}
1342
1343static void
1344bind_workspace_manager(struct wl_client *client,
1345 void *data, uint32_t version, uint32_t id)
1346{
1347 struct desktop_shell *shell = data;
1348 struct wl_resource *resource;
1349
Jason Ekstranda85118c2013-06-27 20:17:02 -05001350 resource = wl_resource_create(client,
1351 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001352
1353 if (resource == NULL) {
1354 weston_log("couldn't add workspace manager object");
1355 return;
1356 }
1357
Jason Ekstranda85118c2013-06-27 20:17:02 -05001358 wl_resource_set_implementation(resource,
1359 &workspace_manager_implementation,
1360 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001361 wl_list_insert(&shell->workspaces.client_list,
1362 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001363
1364 workspace_manager_send_state(resource,
1365 shell->workspaces.current,
1366 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001367}
1368
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001369static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001370touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1371 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1372{
1373}
1374
1375static void
1376touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1377{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001378 struct weston_touch_move_grab *move =
1379 (struct weston_touch_move_grab *) container_of(
1380 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001381
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001382 if (touch_id == 0)
1383 move->active = 0;
1384
Jonas Ådahl9484b692013-12-02 22:05:03 +01001385 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001386 shell_touch_grab_end(&move->base);
1387 free(move);
1388 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001389}
1390
1391static void
1392touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1393 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1394{
1395 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1396 struct shell_surface *shsurf = move->base.shsurf;
1397 struct weston_surface *es;
1398 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1399 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1400
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001401 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001402 return;
1403
1404 es = shsurf->surface;
1405
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001406 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001407
1408 weston_compositor_schedule_repaint(es->compositor);
1409}
1410
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001411static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001412touch_move_grab_frame(struct weston_touch_grab *grab)
1413{
1414}
1415
1416static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001417touch_move_grab_cancel(struct weston_touch_grab *grab)
1418{
1419 struct weston_touch_move_grab *move =
1420 (struct weston_touch_move_grab *) container_of(
1421 grab, struct shell_touch_grab, grab);
1422
1423 shell_touch_grab_end(&move->base);
1424 free(move);
1425}
1426
Rusty Lynch1084da52013-08-15 09:10:08 -07001427static const struct weston_touch_grab_interface touch_move_grab_interface = {
1428 touch_move_grab_down,
1429 touch_move_grab_up,
1430 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001431 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001432 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001433};
1434
1435static int
1436surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1437{
1438 struct weston_touch_move_grab *move;
1439
1440 if (!shsurf)
1441 return -1;
1442
Rafael Antognolli03b16592013-12-03 15:35:42 -02001443 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001444 return 0;
1445
1446 move = malloc(sizeof *move);
1447 if (!move)
1448 return -1;
1449
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001450 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001452 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001454 seat->touch->grab_y;
1455
1456 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1457 seat->touch);
1458
1459 return 0;
1460}
1461
1462static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001463noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001464{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001465}
1466
1467static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001468constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1469{
1470 struct shell_surface *shsurf = move->base.shsurf;
1471 struct weston_pointer *pointer = move->base.grab.pointer;
1472 int x, y, panel_height, bottom, left, right;
1473 const int safety = 50;
1474
1475 x = wl_fixed_to_int(pointer->x + move->dx);
1476 y = wl_fixed_to_int(pointer->y + move->dy);
1477
1478 panel_height = get_output_panel_height(shsurf->shell,
1479 shsurf->surface->output);
1480 bottom = y + shsurf->surface->height - shsurf->margin.bottom;
1481 if (bottom - panel_height < safety)
1482 y = panel_height + safety -
1483 shsurf->surface->height + shsurf->margin.bottom;
1484
1485 if (move->client_initiated &&
1486 y + shsurf->margin.top < panel_height)
1487 y = panel_height - shsurf->margin.top;
1488
1489 *cx = x;
1490 *cy = y;
1491}
1492
1493static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001494move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1495 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001496{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001497 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001498 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001499 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001500 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001501
1502 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001503 if (!shsurf)
1504 return;
1505
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001506 constrain_position(move, &cx, &cy);
1507
1508 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001509
Jason Ekstranda7af7042013-10-12 22:38:11 -05001510 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001511}
1512
1513static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001514move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001515 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001516{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001517 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1518 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001519 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001520 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001521
Daniel Stone4dbadb12012-05-30 16:31:51 +01001522 if (pointer->button_count == 0 &&
1523 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001524 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001525 free(grab);
1526 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001527}
1528
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001529static void
1530move_grab_cancel(struct weston_pointer_grab *grab)
1531{
1532 struct shell_grab *shell_grab =
1533 container_of(grab, struct shell_grab, grab);
1534
1535 shell_grab_end(shell_grab);
1536 free(grab);
1537}
1538
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001539static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001540 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001541 move_grab_motion,
1542 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001543 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001544};
1545
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001546static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001547surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1548 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001549{
1550 struct weston_move_grab *move;
1551
1552 if (!shsurf)
1553 return -1;
1554
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001555 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001556 return 0;
1557
1558 move = malloc(sizeof *move);
1559 if (!move)
1560 return -1;
1561
Jason Ekstranda7af7042013-10-12 22:38:11 -05001562 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001563 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001565 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001566 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001567
1568 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001569 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001570
1571 return 0;
1572}
1573
1574static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001575common_surface_move(struct wl_resource *resource,
1576 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001577{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001578 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001579 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001580 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581
Kristian Høgsbergd0b40ed2014-04-29 14:47:46 -07001582 if (shsurf->grabbed)
1583 return;
1584
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001585 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001586 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001587 seat->pointer->button_count > 0 &&
1588 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001590 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001591 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001592 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001593 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001594 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001595 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001597 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001598 (surface_touch_move(shsurf, seat) < 0))
1599 wl_resource_post_no_memory(resource);
1600 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001601}
1602
Rafael Antognollie2a34552013-12-03 15:35:45 -02001603static void
1604shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1605 struct wl_resource *seat_resource, uint32_t serial)
1606{
1607 common_surface_move(resource, seat_resource, serial);
1608}
1609
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001610struct weston_resize_grab {
1611 struct shell_grab base;
1612 uint32_t edges;
1613 int32_t width, height;
1614};
1615
1616static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001617resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1618 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001619{
1620 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001621 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001622 struct shell_surface *shsurf = resize->base.shsurf;
1623 int32_t width, height;
1624 wl_fixed_t from_x, from_y;
1625 wl_fixed_t to_x, to_y;
1626
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001627 weston_pointer_move(pointer, x, y);
1628
Ander Conselvan de Oliveira9c376b52014-04-29 17:54:03 +03001629 if (!shsurf || !shsurf->resource)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001630 return;
1631
Jason Ekstranda7af7042013-10-12 22:38:11 -05001632 weston_view_from_global_fixed(shsurf->view,
1633 pointer->grab_x, pointer->grab_y,
1634 &from_x, &from_y);
1635 weston_view_from_global_fixed(shsurf->view,
1636 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001637
1638 width = resize->width;
1639 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1640 width += wl_fixed_to_int(from_x - to_x);
1641 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1642 width += wl_fixed_to_int(to_x - from_x);
1643 }
1644
1645 height = resize->height;
1646 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1647 height += wl_fixed_to_int(from_y - to_y);
1648 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1649 height += wl_fixed_to_int(to_y - from_y);
1650 }
1651
1652 shsurf->client->send_configure(shsurf->surface,
1653 resize->edges, width, height);
1654}
1655
1656static void
1657send_configure(struct weston_surface *surface,
1658 uint32_t edges, int32_t width, int32_t height)
1659{
1660 struct shell_surface *shsurf = get_shell_surface(surface);
1661
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001662 assert(shsurf);
1663
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001664 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001665 edges, width, height);
1666}
1667
1668static const struct weston_shell_client shell_client = {
1669 send_configure
1670};
1671
1672static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001673resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001674 uint32_t time, uint32_t button, uint32_t state_w)
1675{
1676 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001677 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001678 enum wl_pointer_button_state state = state_w;
1679
1680 if (pointer->button_count == 0 &&
1681 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1682 shell_grab_end(&resize->base);
1683 free(grab);
1684 }
1685}
1686
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001687static void
1688resize_grab_cancel(struct weston_pointer_grab *grab)
1689{
1690 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1691
1692 shell_grab_end(&resize->base);
1693 free(grab);
1694}
1695
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001696static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001697 noop_grab_focus,
1698 resize_grab_motion,
1699 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001700 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701};
1702
Giulio Camuffob8366642013-04-25 13:57:46 +03001703/*
1704 * Returns the bounding box of a surface and all its sub-surfaces,
1705 * in the surface coordinates system. */
1706static void
1707surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1708 int32_t *y, int32_t *w, int32_t *h) {
1709 pixman_region32_t region;
1710 pixman_box32_t *box;
1711 struct weston_subsurface *subsurface;
1712
1713 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714 surface->width,
1715 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001716
1717 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1718 pixman_region32_union_rect(&region, &region,
1719 subsurface->position.x,
1720 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001721 subsurface->surface->width,
1722 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001723 }
1724
1725 box = pixman_region32_extents(&region);
1726 if (x)
1727 *x = box->x1;
1728 if (y)
1729 *y = box->y1;
1730 if (w)
1731 *w = box->x2 - box->x1;
1732 if (h)
1733 *h = box->y2 - box->y1;
1734
1735 pixman_region32_fini(&region);
1736}
1737
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001738static int
1739surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001740 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001741{
1742 struct weston_resize_grab *resize;
1743
Rafael Antognolli03b16592013-12-03 15:35:42 -02001744 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001745 return 0;
1746
1747 if (edges == 0 || edges > 15 ||
1748 (edges & 3) == 3 || (edges & 12) == 12)
1749 return 0;
1750
1751 resize = malloc(sizeof *resize);
1752 if (!resize)
1753 return -1;
1754
1755 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001756 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1757 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001758
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001759 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001760 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001761 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762
1763 return 0;
1764}
1765
1766static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001767common_surface_resize(struct wl_resource *resource,
1768 struct wl_resource *seat_resource, uint32_t serial,
1769 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001770{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001771 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001772 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001773 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774
Rafael Antognolli03b16592013-12-03 15:35:42 -02001775 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001776 return;
1777
Kristian Høgsberg7b83ae42014-04-29 14:50:25 -07001778 if (shsurf->grabbed)
1779 return;
1780
Kristian Høgsberge3148752013-05-06 23:19:49 -04001781 if (seat->pointer->button_count == 0 ||
1782 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001783 seat->pointer->focus == NULL)
1784 return;
1785
1786 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1787 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001788 return;
1789
Kristian Høgsberge3148752013-05-06 23:19:49 -04001790 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001791 wl_resource_post_no_memory(resource);
1792}
1793
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001794static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001795shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1796 struct wl_resource *seat_resource, uint32_t serial,
1797 uint32_t edges)
1798{
1799 common_surface_resize(resource, seat_resource, serial, edges);
1800}
1801
1802static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001803busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001804{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001805 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001806 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001807 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001808 wl_fixed_t sx, sy;
1809
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810 view = weston_compositor_pick_view(pointer->seat->compositor,
1811 pointer->x, pointer->y,
1812 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001813
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001814 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1815 shell_grab_end(grab);
1816 free(grab);
1817 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001818}
1819
1820static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001821busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1822 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001823{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001824 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001826
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001827static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001828busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001829 uint32_t time, uint32_t button, uint32_t state)
1830{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001831 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001832 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001833 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001834
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001835 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001836 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001837 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001838 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001839 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001840 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001841 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001842}
1843
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001844static void
1845busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1846{
1847 struct shell_grab *grab = (struct shell_grab *) base;
1848
1849 shell_grab_end(grab);
1850 free(grab);
1851}
1852
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001853static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001854 busy_cursor_grab_focus,
1855 busy_cursor_grab_motion,
1856 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001857 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001858};
1859
1860static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001861set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001862{
1863 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001864
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001865 if (pointer->grab->interface == &busy_cursor_grab_interface)
1866 return;
1867
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001868 grab = malloc(sizeof *grab);
1869 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001870 return;
1871
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001872 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1873 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001874 /* Mark the shsurf as ungrabbed so that button binding is able
1875 * to move it. */
1876 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001877}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001878
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001879static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001880end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001881{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001882 struct shell_grab *grab;
1883 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001884
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001885 wl_list_for_each(seat, &compositor->seat_list, link) {
1886 if (seat->pointer == NULL)
1887 continue;
1888
1889 grab = (struct shell_grab *) seat->pointer->grab;
1890 if (grab->grab.interface == &busy_cursor_grab_interface &&
1891 wl_resource_get_client(grab->shsurf->resource) == client) {
1892 shell_grab_end(grab);
1893 free(grab);
1894 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001895 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001896}
1897
Scott Moreau9521d5e2012-04-19 13:06:17 -06001898static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001899handle_shell_client_destroy(struct wl_listener *listener, void *data);
1900
1901static int
1902xdg_ping_timeout_handler(void *data)
1903{
1904 struct shell_client *sc = data;
1905 struct weston_seat *seat;
1906 struct shell_surface *shsurf;
1907
1908 /* Client is not responding */
1909 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001910 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1911 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1912 continue;
1913 if (seat->pointer->focus->surface->resource == NULL)
1914 continue;
1915
1916 shsurf = get_shell_surface(seat->pointer->focus->surface);
1917 if (shsurf &&
1918 wl_resource_get_client(shsurf->resource) == sc->client)
1919 set_busy_cursor(shsurf, seat->pointer);
1920 }
1921
1922 return 1;
1923}
1924
1925static void
1926handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1927{
1928 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05001929 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001930 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001931 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001932
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001933 if (sc->unresponsive) {
1934 xdg_ping_timeout_handler(sc);
1935 return;
1936 }
1937
1938 sc->ping_serial = serial;
1939 loop = wl_display_get_event_loop(compositor->wl_display);
1940 if (sc->ping_timer == NULL)
1941 sc->ping_timer =
1942 wl_event_loop_add_timer(loop,
1943 xdg_ping_timeout_handler, sc);
1944 if (sc->ping_timer == NULL)
1945 return;
1946
1947 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1948
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001949 if (shell_surface_is_xdg_surface(shsurf) ||
1950 shell_surface_is_xdg_popup(shsurf))
1951 xdg_shell_send_ping(sc->resource, serial);
1952 else if (shell_surface_is_wl_shell_surface(shsurf))
1953 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001954}
1955
Scott Moreauff1db4a2012-04-17 19:06:18 -06001956static void
1957ping_handler(struct weston_surface *surface, uint32_t serial)
1958{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001959 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001960
1961 if (!shsurf)
1962 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001963 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001964 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001965 if (shsurf->surface == shsurf->shell->grab_surface)
1966 return;
1967
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001968 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001969}
1970
1971static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001972handle_pointer_focus(struct wl_listener *listener, void *data)
1973{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001974 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001975 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001976 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001977 uint32_t serial;
1978
Jason Ekstranda7af7042013-10-12 22:38:11 -05001979 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001980 return;
1981
Jason Ekstranda7af7042013-10-12 22:38:11 -05001982 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001983 serial = wl_display_next_serial(compositor->wl_display);
1984 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001985}
1986
1987static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001988create_pointer_focus_listener(struct weston_seat *seat)
1989{
1990 struct wl_listener *listener;
1991
Kristian Høgsberge3148752013-05-06 23:19:49 -04001992 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001993 return;
1994
1995 listener = malloc(sizeof *listener);
1996 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001997 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001998}
1999
2000static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002001shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2002{
2003 if (--shsurf->focus_count == 0)
2004 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05002005 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002006}
2007
2008static void
2009shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2010{
2011 if (shsurf->focus_count++ == 0)
2012 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05002013 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002014}
2015
2016static void
2017handle_keyboard_focus(struct wl_listener *listener, void *data)
2018{
2019 struct weston_keyboard *keyboard = data;
2020 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2021
2022 if (seat->focused_surface) {
2023 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2024 if (shsurf)
2025 shell_surface_lose_keyboard_focus(shsurf);
2026 }
2027
2028 seat->focused_surface = keyboard->focus;
2029
2030 if (seat->focused_surface) {
2031 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2032 if (shsurf)
2033 shell_surface_gain_keyboard_focus(shsurf);
2034 }
2035}
2036
2037static void
2038create_keyboard_focus_listener(struct weston_seat *seat)
2039{
2040 struct wl_listener *listener;
2041
2042 if (!seat->keyboard)
2043 return;
2044
2045 listener = malloc(sizeof *listener);
2046 listener->notify = handle_keyboard_focus;
2047 wl_signal_add(&seat->keyboard->focus_signal, listener);
2048}
2049
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002050static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002051shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002052{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002053 if (sc->ping_serial != serial)
2054 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002055
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002056 sc->unresponsive = 0;
2057 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002058
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002059 if (sc->ping_timer) {
2060 wl_event_source_remove(sc->ping_timer);
2061 sc->ping_timer = NULL;
2062 }
2063
2064}
2065
2066static void
2067shell_surface_pong(struct wl_client *client,
2068 struct wl_resource *resource, uint32_t serial)
2069{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002070 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2071 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002072
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002073 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002074}
2075
2076static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002077set_title(struct shell_surface *shsurf, const char *title)
2078{
2079 free(shsurf->title);
2080 shsurf->title = strdup(title);
2081}
2082
2083static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002084shell_surface_set_title(struct wl_client *client,
2085 struct wl_resource *resource, const char *title)
2086{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002087 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002088
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002089 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002090}
2091
2092static void
2093shell_surface_set_class(struct wl_client *client,
2094 struct wl_resource *resource, const char *class)
2095{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002096 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002097
2098 free(shsurf->class);
2099 shsurf->class = strdup(class);
2100}
2101
Alex Wu4539b082012-03-01 12:57:46 +08002102static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002103restore_output_mode(struct weston_output *output)
2104{
Hardening57388e42013-09-18 23:56:36 +02002105 if (output->current_mode != output->original_mode ||
2106 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002107 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002108 output->original_mode,
2109 output->original_scale,
2110 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002111}
2112
2113static void
2114restore_all_output_modes(struct weston_compositor *compositor)
2115{
2116 struct weston_output *output;
2117
2118 wl_list_for_each(output, &compositor->output_list, link)
2119 restore_output_mode(output);
2120}
2121
Philip Withnallbecb77e2013-11-25 18:01:30 +00002122static int
2123get_output_panel_height(struct desktop_shell *shell,
2124 struct weston_output *output)
2125{
2126 struct weston_view *view;
2127 int panel_height = 0;
2128
2129 if (!output)
2130 return 0;
2131
2132 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2133 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002134 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002135 break;
2136 }
2137 }
2138
2139 return panel_height;
2140}
2141
Philip Withnall07926d92013-11-25 18:01:40 +00002142/* The surface will be inserted into the list immediately after the link
2143 * returned by this function (i.e. will be stacked immediately above the
2144 * returned link). */
2145static struct wl_list *
2146shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2147{
2148 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002149 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002150
2151 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002152 case SHELL_SURFACE_POPUP:
2153 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002154 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002155 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002156 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002157 /* Move the surface to its parent layer so
2158 * that surfaces which are transient for
2159 * fullscreen surfaces don't get hidden by the
2160 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002161
2162 /* TODO: Handle a parent with multiple views */
2163 parent = get_default_view(shsurf->parent);
2164 if (parent)
2165 return parent->layer_link.prev;
2166 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002167 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002168
2169 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002170 return &shsurf->shell->fullscreen_layer.view_list;
2171
Philip Withnall07926d92013-11-25 18:01:40 +00002172 case SHELL_SURFACE_NONE:
2173 default:
2174 /* Go to the fallback, below. */
2175 break;
2176 }
2177
2178 /* Move the surface to a normal workspace layer so that surfaces
2179 * which were previously fullscreen or transient are no longer
2180 * rendered on top. */
2181 ws = get_current_workspace(shsurf->shell);
2182 return &ws->layer.view_list;
2183}
2184
Philip Withnall648a4dd2013-11-25 18:01:44 +00002185static void
2186shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2187{
2188 struct shell_surface *child;
2189
2190 /* Move the child layers to the same workspace as shsurf. They will be
2191 * stacked above shsurf. */
2192 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2193 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002194 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002195 weston_view_geometry_dirty(child->view);
2196 wl_list_remove(&child->view->layer_link);
2197 wl_list_insert(shsurf->view->layer_link.prev,
2198 &child->view->layer_link);
2199 weston_view_geometry_dirty(child->view);
2200 weston_surface_damage(child->surface);
2201
2202 /* Recurse. We don’t expect this to recurse very far (if
2203 * at all) because that would imply we have transient
2204 * (or popup) children of transient surfaces, which
2205 * would be unusual. */
2206 shell_surface_update_child_surface_layers(child);
2207 }
2208 }
2209}
2210
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002211/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002212 * geometry to ensure the changes are redrawn.
2213 *
2214 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2215 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002216static void
2217shell_surface_update_layer(struct shell_surface *shsurf)
2218{
2219 struct wl_list *new_layer_link;
2220
2221 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2222
2223 if (new_layer_link == &shsurf->view->layer_link)
2224 return;
2225
2226 weston_view_geometry_dirty(shsurf->view);
2227 wl_list_remove(&shsurf->view->layer_link);
2228 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2229 weston_view_geometry_dirty(shsurf->view);
2230 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002231
2232 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002233}
2234
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002235static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002236shell_surface_set_parent(struct shell_surface *shsurf,
2237 struct weston_surface *parent)
2238{
2239 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002240
2241 wl_list_remove(&shsurf->children_link);
2242 wl_list_init(&shsurf->children_link);
2243
2244 /* Insert into the parent surface’s child list. */
2245 if (parent != NULL) {
2246 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2247 if (parent_shsurf != NULL)
2248 wl_list_insert(&parent_shsurf->children_list,
2249 &shsurf->children_link);
2250 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002251}
2252
2253static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002254shell_surface_set_output(struct shell_surface *shsurf,
2255 struct weston_output *output)
2256{
2257 struct weston_surface *es = shsurf->surface;
2258
2259 /* get the default output, if the client set it as NULL
2260 check whether the ouput is available */
2261 if (output)
2262 shsurf->output = output;
2263 else if (es->output)
2264 shsurf->output = es->output;
2265 else
2266 shsurf->output = get_default_output(es->compositor);
2267}
2268
2269static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002270surface_clear_next_states(struct shell_surface *shsurf)
2271{
2272 shsurf->next_state.maximized = false;
2273 shsurf->next_state.fullscreen = false;
2274
2275 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2276 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2277 shsurf->state_changed = true;
2278}
2279
2280static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002281set_toplevel(struct shell_surface *shsurf)
2282{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002283 shell_surface_set_parent(shsurf, NULL);
2284 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002285 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002286
2287 /* The layer_link is updated in set_surface_type(),
2288 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002289}
2290
2291static void
2292shell_surface_set_toplevel(struct wl_client *client,
2293 struct wl_resource *resource)
2294{
2295 struct shell_surface *surface = wl_resource_get_user_data(resource);
2296
2297 set_toplevel(surface);
2298}
2299
2300static void
2301set_transient(struct shell_surface *shsurf,
2302 struct weston_surface *parent, int x, int y, uint32_t flags)
2303{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002304 assert(parent != NULL);
2305
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002306 shell_surface_set_parent(shsurf, parent);
2307
2308 surface_clear_next_states(shsurf);
2309
Philip Withnallbecb77e2013-11-25 18:01:30 +00002310 shsurf->transient.x = x;
2311 shsurf->transient.y = y;
2312 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002313
Rafael Antognollied207b42013-12-03 15:35:43 -02002314 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002315 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002316 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002317
2318 /* The layer_link is updated in set_surface_type(),
2319 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002320}
2321
2322static void
2323shell_surface_set_transient(struct wl_client *client,
2324 struct wl_resource *resource,
2325 struct wl_resource *parent_resource,
2326 int x, int y, uint32_t flags)
2327{
2328 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2329 struct weston_surface *parent =
2330 wl_resource_get_user_data(parent_resource);
2331
2332 set_transient(shsurf, parent, x, y, flags);
2333}
2334
2335static void
2336set_fullscreen(struct shell_surface *shsurf,
2337 uint32_t method,
2338 uint32_t framerate,
2339 struct weston_output *output)
2340{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002341 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002342
2343 shsurf->fullscreen_output = shsurf->output;
2344 shsurf->fullscreen.type = method;
2345 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002346
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002347 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002348
2349 shsurf->client->send_configure(shsurf->surface, 0,
2350 shsurf->output->width,
2351 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002352
2353 /* The layer_link is updated in set_surface_type(),
2354 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002355}
2356
2357static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002358weston_view_set_initial_position(struct weston_view *view,
2359 struct desktop_shell *shell);
2360
2361static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002362unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002363{
Philip Withnallf85fe842013-11-25 18:01:37 +00002364 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002365 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2366 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002367 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002368 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002369
Alex Wu4539b082012-03-01 12:57:46 +08002370 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2371 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002372
Alex Wu4539b082012-03-01 12:57:46 +08002373 wl_list_remove(&shsurf->fullscreen.transform.link);
2374 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002375
Jason Ekstranda7af7042013-10-12 22:38:11 -05002376 if (shsurf->fullscreen.black_view)
2377 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2378 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002379
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002380 if (shsurf->saved_position_valid)
2381 weston_view_set_position(shsurf->view,
2382 shsurf->saved_x, shsurf->saved_y);
2383 else
2384 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2385
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002386 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002387 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002388 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002389 shsurf->saved_rotation_valid = false;
2390 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002391
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002392 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002393}
2394
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002395static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002396shell_surface_set_fullscreen(struct wl_client *client,
2397 struct wl_resource *resource,
2398 uint32_t method,
2399 uint32_t framerate,
2400 struct wl_resource *output_resource)
2401{
2402 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2403 struct weston_output *output;
2404
2405 if (output_resource)
2406 output = wl_resource_get_user_data(output_resource);
2407 else
2408 output = NULL;
2409
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002410 shell_surface_set_parent(shsurf, NULL);
2411
Rafael Antognolli03b16592013-12-03 15:35:42 -02002412 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002413 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002414
2415 shsurf->next_state.fullscreen = true;
2416 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002417}
2418
2419static void
2420set_popup(struct shell_surface *shsurf,
2421 struct weston_surface *parent,
2422 struct weston_seat *seat,
2423 uint32_t serial,
2424 int32_t x,
2425 int32_t y)
2426{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002427 assert(parent != NULL);
2428
Philip Withnallbecb77e2013-11-25 18:01:30 +00002429 shsurf->popup.shseat = get_shell_seat(seat);
2430 shsurf->popup.serial = serial;
2431 shsurf->popup.x = x;
2432 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002433
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002434 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002435}
2436
2437static void
2438shell_surface_set_popup(struct wl_client *client,
2439 struct wl_resource *resource,
2440 struct wl_resource *seat_resource,
2441 uint32_t serial,
2442 struct wl_resource *parent_resource,
2443 int32_t x, int32_t y, uint32_t flags)
2444{
2445 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002446 struct weston_surface *parent =
2447 wl_resource_get_user_data(parent_resource);
2448
2449 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002450
Rafael Antognolli03b16592013-12-03 15:35:42 -02002451 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002452 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002453 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002454 wl_resource_get_user_data(seat_resource),
2455 serial, x, y);
2456}
2457
2458static void
2459set_maximized(struct shell_surface *shsurf,
2460 struct weston_output *output)
2461{
2462 struct desktop_shell *shell;
2463 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002464
Philip Withnall352e7ed2013-11-25 18:01:35 +00002465 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002466
2467 shell = shell_surface_get_shell(shsurf);
2468 panel_height = get_output_panel_height(shell, shsurf->output);
2469 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2470
2471 shsurf->client->send_configure(shsurf->surface, edges,
2472 shsurf->output->width,
2473 shsurf->output->height - panel_height);
2474
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002475 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002476}
2477
2478static void
2479unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002480{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002481 /* undo all maximized things here */
2482 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002483
2484 if (shsurf->saved_position_valid)
2485 weston_view_set_position(shsurf->view,
2486 shsurf->saved_x, shsurf->saved_y);
2487 else
2488 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002489
2490 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002491 wl_list_insert(&shsurf->view->geometry.transformation_list,
2492 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002493 shsurf->saved_rotation_valid = false;
2494 }
2495
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002496 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002497}
2498
Philip Withnallbecb77e2013-11-25 18:01:30 +00002499static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002500set_minimized(struct weston_surface *surface, uint32_t is_true)
2501{
2502 struct shell_surface *shsurf;
2503 struct workspace *current_ws;
2504 struct weston_seat *seat;
2505 struct weston_surface *focus;
2506 struct weston_view *view;
2507
2508 view = get_default_view(surface);
2509 if (!view)
2510 return;
2511
2512 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2513
2514 shsurf = get_shell_surface(surface);
2515 current_ws = get_current_workspace(shsurf->shell);
2516
2517 wl_list_remove(&view->layer_link);
2518 /* hide or show, depending on the state */
2519 if (is_true) {
2520 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2521
2522 drop_focus_state(shsurf->shell, current_ws, view->surface);
2523 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2524 if (!seat->keyboard)
2525 continue;
2526 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2527 if (focus == view->surface)
2528 weston_keyboard_set_focus(seat->keyboard, NULL);
2529 }
2530 }
2531 else {
2532 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2533
2534 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2535 if (!seat->keyboard)
2536 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002537 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002538 }
2539 }
2540
2541 shell_surface_update_child_surface_layers(shsurf);
2542
2543 weston_view_damage_below(view);
2544}
2545
2546static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002547shell_surface_set_maximized(struct wl_client *client,
2548 struct wl_resource *resource,
2549 struct wl_resource *output_resource)
2550{
2551 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2552 struct weston_output *output;
2553
2554 if (output_resource)
2555 output = wl_resource_get_user_data(output_resource);
2556 else
2557 output = NULL;
2558
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002559 shell_surface_set_parent(shsurf, NULL);
2560
Rafael Antognolli03b16592013-12-03 15:35:42 -02002561 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002562 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002563
2564 shsurf->next_state.maximized = true;
2565 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002566}
2567
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002568/* This is only ever called from set_surface_type(), so there’s no need to
2569 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002570static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002571reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002572{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002573 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002574 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002575 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002576 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002577
Pekka Paalanen98262232011-12-01 10:42:22 +02002578 return 0;
2579}
2580
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002581static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002582set_full_output(struct shell_surface *shsurf)
2583{
2584 shsurf->saved_x = shsurf->view->geometry.x;
2585 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002586 shsurf->saved_width = shsurf->surface->width;
2587 shsurf->saved_height = shsurf->surface->height;
2588 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002589 shsurf->saved_position_valid = true;
2590
2591 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2592 wl_list_remove(&shsurf->rotation.transform.link);
2593 wl_list_init(&shsurf->rotation.transform.link);
2594 weston_view_geometry_dirty(shsurf->view);
2595 shsurf->saved_rotation_valid = true;
2596 }
2597}
2598
2599static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002600set_surface_type(struct shell_surface *shsurf)
2601{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002602 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002603 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002604
Philip Withnallbecb77e2013-11-25 18:01:30 +00002605 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002606
Rafael Antognolli03b16592013-12-03 15:35:42 -02002607 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002608 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002609
2610 switch (shsurf->type) {
2611 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002612 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002613 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002614 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002615 weston_view_set_position(shsurf->view,
2616 pev->geometry.x + shsurf->transient.x,
2617 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002618 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002619 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002620
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002621 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002622 weston_view_set_position(shsurf->view, shsurf->transient.x,
2623 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002624 break;
2625
Philip Withnall0f640e22013-11-25 18:01:31 +00002626 case SHELL_SURFACE_POPUP:
2627 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002628 default:
2629 break;
2630 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002631
2632 /* Update the surface’s layer. */
2633 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002634}
2635
Tiago Vignattibe143262012-04-16 17:31:41 +03002636static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002637shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002638{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002639 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002640}
2641
Alex Wu21858432012-04-01 20:13:08 +08002642static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002643black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002644
Jason Ekstranda7af7042013-10-12 22:38:11 -05002645static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002646create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002647 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002648 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002649{
2650 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002651 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002652
2653 surface = weston_surface_create(ec);
2654 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002655 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002656 return NULL;
2657 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002658 view = weston_view_create(surface);
2659 if (surface == NULL) {
2660 weston_log("no memory\n");
2661 weston_surface_destroy(surface);
2662 return NULL;
2663 }
Alex Wu4539b082012-03-01 12:57:46 +08002664
Alex Wu21858432012-04-01 20:13:08 +08002665 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002666 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002667 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002668 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002669 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002670 pixman_region32_fini(&surface->input);
2671 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002672
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002673 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002674 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002675
2676 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002677}
2678
Philip Withnalled8f1a92013-11-25 18:01:43 +00002679static void
2680shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2681{
2682 struct weston_output *output = shsurf->fullscreen_output;
2683
Rafael Antognolli03b16592013-12-03 15:35:42 -02002684 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002685
2686 if (!shsurf->fullscreen.black_view)
2687 shsurf->fullscreen.black_view =
2688 create_black_surface(shsurf->surface->compositor,
2689 shsurf->surface,
2690 output->x, output->y,
2691 output->width,
2692 output->height);
2693
2694 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2695 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2696 wl_list_insert(&shsurf->view->layer_link,
2697 &shsurf->fullscreen.black_view->layer_link);
2698 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2699 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002700
2701 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002702}
2703
Alex Wu4539b082012-03-01 12:57:46 +08002704/* Create black surface and append it to the associated fullscreen surface.
2705 * Handle size dismatch and positioning according to the method. */
2706static void
2707shell_configure_fullscreen(struct shell_surface *shsurf)
2708{
2709 struct weston_output *output = shsurf->fullscreen_output;
2710 struct weston_surface *surface = shsurf->surface;
2711 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002712 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002713 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002714
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002715 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2716 restore_output_mode(output);
2717
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002718 /* Reverse the effect of lower_fullscreen_layer() */
2719 wl_list_remove(&shsurf->view->layer_link);
2720 wl_list_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2721
Philip Withnalled8f1a92013-11-25 18:01:43 +00002722 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002723
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002725 &surf_width, &surf_height);
2726
Alex Wu4539b082012-03-01 12:57:46 +08002727 switch (shsurf->fullscreen.type) {
2728 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002729 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002730 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002731 break;
2732 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002733 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002734 if (output->width == surf_width &&
2735 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002736 weston_view_set_position(shsurf->view,
2737 output->x - surf_x,
2738 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002739 break;
2740 }
2741
Alex Wu4539b082012-03-01 12:57:46 +08002742 matrix = &shsurf->fullscreen.transform.matrix;
2743 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002744
Scott Moreau1bad5db2012-08-18 01:04:05 -06002745 output_aspect = (float) output->width /
2746 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002747 /* XXX: Use surf_width and surf_height here? */
2748 surface_aspect = (float) surface->width /
2749 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002750 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002751 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002752 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002753 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002754 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002755 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002756
Alex Wu4539b082012-03-01 12:57:46 +08002757 weston_matrix_scale(matrix, scale, scale, 1);
2758 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002759 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002760 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002761 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2762 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002764
Alex Wu4539b082012-03-01 12:57:46 +08002765 break;
2766 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002767 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002768 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002769 surf_width * surface->buffer_viewport.buffer.scale,
2770 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002771 shsurf->fullscreen.framerate};
2772
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002773 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002774 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002775 weston_view_set_position(shsurf->view,
2776 output->x - surf_x,
2777 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002778 shsurf->fullscreen.black_view->surface->width = output->width;
2779 shsurf->fullscreen.black_view->surface->height = output->height;
2780 weston_view_set_position(shsurf->fullscreen.black_view,
2781 output->x - surf_x,
2782 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002783 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002784 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002785 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002786 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002787 }
Alex Wubd3354b2012-04-17 17:20:49 +08002788 }
Alex Wu4539b082012-03-01 12:57:46 +08002789 break;
2790 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002792 break;
2793 default:
2794 break;
2795 }
2796}
2797
Alex Wu4539b082012-03-01 12:57:46 +08002798static void
2799shell_map_fullscreen(struct shell_surface *shsurf)
2800{
Alex Wubd3354b2012-04-17 17:20:49 +08002801 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002802}
2803
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002804static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002805set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2806{
2807 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002808 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002809 shsurf->transient.x = x;
2810 shsurf->transient.y = y;
2811 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002812
2813 shell_surface_set_parent(shsurf, NULL);
2814
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002815 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002816 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002817}
2818
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002819static int
2820shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2821{
2822 return surface_move(shsurf, ws, 1);
2823}
2824
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002825static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002826
2827static void
2828destroy_shell_seat(struct wl_listener *listener, void *data)
2829{
2830 struct shell_seat *shseat =
2831 container_of(listener,
2832 struct shell_seat, seat_destroy_listener);
2833 struct shell_surface *shsurf, *prev = NULL;
2834
2835 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002836 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002837 shseat->popup_grab.client = NULL;
2838
2839 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2840 shsurf->popup.shseat = NULL;
2841 if (prev) {
2842 wl_list_init(&prev->popup.grab_link);
2843 }
2844 prev = shsurf;
2845 }
2846 wl_list_init(&prev->popup.grab_link);
2847 }
2848
2849 wl_list_remove(&shseat->seat_destroy_listener.link);
2850 free(shseat);
2851}
2852
2853static struct shell_seat *
2854create_shell_seat(struct weston_seat *seat)
2855{
2856 struct shell_seat *shseat;
2857
2858 shseat = calloc(1, sizeof *shseat);
2859 if (!shseat) {
2860 weston_log("no memory to allocate shell seat\n");
2861 return NULL;
2862 }
2863
2864 shseat->seat = seat;
2865 wl_list_init(&shseat->popup_grab.surfaces_list);
2866
2867 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2868 wl_signal_add(&seat->destroy_signal,
2869 &shseat->seat_destroy_listener);
2870
2871 return shseat;
2872}
2873
2874static struct shell_seat *
2875get_shell_seat(struct weston_seat *seat)
2876{
2877 struct wl_listener *listener;
2878
2879 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2880 if (listener == NULL)
2881 return create_shell_seat(seat);
2882
2883 return container_of(listener,
2884 struct shell_seat, seat_destroy_listener);
2885}
2886
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002887static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002888popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002889{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002890 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002891 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002892 struct shell_seat *shseat =
2893 container_of(grab, struct shell_seat, popup_grab.grab);
2894 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002895 wl_fixed_t sx, sy;
2896
Jason Ekstranda7af7042013-10-12 22:38:11 -05002897 view = weston_compositor_pick_view(pointer->seat->compositor,
2898 pointer->x, pointer->y,
2899 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002900
Jason Ekstranda7af7042013-10-12 22:38:11 -05002901 if (view && view->surface->resource &&
2902 wl_resource_get_client(view->surface->resource) == client) {
2903 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002904 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002905 weston_pointer_set_focus(pointer, NULL,
2906 wl_fixed_from_int(0),
2907 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002908 }
2909}
2910
2911static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002912popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2913 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002914{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002915 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002916 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002917 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002918
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002919 weston_pointer_move(pointer, x, y);
2920
Neil Roberts96d790e2013-09-19 17:32:00 +01002921 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002922 weston_view_from_global_fixed(pointer->focus,
2923 pointer->x, pointer->y,
2924 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002925 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002926 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002927}
2928
2929static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002930popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002931 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002932{
2933 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002934 struct shell_seat *shseat =
2935 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002936 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002937 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002938 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002939 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002940
Neil Roberts96d790e2013-09-19 17:32:00 +01002941 resource_list = &grab->pointer->focus_resource_list;
2942 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002943 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002944 wl_resource_for_each(resource, resource_list) {
2945 wl_pointer_send_button(resource, serial,
2946 time, button, state);
2947 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002948 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002949 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002950 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002951 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002952 }
2953
Daniel Stone4dbadb12012-05-30 16:31:51 +01002954 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002955 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002956}
2957
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002958static void
2959popup_grab_cancel(struct weston_pointer_grab *grab)
2960{
2961 popup_grab_end(grab->pointer);
2962}
2963
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002964static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002965 popup_grab_focus,
2966 popup_grab_motion,
2967 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002968 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002969};
2970
2971static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002972shell_surface_send_popup_done(struct shell_surface *shsurf)
2973{
2974 if (shell_surface_is_wl_shell_surface(shsurf))
2975 wl_shell_surface_send_popup_done(shsurf->resource);
2976 else if (shell_surface_is_xdg_popup(shsurf))
2977 xdg_popup_send_popup_done(shsurf->resource,
2978 shsurf->popup.serial);
2979}
2980
2981static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002982popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002983{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002984 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002985 struct shell_seat *shseat =
2986 container_of(grab, struct shell_seat, popup_grab.grab);
2987 struct shell_surface *shsurf;
2988 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002989
2990 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002991 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002992 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002993 shseat->popup_grab.grab.interface = NULL;
2994 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002995 /* Send the popup_done event to all the popups open */
2996 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002997 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002998 shsurf->popup.shseat = NULL;
2999 if (prev) {
3000 wl_list_init(&prev->popup.grab_link);
3001 }
3002 prev = shsurf;
3003 }
3004 wl_list_init(&prev->popup.grab_link);
3005 wl_list_init(&shseat->popup_grab.surfaces_list);
3006 }
3007}
3008
3009static void
3010add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3011{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003012 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003013
3014 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003015 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003016 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003017 /* We must make sure here that this popup was opened after
3018 * a mouse press, and not just by moving around with other
3019 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003020 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003021 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003022
Rob Bradforddfe31052013-06-26 19:49:11 +01003023 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003024 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003025 } else {
3026 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003027 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003028}
3029
3030static void
3031remove_popup_grab(struct shell_surface *shsurf)
3032{
3033 struct shell_seat *shseat = shsurf->popup.shseat;
3034
3035 wl_list_remove(&shsurf->popup.grab_link);
3036 wl_list_init(&shsurf->popup.grab_link);
3037 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003038 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003039 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003040 }
3041}
3042
3043static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003044shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003045{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003046 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003047 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003048
Jason Ekstranda7af7042013-10-12 22:38:11 -05003049 shsurf->surface->output = parent_view->output;
3050 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003051
Jason Ekstranda7af7042013-10-12 22:38:11 -05003052 weston_view_set_transform_parent(shsurf->view, parent_view);
3053 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3054 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003055
Kristian Høgsberge3148752013-05-06 23:19:49 -04003056 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003057 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003058 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003059 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003060 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003061 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003062}
3063
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003064static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003065 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003066 shell_surface_move,
3067 shell_surface_resize,
3068 shell_surface_set_toplevel,
3069 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003070 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003071 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003072 shell_surface_set_maximized,
3073 shell_surface_set_title,
3074 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003075};
3076
3077static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003078destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003079{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003080 struct shell_surface *child, *next;
3081
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003082 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3083
Giulio Camuffo5085a752013-03-25 21:42:45 +01003084 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3085 remove_popup_grab(shsurf);
3086 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003087
Alex Wubd3354b2012-04-17 17:20:49 +08003088 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003089 shell_surface_is_top_fullscreen(shsurf))
3090 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003091
Jason Ekstranda7af7042013-10-12 22:38:11 -05003092 if (shsurf->fullscreen.black_view)
3093 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003094
Alex Wubd3354b2012-04-17 17:20:49 +08003095 /* As destroy_resource() use wl_list_for_each_safe(),
3096 * we can always remove the listener.
3097 */
3098 wl_list_remove(&shsurf->surface_destroy_listener.link);
3099 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003100 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003101
Jason Ekstranda7af7042013-10-12 22:38:11 -05003102 weston_view_destroy(shsurf->view);
3103
Philip Withnall648a4dd2013-11-25 18:01:44 +00003104 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003105 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3106 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003107
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003108 wl_list_remove(&shsurf->link);
3109 free(shsurf);
3110}
3111
3112static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003113shell_destroy_shell_surface(struct wl_resource *resource)
3114{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003115 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003116
Bryan Caina46b9462014-04-04 17:41:24 -05003117 if (!wl_list_empty(&shsurf->popup.grab_link))
3118 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003119 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003120}
3121
3122static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003123shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003124{
3125 struct shell_surface *shsurf = container_of(listener,
3126 struct shell_surface,
3127 surface_destroy_listener);
3128
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003129 if (shsurf->resource)
3130 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003131
3132 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003133}
3134
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003135static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003136fade_out_done(struct weston_view_animation *animation, void *data)
3137{
3138 struct shell_surface *shsurf = data;
3139
3140 weston_surface_destroy(shsurf->surface);
3141}
3142
3143static void
3144handle_resource_destroy(struct wl_listener *listener, void *data)
3145{
3146 struct shell_surface *shsurf =
3147 container_of(listener, struct shell_surface,
3148 resource_destroy_listener);
3149
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003150 if (!weston_surface_is_mapped(shsurf->surface))
3151 return;
3152
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003153 shsurf->surface->ref_count++;
3154
3155 pixman_region32_fini(&shsurf->surface->pending.input);
3156 pixman_region32_init(&shsurf->surface->pending.input);
3157 pixman_region32_fini(&shsurf->surface->input);
3158 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003159 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3160 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003161}
3162
3163static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003164shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003165
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003166struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003167get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003168{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003169 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003170 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003171 else
3172 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003173}
3174
Rafael Antognollie2a34552013-12-03 15:35:45 -02003175static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003176create_common_surface(struct shell_client *owner, void *shell,
3177 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003178 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003179{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003180 struct shell_surface *shsurf;
3181
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003182 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003183 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003184 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003185 }
3186
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003187 shsurf = calloc(1, sizeof *shsurf);
3188 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003189 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003190 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003191 }
3192
Jason Ekstranda7af7042013-10-12 22:38:11 -05003193 shsurf->view = weston_view_create(surface);
3194 if (!shsurf->view) {
3195 weston_log("no memory to allocate shell surface\n");
3196 free(shsurf);
3197 return NULL;
3198 }
3199
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003200 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003201 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003202
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003203 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3204 wl_resource_add_destroy_listener(surface->resource,
3205 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003206 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003207
Tiago Vignattibc052c92012-04-19 16:18:18 +03003208 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003209 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003210 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003211 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003212 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003213 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003214 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3215 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003216 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003217 wl_list_init(&shsurf->fullscreen.transform.link);
3218
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003219 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003220 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003221 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003222 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003223
3224 /* init link so its safe to always remove it in destroy_shell_surface */
3225 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003226 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003227
Pekka Paalanen460099f2012-01-20 16:48:25 +02003228 /* empty when not in use */
3229 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003230 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003231
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003232 wl_list_init(&shsurf->workspace_transform.link);
3233
Philip Withnall648a4dd2013-11-25 18:01:44 +00003234 wl_list_init(&shsurf->children_link);
3235 wl_list_init(&shsurf->children_list);
3236 shsurf->parent = NULL;
3237
Pekka Paalanen98262232011-12-01 10:42:22 +02003238 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003239
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003240 shsurf->client = client;
3241
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003242 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003243}
3244
Rafael Antognollie2a34552013-12-03 15:35:45 -02003245static struct shell_surface *
3246create_shell_surface(void *shell, struct weston_surface *surface,
3247 const struct weston_shell_client *client)
3248{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003249 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003250}
3251
Jason Ekstranda7af7042013-10-12 22:38:11 -05003252static struct weston_view *
3253get_primary_view(void *shell, struct shell_surface *shsurf)
3254{
3255 return shsurf->view;
3256}
3257
Tiago Vignattibc052c92012-04-19 16:18:18 +03003258static void
3259shell_get_shell_surface(struct wl_client *client,
3260 struct wl_resource *resource,
3261 uint32_t id,
3262 struct wl_resource *surface_resource)
3263{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003264 struct weston_surface *surface =
3265 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003266 struct shell_client *sc = wl_resource_get_user_data(resource);
3267 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003268 struct shell_surface *shsurf;
3269
3270 if (get_shell_surface(surface)) {
3271 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003272 WL_DISPLAY_ERROR_INVALID_OBJECT,
3273 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003274 return;
3275 }
3276
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003277 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003278 if (!shsurf) {
3279 wl_resource_post_error(surface_resource,
3280 WL_DISPLAY_ERROR_INVALID_OBJECT,
3281 "surface->configure already set");
3282 return;
3283 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003284
Jason Ekstranda85118c2013-06-27 20:17:02 -05003285 shsurf->resource =
3286 wl_resource_create(client,
3287 &wl_shell_surface_interface, 1, id);
3288 wl_resource_set_implementation(shsurf->resource,
3289 &shell_surface_implementation,
3290 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003291}
3292
Rafael Antognollie2a34552013-12-03 15:35:45 -02003293static bool
3294shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3295{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003296 /* A shell surface without a resource is created from xwayland
3297 * and is considered a wl_shell surface for now. */
3298
3299 return shsurf->resource == NULL ||
3300 wl_resource_instance_of(shsurf->resource,
3301 &wl_shell_surface_interface,
3302 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003303}
3304
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003305static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003306 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003307};
3308
Rafael Antognollie2a34552013-12-03 15:35:45 -02003309/****************************
3310 * xdg-shell implementation */
3311
3312static void
3313xdg_surface_destroy(struct wl_client *client,
3314 struct wl_resource *resource)
3315{
3316 wl_resource_destroy(resource);
3317}
3318
3319static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003320xdg_surface_set_transient_for(struct wl_client *client,
3321 struct wl_resource *resource,
3322 struct wl_resource *parent_resource)
3323{
3324 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3325 struct weston_surface *parent;
3326
3327 if (parent_resource)
3328 parent = wl_resource_get_user_data(parent_resource);
3329 else
3330 parent = NULL;
3331
3332 shell_surface_set_parent(shsurf, parent);
3333}
3334
3335static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003336xdg_surface_set_margin(struct wl_client *client,
3337 struct wl_resource *resource,
3338 int32_t left,
3339 int32_t right,
3340 int32_t top,
3341 int32_t bottom)
3342{
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003343 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3344
3345 shsurf->margin.left = left;
3346 shsurf->margin.right = right;
3347 shsurf->margin.top = top;
3348 shsurf->margin.bottom = bottom;
Jasper St. Pierre74073452014-02-01 18:36:41 -05003349}
3350
3351static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003352xdg_surface_set_app_id(struct wl_client *client,
3353 struct wl_resource *resource,
3354 const char *app_id)
3355{
3356 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3357
3358 free(shsurf->class);
3359 shsurf->class = strdup(app_id);
3360}
3361
3362static void
3363xdg_surface_set_title(struct wl_client *client,
3364 struct wl_resource *resource, const char *title)
3365{
3366 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3367
3368 set_title(shsurf, title);
3369}
3370
3371static void
3372xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3373 struct wl_resource *seat_resource, uint32_t serial)
3374{
3375 common_surface_move(resource, seat_resource, serial);
3376}
3377
3378static void
3379xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3380 struct wl_resource *seat_resource, uint32_t serial,
3381 uint32_t edges)
3382{
3383 common_surface_resize(resource, seat_resource, serial, edges);
3384}
3385
3386static void
3387xdg_surface_set_output(struct wl_client *client,
3388 struct wl_resource *resource,
3389 struct wl_resource *output_resource)
3390{
3391 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3392 struct weston_output *output;
3393
3394 if (output_resource)
3395 output = wl_resource_get_user_data(output_resource);
3396 else
3397 output = NULL;
3398
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003399 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003400}
3401
3402static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003403xdg_surface_change_state(struct shell_surface *shsurf,
3404 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003405{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003406 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003407 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003408
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003409 switch (state) {
3410 case XDG_SURFACE_STATE_MAXIMIZED:
3411 shsurf->requested_state.maximized = value;
3412 if (value)
3413 set_maximized(shsurf, NULL);
3414 break;
3415 case XDG_SURFACE_STATE_FULLSCREEN:
3416 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003417
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003418 if (value)
3419 set_fullscreen(shsurf,
3420 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3421 0, shsurf->recommended_output);
3422 break;
3423 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003424}
3425
3426static void
3427xdg_surface_request_change_state(struct wl_client *client,
3428 struct wl_resource *resource,
3429 uint32_t state,
3430 uint32_t value,
3431 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003432{
3433 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003434
3435 /* The client can't know what the current state is, so we need
3436 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003437
3438 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3439 return;
3440
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003441 switch (state) {
3442 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003443 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003444 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003445 default:
3446 /* send error? ignore? send change state with value 0? */
3447 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003448 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003449
3450 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003451}
3452
3453static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003454xdg_surface_ack_change_state(struct wl_client *client,
3455 struct wl_resource *resource,
3456 uint32_t state,
3457 uint32_t value,
3458 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003459{
3460 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3461
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003462 if (shsurf->state_requested) {
3463 shsurf->next_state = shsurf->requested_state;
3464 shsurf->state_changed = true;
3465 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003466 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003467}
3468
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003469static void
3470xdg_surface_set_minimized(struct wl_client *client,
3471 struct wl_resource *resource)
3472{
3473 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3474
3475 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3476 return;
3477
3478 /* apply compositor's own minimization logic (hide) */
3479 set_minimized(shsurf->surface, 1);
3480}
3481
Rafael Antognollie2a34552013-12-03 15:35:45 -02003482static const struct xdg_surface_interface xdg_surface_implementation = {
3483 xdg_surface_destroy,
3484 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003485 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003486 xdg_surface_set_title,
3487 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003488 xdg_surface_move,
3489 xdg_surface_resize,
3490 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003491 xdg_surface_request_change_state,
3492 xdg_surface_ack_change_state,
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003493 xdg_surface_set_minimized
Rafael Antognollie2a34552013-12-03 15:35:45 -02003494};
3495
3496static void
3497xdg_send_configure(struct weston_surface *surface,
3498 uint32_t edges, int32_t width, int32_t height)
3499{
3500 struct shell_surface *shsurf = get_shell_surface(surface);
3501
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003502 assert(shsurf);
3503
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003504 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003505}
3506
3507static const struct weston_shell_client xdg_client = {
3508 xdg_send_configure
3509};
3510
3511static void
3512xdg_use_unstable_version(struct wl_client *client,
3513 struct wl_resource *resource,
3514 int32_t version)
3515{
3516 if (version > 1) {
3517 wl_resource_post_error(resource,
3518 1,
3519 "xdg-shell:: version not implemented yet.");
3520 return;
3521 }
3522}
3523
3524static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003525create_xdg_surface(struct shell_client *owner, void *shell,
3526 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003527 const struct weston_shell_client *client)
3528{
3529 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003530
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003531 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003532 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003533
3534 return shsurf;
3535}
3536
3537static void
3538xdg_get_xdg_surface(struct wl_client *client,
3539 struct wl_resource *resource,
3540 uint32_t id,
3541 struct wl_resource *surface_resource)
3542{
3543 struct weston_surface *surface =
3544 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003545 struct shell_client *sc = wl_resource_get_user_data(resource);
3546 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003547 struct shell_surface *shsurf;
3548
3549 if (get_shell_surface(surface)) {
3550 wl_resource_post_error(surface_resource,
3551 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003552 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003553 return;
3554 }
3555
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003556 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003557 if (!shsurf) {
3558 wl_resource_post_error(surface_resource,
3559 WL_DISPLAY_ERROR_INVALID_OBJECT,
3560 "surface->configure already set");
3561 return;
3562 }
3563
3564 shsurf->resource =
3565 wl_resource_create(client,
3566 &xdg_surface_interface, 1, id);
3567 wl_resource_set_implementation(shsurf->resource,
3568 &xdg_surface_implementation,
3569 shsurf, shell_destroy_shell_surface);
3570}
3571
3572static bool
3573shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3574{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003575 return shsurf->resource &&
3576 wl_resource_instance_of(shsurf->resource,
3577 &xdg_surface_interface,
3578 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003579}
3580
3581/* xdg-popup implementation */
3582
3583static void
3584xdg_popup_destroy(struct wl_client *client,
3585 struct wl_resource *resource)
3586{
3587 wl_resource_destroy(resource);
3588}
3589
Rafael Antognollie2a34552013-12-03 15:35:45 -02003590static const struct xdg_popup_interface xdg_popup_implementation = {
3591 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003592};
3593
3594static void
3595xdg_popup_send_configure(struct weston_surface *surface,
3596 uint32_t edges, int32_t width, int32_t height)
3597{
3598}
3599
3600static const struct weston_shell_client xdg_popup_client = {
3601 xdg_popup_send_configure
3602};
3603
3604static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003605create_xdg_popup(struct shell_client *owner, void *shell,
3606 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003607 const struct weston_shell_client *client,
3608 struct weston_surface *parent,
3609 struct shell_seat *seat,
3610 uint32_t serial,
3611 int32_t x, int32_t y)
3612{
3613 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003614
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003615 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003616 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003617 shsurf->popup.shseat = seat;
3618 shsurf->popup.serial = serial;
3619 shsurf->popup.x = x;
3620 shsurf->popup.y = y;
3621 shell_surface_set_parent(shsurf, parent);
3622
3623 return shsurf;
3624}
3625
3626static void
3627xdg_get_xdg_popup(struct wl_client *client,
3628 struct wl_resource *resource,
3629 uint32_t id,
3630 struct wl_resource *surface_resource,
3631 struct wl_resource *parent_resource,
3632 struct wl_resource *seat_resource,
3633 uint32_t serial,
3634 int32_t x, int32_t y, uint32_t flags)
3635{
3636 struct weston_surface *surface =
3637 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003638 struct shell_client *sc = wl_resource_get_user_data(resource);
3639 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003640 struct shell_surface *shsurf;
3641 struct weston_surface *parent;
3642 struct shell_seat *seat;
3643
3644 if (get_shell_surface(surface)) {
3645 wl_resource_post_error(surface_resource,
3646 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003647 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003648 return;
3649 }
3650
3651 if (!parent_resource) {
3652 wl_resource_post_error(surface_resource,
3653 WL_DISPLAY_ERROR_INVALID_OBJECT,
3654 "xdg_shell::get_xdg_popup requires a parent shell surface");
3655 }
3656
3657 parent = wl_resource_get_user_data(parent_resource);
3658 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3659
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003660 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003661 parent, seat, serial, x, y);
3662 if (!shsurf) {
3663 wl_resource_post_error(surface_resource,
3664 WL_DISPLAY_ERROR_INVALID_OBJECT,
3665 "surface->configure already set");
3666 return;
3667 }
3668
3669 shsurf->resource =
3670 wl_resource_create(client,
3671 &xdg_popup_interface, 1, id);
3672 wl_resource_set_implementation(shsurf->resource,
3673 &xdg_popup_implementation,
3674 shsurf, shell_destroy_shell_surface);
3675}
3676
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003677static void
3678xdg_pong(struct wl_client *client,
3679 struct wl_resource *resource, uint32_t serial)
3680{
3681 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003682
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003683 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003684}
3685
Rafael Antognollie2a34552013-12-03 15:35:45 -02003686static bool
3687shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3688{
3689 return wl_resource_instance_of(shsurf->resource,
3690 &xdg_popup_interface,
3691 &xdg_popup_implementation);
3692}
3693
3694static const struct xdg_shell_interface xdg_implementation = {
3695 xdg_use_unstable_version,
3696 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003697 xdg_get_xdg_popup,
3698 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003699};
3700
3701static int
3702xdg_shell_unversioned_dispatch(const void *implementation,
3703 void *_target, uint32_t opcode,
3704 const struct wl_message *message,
3705 union wl_argument *args)
3706{
3707 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003708 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003709
3710 if (opcode != 0) {
3711 wl_resource_post_error(resource,
3712 WL_DISPLAY_ERROR_INVALID_OBJECT,
3713 "must call use_unstable_version first");
3714 return 0;
3715 }
3716
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003717#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003718
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003719 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3720 "shell implementation doesn't match protocol version");
3721
Rafael Antognollie2a34552013-12-03 15:35:45 -02003722 if (args[0].i != XDG_SERVER_VERSION) {
3723 wl_resource_post_error(resource,
3724 WL_DISPLAY_ERROR_INVALID_OBJECT,
3725 "incompatible version, server is %d "
3726 "client wants %d",
3727 XDG_SERVER_VERSION, args[0].i);
3728 return 0;
3729 }
3730
3731 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003732 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003733
3734 return 1;
3735}
3736
3737/* end of xdg-shell implementation */
3738/***********************************/
3739
Kristian Høgsberg07937562011-04-12 17:25:42 -04003740static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003741shell_fade(struct desktop_shell *shell, enum fade_type type);
3742
3743static int
3744screensaver_timeout(void *data)
3745{
3746 struct desktop_shell *shell = data;
3747
3748 shell_fade(shell, FADE_OUT);
3749
3750 return 1;
3751}
3752
3753static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003754handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003755{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003756 struct desktop_shell *shell =
3757 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003758
Pekka Paalanen18027e52011-12-02 16:31:49 +02003759 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003760
3761 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003762 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003763}
3764
3765static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003766launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003767{
3768 if (shell->screensaver.binding)
3769 return;
3770
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003771 if (!shell->screensaver.path) {
3772 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003773 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003774 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003775
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003776 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003777 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003778 return;
3779 }
3780
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003781 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003782 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003783 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003784 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003785}
3786
3787static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003788terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003789{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003790 if (shell->screensaver.process.pid == 0)
3791 return;
3792
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003793 /* Disarm the screensaver timer, otherwise it may fire when the
3794 * compositor is not in the idle state. In that case, the screen will
3795 * be locked, but the wake_signal won't fire on user input, making the
3796 * system unresponsive. */
3797 wl_event_source_timer_update(shell->screensaver.timer, 0);
3798
Pekka Paalanen18027e52011-12-02 16:31:49 +02003799 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003800}
3801
3802static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003803configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003804{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003805 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003806
Jason Ekstranda7af7042013-10-12 22:38:11 -05003807 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3808 if (v->output == ev->output && v != ev) {
3809 weston_view_unmap(v);
3810 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003811 }
3812 }
3813
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003814 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003815
Jason Ekstranda7af7042013-10-12 22:38:11 -05003816 if (wl_list_empty(&ev->layer_link)) {
3817 wl_list_insert(&layer->view_list, &ev->layer_link);
3818 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003819 }
3820}
3821
3822static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003823background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003824{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003825 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003826 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003827
Jason Ekstranda7af7042013-10-12 22:38:11 -05003828 view = container_of(es->views.next, struct weston_view, surface_link);
3829
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003830 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003831}
3832
3833static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003834desktop_shell_set_background(struct wl_client *client,
3835 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003836 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003837 struct wl_resource *surface_resource)
3838{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003839 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003840 struct weston_surface *surface =
3841 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003842 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003843
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003844 if (surface->configure) {
3845 wl_resource_post_error(surface_resource,
3846 WL_DISPLAY_ERROR_INVALID_OBJECT,
3847 "surface role already assigned");
3848 return;
3849 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003850
Jason Ekstranda7af7042013-10-12 22:38:11 -05003851 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3852 weston_view_destroy(view);
3853 view = weston_view_create(surface);
3854
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003855 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003856 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003857 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003858 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003859 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003860 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003861 surface->output->width,
3862 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003863}
3864
3865static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003866panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003867{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003868 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003869 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003870
Jason Ekstranda7af7042013-10-12 22:38:11 -05003871 view = container_of(es->views.next, struct weston_view, surface_link);
3872
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003873 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003874}
3875
3876static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003877desktop_shell_set_panel(struct wl_client *client,
3878 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003879 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003880 struct wl_resource *surface_resource)
3881{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003882 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003883 struct weston_surface *surface =
3884 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003885 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003886
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003887 if (surface->configure) {
3888 wl_resource_post_error(surface_resource,
3889 WL_DISPLAY_ERROR_INVALID_OBJECT,
3890 "surface role already assigned");
3891 return;
3892 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003893
Jason Ekstranda7af7042013-10-12 22:38:11 -05003894 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3895 weston_view_destroy(view);
3896 view = weston_view_create(surface);
3897
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003898 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003899 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003900 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003901 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003902 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003903 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003904 surface->output->width,
3905 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003906}
3907
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003908static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003909lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003910{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003911 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003912 struct weston_view *view;
3913
3914 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003915
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003916 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003917 return;
3918
Jason Ekstranda7af7042013-10-12 22:38:11 -05003919 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003920
3921 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003922 wl_list_insert(&shell->lock_layer.view_list,
3923 &view->layer_link);
3924 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003925 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003926 }
3927}
3928
3929static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003930handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003931{
Tiago Vignattibe143262012-04-16 17:31:41 +03003932 struct desktop_shell *shell =
3933 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003934
Martin Minarik6d118362012-06-07 18:01:59 +02003935 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003936 shell->lock_surface = NULL;
3937}
3938
3939static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003940desktop_shell_set_lock_surface(struct wl_client *client,
3941 struct wl_resource *resource,
3942 struct wl_resource *surface_resource)
3943{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003944 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003945 struct weston_surface *surface =
3946 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003947
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003948 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003949
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003950 if (!shell->locked)
3951 return;
3952
Pekka Paalanen98262232011-12-01 10:42:22 +02003953 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003954
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003955 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003956 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003957 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003958
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003959 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003960 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003961 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003962}
3963
3964static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003965resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003966{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003967 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003968
Pekka Paalanen77346a62011-11-30 16:26:35 +02003969 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003970
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003971 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003972 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003973 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003974 &shell->input_panel_layer.link);
3975 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003976 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003977 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003978 wl_list_insert(&shell->compositor->cursor_layer.link,
3979 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003980 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003981 wl_list_insert(&shell->fullscreen_layer.link,
3982 &shell->panel_layer.link);
3983 wl_list_insert(&shell->panel_layer.link,
3984 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003985
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003986 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003987
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003988 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003989 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003990 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003991}
3992
3993static void
3994desktop_shell_unlock(struct wl_client *client,
3995 struct wl_resource *resource)
3996{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003997 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003998
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003999 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004000
4001 if (shell->locked)
4002 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004003}
4004
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004005static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004006desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004007 struct wl_resource *resource,
4008 struct wl_resource *surface_resource)
4009{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004010 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004011
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004012 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004013 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004014}
4015
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004016static void
4017desktop_shell_desktop_ready(struct wl_client *client,
4018 struct wl_resource *resource)
4019{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004020 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004021
4022 shell_fade_startup(shell);
4023}
4024
Kristian Høgsberg75840622011-09-06 13:48:16 -04004025static const struct desktop_shell_interface desktop_shell_implementation = {
4026 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004027 desktop_shell_set_panel,
4028 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004029 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004030 desktop_shell_set_grab_surface,
4031 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004032};
4033
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004034static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004035get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004036{
4037 struct shell_surface *shsurf;
4038
4039 shsurf = get_shell_surface(surface);
4040 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004041 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004042 return shsurf->type;
4043}
4044
Kristian Høgsberg75840622011-09-06 13:48:16 -04004045static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004046move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004047{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004048 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004049 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004050 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004051
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004052 if (seat->pointer->focus == NULL)
4053 return;
4054
4055 focus = seat->pointer->focus->surface;
4056
Pekka Paalanen01388e22013-04-25 13:57:44 +03004057 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004058 if (surface == NULL)
4059 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004060
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004061 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004062 if (shsurf == NULL || shsurf->state.fullscreen ||
4063 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004064 return;
4065
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004066 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004067}
4068
4069static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004070maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4071{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004072 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004073 struct weston_surface *surface;
4074 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004075 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004076
4077 surface = weston_surface_get_main_surface(focus);
4078 if (surface == NULL)
4079 return;
4080
4081 shsurf = get_shell_surface(surface);
4082 if (shsurf == NULL)
4083 return;
4084
4085 if (!shell_surface_is_xdg_surface(shsurf))
4086 return;
4087
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004088 serial = wl_display_next_serial(seat->compositor->wl_display);
4089 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4090 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004091}
4092
4093static void
4094fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4095{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004096 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004097 struct weston_surface *surface;
4098 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004099 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004100
4101 surface = weston_surface_get_main_surface(focus);
4102 if (surface == NULL)
4103 return;
4104
4105 shsurf = get_shell_surface(surface);
4106 if (shsurf == NULL)
4107 return;
4108
4109 if (!shell_surface_is_xdg_surface(shsurf))
4110 return;
4111
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004112 serial = wl_display_next_serial(seat->compositor->wl_display);
4113 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4114 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004115}
4116
4117static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004118touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4119{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004120 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004121 struct weston_surface *surface;
4122 struct shell_surface *shsurf;
4123
4124 surface = weston_surface_get_main_surface(focus);
4125 if (surface == NULL)
4126 return;
4127
4128 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004129 if (shsurf == NULL || shsurf->state.fullscreen ||
4130 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004131 return;
4132
4133 surface_touch_move(shsurf, (struct weston_seat *) seat);
4134}
4135
4136static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004137resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004138{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004139 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004140 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004141 uint32_t edges = 0;
4142 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004143 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004144
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004145 if (seat->pointer->focus == NULL)
4146 return;
4147
4148 focus = seat->pointer->focus->surface;
4149
Pekka Paalanen01388e22013-04-25 13:57:44 +03004150 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004151 if (surface == NULL)
4152 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004153
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004154 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004155 if (shsurf == NULL || shsurf->state.fullscreen ||
4156 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004157 return;
4158
Jason Ekstranda7af7042013-10-12 22:38:11 -05004159 weston_view_from_global(shsurf->view,
4160 wl_fixed_to_int(seat->pointer->grab_x),
4161 wl_fixed_to_int(seat->pointer->grab_y),
4162 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004163
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004164 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004165 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004166 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004167 edges |= 0;
4168 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004169 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004170
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004171 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004172 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004173 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004174 edges |= 0;
4175 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004176 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004177
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004178 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004179}
4180
4181static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004182surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004183 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004184{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004185 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004186 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004187 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004188 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004189
Pekka Paalanen01388e22013-04-25 13:57:44 +03004190 /* XXX: broken for windows containing sub-surfaces */
4191 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004192 if (surface == NULL)
4193 return;
4194
4195 shsurf = get_shell_surface(surface);
4196 if (!shsurf)
4197 return;
4198
Jason Ekstranda7af7042013-10-12 22:38:11 -05004199 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004200
Jason Ekstranda7af7042013-10-12 22:38:11 -05004201 if (shsurf->view->alpha > 1.0)
4202 shsurf->view->alpha = 1.0;
4203 if (shsurf->view->alpha < step)
4204 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004205
Jason Ekstranda7af7042013-10-12 22:38:11 -05004206 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004207 weston_surface_damage(surface);
4208}
4209
4210static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004211do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004212 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004213{
Daniel Stone37816df2012-05-16 18:45:18 +01004214 struct weston_seat *ws = (struct weston_seat *) seat;
4215 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004216 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004217 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004218
4219 wl_list_for_each(output, &compositor->output_list, link) {
4220 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004221 wl_fixed_to_double(seat->pointer->x),
4222 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004223 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004224 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004225 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004226 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004227 increment = -output->zoom.increment;
4228 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004229 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004230 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004231 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004232 else
4233 increment = 0;
4234
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004235 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004236
Scott Moreaue6603982012-06-11 13:07:51 -06004237 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004238 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004239 else if (output->zoom.level > output->zoom.max_level)
4240 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004241 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004242 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004243 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004244
Scott Moreaue6603982012-06-11 13:07:51 -06004245 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004246
Jason Ekstranda7af7042013-10-12 22:38:11 -05004247 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004248 }
4249 }
4250}
4251
Scott Moreauccbf29d2012-02-22 14:21:41 -07004252static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004253zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004254 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004255{
4256 do_zoom(seat, time, 0, axis, value);
4257}
4258
4259static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004260zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004261 void *data)
4262{
4263 do_zoom(seat, time, key, 0, 0);
4264}
4265
4266static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004267terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004268 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004269{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004270 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004271
Daniel Stone325fc2d2012-05-30 16:31:58 +01004272 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004273}
4274
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004275static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004276rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4277 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004278{
4279 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004280 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004281 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004282 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004283 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004284
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004285 weston_pointer_move(pointer, x, y);
4286
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004287 if (!shsurf)
4288 return;
4289
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004290 cx = 0.5f * shsurf->surface->width;
4291 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004292
Daniel Stone37816df2012-05-16 18:45:18 +01004293 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4294 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004295 r = sqrtf(dx * dx + dy * dy);
4296
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004297 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004298 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004299
4300 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004301 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004302 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004303
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004304 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004305 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004306
4307 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004308 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004309 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004310 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004311 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004312
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004313 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004314 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004315 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004316 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004317 wl_list_init(&shsurf->rotation.transform.link);
4318 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004319 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004320 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004321
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004322 /* We need to adjust the position of the surface
4323 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004324 cposx = shsurf->view->geometry.x + cx;
4325 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004326 dposx = rotate->center.x - cposx;
4327 dposy = rotate->center.y - cposy;
4328 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004329 weston_view_set_position(shsurf->view,
4330 shsurf->view->geometry.x + dposx,
4331 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004332 }
4333
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004334 /* Repaint implies weston_surface_update_transform(), which
4335 * lazily applies the damage due to rotation update.
4336 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004337 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004338}
4339
4340static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004341rotate_grab_button(struct weston_pointer_grab *grab,
4342 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004343{
4344 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004345 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004346 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004347 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004348 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004349
Daniel Stone4dbadb12012-05-30 16:31:51 +01004350 if (pointer->button_count == 0 &&
4351 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004352 if (shsurf)
4353 weston_matrix_multiply(&shsurf->rotation.rotation,
4354 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004355 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004356 free(rotate);
4357 }
4358}
4359
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004360static void
4361rotate_grab_cancel(struct weston_pointer_grab *grab)
4362{
4363 struct rotate_grab *rotate =
4364 container_of(grab, struct rotate_grab, base.grab);
4365
4366 shell_grab_end(&rotate->base);
4367 free(rotate);
4368}
4369
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004370static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004371 noop_grab_focus,
4372 rotate_grab_motion,
4373 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004374 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004375};
4376
4377static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004378surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004379{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004380 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004381 float dx, dy;
4382 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004383
Pekka Paalanen460099f2012-01-20 16:48:25 +02004384 rotate = malloc(sizeof *rotate);
4385 if (!rotate)
4386 return;
4387
Jason Ekstranda7af7042013-10-12 22:38:11 -05004388 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004389 surface->surface->width * 0.5f,
4390 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004391 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004392
Daniel Stone37816df2012-05-16 18:45:18 +01004393 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4394 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004395 r = sqrtf(dx * dx + dy * dy);
4396 if (r > 20.0f) {
4397 struct weston_matrix inverse;
4398
4399 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004400 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004401 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004402
4403 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004404 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004405 } else {
4406 weston_matrix_init(&surface->rotation.rotation);
4407 weston_matrix_init(&rotate->rotation);
4408 }
4409
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004410 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4411 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004412}
4413
4414static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004415rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004416 void *data)
4417{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004418 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004419 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004420 struct shell_surface *surface;
4421
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004422 if (seat->pointer->focus == NULL)
4423 return;
4424
4425 focus = seat->pointer->focus->surface;
4426
Pekka Paalanen01388e22013-04-25 13:57:44 +03004427 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004428 if (base_surface == NULL)
4429 return;
4430
4431 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004432 if (surface == NULL || surface->state.fullscreen ||
4433 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004434 return;
4435
4436 surface_rotate(surface, seat);
4437}
4438
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004439/* Move all fullscreen layers down to the current workspace and hide their
4440 * black views. The surfaces' state is set to both fullscreen and lowered,
4441 * and this is reversed when such a surface is re-configured, see
4442 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4443 *
4444 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004445 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004446void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004447lower_fullscreen_layer(struct desktop_shell *shell)
4448{
4449 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004450 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004451
4452 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004453 wl_list_for_each_reverse_safe(view, prev,
4454 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004455 layer_link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004456 struct shell_surface *shsurf = get_shell_surface(view->surface);
4457
4458 if (!shsurf)
4459 continue;
4460
4461 /* We can have a non-fullscreen popup for a fullscreen surface
4462 * in the fullscreen layer. */
4463 if (shsurf->state.fullscreen) {
4464 /* Hide the black view */
4465 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
4466 wl_list_init(&shsurf->fullscreen.black_view->layer_link);
4467 }
4468
4469 /* Lower the view to the workspace layer */
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004470 wl_list_remove(&view->layer_link);
4471 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4472 weston_view_damage_below(view);
4473 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004474
4475 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004476 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004477}
4478
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004479void
Tiago Vignattibe143262012-04-16 17:31:41 +03004480activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004481 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004482{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004483 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004484 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004485 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004486 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004487 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004488
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004489 lower_fullscreen_layer(shell);
4490
Pekka Paalanen01388e22013-04-25 13:57:44 +03004491 main_surface = weston_surface_get_main_surface(es);
4492
Daniel Stone37816df2012-05-16 18:45:18 +01004493 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004494
Jonas Ådahl8538b222012-08-29 22:13:03 +02004495 state = ensure_focus_state(shell, seat);
4496 if (state == NULL)
4497 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004498
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004499 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004500 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004501
Rafael Antognolli03b16592013-12-03 15:35:42 -02004502 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004503 assert(shsurf);
4504
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004505 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004506 shell_configure_fullscreen(shsurf);
4507 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004508 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004509
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004510 /* Update the surface’s layer. This brings it to the top of the stacking
4511 * order as appropriate. */
4512 shell_surface_update_layer(shsurf);
4513
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004514 if (shell->focus_animation_type != ANIMATION_NONE) {
4515 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004516 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004517 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004518}
4519
Alex Wu21858432012-04-01 20:13:08 +08004520/* no-op func for checking black surface */
4521static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004522black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004523{
4524}
4525
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004526static bool
Alex Wu21858432012-04-01 20:13:08 +08004527is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4528{
4529 if (es->configure == black_surface_configure) {
4530 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004531 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004532 return true;
4533 }
4534 return false;
4535}
4536
Kristian Høgsberg75840622011-09-06 13:48:16 -04004537static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004538activate_binding(struct weston_seat *seat,
4539 struct desktop_shell *shell,
4540 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004541{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004542 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004543
Alex Wu9c35e6b2012-03-05 14:13:13 +08004544 if (!focus)
4545 return;
4546
Pekka Paalanen01388e22013-04-25 13:57:44 +03004547 if (is_black_surface(focus, &main_surface))
4548 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004549
Pekka Paalanen01388e22013-04-25 13:57:44 +03004550 main_surface = weston_surface_get_main_surface(focus);
4551 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004552 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004553
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004554 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004555}
4556
4557static void
4558click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4559 void *data)
4560{
4561 if (seat->pointer->grab != &seat->pointer->default_grab)
4562 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004563 if (seat->pointer->focus == NULL)
4564 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004565
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004566 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004567}
4568
4569static void
4570touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4571{
4572 if (seat->touch->grab != &seat->touch->default_grab)
4573 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004574 if (seat->touch->focus == NULL)
4575 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004576
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004577 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004578}
4579
4580static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004581unfocus_all_seats(struct desktop_shell *shell)
4582{
4583 struct weston_seat *seat, *next;
4584
4585 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4586 if (seat->keyboard == NULL)
4587 continue;
4588
4589 weston_keyboard_set_focus(seat->keyboard, NULL);
4590 }
4591}
4592
4593static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004594lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004595{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004596 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004597
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004598 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004599 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004600 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004601 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004602
4603 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004604
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004605 /* Hide all surfaces by removing the fullscreen, panel and
4606 * toplevel layers. This way nothing else can show or receive
4607 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004608
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004609 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004610 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004611 if (shell->showing_input_panels)
4612 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004613 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004614 wl_list_insert(&shell->compositor->cursor_layer.link,
4615 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004616
Pekka Paalanen77346a62011-11-30 16:26:35 +02004617 launch_screensaver(shell);
4618
Neil Robertsb4a91702014-04-09 16:33:32 +01004619 /* Remove the keyboard focus on all seats. This will be
4620 * restored to the workspace's saved state via
4621 * restore_focus_state when the compositor is unlocked */
4622 unfocus_all_seats(shell);
4623
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004624 /* TODO: disable bindings that should not work while locked. */
4625
4626 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004627}
4628
4629static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004630unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004631{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004632 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004633 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004634 return;
4635 }
4636
4637 /* If desktop-shell client has gone away, unlock immediately. */
4638 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004639 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004640 return;
4641 }
4642
4643 if (shell->prepare_event_sent)
4644 return;
4645
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004646 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004647 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004648}
4649
4650static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004651shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004652{
4653 struct desktop_shell *shell = data;
4654
4655 shell->fade.animation = NULL;
4656
4657 switch (shell->fade.type) {
4658 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004659 weston_surface_destroy(shell->fade.view->surface);
4660 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004661 break;
4662 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004663 lock(shell);
4664 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004665 default:
4666 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004667 }
4668}
4669
Jason Ekstranda7af7042013-10-12 22:38:11 -05004670static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004671shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004672{
4673 struct weston_compositor *compositor = shell->compositor;
4674 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004675 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004676
4677 surface = weston_surface_create(compositor);
4678 if (!surface)
4679 return NULL;
4680
Jason Ekstranda7af7042013-10-12 22:38:11 -05004681 view = weston_view_create(surface);
4682 if (!view) {
4683 weston_surface_destroy(surface);
4684 return NULL;
4685 }
4686
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004687 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004688 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004689 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004690 wl_list_insert(&compositor->fade_layer.view_list,
4691 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004692 pixman_region32_init(&surface->input);
4693
Jason Ekstranda7af7042013-10-12 22:38:11 -05004694 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004695}
4696
4697static void
4698shell_fade(struct desktop_shell *shell, enum fade_type type)
4699{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004700 float tint;
4701
4702 switch (type) {
4703 case FADE_IN:
4704 tint = 0.0;
4705 break;
4706 case FADE_OUT:
4707 tint = 1.0;
4708 break;
4709 default:
4710 weston_log("shell: invalid fade type\n");
4711 return;
4712 }
4713
4714 shell->fade.type = type;
4715
Jason Ekstranda7af7042013-10-12 22:38:11 -05004716 if (shell->fade.view == NULL) {
4717 shell->fade.view = shell_fade_create_surface(shell);
4718 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004719 return;
4720
Jason Ekstranda7af7042013-10-12 22:38:11 -05004721 shell->fade.view->alpha = 1.0 - tint;
4722 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004723 }
4724
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004725 if (shell->fade.view->output == NULL) {
4726 /* If the black view gets a NULL output, we lost the
4727 * last output and we'll just cancel the fade. This
4728 * happens when you close the last window under the
4729 * X11 or Wayland backends. */
4730 shell->locked = false;
4731 weston_surface_destroy(shell->fade.view->surface);
4732 shell->fade.view = NULL;
4733 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004734 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004735 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004736 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004737 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004738 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004739 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004740 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004741}
4742
4743static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004744do_shell_fade_startup(void *data)
4745{
4746 struct desktop_shell *shell = data;
4747
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004748 if (shell->startup_animation_type == ANIMATION_FADE)
4749 shell_fade(shell, FADE_IN);
4750 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004751 weston_surface_destroy(shell->fade.view->surface);
4752 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004753 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004754}
4755
4756static void
4757shell_fade_startup(struct desktop_shell *shell)
4758{
4759 struct wl_event_loop *loop;
4760
4761 if (!shell->fade.startup_timer)
4762 return;
4763
4764 wl_event_source_remove(shell->fade.startup_timer);
4765 shell->fade.startup_timer = NULL;
4766
4767 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4768 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4769}
4770
4771static int
4772fade_startup_timeout(void *data)
4773{
4774 struct desktop_shell *shell = data;
4775
4776 shell_fade_startup(shell);
4777 return 0;
4778}
4779
4780static void
4781shell_fade_init(struct desktop_shell *shell)
4782{
4783 /* Make compositor output all black, and wait for the desktop-shell
4784 * client to signal it is ready, then fade in. The timer triggers a
4785 * fade-in, in case the desktop-shell client takes too long.
4786 */
4787
4788 struct wl_event_loop *loop;
4789
Jason Ekstranda7af7042013-10-12 22:38:11 -05004790 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004791 weston_log("%s: warning: fade surface already exists\n",
4792 __func__);
4793 return;
4794 }
4795
Jason Ekstranda7af7042013-10-12 22:38:11 -05004796 shell->fade.view = shell_fade_create_surface(shell);
4797 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004798 return;
4799
Jason Ekstranda7af7042013-10-12 22:38:11 -05004800 weston_view_update_transform(shell->fade.view);
4801 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004802
4803 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4804 shell->fade.startup_timer =
4805 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4806 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4807}
4808
4809static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004810idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004811{
4812 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004813 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004814 struct weston_seat *seat;
4815
4816 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4817 if (seat->pointer)
4818 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004819
4820 shell_fade(shell, FADE_OUT);
4821 /* lock() is called from shell_fade_done() */
4822}
4823
4824static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004825wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004826{
4827 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004828 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004829
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004830 unlock(shell);
4831}
4832
4833static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004834center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004835{
Giulio Camuffob8366642013-04-25 13:57:46 +03004836 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004837 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004838
Jason Ekstranda7af7042013-10-12 22:38:11 -05004839 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004840
4841 x = output->x + (output->width - width) / 2 - surf_x / 2;
4842 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004843
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004844 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004845}
4846
4847static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004848weston_view_set_initial_position(struct weston_view *view,
4849 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004850{
4851 struct weston_compositor *compositor = shell->compositor;
4852 int ix = 0, iy = 0;
4853 int range_x, range_y;
4854 int dx, dy, x, y, panel_height;
4855 struct weston_output *output, *target_output = NULL;
4856 struct weston_seat *seat;
4857
4858 /* As a heuristic place the new window on the same output as the
4859 * pointer. Falling back to the output containing 0, 0.
4860 *
4861 * TODO: Do something clever for touch too?
4862 */
4863 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004864 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004865 ix = wl_fixed_to_int(seat->pointer->x);
4866 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004867 break;
4868 }
4869 }
4870
4871 wl_list_for_each(output, &compositor->output_list, link) {
4872 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4873 target_output = output;
4874 break;
4875 }
4876 }
4877
4878 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004879 weston_view_set_position(view, 10 + random() % 400,
4880 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004881 return;
4882 }
4883
4884 /* Valid range within output where the surface will still be onscreen.
4885 * If this is negative it means that the surface is bigger than
4886 * output.
4887 */
4888 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004889 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004890 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004891 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004892
Rob Bradford4cb88c72012-08-13 15:18:44 +01004893 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004894 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004895 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004896 dx = 0;
4897
4898 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004899 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004900 else
4901 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004902
4903 x = target_output->x + dx;
4904 y = target_output->y + dy;
4905
Jason Ekstranda7af7042013-10-12 22:38:11 -05004906 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004907}
4908
4909static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004910map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004911 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004912{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004913 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004914 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004915 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004916 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004917
Pekka Paalanen77346a62011-11-30 16:26:35 +02004918 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004919 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004920 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004921 if (shsurf->state.fullscreen) {
4922 center_on_output(shsurf->view, shsurf->fullscreen_output);
4923 shell_map_fullscreen(shsurf);
4924 } else if (shsurf->state.maximized) {
4925 /* use surface configure to set the geometry */
4926 panel_height = get_output_panel_height(shell, shsurf->output);
4927 surface_subsurfaces_boundingbox(shsurf->surface,
4928 &surf_x, &surf_y, NULL, NULL);
4929 weston_view_set_position(shsurf->view,
4930 shsurf->output->x - surf_x,
4931 shsurf->output->y +
4932 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004933 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004934 weston_view_set_initial_position(shsurf->view, shell);
4935 }
Juan Zhao96879df2012-02-07 08:45:41 +08004936 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004937 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004938 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004939 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004940 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004941 weston_view_set_position(shsurf->view,
4942 shsurf->view->geometry.x + sx,
4943 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004944 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004945 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004946 default:
4947 ;
4948 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004949
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004950 /* Surface stacking order, see also activate(). */
4951 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004952
Jason Ekstranda7af7042013-10-12 22:38:11 -05004953 if (shsurf->type != SHELL_SURFACE_NONE) {
4954 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004955 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004956 shsurf->surface->output = shsurf->output;
4957 shsurf->view->output = shsurf->output;
4958 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004959 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004960
Jason Ekstranda7af7042013-10-12 22:38:11 -05004961 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004962 /* XXX: xwayland's using the same fields for transient type */
4963 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004964 if (shsurf->transient.flags ==
4965 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4966 break;
4967 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004968 if (shsurf->state.relative &&
4969 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4970 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004971 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004972 break;
4973 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004974 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004975 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004976 case SHELL_SURFACE_POPUP:
4977 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004978 default:
4979 break;
4980 }
4981
Rafael Antognolli03b16592013-12-03 15:35:42 -02004982 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4983 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004984 {
4985 switch (shell->win_animation_type) {
4986 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004987 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004988 break;
4989 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004990 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004991 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004992 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004993 default:
4994 break;
4995 }
4996 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004997}
4998
4999static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005000configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005001 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005002{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005003 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005004 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005005 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005006
Pekka Paalanen77346a62011-11-30 16:26:35 +02005007 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005008
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005009 assert(shsurf);
5010
Rafael Antognolli03b16592013-12-03 15:35:42 -02005011 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005012 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005013 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005014 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005015 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5016 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005017 mx = shsurf->output->x - surf_x;
5018 my = shsurf->output->y +
5019 get_output_panel_height(shell,shsurf->output) - surf_y;
5020 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005021 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005022 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005023 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005024
Alex Wu4539b082012-03-01 12:57:46 +08005025 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005026 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005027 wl_list_for_each(view, &surface->views, surface_link)
5028 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005029
Rafael Antognolli03b16592013-12-03 15:35:42 -02005030 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005031 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005032 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005033}
5034
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005035static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005036shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005037{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005038 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005039 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005040 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005041
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005042 assert(shsurf);
5043
5044 shell = shsurf->shell;
5045
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005046 if (!weston_surface_is_mapped(es) &&
5047 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005048 remove_popup_grab(shsurf);
5049 }
5050
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005051 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005052 return;
5053
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005054 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005055 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005056 type_changed = 1;
5057 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005058
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005059 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005060 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005061 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005062 shsurf->last_width != es->width ||
5063 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005064 float from_x, from_y;
5065 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005066
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005067 if (shsurf->resize_edges) {
5068 sx = 0;
5069 sy = 0;
5070 }
5071
5072 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5073 sx = shsurf->last_width - es->width;
5074 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5075 sy = shsurf->last_height - es->height;
5076
5077 shsurf->last_width = es->width;
5078 shsurf->last_height = es->height;
5079
Jason Ekstranda7af7042013-10-12 22:38:11 -05005080 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5081 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005082 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005083 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005084 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005085 }
5086}
5087
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005088static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005089
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005090static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005091desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005092{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005093 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005094 struct desktop_shell *shell =
5095 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005096
5097 shell->child.process.pid = 0;
5098 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005099
5100 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5101 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005102 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005103 shell->child.deathstamp = time;
5104 shell->child.deathcount = 0;
5105 }
5106
5107 shell->child.deathcount++;
5108 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005109 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005110 return;
5111 }
5112
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005113 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005114 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005115 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005116}
5117
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005118static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005119desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5120{
5121 struct desktop_shell *shell;
5122
5123 shell = container_of(listener, struct desktop_shell,
5124 child.client_destroy_listener);
5125
5126 shell->child.client = NULL;
5127}
5128
5129static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005130launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005131{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005132 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005133
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005134 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005135 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005136 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005137 desktop_shell_sigchld);
5138
5139 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005140 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005141
5142 shell->child.client_destroy_listener.notify =
5143 desktop_shell_client_destroy;
5144 wl_client_add_destroy_listener(shell->child.client,
5145 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005146}
5147
5148static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005149handle_shell_client_destroy(struct wl_listener *listener, void *data)
5150{
5151 struct shell_client *sc =
5152 container_of(listener, struct shell_client, destroy_listener);
5153
5154 if (sc->ping_timer)
5155 wl_event_source_remove(sc->ping_timer);
5156 free(sc);
5157}
5158
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005159static struct shell_client *
5160shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5161 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005162{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005163 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005164
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005165 sc = zalloc(sizeof *sc);
5166 if (sc == NULL) {
5167 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005168 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005169 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005170
5171 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005172 if (sc->resource == NULL) {
5173 free(sc);
5174 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005175 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005176 }
5177
5178 sc->client = client;
5179 sc->shell = shell;
5180 sc->destroy_listener.notify = handle_shell_client_destroy;
5181 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5182
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005183 return sc;
5184}
5185
5186static void
5187bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5188{
5189 struct desktop_shell *shell = data;
5190 struct shell_client *sc;
5191
5192 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5193 if (sc)
5194 wl_resource_set_implementation(sc->resource,
5195 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005196 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005197}
5198
5199static void
5200bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5201{
5202 struct desktop_shell *shell = data;
5203 struct shell_client *sc;
5204
5205 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5206 if (sc)
5207 wl_resource_set_dispatcher(sc->resource,
5208 xdg_shell_unversioned_dispatch,
5209 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005210}
5211
5212static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005213unbind_desktop_shell(struct wl_resource *resource)
5214{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005215 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005216
5217 if (shell->locked)
5218 resume_desktop(shell);
5219
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005220 shell->child.desktop_shell = NULL;
5221 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005222}
5223
5224static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005225bind_desktop_shell(struct wl_client *client,
5226 void *data, uint32_t version, uint32_t id)
5227{
Tiago Vignattibe143262012-04-16 17:31:41 +03005228 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005229 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005230
Jason Ekstranda85118c2013-06-27 20:17:02 -05005231 resource = wl_resource_create(client, &desktop_shell_interface,
5232 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005233
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005234 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005235 wl_resource_set_implementation(resource,
5236 &desktop_shell_implementation,
5237 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005238 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005239
5240 if (version < 2)
5241 shell_fade_startup(shell);
5242
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005243 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005244 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005245
5246 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5247 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005248 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005249}
5250
Pekka Paalanen6e168112011-11-24 11:34:05 +02005251static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005252screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005253{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005254 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005255 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005256
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005257 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005258 return;
5259
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005260 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005261 if (!shell->locked)
5262 return;
5263
Jason Ekstranda7af7042013-10-12 22:38:11 -05005264 view = container_of(surface->views.next, struct weston_view, surface_link);
5265 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005266
Jason Ekstranda7af7042013-10-12 22:38:11 -05005267 if (wl_list_empty(&view->layer_link)) {
5268 wl_list_insert(shell->lock_layer.view_list.prev,
5269 &view->layer_link);
5270 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005271 wl_event_source_timer_update(shell->screensaver.timer,
5272 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005273 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005274 }
5275}
5276
5277static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005278screensaver_set_surface(struct wl_client *client,
5279 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005280 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005281 struct wl_resource *output_resource)
5282{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005283 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005284 struct weston_surface *surface =
5285 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005286 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005287 struct weston_view *view, *next;
5288
5289 /* Make sure we only have one view */
5290 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5291 weston_view_destroy(view);
5292 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005293
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005294 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005295 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005296 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005297}
5298
5299static const struct screensaver_interface screensaver_implementation = {
5300 screensaver_set_surface
5301};
5302
5303static void
5304unbind_screensaver(struct wl_resource *resource)
5305{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005306 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005307
Pekka Paalanen77346a62011-11-30 16:26:35 +02005308 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005309}
5310
5311static void
5312bind_screensaver(struct wl_client *client,
5313 void *data, uint32_t version, uint32_t id)
5314{
Tiago Vignattibe143262012-04-16 17:31:41 +03005315 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005316 struct wl_resource *resource;
5317
Jason Ekstranda85118c2013-06-27 20:17:02 -05005318 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005319
Pekka Paalanen77346a62011-11-30 16:26:35 +02005320 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005321 wl_resource_set_implementation(resource,
5322 &screensaver_implementation,
5323 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005324 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005325 return;
5326 }
5327
5328 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5329 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005330 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005331}
5332
Kristian Høgsberg07045392012-02-19 18:52:44 -05005333struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005334 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005335 struct weston_surface *current;
5336 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005337 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005338 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005339};
5340
5341static void
5342switcher_next(struct switcher *switcher)
5343{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005344 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005345 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005346 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005347 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005348
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005349 /* temporary re-display minimized surfaces */
5350 struct weston_view *tmp;
5351 struct weston_view **minimized;
5352 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5353 wl_list_remove(&view->layer_link);
5354 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5355 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5356 *minimized = view;
5357 }
5358
Jason Ekstranda7af7042013-10-12 22:38:11 -05005359 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005360 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005361 if (shsurf &&
5362 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5363 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005364 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005365 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005366 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005367 next = view->surface;
5368 prev = view->surface;
5369 view->alpha = 0.25;
5370 weston_view_geometry_dirty(view);
5371 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005372 }
Alex Wu1659daa2012-04-01 20:13:09 +08005373
Jason Ekstranda7af7042013-10-12 22:38:11 -05005374 if (is_black_surface(view->surface, NULL)) {
5375 view->alpha = 0.25;
5376 weston_view_geometry_dirty(view);
5377 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005378 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005379 }
5380
5381 if (next == NULL)
5382 next = first;
5383
Alex Wu07b26062012-03-12 16:06:01 +08005384 if (next == NULL)
5385 return;
5386
Kristian Høgsberg07045392012-02-19 18:52:44 -05005387 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005388 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005389
5390 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005391 wl_list_for_each(view, &next->views, surface_link)
5392 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005393
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005394 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005395 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005396 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005397}
5398
5399static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005400switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005401{
5402 struct switcher *switcher =
5403 container_of(listener, struct switcher, listener);
5404
5405 switcher_next(switcher);
5406}
5407
5408static void
Daniel Stone351eb612012-05-31 15:27:47 -04005409switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005410{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005411 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005412 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005413 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005414
Jason Ekstranda7af7042013-10-12 22:38:11 -05005415 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005416 if (is_focus_view(view))
5417 continue;
5418
Jason Ekstranda7af7042013-10-12 22:38:11 -05005419 view->alpha = 1.0;
5420 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005421 }
5422
Alex Wu07b26062012-03-12 16:06:01 +08005423 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005424 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005425 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005426 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005427 weston_keyboard_end_grab(keyboard);
5428 if (keyboard->input_method_resource)
5429 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005430
5431 /* re-hide surfaces that were temporary shown during the switch */
5432 struct weston_view **minimized;
5433 wl_array_for_each(minimized, &switcher->minimized_array) {
5434 /* with the exception of the current selected */
5435 if ((*minimized)->surface != switcher->current) {
5436 wl_list_remove(&(*minimized)->layer_link);
5437 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5438 weston_view_damage_below(*minimized);
5439 }
5440 }
5441 wl_array_release(&switcher->minimized_array);
5442
Kristian Høgsberg07045392012-02-19 18:52:44 -05005443 free(switcher);
5444}
5445
5446static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005447switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005448 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005449{
5450 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005451 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005452
Daniel Stonec9785ea2012-05-30 16:31:52 +01005453 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005454 switcher_next(switcher);
5455}
5456
5457static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005458switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005459 uint32_t mods_depressed, uint32_t mods_latched,
5460 uint32_t mods_locked, uint32_t group)
5461{
5462 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005463 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005464
Daniel Stone351eb612012-05-31 15:27:47 -04005465 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5466 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005467}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005468
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005469static void
5470switcher_cancel(struct weston_keyboard_grab *grab)
5471{
5472 struct switcher *switcher = container_of(grab, struct switcher, grab);
5473
5474 switcher_destroy(switcher);
5475}
5476
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005477static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005478 switcher_key,
5479 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005480 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005481};
5482
5483static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005484switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005485 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005486{
Tiago Vignattibe143262012-04-16 17:31:41 +03005487 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005488 struct switcher *switcher;
5489
5490 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005491 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005492 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005493 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005494 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005495 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005496
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005497 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005498 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005499 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005500 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5501 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005502 switcher_next(switcher);
5503}
5504
Pekka Paalanen3c647232011-12-22 13:43:43 +02005505static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005506backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005507 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005508{
5509 struct weston_compositor *compositor = data;
5510 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005511 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005512
5513 /* TODO: we're limiting to simple use cases, where we assume just
5514 * control on the primary display. We'd have to extend later if we
5515 * ever get support for setting backlights on random desktop LCD
5516 * panels though */
5517 output = get_default_output(compositor);
5518 if (!output)
5519 return;
5520
5521 if (!output->set_backlight)
5522 return;
5523
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005524 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5525 backlight_new = output->backlight_current - 25;
5526 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5527 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005528
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005529 if (backlight_new < 5)
5530 backlight_new = 5;
5531 if (backlight_new > 255)
5532 backlight_new = 255;
5533
5534 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005535 output->set_backlight(output, output->backlight_current);
5536}
5537
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005538struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005539 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005540 struct weston_seat *seat;
5541 uint32_t key[2];
5542 int key_released[2];
5543};
5544
5545static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005546debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005547 uint32_t key, uint32_t state)
5548{
5549 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005550 struct weston_compositor *ec = db->seat->compositor;
5551 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005552 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005553 uint32_t serial;
5554 int send = 0, terminate = 0;
5555 int check_binding = 1;
5556 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005557 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005558
5559 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5560 /* Do not run bindings on key releases */
5561 check_binding = 0;
5562
5563 for (i = 0; i < 2; i++)
5564 if (key == db->key[i])
5565 db->key_released[i] = 1;
5566
5567 if (db->key_released[0] && db->key_released[1]) {
5568 /* All key releases been swalled so end the grab */
5569 terminate = 1;
5570 } else if (key != db->key[0] && key != db->key[1]) {
5571 /* Should not swallow release of other keys */
5572 send = 1;
5573 }
5574 } else if (key == db->key[0] && !db->key_released[0]) {
5575 /* Do not check bindings for the first press of the binding
5576 * key. This allows it to be used as a debug shortcut.
5577 * We still need to swallow this event. */
5578 check_binding = 0;
5579 } else if (db->key[1]) {
5580 /* If we already ran a binding don't process another one since
5581 * we can't keep track of all the binding keys that were
5582 * pressed in order to swallow the release events. */
5583 send = 1;
5584 check_binding = 0;
5585 }
5586
5587 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005588 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5589 key, state)) {
5590 /* We ran a binding so swallow the press and keep the
5591 * grab to swallow the released too. */
5592 send = 0;
5593 terminate = 0;
5594 db->key[1] = key;
5595 } else {
5596 /* Terminate the grab since the key pressed is not a
5597 * debug binding key. */
5598 send = 1;
5599 terminate = 1;
5600 }
5601 }
5602
5603 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005604 serial = wl_display_next_serial(display);
5605 resource_list = &grab->keyboard->focus_resource_list;
5606 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005607 wl_keyboard_send_key(resource, serial, time, key, state);
5608 }
5609 }
5610
5611 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005612 weston_keyboard_end_grab(grab->keyboard);
5613 if (grab->keyboard->input_method_resource)
5614 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005615 free(db);
5616 }
5617}
5618
5619static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005620debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005621 uint32_t mods_depressed, uint32_t mods_latched,
5622 uint32_t mods_locked, uint32_t group)
5623{
5624 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005625 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005626
Neil Roberts96d790e2013-09-19 17:32:00 +01005627 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005628
Neil Roberts96d790e2013-09-19 17:32:00 +01005629 wl_resource_for_each(resource, resource_list) {
5630 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5631 mods_latched, mods_locked, group);
5632 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005633}
5634
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005635static void
5636debug_binding_cancel(struct weston_keyboard_grab *grab)
5637{
5638 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5639
5640 weston_keyboard_end_grab(grab->keyboard);
5641 free(db);
5642}
5643
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005644struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005645 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005646 debug_binding_modifiers,
5647 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005648};
5649
5650static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005651debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005652{
5653 struct debug_binding_grab *grab;
5654
5655 grab = calloc(1, sizeof *grab);
5656 if (!grab)
5657 return;
5658
5659 grab->seat = (struct weston_seat *) seat;
5660 grab->key[0] = key;
5661 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005662 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005663}
5664
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005665static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005666force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005667 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005668{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005669 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005670 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005671 struct desktop_shell *shell = data;
5672 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005673 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005674
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005675 focus_surface = seat->keyboard->focus;
5676 if (!focus_surface)
5677 return;
5678
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005679 wl_signal_emit(&compositor->kill_signal, focus_surface);
5680
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005681 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005682 wl_client_get_credentials(client, &pid, NULL, NULL);
5683
5684 /* Skip clients that we launched ourselves (the credentials of
5685 * the socketpair is ours) */
5686 if (pid == getpid())
5687 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005688
Daniel Stone325fc2d2012-05-30 16:31:58 +01005689 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005690}
5691
5692static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005693workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005694 uint32_t key, void *data)
5695{
5696 struct desktop_shell *shell = data;
5697 unsigned int new_index = shell->workspaces.current;
5698
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005699 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005700 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005701 if (new_index != 0)
5702 new_index--;
5703
5704 change_workspace(shell, new_index);
5705}
5706
5707static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005708workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005709 uint32_t key, void *data)
5710{
5711 struct desktop_shell *shell = data;
5712 unsigned int new_index = shell->workspaces.current;
5713
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005714 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005715 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005716 if (new_index < shell->workspaces.num - 1)
5717 new_index++;
5718
5719 change_workspace(shell, new_index);
5720}
5721
5722static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005723workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005724 uint32_t key, void *data)
5725{
5726 struct desktop_shell *shell = data;
5727 unsigned int new_index;
5728
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005729 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005730 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005731 new_index = key - KEY_F1;
5732 if (new_index >= shell->workspaces.num)
5733 new_index = shell->workspaces.num - 1;
5734
5735 change_workspace(shell, new_index);
5736}
5737
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005738static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005739workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005740 uint32_t key, void *data)
5741{
5742 struct desktop_shell *shell = data;
5743 unsigned int new_index = shell->workspaces.current;
5744
5745 if (shell->locked)
5746 return;
5747
5748 if (new_index != 0)
5749 new_index--;
5750
5751 take_surface_to_workspace_by_seat(shell, seat, new_index);
5752}
5753
5754static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005755workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005756 uint32_t key, void *data)
5757{
5758 struct desktop_shell *shell = data;
5759 unsigned int new_index = shell->workspaces.current;
5760
5761 if (shell->locked)
5762 return;
5763
5764 if (new_index < shell->workspaces.num - 1)
5765 new_index++;
5766
5767 take_surface_to_workspace_by_seat(shell, seat, new_index);
5768}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005769
5770static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005771shell_reposition_view_on_output_destroy(struct weston_view *view)
5772{
5773 struct weston_output *output, *first_output;
5774 struct weston_compositor *ec = view->surface->compositor;
5775 struct shell_surface *shsurf;
5776 float x, y;
5777 int visible;
5778
5779 x = view->geometry.x;
5780 y = view->geometry.y;
5781
5782 /* At this point the destroyed output is not in the list anymore.
5783 * If the view is still visible somewhere, we leave where it is,
5784 * otherwise, move it to the first output. */
5785 visible = 0;
5786 wl_list_for_each(output, &ec->output_list, link) {
5787 if (pixman_region32_contains_point(&output->region,
5788 x, y, NULL)) {
5789 visible = 1;
5790 break;
5791 }
5792 }
5793
5794 if (!visible) {
5795 first_output = container_of(ec->output_list.next,
5796 struct weston_output, link);
5797
5798 x = first_output->x + first_output->width / 4;
5799 y = first_output->y + first_output->height / 4;
5800 }
5801
5802 weston_view_set_position(view, x, y);
5803
5804 shsurf = get_shell_surface(view->surface);
5805
5806 if (shsurf) {
5807 shsurf->saved_position_valid = false;
5808 shsurf->next_state.maximized = false;
5809 shsurf->next_state.fullscreen = false;
5810 shsurf->state_changed = true;
5811 }
5812}
5813
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005814void
5815shell_for_each_layer(struct desktop_shell *shell,
5816 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005817{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005818 struct workspace **ws;
5819
5820 func(shell, &shell->fullscreen_layer, data);
5821 func(shell, &shell->panel_layer, data);
5822 func(shell, &shell->background_layer, data);
5823 func(shell, &shell->lock_layer, data);
5824 func(shell, &shell->input_panel_layer, data);
5825
5826 wl_array_for_each(ws, &shell->workspaces.array)
5827 func(shell, &(*ws)->layer, data);
5828}
5829
5830static void
5831shell_output_destroy_move_layer(struct desktop_shell *shell,
5832 struct weston_layer *layer,
5833 void *data)
5834{
5835 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005836 struct weston_view *view;
5837
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005838 wl_list_for_each(view, &layer->view_list, layer_link) {
5839 if (view->output != output)
5840 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005841
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005842 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005843 }
5844}
5845
5846static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005847handle_output_destroy(struct wl_listener *listener, void *data)
5848{
5849 struct shell_output *output_listener =
5850 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005851 struct weston_output *output = output_listener->output;
5852 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005853
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005854 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005855
Xiong Zhang6b481422013-10-23 13:58:32 +08005856 wl_list_remove(&output_listener->destroy_listener.link);
5857 wl_list_remove(&output_listener->link);
5858 free(output_listener);
5859}
5860
5861static void
5862create_shell_output(struct desktop_shell *shell,
5863 struct weston_output *output)
5864{
5865 struct shell_output *shell_output;
5866
5867 shell_output = zalloc(sizeof *shell_output);
5868 if (shell_output == NULL)
5869 return;
5870
5871 shell_output->output = output;
5872 shell_output->shell = shell;
5873 shell_output->destroy_listener.notify = handle_output_destroy;
5874 wl_signal_add(&output->destroy_signal,
5875 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005876 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005877}
5878
5879static void
5880handle_output_create(struct wl_listener *listener, void *data)
5881{
5882 struct desktop_shell *shell =
5883 container_of(listener, struct desktop_shell, output_create_listener);
5884 struct weston_output *output = (struct weston_output *)data;
5885
5886 create_shell_output(shell, output);
5887}
5888
5889static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005890handle_output_move_layer(struct desktop_shell *shell,
5891 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005892{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005893 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005894 struct weston_view *view;
5895 float x, y;
5896
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005897 wl_list_for_each(view, &layer->view_list, layer_link) {
5898 if (view->output != output)
5899 continue;
5900
5901 x = view->geometry.x + output->move_x;
5902 y = view->geometry.y + output->move_y;
5903 weston_view_set_position(view, x, y);
5904 }
5905}
5906
5907static void
5908handle_output_move(struct wl_listener *listener, void *data)
5909{
5910 struct desktop_shell *shell;
5911
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005912 shell = container_of(listener, struct desktop_shell,
5913 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005914
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005915 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005916}
5917
5918static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005919setup_output_destroy_handler(struct weston_compositor *ec,
5920 struct desktop_shell *shell)
5921{
5922 struct weston_output *output;
5923
5924 wl_list_init(&shell->output_list);
5925 wl_list_for_each(output, &ec->output_list, link)
5926 create_shell_output(shell, output);
5927
5928 shell->output_create_listener.notify = handle_output_create;
5929 wl_signal_add(&ec->output_created_signal,
5930 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005931
5932 shell->output_move_listener.notify = handle_output_move;
5933 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005934}
5935
5936static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005937shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005938{
Tiago Vignattibe143262012-04-16 17:31:41 +03005939 struct desktop_shell *shell =
5940 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005941 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005942 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005943
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005944 /* Force state to unlocked so we don't try to fade */
5945 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005946 if (shell->child.client)
5947 wl_client_destroy(shell->child.client);
5948
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005949 wl_list_remove(&shell->idle_listener.link);
5950 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005951
5952 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005953
Xiong Zhang6b481422013-10-23 13:58:32 +08005954 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5955 wl_list_remove(&shell_output->destroy_listener.link);
5956 wl_list_remove(&shell_output->link);
5957 free(shell_output);
5958 }
5959
5960 wl_list_remove(&shell->output_create_listener.link);
5961
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005962 wl_array_for_each(ws, &shell->workspaces.array)
5963 workspace_destroy(*ws);
5964 wl_array_release(&shell->workspaces.array);
5965
Pekka Paalanen3c647232011-12-22 13:43:43 +02005966 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005967 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005968 free(shell);
5969}
5970
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005971static void
5972shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5973{
5974 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005975 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005976
5977 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005978 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5979 MODIFIER_CTRL | MODIFIER_ALT,
5980 terminate_binding, ec);
5981 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5982 click_to_activate_binding,
5983 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005984 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5985 click_to_activate_binding,
5986 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005987 weston_compositor_add_touch_binding(ec, 0,
5988 touch_to_activate_binding,
5989 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005990 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5991 MODIFIER_SUPER | MODIFIER_ALT,
5992 surface_opacity_binding, NULL);
5993 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5994 MODIFIER_SUPER, zoom_axis_binding,
5995 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005996
5997 /* configurable bindings */
5998 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005999 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6000 zoom_key_binding, NULL);
6001 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6002 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006003 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6004 maximize_binding, NULL);
6005 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6006 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006007 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6008 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006009 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006010 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6011 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006012 weston_compositor_add_button_binding(ec, BTN_LEFT,
6013 mod | MODIFIER_SHIFT,
6014 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006015
6016 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6017 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6018 rotate_binding, NULL);
6019
Daniel Stone325fc2d2012-05-30 16:31:58 +01006020 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6021 shell);
6022 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6023 ec);
6024 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6025 backlight_binding, ec);
6026 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6027 ec);
6028 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6029 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006030 weston_compositor_add_key_binding(ec, KEY_K, mod,
6031 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006032 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6033 workspace_up_binding, shell);
6034 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6035 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006036 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6037 workspace_move_surface_up_binding,
6038 shell);
6039 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6040 workspace_move_surface_down_binding,
6041 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006042
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006043 if (shell->exposay_modifier)
6044 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6045 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006046
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006047 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6048 if (shell->workspaces.num > 1) {
6049 num_workspace_bindings = shell->workspaces.num;
6050 if (num_workspace_bindings > 6)
6051 num_workspace_bindings = 6;
6052 for (i = 0; i < num_workspace_bindings; i++)
6053 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6054 workspace_f_binding,
6055 shell);
6056 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006057
6058 /* Debug bindings */
6059 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6060 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006061}
6062
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006063WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006064module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006065 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006066{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006067 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006068 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006069 struct workspace **pws;
6070 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006071 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006072
Peter Huttererf3d62272013-08-08 11:57:05 +10006073 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006074 if (shell == NULL)
6075 return -1;
6076
Kristian Høgsberg75840622011-09-06 13:48:16 -04006077 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006078
6079 shell->destroy_listener.notify = shell_destroy;
6080 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006081 shell->idle_listener.notify = idle_handler;
6082 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6083 shell->wake_listener.notify = wake_handler;
6084 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006085
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006086 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006087 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006088 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006089 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006090 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006091 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006092 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006093 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006094 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006095 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006096
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006097 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6098 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006099 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6100 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006101 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006102
6103 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006104 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006105
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006106 if (input_panel_setup(shell) < 0)
6107 return -1;
6108
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006109 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006110
Daniel Stonedf8133b2013-11-19 11:37:14 +01006111 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6112 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6113
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006114 for (i = 0; i < shell->workspaces.num; i++) {
6115 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6116 if (pws == NULL)
6117 return -1;
6118
6119 *pws = workspace_create();
6120 if (*pws == NULL)
6121 return -1;
6122 }
6123 activate_workspace(shell, 0);
6124
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006125 weston_layer_init(&shell->minimized_layer, NULL);
6126
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006127 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006128 wl_list_init(&shell->workspaces.animation.link);
6129 shell->workspaces.animation.frame = animate_workspace_change_frame;
6130
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006131 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006132 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006133 return -1;
6134
Rafael Antognollie2a34552013-12-03 15:35:45 -02006135 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6136 shell, bind_xdg_shell) == NULL)
6137 return -1;
6138
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006139 if (wl_global_create(ec->wl_display,
6140 &desktop_shell_interface, 2,
6141 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006142 return -1;
6143
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006144 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6145 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006146 return -1;
6147
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006148 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6149 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006150 return -1;
6151
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006152 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006153
Xiong Zhang6b481422013-10-23 13:58:32 +08006154 setup_output_destroy_handler(ec, shell);
6155
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006156 loop = wl_display_get_event_loop(ec->wl_display);
6157 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006158
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006159 shell->screensaver.timer =
6160 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6161
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006162 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006163 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006164 create_keyboard_focus_listener(seat);
6165 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006166
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006167 screenshooter_create(ec);
6168
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006169 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006170
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006171 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006172
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006173 return 0;
6174}