blob: a631c6299b2462ce128ea64c0b51a6625105cb23 [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
734 if (state->seat->keyboard == NULL)
735 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{
Scott Moreau4272e632012-08-13 09:58:41 -06001030 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001031
1032 wl_list_remove(&shell->workspaces.animation.link);
1033 workspace_deactivate_transforms(from);
1034 workspace_deactivate_transforms(to);
1035 shell->workspaces.anim_to = NULL;
1036
1037 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1038}
1039
1040static void
1041animate_workspace_change_frame(struct weston_animation *animation,
1042 struct weston_output *output, uint32_t msecs)
1043{
1044 struct desktop_shell *shell =
1045 container_of(animation, struct desktop_shell,
1046 workspaces.animation);
1047 struct workspace *from = shell->workspaces.anim_from;
1048 struct workspace *to = shell->workspaces.anim_to;
1049 uint32_t t;
1050 double x, y;
1051
1052 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1053 finish_workspace_change_animation(shell, from, to);
1054 return;
1055 }
1056
1057 if (shell->workspaces.anim_timestamp == 0) {
1058 if (shell->workspaces.anim_current == 0.0)
1059 shell->workspaces.anim_timestamp = msecs;
1060 else
1061 shell->workspaces.anim_timestamp =
1062 msecs -
1063 /* Invers of movement function 'y' below. */
1064 (asin(1.0 - shell->workspaces.anim_current) *
1065 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1066 M_2_PI);
1067 }
1068
1069 t = msecs - shell->workspaces.anim_timestamp;
1070
1071 /*
1072 * x = [0, π/2]
1073 * y(x) = sin(x)
1074 */
1075 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1076 y = sin(x);
1077
1078 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001079 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080
1081 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1082 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1083 shell->workspaces.anim_current = y;
1084
Scott Moreau4272e632012-08-13 09:58:41 -06001085 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001086 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001087 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001088 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001089}
1090
1091static void
1092animate_workspace_change(struct desktop_shell *shell,
1093 unsigned int index,
1094 struct workspace *from,
1095 struct workspace *to)
1096{
1097 struct weston_output *output;
1098
1099 int dir;
1100
1101 if (index > shell->workspaces.current)
1102 dir = -1;
1103 else
1104 dir = 1;
1105
1106 shell->workspaces.current = index;
1107
1108 shell->workspaces.anim_dir = dir;
1109 shell->workspaces.anim_from = from;
1110 shell->workspaces.anim_to = to;
1111 shell->workspaces.anim_current = 0.0;
1112 shell->workspaces.anim_timestamp = 0;
1113
1114 output = container_of(shell->compositor->output_list.next,
1115 struct weston_output, link);
1116 wl_list_insert(&output->animation_list,
1117 &shell->workspaces.animation.link);
1118
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001119 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120
1121 workspace_translate_in(to, 0);
1122
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001123 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001124
Scott Moreau4272e632012-08-13 09:58:41 -06001125 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126}
1127
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001128static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001129update_workspace(struct desktop_shell *shell, unsigned int index,
1130 struct workspace *from, struct workspace *to)
1131{
1132 shell->workspaces.current = index;
1133 wl_list_insert(&from->layer.link, &to->layer.link);
1134 wl_list_remove(&from->layer.link);
1135}
1136
1137static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001138change_workspace(struct desktop_shell *shell, unsigned int index)
1139{
1140 struct workspace *from;
1141 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001142 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001143
1144 if (index == shell->workspaces.current)
1145 return;
1146
1147 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001149 return;
1150
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001151 from = get_current_workspace(shell);
1152 to = get_workspace(shell, index);
1153
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154 if (shell->workspaces.anim_from == to &&
1155 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001156 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001157 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001158 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001159 return;
1160 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001161
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001162 if (shell->workspaces.anim_to != NULL)
1163 finish_workspace_change_animation(shell,
1164 shell->workspaces.anim_from,
1165 shell->workspaces.anim_to);
1166
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001167 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001168
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001169 if (shell->focus_animation_type != ANIMATION_NONE) {
1170 wl_list_for_each(state, &from->focus_list, link)
1171 if (state->keyboard_focus)
1172 animate_focus_change(shell, from,
1173 get_default_view(state->keyboard_focus), NULL);
1174
1175 wl_list_for_each(state, &to->focus_list, link)
1176 if (state->keyboard_focus)
1177 animate_focus_change(shell, to,
1178 NULL, get_default_view(state->keyboard_focus));
1179 }
1180
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001181 if (workspace_is_empty(to) && workspace_is_empty(from))
1182 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001183 else
1184 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001185
1186 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001187}
1188
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001189static bool
1190workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1191{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001193 struct wl_list *e;
1194
1195 if (wl_list_empty(list))
1196 return false;
1197
1198 e = list->next;
1199
1200 if (e->next != list)
1201 return false;
1202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204}
1205
1206static void
Philip Withnall659163d2013-11-25 18:01:36 +00001207move_surface_to_workspace(struct desktop_shell *shell,
1208 struct shell_surface *shsurf,
1209 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001210{
1211 struct workspace *from;
1212 struct workspace *to;
1213 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001214 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001215 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001216
1217 if (workspace == shell->workspaces.current)
1218 return;
1219
Philip Withnall659163d2013-11-25 18:01:36 +00001220 view = get_default_view(shsurf->surface);
1221 if (!view)
1222 return;
1223
1224 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1225
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001226 if (workspace >= shell->workspaces.num)
1227 workspace = shell->workspaces.num - 1;
1228
Jonas Ådahle9d22502012-08-29 22:13:01 +02001229 from = get_current_workspace(shell);
1230 to = get_workspace(shell, workspace);
1231
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 wl_list_remove(&view->layer_link);
1233 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001234
Philip Withnall648a4dd2013-11-25 18:01:44 +00001235 shell_surface_update_child_surface_layers(shsurf);
1236
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001238 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1239 if (!seat->keyboard)
1240 continue;
1241
1242 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001244 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001245 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001248}
1249
1250static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001251take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001252 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001253 unsigned int index)
1254{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001255 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257 struct shell_surface *shsurf;
1258 struct workspace *from;
1259 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001260 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001261
Pekka Paalanen01388e22013-04-25 13:57:44 +03001262 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 view = get_default_view(surface);
1264 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001265 index == shell->workspaces.current ||
1266 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001267 return;
1268
1269 from = get_current_workspace(shell);
1270 to = get_workspace(shell, index);
1271
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 wl_list_remove(&view->layer_link);
1273 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001274
Philip Withnall659163d2013-11-25 18:01:36 +00001275 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001276 if (shsurf != NULL)
1277 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001278
Jonas Ådahle9d22502012-08-29 22:13:01 +02001279 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001280 drop_focus_state(shell, from, surface);
1281
1282 if (shell->workspaces.anim_from == to &&
1283 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001284 wl_list_remove(&to->layer.link);
1285 wl_list_insert(from->layer.link.prev, &to->layer.link);
1286
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001287 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001288 broadcast_current_workspace_state(shell);
1289
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001290 return;
1291 }
1292
1293 if (shell->workspaces.anim_to != NULL)
1294 finish_workspace_change_animation(shell,
1295 shell->workspaces.anim_from,
1296 shell->workspaces.anim_to);
1297
1298 if (workspace_is_empty(from) &&
1299 workspace_has_only(to, surface))
1300 update_workspace(shell, index, from, to);
1301 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001302 if (shsurf != NULL &&
1303 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001304 wl_list_insert(&shell->workspaces.anim_sticky_list,
1305 &shsurf->workspace_transform.link);
1306
1307 animate_workspace_change(shell, index, from, to);
1308 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001309
1310 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001311
1312 state = ensure_focus_state(shell, seat);
1313 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001314 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001315}
1316
1317static void
1318workspace_manager_move_surface(struct wl_client *client,
1319 struct wl_resource *resource,
1320 struct wl_resource *surface_resource,
1321 uint32_t workspace)
1322{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001323 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001324 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001325 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001326 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001327 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001328
Pekka Paalanen01388e22013-04-25 13:57:44 +03001329 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001330 shell_surface = get_shell_surface(main_surface);
1331 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001333
1334 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001335}
1336
1337static const struct workspace_manager_interface workspace_manager_implementation = {
1338 workspace_manager_move_surface,
1339};
1340
1341static void
1342unbind_resource(struct wl_resource *resource)
1343{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001344 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001345}
1346
1347static void
1348bind_workspace_manager(struct wl_client *client,
1349 void *data, uint32_t version, uint32_t id)
1350{
1351 struct desktop_shell *shell = data;
1352 struct wl_resource *resource;
1353
Jason Ekstranda85118c2013-06-27 20:17:02 -05001354 resource = wl_resource_create(client,
1355 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001356
1357 if (resource == NULL) {
1358 weston_log("couldn't add workspace manager object");
1359 return;
1360 }
1361
Jason Ekstranda85118c2013-06-27 20:17:02 -05001362 wl_resource_set_implementation(resource,
1363 &workspace_manager_implementation,
1364 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001365 wl_list_insert(&shell->workspaces.client_list,
1366 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001367
1368 workspace_manager_send_state(resource,
1369 shell->workspaces.current,
1370 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001371}
1372
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001373static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001374touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1375 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1376{
1377}
1378
1379static void
1380touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1381{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001382 struct weston_touch_move_grab *move =
1383 (struct weston_touch_move_grab *) container_of(
1384 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001385
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001386 if (touch_id == 0)
1387 move->active = 0;
1388
Jonas Ådahl9484b692013-12-02 22:05:03 +01001389 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001390 shell_touch_grab_end(&move->base);
1391 free(move);
1392 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001393}
1394
1395static void
1396touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1397 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1398{
1399 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1400 struct shell_surface *shsurf = move->base.shsurf;
1401 struct weston_surface *es;
1402 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1403 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1404
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001405 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001406 return;
1407
1408 es = shsurf->surface;
1409
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001410 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001411
1412 weston_compositor_schedule_repaint(es->compositor);
1413}
1414
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001415static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001416touch_move_grab_frame(struct weston_touch_grab *grab)
1417{
1418}
1419
1420static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001421touch_move_grab_cancel(struct weston_touch_grab *grab)
1422{
1423 struct weston_touch_move_grab *move =
1424 (struct weston_touch_move_grab *) container_of(
1425 grab, struct shell_touch_grab, grab);
1426
1427 shell_touch_grab_end(&move->base);
1428 free(move);
1429}
1430
Rusty Lynch1084da52013-08-15 09:10:08 -07001431static const struct weston_touch_grab_interface touch_move_grab_interface = {
1432 touch_move_grab_down,
1433 touch_move_grab_up,
1434 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001435 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001436 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001437};
1438
1439static int
1440surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1441{
1442 struct weston_touch_move_grab *move;
1443
1444 if (!shsurf)
1445 return -1;
1446
Rafael Antognolli03b16592013-12-03 15:35:42 -02001447 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001448 return 0;
1449
1450 move = malloc(sizeof *move);
1451 if (!move)
1452 return -1;
1453
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001454 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001456 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001458 seat->touch->grab_y;
1459
1460 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1461 seat->touch);
1462
1463 return 0;
1464}
1465
1466static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001467noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001468{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001469}
1470
1471static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001472constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1473{
1474 struct shell_surface *shsurf = move->base.shsurf;
1475 struct weston_pointer *pointer = move->base.grab.pointer;
Kristian Høgsberg3434b332014-04-29 16:38:23 -07001476 int x, y, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001477 const int safety = 50;
1478
1479 x = wl_fixed_to_int(pointer->x + move->dx);
1480 y = wl_fixed_to_int(pointer->y + move->dy);
1481
1482 panel_height = get_output_panel_height(shsurf->shell,
1483 shsurf->surface->output);
1484 bottom = y + shsurf->surface->height - shsurf->margin.bottom;
1485 if (bottom - panel_height < safety)
1486 y = panel_height + safety -
1487 shsurf->surface->height + shsurf->margin.bottom;
1488
1489 if (move->client_initiated &&
1490 y + shsurf->margin.top < panel_height)
1491 y = panel_height - shsurf->margin.top;
1492
1493 *cx = x;
1494 *cy = y;
1495}
1496
1497static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001498move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1499 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001500{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001501 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001502 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001503 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001504 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001505
1506 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001507 if (!shsurf)
1508 return;
1509
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001510 constrain_position(move, &cx, &cy);
1511
1512 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001513
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001515}
1516
1517static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001518move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001519 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001520{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001521 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1522 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001523 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001524 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001525
Daniel Stone4dbadb12012-05-30 16:31:51 +01001526 if (pointer->button_count == 0 &&
1527 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001528 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001529 free(grab);
1530 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001531}
1532
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001533static void
1534move_grab_cancel(struct weston_pointer_grab *grab)
1535{
1536 struct shell_grab *shell_grab =
1537 container_of(grab, struct shell_grab, grab);
1538
1539 shell_grab_end(shell_grab);
1540 free(grab);
1541}
1542
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001543static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001544 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001545 move_grab_motion,
1546 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001547 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001548};
1549
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001550static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001551surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1552 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001553{
1554 struct weston_move_grab *move;
1555
1556 if (!shsurf)
1557 return -1;
1558
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001559 if (shsurf->grabbed ||
1560 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561 return 0;
1562
1563 move = malloc(sizeof *move);
1564 if (!move)
1565 return -1;
1566
Jason Ekstranda7af7042013-10-12 22:38:11 -05001567 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001568 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001570 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001571 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001572
1573 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001574 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001575
1576 return 0;
1577}
1578
1579static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001580common_surface_move(struct wl_resource *resource,
1581 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001582{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001583 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001584 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001585 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001586
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001587 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001588 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001589 seat->pointer->button_count > 0 &&
1590 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001592 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001593 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001594 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001595 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001596 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001597 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001599 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001600 (surface_touch_move(shsurf, seat) < 0))
1601 wl_resource_post_no_memory(resource);
1602 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001603}
1604
Rafael Antognollie2a34552013-12-03 15:35:45 -02001605static void
1606shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1607 struct wl_resource *seat_resource, uint32_t serial)
1608{
1609 common_surface_move(resource, seat_resource, serial);
1610}
1611
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001612struct weston_resize_grab {
1613 struct shell_grab base;
1614 uint32_t edges;
1615 int32_t width, height;
1616};
1617
1618static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001619resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1620 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621{
1622 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001623 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001624 struct shell_surface *shsurf = resize->base.shsurf;
1625 int32_t width, height;
1626 wl_fixed_t from_x, from_y;
1627 wl_fixed_t to_x, to_y;
1628
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001629 weston_pointer_move(pointer, x, y);
1630
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001631 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001632 return;
1633
Jason Ekstranda7af7042013-10-12 22:38:11 -05001634 weston_view_from_global_fixed(shsurf->view,
1635 pointer->grab_x, pointer->grab_y,
1636 &from_x, &from_y);
1637 weston_view_from_global_fixed(shsurf->view,
1638 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001639
1640 width = resize->width;
1641 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1642 width += wl_fixed_to_int(from_x - to_x);
1643 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1644 width += wl_fixed_to_int(to_x - from_x);
1645 }
1646
1647 height = resize->height;
1648 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1649 height += wl_fixed_to_int(from_y - to_y);
1650 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1651 height += wl_fixed_to_int(to_y - from_y);
1652 }
1653
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001654 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001655}
1656
1657static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001658send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001659{
1660 struct shell_surface *shsurf = get_shell_surface(surface);
1661
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001662 assert(shsurf);
1663
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001664 if (shsurf->resource)
1665 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001666 shsurf->resize_edges,
1667 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001668}
1669
1670static const struct weston_shell_client shell_client = {
1671 send_configure
1672};
1673
1674static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001675resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001676 uint32_t time, uint32_t button, uint32_t state_w)
1677{
1678 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001679 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001680 enum wl_pointer_button_state state = state_w;
1681
1682 if (pointer->button_count == 0 &&
1683 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1684 shell_grab_end(&resize->base);
1685 free(grab);
1686 }
1687}
1688
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001689static void
1690resize_grab_cancel(struct weston_pointer_grab *grab)
1691{
1692 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1693
1694 shell_grab_end(&resize->base);
1695 free(grab);
1696}
1697
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001698static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699 noop_grab_focus,
1700 resize_grab_motion,
1701 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001702 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001703};
1704
Giulio Camuffob8366642013-04-25 13:57:46 +03001705/*
1706 * Returns the bounding box of a surface and all its sub-surfaces,
1707 * in the surface coordinates system. */
1708static void
1709surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1710 int32_t *y, int32_t *w, int32_t *h) {
1711 pixman_region32_t region;
1712 pixman_box32_t *box;
1713 struct weston_subsurface *subsurface;
1714
1715 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716 surface->width,
1717 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001718
1719 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1720 pixman_region32_union_rect(&region, &region,
1721 subsurface->position.x,
1722 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001723 subsurface->surface->width,
1724 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001725 }
1726
1727 box = pixman_region32_extents(&region);
1728 if (x)
1729 *x = box->x1;
1730 if (y)
1731 *y = box->y1;
1732 if (w)
1733 *w = box->x2 - box->x1;
1734 if (h)
1735 *h = box->y2 - box->y1;
1736
1737 pixman_region32_fini(&region);
1738}
1739
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001740static int
1741surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001742 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001743{
1744 struct weston_resize_grab *resize;
1745
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001746 if (shsurf->grabbed ||
1747 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001748 return 0;
1749
1750 if (edges == 0 || edges > 15 ||
1751 (edges & 3) == 3 || (edges & 12) == 12)
1752 return 0;
1753
1754 resize = malloc(sizeof *resize);
1755 if (!resize)
1756 return -1;
1757
1758 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001759 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1760 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001761
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001762 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001763 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001764 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001765
1766 return 0;
1767}
1768
1769static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001770common_surface_resize(struct wl_resource *resource,
1771 struct wl_resource *seat_resource, uint32_t serial,
1772 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001773{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001774 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001775 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001776 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001777
Kristian Høgsberge3148752013-05-06 23:19:49 -04001778 if (seat->pointer->button_count == 0 ||
1779 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001780 seat->pointer->focus == NULL)
1781 return;
1782
1783 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1784 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001785 return;
1786
Kristian Høgsberge3148752013-05-06 23:19:49 -04001787 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001788 wl_resource_post_no_memory(resource);
1789}
1790
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001791static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001792shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1793 struct wl_resource *seat_resource, uint32_t serial,
1794 uint32_t edges)
1795{
1796 common_surface_resize(resource, seat_resource, serial, edges);
1797}
1798
1799static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001800busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001801{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001802 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001803 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001805 wl_fixed_t sx, sy;
1806
Jason Ekstranda7af7042013-10-12 22:38:11 -05001807 view = weston_compositor_pick_view(pointer->seat->compositor,
1808 pointer->x, pointer->y,
1809 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001810
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001811 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1812 shell_grab_end(grab);
1813 free(grab);
1814 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001815}
1816
1817static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001818busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1819 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001820{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001821 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001822}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001823
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001824static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001825busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001826 uint32_t time, uint32_t button, uint32_t state)
1827{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001828 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001829 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001830 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001831
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001832 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001833 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001834 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001835 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001836 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001837 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001838 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001839}
1840
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001841static void
1842busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1843{
1844 struct shell_grab *grab = (struct shell_grab *) base;
1845
1846 shell_grab_end(grab);
1847 free(grab);
1848}
1849
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001850static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001851 busy_cursor_grab_focus,
1852 busy_cursor_grab_motion,
1853 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001854 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001855};
1856
1857static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001858set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001859{
1860 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001861
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001862 if (pointer->grab->interface == &busy_cursor_grab_interface)
1863 return;
1864
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001865 grab = malloc(sizeof *grab);
1866 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001867 return;
1868
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001869 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1870 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001871 /* Mark the shsurf as ungrabbed so that button binding is able
1872 * to move it. */
1873 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001874}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001875
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001876static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001877end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001878{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001879 struct shell_grab *grab;
1880 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001881
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001882 wl_list_for_each(seat, &compositor->seat_list, link) {
1883 if (seat->pointer == NULL)
1884 continue;
1885
1886 grab = (struct shell_grab *) seat->pointer->grab;
1887 if (grab->grab.interface == &busy_cursor_grab_interface &&
1888 wl_resource_get_client(grab->shsurf->resource) == client) {
1889 shell_grab_end(grab);
1890 free(grab);
1891 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001892 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001893}
1894
Scott Moreau9521d5e2012-04-19 13:06:17 -06001895static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001896handle_shell_client_destroy(struct wl_listener *listener, void *data);
1897
1898static int
1899xdg_ping_timeout_handler(void *data)
1900{
1901 struct shell_client *sc = data;
1902 struct weston_seat *seat;
1903 struct shell_surface *shsurf;
1904
1905 /* Client is not responding */
1906 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001907 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1908 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1909 continue;
1910 if (seat->pointer->focus->surface->resource == NULL)
1911 continue;
1912
1913 shsurf = get_shell_surface(seat->pointer->focus->surface);
1914 if (shsurf &&
1915 wl_resource_get_client(shsurf->resource) == sc->client)
1916 set_busy_cursor(shsurf, seat->pointer);
1917 }
1918
1919 return 1;
1920}
1921
1922static void
1923handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1924{
1925 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05001926 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001927 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001928 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001929
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001930 if (sc->unresponsive) {
1931 xdg_ping_timeout_handler(sc);
1932 return;
1933 }
1934
1935 sc->ping_serial = serial;
1936 loop = wl_display_get_event_loop(compositor->wl_display);
1937 if (sc->ping_timer == NULL)
1938 sc->ping_timer =
1939 wl_event_loop_add_timer(loop,
1940 xdg_ping_timeout_handler, sc);
1941 if (sc->ping_timer == NULL)
1942 return;
1943
1944 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1945
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001946 if (shell_surface_is_xdg_surface(shsurf) ||
1947 shell_surface_is_xdg_popup(shsurf))
1948 xdg_shell_send_ping(sc->resource, serial);
1949 else if (shell_surface_is_wl_shell_surface(shsurf))
1950 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001951}
1952
Scott Moreauff1db4a2012-04-17 19:06:18 -06001953static void
1954ping_handler(struct weston_surface *surface, uint32_t serial)
1955{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001956 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001957
1958 if (!shsurf)
1959 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001960 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001961 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001962 if (shsurf->surface == shsurf->shell->grab_surface)
1963 return;
1964
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001965 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001966}
1967
1968static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001969handle_pointer_focus(struct wl_listener *listener, void *data)
1970{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001971 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001972 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001973 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001974 uint32_t serial;
1975
Jason Ekstranda7af7042013-10-12 22:38:11 -05001976 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001977 return;
1978
Jason Ekstranda7af7042013-10-12 22:38:11 -05001979 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001980 serial = wl_display_next_serial(compositor->wl_display);
1981 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001982}
1983
1984static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001985shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1986{
1987 if (--shsurf->focus_count == 0)
1988 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001989 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001990}
1991
1992static void
1993shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1994{
1995 if (shsurf->focus_count++ == 0)
1996 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001997 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001998}
1999
2000static void
2001handle_keyboard_focus(struct wl_listener *listener, void *data)
2002{
2003 struct weston_keyboard *keyboard = data;
2004 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2005
2006 if (seat->focused_surface) {
2007 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2008 if (shsurf)
2009 shell_surface_lose_keyboard_focus(shsurf);
2010 }
2011
2012 seat->focused_surface = keyboard->focus;
2013
2014 if (seat->focused_surface) {
2015 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2016 if (shsurf)
2017 shell_surface_gain_keyboard_focus(shsurf);
2018 }
2019}
2020
2021static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002022shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002023{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002024 if (sc->ping_serial != serial)
2025 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002026
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002027 sc->unresponsive = 0;
2028 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002029
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002030 if (sc->ping_timer) {
2031 wl_event_source_remove(sc->ping_timer);
2032 sc->ping_timer = NULL;
2033 }
2034
2035}
2036
2037static void
2038shell_surface_pong(struct wl_client *client,
2039 struct wl_resource *resource, uint32_t serial)
2040{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002041 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2042 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002043
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002044 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002045}
2046
2047static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002048set_title(struct shell_surface *shsurf, const char *title)
2049{
2050 free(shsurf->title);
2051 shsurf->title = strdup(title);
2052}
2053
2054static void
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002055set_margin(struct shell_surface *shsurf,
2056 int32_t left, int32_t right, int32_t top, int32_t bottom)
2057{
2058 shsurf->margin.left = left;
2059 shsurf->margin.right = right;
2060 shsurf->margin.top = top;
2061 shsurf->margin.bottom = bottom;
2062}
2063
2064static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002065shell_surface_set_title(struct wl_client *client,
2066 struct wl_resource *resource, const char *title)
2067{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002068 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002069
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002070 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002071}
2072
2073static void
2074shell_surface_set_class(struct wl_client *client,
2075 struct wl_resource *resource, const char *class)
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
2079 free(shsurf->class);
2080 shsurf->class = strdup(class);
2081}
2082
Alex Wu4539b082012-03-01 12:57:46 +08002083static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002084restore_output_mode(struct weston_output *output)
2085{
Hardening57388e42013-09-18 23:56:36 +02002086 if (output->current_mode != output->original_mode ||
2087 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002088 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002089 output->original_mode,
2090 output->original_scale,
2091 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002092}
2093
2094static void
2095restore_all_output_modes(struct weston_compositor *compositor)
2096{
2097 struct weston_output *output;
2098
2099 wl_list_for_each(output, &compositor->output_list, link)
2100 restore_output_mode(output);
2101}
2102
Philip Withnallbecb77e2013-11-25 18:01:30 +00002103static int
2104get_output_panel_height(struct desktop_shell *shell,
2105 struct weston_output *output)
2106{
2107 struct weston_view *view;
2108 int panel_height = 0;
2109
2110 if (!output)
2111 return 0;
2112
2113 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2114 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002115 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002116 break;
2117 }
2118 }
2119
2120 return panel_height;
2121}
2122
Philip Withnall07926d92013-11-25 18:01:40 +00002123/* The surface will be inserted into the list immediately after the link
2124 * returned by this function (i.e. will be stacked immediately above the
2125 * returned link). */
2126static struct wl_list *
2127shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2128{
2129 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002130 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002131
2132 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002133 case SHELL_SURFACE_XWAYLAND:
2134 return &shsurf->shell->fullscreen_layer.view_list;
2135
2136 case SHELL_SURFACE_NONE:
2137 return NULL;
2138
Kristian Høgsbergead52422014-01-01 13:51:52 -08002139 case SHELL_SURFACE_POPUP:
2140 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002141 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002142 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002143 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002144 /* Move the surface to its parent layer so
2145 * that surfaces which are transient for
2146 * fullscreen surfaces don't get hidden by the
2147 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002148
2149 /* TODO: Handle a parent with multiple views */
2150 parent = get_default_view(shsurf->parent);
2151 if (parent)
2152 return parent->layer_link.prev;
2153 }
Philip Withnall07926d92013-11-25 18:01:40 +00002154
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002155 /* Move the surface to a normal workspace layer so that surfaces
2156 * which were previously fullscreen or transient are no longer
2157 * rendered on top. */
2158 ws = get_current_workspace(shsurf->shell);
2159 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002160 }
2161
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002162 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002163}
2164
Philip Withnall648a4dd2013-11-25 18:01:44 +00002165static void
2166shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2167{
2168 struct shell_surface *child;
2169
2170 /* Move the child layers to the same workspace as shsurf. They will be
2171 * stacked above shsurf. */
2172 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2173 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002174 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002175 weston_view_geometry_dirty(child->view);
2176 wl_list_remove(&child->view->layer_link);
2177 wl_list_insert(shsurf->view->layer_link.prev,
2178 &child->view->layer_link);
2179 weston_view_geometry_dirty(child->view);
2180 weston_surface_damage(child->surface);
2181
2182 /* Recurse. We don’t expect this to recurse very far (if
2183 * at all) because that would imply we have transient
2184 * (or popup) children of transient surfaces, which
2185 * would be unusual. */
2186 shell_surface_update_child_surface_layers(child);
2187 }
2188 }
2189}
2190
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002191/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002192 * geometry to ensure the changes are redrawn.
2193 *
2194 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2195 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002196static void
2197shell_surface_update_layer(struct shell_surface *shsurf)
2198{
2199 struct wl_list *new_layer_link;
2200
2201 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2202
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002203 if (new_layer_link == NULL)
2204 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002205 if (new_layer_link == &shsurf->view->layer_link)
2206 return;
2207
2208 weston_view_geometry_dirty(shsurf->view);
2209 wl_list_remove(&shsurf->view->layer_link);
2210 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2211 weston_view_geometry_dirty(shsurf->view);
2212 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002213
2214 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002215}
2216
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002217static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002218shell_surface_set_parent(struct shell_surface *shsurf,
2219 struct weston_surface *parent)
2220{
2221 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002222
2223 wl_list_remove(&shsurf->children_link);
2224 wl_list_init(&shsurf->children_link);
2225
2226 /* Insert into the parent surface’s child list. */
2227 if (parent != NULL) {
2228 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2229 if (parent_shsurf != NULL)
2230 wl_list_insert(&parent_shsurf->children_list,
2231 &shsurf->children_link);
2232 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002233}
2234
2235static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002236shell_surface_set_output(struct shell_surface *shsurf,
2237 struct weston_output *output)
2238{
2239 struct weston_surface *es = shsurf->surface;
2240
2241 /* get the default output, if the client set it as NULL
2242 check whether the ouput is available */
2243 if (output)
2244 shsurf->output = output;
2245 else if (es->output)
2246 shsurf->output = es->output;
2247 else
2248 shsurf->output = get_default_output(es->compositor);
2249}
2250
2251static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002252surface_clear_next_states(struct shell_surface *shsurf)
2253{
2254 shsurf->next_state.maximized = false;
2255 shsurf->next_state.fullscreen = false;
2256
2257 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2258 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2259 shsurf->state_changed = true;
2260}
2261
2262static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002263set_toplevel(struct shell_surface *shsurf)
2264{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002265 shell_surface_set_parent(shsurf, NULL);
2266 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002267 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002268
2269 /* The layer_link is updated in set_surface_type(),
2270 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002271}
2272
2273static void
2274shell_surface_set_toplevel(struct wl_client *client,
2275 struct wl_resource *resource)
2276{
2277 struct shell_surface *surface = wl_resource_get_user_data(resource);
2278
2279 set_toplevel(surface);
2280}
2281
2282static void
2283set_transient(struct shell_surface *shsurf,
2284 struct weston_surface *parent, int x, int y, uint32_t flags)
2285{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002286 assert(parent != NULL);
2287
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002288 shell_surface_set_parent(shsurf, parent);
2289
2290 surface_clear_next_states(shsurf);
2291
Philip Withnallbecb77e2013-11-25 18:01:30 +00002292 shsurf->transient.x = x;
2293 shsurf->transient.y = y;
2294 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002295
Rafael Antognollied207b42013-12-03 15:35:43 -02002296 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002297 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002298 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002299
2300 /* The layer_link is updated in set_surface_type(),
2301 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002302}
2303
2304static void
2305shell_surface_set_transient(struct wl_client *client,
2306 struct wl_resource *resource,
2307 struct wl_resource *parent_resource,
2308 int x, int y, uint32_t flags)
2309{
2310 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2311 struct weston_surface *parent =
2312 wl_resource_get_user_data(parent_resource);
2313
2314 set_transient(shsurf, parent, x, y, flags);
2315}
2316
2317static void
2318set_fullscreen(struct shell_surface *shsurf,
2319 uint32_t method,
2320 uint32_t framerate,
2321 struct weston_output *output)
2322{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002323 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002324
2325 shsurf->fullscreen_output = shsurf->output;
2326 shsurf->fullscreen.type = method;
2327 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002328
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002329 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002330
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002331 shsurf->client->send_configure(shsurf->surface,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002332 shsurf->output->width,
2333 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002334
2335 /* The layer_link is updated in set_surface_type(),
2336 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002337}
2338
2339static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002340weston_view_set_initial_position(struct weston_view *view,
2341 struct desktop_shell *shell);
2342
2343static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002344unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002345{
Philip Withnallf85fe842013-11-25 18:01:37 +00002346 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002347 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2348 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002349 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002350 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002351
Alex Wu4539b082012-03-01 12:57:46 +08002352 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2353 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002354
Alex Wu4539b082012-03-01 12:57:46 +08002355 wl_list_remove(&shsurf->fullscreen.transform.link);
2356 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002357
Jason Ekstranda7af7042013-10-12 22:38:11 -05002358 if (shsurf->fullscreen.black_view)
2359 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2360 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002361
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002362 if (shsurf->saved_position_valid)
2363 weston_view_set_position(shsurf->view,
2364 shsurf->saved_x, shsurf->saved_y);
2365 else
2366 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2367
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002368 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002369 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002370 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002371 shsurf->saved_rotation_valid = false;
2372 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002373
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002374 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002375}
2376
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002377static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002378shell_surface_set_fullscreen(struct wl_client *client,
2379 struct wl_resource *resource,
2380 uint32_t method,
2381 uint32_t framerate,
2382 struct wl_resource *output_resource)
2383{
2384 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2385 struct weston_output *output;
2386
2387 if (output_resource)
2388 output = wl_resource_get_user_data(output_resource);
2389 else
2390 output = NULL;
2391
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002392 shell_surface_set_parent(shsurf, NULL);
2393
Rafael Antognolli03b16592013-12-03 15:35:42 -02002394 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002395 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002396
2397 shsurf->next_state.fullscreen = true;
2398 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002399}
2400
2401static void
2402set_popup(struct shell_surface *shsurf,
2403 struct weston_surface *parent,
2404 struct weston_seat *seat,
2405 uint32_t serial,
2406 int32_t x,
2407 int32_t y)
2408{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002409 assert(parent != NULL);
2410
Philip Withnallbecb77e2013-11-25 18:01:30 +00002411 shsurf->popup.shseat = get_shell_seat(seat);
2412 shsurf->popup.serial = serial;
2413 shsurf->popup.x = x;
2414 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002415
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002416 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002417}
2418
2419static void
2420shell_surface_set_popup(struct wl_client *client,
2421 struct wl_resource *resource,
2422 struct wl_resource *seat_resource,
2423 uint32_t serial,
2424 struct wl_resource *parent_resource,
2425 int32_t x, int32_t y, uint32_t flags)
2426{
2427 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002428 struct weston_surface *parent =
2429 wl_resource_get_user_data(parent_resource);
2430
2431 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002432
Rafael Antognolli03b16592013-12-03 15:35:42 -02002433 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002434 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002435 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002436 wl_resource_get_user_data(seat_resource),
2437 serial, x, y);
2438}
2439
2440static void
2441set_maximized(struct shell_surface *shsurf,
2442 struct weston_output *output)
2443{
2444 struct desktop_shell *shell;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002445 uint32_t panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002446
Philip Withnall352e7ed2013-11-25 18:01:35 +00002447 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002448
2449 shell = shell_surface_get_shell(shsurf);
2450 panel_height = get_output_panel_height(shell, shsurf->output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002451
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002452 shsurf->client->send_configure(shsurf->surface,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002453 shsurf->output->width,
2454 shsurf->output->height - panel_height);
2455
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002456 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002457}
2458
2459static void
2460unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002461{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002462 /* undo all maximized things here */
2463 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002464
2465 if (shsurf->saved_position_valid)
2466 weston_view_set_position(shsurf->view,
2467 shsurf->saved_x, shsurf->saved_y);
2468 else
2469 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002470
2471 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002472 wl_list_insert(&shsurf->view->geometry.transformation_list,
2473 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002474 shsurf->saved_rotation_valid = false;
2475 }
2476
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002477 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002478}
2479
Philip Withnallbecb77e2013-11-25 18:01:30 +00002480static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002481set_minimized(struct weston_surface *surface, uint32_t is_true)
2482{
2483 struct shell_surface *shsurf;
2484 struct workspace *current_ws;
2485 struct weston_seat *seat;
2486 struct weston_surface *focus;
2487 struct weston_view *view;
2488
2489 view = get_default_view(surface);
2490 if (!view)
2491 return;
2492
2493 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2494
2495 shsurf = get_shell_surface(surface);
2496 current_ws = get_current_workspace(shsurf->shell);
2497
2498 wl_list_remove(&view->layer_link);
2499 /* hide or show, depending on the state */
2500 if (is_true) {
2501 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2502
2503 drop_focus_state(shsurf->shell, current_ws, view->surface);
2504 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2505 if (!seat->keyboard)
2506 continue;
2507 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2508 if (focus == view->surface)
2509 weston_keyboard_set_focus(seat->keyboard, NULL);
2510 }
2511 }
2512 else {
2513 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2514
2515 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2516 if (!seat->keyboard)
2517 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002518 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002519 }
2520 }
2521
2522 shell_surface_update_child_surface_layers(shsurf);
2523
2524 weston_view_damage_below(view);
2525}
2526
2527static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002528shell_surface_set_maximized(struct wl_client *client,
2529 struct wl_resource *resource,
2530 struct wl_resource *output_resource)
2531{
2532 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2533 struct weston_output *output;
2534
2535 if (output_resource)
2536 output = wl_resource_get_user_data(output_resource);
2537 else
2538 output = NULL;
2539
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002540 shell_surface_set_parent(shsurf, NULL);
2541
Rafael Antognolli03b16592013-12-03 15:35:42 -02002542 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002543 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002544
2545 shsurf->next_state.maximized = true;
2546 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002547}
2548
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002549/* This is only ever called from set_surface_type(), so there’s no need to
2550 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002551static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002552reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002553{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002554 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002555 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002556 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002557 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002558
Pekka Paalanen98262232011-12-01 10:42:22 +02002559 return 0;
2560}
2561
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002562static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002563set_full_output(struct shell_surface *shsurf)
2564{
2565 shsurf->saved_x = shsurf->view->geometry.x;
2566 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002567 shsurf->saved_width = shsurf->surface->width;
2568 shsurf->saved_height = shsurf->surface->height;
2569 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002570 shsurf->saved_position_valid = true;
2571
2572 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2573 wl_list_remove(&shsurf->rotation.transform.link);
2574 wl_list_init(&shsurf->rotation.transform.link);
2575 weston_view_geometry_dirty(shsurf->view);
2576 shsurf->saved_rotation_valid = true;
2577 }
2578}
2579
2580static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002581set_surface_type(struct shell_surface *shsurf)
2582{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002583 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002584 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002585
Philip Withnallbecb77e2013-11-25 18:01:30 +00002586 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002587
Rafael Antognolli03b16592013-12-03 15:35:42 -02002588 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002589 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002590
2591 switch (shsurf->type) {
2592 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002593 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002594 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002595 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002596 weston_view_set_position(shsurf->view,
2597 pev->geometry.x + shsurf->transient.x,
2598 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002599 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002600 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002601
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002602 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002603 weston_view_set_position(shsurf->view, shsurf->transient.x,
2604 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002605 break;
2606
Philip Withnall0f640e22013-11-25 18:01:31 +00002607 case SHELL_SURFACE_POPUP:
2608 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002609 default:
2610 break;
2611 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002612
2613 /* Update the surface’s layer. */
2614 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002615}
2616
Tiago Vignattibe143262012-04-16 17:31:41 +03002617static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002618shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002619{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002620 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002621}
2622
Alex Wu21858432012-04-01 20:13:08 +08002623static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002624black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002625
Jason Ekstranda7af7042013-10-12 22:38:11 -05002626static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002627create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002628 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002629 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002630{
2631 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002632 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002633
2634 surface = weston_surface_create(ec);
2635 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002636 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002637 return NULL;
2638 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002639 view = weston_view_create(surface);
2640 if (surface == NULL) {
2641 weston_log("no memory\n");
2642 weston_surface_destroy(surface);
2643 return NULL;
2644 }
Alex Wu4539b082012-03-01 12:57:46 +08002645
Alex Wu21858432012-04-01 20:13:08 +08002646 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002647 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002648 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002649 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002650 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002651 pixman_region32_fini(&surface->input);
2652 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002653
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002654 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002655 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002656
2657 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002658}
2659
Philip Withnalled8f1a92013-11-25 18:01:43 +00002660static void
2661shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2662{
2663 struct weston_output *output = shsurf->fullscreen_output;
2664
Rafael Antognolli03b16592013-12-03 15:35:42 -02002665 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002666
2667 if (!shsurf->fullscreen.black_view)
2668 shsurf->fullscreen.black_view =
2669 create_black_surface(shsurf->surface->compositor,
2670 shsurf->surface,
2671 output->x, output->y,
2672 output->width,
2673 output->height);
2674
2675 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2676 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2677 wl_list_insert(&shsurf->view->layer_link,
2678 &shsurf->fullscreen.black_view->layer_link);
2679 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2680 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002681
2682 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002683}
2684
Alex Wu4539b082012-03-01 12:57:46 +08002685/* Create black surface and append it to the associated fullscreen surface.
2686 * Handle size dismatch and positioning according to the method. */
2687static void
2688shell_configure_fullscreen(struct shell_surface *shsurf)
2689{
2690 struct weston_output *output = shsurf->fullscreen_output;
2691 struct weston_surface *surface = shsurf->surface;
2692 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002693 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002694 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002695
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002696 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2697 restore_output_mode(output);
2698
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002699 /* Reverse the effect of lower_fullscreen_layer() */
2700 wl_list_remove(&shsurf->view->layer_link);
2701 wl_list_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2702
Philip Withnalled8f1a92013-11-25 18:01:43 +00002703 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002704
Jason Ekstranda7af7042013-10-12 22:38:11 -05002705 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002706 &surf_width, &surf_height);
2707
Alex Wu4539b082012-03-01 12:57:46 +08002708 switch (shsurf->fullscreen.type) {
2709 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002710 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002711 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002712 break;
2713 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002714 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002715 if (output->width == surf_width &&
2716 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002717 weston_view_set_position(shsurf->view,
2718 output->x - surf_x,
2719 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002720 break;
2721 }
2722
Alex Wu4539b082012-03-01 12:57:46 +08002723 matrix = &shsurf->fullscreen.transform.matrix;
2724 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002725
Scott Moreau1bad5db2012-08-18 01:04:05 -06002726 output_aspect = (float) output->width /
2727 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002728 /* XXX: Use surf_width and surf_height here? */
2729 surface_aspect = (float) surface->width /
2730 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002731 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002732 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002733 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002734 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002735 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002736 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002737
Alex Wu4539b082012-03-01 12:57:46 +08002738 weston_matrix_scale(matrix, scale, scale, 1);
2739 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002741 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002742 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2743 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002744 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002745
Alex Wu4539b082012-03-01 12:57:46 +08002746 break;
2747 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002748 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002749 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002750 surf_width * surface->buffer_viewport.buffer.scale,
2751 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002752 shsurf->fullscreen.framerate};
2753
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002754 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002755 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756 weston_view_set_position(shsurf->view,
2757 output->x - surf_x,
2758 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002759 shsurf->fullscreen.black_view->surface->width = output->width;
2760 shsurf->fullscreen.black_view->surface->height = output->height;
2761 weston_view_set_position(shsurf->fullscreen.black_view,
2762 output->x - surf_x,
2763 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002764 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002765 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002766 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002767 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002768 }
Alex Wubd3354b2012-04-17 17:20:49 +08002769 }
Alex Wu4539b082012-03-01 12:57:46 +08002770 break;
2771 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002772 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002773 break;
2774 default:
2775 break;
2776 }
2777}
2778
Alex Wu4539b082012-03-01 12:57:46 +08002779static void
2780shell_map_fullscreen(struct shell_surface *shsurf)
2781{
Alex Wubd3354b2012-04-17 17:20:49 +08002782 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002783}
2784
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002785static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002786set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2787{
2788 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002789 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002790 shsurf->transient.x = x;
2791 shsurf->transient.y = y;
2792 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002793
2794 shell_surface_set_parent(shsurf, NULL);
2795
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002796 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002797 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002798}
2799
Kristian Høgsberg47792412014-05-04 13:47:06 -07002800static void
2801shell_interface_set_fullscreen(struct shell_surface *shsurf,
2802 uint32_t method,
2803 uint32_t framerate,
2804 struct weston_output *output)
2805{
2806 surface_clear_next_states(shsurf);
2807 set_fullscreen(shsurf, method, framerate, output);
2808
2809 shsurf->next_state.fullscreen = true;
2810 shsurf->state_changed = true;
2811}
2812
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002813static int
2814shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2815{
2816 return surface_move(shsurf, ws, 1);
2817}
2818
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002819static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002820
2821static void
2822destroy_shell_seat(struct wl_listener *listener, void *data)
2823{
2824 struct shell_seat *shseat =
2825 container_of(listener,
2826 struct shell_seat, seat_destroy_listener);
2827 struct shell_surface *shsurf, *prev = NULL;
2828
2829 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002830 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002831 shseat->popup_grab.client = NULL;
2832
2833 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2834 shsurf->popup.shseat = NULL;
2835 if (prev) {
2836 wl_list_init(&prev->popup.grab_link);
2837 }
2838 prev = shsurf;
2839 }
2840 wl_list_init(&prev->popup.grab_link);
2841 }
2842
2843 wl_list_remove(&shseat->seat_destroy_listener.link);
2844 free(shseat);
2845}
2846
Jason Ekstrand024177c2014-04-21 19:42:58 -05002847static void
2848shell_seat_caps_changed(struct wl_listener *listener, void *data)
2849{
2850 struct shell_seat *seat;
2851
2852 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2853
2854 if (seat->seat->keyboard &&
2855 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2856 wl_signal_add(&seat->seat->keyboard->focus_signal,
2857 &seat->keyboard_focus_listener);
2858 } else if (!seat->seat->keyboard) {
2859 wl_list_init(&seat->keyboard_focus_listener.link);
2860 }
2861
2862 if (seat->seat->pointer &&
2863 wl_list_empty(&seat->pointer_focus_listener.link)) {
2864 wl_signal_add(&seat->seat->pointer->focus_signal,
2865 &seat->pointer_focus_listener);
2866 } else if (!seat->seat->pointer) {
2867 wl_list_init(&seat->pointer_focus_listener.link);
2868 }
2869}
2870
Giulio Camuffo5085a752013-03-25 21:42:45 +01002871static struct shell_seat *
2872create_shell_seat(struct weston_seat *seat)
2873{
2874 struct shell_seat *shseat;
2875
2876 shseat = calloc(1, sizeof *shseat);
2877 if (!shseat) {
2878 weston_log("no memory to allocate shell seat\n");
2879 return NULL;
2880 }
2881
2882 shseat->seat = seat;
2883 wl_list_init(&shseat->popup_grab.surfaces_list);
2884
2885 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2886 wl_signal_add(&seat->destroy_signal,
2887 &shseat->seat_destroy_listener);
2888
Jason Ekstrand024177c2014-04-21 19:42:58 -05002889 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2890 wl_list_init(&shseat->keyboard_focus_listener.link);
2891
2892 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2893 wl_list_init(&shseat->pointer_focus_listener.link);
2894
2895 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2896 wl_signal_add(&seat->updated_caps_signal,
2897 &shseat->caps_changed_listener);
2898 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2899
Giulio Camuffo5085a752013-03-25 21:42:45 +01002900 return shseat;
2901}
2902
2903static struct shell_seat *
2904get_shell_seat(struct weston_seat *seat)
2905{
2906 struct wl_listener *listener;
2907
2908 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002909 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002910
2911 return container_of(listener,
2912 struct shell_seat, seat_destroy_listener);
2913}
2914
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002915static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002916popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002917{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002918 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002919 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002920 struct shell_seat *shseat =
2921 container_of(grab, struct shell_seat, popup_grab.grab);
2922 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002923 wl_fixed_t sx, sy;
2924
Jason Ekstranda7af7042013-10-12 22:38:11 -05002925 view = weston_compositor_pick_view(pointer->seat->compositor,
2926 pointer->x, pointer->y,
2927 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002928
Jason Ekstranda7af7042013-10-12 22:38:11 -05002929 if (view && view->surface->resource &&
2930 wl_resource_get_client(view->surface->resource) == client) {
2931 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002932 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002933 weston_pointer_set_focus(pointer, NULL,
2934 wl_fixed_from_int(0),
2935 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002936 }
2937}
2938
2939static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002940popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2941 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002942{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002943 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002944 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002945 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002946
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002947 weston_pointer_move(pointer, x, y);
2948
Neil Roberts96d790e2013-09-19 17:32:00 +01002949 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002950 weston_view_from_global_fixed(pointer->focus,
2951 pointer->x, pointer->y,
2952 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002953 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002954 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002955}
2956
2957static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002958popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002959 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002960{
2961 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002962 struct shell_seat *shseat =
2963 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002964 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002965 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002966 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002967 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002968
Neil Roberts96d790e2013-09-19 17:32:00 +01002969 resource_list = &grab->pointer->focus_resource_list;
2970 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002971 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002972 wl_resource_for_each(resource, resource_list) {
2973 wl_pointer_send_button(resource, serial,
2974 time, button, state);
2975 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002976 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002977 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002978 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002979 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002980 }
2981
Daniel Stone4dbadb12012-05-30 16:31:51 +01002982 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002983 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002984}
2985
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002986static void
2987popup_grab_cancel(struct weston_pointer_grab *grab)
2988{
2989 popup_grab_end(grab->pointer);
2990}
2991
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002992static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002993 popup_grab_focus,
2994 popup_grab_motion,
2995 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002996 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002997};
2998
2999static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003000shell_surface_send_popup_done(struct shell_surface *shsurf)
3001{
3002 if (shell_surface_is_wl_shell_surface(shsurf))
3003 wl_shell_surface_send_popup_done(shsurf->resource);
3004 else if (shell_surface_is_xdg_popup(shsurf))
3005 xdg_popup_send_popup_done(shsurf->resource,
3006 shsurf->popup.serial);
3007}
3008
3009static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003010popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003011{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003012 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003013 struct shell_seat *shseat =
3014 container_of(grab, struct shell_seat, popup_grab.grab);
3015 struct shell_surface *shsurf;
3016 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003017
3018 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003019 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003020 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003021 shseat->popup_grab.grab.interface = NULL;
3022 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003023 /* Send the popup_done event to all the popups open */
3024 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003025 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003026 shsurf->popup.shseat = NULL;
3027 if (prev) {
3028 wl_list_init(&prev->popup.grab_link);
3029 }
3030 prev = shsurf;
3031 }
3032 wl_list_init(&prev->popup.grab_link);
3033 wl_list_init(&shseat->popup_grab.surfaces_list);
3034 }
3035}
3036
3037static void
3038add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3039{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003040 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003041
3042 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003043 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003044 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003045 /* We must make sure here that this popup was opened after
3046 * a mouse press, and not just by moving around with other
3047 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003048 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003049 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003050
Rob Bradforddfe31052013-06-26 19:49:11 +01003051 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003052 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003053 } else {
3054 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003055 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003056}
3057
3058static void
3059remove_popup_grab(struct shell_surface *shsurf)
3060{
3061 struct shell_seat *shseat = shsurf->popup.shseat;
3062
3063 wl_list_remove(&shsurf->popup.grab_link);
3064 wl_list_init(&shsurf->popup.grab_link);
3065 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003066 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003067 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003068 }
3069}
3070
3071static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003072shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003073{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003074 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003075 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003076
Jason Ekstranda7af7042013-10-12 22:38:11 -05003077 shsurf->surface->output = parent_view->output;
3078 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003079
Jason Ekstranda7af7042013-10-12 22:38:11 -05003080 weston_view_set_transform_parent(shsurf->view, parent_view);
3081 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3082 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003083
Kristian Høgsberge3148752013-05-06 23:19:49 -04003084 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003085 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003086 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003087 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003088 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003089 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003090}
3091
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003092static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003093 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003094 shell_surface_move,
3095 shell_surface_resize,
3096 shell_surface_set_toplevel,
3097 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003098 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003099 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003100 shell_surface_set_maximized,
3101 shell_surface_set_title,
3102 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003103};
3104
3105static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003106destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003107{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003108 struct shell_surface *child, *next;
3109
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003110 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3111
Giulio Camuffo5085a752013-03-25 21:42:45 +01003112 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3113 remove_popup_grab(shsurf);
3114 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003115
Alex Wubd3354b2012-04-17 17:20:49 +08003116 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003117 shell_surface_is_top_fullscreen(shsurf))
3118 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003119
Jason Ekstranda7af7042013-10-12 22:38:11 -05003120 if (shsurf->fullscreen.black_view)
3121 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003122
Alex Wubd3354b2012-04-17 17:20:49 +08003123 /* As destroy_resource() use wl_list_for_each_safe(),
3124 * we can always remove the listener.
3125 */
3126 wl_list_remove(&shsurf->surface_destroy_listener.link);
3127 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003128 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003129
Jason Ekstranda7af7042013-10-12 22:38:11 -05003130 weston_view_destroy(shsurf->view);
3131
Philip Withnall648a4dd2013-11-25 18:01:44 +00003132 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003133 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3134 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003135
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003136 wl_list_remove(&shsurf->link);
3137 free(shsurf);
3138}
3139
3140static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003141shell_destroy_shell_surface(struct wl_resource *resource)
3142{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003143 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003144
Bryan Caina46b9462014-04-04 17:41:24 -05003145 if (!wl_list_empty(&shsurf->popup.grab_link))
3146 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003147 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003148}
3149
3150static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003151shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003152{
3153 struct shell_surface *shsurf = container_of(listener,
3154 struct shell_surface,
3155 surface_destroy_listener);
3156
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003157 if (shsurf->resource)
3158 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003159
3160 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003161}
3162
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003163static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003164fade_out_done(struct weston_view_animation *animation, void *data)
3165{
3166 struct shell_surface *shsurf = data;
3167
3168 weston_surface_destroy(shsurf->surface);
3169}
3170
3171static void
3172handle_resource_destroy(struct wl_listener *listener, void *data)
3173{
3174 struct shell_surface *shsurf =
3175 container_of(listener, struct shell_surface,
3176 resource_destroy_listener);
3177
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003178 if (!weston_surface_is_mapped(shsurf->surface))
3179 return;
3180
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003181 shsurf->surface->ref_count++;
3182
3183 pixman_region32_fini(&shsurf->surface->pending.input);
3184 pixman_region32_init(&shsurf->surface->pending.input);
3185 pixman_region32_fini(&shsurf->surface->input);
3186 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003187 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3188 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003189}
3190
3191static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003192shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003193
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003194struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003195get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003196{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003197 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003198 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003199 else
3200 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003201}
3202
Rafael Antognollie2a34552013-12-03 15:35:45 -02003203static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003204create_common_surface(struct shell_client *owner, void *shell,
3205 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003206 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003207{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003208 struct shell_surface *shsurf;
3209
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003210 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003211 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003212 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003213 }
3214
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003215 shsurf = calloc(1, sizeof *shsurf);
3216 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003217 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003218 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003219 }
3220
Jason Ekstranda7af7042013-10-12 22:38:11 -05003221 shsurf->view = weston_view_create(surface);
3222 if (!shsurf->view) {
3223 weston_log("no memory to allocate shell surface\n");
3224 free(shsurf);
3225 return NULL;
3226 }
3227
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003228 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003229 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003230
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003231 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3232 wl_resource_add_destroy_listener(surface->resource,
3233 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003234 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003235
Tiago Vignattibc052c92012-04-19 16:18:18 +03003236 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003237 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003238 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003239 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003240 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003241 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003242 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3243 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003244 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003245 wl_list_init(&shsurf->fullscreen.transform.link);
3246
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003247 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003248 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003249 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003250 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003251
3252 /* init link so its safe to always remove it in destroy_shell_surface */
3253 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003254 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003255
Pekka Paalanen460099f2012-01-20 16:48:25 +02003256 /* empty when not in use */
3257 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003258 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003259
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003260 wl_list_init(&shsurf->workspace_transform.link);
3261
Philip Withnall648a4dd2013-11-25 18:01:44 +00003262 wl_list_init(&shsurf->children_link);
3263 wl_list_init(&shsurf->children_list);
3264 shsurf->parent = NULL;
3265
Pekka Paalanen98262232011-12-01 10:42:22 +02003266 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003267
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003268 shsurf->client = client;
3269
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003270 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003271}
3272
Rafael Antognollie2a34552013-12-03 15:35:45 -02003273static struct shell_surface *
3274create_shell_surface(void *shell, struct weston_surface *surface,
3275 const struct weston_shell_client *client)
3276{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003277 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003278}
3279
Jason Ekstranda7af7042013-10-12 22:38:11 -05003280static struct weston_view *
3281get_primary_view(void *shell, struct shell_surface *shsurf)
3282{
3283 return shsurf->view;
3284}
3285
Tiago Vignattibc052c92012-04-19 16:18:18 +03003286static void
3287shell_get_shell_surface(struct wl_client *client,
3288 struct wl_resource *resource,
3289 uint32_t id,
3290 struct wl_resource *surface_resource)
3291{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003292 struct weston_surface *surface =
3293 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003294 struct shell_client *sc = wl_resource_get_user_data(resource);
3295 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003296 struct shell_surface *shsurf;
3297
3298 if (get_shell_surface(surface)) {
3299 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003300 WL_DISPLAY_ERROR_INVALID_OBJECT,
3301 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003302 return;
3303 }
3304
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003305 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003306 if (!shsurf) {
3307 wl_resource_post_error(surface_resource,
3308 WL_DISPLAY_ERROR_INVALID_OBJECT,
3309 "surface->configure already set");
3310 return;
3311 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003312
Jason Ekstranda85118c2013-06-27 20:17:02 -05003313 shsurf->resource =
3314 wl_resource_create(client,
3315 &wl_shell_surface_interface, 1, id);
3316 wl_resource_set_implementation(shsurf->resource,
3317 &shell_surface_implementation,
3318 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003319}
3320
Rafael Antognollie2a34552013-12-03 15:35:45 -02003321static bool
3322shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3323{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003324 /* A shell surface without a resource is created from xwayland
3325 * and is considered a wl_shell surface for now. */
3326
3327 return shsurf->resource == NULL ||
3328 wl_resource_instance_of(shsurf->resource,
3329 &wl_shell_surface_interface,
3330 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003331}
3332
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003333static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003334 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003335};
3336
Rafael Antognollie2a34552013-12-03 15:35:45 -02003337/****************************
3338 * xdg-shell implementation */
3339
3340static void
3341xdg_surface_destroy(struct wl_client *client,
3342 struct wl_resource *resource)
3343{
3344 wl_resource_destroy(resource);
3345}
3346
3347static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003348xdg_surface_set_transient_for(struct wl_client *client,
3349 struct wl_resource *resource,
3350 struct wl_resource *parent_resource)
3351{
3352 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3353 struct weston_surface *parent;
3354
3355 if (parent_resource)
3356 parent = wl_resource_get_user_data(parent_resource);
3357 else
3358 parent = NULL;
3359
3360 shell_surface_set_parent(shsurf, parent);
3361}
3362
3363static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003364xdg_surface_set_margin(struct wl_client *client,
3365 struct wl_resource *resource,
3366 int32_t left,
3367 int32_t right,
3368 int32_t top,
3369 int32_t bottom)
3370{
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003371 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3372
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07003373 set_margin(shsurf, left, right, top, bottom);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003374}
3375
3376static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003377xdg_surface_set_app_id(struct wl_client *client,
3378 struct wl_resource *resource,
3379 const char *app_id)
3380{
3381 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3382
3383 free(shsurf->class);
3384 shsurf->class = strdup(app_id);
3385}
3386
3387static void
3388xdg_surface_set_title(struct wl_client *client,
3389 struct wl_resource *resource, const char *title)
3390{
3391 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3392
3393 set_title(shsurf, title);
3394}
3395
3396static void
3397xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3398 struct wl_resource *seat_resource, uint32_t serial)
3399{
3400 common_surface_move(resource, seat_resource, serial);
3401}
3402
3403static void
3404xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3405 struct wl_resource *seat_resource, uint32_t serial,
3406 uint32_t edges)
3407{
3408 common_surface_resize(resource, seat_resource, serial, edges);
3409}
3410
3411static void
3412xdg_surface_set_output(struct wl_client *client,
3413 struct wl_resource *resource,
3414 struct wl_resource *output_resource)
3415{
3416 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3417 struct weston_output *output;
3418
3419 if (output_resource)
3420 output = wl_resource_get_user_data(output_resource);
3421 else
3422 output = NULL;
3423
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003424 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003425}
3426
3427static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003428xdg_surface_change_state(struct shell_surface *shsurf,
3429 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003430{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003431 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003432 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003433
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003434 switch (state) {
3435 case XDG_SURFACE_STATE_MAXIMIZED:
3436 shsurf->requested_state.maximized = value;
3437 if (value)
3438 set_maximized(shsurf, NULL);
3439 break;
3440 case XDG_SURFACE_STATE_FULLSCREEN:
3441 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003442
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003443 if (value)
3444 set_fullscreen(shsurf,
3445 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3446 0, shsurf->recommended_output);
3447 break;
3448 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003449}
3450
3451static void
3452xdg_surface_request_change_state(struct wl_client *client,
3453 struct wl_resource *resource,
3454 uint32_t state,
3455 uint32_t value,
3456 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003457{
3458 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003459
3460 /* The client can't know what the current state is, so we need
3461 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003462
3463 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3464 return;
3465
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003466 switch (state) {
3467 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003468 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003469 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003470 default:
3471 /* send error? ignore? send change state with value 0? */
3472 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003473 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003474
3475 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003476}
3477
3478static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003479xdg_surface_ack_change_state(struct wl_client *client,
3480 struct wl_resource *resource,
3481 uint32_t state,
3482 uint32_t value,
3483 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003484{
3485 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3486
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003487 if (shsurf->state_requested) {
3488 shsurf->next_state = shsurf->requested_state;
3489 shsurf->state_changed = true;
3490 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003491 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003492}
3493
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003494static void
3495xdg_surface_set_minimized(struct wl_client *client,
3496 struct wl_resource *resource)
3497{
3498 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3499
3500 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3501 return;
3502
3503 /* apply compositor's own minimization logic (hide) */
3504 set_minimized(shsurf->surface, 1);
3505}
3506
Rafael Antognollie2a34552013-12-03 15:35:45 -02003507static const struct xdg_surface_interface xdg_surface_implementation = {
3508 xdg_surface_destroy,
3509 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003510 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003511 xdg_surface_set_title,
3512 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003513 xdg_surface_move,
3514 xdg_surface_resize,
3515 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003516 xdg_surface_request_change_state,
3517 xdg_surface_ack_change_state,
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003518 xdg_surface_set_minimized
Rafael Antognollie2a34552013-12-03 15:35:45 -02003519};
3520
3521static void
3522xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003523 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003524{
3525 struct shell_surface *shsurf = get_shell_surface(surface);
3526
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003527 assert(shsurf);
3528
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07003529 if (shsurf->resource)
3530 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003531}
3532
3533static const struct weston_shell_client xdg_client = {
3534 xdg_send_configure
3535};
3536
3537static void
3538xdg_use_unstable_version(struct wl_client *client,
3539 struct wl_resource *resource,
3540 int32_t version)
3541{
3542 if (version > 1) {
3543 wl_resource_post_error(resource,
3544 1,
3545 "xdg-shell:: version not implemented yet.");
3546 return;
3547 }
3548}
3549
3550static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003551create_xdg_surface(struct shell_client *owner, void *shell,
3552 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003553 const struct weston_shell_client *client)
3554{
3555 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003556
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003557 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003558 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003559
3560 return shsurf;
3561}
3562
3563static void
3564xdg_get_xdg_surface(struct wl_client *client,
3565 struct wl_resource *resource,
3566 uint32_t id,
3567 struct wl_resource *surface_resource)
3568{
3569 struct weston_surface *surface =
3570 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003571 struct shell_client *sc = wl_resource_get_user_data(resource);
3572 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003573 struct shell_surface *shsurf;
3574
3575 if (get_shell_surface(surface)) {
3576 wl_resource_post_error(surface_resource,
3577 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003578 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003579 return;
3580 }
3581
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003582 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003583 if (!shsurf) {
3584 wl_resource_post_error(surface_resource,
3585 WL_DISPLAY_ERROR_INVALID_OBJECT,
3586 "surface->configure already set");
3587 return;
3588 }
3589
3590 shsurf->resource =
3591 wl_resource_create(client,
3592 &xdg_surface_interface, 1, id);
3593 wl_resource_set_implementation(shsurf->resource,
3594 &xdg_surface_implementation,
3595 shsurf, shell_destroy_shell_surface);
3596}
3597
3598static bool
3599shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3600{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003601 return shsurf->resource &&
3602 wl_resource_instance_of(shsurf->resource,
3603 &xdg_surface_interface,
3604 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003605}
3606
3607/* xdg-popup implementation */
3608
3609static void
3610xdg_popup_destroy(struct wl_client *client,
3611 struct wl_resource *resource)
3612{
3613 wl_resource_destroy(resource);
3614}
3615
Rafael Antognollie2a34552013-12-03 15:35:45 -02003616static const struct xdg_popup_interface xdg_popup_implementation = {
3617 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003618};
3619
3620static void
3621xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003622 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003623{
3624}
3625
3626static const struct weston_shell_client xdg_popup_client = {
3627 xdg_popup_send_configure
3628};
3629
3630static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003631create_xdg_popup(struct shell_client *owner, void *shell,
3632 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003633 const struct weston_shell_client *client,
3634 struct weston_surface *parent,
3635 struct shell_seat *seat,
3636 uint32_t serial,
3637 int32_t x, int32_t y)
3638{
3639 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003640
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003641 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003642 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003643 shsurf->popup.shseat = seat;
3644 shsurf->popup.serial = serial;
3645 shsurf->popup.x = x;
3646 shsurf->popup.y = y;
3647 shell_surface_set_parent(shsurf, parent);
3648
3649 return shsurf;
3650}
3651
3652static void
3653xdg_get_xdg_popup(struct wl_client *client,
3654 struct wl_resource *resource,
3655 uint32_t id,
3656 struct wl_resource *surface_resource,
3657 struct wl_resource *parent_resource,
3658 struct wl_resource *seat_resource,
3659 uint32_t serial,
3660 int32_t x, int32_t y, uint32_t flags)
3661{
3662 struct weston_surface *surface =
3663 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003664 struct shell_client *sc = wl_resource_get_user_data(resource);
3665 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003666 struct shell_surface *shsurf;
3667 struct weston_surface *parent;
3668 struct shell_seat *seat;
3669
3670 if (get_shell_surface(surface)) {
3671 wl_resource_post_error(surface_resource,
3672 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003673 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003674 return;
3675 }
3676
3677 if (!parent_resource) {
3678 wl_resource_post_error(surface_resource,
3679 WL_DISPLAY_ERROR_INVALID_OBJECT,
3680 "xdg_shell::get_xdg_popup requires a parent shell surface");
3681 }
3682
3683 parent = wl_resource_get_user_data(parent_resource);
3684 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3685
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003686 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003687 parent, seat, serial, x, y);
3688 if (!shsurf) {
3689 wl_resource_post_error(surface_resource,
3690 WL_DISPLAY_ERROR_INVALID_OBJECT,
3691 "surface->configure already set");
3692 return;
3693 }
3694
3695 shsurf->resource =
3696 wl_resource_create(client,
3697 &xdg_popup_interface, 1, id);
3698 wl_resource_set_implementation(shsurf->resource,
3699 &xdg_popup_implementation,
3700 shsurf, shell_destroy_shell_surface);
3701}
3702
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003703static void
3704xdg_pong(struct wl_client *client,
3705 struct wl_resource *resource, uint32_t serial)
3706{
3707 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003708
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003709 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003710}
3711
Rafael Antognollie2a34552013-12-03 15:35:45 -02003712static bool
3713shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3714{
3715 return wl_resource_instance_of(shsurf->resource,
3716 &xdg_popup_interface,
3717 &xdg_popup_implementation);
3718}
3719
3720static const struct xdg_shell_interface xdg_implementation = {
3721 xdg_use_unstable_version,
3722 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003723 xdg_get_xdg_popup,
3724 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003725};
3726
3727static int
3728xdg_shell_unversioned_dispatch(const void *implementation,
3729 void *_target, uint32_t opcode,
3730 const struct wl_message *message,
3731 union wl_argument *args)
3732{
3733 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003734 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003735
3736 if (opcode != 0) {
3737 wl_resource_post_error(resource,
3738 WL_DISPLAY_ERROR_INVALID_OBJECT,
3739 "must call use_unstable_version first");
3740 return 0;
3741 }
3742
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003743#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003744
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003745 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3746 "shell implementation doesn't match protocol version");
3747
Rafael Antognollie2a34552013-12-03 15:35:45 -02003748 if (args[0].i != XDG_SERVER_VERSION) {
3749 wl_resource_post_error(resource,
3750 WL_DISPLAY_ERROR_INVALID_OBJECT,
3751 "incompatible version, server is %d "
3752 "client wants %d",
3753 XDG_SERVER_VERSION, args[0].i);
3754 return 0;
3755 }
3756
3757 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003758 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003759
3760 return 1;
3761}
3762
3763/* end of xdg-shell implementation */
3764/***********************************/
3765
Kristian Høgsberg07937562011-04-12 17:25:42 -04003766static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003767shell_fade(struct desktop_shell *shell, enum fade_type type);
3768
3769static int
3770screensaver_timeout(void *data)
3771{
3772 struct desktop_shell *shell = data;
3773
3774 shell_fade(shell, FADE_OUT);
3775
3776 return 1;
3777}
3778
3779static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003780handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003781{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003782 struct desktop_shell *shell =
3783 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003784
Pekka Paalanen18027e52011-12-02 16:31:49 +02003785 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003786
3787 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003788 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003789}
3790
3791static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003792launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003793{
3794 if (shell->screensaver.binding)
3795 return;
3796
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003797 if (!shell->screensaver.path) {
3798 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003799 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003800 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003801
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003802 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003803 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003804 return;
3805 }
3806
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003807 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003808 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003809 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003810 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003811}
3812
3813static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003814terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003815{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003816 if (shell->screensaver.process.pid == 0)
3817 return;
3818
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003819 /* Disarm the screensaver timer, otherwise it may fire when the
3820 * compositor is not in the idle state. In that case, the screen will
3821 * be locked, but the wake_signal won't fire on user input, making the
3822 * system unresponsive. */
3823 wl_event_source_timer_update(shell->screensaver.timer, 0);
3824
Pekka Paalanen18027e52011-12-02 16:31:49 +02003825 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003826}
3827
3828static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003829configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003830{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003831 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003832
Jason Ekstranda7af7042013-10-12 22:38:11 -05003833 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3834 if (v->output == ev->output && v != ev) {
3835 weston_view_unmap(v);
3836 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003837 }
3838 }
3839
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003840 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003841
Jason Ekstranda7af7042013-10-12 22:38:11 -05003842 if (wl_list_empty(&ev->layer_link)) {
3843 wl_list_insert(&layer->view_list, &ev->layer_link);
3844 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003845 }
3846}
3847
3848static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003849background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003850{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003851 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003852 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003853
Jason Ekstranda7af7042013-10-12 22:38:11 -05003854 view = container_of(es->views.next, struct weston_view, surface_link);
3855
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003856 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003857}
3858
3859static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003860desktop_shell_set_background(struct wl_client *client,
3861 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003862 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003863 struct wl_resource *surface_resource)
3864{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003865 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003866 struct weston_surface *surface =
3867 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003868 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003869
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003870 if (surface->configure) {
3871 wl_resource_post_error(surface_resource,
3872 WL_DISPLAY_ERROR_INVALID_OBJECT,
3873 "surface role already assigned");
3874 return;
3875 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003876
Jason Ekstranda7af7042013-10-12 22:38:11 -05003877 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3878 weston_view_destroy(view);
3879 view = weston_view_create(surface);
3880
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003881 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003882 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003883 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003884 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003885 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003886 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003887 surface->output->width,
3888 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003889}
3890
3891static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003892panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003893{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003894 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003895 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003896
Jason Ekstranda7af7042013-10-12 22:38:11 -05003897 view = container_of(es->views.next, struct weston_view, surface_link);
3898
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003899 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003900}
3901
3902static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003903desktop_shell_set_panel(struct wl_client *client,
3904 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003905 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003906 struct wl_resource *surface_resource)
3907{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003908 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003909 struct weston_surface *surface =
3910 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003911 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003912
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003913 if (surface->configure) {
3914 wl_resource_post_error(surface_resource,
3915 WL_DISPLAY_ERROR_INVALID_OBJECT,
3916 "surface role already assigned");
3917 return;
3918 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003919
Jason Ekstranda7af7042013-10-12 22:38:11 -05003920 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3921 weston_view_destroy(view);
3922 view = weston_view_create(surface);
3923
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003924 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003925 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003926 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003927 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003928 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003929 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003930 surface->output->width,
3931 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003932}
3933
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003934static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003935lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003936{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003937 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003938 struct weston_view *view;
3939
3940 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003941
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003942 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003943 return;
3944
Jason Ekstranda7af7042013-10-12 22:38:11 -05003945 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003946
3947 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003948 wl_list_insert(&shell->lock_layer.view_list,
3949 &view->layer_link);
3950 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003951 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003952 }
3953}
3954
3955static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003956handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003957{
Tiago Vignattibe143262012-04-16 17:31:41 +03003958 struct desktop_shell *shell =
3959 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003960
Martin Minarik6d118362012-06-07 18:01:59 +02003961 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003962 shell->lock_surface = NULL;
3963}
3964
3965static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003966desktop_shell_set_lock_surface(struct wl_client *client,
3967 struct wl_resource *resource,
3968 struct wl_resource *surface_resource)
3969{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003970 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003971 struct weston_surface *surface =
3972 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003973
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003974 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003975
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003976 if (!shell->locked)
3977 return;
3978
Pekka Paalanen98262232011-12-01 10:42:22 +02003979 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003980
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003981 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003982 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003983 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003984
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003985 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003986 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003987 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003988}
3989
3990static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003991resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003992{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003993 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003994
Pekka Paalanen77346a62011-11-30 16:26:35 +02003995 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003996
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003997 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003998 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003999 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004000 &shell->input_panel_layer.link);
4001 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004002 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004003 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004004 wl_list_insert(&shell->compositor->cursor_layer.link,
4005 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004006 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004007 wl_list_insert(&shell->fullscreen_layer.link,
4008 &shell->panel_layer.link);
4009 wl_list_insert(&shell->panel_layer.link,
4010 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004011
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004012 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004013
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004014 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004015 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004016 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004017}
4018
4019static void
4020desktop_shell_unlock(struct wl_client *client,
4021 struct wl_resource *resource)
4022{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004023 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004024
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004025 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004026
4027 if (shell->locked)
4028 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004029}
4030
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004031static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004032desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004033 struct wl_resource *resource,
4034 struct wl_resource *surface_resource)
4035{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004036 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004037
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004038 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004039 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004040}
4041
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004042static void
4043desktop_shell_desktop_ready(struct wl_client *client,
4044 struct wl_resource *resource)
4045{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004046 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004047
4048 shell_fade_startup(shell);
4049}
4050
Kristian Høgsberg75840622011-09-06 13:48:16 -04004051static const struct desktop_shell_interface desktop_shell_implementation = {
4052 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004053 desktop_shell_set_panel,
4054 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004055 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004056 desktop_shell_set_grab_surface,
4057 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004058};
4059
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004060static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004061get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004062{
4063 struct shell_surface *shsurf;
4064
4065 shsurf = get_shell_surface(surface);
4066 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004067 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004068 return shsurf->type;
4069}
4070
Kristian Høgsberg75840622011-09-06 13:48:16 -04004071static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004072move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004073{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004074 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004075 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004076 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004077
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004078 if (seat->pointer->focus == NULL)
4079 return;
4080
4081 focus = seat->pointer->focus->surface;
4082
Pekka Paalanen01388e22013-04-25 13:57:44 +03004083 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004084 if (surface == NULL)
4085 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004086
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004087 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004088 if (shsurf == NULL || shsurf->state.fullscreen ||
4089 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004090 return;
4091
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004092 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004093}
4094
4095static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004096maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4097{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004098 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004099 struct weston_surface *surface;
4100 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004101 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004102
4103 surface = weston_surface_get_main_surface(focus);
4104 if (surface == NULL)
4105 return;
4106
4107 shsurf = get_shell_surface(surface);
4108 if (shsurf == NULL)
4109 return;
4110
4111 if (!shell_surface_is_xdg_surface(shsurf))
4112 return;
4113
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004114 serial = wl_display_next_serial(seat->compositor->wl_display);
4115 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4116 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004117}
4118
4119static void
4120fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4121{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004122 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004123 struct weston_surface *surface;
4124 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004125 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004126
4127 surface = weston_surface_get_main_surface(focus);
4128 if (surface == NULL)
4129 return;
4130
4131 shsurf = get_shell_surface(surface);
4132 if (shsurf == NULL)
4133 return;
4134
4135 if (!shell_surface_is_xdg_surface(shsurf))
4136 return;
4137
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004138 serial = wl_display_next_serial(seat->compositor->wl_display);
4139 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4140 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004141}
4142
4143static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004144touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4145{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004146 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004147 struct weston_surface *surface;
4148 struct shell_surface *shsurf;
4149
4150 surface = weston_surface_get_main_surface(focus);
4151 if (surface == NULL)
4152 return;
4153
4154 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004155 if (shsurf == NULL || shsurf->state.fullscreen ||
4156 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004157 return;
4158
4159 surface_touch_move(shsurf, (struct weston_seat *) seat);
4160}
4161
4162static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004163resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004164{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004165 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004166 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004167 uint32_t edges = 0;
4168 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004169 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004170
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004171 if (seat->pointer->focus == NULL)
4172 return;
4173
4174 focus = seat->pointer->focus->surface;
4175
Pekka Paalanen01388e22013-04-25 13:57:44 +03004176 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004177 if (surface == NULL)
4178 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004179
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004180 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004181 if (shsurf == NULL || shsurf->state.fullscreen ||
4182 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004183 return;
4184
Jason Ekstranda7af7042013-10-12 22:38:11 -05004185 weston_view_from_global(shsurf->view,
4186 wl_fixed_to_int(seat->pointer->grab_x),
4187 wl_fixed_to_int(seat->pointer->grab_y),
4188 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004189
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004190 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004191 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004192 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004193 edges |= 0;
4194 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004195 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004196
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004197 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004198 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004199 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004200 edges |= 0;
4201 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004202 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004203
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004204 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004205}
4206
4207static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004208surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004209 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004210{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004211 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004212 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004213 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004214 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004215
Pekka Paalanen01388e22013-04-25 13:57:44 +03004216 /* XXX: broken for windows containing sub-surfaces */
4217 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004218 if (surface == NULL)
4219 return;
4220
4221 shsurf = get_shell_surface(surface);
4222 if (!shsurf)
4223 return;
4224
Jason Ekstranda7af7042013-10-12 22:38:11 -05004225 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004226
Jason Ekstranda7af7042013-10-12 22:38:11 -05004227 if (shsurf->view->alpha > 1.0)
4228 shsurf->view->alpha = 1.0;
4229 if (shsurf->view->alpha < step)
4230 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004231
Jason Ekstranda7af7042013-10-12 22:38:11 -05004232 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004233 weston_surface_damage(surface);
4234}
4235
4236static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004237do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004238 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004239{
Daniel Stone37816df2012-05-16 18:45:18 +01004240 struct weston_seat *ws = (struct weston_seat *) seat;
4241 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004242 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004243 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004244
4245 wl_list_for_each(output, &compositor->output_list, link) {
4246 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004247 wl_fixed_to_double(seat->pointer->x),
4248 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004249 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004250 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004251 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004252 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004253 increment = -output->zoom.increment;
4254 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004255 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004256 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004257 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004258 else
4259 increment = 0;
4260
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004261 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004262
Scott Moreaue6603982012-06-11 13:07:51 -06004263 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004264 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004265 else if (output->zoom.level > output->zoom.max_level)
4266 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004267 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004268 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004269 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004270
Scott Moreaue6603982012-06-11 13:07:51 -06004271 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004272
Jason Ekstranda7af7042013-10-12 22:38:11 -05004273 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004274 }
4275 }
4276}
4277
Scott Moreauccbf29d2012-02-22 14:21:41 -07004278static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004279zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004280 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004281{
4282 do_zoom(seat, time, 0, axis, value);
4283}
4284
4285static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004286zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004287 void *data)
4288{
4289 do_zoom(seat, time, key, 0, 0);
4290}
4291
4292static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004293terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004294 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004295{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004296 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004297
Daniel Stone325fc2d2012-05-30 16:31:58 +01004298 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004299}
4300
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004301static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004302rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4303 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004304{
4305 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004306 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004307 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004308 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004309 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004310
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004311 weston_pointer_move(pointer, x, y);
4312
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004313 if (!shsurf)
4314 return;
4315
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004316 cx = 0.5f * shsurf->surface->width;
4317 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004318
Daniel Stone37816df2012-05-16 18:45:18 +01004319 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4320 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004321 r = sqrtf(dx * dx + dy * dy);
4322
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004323 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004324 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004325
4326 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004327 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004328 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004329
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004330 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004331 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004332
4333 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004334 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004335 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004336 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004337 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004338
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004339 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004340 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004341 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004342 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004343 wl_list_init(&shsurf->rotation.transform.link);
4344 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004345 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004346 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004347
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004348 /* We need to adjust the position of the surface
4349 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004350 cposx = shsurf->view->geometry.x + cx;
4351 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004352 dposx = rotate->center.x - cposx;
4353 dposy = rotate->center.y - cposy;
4354 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004355 weston_view_set_position(shsurf->view,
4356 shsurf->view->geometry.x + dposx,
4357 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004358 }
4359
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004360 /* Repaint implies weston_surface_update_transform(), which
4361 * lazily applies the damage due to rotation update.
4362 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004363 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004364}
4365
4366static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004367rotate_grab_button(struct weston_pointer_grab *grab,
4368 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004369{
4370 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004371 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004372 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004373 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004374 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004375
Daniel Stone4dbadb12012-05-30 16:31:51 +01004376 if (pointer->button_count == 0 &&
4377 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004378 if (shsurf)
4379 weston_matrix_multiply(&shsurf->rotation.rotation,
4380 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004381 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004382 free(rotate);
4383 }
4384}
4385
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004386static void
4387rotate_grab_cancel(struct weston_pointer_grab *grab)
4388{
4389 struct rotate_grab *rotate =
4390 container_of(grab, struct rotate_grab, base.grab);
4391
4392 shell_grab_end(&rotate->base);
4393 free(rotate);
4394}
4395
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004396static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004397 noop_grab_focus,
4398 rotate_grab_motion,
4399 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004400 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004401};
4402
4403static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004404surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004405{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004406 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004407 float dx, dy;
4408 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004409
Pekka Paalanen460099f2012-01-20 16:48:25 +02004410 rotate = malloc(sizeof *rotate);
4411 if (!rotate)
4412 return;
4413
Jason Ekstranda7af7042013-10-12 22:38:11 -05004414 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004415 surface->surface->width * 0.5f,
4416 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004417 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004418
Daniel Stone37816df2012-05-16 18:45:18 +01004419 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4420 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004421 r = sqrtf(dx * dx + dy * dy);
4422 if (r > 20.0f) {
4423 struct weston_matrix inverse;
4424
4425 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004426 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004427 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004428
4429 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004430 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004431 } else {
4432 weston_matrix_init(&surface->rotation.rotation);
4433 weston_matrix_init(&rotate->rotation);
4434 }
4435
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004436 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4437 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004438}
4439
4440static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004441rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004442 void *data)
4443{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004444 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004445 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004446 struct shell_surface *surface;
4447
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004448 if (seat->pointer->focus == NULL)
4449 return;
4450
4451 focus = seat->pointer->focus->surface;
4452
Pekka Paalanen01388e22013-04-25 13:57:44 +03004453 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004454 if (base_surface == NULL)
4455 return;
4456
4457 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004458 if (surface == NULL || surface->state.fullscreen ||
4459 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004460 return;
4461
4462 surface_rotate(surface, seat);
4463}
4464
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004465/* Move all fullscreen layers down to the current workspace and hide their
4466 * black views. The surfaces' state is set to both fullscreen and lowered,
4467 * and this is reversed when such a surface is re-configured, see
4468 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4469 *
4470 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004471 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004472void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004473lower_fullscreen_layer(struct desktop_shell *shell)
4474{
4475 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004476 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004477
4478 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004479 wl_list_for_each_reverse_safe(view, prev,
4480 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004481 layer_link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004482 struct shell_surface *shsurf = get_shell_surface(view->surface);
4483
4484 if (!shsurf)
4485 continue;
4486
4487 /* We can have a non-fullscreen popup for a fullscreen surface
4488 * in the fullscreen layer. */
4489 if (shsurf->state.fullscreen) {
4490 /* Hide the black view */
4491 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
4492 wl_list_init(&shsurf->fullscreen.black_view->layer_link);
4493 }
4494
4495 /* Lower the view to the workspace layer */
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004496 wl_list_remove(&view->layer_link);
4497 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4498 weston_view_damage_below(view);
4499 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004500
4501 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004502 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004503}
4504
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004505void
Tiago Vignattibe143262012-04-16 17:31:41 +03004506activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004507 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004508{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004509 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004510 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004511 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004512 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004513 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004514
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004515 lower_fullscreen_layer(shell);
4516
Pekka Paalanen01388e22013-04-25 13:57:44 +03004517 main_surface = weston_surface_get_main_surface(es);
4518
Daniel Stone37816df2012-05-16 18:45:18 +01004519 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004520
Jonas Ådahl8538b222012-08-29 22:13:03 +02004521 state = ensure_focus_state(shell, seat);
4522 if (state == NULL)
4523 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004524
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004525 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004526 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004527
Rafael Antognolli03b16592013-12-03 15:35:42 -02004528 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004529 assert(shsurf);
4530
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004531 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004532 shell_configure_fullscreen(shsurf);
4533 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004534 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004535
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004536 /* Update the surface’s layer. This brings it to the top of the stacking
4537 * order as appropriate. */
4538 shell_surface_update_layer(shsurf);
4539
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004540 if (shell->focus_animation_type != ANIMATION_NONE) {
4541 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004542 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004543 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004544}
4545
Alex Wu21858432012-04-01 20:13:08 +08004546/* no-op func for checking black surface */
4547static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004548black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004549{
4550}
4551
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004552static bool
Alex Wu21858432012-04-01 20:13:08 +08004553is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4554{
4555 if (es->configure == black_surface_configure) {
4556 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004557 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004558 return true;
4559 }
4560 return false;
4561}
4562
Kristian Høgsberg75840622011-09-06 13:48:16 -04004563static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004564activate_binding(struct weston_seat *seat,
4565 struct desktop_shell *shell,
4566 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004567{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004568 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004569
Alex Wu9c35e6b2012-03-05 14:13:13 +08004570 if (!focus)
4571 return;
4572
Pekka Paalanen01388e22013-04-25 13:57:44 +03004573 if (is_black_surface(focus, &main_surface))
4574 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004575
Pekka Paalanen01388e22013-04-25 13:57:44 +03004576 main_surface = weston_surface_get_main_surface(focus);
4577 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004578 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004579
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004580 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004581}
4582
4583static void
4584click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4585 void *data)
4586{
4587 if (seat->pointer->grab != &seat->pointer->default_grab)
4588 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004589 if (seat->pointer->focus == NULL)
4590 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004591
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004592 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004593}
4594
4595static void
4596touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4597{
4598 if (seat->touch->grab != &seat->touch->default_grab)
4599 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004600 if (seat->touch->focus == NULL)
4601 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004602
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004603 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004604}
4605
4606static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004607unfocus_all_seats(struct desktop_shell *shell)
4608{
4609 struct weston_seat *seat, *next;
4610
4611 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4612 if (seat->keyboard == NULL)
4613 continue;
4614
4615 weston_keyboard_set_focus(seat->keyboard, NULL);
4616 }
4617}
4618
4619static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004620lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004621{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004622 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004623
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004624 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004625 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004626 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004627 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004628
4629 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004630
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004631 /* Hide all surfaces by removing the fullscreen, panel and
4632 * toplevel layers. This way nothing else can show or receive
4633 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004634
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004635 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004636 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004637 if (shell->showing_input_panels)
4638 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004639 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004640 wl_list_insert(&shell->compositor->cursor_layer.link,
4641 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004642
Pekka Paalanen77346a62011-11-30 16:26:35 +02004643 launch_screensaver(shell);
4644
Neil Robertsb4a91702014-04-09 16:33:32 +01004645 /* Remove the keyboard focus on all seats. This will be
4646 * restored to the workspace's saved state via
4647 * restore_focus_state when the compositor is unlocked */
4648 unfocus_all_seats(shell);
4649
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004650 /* TODO: disable bindings that should not work while locked. */
4651
4652 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004653}
4654
4655static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004656unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004657{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004658 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004659 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004660 return;
4661 }
4662
4663 /* If desktop-shell client has gone away, unlock immediately. */
4664 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004665 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004666 return;
4667 }
4668
4669 if (shell->prepare_event_sent)
4670 return;
4671
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004672 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004673 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004674}
4675
4676static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004677shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004678{
4679 struct desktop_shell *shell = data;
4680
4681 shell->fade.animation = NULL;
4682
4683 switch (shell->fade.type) {
4684 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004685 weston_surface_destroy(shell->fade.view->surface);
4686 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004687 break;
4688 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004689 lock(shell);
4690 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004691 default:
4692 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004693 }
4694}
4695
Jason Ekstranda7af7042013-10-12 22:38:11 -05004696static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004697shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004698{
4699 struct weston_compositor *compositor = shell->compositor;
4700 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004701 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004702
4703 surface = weston_surface_create(compositor);
4704 if (!surface)
4705 return NULL;
4706
Jason Ekstranda7af7042013-10-12 22:38:11 -05004707 view = weston_view_create(surface);
4708 if (!view) {
4709 weston_surface_destroy(surface);
4710 return NULL;
4711 }
4712
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004713 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004714 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004715 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004716 wl_list_insert(&compositor->fade_layer.view_list,
4717 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004718 pixman_region32_init(&surface->input);
4719
Jason Ekstranda7af7042013-10-12 22:38:11 -05004720 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004721}
4722
4723static void
4724shell_fade(struct desktop_shell *shell, enum fade_type type)
4725{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004726 float tint;
4727
4728 switch (type) {
4729 case FADE_IN:
4730 tint = 0.0;
4731 break;
4732 case FADE_OUT:
4733 tint = 1.0;
4734 break;
4735 default:
4736 weston_log("shell: invalid fade type\n");
4737 return;
4738 }
4739
4740 shell->fade.type = type;
4741
Jason Ekstranda7af7042013-10-12 22:38:11 -05004742 if (shell->fade.view == NULL) {
4743 shell->fade.view = shell_fade_create_surface(shell);
4744 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004745 return;
4746
Jason Ekstranda7af7042013-10-12 22:38:11 -05004747 shell->fade.view->alpha = 1.0 - tint;
4748 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004749 }
4750
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004751 if (shell->fade.view->output == NULL) {
4752 /* If the black view gets a NULL output, we lost the
4753 * last output and we'll just cancel the fade. This
4754 * happens when you close the last window under the
4755 * X11 or Wayland backends. */
4756 shell->locked = false;
4757 weston_surface_destroy(shell->fade.view->surface);
4758 shell->fade.view = NULL;
4759 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004760 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004761 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004762 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004763 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004764 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004765 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004766 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004767}
4768
4769static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004770do_shell_fade_startup(void *data)
4771{
4772 struct desktop_shell *shell = data;
4773
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004774 if (shell->startup_animation_type == ANIMATION_FADE)
4775 shell_fade(shell, FADE_IN);
4776 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004777 weston_surface_destroy(shell->fade.view->surface);
4778 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004779 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004780}
4781
4782static void
4783shell_fade_startup(struct desktop_shell *shell)
4784{
4785 struct wl_event_loop *loop;
4786
4787 if (!shell->fade.startup_timer)
4788 return;
4789
4790 wl_event_source_remove(shell->fade.startup_timer);
4791 shell->fade.startup_timer = NULL;
4792
4793 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4794 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4795}
4796
4797static int
4798fade_startup_timeout(void *data)
4799{
4800 struct desktop_shell *shell = data;
4801
4802 shell_fade_startup(shell);
4803 return 0;
4804}
4805
4806static void
4807shell_fade_init(struct desktop_shell *shell)
4808{
4809 /* Make compositor output all black, and wait for the desktop-shell
4810 * client to signal it is ready, then fade in. The timer triggers a
4811 * fade-in, in case the desktop-shell client takes too long.
4812 */
4813
4814 struct wl_event_loop *loop;
4815
Jason Ekstranda7af7042013-10-12 22:38:11 -05004816 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004817 weston_log("%s: warning: fade surface already exists\n",
4818 __func__);
4819 return;
4820 }
4821
Jason Ekstranda7af7042013-10-12 22:38:11 -05004822 shell->fade.view = shell_fade_create_surface(shell);
4823 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004824 return;
4825
Jason Ekstranda7af7042013-10-12 22:38:11 -05004826 weston_view_update_transform(shell->fade.view);
4827 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004828
4829 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4830 shell->fade.startup_timer =
4831 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4832 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4833}
4834
4835static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004836idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004837{
4838 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004839 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004840 struct weston_seat *seat;
4841
4842 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4843 if (seat->pointer)
4844 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004845
4846 shell_fade(shell, FADE_OUT);
4847 /* lock() is called from shell_fade_done() */
4848}
4849
4850static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004851wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004852{
4853 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004854 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004855
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004856 unlock(shell);
4857}
4858
4859static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004860center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004861{
Giulio Camuffob8366642013-04-25 13:57:46 +03004862 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004863 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004864
Jason Ekstranda7af7042013-10-12 22:38:11 -05004865 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004866
4867 x = output->x + (output->width - width) / 2 - surf_x / 2;
4868 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004869
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004870 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004871}
4872
4873static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004874weston_view_set_initial_position(struct weston_view *view,
4875 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004876{
4877 struct weston_compositor *compositor = shell->compositor;
4878 int ix = 0, iy = 0;
4879 int range_x, range_y;
4880 int dx, dy, x, y, panel_height;
4881 struct weston_output *output, *target_output = NULL;
4882 struct weston_seat *seat;
4883
4884 /* As a heuristic place the new window on the same output as the
4885 * pointer. Falling back to the output containing 0, 0.
4886 *
4887 * TODO: Do something clever for touch too?
4888 */
4889 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004890 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004891 ix = wl_fixed_to_int(seat->pointer->x);
4892 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004893 break;
4894 }
4895 }
4896
4897 wl_list_for_each(output, &compositor->output_list, link) {
4898 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4899 target_output = output;
4900 break;
4901 }
4902 }
4903
4904 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004905 weston_view_set_position(view, 10 + random() % 400,
4906 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004907 return;
4908 }
4909
4910 /* Valid range within output where the surface will still be onscreen.
4911 * If this is negative it means that the surface is bigger than
4912 * output.
4913 */
4914 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004915 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004916 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004917 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004918
Rob Bradford4cb88c72012-08-13 15:18:44 +01004919 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004920 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004921 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004922 dx = 0;
4923
4924 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004925 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004926 else
4927 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004928
4929 x = target_output->x + dx;
4930 y = target_output->y + dy;
4931
Jason Ekstranda7af7042013-10-12 22:38:11 -05004932 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004933}
4934
4935static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004936map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004937 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004938{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004939 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004940 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004941 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004942 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004943
Pekka Paalanen77346a62011-11-30 16:26:35 +02004944 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004945 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004946 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004947 if (shsurf->state.fullscreen) {
4948 center_on_output(shsurf->view, shsurf->fullscreen_output);
4949 shell_map_fullscreen(shsurf);
4950 } else if (shsurf->state.maximized) {
4951 /* use surface configure to set the geometry */
4952 panel_height = get_output_panel_height(shell, shsurf->output);
4953 surface_subsurfaces_boundingbox(shsurf->surface,
4954 &surf_x, &surf_y, NULL, NULL);
4955 weston_view_set_position(shsurf->view,
4956 shsurf->output->x - surf_x,
4957 shsurf->output->y +
4958 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004959 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004960 weston_view_set_initial_position(shsurf->view, shell);
4961 }
Juan Zhao96879df2012-02-07 08:45:41 +08004962 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004963 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004964 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004965 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004966 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004967 weston_view_set_position(shsurf->view,
4968 shsurf->view->geometry.x + sx,
4969 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004970 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004971 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004972 default:
4973 ;
4974 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004975
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004976 /* Surface stacking order, see also activate(). */
4977 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004978
Jason Ekstranda7af7042013-10-12 22:38:11 -05004979 if (shsurf->type != SHELL_SURFACE_NONE) {
4980 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004981 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004982 shsurf->surface->output = shsurf->output;
4983 shsurf->view->output = shsurf->output;
4984 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004985 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004986
Jason Ekstranda7af7042013-10-12 22:38:11 -05004987 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004988 /* XXX: xwayland's using the same fields for transient type */
4989 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004990 if (shsurf->transient.flags ==
4991 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4992 break;
4993 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004994 if (shsurf->state.relative &&
4995 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4996 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004997 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004998 break;
4999 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005000 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005001 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005002 case SHELL_SURFACE_POPUP:
5003 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005004 default:
5005 break;
5006 }
5007
Rafael Antognolli03b16592013-12-03 15:35:42 -02005008 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5009 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005010 {
5011 switch (shell->win_animation_type) {
5012 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005013 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005014 break;
5015 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005016 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005017 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005018 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005019 default:
5020 break;
5021 }
5022 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005023}
5024
5025static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005026configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005027 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005028{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005029 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005030 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005031 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005032
Pekka Paalanen77346a62011-11-30 16:26:35 +02005033 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005034
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005035 assert(shsurf);
5036
Rafael Antognolli03b16592013-12-03 15:35:42 -02005037 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005038 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005039 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005040 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005041 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5042 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005043 mx = shsurf->output->x - surf_x;
5044 my = shsurf->output->y +
5045 get_output_panel_height(shell,shsurf->output) - surf_y;
5046 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005047 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005048 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005049 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005050
Alex Wu4539b082012-03-01 12:57:46 +08005051 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005052 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005053 wl_list_for_each(view, &surface->views, surface_link)
5054 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005055
Rafael Antognolli03b16592013-12-03 15:35:42 -02005056 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005057 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005058 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005059}
5060
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005061static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005062shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005063{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005064 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005065 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005066 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005067
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005068 assert(shsurf);
5069
5070 shell = shsurf->shell;
5071
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005072 if (!weston_surface_is_mapped(es) &&
5073 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005074 remove_popup_grab(shsurf);
5075 }
5076
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005077 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005078 return;
5079
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005080 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005081 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005082 type_changed = 1;
5083 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005084
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005085 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005086 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005087 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005088 shsurf->last_width != es->width ||
5089 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005090 float from_x, from_y;
5091 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005092
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005093 if (shsurf->resize_edges) {
5094 sx = 0;
5095 sy = 0;
5096 }
5097
5098 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5099 sx = shsurf->last_width - es->width;
5100 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5101 sy = shsurf->last_height - es->height;
5102
5103 shsurf->last_width = es->width;
5104 shsurf->last_height = es->height;
5105
Jason Ekstranda7af7042013-10-12 22:38:11 -05005106 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5107 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005108 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005109 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005110 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005111 }
5112}
5113
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005114static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005115
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005116static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005117desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005118{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005119 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005120 struct desktop_shell *shell =
5121 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005122
5123 shell->child.process.pid = 0;
5124 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005125
5126 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5127 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005128 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005129 shell->child.deathstamp = time;
5130 shell->child.deathcount = 0;
5131 }
5132
5133 shell->child.deathcount++;
5134 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005135 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005136 return;
5137 }
5138
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005139 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005140 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005141 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005142}
5143
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005144static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005145desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5146{
5147 struct desktop_shell *shell;
5148
5149 shell = container_of(listener, struct desktop_shell,
5150 child.client_destroy_listener);
5151
5152 shell->child.client = NULL;
5153}
5154
5155static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005156launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005157{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005158 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005159
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005160 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005161 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005162 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005163 desktop_shell_sigchld);
5164
5165 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005166 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005167
5168 shell->child.client_destroy_listener.notify =
5169 desktop_shell_client_destroy;
5170 wl_client_add_destroy_listener(shell->child.client,
5171 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005172}
5173
5174static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005175handle_shell_client_destroy(struct wl_listener *listener, void *data)
5176{
5177 struct shell_client *sc =
5178 container_of(listener, struct shell_client, destroy_listener);
5179
5180 if (sc->ping_timer)
5181 wl_event_source_remove(sc->ping_timer);
5182 free(sc);
5183}
5184
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005185static struct shell_client *
5186shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5187 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005188{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005189 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005190
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005191 sc = zalloc(sizeof *sc);
5192 if (sc == NULL) {
5193 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005194 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005195 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005196
5197 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005198 if (sc->resource == NULL) {
5199 free(sc);
5200 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005201 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005202 }
5203
5204 sc->client = client;
5205 sc->shell = shell;
5206 sc->destroy_listener.notify = handle_shell_client_destroy;
5207 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5208
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005209 return sc;
5210}
5211
5212static void
5213bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5214{
5215 struct desktop_shell *shell = data;
5216 struct shell_client *sc;
5217
5218 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5219 if (sc)
5220 wl_resource_set_implementation(sc->resource,
5221 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005222 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005223}
5224
5225static void
5226bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5227{
5228 struct desktop_shell *shell = data;
5229 struct shell_client *sc;
5230
5231 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5232 if (sc)
5233 wl_resource_set_dispatcher(sc->resource,
5234 xdg_shell_unversioned_dispatch,
5235 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005236}
5237
5238static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005239unbind_desktop_shell(struct wl_resource *resource)
5240{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005241 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005242
5243 if (shell->locked)
5244 resume_desktop(shell);
5245
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005246 shell->child.desktop_shell = NULL;
5247 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005248}
5249
5250static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005251bind_desktop_shell(struct wl_client *client,
5252 void *data, uint32_t version, uint32_t id)
5253{
Tiago Vignattibe143262012-04-16 17:31:41 +03005254 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005255 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005256
Jason Ekstranda85118c2013-06-27 20:17:02 -05005257 resource = wl_resource_create(client, &desktop_shell_interface,
5258 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005259
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005260 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005261 wl_resource_set_implementation(resource,
5262 &desktop_shell_implementation,
5263 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005264 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005265
5266 if (version < 2)
5267 shell_fade_startup(shell);
5268
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005269 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005270 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005271
5272 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5273 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005274 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005275}
5276
Pekka Paalanen6e168112011-11-24 11:34:05 +02005277static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005278screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005279{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005280 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005281 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005282
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005283 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005284 return;
5285
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005286 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005287 if (!shell->locked)
5288 return;
5289
Jason Ekstranda7af7042013-10-12 22:38:11 -05005290 view = container_of(surface->views.next, struct weston_view, surface_link);
5291 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005292
Jason Ekstranda7af7042013-10-12 22:38:11 -05005293 if (wl_list_empty(&view->layer_link)) {
5294 wl_list_insert(shell->lock_layer.view_list.prev,
5295 &view->layer_link);
5296 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005297 wl_event_source_timer_update(shell->screensaver.timer,
5298 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005299 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005300 }
5301}
5302
5303static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005304screensaver_set_surface(struct wl_client *client,
5305 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005306 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005307 struct wl_resource *output_resource)
5308{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005309 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005310 struct weston_surface *surface =
5311 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005312 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005313 struct weston_view *view, *next;
5314
5315 /* Make sure we only have one view */
5316 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5317 weston_view_destroy(view);
5318 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005319
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005320 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005321 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005322 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005323}
5324
5325static const struct screensaver_interface screensaver_implementation = {
5326 screensaver_set_surface
5327};
5328
5329static void
5330unbind_screensaver(struct wl_resource *resource)
5331{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005332 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005333
Pekka Paalanen77346a62011-11-30 16:26:35 +02005334 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005335}
5336
5337static void
5338bind_screensaver(struct wl_client *client,
5339 void *data, uint32_t version, uint32_t id)
5340{
Tiago Vignattibe143262012-04-16 17:31:41 +03005341 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005342 struct wl_resource *resource;
5343
Jason Ekstranda85118c2013-06-27 20:17:02 -05005344 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005345
Pekka Paalanen77346a62011-11-30 16:26:35 +02005346 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005347 wl_resource_set_implementation(resource,
5348 &screensaver_implementation,
5349 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005350 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005351 return;
5352 }
5353
5354 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5355 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005356 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005357}
5358
Kristian Høgsberg07045392012-02-19 18:52:44 -05005359struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005360 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005361 struct weston_surface *current;
5362 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005363 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005364 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005365};
5366
5367static void
5368switcher_next(struct switcher *switcher)
5369{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005370 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005371 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005372 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005373 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005374
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005375 /* temporary re-display minimized surfaces */
5376 struct weston_view *tmp;
5377 struct weston_view **minimized;
5378 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5379 wl_list_remove(&view->layer_link);
5380 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5381 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5382 *minimized = view;
5383 }
5384
Jason Ekstranda7af7042013-10-12 22:38:11 -05005385 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005386 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005387 if (shsurf &&
5388 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5389 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005390 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005391 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005392 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005393 next = view->surface;
5394 prev = view->surface;
5395 view->alpha = 0.25;
5396 weston_view_geometry_dirty(view);
5397 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005398 }
Alex Wu1659daa2012-04-01 20:13:09 +08005399
Jason Ekstranda7af7042013-10-12 22:38:11 -05005400 if (is_black_surface(view->surface, NULL)) {
5401 view->alpha = 0.25;
5402 weston_view_geometry_dirty(view);
5403 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005404 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005405 }
5406
5407 if (next == NULL)
5408 next = first;
5409
Alex Wu07b26062012-03-12 16:06:01 +08005410 if (next == NULL)
5411 return;
5412
Kristian Høgsberg07045392012-02-19 18:52:44 -05005413 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005414 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005415
5416 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005417 wl_list_for_each(view, &next->views, surface_link)
5418 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005419
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005420 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005421 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005422 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005423}
5424
5425static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005426switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005427{
5428 struct switcher *switcher =
5429 container_of(listener, struct switcher, listener);
5430
5431 switcher_next(switcher);
5432}
5433
5434static void
Daniel Stone351eb612012-05-31 15:27:47 -04005435switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005436{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005437 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005438 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005439 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005440
Jason Ekstranda7af7042013-10-12 22:38:11 -05005441 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005442 if (is_focus_view(view))
5443 continue;
5444
Jason Ekstranda7af7042013-10-12 22:38:11 -05005445 view->alpha = 1.0;
5446 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005447 }
5448
Alex Wu07b26062012-03-12 16:06:01 +08005449 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005450 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005451 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005452 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005453 weston_keyboard_end_grab(keyboard);
5454 if (keyboard->input_method_resource)
5455 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005456
5457 /* re-hide surfaces that were temporary shown during the switch */
5458 struct weston_view **minimized;
5459 wl_array_for_each(minimized, &switcher->minimized_array) {
5460 /* with the exception of the current selected */
5461 if ((*minimized)->surface != switcher->current) {
5462 wl_list_remove(&(*minimized)->layer_link);
5463 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5464 weston_view_damage_below(*minimized);
5465 }
5466 }
5467 wl_array_release(&switcher->minimized_array);
5468
Kristian Høgsberg07045392012-02-19 18:52:44 -05005469 free(switcher);
5470}
5471
5472static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005473switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005474 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005475{
5476 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005477 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005478
Daniel Stonec9785ea2012-05-30 16:31:52 +01005479 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005480 switcher_next(switcher);
5481}
5482
5483static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005484switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005485 uint32_t mods_depressed, uint32_t mods_latched,
5486 uint32_t mods_locked, uint32_t group)
5487{
5488 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005489 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005490
Daniel Stone351eb612012-05-31 15:27:47 -04005491 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5492 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005493}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005494
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005495static void
5496switcher_cancel(struct weston_keyboard_grab *grab)
5497{
5498 struct switcher *switcher = container_of(grab, struct switcher, grab);
5499
5500 switcher_destroy(switcher);
5501}
5502
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005503static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005504 switcher_key,
5505 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005506 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005507};
5508
5509static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005510switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005511 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005512{
Tiago Vignattibe143262012-04-16 17:31:41 +03005513 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005514 struct switcher *switcher;
5515
5516 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005517 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005518 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005519 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005520 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005521 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005522
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005523 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005524 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005525 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005526 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5527 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005528 switcher_next(switcher);
5529}
5530
Pekka Paalanen3c647232011-12-22 13:43:43 +02005531static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005532backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005533 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005534{
5535 struct weston_compositor *compositor = data;
5536 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005537 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005538
5539 /* TODO: we're limiting to simple use cases, where we assume just
5540 * control on the primary display. We'd have to extend later if we
5541 * ever get support for setting backlights on random desktop LCD
5542 * panels though */
5543 output = get_default_output(compositor);
5544 if (!output)
5545 return;
5546
5547 if (!output->set_backlight)
5548 return;
5549
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005550 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5551 backlight_new = output->backlight_current - 25;
5552 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5553 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005554
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005555 if (backlight_new < 5)
5556 backlight_new = 5;
5557 if (backlight_new > 255)
5558 backlight_new = 255;
5559
5560 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005561 output->set_backlight(output, output->backlight_current);
5562}
5563
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005564struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005565 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005566 struct weston_seat *seat;
5567 uint32_t key[2];
5568 int key_released[2];
5569};
5570
5571static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005572debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005573 uint32_t key, uint32_t state)
5574{
5575 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005576 struct weston_compositor *ec = db->seat->compositor;
5577 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005578 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005579 uint32_t serial;
5580 int send = 0, terminate = 0;
5581 int check_binding = 1;
5582 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005583 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005584
5585 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5586 /* Do not run bindings on key releases */
5587 check_binding = 0;
5588
5589 for (i = 0; i < 2; i++)
5590 if (key == db->key[i])
5591 db->key_released[i] = 1;
5592
5593 if (db->key_released[0] && db->key_released[1]) {
5594 /* All key releases been swalled so end the grab */
5595 terminate = 1;
5596 } else if (key != db->key[0] && key != db->key[1]) {
5597 /* Should not swallow release of other keys */
5598 send = 1;
5599 }
5600 } else if (key == db->key[0] && !db->key_released[0]) {
5601 /* Do not check bindings for the first press of the binding
5602 * key. This allows it to be used as a debug shortcut.
5603 * We still need to swallow this event. */
5604 check_binding = 0;
5605 } else if (db->key[1]) {
5606 /* If we already ran a binding don't process another one since
5607 * we can't keep track of all the binding keys that were
5608 * pressed in order to swallow the release events. */
5609 send = 1;
5610 check_binding = 0;
5611 }
5612
5613 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005614 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5615 key, state)) {
5616 /* We ran a binding so swallow the press and keep the
5617 * grab to swallow the released too. */
5618 send = 0;
5619 terminate = 0;
5620 db->key[1] = key;
5621 } else {
5622 /* Terminate the grab since the key pressed is not a
5623 * debug binding key. */
5624 send = 1;
5625 terminate = 1;
5626 }
5627 }
5628
5629 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005630 serial = wl_display_next_serial(display);
5631 resource_list = &grab->keyboard->focus_resource_list;
5632 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005633 wl_keyboard_send_key(resource, serial, time, key, state);
5634 }
5635 }
5636
5637 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005638 weston_keyboard_end_grab(grab->keyboard);
5639 if (grab->keyboard->input_method_resource)
5640 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005641 free(db);
5642 }
5643}
5644
5645static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005646debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005647 uint32_t mods_depressed, uint32_t mods_latched,
5648 uint32_t mods_locked, uint32_t group)
5649{
5650 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005651 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005652
Neil Roberts96d790e2013-09-19 17:32:00 +01005653 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005654
Neil Roberts96d790e2013-09-19 17:32:00 +01005655 wl_resource_for_each(resource, resource_list) {
5656 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5657 mods_latched, mods_locked, group);
5658 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005659}
5660
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005661static void
5662debug_binding_cancel(struct weston_keyboard_grab *grab)
5663{
5664 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5665
5666 weston_keyboard_end_grab(grab->keyboard);
5667 free(db);
5668}
5669
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005670struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005671 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005672 debug_binding_modifiers,
5673 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005674};
5675
5676static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005677debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005678{
5679 struct debug_binding_grab *grab;
5680
5681 grab = calloc(1, sizeof *grab);
5682 if (!grab)
5683 return;
5684
5685 grab->seat = (struct weston_seat *) seat;
5686 grab->key[0] = key;
5687 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005688 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005689}
5690
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005691static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005692force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005693 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005694{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005695 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005696 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005697 struct desktop_shell *shell = data;
5698 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005699 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005700
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005701 focus_surface = seat->keyboard->focus;
5702 if (!focus_surface)
5703 return;
5704
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005705 wl_signal_emit(&compositor->kill_signal, focus_surface);
5706
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005707 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005708 wl_client_get_credentials(client, &pid, NULL, NULL);
5709
5710 /* Skip clients that we launched ourselves (the credentials of
5711 * the socketpair is ours) */
5712 if (pid == getpid())
5713 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005714
Daniel Stone325fc2d2012-05-30 16:31:58 +01005715 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005716}
5717
5718static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005719workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005720 uint32_t key, void *data)
5721{
5722 struct desktop_shell *shell = data;
5723 unsigned int new_index = shell->workspaces.current;
5724
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005725 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005726 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005727 if (new_index != 0)
5728 new_index--;
5729
5730 change_workspace(shell, new_index);
5731}
5732
5733static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005734workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005735 uint32_t key, void *data)
5736{
5737 struct desktop_shell *shell = data;
5738 unsigned int new_index = shell->workspaces.current;
5739
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005740 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005741 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005742 if (new_index < shell->workspaces.num - 1)
5743 new_index++;
5744
5745 change_workspace(shell, new_index);
5746}
5747
5748static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005749workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005750 uint32_t key, void *data)
5751{
5752 struct desktop_shell *shell = data;
5753 unsigned int new_index;
5754
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005755 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005756 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005757 new_index = key - KEY_F1;
5758 if (new_index >= shell->workspaces.num)
5759 new_index = shell->workspaces.num - 1;
5760
5761 change_workspace(shell, new_index);
5762}
5763
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005764static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005765workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005766 uint32_t key, void *data)
5767{
5768 struct desktop_shell *shell = data;
5769 unsigned int new_index = shell->workspaces.current;
5770
5771 if (shell->locked)
5772 return;
5773
5774 if (new_index != 0)
5775 new_index--;
5776
5777 take_surface_to_workspace_by_seat(shell, seat, new_index);
5778}
5779
5780static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005781workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005782 uint32_t key, void *data)
5783{
5784 struct desktop_shell *shell = data;
5785 unsigned int new_index = shell->workspaces.current;
5786
5787 if (shell->locked)
5788 return;
5789
5790 if (new_index < shell->workspaces.num - 1)
5791 new_index++;
5792
5793 take_surface_to_workspace_by_seat(shell, seat, new_index);
5794}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005795
5796static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005797shell_reposition_view_on_output_destroy(struct weston_view *view)
5798{
5799 struct weston_output *output, *first_output;
5800 struct weston_compositor *ec = view->surface->compositor;
5801 struct shell_surface *shsurf;
5802 float x, y;
5803 int visible;
5804
5805 x = view->geometry.x;
5806 y = view->geometry.y;
5807
5808 /* At this point the destroyed output is not in the list anymore.
5809 * If the view is still visible somewhere, we leave where it is,
5810 * otherwise, move it to the first output. */
5811 visible = 0;
5812 wl_list_for_each(output, &ec->output_list, link) {
5813 if (pixman_region32_contains_point(&output->region,
5814 x, y, NULL)) {
5815 visible = 1;
5816 break;
5817 }
5818 }
5819
5820 if (!visible) {
5821 first_output = container_of(ec->output_list.next,
5822 struct weston_output, link);
5823
5824 x = first_output->x + first_output->width / 4;
5825 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005826
5827 weston_view_set_position(view, x, y);
5828 } else {
5829 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005830 }
5831
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005832
5833 shsurf = get_shell_surface(view->surface);
5834
5835 if (shsurf) {
5836 shsurf->saved_position_valid = false;
5837 shsurf->next_state.maximized = false;
5838 shsurf->next_state.fullscreen = false;
5839 shsurf->state_changed = true;
5840 }
5841}
5842
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005843void
5844shell_for_each_layer(struct desktop_shell *shell,
5845 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005846{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005847 struct workspace **ws;
5848
5849 func(shell, &shell->fullscreen_layer, data);
5850 func(shell, &shell->panel_layer, data);
5851 func(shell, &shell->background_layer, data);
5852 func(shell, &shell->lock_layer, data);
5853 func(shell, &shell->input_panel_layer, data);
5854
5855 wl_array_for_each(ws, &shell->workspaces.array)
5856 func(shell, &(*ws)->layer, data);
5857}
5858
5859static void
5860shell_output_destroy_move_layer(struct desktop_shell *shell,
5861 struct weston_layer *layer,
5862 void *data)
5863{
5864 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005865 struct weston_view *view;
5866
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005867 wl_list_for_each(view, &layer->view_list, layer_link) {
5868 if (view->output != output)
5869 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005870
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005871 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005872 }
5873}
5874
5875static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005876handle_output_destroy(struct wl_listener *listener, void *data)
5877{
5878 struct shell_output *output_listener =
5879 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005880 struct weston_output *output = output_listener->output;
5881 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005882
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005883 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005884
Xiong Zhang6b481422013-10-23 13:58:32 +08005885 wl_list_remove(&output_listener->destroy_listener.link);
5886 wl_list_remove(&output_listener->link);
5887 free(output_listener);
5888}
5889
5890static void
5891create_shell_output(struct desktop_shell *shell,
5892 struct weston_output *output)
5893{
5894 struct shell_output *shell_output;
5895
5896 shell_output = zalloc(sizeof *shell_output);
5897 if (shell_output == NULL)
5898 return;
5899
5900 shell_output->output = output;
5901 shell_output->shell = shell;
5902 shell_output->destroy_listener.notify = handle_output_destroy;
5903 wl_signal_add(&output->destroy_signal,
5904 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005905 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005906}
5907
5908static void
5909handle_output_create(struct wl_listener *listener, void *data)
5910{
5911 struct desktop_shell *shell =
5912 container_of(listener, struct desktop_shell, output_create_listener);
5913 struct weston_output *output = (struct weston_output *)data;
5914
5915 create_shell_output(shell, output);
5916}
5917
5918static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005919handle_output_move_layer(struct desktop_shell *shell,
5920 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005921{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005922 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005923 struct weston_view *view;
5924 float x, y;
5925
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005926 wl_list_for_each(view, &layer->view_list, layer_link) {
5927 if (view->output != output)
5928 continue;
5929
5930 x = view->geometry.x + output->move_x;
5931 y = view->geometry.y + output->move_y;
5932 weston_view_set_position(view, x, y);
5933 }
5934}
5935
5936static void
5937handle_output_move(struct wl_listener *listener, void *data)
5938{
5939 struct desktop_shell *shell;
5940
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005941 shell = container_of(listener, struct desktop_shell,
5942 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005943
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005944 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005945}
5946
5947static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005948setup_output_destroy_handler(struct weston_compositor *ec,
5949 struct desktop_shell *shell)
5950{
5951 struct weston_output *output;
5952
5953 wl_list_init(&shell->output_list);
5954 wl_list_for_each(output, &ec->output_list, link)
5955 create_shell_output(shell, output);
5956
5957 shell->output_create_listener.notify = handle_output_create;
5958 wl_signal_add(&ec->output_created_signal,
5959 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005960
5961 shell->output_move_listener.notify = handle_output_move;
5962 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005963}
5964
5965static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005966shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005967{
Tiago Vignattibe143262012-04-16 17:31:41 +03005968 struct desktop_shell *shell =
5969 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005970 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005971 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005972
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005973 /* Force state to unlocked so we don't try to fade */
5974 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005975 if (shell->child.client)
5976 wl_client_destroy(shell->child.client);
5977
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005978 wl_list_remove(&shell->idle_listener.link);
5979 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005980
5981 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005982
Xiong Zhang6b481422013-10-23 13:58:32 +08005983 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5984 wl_list_remove(&shell_output->destroy_listener.link);
5985 wl_list_remove(&shell_output->link);
5986 free(shell_output);
5987 }
5988
5989 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07005990 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005991
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005992 wl_array_for_each(ws, &shell->workspaces.array)
5993 workspace_destroy(*ws);
5994 wl_array_release(&shell->workspaces.array);
5995
Pekka Paalanen3c647232011-12-22 13:43:43 +02005996 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005997 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005998 free(shell);
5999}
6000
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006001static void
6002shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6003{
6004 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006005 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006006
6007 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006008 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6009 MODIFIER_CTRL | MODIFIER_ALT,
6010 terminate_binding, ec);
6011 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6012 click_to_activate_binding,
6013 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006014 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6015 click_to_activate_binding,
6016 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006017 weston_compositor_add_touch_binding(ec, 0,
6018 touch_to_activate_binding,
6019 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006020 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6021 MODIFIER_SUPER | MODIFIER_ALT,
6022 surface_opacity_binding, NULL);
6023 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6024 MODIFIER_SUPER, zoom_axis_binding,
6025 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006026
6027 /* configurable bindings */
6028 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006029 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6030 zoom_key_binding, NULL);
6031 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6032 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006033 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6034 maximize_binding, NULL);
6035 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6036 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006037 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6038 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006039 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006040 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6041 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006042 weston_compositor_add_button_binding(ec, BTN_LEFT,
6043 mod | MODIFIER_SHIFT,
6044 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006045
6046 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6047 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6048 rotate_binding, NULL);
6049
Daniel Stone325fc2d2012-05-30 16:31:58 +01006050 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6051 shell);
6052 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6053 ec);
6054 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6055 backlight_binding, ec);
6056 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6057 ec);
6058 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6059 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006060 weston_compositor_add_key_binding(ec, KEY_K, mod,
6061 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006062 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6063 workspace_up_binding, shell);
6064 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6065 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006066 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6067 workspace_move_surface_up_binding,
6068 shell);
6069 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6070 workspace_move_surface_down_binding,
6071 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006072
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006073 if (shell->exposay_modifier)
6074 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6075 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006076
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006077 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6078 if (shell->workspaces.num > 1) {
6079 num_workspace_bindings = shell->workspaces.num;
6080 if (num_workspace_bindings > 6)
6081 num_workspace_bindings = 6;
6082 for (i = 0; i < num_workspace_bindings; i++)
6083 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6084 workspace_f_binding,
6085 shell);
6086 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006087
6088 /* Debug bindings */
6089 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6090 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006091}
6092
Jason Ekstrand024177c2014-04-21 19:42:58 -05006093static void
6094handle_seat_created(struct wl_listener *listener, void *data)
6095{
6096 struct weston_seat *seat = data;
6097
6098 create_shell_seat(seat);
6099}
6100
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006101WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006102module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006103 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006104{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006105 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006106 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006107 struct workspace **pws;
6108 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006109 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006110
Peter Huttererf3d62272013-08-08 11:57:05 +10006111 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006112 if (shell == NULL)
6113 return -1;
6114
Kristian Høgsberg75840622011-09-06 13:48:16 -04006115 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006116
6117 shell->destroy_listener.notify = shell_destroy;
6118 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006119 shell->idle_listener.notify = idle_handler;
6120 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6121 shell->wake_listener.notify = wake_handler;
6122 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006123
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006124 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006125 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006126 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006127 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006128 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006129 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006130 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006131 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006132 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006133 ec->shell_interface.set_title = set_title;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07006134 ec->shell_interface.set_margin = set_margin;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006135
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006136 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6137 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006138 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6139 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006140 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006141
6142 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006143 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006144
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006145 if (input_panel_setup(shell) < 0)
6146 return -1;
6147
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006148 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006149
Daniel Stonedf8133b2013-11-19 11:37:14 +01006150 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6151 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6152
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006153 for (i = 0; i < shell->workspaces.num; i++) {
6154 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6155 if (pws == NULL)
6156 return -1;
6157
6158 *pws = workspace_create();
6159 if (*pws == NULL)
6160 return -1;
6161 }
6162 activate_workspace(shell, 0);
6163
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006164 weston_layer_init(&shell->minimized_layer, NULL);
6165
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006166 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006167 wl_list_init(&shell->workspaces.animation.link);
6168 shell->workspaces.animation.frame = animate_workspace_change_frame;
6169
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006170 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006171 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006172 return -1;
6173
Rafael Antognollie2a34552013-12-03 15:35:45 -02006174 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6175 shell, bind_xdg_shell) == NULL)
6176 return -1;
6177
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006178 if (wl_global_create(ec->wl_display,
6179 &desktop_shell_interface, 2,
6180 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006181 return -1;
6182
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006183 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6184 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006185 return -1;
6186
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006187 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6188 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006189 return -1;
6190
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006191 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006192
Xiong Zhang6b481422013-10-23 13:58:32 +08006193 setup_output_destroy_handler(ec, shell);
6194
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006195 loop = wl_display_get_event_loop(ec->wl_display);
6196 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006197
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006198 shell->screensaver.timer =
6199 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6200
Jason Ekstrand024177c2014-04-21 19:42:58 -05006201 wl_list_for_each(seat, &ec->seat_list, link)
6202 handle_seat_created(NULL, seat);
6203 shell->seat_create_listener.notify = handle_seat_created;
6204 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006205
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006206 screenshooter_create(ec);
6207
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006208 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006209
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006210 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006211
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006212 return 0;
6213}