blob: 574681fd7af2b396db4f3673809805003f50812d [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
Jason Ekstrand024177c2014-04-21 19:42:58 -0500220 struct wl_listener caps_changed_listener;
221 struct wl_listener pointer_focus_listener;
222 struct wl_listener keyboard_focus_listener;
223
Giulio Camuffo5085a752013-03-25 21:42:45 +0100224 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400225 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100226 struct wl_list surfaces_list;
227 struct wl_client *client;
228 int32_t initial_up;
229 } popup_grab;
230};
231
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800232struct shell_client {
233 struct wl_resource *resource;
234 struct wl_client *client;
235 struct desktop_shell *shell;
236 struct wl_listener destroy_listener;
237 struct wl_event_source *ping_timer;
238 uint32_t ping_serial;
239 int unresponsive;
240};
241
Alex Wubd3354b2012-04-17 17:20:49 +0800242static struct desktop_shell *
243shell_surface_get_shell(struct shell_surface *shsurf);
244
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500245static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400246surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500247
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300248static void
249shell_fade_startup(struct desktop_shell *shell);
250
Philip Withnallbecb77e2013-11-25 18:01:30 +0000251static struct shell_seat *
252get_shell_seat(struct weston_seat *seat);
253
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700254static int
255get_output_panel_height(struct desktop_shell *shell,
256 struct weston_output *output);
257
Philip Withnall648a4dd2013-11-25 18:01:44 +0000258static void
259shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
260
Alex Wubd3354b2012-04-17 17:20:49 +0800261static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200262shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
263
264static bool
265shell_surface_is_xdg_surface(struct shell_surface *shsurf);
266
267static bool
268shell_surface_is_xdg_popup(struct shell_surface *shsurf);
269
270static void
271shell_surface_set_parent(struct shell_surface *shsurf,
272 struct weston_surface *parent);
273
274static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800275shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
276{
277 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800279
280 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100281
Jason Ekstranda7af7042013-10-12 22:38:11 -0500282 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800283 return false;
284
Jason Ekstranda7af7042013-10-12 22:38:11 -0500285 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
286 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800287 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500288 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800289}
290
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500291static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400292destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300293{
294 struct shell_grab *grab;
295
296 grab = container_of(listener, struct shell_grab,
297 shsurf_destroy_listener);
298
299 grab->shsurf = NULL;
300}
301
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800302struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500303get_default_view(struct weston_surface *surface)
304{
305 struct shell_surface *shsurf;
306 struct weston_view *view;
307
308 if (!surface || wl_list_empty(&surface->views))
309 return NULL;
310
311 shsurf = get_shell_surface(surface);
312 if (shsurf)
313 return shsurf->view;
314
315 wl_list_for_each(view, &surface->views, surface_link)
316 if (weston_view_is_mapped(view))
317 return view;
318
319 return container_of(surface->views.next, struct weston_view, surface_link);
320}
321
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400323popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400324
325static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300326shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400327 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300328 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400329 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300330 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300331{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300332 struct desktop_shell *shell = shsurf->shell;
333
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400334 popup_grab_end(pointer);
335
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300336 grab->grab.interface = interface;
337 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400338 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500339 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400340 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700342 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400343 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400344 if (shell->child.desktop_shell) {
345 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
346 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500347 weston_pointer_set_focus(pointer,
348 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400349 wl_fixed_from_int(0),
350 wl_fixed_from_int(0));
351 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300352}
353
354static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300355shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300356{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700357 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400358 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700359 grab->shsurf->grabbed = 0;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800360 grab->shsurf->resize_edges = 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700361 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300362
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700363 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300364}
365
366static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700367shell_touch_grab_start(struct shell_touch_grab *grab,
368 const struct weston_touch_grab_interface *interface,
369 struct shell_surface *shsurf,
370 struct weston_touch *touch)
371{
372 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800373
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700374 if (touch->seat->pointer)
375 popup_grab_end(touch->seat->pointer);
376
Rusty Lynch1084da52013-08-15 09:10:08 -0700377 grab->grab.interface = interface;
378 grab->shsurf = shsurf;
379 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
380 wl_signal_add(&shsurf->destroy_signal,
381 &grab->shsurf_destroy_listener);
382
383 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700384 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700385
386 weston_touch_start_grab(touch, &grab->grab);
387 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500388 weston_touch_set_focus(touch->seat,
389 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700390}
391
392static void
393shell_touch_grab_end(struct shell_touch_grab *grab)
394{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700395 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700396 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700397 grab->shsurf->grabbed = 0;
398 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700399
400 weston_touch_end_grab(grab->touch);
401}
402
403static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500404center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800405 struct weston_output *output);
406
Daniel Stone496ca172012-05-30 16:31:42 +0100407static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300408get_modifier(char *modifier)
409{
410 if (!modifier)
411 return MODIFIER_SUPER;
412
413 if (!strcmp("ctrl", modifier))
414 return MODIFIER_CTRL;
415 else if (!strcmp("alt", modifier))
416 return MODIFIER_ALT;
417 else if (!strcmp("super", modifier))
418 return MODIFIER_SUPER;
419 else
420 return MODIFIER_SUPER;
421}
422
Juan Zhaoe10d2792012-04-25 19:09:52 +0800423static enum animation_type
424get_animation_type(char *animation)
425{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800426 if (!animation)
427 return ANIMATION_NONE;
428
Juan Zhaoe10d2792012-04-25 19:09:52 +0800429 if (!strcmp("zoom", animation))
430 return ANIMATION_ZOOM;
431 else if (!strcmp("fade", animation))
432 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100433 else if (!strcmp("dim-layer", animation))
434 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800435 else
436 return ANIMATION_NONE;
437}
438
Alex Wu4539b082012-03-01 12:57:46 +0800439static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400440shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200441{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400442 struct weston_config_section *section;
443 int duration;
444 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200445
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400446 section = weston_config_get_section(shell->compositor->config,
447 "screensaver", NULL, NULL);
448 weston_config_section_get_string(section,
449 "path", &shell->screensaver.path, NULL);
450 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200451 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400452
453 section = weston_config_get_section(shell->compositor->config,
454 "shell", NULL, NULL);
455 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100456 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100457 shell->client = s;
458 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400459 "binding-modifier", &s, "super");
460 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200461 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800462
463 weston_config_section_get_string(section,
464 "exposay-modifier", &s, "none");
465 if (strcmp(s, "none") == 0)
466 shell->exposay_modifier = 0;
467 else
468 shell->exposay_modifier = get_modifier(s);
469 free(s);
470
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400471 weston_config_section_get_string(section, "animation", &s, "none");
472 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200473 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700474 weston_config_section_get_string(section,
475 "startup-animation", &s, "fade");
476 shell->startup_animation_type = get_animation_type(s);
477 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700478 if (shell->startup_animation_type == ANIMATION_ZOOM)
479 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100480 weston_config_section_get_string(section, "focus-animation", &s, "none");
481 shell->focus_animation_type = get_animation_type(s);
482 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400483 weston_config_section_get_uint(section, "num-workspaces",
484 &shell->workspaces.num,
485 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200486}
487
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800488struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100489get_default_output(struct weston_compositor *compositor)
490{
491 return container_of(compositor->output_list.next,
492 struct weston_output, link);
493}
494
495
496/* no-op func for checking focus surface */
497static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600498focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100499{
500}
501
502static struct focus_surface *
503get_focus_surface(struct weston_surface *surface)
504{
505 if (surface->configure == focus_surface_configure)
506 return surface->configure_private;
507 else
508 return NULL;
509}
510
511static bool
512is_focus_surface (struct weston_surface *es)
513{
514 return (es->configure == focus_surface_configure);
515}
516
517static bool
518is_focus_view (struct weston_view *view)
519{
520 return is_focus_surface (view->surface);
521}
522
523static struct focus_surface *
524create_focus_surface(struct weston_compositor *ec,
525 struct weston_output *output)
526{
527 struct focus_surface *fsurf = NULL;
528 struct weston_surface *surface = NULL;
529
530 fsurf = malloc(sizeof *fsurf);
531 if (!fsurf)
532 return NULL;
533
534 fsurf->surface = weston_surface_create(ec);
535 surface = fsurf->surface;
536 if (surface == NULL) {
537 free(fsurf);
538 return NULL;
539 }
540
541 surface->configure = focus_surface_configure;
542 surface->output = output;
543 surface->configure_private = fsurf;
544
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800545 fsurf->view = weston_view_create(surface);
546 if (fsurf->view == NULL) {
547 weston_surface_destroy(surface);
548 free(fsurf);
549 return NULL;
550 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100551 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100552
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600553 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600554 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100555 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
556 pixman_region32_fini(&surface->opaque);
557 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
558 output->width, output->height);
559 pixman_region32_fini(&surface->input);
560 pixman_region32_init(&surface->input);
561
562 wl_list_init(&fsurf->workspace_transform.link);
563
564 return fsurf;
565}
566
567static void
568focus_surface_destroy(struct focus_surface *fsurf)
569{
570 weston_surface_destroy(fsurf->surface);
571 free(fsurf);
572}
573
574static void
575focus_animation_done(struct weston_view_animation *animation, void *data)
576{
577 struct workspace *ws = data;
578
579 ws->focus_animation = NULL;
580}
581
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200582static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200583focus_state_destroy(struct focus_state *state)
584{
585 wl_list_remove(&state->seat_destroy_listener.link);
586 wl_list_remove(&state->surface_destroy_listener.link);
587 free(state);
588}
589
590static void
591focus_state_seat_destroy(struct wl_listener *listener, void *data)
592{
593 struct focus_state *state = container_of(listener,
594 struct focus_state,
595 seat_destroy_listener);
596
597 wl_list_remove(&state->link);
598 focus_state_destroy(state);
599}
600
601static void
602focus_state_surface_destroy(struct wl_listener *listener, void *data)
603{
604 struct focus_state *state = container_of(listener,
605 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400606 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400607 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500608 struct weston_surface *main_surface, *next;
609 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200610
Pekka Paalanen01388e22013-04-25 13:57:44 +0300611 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
612
Kristian Høgsberge3778222012-07-31 17:29:30 -0400613 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500614 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
615 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400616 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100617 if (is_focus_view(view))
618 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400619
Jason Ekstranda7af7042013-10-12 22:38:11 -0500620 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400621 break;
622 }
623
Pekka Paalanen01388e22013-04-25 13:57:44 +0300624 /* if the focus was a sub-surface, activate its main surface */
625 if (main_surface != state->keyboard_focus)
626 next = main_surface;
627
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100628 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400629 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100630 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100631 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400632 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100633 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
634 if (state->ws->focus_animation)
635 weston_view_animation_destroy(state->ws->focus_animation);
636
637 state->ws->focus_animation = weston_fade_run(
638 state->ws->fsurf_front->view,
639 state->ws->fsurf_front->view->alpha, 0.0, 300,
640 focus_animation_done, state->ws);
641 }
642
Kristian Høgsberge3778222012-07-31 17:29:30 -0400643 wl_list_remove(&state->link);
644 focus_state_destroy(state);
645 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200646}
647
648static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400649focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200650{
Jonas Ådahl04769742012-06-13 00:01:24 +0200651 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200652
653 state = malloc(sizeof *state);
654 if (state == NULL)
655 return NULL;
656
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100657 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400658 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200659 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400660 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200661
662 state->seat_destroy_listener.notify = focus_state_seat_destroy;
663 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400664 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200665 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400666 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200667
668 return state;
669}
670
Jonas Ådahl8538b222012-08-29 22:13:03 +0200671static struct focus_state *
672ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
673{
674 struct workspace *ws = get_current_workspace(shell);
675 struct focus_state *state;
676
677 wl_list_for_each(state, &ws->focus_list, link)
678 if (state->seat == seat)
679 break;
680
681 if (&state->link == &ws->focus_list)
682 state = focus_state_create(seat, ws);
683
684 return state;
685}
686
Jonas Ådahl04769742012-06-13 00:01:24 +0200687static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800688focus_state_set_focus(struct focus_state *state,
689 struct weston_surface *surface)
690{
691 if (state->keyboard_focus) {
692 wl_list_remove(&state->surface_destroy_listener.link);
693 wl_list_init(&state->surface_destroy_listener.link);
694 }
695
696 state->keyboard_focus = surface;
697 if (surface)
698 wl_signal_add(&surface->destroy_signal,
699 &state->surface_destroy_listener);
700}
701
702static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400703restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200704{
705 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400706 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100707 struct wl_list pending_seat_list;
708 struct weston_seat *seat, *next_seat;
709
710 /* Temporarily steal the list of seats so that we can keep
711 * track of the seats we've already processed */
712 wl_list_init(&pending_seat_list);
713 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
714 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200715
716 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100717 wl_list_remove(&state->seat->link);
718 wl_list_insert(&shell->compositor->seat_list,
719 &state->seat->link);
720
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800721 if (state->seat->keyboard == NULL)
722 continue;
723
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400724 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200725
Kristian Høgsberge3148752013-05-06 23:19:49 -0400726 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200727 }
Neil Roberts4237f502014-04-09 16:33:31 +0100728
729 /* For any remaining seats that we don't have a focus state
730 * for we'll reset the keyboard focus to NULL */
731 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
732 wl_list_insert(&shell->compositor->seat_list, &seat->link);
733
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300734 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100735 continue;
736
737 weston_keyboard_set_focus(seat->keyboard, NULL);
738 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200739}
740
741static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200742replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
743 struct weston_seat *seat)
744{
745 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200746
747 wl_list_for_each(state, &ws->focus_list, link) {
748 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800749 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200750 return;
751 }
752 }
753}
754
755static void
756drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
757 struct weston_surface *surface)
758{
759 struct focus_state *state;
760
761 wl_list_for_each(state, &ws->focus_list, link)
762 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800763 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200764}
765
766static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100767animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
768 struct weston_view *from, struct weston_view *to)
769{
770 struct weston_output *output;
771 bool focus_surface_created = false;
772
773 /* FIXME: Only support dim animation using two layers */
774 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
775 return;
776
777 output = get_default_output(shell->compositor);
778 if (ws->fsurf_front == NULL && (from || to)) {
779 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800780 if (ws->fsurf_front == NULL)
781 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100782 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800783
784 ws->fsurf_back = create_focus_surface(shell->compositor, output);
785 if (ws->fsurf_back == NULL) {
786 focus_surface_destroy(ws->fsurf_front);
787 return;
788 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100789 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800790
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100791 focus_surface_created = true;
792 } else {
793 wl_list_remove(&ws->fsurf_front->view->layer_link);
794 wl_list_remove(&ws->fsurf_back->view->layer_link);
795 }
796
797 if (ws->focus_animation) {
798 weston_view_animation_destroy(ws->focus_animation);
799 ws->focus_animation = NULL;
800 }
801
802 if (to)
803 wl_list_insert(&to->layer_link,
804 &ws->fsurf_front->view->layer_link);
805 else if (from)
806 wl_list_insert(&ws->layer.view_list,
807 &ws->fsurf_front->view->layer_link);
808
809 if (focus_surface_created) {
810 ws->focus_animation = weston_fade_run(
811 ws->fsurf_front->view,
812 ws->fsurf_front->view->alpha, 0.6, 300,
813 focus_animation_done, ws);
814 } else if (from) {
815 wl_list_insert(&from->layer_link,
816 &ws->fsurf_back->view->layer_link);
817 ws->focus_animation = weston_stable_fade_run(
818 ws->fsurf_front->view, 0.0,
819 ws->fsurf_back->view, 0.6,
820 focus_animation_done, ws);
821 } else if (to) {
822 wl_list_insert(&ws->layer.view_list,
823 &ws->fsurf_back->view->layer_link);
824 ws->focus_animation = weston_stable_fade_run(
825 ws->fsurf_front->view, 0.0,
826 ws->fsurf_back->view, 0.6,
827 focus_animation_done, ws);
828 }
829}
830
831static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200832workspace_destroy(struct workspace *ws)
833{
Jonas Ådahl04769742012-06-13 00:01:24 +0200834 struct focus_state *state, *next;
835
836 wl_list_for_each_safe(state, next, &ws->focus_list, link)
837 focus_state_destroy(state);
838
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100839 if (ws->fsurf_front)
840 focus_surface_destroy(ws->fsurf_front);
841 if (ws->fsurf_back)
842 focus_surface_destroy(ws->fsurf_back);
843
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200844 free(ws);
845}
846
Jonas Ådahl04769742012-06-13 00:01:24 +0200847static void
848seat_destroyed(struct wl_listener *listener, void *data)
849{
850 struct weston_seat *seat = data;
851 struct focus_state *state, *next;
852 struct workspace *ws = container_of(listener,
853 struct workspace,
854 seat_destroyed_listener);
855
856 wl_list_for_each_safe(state, next, &ws->focus_list, link)
857 if (state->seat == seat)
858 wl_list_remove(&state->link);
859}
860
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200861static struct workspace *
862workspace_create(void)
863{
864 struct workspace *ws = malloc(sizeof *ws);
865 if (ws == NULL)
866 return NULL;
867
868 weston_layer_init(&ws->layer, NULL);
869
Jonas Ådahl04769742012-06-13 00:01:24 +0200870 wl_list_init(&ws->focus_list);
871 wl_list_init(&ws->seat_destroyed_listener.link);
872 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100873 ws->fsurf_front = NULL;
874 ws->fsurf_back = NULL;
875 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200876
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200877 return ws;
878}
879
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200880static int
881workspace_is_empty(struct workspace *ws)
882{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200884}
885
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200886static struct workspace *
887get_workspace(struct desktop_shell *shell, unsigned int index)
888{
889 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200890 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200891 pws += index;
892 return *pws;
893}
894
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800895struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200896get_current_workspace(struct desktop_shell *shell)
897{
898 return get_workspace(shell, shell->workspaces.current);
899}
900
901static void
902activate_workspace(struct desktop_shell *shell, unsigned int index)
903{
904 struct workspace *ws;
905
906 ws = get_workspace(shell, index);
907 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
908
909 shell->workspaces.current = index;
910}
911
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912static unsigned int
913get_output_height(struct weston_output *output)
914{
915 return abs(output->region.extents.y1 - output->region.extents.y2);
916}
917
918static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100919view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200920{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200921 struct weston_transform *transform;
922
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100923 if (is_focus_view(view)) {
924 struct focus_surface *fsurf = get_focus_surface(view->surface);
925 transform = &fsurf->workspace_transform;
926 } else {
927 struct shell_surface *shsurf = get_shell_surface(view->surface);
928 transform = &shsurf->workspace_transform;
929 }
930
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200931 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100933 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200934
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100935 weston_matrix_init(&transform->matrix);
936 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200937 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200939}
940
941static void
942workspace_translate_out(struct workspace *ws, double fraction)
943{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200945 unsigned int height;
946 double d;
947
Jason Ekstranda7af7042013-10-12 22:38:11 -0500948 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
949 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200950 d = height * fraction;
951
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100952 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200953 }
954}
955
956static void
957workspace_translate_in(struct workspace *ws, double fraction)
958{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200960 unsigned int height;
961 double d;
962
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
964 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965
966 if (fraction > 0)
967 d = -(height - height * fraction);
968 else
969 d = height + height * fraction;
970
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100971 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200972 }
973}
974
975static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200976broadcast_current_workspace_state(struct desktop_shell *shell)
977{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700978 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200979
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700980 wl_resource_for_each(resource, &shell->workspaces.client_list)
981 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200982 shell->workspaces.current,
983 shell->workspaces.num);
984}
985
986static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200987reverse_workspace_change_animation(struct desktop_shell *shell,
988 unsigned int index,
989 struct workspace *from,
990 struct workspace *to)
991{
992 shell->workspaces.current = index;
993
994 shell->workspaces.anim_to = to;
995 shell->workspaces.anim_from = from;
996 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
997 shell->workspaces.anim_timestamp = 0;
998
Scott Moreau4272e632012-08-13 09:58:41 -0600999 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001000}
1001
1002static void
1003workspace_deactivate_transforms(struct workspace *ws)
1004{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001006 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001009 if (is_focus_view(view)) {
1010 struct focus_surface *fsurf = get_focus_surface(view->surface);
1011 transform = &fsurf->workspace_transform;
1012 } else {
1013 struct shell_surface *shsurf = get_shell_surface(view->surface);
1014 transform = &shsurf->workspace_transform;
1015 }
1016
1017 if (!wl_list_empty(&transform->link)) {
1018 wl_list_remove(&transform->link);
1019 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001020 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001021 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001022 }
1023}
1024
1025static void
1026finish_workspace_change_animation(struct desktop_shell *shell,
1027 struct workspace *from,
1028 struct workspace *to)
1029{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001030 struct weston_view *view;
1031
Scott Moreau4272e632012-08-13 09:58:41 -06001032 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001034 /* Views that extend past the bottom of the output are still
1035 * visible after the workspace animation ends but before its layer
1036 * is hidden. In that case, we need to damage below those views so
1037 * that the screen is properly repainted. */
1038 wl_list_for_each(view, &from->layer.view_list, layer_link)
1039 weston_view_damage_below(view);
1040
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001041 wl_list_remove(&shell->workspaces.animation.link);
1042 workspace_deactivate_transforms(from);
1043 workspace_deactivate_transforms(to);
1044 shell->workspaces.anim_to = NULL;
1045
1046 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1047}
1048
1049static void
1050animate_workspace_change_frame(struct weston_animation *animation,
1051 struct weston_output *output, uint32_t msecs)
1052{
1053 struct desktop_shell *shell =
1054 container_of(animation, struct desktop_shell,
1055 workspaces.animation);
1056 struct workspace *from = shell->workspaces.anim_from;
1057 struct workspace *to = shell->workspaces.anim_to;
1058 uint32_t t;
1059 double x, y;
1060
1061 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1062 finish_workspace_change_animation(shell, from, to);
1063 return;
1064 }
1065
1066 if (shell->workspaces.anim_timestamp == 0) {
1067 if (shell->workspaces.anim_current == 0.0)
1068 shell->workspaces.anim_timestamp = msecs;
1069 else
1070 shell->workspaces.anim_timestamp =
1071 msecs -
1072 /* Invers of movement function 'y' below. */
1073 (asin(1.0 - shell->workspaces.anim_current) *
1074 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1075 M_2_PI);
1076 }
1077
1078 t = msecs - shell->workspaces.anim_timestamp;
1079
1080 /*
1081 * x = [0, π/2]
1082 * y(x) = sin(x)
1083 */
1084 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1085 y = sin(x);
1086
1087 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001088 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001089
1090 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1091 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1092 shell->workspaces.anim_current = y;
1093
Scott Moreau4272e632012-08-13 09:58:41 -06001094 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001095 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001096 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098}
1099
1100static void
1101animate_workspace_change(struct desktop_shell *shell,
1102 unsigned int index,
1103 struct workspace *from,
1104 struct workspace *to)
1105{
1106 struct weston_output *output;
1107
1108 int dir;
1109
1110 if (index > shell->workspaces.current)
1111 dir = -1;
1112 else
1113 dir = 1;
1114
1115 shell->workspaces.current = index;
1116
1117 shell->workspaces.anim_dir = dir;
1118 shell->workspaces.anim_from = from;
1119 shell->workspaces.anim_to = to;
1120 shell->workspaces.anim_current = 0.0;
1121 shell->workspaces.anim_timestamp = 0;
1122
1123 output = container_of(shell->compositor->output_list.next,
1124 struct weston_output, link);
1125 wl_list_insert(&output->animation_list,
1126 &shell->workspaces.animation.link);
1127
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001128 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129
1130 workspace_translate_in(to, 0);
1131
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001132 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001133
Scott Moreau4272e632012-08-13 09:58:41 -06001134 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001135}
1136
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001137static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001138update_workspace(struct desktop_shell *shell, unsigned int index,
1139 struct workspace *from, struct workspace *to)
1140{
1141 shell->workspaces.current = index;
1142 wl_list_insert(&from->layer.link, &to->layer.link);
1143 wl_list_remove(&from->layer.link);
1144}
1145
1146static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001147change_workspace(struct desktop_shell *shell, unsigned int index)
1148{
1149 struct workspace *from;
1150 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001151 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001152
1153 if (index == shell->workspaces.current)
1154 return;
1155
1156 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001158 return;
1159
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001160 from = get_current_workspace(shell);
1161 to = get_workspace(shell, index);
1162
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163 if (shell->workspaces.anim_from == to &&
1164 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001165 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001166 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001167 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001168 return;
1169 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001170
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001171 if (shell->workspaces.anim_to != NULL)
1172 finish_workspace_change_animation(shell,
1173 shell->workspaces.anim_from,
1174 shell->workspaces.anim_to);
1175
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001176 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001177
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001178 if (shell->focus_animation_type != ANIMATION_NONE) {
1179 wl_list_for_each(state, &from->focus_list, link)
1180 if (state->keyboard_focus)
1181 animate_focus_change(shell, from,
1182 get_default_view(state->keyboard_focus), NULL);
1183
1184 wl_list_for_each(state, &to->focus_list, link)
1185 if (state->keyboard_focus)
1186 animate_focus_change(shell, to,
1187 NULL, get_default_view(state->keyboard_focus));
1188 }
1189
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001190 if (workspace_is_empty(to) && workspace_is_empty(from))
1191 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001192 else
1193 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001194
1195 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001196}
1197
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198static bool
1199workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1200{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001202 struct wl_list *e;
1203
1204 if (wl_list_empty(list))
1205 return false;
1206
1207 e = list->next;
1208
1209 if (e->next != list)
1210 return false;
1211
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001213}
1214
1215static void
Philip Withnall659163d2013-11-25 18:01:36 +00001216move_surface_to_workspace(struct desktop_shell *shell,
1217 struct shell_surface *shsurf,
1218 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001219{
1220 struct workspace *from;
1221 struct workspace *to;
1222 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001223 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001224 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001225
1226 if (workspace == shell->workspaces.current)
1227 return;
1228
Philip Withnall659163d2013-11-25 18:01:36 +00001229 view = get_default_view(shsurf->surface);
1230 if (!view)
1231 return;
1232
1233 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1234
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001235 if (workspace >= shell->workspaces.num)
1236 workspace = shell->workspaces.num - 1;
1237
Jonas Ådahle9d22502012-08-29 22:13:01 +02001238 from = get_current_workspace(shell);
1239 to = get_workspace(shell, workspace);
1240
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 wl_list_remove(&view->layer_link);
1242 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001243
Philip Withnall648a4dd2013-11-25 18:01:44 +00001244 shell_surface_update_child_surface_layers(shsurf);
1245
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001247 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1248 if (!seat->keyboard)
1249 continue;
1250
1251 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001253 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001254 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001255
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001257}
1258
1259static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001260take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001261 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001262 unsigned int index)
1263{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001264 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001266 struct shell_surface *shsurf;
1267 struct workspace *from;
1268 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001269 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270
Pekka Paalanen01388e22013-04-25 13:57:44 +03001271 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 view = get_default_view(surface);
1273 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001274 index == shell->workspaces.current ||
1275 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001276 return;
1277
1278 from = get_current_workspace(shell);
1279 to = get_workspace(shell, index);
1280
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 wl_list_remove(&view->layer_link);
1282 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001283
Philip Withnall659163d2013-11-25 18:01:36 +00001284 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001285 if (shsurf != NULL)
1286 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001287
Jonas Ådahle9d22502012-08-29 22:13:01 +02001288 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289 drop_focus_state(shell, from, surface);
1290
1291 if (shell->workspaces.anim_from == to &&
1292 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001293 wl_list_remove(&to->layer.link);
1294 wl_list_insert(from->layer.link.prev, &to->layer.link);
1295
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001296 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001297 broadcast_current_workspace_state(shell);
1298
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001299 return;
1300 }
1301
1302 if (shell->workspaces.anim_to != NULL)
1303 finish_workspace_change_animation(shell,
1304 shell->workspaces.anim_from,
1305 shell->workspaces.anim_to);
1306
1307 if (workspace_is_empty(from) &&
1308 workspace_has_only(to, surface))
1309 update_workspace(shell, index, from, to);
1310 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001311 if (shsurf != NULL &&
1312 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001313 wl_list_insert(&shell->workspaces.anim_sticky_list,
1314 &shsurf->workspace_transform.link);
1315
1316 animate_workspace_change(shell, index, from, to);
1317 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001318
1319 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001320
1321 state = ensure_focus_state(shell, seat);
1322 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001323 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001324}
1325
1326static void
1327workspace_manager_move_surface(struct wl_client *client,
1328 struct wl_resource *resource,
1329 struct wl_resource *surface_resource,
1330 uint32_t workspace)
1331{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001332 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001333 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001334 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001335 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001336 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001337
Pekka Paalanen01388e22013-04-25 13:57:44 +03001338 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001339 shell_surface = get_shell_surface(main_surface);
1340 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001342
1343 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001344}
1345
1346static const struct workspace_manager_interface workspace_manager_implementation = {
1347 workspace_manager_move_surface,
1348};
1349
1350static void
1351unbind_resource(struct wl_resource *resource)
1352{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001353 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001354}
1355
1356static void
1357bind_workspace_manager(struct wl_client *client,
1358 void *data, uint32_t version, uint32_t id)
1359{
1360 struct desktop_shell *shell = data;
1361 struct wl_resource *resource;
1362
Jason Ekstranda85118c2013-06-27 20:17:02 -05001363 resource = wl_resource_create(client,
1364 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001365
1366 if (resource == NULL) {
1367 weston_log("couldn't add workspace manager object");
1368 return;
1369 }
1370
Jason Ekstranda85118c2013-06-27 20:17:02 -05001371 wl_resource_set_implementation(resource,
1372 &workspace_manager_implementation,
1373 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001374 wl_list_insert(&shell->workspaces.client_list,
1375 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001376
1377 workspace_manager_send_state(resource,
1378 shell->workspaces.current,
1379 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001380}
1381
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001382static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001383touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1384 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1385{
1386}
1387
1388static void
1389touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1390{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001391 struct weston_touch_move_grab *move =
1392 (struct weston_touch_move_grab *) container_of(
1393 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001394
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001395 if (touch_id == 0)
1396 move->active = 0;
1397
Jonas Ådahl9484b692013-12-02 22:05:03 +01001398 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001399 shell_touch_grab_end(&move->base);
1400 free(move);
1401 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001402}
1403
1404static void
1405touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1406 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1407{
1408 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1409 struct shell_surface *shsurf = move->base.shsurf;
1410 struct weston_surface *es;
1411 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1412 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1413
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001414 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001415 return;
1416
1417 es = shsurf->surface;
1418
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001419 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001420
1421 weston_compositor_schedule_repaint(es->compositor);
1422}
1423
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001424static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001425touch_move_grab_frame(struct weston_touch_grab *grab)
1426{
1427}
1428
1429static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001430touch_move_grab_cancel(struct weston_touch_grab *grab)
1431{
1432 struct weston_touch_move_grab *move =
1433 (struct weston_touch_move_grab *) container_of(
1434 grab, struct shell_touch_grab, grab);
1435
1436 shell_touch_grab_end(&move->base);
1437 free(move);
1438}
1439
Rusty Lynch1084da52013-08-15 09:10:08 -07001440static const struct weston_touch_grab_interface touch_move_grab_interface = {
1441 touch_move_grab_down,
1442 touch_move_grab_up,
1443 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001444 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001445 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001446};
1447
1448static int
1449surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1450{
1451 struct weston_touch_move_grab *move;
1452
1453 if (!shsurf)
1454 return -1;
1455
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001456 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001457 return 0;
1458
1459 move = malloc(sizeof *move);
1460 if (!move)
1461 return -1;
1462
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001463 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001465 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001467 seat->touch->grab_y;
1468
1469 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1470 seat->touch);
1471
1472 return 0;
1473}
1474
1475static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001476noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001477{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001478}
1479
1480static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001481constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1482{
1483 struct shell_surface *shsurf = move->base.shsurf;
1484 struct weston_pointer *pointer = move->base.grab.pointer;
Kristian Høgsberg3434b332014-04-29 16:38:23 -07001485 int x, y, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001486 const int safety = 50;
1487
1488 x = wl_fixed_to_int(pointer->x + move->dx);
1489 y = wl_fixed_to_int(pointer->y + move->dy);
1490
1491 panel_height = get_output_panel_height(shsurf->shell,
1492 shsurf->surface->output);
1493 bottom = y + shsurf->surface->height - shsurf->margin.bottom;
1494 if (bottom - panel_height < safety)
1495 y = panel_height + safety -
1496 shsurf->surface->height + shsurf->margin.bottom;
1497
1498 if (move->client_initiated &&
1499 y + shsurf->margin.top < panel_height)
1500 y = panel_height - shsurf->margin.top;
1501
1502 *cx = x;
1503 *cy = y;
1504}
1505
1506static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001507move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1508 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001509{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001510 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001511 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001512 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001513 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001514
1515 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001516 if (!shsurf)
1517 return;
1518
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001519 constrain_position(move, &cx, &cy);
1520
1521 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001522
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001524}
1525
1526static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001527move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001528 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001529{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001530 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1531 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001532 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001533 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001534
Daniel Stone4dbadb12012-05-30 16:31:51 +01001535 if (pointer->button_count == 0 &&
1536 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001537 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001538 free(grab);
1539 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001540}
1541
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001542static void
1543move_grab_cancel(struct weston_pointer_grab *grab)
1544{
1545 struct shell_grab *shell_grab =
1546 container_of(grab, struct shell_grab, grab);
1547
1548 shell_grab_end(shell_grab);
1549 free(grab);
1550}
1551
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001552static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001553 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001554 move_grab_motion,
1555 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001556 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001557};
1558
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001559static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001560surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1561 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001562{
1563 struct weston_move_grab *move;
1564
1565 if (!shsurf)
1566 return -1;
1567
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001568 if (shsurf->grabbed ||
1569 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001570 return 0;
1571
1572 move = malloc(sizeof *move);
1573 if (!move)
1574 return -1;
1575
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001577 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001579 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001580 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581
1582 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001583 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584
1585 return 0;
1586}
1587
1588static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001589common_surface_move(struct wl_resource *resource,
1590 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001591{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001592 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001593 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001594 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001595
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001596 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001597 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001598 seat->pointer->button_count > 0 &&
1599 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001601 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001602 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001603 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001604 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001605 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001606 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001608 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001609 (surface_touch_move(shsurf, seat) < 0))
1610 wl_resource_post_no_memory(resource);
1611 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001612}
1613
Rafael Antognollie2a34552013-12-03 15:35:45 -02001614static void
1615shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1616 struct wl_resource *seat_resource, uint32_t serial)
1617{
1618 common_surface_move(resource, seat_resource, serial);
1619}
1620
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621struct weston_resize_grab {
1622 struct shell_grab base;
1623 uint32_t edges;
1624 int32_t width, height;
1625};
1626
1627static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001628resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1629 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001630{
1631 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001632 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633 struct shell_surface *shsurf = resize->base.shsurf;
1634 int32_t width, height;
1635 wl_fixed_t from_x, from_y;
1636 wl_fixed_t to_x, to_y;
1637
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001638 weston_pointer_move(pointer, x, y);
1639
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001640 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001641 return;
1642
Jason Ekstranda7af7042013-10-12 22:38:11 -05001643 weston_view_from_global_fixed(shsurf->view,
1644 pointer->grab_x, pointer->grab_y,
1645 &from_x, &from_y);
1646 weston_view_from_global_fixed(shsurf->view,
1647 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001648
1649 width = resize->width;
1650 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1651 width += wl_fixed_to_int(from_x - to_x);
1652 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1653 width += wl_fixed_to_int(to_x - from_x);
1654 }
1655
1656 height = resize->height;
1657 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1658 height += wl_fixed_to_int(from_y - to_y);
1659 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1660 height += wl_fixed_to_int(to_y - from_y);
1661 }
1662
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001663 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001664}
1665
1666static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001667send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001668{
1669 struct shell_surface *shsurf = get_shell_surface(surface);
1670
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001671 assert(shsurf);
1672
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001673 if (shsurf->resource)
1674 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001675 shsurf->resize_edges,
1676 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677}
1678
1679static const struct weston_shell_client shell_client = {
1680 send_configure
1681};
1682
1683static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001684resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001685 uint32_t time, uint32_t button, uint32_t state_w)
1686{
1687 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001688 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001689 enum wl_pointer_button_state state = state_w;
1690
1691 if (pointer->button_count == 0 &&
1692 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1693 shell_grab_end(&resize->base);
1694 free(grab);
1695 }
1696}
1697
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001698static void
1699resize_grab_cancel(struct weston_pointer_grab *grab)
1700{
1701 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1702
1703 shell_grab_end(&resize->base);
1704 free(grab);
1705}
1706
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001707static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708 noop_grab_focus,
1709 resize_grab_motion,
1710 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001711 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001712};
1713
Giulio Camuffob8366642013-04-25 13:57:46 +03001714/*
1715 * Returns the bounding box of a surface and all its sub-surfaces,
1716 * in the surface coordinates system. */
1717static void
1718surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1719 int32_t *y, int32_t *w, int32_t *h) {
1720 pixman_region32_t region;
1721 pixman_box32_t *box;
1722 struct weston_subsurface *subsurface;
1723
1724 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725 surface->width,
1726 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001727
1728 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1729 pixman_region32_union_rect(&region, &region,
1730 subsurface->position.x,
1731 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001732 subsurface->surface->width,
1733 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001734 }
1735
1736 box = pixman_region32_extents(&region);
1737 if (x)
1738 *x = box->x1;
1739 if (y)
1740 *y = box->y1;
1741 if (w)
1742 *w = box->x2 - box->x1;
1743 if (h)
1744 *h = box->y2 - box->y1;
1745
1746 pixman_region32_fini(&region);
1747}
1748
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001749static int
1750surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001751 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001752{
1753 struct weston_resize_grab *resize;
1754
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001755 if (shsurf->grabbed ||
1756 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001757 return 0;
1758
1759 if (edges == 0 || edges > 15 ||
1760 (edges & 3) == 3 || (edges & 12) == 12)
1761 return 0;
1762
1763 resize = malloc(sizeof *resize);
1764 if (!resize)
1765 return -1;
1766
1767 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001768 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1769 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001770
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001771 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001772 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001773 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774
1775 return 0;
1776}
1777
1778static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001779common_surface_resize(struct wl_resource *resource,
1780 struct wl_resource *seat_resource, uint32_t serial,
1781 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001782{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001783 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001784 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001785 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001786
Kristian Høgsberge3148752013-05-06 23:19:49 -04001787 if (seat->pointer->button_count == 0 ||
1788 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001789 seat->pointer->focus == NULL)
1790 return;
1791
1792 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1793 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001794 return;
1795
Kristian Høgsberge3148752013-05-06 23:19:49 -04001796 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001797 wl_resource_post_no_memory(resource);
1798}
1799
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001800static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001801shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1802 struct wl_resource *seat_resource, uint32_t serial,
1803 uint32_t edges)
1804{
1805 common_surface_resize(resource, seat_resource, serial, edges);
1806}
1807
1808static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001809busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001810{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001811 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001812 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001814 wl_fixed_t sx, sy;
1815
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 view = weston_compositor_pick_view(pointer->seat->compositor,
1817 pointer->x, pointer->y,
1818 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001819
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001820 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1821 shell_grab_end(grab);
1822 free(grab);
1823 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001824}
1825
1826static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001827busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1828 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001829{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001830 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001831}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001832
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001833static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001834busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001835 uint32_t time, uint32_t button, uint32_t state)
1836{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001837 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001838 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001839 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001840
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001841 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001842 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001843 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001844 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001845 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001846 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001847 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001848}
1849
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001850static void
1851busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1852{
1853 struct shell_grab *grab = (struct shell_grab *) base;
1854
1855 shell_grab_end(grab);
1856 free(grab);
1857}
1858
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001859static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001860 busy_cursor_grab_focus,
1861 busy_cursor_grab_motion,
1862 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001863 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001864};
1865
1866static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001867set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001868{
1869 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001870
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001871 if (pointer->grab->interface == &busy_cursor_grab_interface)
1872 return;
1873
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001874 grab = malloc(sizeof *grab);
1875 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001876 return;
1877
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001878 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1879 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001880 /* Mark the shsurf as ungrabbed so that button binding is able
1881 * to move it. */
1882 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001883}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001884
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001885static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001886end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001887{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001888 struct shell_grab *grab;
1889 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001890
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001891 wl_list_for_each(seat, &compositor->seat_list, link) {
1892 if (seat->pointer == NULL)
1893 continue;
1894
1895 grab = (struct shell_grab *) seat->pointer->grab;
1896 if (grab->grab.interface == &busy_cursor_grab_interface &&
1897 wl_resource_get_client(grab->shsurf->resource) == client) {
1898 shell_grab_end(grab);
1899 free(grab);
1900 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001901 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001902}
1903
Scott Moreau9521d5e2012-04-19 13:06:17 -06001904static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001905handle_shell_client_destroy(struct wl_listener *listener, void *data);
1906
1907static int
1908xdg_ping_timeout_handler(void *data)
1909{
1910 struct shell_client *sc = data;
1911 struct weston_seat *seat;
1912 struct shell_surface *shsurf;
1913
1914 /* Client is not responding */
1915 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001916 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1917 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1918 continue;
1919 if (seat->pointer->focus->surface->resource == NULL)
1920 continue;
1921
1922 shsurf = get_shell_surface(seat->pointer->focus->surface);
1923 if (shsurf &&
1924 wl_resource_get_client(shsurf->resource) == sc->client)
1925 set_busy_cursor(shsurf, seat->pointer);
1926 }
1927
1928 return 1;
1929}
1930
1931static void
1932handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1933{
1934 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05001935 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001936 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001937 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001938
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001939 if (sc->unresponsive) {
1940 xdg_ping_timeout_handler(sc);
1941 return;
1942 }
1943
1944 sc->ping_serial = serial;
1945 loop = wl_display_get_event_loop(compositor->wl_display);
1946 if (sc->ping_timer == NULL)
1947 sc->ping_timer =
1948 wl_event_loop_add_timer(loop,
1949 xdg_ping_timeout_handler, sc);
1950 if (sc->ping_timer == NULL)
1951 return;
1952
1953 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1954
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001955 if (shell_surface_is_xdg_surface(shsurf) ||
1956 shell_surface_is_xdg_popup(shsurf))
1957 xdg_shell_send_ping(sc->resource, serial);
1958 else if (shell_surface_is_wl_shell_surface(shsurf))
1959 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001960}
1961
Scott Moreauff1db4a2012-04-17 19:06:18 -06001962static void
1963ping_handler(struct weston_surface *surface, uint32_t serial)
1964{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001965 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001966
1967 if (!shsurf)
1968 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001969 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001970 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001971 if (shsurf->surface == shsurf->shell->grab_surface)
1972 return;
1973
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001974 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001975}
1976
1977static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001978handle_pointer_focus(struct wl_listener *listener, void *data)
1979{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001980 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001981 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001982 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001983 uint32_t serial;
1984
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001986 return;
1987
Jason Ekstranda7af7042013-10-12 22:38:11 -05001988 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001989 serial = wl_display_next_serial(compositor->wl_display);
1990 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001991}
1992
1993static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001994shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1995{
1996 if (--shsurf->focus_count == 0)
1997 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001998 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001999}
2000
2001static void
2002shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2003{
2004 if (shsurf->focus_count++ == 0)
2005 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05002006 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002007}
2008
2009static void
2010handle_keyboard_focus(struct wl_listener *listener, void *data)
2011{
2012 struct weston_keyboard *keyboard = data;
2013 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2014
2015 if (seat->focused_surface) {
2016 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2017 if (shsurf)
2018 shell_surface_lose_keyboard_focus(shsurf);
2019 }
2020
2021 seat->focused_surface = keyboard->focus;
2022
2023 if (seat->focused_surface) {
2024 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2025 if (shsurf)
2026 shell_surface_gain_keyboard_focus(shsurf);
2027 }
2028}
2029
2030static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002031shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002032{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002033 if (sc->ping_serial != serial)
2034 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002035
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002036 sc->unresponsive = 0;
2037 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002038
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002039 if (sc->ping_timer) {
2040 wl_event_source_remove(sc->ping_timer);
2041 sc->ping_timer = NULL;
2042 }
2043
2044}
2045
2046static void
2047shell_surface_pong(struct wl_client *client,
2048 struct wl_resource *resource, uint32_t serial)
2049{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002050 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2051 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002052
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002053 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002054}
2055
2056static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002057set_title(struct shell_surface *shsurf, const char *title)
2058{
2059 free(shsurf->title);
2060 shsurf->title = strdup(title);
2061}
2062
2063static void
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002064set_margin(struct shell_surface *shsurf,
2065 int32_t left, int32_t right, int32_t top, int32_t bottom)
2066{
2067 shsurf->margin.left = left;
2068 shsurf->margin.right = right;
2069 shsurf->margin.top = top;
2070 shsurf->margin.bottom = bottom;
2071}
2072
2073static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002074shell_surface_set_title(struct wl_client *client,
2075 struct wl_resource *resource, const char *title)
2076{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002077 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002078
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002079 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002080}
2081
2082static void
2083shell_surface_set_class(struct wl_client *client,
2084 struct wl_resource *resource, const char *class)
2085{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002086 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002087
2088 free(shsurf->class);
2089 shsurf->class = strdup(class);
2090}
2091
Alex Wu4539b082012-03-01 12:57:46 +08002092static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002093restore_output_mode(struct weston_output *output)
2094{
Hardening57388e42013-09-18 23:56:36 +02002095 if (output->current_mode != output->original_mode ||
2096 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002097 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002098 output->original_mode,
2099 output->original_scale,
2100 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002101}
2102
2103static void
2104restore_all_output_modes(struct weston_compositor *compositor)
2105{
2106 struct weston_output *output;
2107
2108 wl_list_for_each(output, &compositor->output_list, link)
2109 restore_output_mode(output);
2110}
2111
Philip Withnallbecb77e2013-11-25 18:01:30 +00002112static int
2113get_output_panel_height(struct desktop_shell *shell,
2114 struct weston_output *output)
2115{
2116 struct weston_view *view;
2117 int panel_height = 0;
2118
2119 if (!output)
2120 return 0;
2121
2122 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2123 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002124 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002125 break;
2126 }
2127 }
2128
2129 return panel_height;
2130}
2131
Philip Withnall07926d92013-11-25 18:01:40 +00002132/* The surface will be inserted into the list immediately after the link
2133 * returned by this function (i.e. will be stacked immediately above the
2134 * returned link). */
2135static struct wl_list *
2136shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2137{
2138 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002139 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002140
2141 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002142 case SHELL_SURFACE_XWAYLAND:
2143 return &shsurf->shell->fullscreen_layer.view_list;
2144
2145 case SHELL_SURFACE_NONE:
2146 return NULL;
2147
Kristian Høgsbergead52422014-01-01 13:51:52 -08002148 case SHELL_SURFACE_POPUP:
2149 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002150 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002151 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002152 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002153 /* Move the surface to its parent layer so
2154 * that surfaces which are transient for
2155 * fullscreen surfaces don't get hidden by the
2156 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002157
2158 /* TODO: Handle a parent with multiple views */
2159 parent = get_default_view(shsurf->parent);
2160 if (parent)
2161 return parent->layer_link.prev;
2162 }
Philip Withnall07926d92013-11-25 18:01:40 +00002163
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002164 /* Move the surface to a normal workspace layer so that surfaces
2165 * which were previously fullscreen or transient are no longer
2166 * rendered on top. */
2167 ws = get_current_workspace(shsurf->shell);
2168 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002169 }
2170
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002171 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002172}
2173
Philip Withnall648a4dd2013-11-25 18:01:44 +00002174static void
2175shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2176{
2177 struct shell_surface *child;
2178
2179 /* Move the child layers to the same workspace as shsurf. They will be
2180 * stacked above shsurf. */
2181 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2182 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002183 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002184 weston_view_geometry_dirty(child->view);
2185 wl_list_remove(&child->view->layer_link);
2186 wl_list_insert(shsurf->view->layer_link.prev,
2187 &child->view->layer_link);
2188 weston_view_geometry_dirty(child->view);
2189 weston_surface_damage(child->surface);
2190
2191 /* Recurse. We don’t expect this to recurse very far (if
2192 * at all) because that would imply we have transient
2193 * (or popup) children of transient surfaces, which
2194 * would be unusual. */
2195 shell_surface_update_child_surface_layers(child);
2196 }
2197 }
2198}
2199
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002200/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002201 * geometry to ensure the changes are redrawn.
2202 *
2203 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2204 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002205static void
2206shell_surface_update_layer(struct shell_surface *shsurf)
2207{
2208 struct wl_list *new_layer_link;
2209
2210 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2211
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002212 if (new_layer_link == NULL)
2213 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002214 if (new_layer_link == &shsurf->view->layer_link)
2215 return;
2216
2217 weston_view_geometry_dirty(shsurf->view);
2218 wl_list_remove(&shsurf->view->layer_link);
2219 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2220 weston_view_geometry_dirty(shsurf->view);
2221 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002222
2223 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002224}
2225
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002226static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002227shell_surface_set_parent(struct shell_surface *shsurf,
2228 struct weston_surface *parent)
2229{
2230 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002231
2232 wl_list_remove(&shsurf->children_link);
2233 wl_list_init(&shsurf->children_link);
2234
2235 /* Insert into the parent surface’s child list. */
2236 if (parent != NULL) {
2237 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2238 if (parent_shsurf != NULL)
2239 wl_list_insert(&parent_shsurf->children_list,
2240 &shsurf->children_link);
2241 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002242}
2243
2244static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002245shell_surface_set_output(struct shell_surface *shsurf,
2246 struct weston_output *output)
2247{
2248 struct weston_surface *es = shsurf->surface;
2249
2250 /* get the default output, if the client set it as NULL
2251 check whether the ouput is available */
2252 if (output)
2253 shsurf->output = output;
2254 else if (es->output)
2255 shsurf->output = es->output;
2256 else
2257 shsurf->output = get_default_output(es->compositor);
2258}
2259
2260static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002261surface_clear_next_states(struct shell_surface *shsurf)
2262{
2263 shsurf->next_state.maximized = false;
2264 shsurf->next_state.fullscreen = false;
2265
2266 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2267 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2268 shsurf->state_changed = true;
2269}
2270
2271static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002272set_toplevel(struct shell_surface *shsurf)
2273{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002274 shell_surface_set_parent(shsurf, NULL);
2275 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002276 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002277
2278 /* The layer_link is updated in set_surface_type(),
2279 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002280}
2281
2282static void
2283shell_surface_set_toplevel(struct wl_client *client,
2284 struct wl_resource *resource)
2285{
2286 struct shell_surface *surface = wl_resource_get_user_data(resource);
2287
2288 set_toplevel(surface);
2289}
2290
2291static void
2292set_transient(struct shell_surface *shsurf,
2293 struct weston_surface *parent, int x, int y, uint32_t flags)
2294{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002295 assert(parent != NULL);
2296
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002297 shell_surface_set_parent(shsurf, parent);
2298
2299 surface_clear_next_states(shsurf);
2300
Philip Withnallbecb77e2013-11-25 18:01:30 +00002301 shsurf->transient.x = x;
2302 shsurf->transient.y = y;
2303 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002304
Rafael Antognollied207b42013-12-03 15:35:43 -02002305 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002306 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002307 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002308
2309 /* The layer_link is updated in set_surface_type(),
2310 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002311}
2312
2313static void
2314shell_surface_set_transient(struct wl_client *client,
2315 struct wl_resource *resource,
2316 struct wl_resource *parent_resource,
2317 int x, int y, uint32_t flags)
2318{
2319 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2320 struct weston_surface *parent =
2321 wl_resource_get_user_data(parent_resource);
2322
2323 set_transient(shsurf, parent, x, y, flags);
2324}
2325
2326static void
2327set_fullscreen(struct shell_surface *shsurf,
2328 uint32_t method,
2329 uint32_t framerate,
2330 struct weston_output *output)
2331{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002332 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002333
2334 shsurf->fullscreen_output = shsurf->output;
2335 shsurf->fullscreen.type = method;
2336 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002337
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002338 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002339
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002340 shsurf->client->send_configure(shsurf->surface,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002341 shsurf->output->width,
2342 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002343
2344 /* The layer_link is updated in set_surface_type(),
2345 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002346}
2347
2348static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002349weston_view_set_initial_position(struct weston_view *view,
2350 struct desktop_shell *shell);
2351
2352static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002353unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002354{
Philip Withnallf85fe842013-11-25 18:01:37 +00002355 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002356 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2357 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002358 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002359 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002360
Alex Wu4539b082012-03-01 12:57:46 +08002361 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2362 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002363
Alex Wu4539b082012-03-01 12:57:46 +08002364 wl_list_remove(&shsurf->fullscreen.transform.link);
2365 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002366
Jason Ekstranda7af7042013-10-12 22:38:11 -05002367 if (shsurf->fullscreen.black_view)
2368 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2369 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002370
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002371 if (shsurf->saved_position_valid)
2372 weston_view_set_position(shsurf->view,
2373 shsurf->saved_x, shsurf->saved_y);
2374 else
2375 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2376
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002377 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002378 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002379 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002380 shsurf->saved_rotation_valid = false;
2381 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002382
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002383 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002384}
2385
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002386static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002387shell_surface_set_fullscreen(struct wl_client *client,
2388 struct wl_resource *resource,
2389 uint32_t method,
2390 uint32_t framerate,
2391 struct wl_resource *output_resource)
2392{
2393 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2394 struct weston_output *output;
2395
2396 if (output_resource)
2397 output = wl_resource_get_user_data(output_resource);
2398 else
2399 output = NULL;
2400
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002401 shell_surface_set_parent(shsurf, NULL);
2402
Rafael Antognolli03b16592013-12-03 15:35:42 -02002403 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002404 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002405
2406 shsurf->next_state.fullscreen = true;
2407 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002408}
2409
2410static void
2411set_popup(struct shell_surface *shsurf,
2412 struct weston_surface *parent,
2413 struct weston_seat *seat,
2414 uint32_t serial,
2415 int32_t x,
2416 int32_t y)
2417{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002418 assert(parent != NULL);
2419
Philip Withnallbecb77e2013-11-25 18:01:30 +00002420 shsurf->popup.shseat = get_shell_seat(seat);
2421 shsurf->popup.serial = serial;
2422 shsurf->popup.x = x;
2423 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002424
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002425 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002426}
2427
2428static void
2429shell_surface_set_popup(struct wl_client *client,
2430 struct wl_resource *resource,
2431 struct wl_resource *seat_resource,
2432 uint32_t serial,
2433 struct wl_resource *parent_resource,
2434 int32_t x, int32_t y, uint32_t flags)
2435{
2436 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002437 struct weston_surface *parent =
2438 wl_resource_get_user_data(parent_resource);
2439
2440 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002441
Rafael Antognolli03b16592013-12-03 15:35:42 -02002442 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002443 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002444 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002445 wl_resource_get_user_data(seat_resource),
2446 serial, x, y);
2447}
2448
2449static void
2450set_maximized(struct shell_surface *shsurf,
2451 struct weston_output *output)
2452{
2453 struct desktop_shell *shell;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002454 uint32_t panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002455
Philip Withnall352e7ed2013-11-25 18:01:35 +00002456 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002457
2458 shell = shell_surface_get_shell(shsurf);
2459 panel_height = get_output_panel_height(shell, shsurf->output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002460
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002461 shsurf->client->send_configure(shsurf->surface,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002462 shsurf->output->width,
2463 shsurf->output->height - panel_height);
2464
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002465 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002466}
2467
2468static void
2469unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002470{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002471 /* undo all maximized things here */
2472 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002473
2474 if (shsurf->saved_position_valid)
2475 weston_view_set_position(shsurf->view,
2476 shsurf->saved_x, shsurf->saved_y);
2477 else
2478 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002479
2480 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002481 wl_list_insert(&shsurf->view->geometry.transformation_list,
2482 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002483 shsurf->saved_rotation_valid = false;
2484 }
2485
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002486 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002487}
2488
Philip Withnallbecb77e2013-11-25 18:01:30 +00002489static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002490set_minimized(struct weston_surface *surface, uint32_t is_true)
2491{
2492 struct shell_surface *shsurf;
2493 struct workspace *current_ws;
2494 struct weston_seat *seat;
2495 struct weston_surface *focus;
2496 struct weston_view *view;
2497
2498 view = get_default_view(surface);
2499 if (!view)
2500 return;
2501
2502 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2503
2504 shsurf = get_shell_surface(surface);
2505 current_ws = get_current_workspace(shsurf->shell);
2506
2507 wl_list_remove(&view->layer_link);
2508 /* hide or show, depending on the state */
2509 if (is_true) {
2510 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2511
2512 drop_focus_state(shsurf->shell, current_ws, view->surface);
2513 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2514 if (!seat->keyboard)
2515 continue;
2516 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2517 if (focus == view->surface)
2518 weston_keyboard_set_focus(seat->keyboard, NULL);
2519 }
2520 }
2521 else {
2522 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2523
2524 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2525 if (!seat->keyboard)
2526 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002527 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002528 }
2529 }
2530
2531 shell_surface_update_child_surface_layers(shsurf);
2532
2533 weston_view_damage_below(view);
2534}
2535
2536static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002537shell_surface_set_maximized(struct wl_client *client,
2538 struct wl_resource *resource,
2539 struct wl_resource *output_resource)
2540{
2541 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2542 struct weston_output *output;
2543
2544 if (output_resource)
2545 output = wl_resource_get_user_data(output_resource);
2546 else
2547 output = NULL;
2548
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002549 shell_surface_set_parent(shsurf, NULL);
2550
Rafael Antognolli03b16592013-12-03 15:35:42 -02002551 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002552 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002553
2554 shsurf->next_state.maximized = true;
2555 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002556}
2557
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002558/* This is only ever called from set_surface_type(), so there’s no need to
2559 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002560static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002561reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002562{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002563 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002564 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002565 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002566 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002567
Pekka Paalanen98262232011-12-01 10:42:22 +02002568 return 0;
2569}
2570
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002571static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002572set_full_output(struct shell_surface *shsurf)
2573{
2574 shsurf->saved_x = shsurf->view->geometry.x;
2575 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002576 shsurf->saved_width = shsurf->surface->width;
2577 shsurf->saved_height = shsurf->surface->height;
2578 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002579 shsurf->saved_position_valid = true;
2580
2581 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2582 wl_list_remove(&shsurf->rotation.transform.link);
2583 wl_list_init(&shsurf->rotation.transform.link);
2584 weston_view_geometry_dirty(shsurf->view);
2585 shsurf->saved_rotation_valid = true;
2586 }
2587}
2588
2589static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002590set_surface_type(struct shell_surface *shsurf)
2591{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002592 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002594
Philip Withnallbecb77e2013-11-25 18:01:30 +00002595 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002596
Rafael Antognolli03b16592013-12-03 15:35:42 -02002597 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002598 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002599
2600 switch (shsurf->type) {
2601 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002602 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002603 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002604 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002605 weston_view_set_position(shsurf->view,
2606 pev->geometry.x + shsurf->transient.x,
2607 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002608 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002609 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002610
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002611 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002612 weston_view_set_position(shsurf->view, shsurf->transient.x,
2613 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002614 break;
2615
Philip Withnall0f640e22013-11-25 18:01:31 +00002616 case SHELL_SURFACE_POPUP:
2617 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002618 default:
2619 break;
2620 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002621
2622 /* Update the surface’s layer. */
2623 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002624}
2625
Tiago Vignattibe143262012-04-16 17:31:41 +03002626static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002627shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002628{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002629 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002630}
2631
Alex Wu21858432012-04-01 20:13:08 +08002632static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002633black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002634
Jason Ekstranda7af7042013-10-12 22:38:11 -05002635static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002636create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002637 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002638 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002639{
2640 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002641 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002642
2643 surface = weston_surface_create(ec);
2644 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002645 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002646 return NULL;
2647 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002648 view = weston_view_create(surface);
2649 if (surface == NULL) {
2650 weston_log("no memory\n");
2651 weston_surface_destroy(surface);
2652 return NULL;
2653 }
Alex Wu4539b082012-03-01 12:57:46 +08002654
Alex Wu21858432012-04-01 20:13:08 +08002655 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002656 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002657 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002658 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002659 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002660 pixman_region32_fini(&surface->input);
2661 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002662
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002663 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002664 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002665
2666 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002667}
2668
Philip Withnalled8f1a92013-11-25 18:01:43 +00002669static void
2670shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2671{
2672 struct weston_output *output = shsurf->fullscreen_output;
2673
Rafael Antognolli03b16592013-12-03 15:35:42 -02002674 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002675
2676 if (!shsurf->fullscreen.black_view)
2677 shsurf->fullscreen.black_view =
2678 create_black_surface(shsurf->surface->compositor,
2679 shsurf->surface,
2680 output->x, output->y,
2681 output->width,
2682 output->height);
2683
2684 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2685 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2686 wl_list_insert(&shsurf->view->layer_link,
2687 &shsurf->fullscreen.black_view->layer_link);
2688 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2689 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002690
2691 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002692}
2693
Alex Wu4539b082012-03-01 12:57:46 +08002694/* Create black surface and append it to the associated fullscreen surface.
2695 * Handle size dismatch and positioning according to the method. */
2696static void
2697shell_configure_fullscreen(struct shell_surface *shsurf)
2698{
2699 struct weston_output *output = shsurf->fullscreen_output;
2700 struct weston_surface *surface = shsurf->surface;
2701 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002702 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002703 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002704
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002705 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2706 restore_output_mode(output);
2707
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002708 /* Reverse the effect of lower_fullscreen_layer() */
2709 wl_list_remove(&shsurf->view->layer_link);
2710 wl_list_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2711
Philip Withnalled8f1a92013-11-25 18:01:43 +00002712 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002713
Jason Ekstranda7af7042013-10-12 22:38:11 -05002714 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002715 &surf_width, &surf_height);
2716
Alex Wu4539b082012-03-01 12:57:46 +08002717 switch (shsurf->fullscreen.type) {
2718 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002719 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002720 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002721 break;
2722 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002723 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002724 if (output->width == surf_width &&
2725 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002726 weston_view_set_position(shsurf->view,
2727 output->x - surf_x,
2728 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002729 break;
2730 }
2731
Alex Wu4539b082012-03-01 12:57:46 +08002732 matrix = &shsurf->fullscreen.transform.matrix;
2733 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002734
Scott Moreau1bad5db2012-08-18 01:04:05 -06002735 output_aspect = (float) output->width /
2736 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002737 /* XXX: Use surf_width and surf_height here? */
2738 surface_aspect = (float) surface->width /
2739 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002740 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002741 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002742 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002743 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002744 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002745 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002746
Alex Wu4539b082012-03-01 12:57:46 +08002747 weston_matrix_scale(matrix, scale, scale, 1);
2748 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002749 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002750 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002751 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2752 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002753 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002754
Alex Wu4539b082012-03-01 12:57:46 +08002755 break;
2756 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002757 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002758 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002759 surf_width * surface->buffer_viewport.buffer.scale,
2760 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002761 shsurf->fullscreen.framerate};
2762
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002763 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002764 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002765 weston_view_set_position(shsurf->view,
2766 output->x - surf_x,
2767 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002768 shsurf->fullscreen.black_view->surface->width = output->width;
2769 shsurf->fullscreen.black_view->surface->height = output->height;
2770 weston_view_set_position(shsurf->fullscreen.black_view,
2771 output->x - surf_x,
2772 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002773 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002774 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002775 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002776 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002777 }
Alex Wubd3354b2012-04-17 17:20:49 +08002778 }
Alex Wu4539b082012-03-01 12:57:46 +08002779 break;
2780 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002781 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002782 break;
2783 default:
2784 break;
2785 }
2786}
2787
Alex Wu4539b082012-03-01 12:57:46 +08002788static void
2789shell_map_fullscreen(struct shell_surface *shsurf)
2790{
Alex Wubd3354b2012-04-17 17:20:49 +08002791 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002792}
2793
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002794static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002795set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2796{
2797 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002798 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002799 shsurf->transient.x = x;
2800 shsurf->transient.y = y;
2801 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002802
2803 shell_surface_set_parent(shsurf, NULL);
2804
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002805 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002806 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002807}
2808
Kristian Høgsberg47792412014-05-04 13:47:06 -07002809static void
2810shell_interface_set_fullscreen(struct shell_surface *shsurf,
2811 uint32_t method,
2812 uint32_t framerate,
2813 struct weston_output *output)
2814{
2815 surface_clear_next_states(shsurf);
2816 set_fullscreen(shsurf, method, framerate, output);
2817
2818 shsurf->next_state.fullscreen = true;
2819 shsurf->state_changed = true;
2820}
2821
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002822static int
2823shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2824{
2825 return surface_move(shsurf, ws, 1);
2826}
2827
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002828static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002829
2830static void
2831destroy_shell_seat(struct wl_listener *listener, void *data)
2832{
2833 struct shell_seat *shseat =
2834 container_of(listener,
2835 struct shell_seat, seat_destroy_listener);
2836 struct shell_surface *shsurf, *prev = NULL;
2837
2838 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002839 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002840 shseat->popup_grab.client = NULL;
2841
2842 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2843 shsurf->popup.shseat = NULL;
2844 if (prev) {
2845 wl_list_init(&prev->popup.grab_link);
2846 }
2847 prev = shsurf;
2848 }
2849 wl_list_init(&prev->popup.grab_link);
2850 }
2851
2852 wl_list_remove(&shseat->seat_destroy_listener.link);
2853 free(shseat);
2854}
2855
Jason Ekstrand024177c2014-04-21 19:42:58 -05002856static void
2857shell_seat_caps_changed(struct wl_listener *listener, void *data)
2858{
2859 struct shell_seat *seat;
2860
2861 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2862
2863 if (seat->seat->keyboard &&
2864 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2865 wl_signal_add(&seat->seat->keyboard->focus_signal,
2866 &seat->keyboard_focus_listener);
2867 } else if (!seat->seat->keyboard) {
2868 wl_list_init(&seat->keyboard_focus_listener.link);
2869 }
2870
2871 if (seat->seat->pointer &&
2872 wl_list_empty(&seat->pointer_focus_listener.link)) {
2873 wl_signal_add(&seat->seat->pointer->focus_signal,
2874 &seat->pointer_focus_listener);
2875 } else if (!seat->seat->pointer) {
2876 wl_list_init(&seat->pointer_focus_listener.link);
2877 }
2878}
2879
Giulio Camuffo5085a752013-03-25 21:42:45 +01002880static struct shell_seat *
2881create_shell_seat(struct weston_seat *seat)
2882{
2883 struct shell_seat *shseat;
2884
2885 shseat = calloc(1, sizeof *shseat);
2886 if (!shseat) {
2887 weston_log("no memory to allocate shell seat\n");
2888 return NULL;
2889 }
2890
2891 shseat->seat = seat;
2892 wl_list_init(&shseat->popup_grab.surfaces_list);
2893
2894 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2895 wl_signal_add(&seat->destroy_signal,
2896 &shseat->seat_destroy_listener);
2897
Jason Ekstrand024177c2014-04-21 19:42:58 -05002898 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2899 wl_list_init(&shseat->keyboard_focus_listener.link);
2900
2901 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2902 wl_list_init(&shseat->pointer_focus_listener.link);
2903
2904 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2905 wl_signal_add(&seat->updated_caps_signal,
2906 &shseat->caps_changed_listener);
2907 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2908
Giulio Camuffo5085a752013-03-25 21:42:45 +01002909 return shseat;
2910}
2911
2912static struct shell_seat *
2913get_shell_seat(struct weston_seat *seat)
2914{
2915 struct wl_listener *listener;
2916
2917 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002918 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002919
2920 return container_of(listener,
2921 struct shell_seat, seat_destroy_listener);
2922}
2923
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002924static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002925popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002926{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002927 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002929 struct shell_seat *shseat =
2930 container_of(grab, struct shell_seat, popup_grab.grab);
2931 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002932 wl_fixed_t sx, sy;
2933
Jason Ekstranda7af7042013-10-12 22:38:11 -05002934 view = weston_compositor_pick_view(pointer->seat->compositor,
2935 pointer->x, pointer->y,
2936 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002937
Jason Ekstranda7af7042013-10-12 22:38:11 -05002938 if (view && view->surface->resource &&
2939 wl_resource_get_client(view->surface->resource) == client) {
2940 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002941 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002942 weston_pointer_set_focus(pointer, NULL,
2943 wl_fixed_from_int(0),
2944 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002945 }
2946}
2947
2948static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002949popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2950 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002951{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002952 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002953 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002954 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002955
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002956 weston_pointer_move(pointer, x, y);
2957
Neil Roberts96d790e2013-09-19 17:32:00 +01002958 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002959 weston_view_from_global_fixed(pointer->focus,
2960 pointer->x, pointer->y,
2961 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002962 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002963 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002964}
2965
2966static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002967popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002968 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002969{
2970 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002971 struct shell_seat *shseat =
2972 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002973 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002974 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002975 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002976 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002977
Neil Roberts96d790e2013-09-19 17:32:00 +01002978 resource_list = &grab->pointer->focus_resource_list;
2979 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002980 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002981 wl_resource_for_each(resource, resource_list) {
2982 wl_pointer_send_button(resource, serial,
2983 time, button, state);
2984 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002985 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002986 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002987 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002988 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002989 }
2990
Daniel Stone4dbadb12012-05-30 16:31:51 +01002991 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002992 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002993}
2994
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002995static void
2996popup_grab_cancel(struct weston_pointer_grab *grab)
2997{
2998 popup_grab_end(grab->pointer);
2999}
3000
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003001static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003002 popup_grab_focus,
3003 popup_grab_motion,
3004 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003005 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003006};
3007
3008static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003009shell_surface_send_popup_done(struct shell_surface *shsurf)
3010{
3011 if (shell_surface_is_wl_shell_surface(shsurf))
3012 wl_shell_surface_send_popup_done(shsurf->resource);
3013 else if (shell_surface_is_xdg_popup(shsurf))
3014 xdg_popup_send_popup_done(shsurf->resource,
3015 shsurf->popup.serial);
3016}
3017
3018static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003019popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003020{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003021 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003022 struct shell_seat *shseat =
3023 container_of(grab, struct shell_seat, popup_grab.grab);
3024 struct shell_surface *shsurf;
3025 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003026
3027 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003028 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003029 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003030 shseat->popup_grab.grab.interface = NULL;
3031 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003032 /* Send the popup_done event to all the popups open */
3033 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003034 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003035 shsurf->popup.shseat = NULL;
3036 if (prev) {
3037 wl_list_init(&prev->popup.grab_link);
3038 }
3039 prev = shsurf;
3040 }
3041 wl_list_init(&prev->popup.grab_link);
3042 wl_list_init(&shseat->popup_grab.surfaces_list);
3043 }
3044}
3045
3046static void
3047add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3048{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003049 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003050
3051 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003052 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003053 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003054 /* We must make sure here that this popup was opened after
3055 * a mouse press, and not just by moving around with other
3056 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003057 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003058 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003059
Rob Bradforddfe31052013-06-26 19:49:11 +01003060 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003061 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003062 } else {
3063 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003064 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003065}
3066
3067static void
3068remove_popup_grab(struct shell_surface *shsurf)
3069{
3070 struct shell_seat *shseat = shsurf->popup.shseat;
3071
3072 wl_list_remove(&shsurf->popup.grab_link);
3073 wl_list_init(&shsurf->popup.grab_link);
3074 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003075 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003076 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003077 }
3078}
3079
3080static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003081shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003082{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003083 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003084 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003085
Jason Ekstranda7af7042013-10-12 22:38:11 -05003086 shsurf->surface->output = parent_view->output;
3087 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003088
Jason Ekstranda7af7042013-10-12 22:38:11 -05003089 weston_view_set_transform_parent(shsurf->view, parent_view);
3090 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3091 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003092
Kristian Høgsberge3148752013-05-06 23:19:49 -04003093 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003094 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003095 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003096 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003097 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003098 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003099}
3100
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003101static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003102 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003103 shell_surface_move,
3104 shell_surface_resize,
3105 shell_surface_set_toplevel,
3106 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003107 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003108 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003109 shell_surface_set_maximized,
3110 shell_surface_set_title,
3111 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003112};
3113
3114static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003115destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003116{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003117 struct shell_surface *child, *next;
3118
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003119 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3120
Giulio Camuffo5085a752013-03-25 21:42:45 +01003121 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3122 remove_popup_grab(shsurf);
3123 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003124
Alex Wubd3354b2012-04-17 17:20:49 +08003125 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003126 shell_surface_is_top_fullscreen(shsurf))
3127 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003128
Jason Ekstranda7af7042013-10-12 22:38:11 -05003129 if (shsurf->fullscreen.black_view)
3130 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003131
Alex Wubd3354b2012-04-17 17:20:49 +08003132 /* As destroy_resource() use wl_list_for_each_safe(),
3133 * we can always remove the listener.
3134 */
3135 wl_list_remove(&shsurf->surface_destroy_listener.link);
3136 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003137 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003138
Jason Ekstranda7af7042013-10-12 22:38:11 -05003139 weston_view_destroy(shsurf->view);
3140
Philip Withnall648a4dd2013-11-25 18:01:44 +00003141 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003142 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3143 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003144
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003145 wl_list_remove(&shsurf->link);
3146 free(shsurf);
3147}
3148
3149static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003150shell_destroy_shell_surface(struct wl_resource *resource)
3151{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003152 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003153
Bryan Caina46b9462014-04-04 17:41:24 -05003154 if (!wl_list_empty(&shsurf->popup.grab_link))
3155 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003156 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003157}
3158
3159static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003160shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003161{
3162 struct shell_surface *shsurf = container_of(listener,
3163 struct shell_surface,
3164 surface_destroy_listener);
3165
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003166 if (shsurf->resource)
3167 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003168
3169 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003170}
3171
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003172static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003173fade_out_done(struct weston_view_animation *animation, void *data)
3174{
3175 struct shell_surface *shsurf = data;
3176
3177 weston_surface_destroy(shsurf->surface);
3178}
3179
3180static void
3181handle_resource_destroy(struct wl_listener *listener, void *data)
3182{
3183 struct shell_surface *shsurf =
3184 container_of(listener, struct shell_surface,
3185 resource_destroy_listener);
3186
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003187 if (!weston_surface_is_mapped(shsurf->surface))
3188 return;
3189
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003190 shsurf->surface->ref_count++;
3191
3192 pixman_region32_fini(&shsurf->surface->pending.input);
3193 pixman_region32_init(&shsurf->surface->pending.input);
3194 pixman_region32_fini(&shsurf->surface->input);
3195 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003196 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3197 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003198}
3199
3200static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003201shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003202
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003203struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003204get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003205{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003206 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003207 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003208 else
3209 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003210}
3211
Rafael Antognollie2a34552013-12-03 15:35:45 -02003212static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003213create_common_surface(struct shell_client *owner, void *shell,
3214 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003215 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003216{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003217 struct shell_surface *shsurf;
3218
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003219 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003220 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003221 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003222 }
3223
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003224 shsurf = calloc(1, sizeof *shsurf);
3225 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003226 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003227 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003228 }
3229
Jason Ekstranda7af7042013-10-12 22:38:11 -05003230 shsurf->view = weston_view_create(surface);
3231 if (!shsurf->view) {
3232 weston_log("no memory to allocate shell surface\n");
3233 free(shsurf);
3234 return NULL;
3235 }
3236
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003237 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003238 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003239
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003240 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3241 wl_resource_add_destroy_listener(surface->resource,
3242 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003243 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003244
Tiago Vignattibc052c92012-04-19 16:18:18 +03003245 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003246 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003247 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003248 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003249 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003250 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003251 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3252 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003253 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003254 wl_list_init(&shsurf->fullscreen.transform.link);
3255
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003256 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003257 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003258 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003259 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003260
3261 /* init link so its safe to always remove it in destroy_shell_surface */
3262 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003263 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003264
Pekka Paalanen460099f2012-01-20 16:48:25 +02003265 /* empty when not in use */
3266 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003267 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003268
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003269 wl_list_init(&shsurf->workspace_transform.link);
3270
Philip Withnall648a4dd2013-11-25 18:01:44 +00003271 wl_list_init(&shsurf->children_link);
3272 wl_list_init(&shsurf->children_list);
3273 shsurf->parent = NULL;
3274
Pekka Paalanen98262232011-12-01 10:42:22 +02003275 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003276
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003277 shsurf->client = client;
3278
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003279 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003280}
3281
Rafael Antognollie2a34552013-12-03 15:35:45 -02003282static struct shell_surface *
3283create_shell_surface(void *shell, struct weston_surface *surface,
3284 const struct weston_shell_client *client)
3285{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003286 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003287}
3288
Jason Ekstranda7af7042013-10-12 22:38:11 -05003289static struct weston_view *
3290get_primary_view(void *shell, struct shell_surface *shsurf)
3291{
3292 return shsurf->view;
3293}
3294
Tiago Vignattibc052c92012-04-19 16:18:18 +03003295static void
3296shell_get_shell_surface(struct wl_client *client,
3297 struct wl_resource *resource,
3298 uint32_t id,
3299 struct wl_resource *surface_resource)
3300{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003301 struct weston_surface *surface =
3302 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003303 struct shell_client *sc = wl_resource_get_user_data(resource);
3304 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003305 struct shell_surface *shsurf;
3306
3307 if (get_shell_surface(surface)) {
3308 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003309 WL_DISPLAY_ERROR_INVALID_OBJECT,
3310 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003311 return;
3312 }
3313
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003314 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003315 if (!shsurf) {
3316 wl_resource_post_error(surface_resource,
3317 WL_DISPLAY_ERROR_INVALID_OBJECT,
3318 "surface->configure already set");
3319 return;
3320 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003321
Jason Ekstranda85118c2013-06-27 20:17:02 -05003322 shsurf->resource =
3323 wl_resource_create(client,
3324 &wl_shell_surface_interface, 1, id);
3325 wl_resource_set_implementation(shsurf->resource,
3326 &shell_surface_implementation,
3327 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003328}
3329
Rafael Antognollie2a34552013-12-03 15:35:45 -02003330static bool
3331shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3332{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003333 /* A shell surface without a resource is created from xwayland
3334 * and is considered a wl_shell surface for now. */
3335
3336 return shsurf->resource == NULL ||
3337 wl_resource_instance_of(shsurf->resource,
3338 &wl_shell_surface_interface,
3339 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003340}
3341
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003342static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003343 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003344};
3345
Rafael Antognollie2a34552013-12-03 15:35:45 -02003346/****************************
3347 * xdg-shell implementation */
3348
3349static void
3350xdg_surface_destroy(struct wl_client *client,
3351 struct wl_resource *resource)
3352{
3353 wl_resource_destroy(resource);
3354}
3355
3356static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003357xdg_surface_set_parent(struct wl_client *client,
3358 struct wl_resource *resource,
3359 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003360{
3361 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3362 struct weston_surface *parent;
3363
3364 if (parent_resource)
3365 parent = wl_resource_get_user_data(parent_resource);
3366 else
3367 parent = NULL;
3368
3369 shell_surface_set_parent(shsurf, parent);
3370}
3371
3372static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003373xdg_surface_set_margin(struct wl_client *client,
3374 struct wl_resource *resource,
3375 int32_t left,
3376 int32_t right,
3377 int32_t top,
3378 int32_t bottom)
3379{
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003380 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3381
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07003382 set_margin(shsurf, left, right, top, bottom);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003383}
3384
3385static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003386xdg_surface_set_app_id(struct wl_client *client,
3387 struct wl_resource *resource,
3388 const char *app_id)
3389{
3390 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3391
3392 free(shsurf->class);
3393 shsurf->class = strdup(app_id);
3394}
3395
3396static void
3397xdg_surface_set_title(struct wl_client *client,
3398 struct wl_resource *resource, const char *title)
3399{
3400 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3401
3402 set_title(shsurf, title);
3403}
3404
3405static void
3406xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3407 struct wl_resource *seat_resource, uint32_t serial)
3408{
3409 common_surface_move(resource, seat_resource, serial);
3410}
3411
3412static void
3413xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3414 struct wl_resource *seat_resource, uint32_t serial,
3415 uint32_t edges)
3416{
3417 common_surface_resize(resource, seat_resource, serial, edges);
3418}
3419
3420static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003421xdg_surface_ack_configure(struct wl_client *client,
3422 struct wl_resource *resource,
3423 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003424{
3425 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3426
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003427 if (shsurf->state_requested) {
3428 shsurf->next_state = shsurf->requested_state;
3429 shsurf->state_changed = true;
3430 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003431 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003432}
3433
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003434static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003435xdg_surface_set_maximized(struct wl_client *client,
3436 struct wl_resource *resource)
3437{
3438 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3439
3440 shsurf->state_requested = true;
3441 shsurf->requested_state.maximized = true;
3442 set_maximized(shsurf, NULL);
3443}
3444
3445static void
3446xdg_surface_unset_maximized(struct wl_client *client,
3447 struct wl_resource *resource)
3448{
3449 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3450
3451 shsurf->state_requested = true;
3452 shsurf->requested_state.maximized = false;
3453 shsurf->client->send_configure(shsurf->surface, 0, 0);
3454}
3455
3456static void
3457xdg_surface_set_fullscreen(struct wl_client *client,
3458 struct wl_resource *resource,
3459 struct wl_resource *output_resource)
3460{
3461 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3462 struct weston_output *output;
3463
3464 shsurf->state_requested = true;
3465 shsurf->requested_state.fullscreen = true;
3466
3467 if (output_resource)
3468 output = wl_resource_get_user_data(output_resource);
3469 else
3470 output = NULL;
3471
3472 shsurf->recommended_output = output;
3473
3474 set_fullscreen(shsurf,
3475 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3476 0, shsurf->recommended_output);
3477}
3478
3479static void
3480xdg_surface_unset_fullscreen(struct wl_client *client,
3481 struct wl_resource *resource)
3482{
3483 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3484
3485 shsurf->state_requested = true;
3486 shsurf->requested_state.fullscreen = false;
3487 shsurf->client->send_configure(shsurf->surface, 0, 0);
3488}
3489
3490static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003491xdg_surface_set_minimized(struct wl_client *client,
3492 struct wl_resource *resource)
3493{
3494 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3495
3496 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3497 return;
3498
3499 /* apply compositor's own minimization logic (hide) */
3500 set_minimized(shsurf->surface, 1);
3501}
3502
Rafael Antognollie2a34552013-12-03 15:35:45 -02003503static const struct xdg_surface_interface xdg_surface_implementation = {
3504 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003505 xdg_surface_set_parent,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003506 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003507 xdg_surface_set_title,
3508 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003509 xdg_surface_move,
3510 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003511 xdg_surface_ack_configure,
3512 xdg_surface_set_maximized,
3513 xdg_surface_unset_maximized,
3514 xdg_surface_set_fullscreen,
3515 xdg_surface_unset_fullscreen,
3516 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003517};
3518
3519static void
3520xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003521 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003522{
3523 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003524 uint32_t *s;
3525 struct wl_array states;
3526 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003527
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003528 assert(shsurf);
3529
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003530 if (!shsurf->resource)
3531 return;
3532
3533 wl_array_init(&states);
3534 if (shsurf->requested_state.fullscreen) {
3535 s = wl_array_add(&states, sizeof *s);
3536 *s = XDG_SURFACE_STATE_FULLSCREEN;
3537 } else if (shsurf->requested_state.maximized) {
3538 s = wl_array_add(&states, sizeof *s);
3539 *s = XDG_SURFACE_STATE_MAXIMIZED;
3540 }
3541
3542 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3543 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3544
3545 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003546}
3547
3548static const struct weston_shell_client xdg_client = {
3549 xdg_send_configure
3550};
3551
3552static void
3553xdg_use_unstable_version(struct wl_client *client,
3554 struct wl_resource *resource,
3555 int32_t version)
3556{
3557 if (version > 1) {
3558 wl_resource_post_error(resource,
3559 1,
3560 "xdg-shell:: version not implemented yet.");
3561 return;
3562 }
3563}
3564
3565static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003566create_xdg_surface(struct shell_client *owner, void *shell,
3567 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003568 const struct weston_shell_client *client)
3569{
3570 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003571
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003572 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003573 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003574
3575 return shsurf;
3576}
3577
3578static void
3579xdg_get_xdg_surface(struct wl_client *client,
3580 struct wl_resource *resource,
3581 uint32_t id,
3582 struct wl_resource *surface_resource)
3583{
3584 struct weston_surface *surface =
3585 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003586 struct shell_client *sc = wl_resource_get_user_data(resource);
3587 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003588 struct shell_surface *shsurf;
3589
3590 if (get_shell_surface(surface)) {
3591 wl_resource_post_error(surface_resource,
3592 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003593 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003594 return;
3595 }
3596
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003597 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003598 if (!shsurf) {
3599 wl_resource_post_error(surface_resource,
3600 WL_DISPLAY_ERROR_INVALID_OBJECT,
3601 "surface->configure already set");
3602 return;
3603 }
3604
3605 shsurf->resource =
3606 wl_resource_create(client,
3607 &xdg_surface_interface, 1, id);
3608 wl_resource_set_implementation(shsurf->resource,
3609 &xdg_surface_implementation,
3610 shsurf, shell_destroy_shell_surface);
3611}
3612
3613static bool
3614shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3615{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003616 return shsurf->resource &&
3617 wl_resource_instance_of(shsurf->resource,
3618 &xdg_surface_interface,
3619 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003620}
3621
3622/* xdg-popup implementation */
3623
3624static void
3625xdg_popup_destroy(struct wl_client *client,
3626 struct wl_resource *resource)
3627{
3628 wl_resource_destroy(resource);
3629}
3630
Rafael Antognollie2a34552013-12-03 15:35:45 -02003631static const struct xdg_popup_interface xdg_popup_implementation = {
3632 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003633};
3634
3635static void
3636xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003637 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003638{
3639}
3640
3641static const struct weston_shell_client xdg_popup_client = {
3642 xdg_popup_send_configure
3643};
3644
3645static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003646create_xdg_popup(struct shell_client *owner, void *shell,
3647 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003648 const struct weston_shell_client *client,
3649 struct weston_surface *parent,
3650 struct shell_seat *seat,
3651 uint32_t serial,
3652 int32_t x, int32_t y)
3653{
3654 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003655
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003656 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003657 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003658 shsurf->popup.shseat = seat;
3659 shsurf->popup.serial = serial;
3660 shsurf->popup.x = x;
3661 shsurf->popup.y = y;
3662 shell_surface_set_parent(shsurf, parent);
3663
3664 return shsurf;
3665}
3666
3667static void
3668xdg_get_xdg_popup(struct wl_client *client,
3669 struct wl_resource *resource,
3670 uint32_t id,
3671 struct wl_resource *surface_resource,
3672 struct wl_resource *parent_resource,
3673 struct wl_resource *seat_resource,
3674 uint32_t serial,
3675 int32_t x, int32_t y, uint32_t flags)
3676{
3677 struct weston_surface *surface =
3678 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003679 struct shell_client *sc = wl_resource_get_user_data(resource);
3680 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003681 struct shell_surface *shsurf;
3682 struct weston_surface *parent;
3683 struct shell_seat *seat;
3684
3685 if (get_shell_surface(surface)) {
3686 wl_resource_post_error(surface_resource,
3687 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003688 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003689 return;
3690 }
3691
3692 if (!parent_resource) {
3693 wl_resource_post_error(surface_resource,
3694 WL_DISPLAY_ERROR_INVALID_OBJECT,
3695 "xdg_shell::get_xdg_popup requires a parent shell surface");
3696 }
3697
3698 parent = wl_resource_get_user_data(parent_resource);
3699 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3700
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003701 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003702 parent, seat, serial, x, y);
3703 if (!shsurf) {
3704 wl_resource_post_error(surface_resource,
3705 WL_DISPLAY_ERROR_INVALID_OBJECT,
3706 "surface->configure already set");
3707 return;
3708 }
3709
3710 shsurf->resource =
3711 wl_resource_create(client,
3712 &xdg_popup_interface, 1, id);
3713 wl_resource_set_implementation(shsurf->resource,
3714 &xdg_popup_implementation,
3715 shsurf, shell_destroy_shell_surface);
3716}
3717
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003718static void
3719xdg_pong(struct wl_client *client,
3720 struct wl_resource *resource, uint32_t serial)
3721{
3722 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003723
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003724 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003725}
3726
Rafael Antognollie2a34552013-12-03 15:35:45 -02003727static bool
3728shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3729{
3730 return wl_resource_instance_of(shsurf->resource,
3731 &xdg_popup_interface,
3732 &xdg_popup_implementation);
3733}
3734
3735static const struct xdg_shell_interface xdg_implementation = {
3736 xdg_use_unstable_version,
3737 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003738 xdg_get_xdg_popup,
3739 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003740};
3741
3742static int
3743xdg_shell_unversioned_dispatch(const void *implementation,
3744 void *_target, uint32_t opcode,
3745 const struct wl_message *message,
3746 union wl_argument *args)
3747{
3748 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003749 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003750
3751 if (opcode != 0) {
3752 wl_resource_post_error(resource,
3753 WL_DISPLAY_ERROR_INVALID_OBJECT,
3754 "must call use_unstable_version first");
3755 return 0;
3756 }
3757
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003758#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003759
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003760 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3761 "shell implementation doesn't match protocol version");
3762
Rafael Antognollie2a34552013-12-03 15:35:45 -02003763 if (args[0].i != XDG_SERVER_VERSION) {
3764 wl_resource_post_error(resource,
3765 WL_DISPLAY_ERROR_INVALID_OBJECT,
3766 "incompatible version, server is %d "
3767 "client wants %d",
3768 XDG_SERVER_VERSION, args[0].i);
3769 return 0;
3770 }
3771
3772 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003773 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003774
3775 return 1;
3776}
3777
3778/* end of xdg-shell implementation */
3779/***********************************/
3780
Kristian Høgsberg07937562011-04-12 17:25:42 -04003781static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003782shell_fade(struct desktop_shell *shell, enum fade_type type);
3783
3784static int
3785screensaver_timeout(void *data)
3786{
3787 struct desktop_shell *shell = data;
3788
3789 shell_fade(shell, FADE_OUT);
3790
3791 return 1;
3792}
3793
3794static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003795handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003796{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003797 struct desktop_shell *shell =
3798 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003799
Pekka Paalanen18027e52011-12-02 16:31:49 +02003800 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003801
3802 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003803 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003804}
3805
3806static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003807launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003808{
3809 if (shell->screensaver.binding)
3810 return;
3811
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003812 if (!shell->screensaver.path) {
3813 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003814 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003815 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003816
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003817 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003818 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003819 return;
3820 }
3821
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003822 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003823 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003824 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003825 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003826}
3827
3828static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003829terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003830{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003831 if (shell->screensaver.process.pid == 0)
3832 return;
3833
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003834 /* Disarm the screensaver timer, otherwise it may fire when the
3835 * compositor is not in the idle state. In that case, the screen will
3836 * be locked, but the wake_signal won't fire on user input, making the
3837 * system unresponsive. */
3838 wl_event_source_timer_update(shell->screensaver.timer, 0);
3839
Pekka Paalanen18027e52011-12-02 16:31:49 +02003840 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003841}
3842
3843static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003844configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003845{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003846 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003847
Jason Ekstranda7af7042013-10-12 22:38:11 -05003848 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3849 if (v->output == ev->output && v != ev) {
3850 weston_view_unmap(v);
3851 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003852 }
3853 }
3854
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003855 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003856
Jason Ekstranda7af7042013-10-12 22:38:11 -05003857 if (wl_list_empty(&ev->layer_link)) {
3858 wl_list_insert(&layer->view_list, &ev->layer_link);
3859 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003860 }
3861}
3862
3863static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003864background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003865{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003866 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003867 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003868
Jason Ekstranda7af7042013-10-12 22:38:11 -05003869 view = container_of(es->views.next, struct weston_view, surface_link);
3870
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003871 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003872}
3873
3874static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003875desktop_shell_set_background(struct wl_client *client,
3876 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003877 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003878 struct wl_resource *surface_resource)
3879{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003880 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003881 struct weston_surface *surface =
3882 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003883 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003884
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003885 if (surface->configure) {
3886 wl_resource_post_error(surface_resource,
3887 WL_DISPLAY_ERROR_INVALID_OBJECT,
3888 "surface role already assigned");
3889 return;
3890 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003891
Jason Ekstranda7af7042013-10-12 22:38:11 -05003892 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3893 weston_view_destroy(view);
3894 view = weston_view_create(surface);
3895
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003896 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003897 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003898 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003899 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003900 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003901 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003902 surface->output->width,
3903 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003904}
3905
3906static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003907panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003908{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003909 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003910 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003911
Jason Ekstranda7af7042013-10-12 22:38:11 -05003912 view = container_of(es->views.next, struct weston_view, surface_link);
3913
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003914 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003915}
3916
3917static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003918desktop_shell_set_panel(struct wl_client *client,
3919 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003920 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003921 struct wl_resource *surface_resource)
3922{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003923 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003924 struct weston_surface *surface =
3925 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003926 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003927
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003928 if (surface->configure) {
3929 wl_resource_post_error(surface_resource,
3930 WL_DISPLAY_ERROR_INVALID_OBJECT,
3931 "surface role already assigned");
3932 return;
3933 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003934
Jason Ekstranda7af7042013-10-12 22:38:11 -05003935 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3936 weston_view_destroy(view);
3937 view = weston_view_create(surface);
3938
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003939 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003940 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003941 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003942 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003943 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003944 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003945 surface->output->width,
3946 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003947}
3948
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003949static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003950lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003951{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003952 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003953 struct weston_view *view;
3954
3955 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003956
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003957 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003958 return;
3959
Jason Ekstranda7af7042013-10-12 22:38:11 -05003960 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003961
3962 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003963 wl_list_insert(&shell->lock_layer.view_list,
3964 &view->layer_link);
3965 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003966 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003967 }
3968}
3969
3970static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003971handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003972{
Tiago Vignattibe143262012-04-16 17:31:41 +03003973 struct desktop_shell *shell =
3974 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003975
Martin Minarik6d118362012-06-07 18:01:59 +02003976 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003977 shell->lock_surface = NULL;
3978}
3979
3980static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003981desktop_shell_set_lock_surface(struct wl_client *client,
3982 struct wl_resource *resource,
3983 struct wl_resource *surface_resource)
3984{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003985 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003986 struct weston_surface *surface =
3987 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003988
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003989 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003990
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003991 if (!shell->locked)
3992 return;
3993
Pekka Paalanen98262232011-12-01 10:42:22 +02003994 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003995
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003996 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003997 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003998 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003999
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004000 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004001 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004002 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004003}
4004
4005static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004006resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004007{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004008 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004009
Pekka Paalanen77346a62011-11-30 16:26:35 +02004010 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004011
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004012 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004013 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004014 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004015 &shell->input_panel_layer.link);
4016 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004017 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004018 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004019 wl_list_insert(&shell->compositor->cursor_layer.link,
4020 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004021 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004022 wl_list_insert(&shell->fullscreen_layer.link,
4023 &shell->panel_layer.link);
4024 wl_list_insert(&shell->panel_layer.link,
4025 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004026
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004027 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004028
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004029 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004030 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004031 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004032}
4033
4034static void
4035desktop_shell_unlock(struct wl_client *client,
4036 struct wl_resource *resource)
4037{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004038 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004039
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004040 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004041
4042 if (shell->locked)
4043 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004044}
4045
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004046static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004047desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004048 struct wl_resource *resource,
4049 struct wl_resource *surface_resource)
4050{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004051 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004052
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004053 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004054 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004055}
4056
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004057static void
4058desktop_shell_desktop_ready(struct wl_client *client,
4059 struct wl_resource *resource)
4060{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004061 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004062
4063 shell_fade_startup(shell);
4064}
4065
Kristian Høgsberg75840622011-09-06 13:48:16 -04004066static const struct desktop_shell_interface desktop_shell_implementation = {
4067 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004068 desktop_shell_set_panel,
4069 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004070 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004071 desktop_shell_set_grab_surface,
4072 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004073};
4074
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004075static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004076get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004077{
4078 struct shell_surface *shsurf;
4079
4080 shsurf = get_shell_surface(surface);
4081 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004082 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004083 return shsurf->type;
4084}
4085
Kristian Høgsberg75840622011-09-06 13:48:16 -04004086static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004087move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004088{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004089 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004090 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004091 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004092
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004093 if (seat->pointer->focus == NULL)
4094 return;
4095
4096 focus = seat->pointer->focus->surface;
4097
Pekka Paalanen01388e22013-04-25 13:57:44 +03004098 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004099 if (surface == NULL)
4100 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004101
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004102 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004103 if (shsurf == NULL || shsurf->state.fullscreen ||
4104 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004105 return;
4106
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004107 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004108}
4109
4110static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004111maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4112{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004113 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004114 struct weston_surface *surface;
4115 struct shell_surface *shsurf;
4116
4117 surface = weston_surface_get_main_surface(focus);
4118 if (surface == NULL)
4119 return;
4120
4121 shsurf = get_shell_surface(surface);
4122 if (shsurf == NULL)
4123 return;
4124
4125 if (!shell_surface_is_xdg_surface(shsurf))
4126 return;
4127
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004128 shsurf->state_requested = true;
4129 shsurf->requested_state.maximized = !shsurf->state.maximized;
4130 if (shsurf->requested_state.maximized)
4131 set_maximized(shsurf, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004132}
4133
4134static void
4135fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4136{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004137 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004138 struct weston_surface *surface;
4139 struct shell_surface *shsurf;
4140
4141 surface = weston_surface_get_main_surface(focus);
4142 if (surface == NULL)
4143 return;
4144
4145 shsurf = get_shell_surface(surface);
4146 if (shsurf == NULL)
4147 return;
4148
4149 if (!shell_surface_is_xdg_surface(shsurf))
4150 return;
4151
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004152 shsurf->state_requested = true;
4153 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
4154 if (shsurf->requested_state.fullscreen)
4155 set_fullscreen(shsurf,
4156 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
4157 0, shsurf->recommended_output);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004158}
4159
4160static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004161touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4162{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004163 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004164 struct weston_surface *surface;
4165 struct shell_surface *shsurf;
4166
4167 surface = weston_surface_get_main_surface(focus);
4168 if (surface == NULL)
4169 return;
4170
4171 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004172 if (shsurf == NULL || shsurf->state.fullscreen ||
4173 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004174 return;
4175
4176 surface_touch_move(shsurf, (struct weston_seat *) seat);
4177}
4178
4179static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004180resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004181{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004182 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004183 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004184 uint32_t edges = 0;
4185 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004186 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004187
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004188 if (seat->pointer->focus == NULL)
4189 return;
4190
4191 focus = seat->pointer->focus->surface;
4192
Pekka Paalanen01388e22013-04-25 13:57:44 +03004193 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004194 if (surface == NULL)
4195 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004196
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004197 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004198 if (shsurf == NULL || shsurf->state.fullscreen ||
4199 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004200 return;
4201
Jason Ekstranda7af7042013-10-12 22:38:11 -05004202 weston_view_from_global(shsurf->view,
4203 wl_fixed_to_int(seat->pointer->grab_x),
4204 wl_fixed_to_int(seat->pointer->grab_y),
4205 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004206
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004207 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004208 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004209 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004210 edges |= 0;
4211 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004212 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004213
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004214 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004215 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004216 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004217 edges |= 0;
4218 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004219 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004220
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004221 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004222}
4223
4224static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004225surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004226 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004227{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004228 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004229 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004230 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004231 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004232
Pekka Paalanen01388e22013-04-25 13:57:44 +03004233 /* XXX: broken for windows containing sub-surfaces */
4234 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004235 if (surface == NULL)
4236 return;
4237
4238 shsurf = get_shell_surface(surface);
4239 if (!shsurf)
4240 return;
4241
Jason Ekstranda7af7042013-10-12 22:38:11 -05004242 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004243
Jason Ekstranda7af7042013-10-12 22:38:11 -05004244 if (shsurf->view->alpha > 1.0)
4245 shsurf->view->alpha = 1.0;
4246 if (shsurf->view->alpha < step)
4247 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004248
Jason Ekstranda7af7042013-10-12 22:38:11 -05004249 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004250 weston_surface_damage(surface);
4251}
4252
4253static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004254do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004255 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004256{
Daniel Stone37816df2012-05-16 18:45:18 +01004257 struct weston_seat *ws = (struct weston_seat *) seat;
4258 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004259 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004260 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004261
4262 wl_list_for_each(output, &compositor->output_list, link) {
4263 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004264 wl_fixed_to_double(seat->pointer->x),
4265 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004266 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004267 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004268 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004269 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004270 increment = -output->zoom.increment;
4271 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004272 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004273 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004274 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004275 else
4276 increment = 0;
4277
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004278 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004279
Scott Moreaue6603982012-06-11 13:07:51 -06004280 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004281 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004282 else if (output->zoom.level > output->zoom.max_level)
4283 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004284 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004285 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004286 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004287
Scott Moreaue6603982012-06-11 13:07:51 -06004288 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004289
Jason Ekstranda7af7042013-10-12 22:38:11 -05004290 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004291 }
4292 }
4293}
4294
Scott Moreauccbf29d2012-02-22 14:21:41 -07004295static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004296zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004297 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004298{
4299 do_zoom(seat, time, 0, axis, value);
4300}
4301
4302static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004303zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004304 void *data)
4305{
4306 do_zoom(seat, time, key, 0, 0);
4307}
4308
4309static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004310terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004311 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004312{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004313 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004314
Daniel Stone325fc2d2012-05-30 16:31:58 +01004315 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004316}
4317
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004318static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004319rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4320 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004321{
4322 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004323 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004324 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004325 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004326 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004327
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004328 weston_pointer_move(pointer, x, y);
4329
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004330 if (!shsurf)
4331 return;
4332
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004333 cx = 0.5f * shsurf->surface->width;
4334 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004335
Daniel Stone37816df2012-05-16 18:45:18 +01004336 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4337 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004338 r = sqrtf(dx * dx + dy * dy);
4339
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004340 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004341 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004342
4343 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004344 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004345 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004346
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004347 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004348 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004349
4350 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004351 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004352 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004353 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004354 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004355
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004356 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004357 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004358 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004359 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004360 wl_list_init(&shsurf->rotation.transform.link);
4361 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004362 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004363 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004364
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004365 /* We need to adjust the position of the surface
4366 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004367 cposx = shsurf->view->geometry.x + cx;
4368 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004369 dposx = rotate->center.x - cposx;
4370 dposy = rotate->center.y - cposy;
4371 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004372 weston_view_set_position(shsurf->view,
4373 shsurf->view->geometry.x + dposx,
4374 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004375 }
4376
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004377 /* Repaint implies weston_surface_update_transform(), which
4378 * lazily applies the damage due to rotation update.
4379 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004380 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004381}
4382
4383static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004384rotate_grab_button(struct weston_pointer_grab *grab,
4385 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004386{
4387 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004388 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004389 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004390 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004391 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004392
Daniel Stone4dbadb12012-05-30 16:31:51 +01004393 if (pointer->button_count == 0 &&
4394 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004395 if (shsurf)
4396 weston_matrix_multiply(&shsurf->rotation.rotation,
4397 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004398 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004399 free(rotate);
4400 }
4401}
4402
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004403static void
4404rotate_grab_cancel(struct weston_pointer_grab *grab)
4405{
4406 struct rotate_grab *rotate =
4407 container_of(grab, struct rotate_grab, base.grab);
4408
4409 shell_grab_end(&rotate->base);
4410 free(rotate);
4411}
4412
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004413static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004414 noop_grab_focus,
4415 rotate_grab_motion,
4416 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004417 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004418};
4419
4420static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004421surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004422{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004423 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004424 float dx, dy;
4425 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004426
Pekka Paalanen460099f2012-01-20 16:48:25 +02004427 rotate = malloc(sizeof *rotate);
4428 if (!rotate)
4429 return;
4430
Jason Ekstranda7af7042013-10-12 22:38:11 -05004431 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004432 surface->surface->width * 0.5f,
4433 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004434 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004435
Daniel Stone37816df2012-05-16 18:45:18 +01004436 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4437 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004438 r = sqrtf(dx * dx + dy * dy);
4439 if (r > 20.0f) {
4440 struct weston_matrix inverse;
4441
4442 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004443 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004444 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004445
4446 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004447 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004448 } else {
4449 weston_matrix_init(&surface->rotation.rotation);
4450 weston_matrix_init(&rotate->rotation);
4451 }
4452
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004453 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4454 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004455}
4456
4457static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004458rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004459 void *data)
4460{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004461 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004462 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004463 struct shell_surface *surface;
4464
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004465 if (seat->pointer->focus == NULL)
4466 return;
4467
4468 focus = seat->pointer->focus->surface;
4469
Pekka Paalanen01388e22013-04-25 13:57:44 +03004470 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004471 if (base_surface == NULL)
4472 return;
4473
4474 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004475 if (surface == NULL || surface->state.fullscreen ||
4476 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004477 return;
4478
4479 surface_rotate(surface, seat);
4480}
4481
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004482/* Move all fullscreen layers down to the current workspace and hide their
4483 * black views. The surfaces' state is set to both fullscreen and lowered,
4484 * and this is reversed when such a surface is re-configured, see
4485 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4486 *
4487 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004488 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004489void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004490lower_fullscreen_layer(struct desktop_shell *shell)
4491{
4492 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004493 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004494
4495 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004496 wl_list_for_each_reverse_safe(view, prev,
4497 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004498 layer_link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004499 struct shell_surface *shsurf = get_shell_surface(view->surface);
4500
4501 if (!shsurf)
4502 continue;
4503
4504 /* We can have a non-fullscreen popup for a fullscreen surface
4505 * in the fullscreen layer. */
4506 if (shsurf->state.fullscreen) {
4507 /* Hide the black view */
4508 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
4509 wl_list_init(&shsurf->fullscreen.black_view->layer_link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004510 weston_view_damage_below(shsurf->fullscreen.black_view);
4511
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004512 }
4513
4514 /* Lower the view to the workspace layer */
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004515 wl_list_remove(&view->layer_link);
4516 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4517 weston_view_damage_below(view);
4518 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004519
4520 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004521 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004522}
4523
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004524void
Tiago Vignattibe143262012-04-16 17:31:41 +03004525activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004526 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004527{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004528 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004529 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004530 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004531 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004532 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004533
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004534 lower_fullscreen_layer(shell);
4535
Pekka Paalanen01388e22013-04-25 13:57:44 +03004536 main_surface = weston_surface_get_main_surface(es);
4537
Daniel Stone37816df2012-05-16 18:45:18 +01004538 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004539
Jonas Ådahl8538b222012-08-29 22:13:03 +02004540 state = ensure_focus_state(shell, seat);
4541 if (state == NULL)
4542 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004543
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004544 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004545 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004546
Rafael Antognolli03b16592013-12-03 15:35:42 -02004547 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004548 assert(shsurf);
4549
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004550 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004551 shell_configure_fullscreen(shsurf);
4552 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004553 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004554
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004555 /* Update the surface’s layer. This brings it to the top of the stacking
4556 * order as appropriate. */
4557 shell_surface_update_layer(shsurf);
4558
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004559 if (shell->focus_animation_type != ANIMATION_NONE) {
4560 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004561 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004562 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004563}
4564
Alex Wu21858432012-04-01 20:13:08 +08004565/* no-op func for checking black surface */
4566static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004567black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004568{
4569}
4570
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004571static bool
Alex Wu21858432012-04-01 20:13:08 +08004572is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4573{
4574 if (es->configure == black_surface_configure) {
4575 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004576 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004577 return true;
4578 }
4579 return false;
4580}
4581
Kristian Høgsberg75840622011-09-06 13:48:16 -04004582static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004583activate_binding(struct weston_seat *seat,
4584 struct desktop_shell *shell,
4585 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004586{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004587 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004588
Alex Wu9c35e6b2012-03-05 14:13:13 +08004589 if (!focus)
4590 return;
4591
Pekka Paalanen01388e22013-04-25 13:57:44 +03004592 if (is_black_surface(focus, &main_surface))
4593 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004594
Pekka Paalanen01388e22013-04-25 13:57:44 +03004595 main_surface = weston_surface_get_main_surface(focus);
4596 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004597 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004598
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004599 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004600}
4601
4602static void
4603click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4604 void *data)
4605{
4606 if (seat->pointer->grab != &seat->pointer->default_grab)
4607 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004608 if (seat->pointer->focus == NULL)
4609 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004610
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004611 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004612}
4613
4614static void
4615touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4616{
4617 if (seat->touch->grab != &seat->touch->default_grab)
4618 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004619 if (seat->touch->focus == NULL)
4620 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004621
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004622 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004623}
4624
4625static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004626unfocus_all_seats(struct desktop_shell *shell)
4627{
4628 struct weston_seat *seat, *next;
4629
4630 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4631 if (seat->keyboard == NULL)
4632 continue;
4633
4634 weston_keyboard_set_focus(seat->keyboard, NULL);
4635 }
4636}
4637
4638static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004639lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004640{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004641 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004642
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004643 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004644 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004645 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004646 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004647
4648 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004649
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004650 /* Hide all surfaces by removing the fullscreen, panel and
4651 * toplevel layers. This way nothing else can show or receive
4652 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004653
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004654 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004655 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004656 if (shell->showing_input_panels)
4657 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004658 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004659 wl_list_insert(&shell->compositor->cursor_layer.link,
4660 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004661
Pekka Paalanen77346a62011-11-30 16:26:35 +02004662 launch_screensaver(shell);
4663
Neil Robertsb4a91702014-04-09 16:33:32 +01004664 /* Remove the keyboard focus on all seats. This will be
4665 * restored to the workspace's saved state via
4666 * restore_focus_state when the compositor is unlocked */
4667 unfocus_all_seats(shell);
4668
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004669 /* TODO: disable bindings that should not work while locked. */
4670
4671 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004672}
4673
4674static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004675unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004676{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004677 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004678 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004679 return;
4680 }
4681
4682 /* If desktop-shell client has gone away, unlock immediately. */
4683 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004684 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004685 return;
4686 }
4687
4688 if (shell->prepare_event_sent)
4689 return;
4690
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004691 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004692 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004693}
4694
4695static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004696shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004697{
4698 struct desktop_shell *shell = data;
4699
4700 shell->fade.animation = NULL;
4701
4702 switch (shell->fade.type) {
4703 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004704 weston_surface_destroy(shell->fade.view->surface);
4705 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004706 break;
4707 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004708 lock(shell);
4709 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004710 default:
4711 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004712 }
4713}
4714
Jason Ekstranda7af7042013-10-12 22:38:11 -05004715static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004716shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004717{
4718 struct weston_compositor *compositor = shell->compositor;
4719 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004720 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004721
4722 surface = weston_surface_create(compositor);
4723 if (!surface)
4724 return NULL;
4725
Jason Ekstranda7af7042013-10-12 22:38:11 -05004726 view = weston_view_create(surface);
4727 if (!view) {
4728 weston_surface_destroy(surface);
4729 return NULL;
4730 }
4731
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004732 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004733 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004734 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004735 wl_list_insert(&compositor->fade_layer.view_list,
4736 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004737 pixman_region32_init(&surface->input);
4738
Jason Ekstranda7af7042013-10-12 22:38:11 -05004739 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004740}
4741
4742static void
4743shell_fade(struct desktop_shell *shell, enum fade_type type)
4744{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004745 float tint;
4746
4747 switch (type) {
4748 case FADE_IN:
4749 tint = 0.0;
4750 break;
4751 case FADE_OUT:
4752 tint = 1.0;
4753 break;
4754 default:
4755 weston_log("shell: invalid fade type\n");
4756 return;
4757 }
4758
4759 shell->fade.type = type;
4760
Jason Ekstranda7af7042013-10-12 22:38:11 -05004761 if (shell->fade.view == NULL) {
4762 shell->fade.view = shell_fade_create_surface(shell);
4763 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004764 return;
4765
Jason Ekstranda7af7042013-10-12 22:38:11 -05004766 shell->fade.view->alpha = 1.0 - tint;
4767 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004768 }
4769
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004770 if (shell->fade.view->output == NULL) {
4771 /* If the black view gets a NULL output, we lost the
4772 * last output and we'll just cancel the fade. This
4773 * happens when you close the last window under the
4774 * X11 or Wayland backends. */
4775 shell->locked = false;
4776 weston_surface_destroy(shell->fade.view->surface);
4777 shell->fade.view = NULL;
4778 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004779 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004780 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004781 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004782 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004783 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004784 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004785 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004786}
4787
4788static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004789do_shell_fade_startup(void *data)
4790{
4791 struct desktop_shell *shell = data;
4792
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004793 if (shell->startup_animation_type == ANIMATION_FADE)
4794 shell_fade(shell, FADE_IN);
4795 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004796 weston_surface_destroy(shell->fade.view->surface);
4797 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004798 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004799}
4800
4801static void
4802shell_fade_startup(struct desktop_shell *shell)
4803{
4804 struct wl_event_loop *loop;
4805
4806 if (!shell->fade.startup_timer)
4807 return;
4808
4809 wl_event_source_remove(shell->fade.startup_timer);
4810 shell->fade.startup_timer = NULL;
4811
4812 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4813 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4814}
4815
4816static int
4817fade_startup_timeout(void *data)
4818{
4819 struct desktop_shell *shell = data;
4820
4821 shell_fade_startup(shell);
4822 return 0;
4823}
4824
4825static void
4826shell_fade_init(struct desktop_shell *shell)
4827{
4828 /* Make compositor output all black, and wait for the desktop-shell
4829 * client to signal it is ready, then fade in. The timer triggers a
4830 * fade-in, in case the desktop-shell client takes too long.
4831 */
4832
4833 struct wl_event_loop *loop;
4834
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004836 weston_log("%s: warning: fade surface already exists\n",
4837 __func__);
4838 return;
4839 }
4840
Jason Ekstranda7af7042013-10-12 22:38:11 -05004841 shell->fade.view = shell_fade_create_surface(shell);
4842 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004843 return;
4844
Jason Ekstranda7af7042013-10-12 22:38:11 -05004845 weston_view_update_transform(shell->fade.view);
4846 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004847
4848 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4849 shell->fade.startup_timer =
4850 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4851 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4852}
4853
4854static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004855idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004856{
4857 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004858 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004859 struct weston_seat *seat;
4860
4861 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4862 if (seat->pointer)
4863 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004864
4865 shell_fade(shell, FADE_OUT);
4866 /* lock() is called from shell_fade_done() */
4867}
4868
4869static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004870wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004871{
4872 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004873 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004874
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004875 unlock(shell);
4876}
4877
4878static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004879center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004880{
Giulio Camuffob8366642013-04-25 13:57:46 +03004881 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004882 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004883
Jason Ekstranda7af7042013-10-12 22:38:11 -05004884 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004885
4886 x = output->x + (output->width - width) / 2 - surf_x / 2;
4887 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004888
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004889 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004890}
4891
4892static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004893weston_view_set_initial_position(struct weston_view *view,
4894 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004895{
4896 struct weston_compositor *compositor = shell->compositor;
4897 int ix = 0, iy = 0;
4898 int range_x, range_y;
4899 int dx, dy, x, y, panel_height;
4900 struct weston_output *output, *target_output = NULL;
4901 struct weston_seat *seat;
4902
4903 /* As a heuristic place the new window on the same output as the
4904 * pointer. Falling back to the output containing 0, 0.
4905 *
4906 * TODO: Do something clever for touch too?
4907 */
4908 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004909 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004910 ix = wl_fixed_to_int(seat->pointer->x);
4911 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004912 break;
4913 }
4914 }
4915
4916 wl_list_for_each(output, &compositor->output_list, link) {
4917 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4918 target_output = output;
4919 break;
4920 }
4921 }
4922
4923 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004924 weston_view_set_position(view, 10 + random() % 400,
4925 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004926 return;
4927 }
4928
4929 /* Valid range within output where the surface will still be onscreen.
4930 * If this is negative it means that the surface is bigger than
4931 * output.
4932 */
4933 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004934 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004935 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004936 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004937
Rob Bradford4cb88c72012-08-13 15:18:44 +01004938 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004939 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004940 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004941 dx = 0;
4942
4943 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004944 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004945 else
4946 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004947
4948 x = target_output->x + dx;
4949 y = target_output->y + dy;
4950
Jason Ekstranda7af7042013-10-12 22:38:11 -05004951 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004952}
4953
4954static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004955map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004956 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004957{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004958 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004959 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004960 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004961 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004962
Pekka Paalanen77346a62011-11-30 16:26:35 +02004963 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004964 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004965 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004966 if (shsurf->state.fullscreen) {
4967 center_on_output(shsurf->view, shsurf->fullscreen_output);
4968 shell_map_fullscreen(shsurf);
4969 } else if (shsurf->state.maximized) {
4970 /* use surface configure to set the geometry */
4971 panel_height = get_output_panel_height(shell, shsurf->output);
4972 surface_subsurfaces_boundingbox(shsurf->surface,
4973 &surf_x, &surf_y, NULL, NULL);
4974 weston_view_set_position(shsurf->view,
4975 shsurf->output->x - surf_x,
4976 shsurf->output->y +
4977 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004978 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004979 weston_view_set_initial_position(shsurf->view, shell);
4980 }
Juan Zhao96879df2012-02-07 08:45:41 +08004981 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004982 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004983 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004984 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004985 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004986 weston_view_set_position(shsurf->view,
4987 shsurf->view->geometry.x + sx,
4988 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004989 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004990 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004991 default:
4992 ;
4993 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004994
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004995 /* Surface stacking order, see also activate(). */
4996 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004997
Jason Ekstranda7af7042013-10-12 22:38:11 -05004998 if (shsurf->type != SHELL_SURFACE_NONE) {
4999 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005000 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005001 shsurf->surface->output = shsurf->output;
5002 shsurf->view->output = shsurf->output;
5003 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005004 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005005
Jason Ekstranda7af7042013-10-12 22:38:11 -05005006 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005007 /* XXX: xwayland's using the same fields for transient type */
5008 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005009 if (shsurf->transient.flags ==
5010 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5011 break;
5012 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005013 if (shsurf->state.relative &&
5014 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5015 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005016 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005017 break;
5018 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005019 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005020 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005021 case SHELL_SURFACE_POPUP:
5022 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005023 default:
5024 break;
5025 }
5026
Rafael Antognolli03b16592013-12-03 15:35:42 -02005027 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5028 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005029 {
5030 switch (shell->win_animation_type) {
5031 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005032 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005033 break;
5034 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005035 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005036 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005037 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005038 default:
5039 break;
5040 }
5041 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005042}
5043
5044static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005045configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005046 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005047{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005048 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005049 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005050 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005051
Pekka Paalanen77346a62011-11-30 16:26:35 +02005052 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005053
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005054 assert(shsurf);
5055
Rafael Antognolli03b16592013-12-03 15:35:42 -02005056 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005057 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005058 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005059 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005060 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5061 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005062 mx = shsurf->output->x - surf_x;
5063 my = shsurf->output->y +
5064 get_output_panel_height(shell,shsurf->output) - surf_y;
5065 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005066 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005067 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005068 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005069
Alex Wu4539b082012-03-01 12:57:46 +08005070 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005071 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005072 wl_list_for_each(view, &surface->views, surface_link)
5073 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005074
Rafael Antognolli03b16592013-12-03 15:35:42 -02005075 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005076 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005077 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005078}
5079
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005080static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005081shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005082{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005083 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005084 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005085 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005086
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005087 assert(shsurf);
5088
5089 shell = shsurf->shell;
5090
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005091 if (!weston_surface_is_mapped(es) &&
5092 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005093 remove_popup_grab(shsurf);
5094 }
5095
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005096 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005097 return;
5098
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005099 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005100 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005101 type_changed = 1;
5102 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005103
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005104 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005105 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005106 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005107 shsurf->last_width != es->width ||
5108 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005109 float from_x, from_y;
5110 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005111
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005112 if (shsurf->resize_edges) {
5113 sx = 0;
5114 sy = 0;
5115 }
5116
5117 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5118 sx = shsurf->last_width - es->width;
5119 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5120 sy = shsurf->last_height - es->height;
5121
5122 shsurf->last_width = es->width;
5123 shsurf->last_height = es->height;
5124
Jason Ekstranda7af7042013-10-12 22:38:11 -05005125 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5126 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005127 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005128 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005129 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005130 }
5131}
5132
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005133static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005134
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005135static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005136desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005137{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005138 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005139 struct desktop_shell *shell =
5140 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005141
5142 shell->child.process.pid = 0;
5143 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005144
5145 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5146 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005147 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005148 shell->child.deathstamp = time;
5149 shell->child.deathcount = 0;
5150 }
5151
5152 shell->child.deathcount++;
5153 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005154 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005155 return;
5156 }
5157
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005158 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005159 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005160 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005161}
5162
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005163static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005164desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5165{
5166 struct desktop_shell *shell;
5167
5168 shell = container_of(listener, struct desktop_shell,
5169 child.client_destroy_listener);
5170
5171 shell->child.client = NULL;
5172}
5173
5174static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005175launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005176{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005177 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005178
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005179 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005180 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005181 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005182 desktop_shell_sigchld);
5183
5184 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005185 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005186
5187 shell->child.client_destroy_listener.notify =
5188 desktop_shell_client_destroy;
5189 wl_client_add_destroy_listener(shell->child.client,
5190 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005191}
5192
5193static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005194handle_shell_client_destroy(struct wl_listener *listener, void *data)
5195{
5196 struct shell_client *sc =
5197 container_of(listener, struct shell_client, destroy_listener);
5198
5199 if (sc->ping_timer)
5200 wl_event_source_remove(sc->ping_timer);
5201 free(sc);
5202}
5203
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005204static struct shell_client *
5205shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5206 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005207{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005208 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005209
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005210 sc = zalloc(sizeof *sc);
5211 if (sc == NULL) {
5212 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005213 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005214 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005215
5216 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005217 if (sc->resource == NULL) {
5218 free(sc);
5219 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005220 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005221 }
5222
5223 sc->client = client;
5224 sc->shell = shell;
5225 sc->destroy_listener.notify = handle_shell_client_destroy;
5226 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5227
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005228 return sc;
5229}
5230
5231static void
5232bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5233{
5234 struct desktop_shell *shell = data;
5235 struct shell_client *sc;
5236
5237 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5238 if (sc)
5239 wl_resource_set_implementation(sc->resource,
5240 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005241 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005242}
5243
5244static void
5245bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5246{
5247 struct desktop_shell *shell = data;
5248 struct shell_client *sc;
5249
5250 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5251 if (sc)
5252 wl_resource_set_dispatcher(sc->resource,
5253 xdg_shell_unversioned_dispatch,
5254 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005255}
5256
5257static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005258unbind_desktop_shell(struct wl_resource *resource)
5259{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005260 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005261
5262 if (shell->locked)
5263 resume_desktop(shell);
5264
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005265 shell->child.desktop_shell = NULL;
5266 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005267}
5268
5269static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005270bind_desktop_shell(struct wl_client *client,
5271 void *data, uint32_t version, uint32_t id)
5272{
Tiago Vignattibe143262012-04-16 17:31:41 +03005273 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005274 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005275
Jason Ekstranda85118c2013-06-27 20:17:02 -05005276 resource = wl_resource_create(client, &desktop_shell_interface,
5277 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005278
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005279 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005280 wl_resource_set_implementation(resource,
5281 &desktop_shell_implementation,
5282 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005283 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005284
5285 if (version < 2)
5286 shell_fade_startup(shell);
5287
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005288 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005289 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005290
5291 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5292 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005293 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005294}
5295
Pekka Paalanen6e168112011-11-24 11:34:05 +02005296static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005297screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005298{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005299 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005300 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005301
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005302 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005303 return;
5304
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005305 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005306 if (!shell->locked)
5307 return;
5308
Jason Ekstranda7af7042013-10-12 22:38:11 -05005309 view = container_of(surface->views.next, struct weston_view, surface_link);
5310 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005311
Jason Ekstranda7af7042013-10-12 22:38:11 -05005312 if (wl_list_empty(&view->layer_link)) {
5313 wl_list_insert(shell->lock_layer.view_list.prev,
5314 &view->layer_link);
5315 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005316 wl_event_source_timer_update(shell->screensaver.timer,
5317 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005318 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005319 }
5320}
5321
5322static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005323screensaver_set_surface(struct wl_client *client,
5324 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005325 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005326 struct wl_resource *output_resource)
5327{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005328 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005329 struct weston_surface *surface =
5330 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005331 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005332 struct weston_view *view, *next;
5333
5334 /* Make sure we only have one view */
5335 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5336 weston_view_destroy(view);
5337 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005338
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005339 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005340 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005341 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005342}
5343
5344static const struct screensaver_interface screensaver_implementation = {
5345 screensaver_set_surface
5346};
5347
5348static void
5349unbind_screensaver(struct wl_resource *resource)
5350{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005351 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005352
Pekka Paalanen77346a62011-11-30 16:26:35 +02005353 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005354}
5355
5356static void
5357bind_screensaver(struct wl_client *client,
5358 void *data, uint32_t version, uint32_t id)
5359{
Tiago Vignattibe143262012-04-16 17:31:41 +03005360 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005361 struct wl_resource *resource;
5362
Jason Ekstranda85118c2013-06-27 20:17:02 -05005363 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005364
Pekka Paalanen77346a62011-11-30 16:26:35 +02005365 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005366 wl_resource_set_implementation(resource,
5367 &screensaver_implementation,
5368 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005369 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005370 return;
5371 }
5372
5373 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5374 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005375 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005376}
5377
Kristian Høgsberg07045392012-02-19 18:52:44 -05005378struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005379 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005380 struct weston_surface *current;
5381 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005382 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005383 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005384};
5385
5386static void
5387switcher_next(struct switcher *switcher)
5388{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005389 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005390 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005391 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005392 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005393
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005394 /* temporary re-display minimized surfaces */
5395 struct weston_view *tmp;
5396 struct weston_view **minimized;
5397 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5398 wl_list_remove(&view->layer_link);
5399 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5400 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5401 *minimized = view;
5402 }
5403
Jason Ekstranda7af7042013-10-12 22:38:11 -05005404 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005405 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005406 if (shsurf &&
5407 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5408 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005409 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005410 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005411 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005412 next = view->surface;
5413 prev = view->surface;
5414 view->alpha = 0.25;
5415 weston_view_geometry_dirty(view);
5416 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005417 }
Alex Wu1659daa2012-04-01 20:13:09 +08005418
Jason Ekstranda7af7042013-10-12 22:38:11 -05005419 if (is_black_surface(view->surface, NULL)) {
5420 view->alpha = 0.25;
5421 weston_view_geometry_dirty(view);
5422 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005423 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005424 }
5425
5426 if (next == NULL)
5427 next = first;
5428
Alex Wu07b26062012-03-12 16:06:01 +08005429 if (next == NULL)
5430 return;
5431
Kristian Høgsberg07045392012-02-19 18:52:44 -05005432 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005433 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005434
5435 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005436 wl_list_for_each(view, &next->views, surface_link)
5437 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005438
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005439 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005440 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005441 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005442}
5443
5444static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005445switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005446{
5447 struct switcher *switcher =
5448 container_of(listener, struct switcher, listener);
5449
5450 switcher_next(switcher);
5451}
5452
5453static void
Daniel Stone351eb612012-05-31 15:27:47 -04005454switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005455{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005456 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005457 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005458 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005459
Jason Ekstranda7af7042013-10-12 22:38:11 -05005460 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005461 if (is_focus_view(view))
5462 continue;
5463
Jason Ekstranda7af7042013-10-12 22:38:11 -05005464 view->alpha = 1.0;
5465 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005466 }
5467
Alex Wu07b26062012-03-12 16:06:01 +08005468 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005469 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005470 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005471 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005472 weston_keyboard_end_grab(keyboard);
5473 if (keyboard->input_method_resource)
5474 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005475
5476 /* re-hide surfaces that were temporary shown during the switch */
5477 struct weston_view **minimized;
5478 wl_array_for_each(minimized, &switcher->minimized_array) {
5479 /* with the exception of the current selected */
5480 if ((*minimized)->surface != switcher->current) {
5481 wl_list_remove(&(*minimized)->layer_link);
5482 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5483 weston_view_damage_below(*minimized);
5484 }
5485 }
5486 wl_array_release(&switcher->minimized_array);
5487
Kristian Høgsberg07045392012-02-19 18:52:44 -05005488 free(switcher);
5489}
5490
5491static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005492switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005493 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005494{
5495 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005496 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005497
Daniel Stonec9785ea2012-05-30 16:31:52 +01005498 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005499 switcher_next(switcher);
5500}
5501
5502static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005503switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005504 uint32_t mods_depressed, uint32_t mods_latched,
5505 uint32_t mods_locked, uint32_t group)
5506{
5507 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005508 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005509
Daniel Stone351eb612012-05-31 15:27:47 -04005510 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5511 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005512}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005513
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005514static void
5515switcher_cancel(struct weston_keyboard_grab *grab)
5516{
5517 struct switcher *switcher = container_of(grab, struct switcher, grab);
5518
5519 switcher_destroy(switcher);
5520}
5521
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005522static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005523 switcher_key,
5524 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005525 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005526};
5527
5528static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005529switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005530 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005531{
Tiago Vignattibe143262012-04-16 17:31:41 +03005532 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005533 struct switcher *switcher;
5534
5535 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005536 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005537 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005538 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005539 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005540 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005541
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005542 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005543 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005544 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005545 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5546 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005547 switcher_next(switcher);
5548}
5549
Pekka Paalanen3c647232011-12-22 13:43:43 +02005550static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005551backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005552 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005553{
5554 struct weston_compositor *compositor = data;
5555 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005556 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005557
5558 /* TODO: we're limiting to simple use cases, where we assume just
5559 * control on the primary display. We'd have to extend later if we
5560 * ever get support for setting backlights on random desktop LCD
5561 * panels though */
5562 output = get_default_output(compositor);
5563 if (!output)
5564 return;
5565
5566 if (!output->set_backlight)
5567 return;
5568
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005569 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5570 backlight_new = output->backlight_current - 25;
5571 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5572 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005573
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005574 if (backlight_new < 5)
5575 backlight_new = 5;
5576 if (backlight_new > 255)
5577 backlight_new = 255;
5578
5579 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005580 output->set_backlight(output, output->backlight_current);
5581}
5582
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005583struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005584 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005585 struct weston_seat *seat;
5586 uint32_t key[2];
5587 int key_released[2];
5588};
5589
5590static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005591debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005592 uint32_t key, uint32_t state)
5593{
5594 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005595 struct weston_compositor *ec = db->seat->compositor;
5596 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005597 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005598 uint32_t serial;
5599 int send = 0, terminate = 0;
5600 int check_binding = 1;
5601 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005602 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005603
5604 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5605 /* Do not run bindings on key releases */
5606 check_binding = 0;
5607
5608 for (i = 0; i < 2; i++)
5609 if (key == db->key[i])
5610 db->key_released[i] = 1;
5611
5612 if (db->key_released[0] && db->key_released[1]) {
5613 /* All key releases been swalled so end the grab */
5614 terminate = 1;
5615 } else if (key != db->key[0] && key != db->key[1]) {
5616 /* Should not swallow release of other keys */
5617 send = 1;
5618 }
5619 } else if (key == db->key[0] && !db->key_released[0]) {
5620 /* Do not check bindings for the first press of the binding
5621 * key. This allows it to be used as a debug shortcut.
5622 * We still need to swallow this event. */
5623 check_binding = 0;
5624 } else if (db->key[1]) {
5625 /* If we already ran a binding don't process another one since
5626 * we can't keep track of all the binding keys that were
5627 * pressed in order to swallow the release events. */
5628 send = 1;
5629 check_binding = 0;
5630 }
5631
5632 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005633 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5634 key, state)) {
5635 /* We ran a binding so swallow the press and keep the
5636 * grab to swallow the released too. */
5637 send = 0;
5638 terminate = 0;
5639 db->key[1] = key;
5640 } else {
5641 /* Terminate the grab since the key pressed is not a
5642 * debug binding key. */
5643 send = 1;
5644 terminate = 1;
5645 }
5646 }
5647
5648 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005649 serial = wl_display_next_serial(display);
5650 resource_list = &grab->keyboard->focus_resource_list;
5651 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005652 wl_keyboard_send_key(resource, serial, time, key, state);
5653 }
5654 }
5655
5656 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005657 weston_keyboard_end_grab(grab->keyboard);
5658 if (grab->keyboard->input_method_resource)
5659 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005660 free(db);
5661 }
5662}
5663
5664static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005665debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005666 uint32_t mods_depressed, uint32_t mods_latched,
5667 uint32_t mods_locked, uint32_t group)
5668{
5669 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005670 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005671
Neil Roberts96d790e2013-09-19 17:32:00 +01005672 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005673
Neil Roberts96d790e2013-09-19 17:32:00 +01005674 wl_resource_for_each(resource, resource_list) {
5675 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5676 mods_latched, mods_locked, group);
5677 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005678}
5679
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005680static void
5681debug_binding_cancel(struct weston_keyboard_grab *grab)
5682{
5683 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5684
5685 weston_keyboard_end_grab(grab->keyboard);
5686 free(db);
5687}
5688
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005689struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005690 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005691 debug_binding_modifiers,
5692 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005693};
5694
5695static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005696debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005697{
5698 struct debug_binding_grab *grab;
5699
5700 grab = calloc(1, sizeof *grab);
5701 if (!grab)
5702 return;
5703
5704 grab->seat = (struct weston_seat *) seat;
5705 grab->key[0] = key;
5706 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005707 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005708}
5709
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005710static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005711force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005712 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005713{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005714 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005715 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005716 struct desktop_shell *shell = data;
5717 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005718 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005719
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005720 focus_surface = seat->keyboard->focus;
5721 if (!focus_surface)
5722 return;
5723
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005724 wl_signal_emit(&compositor->kill_signal, focus_surface);
5725
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005726 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005727 wl_client_get_credentials(client, &pid, NULL, NULL);
5728
5729 /* Skip clients that we launched ourselves (the credentials of
5730 * the socketpair is ours) */
5731 if (pid == getpid())
5732 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005733
Daniel Stone325fc2d2012-05-30 16:31:58 +01005734 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005735}
5736
5737static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005738workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005739 uint32_t key, void *data)
5740{
5741 struct desktop_shell *shell = data;
5742 unsigned int new_index = shell->workspaces.current;
5743
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005744 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005745 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005746 if (new_index != 0)
5747 new_index--;
5748
5749 change_workspace(shell, new_index);
5750}
5751
5752static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005753workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005754 uint32_t key, void *data)
5755{
5756 struct desktop_shell *shell = data;
5757 unsigned int new_index = shell->workspaces.current;
5758
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005759 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005760 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005761 if (new_index < shell->workspaces.num - 1)
5762 new_index++;
5763
5764 change_workspace(shell, new_index);
5765}
5766
5767static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005768workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005769 uint32_t key, void *data)
5770{
5771 struct desktop_shell *shell = data;
5772 unsigned int new_index;
5773
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005774 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005775 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005776 new_index = key - KEY_F1;
5777 if (new_index >= shell->workspaces.num)
5778 new_index = shell->workspaces.num - 1;
5779
5780 change_workspace(shell, new_index);
5781}
5782
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005783static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005784workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005785 uint32_t key, void *data)
5786{
5787 struct desktop_shell *shell = data;
5788 unsigned int new_index = shell->workspaces.current;
5789
5790 if (shell->locked)
5791 return;
5792
5793 if (new_index != 0)
5794 new_index--;
5795
5796 take_surface_to_workspace_by_seat(shell, seat, new_index);
5797}
5798
5799static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005800workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005801 uint32_t key, void *data)
5802{
5803 struct desktop_shell *shell = data;
5804 unsigned int new_index = shell->workspaces.current;
5805
5806 if (shell->locked)
5807 return;
5808
5809 if (new_index < shell->workspaces.num - 1)
5810 new_index++;
5811
5812 take_surface_to_workspace_by_seat(shell, seat, new_index);
5813}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005814
5815static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005816shell_reposition_view_on_output_destroy(struct weston_view *view)
5817{
5818 struct weston_output *output, *first_output;
5819 struct weston_compositor *ec = view->surface->compositor;
5820 struct shell_surface *shsurf;
5821 float x, y;
5822 int visible;
5823
5824 x = view->geometry.x;
5825 y = view->geometry.y;
5826
5827 /* At this point the destroyed output is not in the list anymore.
5828 * If the view is still visible somewhere, we leave where it is,
5829 * otherwise, move it to the first output. */
5830 visible = 0;
5831 wl_list_for_each(output, &ec->output_list, link) {
5832 if (pixman_region32_contains_point(&output->region,
5833 x, y, NULL)) {
5834 visible = 1;
5835 break;
5836 }
5837 }
5838
5839 if (!visible) {
5840 first_output = container_of(ec->output_list.next,
5841 struct weston_output, link);
5842
5843 x = first_output->x + first_output->width / 4;
5844 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005845
5846 weston_view_set_position(view, x, y);
5847 } else {
5848 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005849 }
5850
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005851
5852 shsurf = get_shell_surface(view->surface);
5853
5854 if (shsurf) {
5855 shsurf->saved_position_valid = false;
5856 shsurf->next_state.maximized = false;
5857 shsurf->next_state.fullscreen = false;
5858 shsurf->state_changed = true;
5859 }
5860}
5861
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005862void
5863shell_for_each_layer(struct desktop_shell *shell,
5864 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005865{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005866 struct workspace **ws;
5867
5868 func(shell, &shell->fullscreen_layer, data);
5869 func(shell, &shell->panel_layer, data);
5870 func(shell, &shell->background_layer, data);
5871 func(shell, &shell->lock_layer, data);
5872 func(shell, &shell->input_panel_layer, data);
5873
5874 wl_array_for_each(ws, &shell->workspaces.array)
5875 func(shell, &(*ws)->layer, data);
5876}
5877
5878static void
5879shell_output_destroy_move_layer(struct desktop_shell *shell,
5880 struct weston_layer *layer,
5881 void *data)
5882{
5883 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005884 struct weston_view *view;
5885
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005886 wl_list_for_each(view, &layer->view_list, layer_link) {
5887 if (view->output != output)
5888 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005889
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005890 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005891 }
5892}
5893
5894static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005895handle_output_destroy(struct wl_listener *listener, void *data)
5896{
5897 struct shell_output *output_listener =
5898 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005899 struct weston_output *output = output_listener->output;
5900 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005901
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005902 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005903
Xiong Zhang6b481422013-10-23 13:58:32 +08005904 wl_list_remove(&output_listener->destroy_listener.link);
5905 wl_list_remove(&output_listener->link);
5906 free(output_listener);
5907}
5908
5909static void
5910create_shell_output(struct desktop_shell *shell,
5911 struct weston_output *output)
5912{
5913 struct shell_output *shell_output;
5914
5915 shell_output = zalloc(sizeof *shell_output);
5916 if (shell_output == NULL)
5917 return;
5918
5919 shell_output->output = output;
5920 shell_output->shell = shell;
5921 shell_output->destroy_listener.notify = handle_output_destroy;
5922 wl_signal_add(&output->destroy_signal,
5923 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005924 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005925}
5926
5927static void
5928handle_output_create(struct wl_listener *listener, void *data)
5929{
5930 struct desktop_shell *shell =
5931 container_of(listener, struct desktop_shell, output_create_listener);
5932 struct weston_output *output = (struct weston_output *)data;
5933
5934 create_shell_output(shell, output);
5935}
5936
5937static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005938handle_output_move_layer(struct desktop_shell *shell,
5939 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005940{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005941 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005942 struct weston_view *view;
5943 float x, y;
5944
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005945 wl_list_for_each(view, &layer->view_list, layer_link) {
5946 if (view->output != output)
5947 continue;
5948
5949 x = view->geometry.x + output->move_x;
5950 y = view->geometry.y + output->move_y;
5951 weston_view_set_position(view, x, y);
5952 }
5953}
5954
5955static void
5956handle_output_move(struct wl_listener *listener, void *data)
5957{
5958 struct desktop_shell *shell;
5959
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005960 shell = container_of(listener, struct desktop_shell,
5961 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005962
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005963 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005964}
5965
5966static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005967setup_output_destroy_handler(struct weston_compositor *ec,
5968 struct desktop_shell *shell)
5969{
5970 struct weston_output *output;
5971
5972 wl_list_init(&shell->output_list);
5973 wl_list_for_each(output, &ec->output_list, link)
5974 create_shell_output(shell, output);
5975
5976 shell->output_create_listener.notify = handle_output_create;
5977 wl_signal_add(&ec->output_created_signal,
5978 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005979
5980 shell->output_move_listener.notify = handle_output_move;
5981 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005982}
5983
5984static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005985shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005986{
Tiago Vignattibe143262012-04-16 17:31:41 +03005987 struct desktop_shell *shell =
5988 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005989 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005990 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005991
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005992 /* Force state to unlocked so we don't try to fade */
5993 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005994 if (shell->child.client)
5995 wl_client_destroy(shell->child.client);
5996
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005997 wl_list_remove(&shell->idle_listener.link);
5998 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005999
6000 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006001
Xiong Zhang6b481422013-10-23 13:58:32 +08006002 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6003 wl_list_remove(&shell_output->destroy_listener.link);
6004 wl_list_remove(&shell_output->link);
6005 free(shell_output);
6006 }
6007
6008 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006009 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006010
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006011 wl_array_for_each(ws, &shell->workspaces.array)
6012 workspace_destroy(*ws);
6013 wl_array_release(&shell->workspaces.array);
6014
Pekka Paalanen3c647232011-12-22 13:43:43 +02006015 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006016 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006017 free(shell);
6018}
6019
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006020static void
6021shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6022{
6023 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006024 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006025
6026 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006027 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6028 MODIFIER_CTRL | MODIFIER_ALT,
6029 terminate_binding, ec);
6030 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6031 click_to_activate_binding,
6032 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006033 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6034 click_to_activate_binding,
6035 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006036 weston_compositor_add_touch_binding(ec, 0,
6037 touch_to_activate_binding,
6038 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006039 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6040 MODIFIER_SUPER | MODIFIER_ALT,
6041 surface_opacity_binding, NULL);
6042 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6043 MODIFIER_SUPER, zoom_axis_binding,
6044 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006045
6046 /* configurable bindings */
6047 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006048 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6049 zoom_key_binding, NULL);
6050 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6051 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006052 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6053 maximize_binding, NULL);
6054 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6055 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006056 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6057 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006058 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006059 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6060 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006061 weston_compositor_add_button_binding(ec, BTN_LEFT,
6062 mod | MODIFIER_SHIFT,
6063 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006064
6065 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6066 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6067 rotate_binding, NULL);
6068
Daniel Stone325fc2d2012-05-30 16:31:58 +01006069 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6070 shell);
6071 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6072 ec);
6073 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6074 backlight_binding, ec);
6075 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6076 ec);
6077 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6078 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006079 weston_compositor_add_key_binding(ec, KEY_K, mod,
6080 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006081 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6082 workspace_up_binding, shell);
6083 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6084 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006085 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6086 workspace_move_surface_up_binding,
6087 shell);
6088 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6089 workspace_move_surface_down_binding,
6090 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006091
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006092 if (shell->exposay_modifier)
6093 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6094 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006095
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006096 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6097 if (shell->workspaces.num > 1) {
6098 num_workspace_bindings = shell->workspaces.num;
6099 if (num_workspace_bindings > 6)
6100 num_workspace_bindings = 6;
6101 for (i = 0; i < num_workspace_bindings; i++)
6102 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6103 workspace_f_binding,
6104 shell);
6105 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006106
6107 /* Debug bindings */
6108 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6109 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006110}
6111
Jason Ekstrand024177c2014-04-21 19:42:58 -05006112static void
6113handle_seat_created(struct wl_listener *listener, void *data)
6114{
6115 struct weston_seat *seat = data;
6116
6117 create_shell_seat(seat);
6118}
6119
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006120WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006121module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006122 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006123{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006124 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006125 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006126 struct workspace **pws;
6127 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006128 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006129
Peter Huttererf3d62272013-08-08 11:57:05 +10006130 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006131 if (shell == NULL)
6132 return -1;
6133
Kristian Høgsberg75840622011-09-06 13:48:16 -04006134 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006135
6136 shell->destroy_listener.notify = shell_destroy;
6137 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006138 shell->idle_listener.notify = idle_handler;
6139 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6140 shell->wake_listener.notify = wake_handler;
6141 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006142
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006143 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006144 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006145 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006146 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006147 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006148 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006149 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006150 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006151 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006152 ec->shell_interface.set_title = set_title;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07006153 ec->shell_interface.set_margin = set_margin;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006154
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006155 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6156 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006157 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6158 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006159 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006160
6161 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006162 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006163
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006164 if (input_panel_setup(shell) < 0)
6165 return -1;
6166
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006167 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006168
Daniel Stonedf8133b2013-11-19 11:37:14 +01006169 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6170 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6171
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006172 for (i = 0; i < shell->workspaces.num; i++) {
6173 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6174 if (pws == NULL)
6175 return -1;
6176
6177 *pws = workspace_create();
6178 if (*pws == NULL)
6179 return -1;
6180 }
6181 activate_workspace(shell, 0);
6182
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006183 weston_layer_init(&shell->minimized_layer, NULL);
6184
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006185 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006186 wl_list_init(&shell->workspaces.animation.link);
6187 shell->workspaces.animation.frame = animate_workspace_change_frame;
6188
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006189 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006190 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006191 return -1;
6192
Rafael Antognollie2a34552013-12-03 15:35:45 -02006193 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6194 shell, bind_xdg_shell) == NULL)
6195 return -1;
6196
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006197 if (wl_global_create(ec->wl_display,
6198 &desktop_shell_interface, 2,
6199 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006200 return -1;
6201
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006202 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6203 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006204 return -1;
6205
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006206 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6207 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006208 return -1;
6209
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006210 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006211
Xiong Zhang6b481422013-10-23 13:58:32 +08006212 setup_output_destroy_handler(ec, shell);
6213
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006214 loop = wl_display_get_event_loop(ec->wl_display);
6215 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006216
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006217 shell->screensaver.timer =
6218 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6219
Jason Ekstrand024177c2014-04-21 19:42:58 -05006220 wl_list_for_each(seat, &ec->seat_list, link)
6221 handle_seat_created(NULL, seat);
6222 shell->seat_create_listener.notify = handle_seat_created;
6223 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006224
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006225 screenshooter_create(ec);
6226
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006227 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006228
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006229 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006230
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006231 return 0;
6232}