blob: bbc61cf8ccbde14207be109becaba5cb4b9a2261 [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
1654 shsurf->client->send_configure(shsurf->surface,
1655 resize->edges, width, height);
1656}
1657
1658static void
1659send_configure(struct weston_surface *surface,
1660 uint32_t edges, int32_t width, int32_t height)
1661{
1662 struct shell_surface *shsurf = get_shell_surface(surface);
1663
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001664 assert(shsurf);
1665
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001666 if (shsurf->resource)
1667 wl_shell_surface_send_configure(shsurf->resource,
1668 edges, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001669}
1670
1671static const struct weston_shell_client shell_client = {
1672 send_configure
1673};
1674
1675static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001676resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677 uint32_t time, uint32_t button, uint32_t state_w)
1678{
1679 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001680 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001681 enum wl_pointer_button_state state = state_w;
1682
1683 if (pointer->button_count == 0 &&
1684 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1685 shell_grab_end(&resize->base);
1686 free(grab);
1687 }
1688}
1689
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001690static void
1691resize_grab_cancel(struct weston_pointer_grab *grab)
1692{
1693 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1694
1695 shell_grab_end(&resize->base);
1696 free(grab);
1697}
1698
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001699static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001700 noop_grab_focus,
1701 resize_grab_motion,
1702 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001703 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001704};
1705
Giulio Camuffob8366642013-04-25 13:57:46 +03001706/*
1707 * Returns the bounding box of a surface and all its sub-surfaces,
1708 * in the surface coordinates system. */
1709static void
1710surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1711 int32_t *y, int32_t *w, int32_t *h) {
1712 pixman_region32_t region;
1713 pixman_box32_t *box;
1714 struct weston_subsurface *subsurface;
1715
1716 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001717 surface->width,
1718 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001719
1720 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1721 pixman_region32_union_rect(&region, &region,
1722 subsurface->position.x,
1723 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001724 subsurface->surface->width,
1725 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001726 }
1727
1728 box = pixman_region32_extents(&region);
1729 if (x)
1730 *x = box->x1;
1731 if (y)
1732 *y = box->y1;
1733 if (w)
1734 *w = box->x2 - box->x1;
1735 if (h)
1736 *h = box->y2 - box->y1;
1737
1738 pixman_region32_fini(&region);
1739}
1740
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001741static int
1742surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001743 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001744{
1745 struct weston_resize_grab *resize;
1746
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001747 if (shsurf->grabbed ||
1748 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001749 return 0;
1750
1751 if (edges == 0 || edges > 15 ||
1752 (edges & 3) == 3 || (edges & 12) == 12)
1753 return 0;
1754
1755 resize = malloc(sizeof *resize);
1756 if (!resize)
1757 return -1;
1758
1759 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001760 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1761 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001763 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001764 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001765 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001766
1767 return 0;
1768}
1769
1770static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001771common_surface_resize(struct wl_resource *resource,
1772 struct wl_resource *seat_resource, uint32_t serial,
1773 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001775 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001776 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001777 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001778
Kristian Høgsberge3148752013-05-06 23:19:49 -04001779 if (seat->pointer->button_count == 0 ||
1780 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001781 seat->pointer->focus == NULL)
1782 return;
1783
1784 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1785 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001786 return;
1787
Kristian Høgsberge3148752013-05-06 23:19:49 -04001788 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001789 wl_resource_post_no_memory(resource);
1790}
1791
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001792static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001793shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1794 struct wl_resource *seat_resource, uint32_t serial,
1795 uint32_t edges)
1796{
1797 common_surface_resize(resource, seat_resource, serial, edges);
1798}
1799
1800static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001801busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001802{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001804 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001806 wl_fixed_t sx, sy;
1807
Jason Ekstranda7af7042013-10-12 22:38:11 -05001808 view = weston_compositor_pick_view(pointer->seat->compositor,
1809 pointer->x, pointer->y,
1810 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001811
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001812 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1813 shell_grab_end(grab);
1814 free(grab);
1815 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001816}
1817
1818static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001819busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1820 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001821{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001822 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001823}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001824
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001826busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001827 uint32_t time, uint32_t button, uint32_t state)
1828{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001829 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001830 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001831 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001832
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001833 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001834 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001835 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001836 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001837 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001838 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001839 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001840}
1841
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001842static void
1843busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1844{
1845 struct shell_grab *grab = (struct shell_grab *) base;
1846
1847 shell_grab_end(grab);
1848 free(grab);
1849}
1850
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001851static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001852 busy_cursor_grab_focus,
1853 busy_cursor_grab_motion,
1854 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001855 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001856};
1857
1858static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001859set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001860{
1861 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001862
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001863 if (pointer->grab->interface == &busy_cursor_grab_interface)
1864 return;
1865
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001866 grab = malloc(sizeof *grab);
1867 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001868 return;
1869
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001870 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1871 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001872 /* Mark the shsurf as ungrabbed so that button binding is able
1873 * to move it. */
1874 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001875}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001876
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001877static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001878end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001879{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001880 struct shell_grab *grab;
1881 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001882
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001883 wl_list_for_each(seat, &compositor->seat_list, link) {
1884 if (seat->pointer == NULL)
1885 continue;
1886
1887 grab = (struct shell_grab *) seat->pointer->grab;
1888 if (grab->grab.interface == &busy_cursor_grab_interface &&
1889 wl_resource_get_client(grab->shsurf->resource) == client) {
1890 shell_grab_end(grab);
1891 free(grab);
1892 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001893 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001894}
1895
Scott Moreau9521d5e2012-04-19 13:06:17 -06001896static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001897handle_shell_client_destroy(struct wl_listener *listener, void *data);
1898
1899static int
1900xdg_ping_timeout_handler(void *data)
1901{
1902 struct shell_client *sc = data;
1903 struct weston_seat *seat;
1904 struct shell_surface *shsurf;
1905
1906 /* Client is not responding */
1907 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001908 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1909 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1910 continue;
1911 if (seat->pointer->focus->surface->resource == NULL)
1912 continue;
1913
1914 shsurf = get_shell_surface(seat->pointer->focus->surface);
1915 if (shsurf &&
1916 wl_resource_get_client(shsurf->resource) == sc->client)
1917 set_busy_cursor(shsurf, seat->pointer);
1918 }
1919
1920 return 1;
1921}
1922
1923static void
1924handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1925{
1926 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05001927 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001928 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001929 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001930
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001931 if (sc->unresponsive) {
1932 xdg_ping_timeout_handler(sc);
1933 return;
1934 }
1935
1936 sc->ping_serial = serial;
1937 loop = wl_display_get_event_loop(compositor->wl_display);
1938 if (sc->ping_timer == NULL)
1939 sc->ping_timer =
1940 wl_event_loop_add_timer(loop,
1941 xdg_ping_timeout_handler, sc);
1942 if (sc->ping_timer == NULL)
1943 return;
1944
1945 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1946
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001947 if (shell_surface_is_xdg_surface(shsurf) ||
1948 shell_surface_is_xdg_popup(shsurf))
1949 xdg_shell_send_ping(sc->resource, serial);
1950 else if (shell_surface_is_wl_shell_surface(shsurf))
1951 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001952}
1953
Scott Moreauff1db4a2012-04-17 19:06:18 -06001954static void
1955ping_handler(struct weston_surface *surface, uint32_t serial)
1956{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001957 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001958
1959 if (!shsurf)
1960 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001961 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001962 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001963 if (shsurf->surface == shsurf->shell->grab_surface)
1964 return;
1965
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001966 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001967}
1968
1969static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001970handle_pointer_focus(struct wl_listener *listener, void *data)
1971{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001972 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001973 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001974 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001975 uint32_t serial;
1976
Jason Ekstranda7af7042013-10-12 22:38:11 -05001977 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001978 return;
1979
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001981 serial = wl_display_next_serial(compositor->wl_display);
1982 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001983}
1984
1985static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001986shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1987{
1988 if (--shsurf->focus_count == 0)
1989 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001990 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001991}
1992
1993static void
1994shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1995{
1996 if (shsurf->focus_count++ == 0)
1997 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001998 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001999}
2000
2001static void
2002handle_keyboard_focus(struct wl_listener *listener, void *data)
2003{
2004 struct weston_keyboard *keyboard = data;
2005 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2006
2007 if (seat->focused_surface) {
2008 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2009 if (shsurf)
2010 shell_surface_lose_keyboard_focus(shsurf);
2011 }
2012
2013 seat->focused_surface = keyboard->focus;
2014
2015 if (seat->focused_surface) {
2016 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2017 if (shsurf)
2018 shell_surface_gain_keyboard_focus(shsurf);
2019 }
2020}
2021
2022static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002023shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002024{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002025 if (sc->ping_serial != serial)
2026 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002027
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002028 sc->unresponsive = 0;
2029 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002030
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002031 if (sc->ping_timer) {
2032 wl_event_source_remove(sc->ping_timer);
2033 sc->ping_timer = NULL;
2034 }
2035
2036}
2037
2038static void
2039shell_surface_pong(struct wl_client *client,
2040 struct wl_resource *resource, uint32_t serial)
2041{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002042 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2043 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002044
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002045 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002046}
2047
2048static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002049set_title(struct shell_surface *shsurf, const char *title)
2050{
2051 free(shsurf->title);
2052 shsurf->title = strdup(title);
2053}
2054
2055static void
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002056set_margin(struct shell_surface *shsurf,
2057 int32_t left, int32_t right, int32_t top, int32_t bottom)
2058{
2059 shsurf->margin.left = left;
2060 shsurf->margin.right = right;
2061 shsurf->margin.top = top;
2062 shsurf->margin.bottom = bottom;
2063}
2064
2065static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002066shell_surface_set_title(struct wl_client *client,
2067 struct wl_resource *resource, const char *title)
2068{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002069 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002070
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002071 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002072}
2073
2074static void
2075shell_surface_set_class(struct wl_client *client,
2076 struct wl_resource *resource, const char *class)
2077{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002078 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002079
2080 free(shsurf->class);
2081 shsurf->class = strdup(class);
2082}
2083
Alex Wu4539b082012-03-01 12:57:46 +08002084static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002085restore_output_mode(struct weston_output *output)
2086{
Hardening57388e42013-09-18 23:56:36 +02002087 if (output->current_mode != output->original_mode ||
2088 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002089 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002090 output->original_mode,
2091 output->original_scale,
2092 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002093}
2094
2095static void
2096restore_all_output_modes(struct weston_compositor *compositor)
2097{
2098 struct weston_output *output;
2099
2100 wl_list_for_each(output, &compositor->output_list, link)
2101 restore_output_mode(output);
2102}
2103
Philip Withnallbecb77e2013-11-25 18:01:30 +00002104static int
2105get_output_panel_height(struct desktop_shell *shell,
2106 struct weston_output *output)
2107{
2108 struct weston_view *view;
2109 int panel_height = 0;
2110
2111 if (!output)
2112 return 0;
2113
2114 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2115 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002116 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002117 break;
2118 }
2119 }
2120
2121 return panel_height;
2122}
2123
Philip Withnall07926d92013-11-25 18:01:40 +00002124/* The surface will be inserted into the list immediately after the link
2125 * returned by this function (i.e. will be stacked immediately above the
2126 * returned link). */
2127static struct wl_list *
2128shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2129{
2130 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002131 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002132
2133 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002134 case SHELL_SURFACE_XWAYLAND:
2135 return &shsurf->shell->fullscreen_layer.view_list;
2136
2137 case SHELL_SURFACE_NONE:
2138 return NULL;
2139
Kristian Høgsbergead52422014-01-01 13:51:52 -08002140 case SHELL_SURFACE_POPUP:
2141 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002142 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002143 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002144 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002145 /* Move the surface to its parent layer so
2146 * that surfaces which are transient for
2147 * fullscreen surfaces don't get hidden by the
2148 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002149
2150 /* TODO: Handle a parent with multiple views */
2151 parent = get_default_view(shsurf->parent);
2152 if (parent)
2153 return parent->layer_link.prev;
2154 }
Philip Withnall07926d92013-11-25 18:01:40 +00002155
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002156 /* Move the surface to a normal workspace layer so that surfaces
2157 * which were previously fullscreen or transient are no longer
2158 * rendered on top. */
2159 ws = get_current_workspace(shsurf->shell);
2160 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002161 }
2162
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002163 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002164}
2165
Philip Withnall648a4dd2013-11-25 18:01:44 +00002166static void
2167shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2168{
2169 struct shell_surface *child;
2170
2171 /* Move the child layers to the same workspace as shsurf. They will be
2172 * stacked above shsurf. */
2173 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2174 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002175 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002176 weston_view_geometry_dirty(child->view);
2177 wl_list_remove(&child->view->layer_link);
2178 wl_list_insert(shsurf->view->layer_link.prev,
2179 &child->view->layer_link);
2180 weston_view_geometry_dirty(child->view);
2181 weston_surface_damage(child->surface);
2182
2183 /* Recurse. We don’t expect this to recurse very far (if
2184 * at all) because that would imply we have transient
2185 * (or popup) children of transient surfaces, which
2186 * would be unusual. */
2187 shell_surface_update_child_surface_layers(child);
2188 }
2189 }
2190}
2191
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002192/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002193 * geometry to ensure the changes are redrawn.
2194 *
2195 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2196 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002197static void
2198shell_surface_update_layer(struct shell_surface *shsurf)
2199{
2200 struct wl_list *new_layer_link;
2201
2202 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2203
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002204 if (new_layer_link == NULL)
2205 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002206 if (new_layer_link == &shsurf->view->layer_link)
2207 return;
2208
2209 weston_view_geometry_dirty(shsurf->view);
2210 wl_list_remove(&shsurf->view->layer_link);
2211 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2212 weston_view_geometry_dirty(shsurf->view);
2213 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002214
2215 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002216}
2217
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002218static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002219shell_surface_set_parent(struct shell_surface *shsurf,
2220 struct weston_surface *parent)
2221{
2222 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002223
2224 wl_list_remove(&shsurf->children_link);
2225 wl_list_init(&shsurf->children_link);
2226
2227 /* Insert into the parent surface’s child list. */
2228 if (parent != NULL) {
2229 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2230 if (parent_shsurf != NULL)
2231 wl_list_insert(&parent_shsurf->children_list,
2232 &shsurf->children_link);
2233 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002234}
2235
2236static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002237shell_surface_set_output(struct shell_surface *shsurf,
2238 struct weston_output *output)
2239{
2240 struct weston_surface *es = shsurf->surface;
2241
2242 /* get the default output, if the client set it as NULL
2243 check whether the ouput is available */
2244 if (output)
2245 shsurf->output = output;
2246 else if (es->output)
2247 shsurf->output = es->output;
2248 else
2249 shsurf->output = get_default_output(es->compositor);
2250}
2251
2252static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002253surface_clear_next_states(struct shell_surface *shsurf)
2254{
2255 shsurf->next_state.maximized = false;
2256 shsurf->next_state.fullscreen = false;
2257
2258 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2259 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2260 shsurf->state_changed = true;
2261}
2262
2263static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002264set_toplevel(struct shell_surface *shsurf)
2265{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002266 shell_surface_set_parent(shsurf, NULL);
2267 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002268 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002269
2270 /* The layer_link is updated in set_surface_type(),
2271 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002272}
2273
2274static void
2275shell_surface_set_toplevel(struct wl_client *client,
2276 struct wl_resource *resource)
2277{
2278 struct shell_surface *surface = wl_resource_get_user_data(resource);
2279
2280 set_toplevel(surface);
2281}
2282
2283static void
2284set_transient(struct shell_surface *shsurf,
2285 struct weston_surface *parent, int x, int y, uint32_t flags)
2286{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002287 assert(parent != NULL);
2288
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002289 shell_surface_set_parent(shsurf, parent);
2290
2291 surface_clear_next_states(shsurf);
2292
Philip Withnallbecb77e2013-11-25 18:01:30 +00002293 shsurf->transient.x = x;
2294 shsurf->transient.y = y;
2295 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002296
Rafael Antognollied207b42013-12-03 15:35:43 -02002297 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002298 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002299 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002300
2301 /* The layer_link is updated in set_surface_type(),
2302 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002303}
2304
2305static void
2306shell_surface_set_transient(struct wl_client *client,
2307 struct wl_resource *resource,
2308 struct wl_resource *parent_resource,
2309 int x, int y, uint32_t flags)
2310{
2311 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2312 struct weston_surface *parent =
2313 wl_resource_get_user_data(parent_resource);
2314
2315 set_transient(shsurf, parent, x, y, flags);
2316}
2317
2318static void
2319set_fullscreen(struct shell_surface *shsurf,
2320 uint32_t method,
2321 uint32_t framerate,
2322 struct weston_output *output)
2323{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002324 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002325
2326 shsurf->fullscreen_output = shsurf->output;
2327 shsurf->fullscreen.type = method;
2328 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002329
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002330 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002331
2332 shsurf->client->send_configure(shsurf->surface, 0,
2333 shsurf->output->width,
2334 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002335
2336 /* The layer_link is updated in set_surface_type(),
2337 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002338}
2339
2340static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002341weston_view_set_initial_position(struct weston_view *view,
2342 struct desktop_shell *shell);
2343
2344static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002345unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002346{
Philip Withnallf85fe842013-11-25 18:01:37 +00002347 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002348 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2349 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002350 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002351 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002352
Alex Wu4539b082012-03-01 12:57:46 +08002353 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2354 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002355
Alex Wu4539b082012-03-01 12:57:46 +08002356 wl_list_remove(&shsurf->fullscreen.transform.link);
2357 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002358
Jason Ekstranda7af7042013-10-12 22:38:11 -05002359 if (shsurf->fullscreen.black_view)
2360 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2361 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002362
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002363 if (shsurf->saved_position_valid)
2364 weston_view_set_position(shsurf->view,
2365 shsurf->saved_x, shsurf->saved_y);
2366 else
2367 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2368
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002369 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002370 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002371 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002372 shsurf->saved_rotation_valid = false;
2373 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002374
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002375 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002376}
2377
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002378static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002379shell_surface_set_fullscreen(struct wl_client *client,
2380 struct wl_resource *resource,
2381 uint32_t method,
2382 uint32_t framerate,
2383 struct wl_resource *output_resource)
2384{
2385 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2386 struct weston_output *output;
2387
2388 if (output_resource)
2389 output = wl_resource_get_user_data(output_resource);
2390 else
2391 output = NULL;
2392
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002393 shell_surface_set_parent(shsurf, NULL);
2394
Rafael Antognolli03b16592013-12-03 15:35:42 -02002395 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002396 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002397
2398 shsurf->next_state.fullscreen = true;
2399 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002400}
2401
2402static void
2403set_popup(struct shell_surface *shsurf,
2404 struct weston_surface *parent,
2405 struct weston_seat *seat,
2406 uint32_t serial,
2407 int32_t x,
2408 int32_t y)
2409{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002410 assert(parent != NULL);
2411
Philip Withnallbecb77e2013-11-25 18:01:30 +00002412 shsurf->popup.shseat = get_shell_seat(seat);
2413 shsurf->popup.serial = serial;
2414 shsurf->popup.x = x;
2415 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002416
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002417 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002418}
2419
2420static void
2421shell_surface_set_popup(struct wl_client *client,
2422 struct wl_resource *resource,
2423 struct wl_resource *seat_resource,
2424 uint32_t serial,
2425 struct wl_resource *parent_resource,
2426 int32_t x, int32_t y, uint32_t flags)
2427{
2428 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002429 struct weston_surface *parent =
2430 wl_resource_get_user_data(parent_resource);
2431
2432 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002433
Rafael Antognolli03b16592013-12-03 15:35:42 -02002434 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002435 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002436 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002437 wl_resource_get_user_data(seat_resource),
2438 serial, x, y);
2439}
2440
2441static void
2442set_maximized(struct shell_surface *shsurf,
2443 struct weston_output *output)
2444{
2445 struct desktop_shell *shell;
2446 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002447
Philip Withnall352e7ed2013-11-25 18:01:35 +00002448 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002449
2450 shell = shell_surface_get_shell(shsurf);
2451 panel_height = get_output_panel_height(shell, shsurf->output);
2452 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2453
2454 shsurf->client->send_configure(shsurf->surface, edges,
2455 shsurf->output->width,
2456 shsurf->output->height - panel_height);
2457
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002458 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002459}
2460
2461static void
2462unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002463{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002464 /* undo all maximized things here */
2465 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002466
2467 if (shsurf->saved_position_valid)
2468 weston_view_set_position(shsurf->view,
2469 shsurf->saved_x, shsurf->saved_y);
2470 else
2471 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002472
2473 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002474 wl_list_insert(&shsurf->view->geometry.transformation_list,
2475 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002476 shsurf->saved_rotation_valid = false;
2477 }
2478
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002479 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002480}
2481
Philip Withnallbecb77e2013-11-25 18:01:30 +00002482static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002483set_minimized(struct weston_surface *surface, uint32_t is_true)
2484{
2485 struct shell_surface *shsurf;
2486 struct workspace *current_ws;
2487 struct weston_seat *seat;
2488 struct weston_surface *focus;
2489 struct weston_view *view;
2490
2491 view = get_default_view(surface);
2492 if (!view)
2493 return;
2494
2495 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2496
2497 shsurf = get_shell_surface(surface);
2498 current_ws = get_current_workspace(shsurf->shell);
2499
2500 wl_list_remove(&view->layer_link);
2501 /* hide or show, depending on the state */
2502 if (is_true) {
2503 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2504
2505 drop_focus_state(shsurf->shell, current_ws, view->surface);
2506 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2507 if (!seat->keyboard)
2508 continue;
2509 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2510 if (focus == view->surface)
2511 weston_keyboard_set_focus(seat->keyboard, NULL);
2512 }
2513 }
2514 else {
2515 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2516
2517 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2518 if (!seat->keyboard)
2519 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002520 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002521 }
2522 }
2523
2524 shell_surface_update_child_surface_layers(shsurf);
2525
2526 weston_view_damage_below(view);
2527}
2528
2529static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002530shell_surface_set_maximized(struct wl_client *client,
2531 struct wl_resource *resource,
2532 struct wl_resource *output_resource)
2533{
2534 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2535 struct weston_output *output;
2536
2537 if (output_resource)
2538 output = wl_resource_get_user_data(output_resource);
2539 else
2540 output = NULL;
2541
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002542 shell_surface_set_parent(shsurf, NULL);
2543
Rafael Antognolli03b16592013-12-03 15:35:42 -02002544 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002545 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002546
2547 shsurf->next_state.maximized = true;
2548 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002549}
2550
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002551/* This is only ever called from set_surface_type(), so there’s no need to
2552 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002553static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002554reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002555{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002556 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002557 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002558 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002559 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002560
Pekka Paalanen98262232011-12-01 10:42:22 +02002561 return 0;
2562}
2563
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002564static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002565set_full_output(struct shell_surface *shsurf)
2566{
2567 shsurf->saved_x = shsurf->view->geometry.x;
2568 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002569 shsurf->saved_width = shsurf->surface->width;
2570 shsurf->saved_height = shsurf->surface->height;
2571 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002572 shsurf->saved_position_valid = true;
2573
2574 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2575 wl_list_remove(&shsurf->rotation.transform.link);
2576 wl_list_init(&shsurf->rotation.transform.link);
2577 weston_view_geometry_dirty(shsurf->view);
2578 shsurf->saved_rotation_valid = true;
2579 }
2580}
2581
2582static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002583set_surface_type(struct shell_surface *shsurf)
2584{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002585 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002586 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002587
Philip Withnallbecb77e2013-11-25 18:01:30 +00002588 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002589
Rafael Antognolli03b16592013-12-03 15:35:42 -02002590 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002591 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002592
2593 switch (shsurf->type) {
2594 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002595 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002596 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002597 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002598 weston_view_set_position(shsurf->view,
2599 pev->geometry.x + shsurf->transient.x,
2600 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002601 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002602 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002603
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002604 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002605 weston_view_set_position(shsurf->view, shsurf->transient.x,
2606 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002607 break;
2608
Philip Withnall0f640e22013-11-25 18:01:31 +00002609 case SHELL_SURFACE_POPUP:
2610 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002611 default:
2612 break;
2613 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002614
2615 /* Update the surface’s layer. */
2616 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002617}
2618
Tiago Vignattibe143262012-04-16 17:31:41 +03002619static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002620shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002621{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002622 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002623}
2624
Alex Wu21858432012-04-01 20:13:08 +08002625static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002626black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002627
Jason Ekstranda7af7042013-10-12 22:38:11 -05002628static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002629create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002630 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002631 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002632{
2633 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002634 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002635
2636 surface = weston_surface_create(ec);
2637 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002638 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002639 return NULL;
2640 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002641 view = weston_view_create(surface);
2642 if (surface == NULL) {
2643 weston_log("no memory\n");
2644 weston_surface_destroy(surface);
2645 return NULL;
2646 }
Alex Wu4539b082012-03-01 12:57:46 +08002647
Alex Wu21858432012-04-01 20:13:08 +08002648 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002649 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002650 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002651 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002652 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002653 pixman_region32_fini(&surface->input);
2654 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002655
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002656 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002657 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002658
2659 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002660}
2661
Philip Withnalled8f1a92013-11-25 18:01:43 +00002662static void
2663shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2664{
2665 struct weston_output *output = shsurf->fullscreen_output;
2666
Rafael Antognolli03b16592013-12-03 15:35:42 -02002667 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002668
2669 if (!shsurf->fullscreen.black_view)
2670 shsurf->fullscreen.black_view =
2671 create_black_surface(shsurf->surface->compositor,
2672 shsurf->surface,
2673 output->x, output->y,
2674 output->width,
2675 output->height);
2676
2677 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2678 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2679 wl_list_insert(&shsurf->view->layer_link,
2680 &shsurf->fullscreen.black_view->layer_link);
2681 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2682 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002683
2684 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002685}
2686
Alex Wu4539b082012-03-01 12:57:46 +08002687/* Create black surface and append it to the associated fullscreen surface.
2688 * Handle size dismatch and positioning according to the method. */
2689static void
2690shell_configure_fullscreen(struct shell_surface *shsurf)
2691{
2692 struct weston_output *output = shsurf->fullscreen_output;
2693 struct weston_surface *surface = shsurf->surface;
2694 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002695 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002696 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002697
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002698 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2699 restore_output_mode(output);
2700
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002701 /* Reverse the effect of lower_fullscreen_layer() */
2702 wl_list_remove(&shsurf->view->layer_link);
2703 wl_list_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2704
Philip Withnalled8f1a92013-11-25 18:01:43 +00002705 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002706
Jason Ekstranda7af7042013-10-12 22:38:11 -05002707 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002708 &surf_width, &surf_height);
2709
Alex Wu4539b082012-03-01 12:57:46 +08002710 switch (shsurf->fullscreen.type) {
2711 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002712 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002714 break;
2715 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002716 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002717 if (output->width == surf_width &&
2718 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002719 weston_view_set_position(shsurf->view,
2720 output->x - surf_x,
2721 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002722 break;
2723 }
2724
Alex Wu4539b082012-03-01 12:57:46 +08002725 matrix = &shsurf->fullscreen.transform.matrix;
2726 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002727
Scott Moreau1bad5db2012-08-18 01:04:05 -06002728 output_aspect = (float) output->width /
2729 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002730 /* XXX: Use surf_width and surf_height here? */
2731 surface_aspect = (float) surface->width /
2732 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002733 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002734 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002735 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002736 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002737 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002738 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002739
Alex Wu4539b082012-03-01 12:57:46 +08002740 weston_matrix_scale(matrix, scale, scale, 1);
2741 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002742 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002743 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002744 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2745 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002746 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002747
Alex Wu4539b082012-03-01 12:57:46 +08002748 break;
2749 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002750 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002751 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002752 surf_width * surface->buffer_viewport.buffer.scale,
2753 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002754 shsurf->fullscreen.framerate};
2755
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002756 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002757 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002758 weston_view_set_position(shsurf->view,
2759 output->x - surf_x,
2760 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002761 shsurf->fullscreen.black_view->surface->width = output->width;
2762 shsurf->fullscreen.black_view->surface->height = output->height;
2763 weston_view_set_position(shsurf->fullscreen.black_view,
2764 output->x - surf_x,
2765 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002766 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002767 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002768 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002769 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002770 }
Alex Wubd3354b2012-04-17 17:20:49 +08002771 }
Alex Wu4539b082012-03-01 12:57:46 +08002772 break;
2773 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002774 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002775 break;
2776 default:
2777 break;
2778 }
2779}
2780
Alex Wu4539b082012-03-01 12:57:46 +08002781static void
2782shell_map_fullscreen(struct shell_surface *shsurf)
2783{
Alex Wubd3354b2012-04-17 17:20:49 +08002784 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002785}
2786
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002787static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002788set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2789{
2790 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002791 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002792 shsurf->transient.x = x;
2793 shsurf->transient.y = y;
2794 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002795
2796 shell_surface_set_parent(shsurf, NULL);
2797
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002798 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002799 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002800}
2801
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002802static int
2803shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2804{
2805 return surface_move(shsurf, ws, 1);
2806}
2807
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002808static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002809
2810static void
2811destroy_shell_seat(struct wl_listener *listener, void *data)
2812{
2813 struct shell_seat *shseat =
2814 container_of(listener,
2815 struct shell_seat, seat_destroy_listener);
2816 struct shell_surface *shsurf, *prev = NULL;
2817
2818 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002819 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002820 shseat->popup_grab.client = NULL;
2821
2822 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2823 shsurf->popup.shseat = NULL;
2824 if (prev) {
2825 wl_list_init(&prev->popup.grab_link);
2826 }
2827 prev = shsurf;
2828 }
2829 wl_list_init(&prev->popup.grab_link);
2830 }
2831
2832 wl_list_remove(&shseat->seat_destroy_listener.link);
2833 free(shseat);
2834}
2835
Jason Ekstrand024177c2014-04-21 19:42:58 -05002836static void
2837shell_seat_caps_changed(struct wl_listener *listener, void *data)
2838{
2839 struct shell_seat *seat;
2840
2841 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2842
2843 if (seat->seat->keyboard &&
2844 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2845 wl_signal_add(&seat->seat->keyboard->focus_signal,
2846 &seat->keyboard_focus_listener);
2847 } else if (!seat->seat->keyboard) {
2848 wl_list_init(&seat->keyboard_focus_listener.link);
2849 }
2850
2851 if (seat->seat->pointer &&
2852 wl_list_empty(&seat->pointer_focus_listener.link)) {
2853 wl_signal_add(&seat->seat->pointer->focus_signal,
2854 &seat->pointer_focus_listener);
2855 } else if (!seat->seat->pointer) {
2856 wl_list_init(&seat->pointer_focus_listener.link);
2857 }
2858}
2859
Giulio Camuffo5085a752013-03-25 21:42:45 +01002860static struct shell_seat *
2861create_shell_seat(struct weston_seat *seat)
2862{
2863 struct shell_seat *shseat;
2864
2865 shseat = calloc(1, sizeof *shseat);
2866 if (!shseat) {
2867 weston_log("no memory to allocate shell seat\n");
2868 return NULL;
2869 }
2870
2871 shseat->seat = seat;
2872 wl_list_init(&shseat->popup_grab.surfaces_list);
2873
2874 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2875 wl_signal_add(&seat->destroy_signal,
2876 &shseat->seat_destroy_listener);
2877
Jason Ekstrand024177c2014-04-21 19:42:58 -05002878 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2879 wl_list_init(&shseat->keyboard_focus_listener.link);
2880
2881 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2882 wl_list_init(&shseat->pointer_focus_listener.link);
2883
2884 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2885 wl_signal_add(&seat->updated_caps_signal,
2886 &shseat->caps_changed_listener);
2887 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2888
Giulio Camuffo5085a752013-03-25 21:42:45 +01002889 return shseat;
2890}
2891
2892static struct shell_seat *
2893get_shell_seat(struct weston_seat *seat)
2894{
2895 struct wl_listener *listener;
2896
2897 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002898 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002899
2900 return container_of(listener,
2901 struct shell_seat, seat_destroy_listener);
2902}
2903
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002904static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002905popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002906{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002907 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002908 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002909 struct shell_seat *shseat =
2910 container_of(grab, struct shell_seat, popup_grab.grab);
2911 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002912 wl_fixed_t sx, sy;
2913
Jason Ekstranda7af7042013-10-12 22:38:11 -05002914 view = weston_compositor_pick_view(pointer->seat->compositor,
2915 pointer->x, pointer->y,
2916 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002917
Jason Ekstranda7af7042013-10-12 22:38:11 -05002918 if (view && view->surface->resource &&
2919 wl_resource_get_client(view->surface->resource) == client) {
2920 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002921 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002922 weston_pointer_set_focus(pointer, NULL,
2923 wl_fixed_from_int(0),
2924 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002925 }
2926}
2927
2928static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002929popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2930 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002931{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002932 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002933 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002934 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002935
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002936 weston_pointer_move(pointer, x, y);
2937
Neil Roberts96d790e2013-09-19 17:32:00 +01002938 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002939 weston_view_from_global_fixed(pointer->focus,
2940 pointer->x, pointer->y,
2941 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002942 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002943 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002944}
2945
2946static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002947popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002948 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002949{
2950 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002951 struct shell_seat *shseat =
2952 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002953 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002954 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002955 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002956 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002957
Neil Roberts96d790e2013-09-19 17:32:00 +01002958 resource_list = &grab->pointer->focus_resource_list;
2959 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002960 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002961 wl_resource_for_each(resource, resource_list) {
2962 wl_pointer_send_button(resource, serial,
2963 time, button, state);
2964 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002965 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002966 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002967 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002968 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002969 }
2970
Daniel Stone4dbadb12012-05-30 16:31:51 +01002971 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002972 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002973}
2974
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002975static void
2976popup_grab_cancel(struct weston_pointer_grab *grab)
2977{
2978 popup_grab_end(grab->pointer);
2979}
2980
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002981static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002982 popup_grab_focus,
2983 popup_grab_motion,
2984 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002985 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002986};
2987
2988static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002989shell_surface_send_popup_done(struct shell_surface *shsurf)
2990{
2991 if (shell_surface_is_wl_shell_surface(shsurf))
2992 wl_shell_surface_send_popup_done(shsurf->resource);
2993 else if (shell_surface_is_xdg_popup(shsurf))
2994 xdg_popup_send_popup_done(shsurf->resource,
2995 shsurf->popup.serial);
2996}
2997
2998static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002999popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003000{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003001 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003002 struct shell_seat *shseat =
3003 container_of(grab, struct shell_seat, popup_grab.grab);
3004 struct shell_surface *shsurf;
3005 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003006
3007 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003008 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003009 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003010 shseat->popup_grab.grab.interface = NULL;
3011 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003012 /* Send the popup_done event to all the popups open */
3013 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003014 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003015 shsurf->popup.shseat = NULL;
3016 if (prev) {
3017 wl_list_init(&prev->popup.grab_link);
3018 }
3019 prev = shsurf;
3020 }
3021 wl_list_init(&prev->popup.grab_link);
3022 wl_list_init(&shseat->popup_grab.surfaces_list);
3023 }
3024}
3025
3026static void
3027add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3028{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003029 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003030
3031 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003032 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003033 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003034 /* We must make sure here that this popup was opened after
3035 * a mouse press, and not just by moving around with other
3036 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003037 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003038 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003039
Rob Bradforddfe31052013-06-26 19:49:11 +01003040 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003041 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003042 } else {
3043 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003044 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003045}
3046
3047static void
3048remove_popup_grab(struct shell_surface *shsurf)
3049{
3050 struct shell_seat *shseat = shsurf->popup.shseat;
3051
3052 wl_list_remove(&shsurf->popup.grab_link);
3053 wl_list_init(&shsurf->popup.grab_link);
3054 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003055 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003056 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003057 }
3058}
3059
3060static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003061shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003062{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003063 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003064 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003065
Jason Ekstranda7af7042013-10-12 22:38:11 -05003066 shsurf->surface->output = parent_view->output;
3067 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003068
Jason Ekstranda7af7042013-10-12 22:38:11 -05003069 weston_view_set_transform_parent(shsurf->view, parent_view);
3070 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3071 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003072
Kristian Høgsberge3148752013-05-06 23:19:49 -04003073 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003074 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003075 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003076 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003077 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003078 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003079}
3080
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003081static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003082 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003083 shell_surface_move,
3084 shell_surface_resize,
3085 shell_surface_set_toplevel,
3086 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003087 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003088 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003089 shell_surface_set_maximized,
3090 shell_surface_set_title,
3091 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003092};
3093
3094static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003095destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003096{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003097 struct shell_surface *child, *next;
3098
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003099 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3100
Giulio Camuffo5085a752013-03-25 21:42:45 +01003101 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3102 remove_popup_grab(shsurf);
3103 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003104
Alex Wubd3354b2012-04-17 17:20:49 +08003105 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003106 shell_surface_is_top_fullscreen(shsurf))
3107 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003108
Jason Ekstranda7af7042013-10-12 22:38:11 -05003109 if (shsurf->fullscreen.black_view)
3110 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003111
Alex Wubd3354b2012-04-17 17:20:49 +08003112 /* As destroy_resource() use wl_list_for_each_safe(),
3113 * we can always remove the listener.
3114 */
3115 wl_list_remove(&shsurf->surface_destroy_listener.link);
3116 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003117 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003118
Jason Ekstranda7af7042013-10-12 22:38:11 -05003119 weston_view_destroy(shsurf->view);
3120
Philip Withnall648a4dd2013-11-25 18:01:44 +00003121 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003122 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3123 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003124
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003125 wl_list_remove(&shsurf->link);
3126 free(shsurf);
3127}
3128
3129static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003130shell_destroy_shell_surface(struct wl_resource *resource)
3131{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003132 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003133
Bryan Caina46b9462014-04-04 17:41:24 -05003134 if (!wl_list_empty(&shsurf->popup.grab_link))
3135 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003136 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003137}
3138
3139static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003140shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003141{
3142 struct shell_surface *shsurf = container_of(listener,
3143 struct shell_surface,
3144 surface_destroy_listener);
3145
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003146 if (shsurf->resource)
3147 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003148
3149 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003150}
3151
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003152static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003153fade_out_done(struct weston_view_animation *animation, void *data)
3154{
3155 struct shell_surface *shsurf = data;
3156
3157 weston_surface_destroy(shsurf->surface);
3158}
3159
3160static void
3161handle_resource_destroy(struct wl_listener *listener, void *data)
3162{
3163 struct shell_surface *shsurf =
3164 container_of(listener, struct shell_surface,
3165 resource_destroy_listener);
3166
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003167 if (!weston_surface_is_mapped(shsurf->surface))
3168 return;
3169
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003170 shsurf->surface->ref_count++;
3171
3172 pixman_region32_fini(&shsurf->surface->pending.input);
3173 pixman_region32_init(&shsurf->surface->pending.input);
3174 pixman_region32_fini(&shsurf->surface->input);
3175 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003176 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3177 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003178}
3179
3180static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003181shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003182
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003183struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003184get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003185{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003186 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003187 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003188 else
3189 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003190}
3191
Rafael Antognollie2a34552013-12-03 15:35:45 -02003192static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003193create_common_surface(struct shell_client *owner, void *shell,
3194 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003195 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003196{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003197 struct shell_surface *shsurf;
3198
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003199 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003200 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003201 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003202 }
3203
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003204 shsurf = calloc(1, sizeof *shsurf);
3205 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003206 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003207 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003208 }
3209
Jason Ekstranda7af7042013-10-12 22:38:11 -05003210 shsurf->view = weston_view_create(surface);
3211 if (!shsurf->view) {
3212 weston_log("no memory to allocate shell surface\n");
3213 free(shsurf);
3214 return NULL;
3215 }
3216
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003217 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003218 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003219
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003220 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3221 wl_resource_add_destroy_listener(surface->resource,
3222 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003223 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003224
Tiago Vignattibc052c92012-04-19 16:18:18 +03003225 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003226 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003227 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003228 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003229 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003230 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003231 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3232 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003233 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003234 wl_list_init(&shsurf->fullscreen.transform.link);
3235
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003236 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003237 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003238 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003239 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003240
3241 /* init link so its safe to always remove it in destroy_shell_surface */
3242 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003243 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003244
Pekka Paalanen460099f2012-01-20 16:48:25 +02003245 /* empty when not in use */
3246 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003247 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003248
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003249 wl_list_init(&shsurf->workspace_transform.link);
3250
Philip Withnall648a4dd2013-11-25 18:01:44 +00003251 wl_list_init(&shsurf->children_link);
3252 wl_list_init(&shsurf->children_list);
3253 shsurf->parent = NULL;
3254
Pekka Paalanen98262232011-12-01 10:42:22 +02003255 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003256
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003257 shsurf->client = client;
3258
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003259 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003260}
3261
Rafael Antognollie2a34552013-12-03 15:35:45 -02003262static struct shell_surface *
3263create_shell_surface(void *shell, struct weston_surface *surface,
3264 const struct weston_shell_client *client)
3265{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003266 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003267}
3268
Jason Ekstranda7af7042013-10-12 22:38:11 -05003269static struct weston_view *
3270get_primary_view(void *shell, struct shell_surface *shsurf)
3271{
3272 return shsurf->view;
3273}
3274
Tiago Vignattibc052c92012-04-19 16:18:18 +03003275static void
3276shell_get_shell_surface(struct wl_client *client,
3277 struct wl_resource *resource,
3278 uint32_t id,
3279 struct wl_resource *surface_resource)
3280{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003281 struct weston_surface *surface =
3282 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003283 struct shell_client *sc = wl_resource_get_user_data(resource);
3284 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003285 struct shell_surface *shsurf;
3286
3287 if (get_shell_surface(surface)) {
3288 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003289 WL_DISPLAY_ERROR_INVALID_OBJECT,
3290 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003291 return;
3292 }
3293
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003294 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003295 if (!shsurf) {
3296 wl_resource_post_error(surface_resource,
3297 WL_DISPLAY_ERROR_INVALID_OBJECT,
3298 "surface->configure already set");
3299 return;
3300 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003301
Jason Ekstranda85118c2013-06-27 20:17:02 -05003302 shsurf->resource =
3303 wl_resource_create(client,
3304 &wl_shell_surface_interface, 1, id);
3305 wl_resource_set_implementation(shsurf->resource,
3306 &shell_surface_implementation,
3307 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003308}
3309
Rafael Antognollie2a34552013-12-03 15:35:45 -02003310static bool
3311shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3312{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003313 /* A shell surface without a resource is created from xwayland
3314 * and is considered a wl_shell surface for now. */
3315
3316 return shsurf->resource == NULL ||
3317 wl_resource_instance_of(shsurf->resource,
3318 &wl_shell_surface_interface,
3319 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003320}
3321
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003322static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003323 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003324};
3325
Rafael Antognollie2a34552013-12-03 15:35:45 -02003326/****************************
3327 * xdg-shell implementation */
3328
3329static void
3330xdg_surface_destroy(struct wl_client *client,
3331 struct wl_resource *resource)
3332{
3333 wl_resource_destroy(resource);
3334}
3335
3336static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003337xdg_surface_set_transient_for(struct wl_client *client,
3338 struct wl_resource *resource,
3339 struct wl_resource *parent_resource)
3340{
3341 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3342 struct weston_surface *parent;
3343
3344 if (parent_resource)
3345 parent = wl_resource_get_user_data(parent_resource);
3346 else
3347 parent = NULL;
3348
3349 shell_surface_set_parent(shsurf, parent);
3350}
3351
3352static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003353xdg_surface_set_margin(struct wl_client *client,
3354 struct wl_resource *resource,
3355 int32_t left,
3356 int32_t right,
3357 int32_t top,
3358 int32_t bottom)
3359{
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003360 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3361
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07003362 set_margin(shsurf, left, right, top, bottom);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003363}
3364
3365static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003366xdg_surface_set_app_id(struct wl_client *client,
3367 struct wl_resource *resource,
3368 const char *app_id)
3369{
3370 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3371
3372 free(shsurf->class);
3373 shsurf->class = strdup(app_id);
3374}
3375
3376static void
3377xdg_surface_set_title(struct wl_client *client,
3378 struct wl_resource *resource, const char *title)
3379{
3380 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3381
3382 set_title(shsurf, title);
3383}
3384
3385static void
3386xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3387 struct wl_resource *seat_resource, uint32_t serial)
3388{
3389 common_surface_move(resource, seat_resource, serial);
3390}
3391
3392static void
3393xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3394 struct wl_resource *seat_resource, uint32_t serial,
3395 uint32_t edges)
3396{
3397 common_surface_resize(resource, seat_resource, serial, edges);
3398}
3399
3400static void
3401xdg_surface_set_output(struct wl_client *client,
3402 struct wl_resource *resource,
3403 struct wl_resource *output_resource)
3404{
3405 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3406 struct weston_output *output;
3407
3408 if (output_resource)
3409 output = wl_resource_get_user_data(output_resource);
3410 else
3411 output = NULL;
3412
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003413 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003414}
3415
3416static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003417xdg_surface_change_state(struct shell_surface *shsurf,
3418 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003419{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003420 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003421 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003422
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003423 switch (state) {
3424 case XDG_SURFACE_STATE_MAXIMIZED:
3425 shsurf->requested_state.maximized = value;
3426 if (value)
3427 set_maximized(shsurf, NULL);
3428 break;
3429 case XDG_SURFACE_STATE_FULLSCREEN:
3430 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003431
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003432 if (value)
3433 set_fullscreen(shsurf,
3434 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3435 0, shsurf->recommended_output);
3436 break;
3437 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003438}
3439
3440static void
3441xdg_surface_request_change_state(struct wl_client *client,
3442 struct wl_resource *resource,
3443 uint32_t state,
3444 uint32_t value,
3445 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003446{
3447 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003448
3449 /* The client can't know what the current state is, so we need
3450 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003451
3452 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3453 return;
3454
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003455 switch (state) {
3456 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003457 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003458 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003459 default:
3460 /* send error? ignore? send change state with value 0? */
3461 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003462 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003463
3464 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003465}
3466
3467static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003468xdg_surface_ack_change_state(struct wl_client *client,
3469 struct wl_resource *resource,
3470 uint32_t state,
3471 uint32_t value,
3472 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003473{
3474 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3475
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003476 if (shsurf->state_requested) {
3477 shsurf->next_state = shsurf->requested_state;
3478 shsurf->state_changed = true;
3479 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003480 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003481}
3482
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003483static void
3484xdg_surface_set_minimized(struct wl_client *client,
3485 struct wl_resource *resource)
3486{
3487 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3488
3489 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3490 return;
3491
3492 /* apply compositor's own minimization logic (hide) */
3493 set_minimized(shsurf->surface, 1);
3494}
3495
Rafael Antognollie2a34552013-12-03 15:35:45 -02003496static const struct xdg_surface_interface xdg_surface_implementation = {
3497 xdg_surface_destroy,
3498 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003499 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003500 xdg_surface_set_title,
3501 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003502 xdg_surface_move,
3503 xdg_surface_resize,
3504 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003505 xdg_surface_request_change_state,
3506 xdg_surface_ack_change_state,
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003507 xdg_surface_set_minimized
Rafael Antognollie2a34552013-12-03 15:35:45 -02003508};
3509
3510static void
3511xdg_send_configure(struct weston_surface *surface,
3512 uint32_t edges, int32_t width, int32_t height)
3513{
3514 struct shell_surface *shsurf = get_shell_surface(surface);
3515
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003516 assert(shsurf);
3517
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07003518 if (shsurf->resource)
3519 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003520}
3521
3522static const struct weston_shell_client xdg_client = {
3523 xdg_send_configure
3524};
3525
3526static void
3527xdg_use_unstable_version(struct wl_client *client,
3528 struct wl_resource *resource,
3529 int32_t version)
3530{
3531 if (version > 1) {
3532 wl_resource_post_error(resource,
3533 1,
3534 "xdg-shell:: version not implemented yet.");
3535 return;
3536 }
3537}
3538
3539static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003540create_xdg_surface(struct shell_client *owner, void *shell,
3541 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003542 const struct weston_shell_client *client)
3543{
3544 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003545
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003546 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003547 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003548
3549 return shsurf;
3550}
3551
3552static void
3553xdg_get_xdg_surface(struct wl_client *client,
3554 struct wl_resource *resource,
3555 uint32_t id,
3556 struct wl_resource *surface_resource)
3557{
3558 struct weston_surface *surface =
3559 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003560 struct shell_client *sc = wl_resource_get_user_data(resource);
3561 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003562 struct shell_surface *shsurf;
3563
3564 if (get_shell_surface(surface)) {
3565 wl_resource_post_error(surface_resource,
3566 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003567 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003568 return;
3569 }
3570
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003571 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003572 if (!shsurf) {
3573 wl_resource_post_error(surface_resource,
3574 WL_DISPLAY_ERROR_INVALID_OBJECT,
3575 "surface->configure already set");
3576 return;
3577 }
3578
3579 shsurf->resource =
3580 wl_resource_create(client,
3581 &xdg_surface_interface, 1, id);
3582 wl_resource_set_implementation(shsurf->resource,
3583 &xdg_surface_implementation,
3584 shsurf, shell_destroy_shell_surface);
3585}
3586
3587static bool
3588shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3589{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003590 return shsurf->resource &&
3591 wl_resource_instance_of(shsurf->resource,
3592 &xdg_surface_interface,
3593 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003594}
3595
3596/* xdg-popup implementation */
3597
3598static void
3599xdg_popup_destroy(struct wl_client *client,
3600 struct wl_resource *resource)
3601{
3602 wl_resource_destroy(resource);
3603}
3604
Rafael Antognollie2a34552013-12-03 15:35:45 -02003605static const struct xdg_popup_interface xdg_popup_implementation = {
3606 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003607};
3608
3609static void
3610xdg_popup_send_configure(struct weston_surface *surface,
3611 uint32_t edges, int32_t width, int32_t height)
3612{
3613}
3614
3615static const struct weston_shell_client xdg_popup_client = {
3616 xdg_popup_send_configure
3617};
3618
3619static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003620create_xdg_popup(struct shell_client *owner, void *shell,
3621 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003622 const struct weston_shell_client *client,
3623 struct weston_surface *parent,
3624 struct shell_seat *seat,
3625 uint32_t serial,
3626 int32_t x, int32_t y)
3627{
3628 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003629
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003630 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003631 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003632 shsurf->popup.shseat = seat;
3633 shsurf->popup.serial = serial;
3634 shsurf->popup.x = x;
3635 shsurf->popup.y = y;
3636 shell_surface_set_parent(shsurf, parent);
3637
3638 return shsurf;
3639}
3640
3641static void
3642xdg_get_xdg_popup(struct wl_client *client,
3643 struct wl_resource *resource,
3644 uint32_t id,
3645 struct wl_resource *surface_resource,
3646 struct wl_resource *parent_resource,
3647 struct wl_resource *seat_resource,
3648 uint32_t serial,
3649 int32_t x, int32_t y, uint32_t flags)
3650{
3651 struct weston_surface *surface =
3652 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003653 struct shell_client *sc = wl_resource_get_user_data(resource);
3654 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003655 struct shell_surface *shsurf;
3656 struct weston_surface *parent;
3657 struct shell_seat *seat;
3658
3659 if (get_shell_surface(surface)) {
3660 wl_resource_post_error(surface_resource,
3661 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003662 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003663 return;
3664 }
3665
3666 if (!parent_resource) {
3667 wl_resource_post_error(surface_resource,
3668 WL_DISPLAY_ERROR_INVALID_OBJECT,
3669 "xdg_shell::get_xdg_popup requires a parent shell surface");
3670 }
3671
3672 parent = wl_resource_get_user_data(parent_resource);
3673 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3674
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003675 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003676 parent, seat, serial, x, y);
3677 if (!shsurf) {
3678 wl_resource_post_error(surface_resource,
3679 WL_DISPLAY_ERROR_INVALID_OBJECT,
3680 "surface->configure already set");
3681 return;
3682 }
3683
3684 shsurf->resource =
3685 wl_resource_create(client,
3686 &xdg_popup_interface, 1, id);
3687 wl_resource_set_implementation(shsurf->resource,
3688 &xdg_popup_implementation,
3689 shsurf, shell_destroy_shell_surface);
3690}
3691
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003692static void
3693xdg_pong(struct wl_client *client,
3694 struct wl_resource *resource, uint32_t serial)
3695{
3696 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003697
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003698 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003699}
3700
Rafael Antognollie2a34552013-12-03 15:35:45 -02003701static bool
3702shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3703{
3704 return wl_resource_instance_of(shsurf->resource,
3705 &xdg_popup_interface,
3706 &xdg_popup_implementation);
3707}
3708
3709static const struct xdg_shell_interface xdg_implementation = {
3710 xdg_use_unstable_version,
3711 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003712 xdg_get_xdg_popup,
3713 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003714};
3715
3716static int
3717xdg_shell_unversioned_dispatch(const void *implementation,
3718 void *_target, uint32_t opcode,
3719 const struct wl_message *message,
3720 union wl_argument *args)
3721{
3722 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003723 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003724
3725 if (opcode != 0) {
3726 wl_resource_post_error(resource,
3727 WL_DISPLAY_ERROR_INVALID_OBJECT,
3728 "must call use_unstable_version first");
3729 return 0;
3730 }
3731
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003732#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003733
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003734 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3735 "shell implementation doesn't match protocol version");
3736
Rafael Antognollie2a34552013-12-03 15:35:45 -02003737 if (args[0].i != XDG_SERVER_VERSION) {
3738 wl_resource_post_error(resource,
3739 WL_DISPLAY_ERROR_INVALID_OBJECT,
3740 "incompatible version, server is %d "
3741 "client wants %d",
3742 XDG_SERVER_VERSION, args[0].i);
3743 return 0;
3744 }
3745
3746 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003747 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003748
3749 return 1;
3750}
3751
3752/* end of xdg-shell implementation */
3753/***********************************/
3754
Kristian Høgsberg07937562011-04-12 17:25:42 -04003755static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003756shell_fade(struct desktop_shell *shell, enum fade_type type);
3757
3758static int
3759screensaver_timeout(void *data)
3760{
3761 struct desktop_shell *shell = data;
3762
3763 shell_fade(shell, FADE_OUT);
3764
3765 return 1;
3766}
3767
3768static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003769handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003770{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003771 struct desktop_shell *shell =
3772 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003773
Pekka Paalanen18027e52011-12-02 16:31:49 +02003774 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003775
3776 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003777 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003778}
3779
3780static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003781launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003782{
3783 if (shell->screensaver.binding)
3784 return;
3785
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003786 if (!shell->screensaver.path) {
3787 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003788 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003789 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003790
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003791 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003792 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003793 return;
3794 }
3795
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003796 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003797 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003798 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003799 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003800}
3801
3802static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003803terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003804{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003805 if (shell->screensaver.process.pid == 0)
3806 return;
3807
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003808 /* Disarm the screensaver timer, otherwise it may fire when the
3809 * compositor is not in the idle state. In that case, the screen will
3810 * be locked, but the wake_signal won't fire on user input, making the
3811 * system unresponsive. */
3812 wl_event_source_timer_update(shell->screensaver.timer, 0);
3813
Pekka Paalanen18027e52011-12-02 16:31:49 +02003814 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003815}
3816
3817static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003818configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003819{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003820 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003821
Jason Ekstranda7af7042013-10-12 22:38:11 -05003822 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3823 if (v->output == ev->output && v != ev) {
3824 weston_view_unmap(v);
3825 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003826 }
3827 }
3828
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003829 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003830
Jason Ekstranda7af7042013-10-12 22:38:11 -05003831 if (wl_list_empty(&ev->layer_link)) {
3832 wl_list_insert(&layer->view_list, &ev->layer_link);
3833 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003834 }
3835}
3836
3837static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003838background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003839{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003840 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003841 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003842
Jason Ekstranda7af7042013-10-12 22:38:11 -05003843 view = container_of(es->views.next, struct weston_view, surface_link);
3844
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003845 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003846}
3847
3848static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003849desktop_shell_set_background(struct wl_client *client,
3850 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003851 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003852 struct wl_resource *surface_resource)
3853{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003854 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003855 struct weston_surface *surface =
3856 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003857 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003858
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003859 if (surface->configure) {
3860 wl_resource_post_error(surface_resource,
3861 WL_DISPLAY_ERROR_INVALID_OBJECT,
3862 "surface role already assigned");
3863 return;
3864 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003865
Jason Ekstranda7af7042013-10-12 22:38:11 -05003866 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3867 weston_view_destroy(view);
3868 view = weston_view_create(surface);
3869
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003870 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003871 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003872 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003873 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003874 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003875 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003876 surface->output->width,
3877 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003878}
3879
3880static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003881panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003882{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003883 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003884 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003885
Jason Ekstranda7af7042013-10-12 22:38:11 -05003886 view = container_of(es->views.next, struct weston_view, surface_link);
3887
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003888 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003889}
3890
3891static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003892desktop_shell_set_panel(struct wl_client *client,
3893 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003894 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003895 struct wl_resource *surface_resource)
3896{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003897 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003898 struct weston_surface *surface =
3899 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003900 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003901
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003902 if (surface->configure) {
3903 wl_resource_post_error(surface_resource,
3904 WL_DISPLAY_ERROR_INVALID_OBJECT,
3905 "surface role already assigned");
3906 return;
3907 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003908
Jason Ekstranda7af7042013-10-12 22:38:11 -05003909 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3910 weston_view_destroy(view);
3911 view = weston_view_create(surface);
3912
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003913 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003914 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003915 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003916 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003917 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003918 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003919 surface->output->width,
3920 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003921}
3922
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003923static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003924lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003925{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003926 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003927 struct weston_view *view;
3928
3929 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003930
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003931 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003932 return;
3933
Jason Ekstranda7af7042013-10-12 22:38:11 -05003934 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003935
3936 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003937 wl_list_insert(&shell->lock_layer.view_list,
3938 &view->layer_link);
3939 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003940 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003941 }
3942}
3943
3944static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003945handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003946{
Tiago Vignattibe143262012-04-16 17:31:41 +03003947 struct desktop_shell *shell =
3948 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003949
Martin Minarik6d118362012-06-07 18:01:59 +02003950 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003951 shell->lock_surface = NULL;
3952}
3953
3954static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003955desktop_shell_set_lock_surface(struct wl_client *client,
3956 struct wl_resource *resource,
3957 struct wl_resource *surface_resource)
3958{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003959 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003960 struct weston_surface *surface =
3961 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003962
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003963 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003964
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003965 if (!shell->locked)
3966 return;
3967
Pekka Paalanen98262232011-12-01 10:42:22 +02003968 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003969
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003970 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003971 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003972 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003973
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003974 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003975 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003976 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003977}
3978
3979static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003980resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003981{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003982 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003983
Pekka Paalanen77346a62011-11-30 16:26:35 +02003984 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003985
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003986 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003987 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003988 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003989 &shell->input_panel_layer.link);
3990 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003991 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003992 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003993 wl_list_insert(&shell->compositor->cursor_layer.link,
3994 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003995 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003996 wl_list_insert(&shell->fullscreen_layer.link,
3997 &shell->panel_layer.link);
3998 wl_list_insert(&shell->panel_layer.link,
3999 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004000
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004001 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004002
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004003 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004004 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004005 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004006}
4007
4008static void
4009desktop_shell_unlock(struct wl_client *client,
4010 struct wl_resource *resource)
4011{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004012 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004013
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004014 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004015
4016 if (shell->locked)
4017 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004018}
4019
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004020static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004021desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004022 struct wl_resource *resource,
4023 struct wl_resource *surface_resource)
4024{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004025 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004026
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004027 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004028 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004029}
4030
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004031static void
4032desktop_shell_desktop_ready(struct wl_client *client,
4033 struct wl_resource *resource)
4034{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004035 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004036
4037 shell_fade_startup(shell);
4038}
4039
Kristian Høgsberg75840622011-09-06 13:48:16 -04004040static const struct desktop_shell_interface desktop_shell_implementation = {
4041 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004042 desktop_shell_set_panel,
4043 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004044 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004045 desktop_shell_set_grab_surface,
4046 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004047};
4048
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004049static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004050get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004051{
4052 struct shell_surface *shsurf;
4053
4054 shsurf = get_shell_surface(surface);
4055 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004056 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004057 return shsurf->type;
4058}
4059
Kristian Høgsberg75840622011-09-06 13:48:16 -04004060static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004061move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004062{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004063 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004064 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004065 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004066
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004067 if (seat->pointer->focus == NULL)
4068 return;
4069
4070 focus = seat->pointer->focus->surface;
4071
Pekka Paalanen01388e22013-04-25 13:57:44 +03004072 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004073 if (surface == NULL)
4074 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004075
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004076 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004077 if (shsurf == NULL || shsurf->state.fullscreen ||
4078 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004079 return;
4080
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004081 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004082}
4083
4084static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004085maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4086{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004087 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004088 struct weston_surface *surface;
4089 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004090 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004091
4092 surface = weston_surface_get_main_surface(focus);
4093 if (surface == NULL)
4094 return;
4095
4096 shsurf = get_shell_surface(surface);
4097 if (shsurf == NULL)
4098 return;
4099
4100 if (!shell_surface_is_xdg_surface(shsurf))
4101 return;
4102
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004103 serial = wl_display_next_serial(seat->compositor->wl_display);
4104 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4105 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004106}
4107
4108static void
4109fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4110{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004111 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004112 struct weston_surface *surface;
4113 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004114 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004115
4116 surface = weston_surface_get_main_surface(focus);
4117 if (surface == NULL)
4118 return;
4119
4120 shsurf = get_shell_surface(surface);
4121 if (shsurf == NULL)
4122 return;
4123
4124 if (!shell_surface_is_xdg_surface(shsurf))
4125 return;
4126
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004127 serial = wl_display_next_serial(seat->compositor->wl_display);
4128 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4129 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004130}
4131
4132static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004133touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4134{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004135 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004136 struct weston_surface *surface;
4137 struct shell_surface *shsurf;
4138
4139 surface = weston_surface_get_main_surface(focus);
4140 if (surface == NULL)
4141 return;
4142
4143 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004144 if (shsurf == NULL || shsurf->state.fullscreen ||
4145 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004146 return;
4147
4148 surface_touch_move(shsurf, (struct weston_seat *) seat);
4149}
4150
4151static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004152resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004153{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004154 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004155 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004156 uint32_t edges = 0;
4157 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004158 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004159
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004160 if (seat->pointer->focus == NULL)
4161 return;
4162
4163 focus = seat->pointer->focus->surface;
4164
Pekka Paalanen01388e22013-04-25 13:57:44 +03004165 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004166 if (surface == NULL)
4167 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004168
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004169 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004170 if (shsurf == NULL || shsurf->state.fullscreen ||
4171 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004172 return;
4173
Jason Ekstranda7af7042013-10-12 22:38:11 -05004174 weston_view_from_global(shsurf->view,
4175 wl_fixed_to_int(seat->pointer->grab_x),
4176 wl_fixed_to_int(seat->pointer->grab_y),
4177 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004178
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004179 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004180 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004181 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004182 edges |= 0;
4183 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004184 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004185
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004186 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004187 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004188 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004189 edges |= 0;
4190 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004191 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004192
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004193 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004194}
4195
4196static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004197surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004198 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004199{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004200 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004201 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004202 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004203 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004204
Pekka Paalanen01388e22013-04-25 13:57:44 +03004205 /* XXX: broken for windows containing sub-surfaces */
4206 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004207 if (surface == NULL)
4208 return;
4209
4210 shsurf = get_shell_surface(surface);
4211 if (!shsurf)
4212 return;
4213
Jason Ekstranda7af7042013-10-12 22:38:11 -05004214 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004215
Jason Ekstranda7af7042013-10-12 22:38:11 -05004216 if (shsurf->view->alpha > 1.0)
4217 shsurf->view->alpha = 1.0;
4218 if (shsurf->view->alpha < step)
4219 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004220
Jason Ekstranda7af7042013-10-12 22:38:11 -05004221 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004222 weston_surface_damage(surface);
4223}
4224
4225static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004226do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004227 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004228{
Daniel Stone37816df2012-05-16 18:45:18 +01004229 struct weston_seat *ws = (struct weston_seat *) seat;
4230 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004231 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004232 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004233
4234 wl_list_for_each(output, &compositor->output_list, link) {
4235 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004236 wl_fixed_to_double(seat->pointer->x),
4237 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004238 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004239 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004240 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004241 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004242 increment = -output->zoom.increment;
4243 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004244 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004245 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004246 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004247 else
4248 increment = 0;
4249
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004250 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004251
Scott Moreaue6603982012-06-11 13:07:51 -06004252 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004253 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004254 else if (output->zoom.level > output->zoom.max_level)
4255 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004256 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004257 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004258 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004259
Scott Moreaue6603982012-06-11 13:07:51 -06004260 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004261
Jason Ekstranda7af7042013-10-12 22:38:11 -05004262 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004263 }
4264 }
4265}
4266
Scott Moreauccbf29d2012-02-22 14:21:41 -07004267static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004268zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004269 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004270{
4271 do_zoom(seat, time, 0, axis, value);
4272}
4273
4274static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004275zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004276 void *data)
4277{
4278 do_zoom(seat, time, key, 0, 0);
4279}
4280
4281static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004282terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004283 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004284{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004285 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004286
Daniel Stone325fc2d2012-05-30 16:31:58 +01004287 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004288}
4289
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004290static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004291rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4292 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004293{
4294 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004295 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004296 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004297 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004298 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004299
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004300 weston_pointer_move(pointer, x, y);
4301
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004302 if (!shsurf)
4303 return;
4304
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004305 cx = 0.5f * shsurf->surface->width;
4306 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004307
Daniel Stone37816df2012-05-16 18:45:18 +01004308 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4309 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004310 r = sqrtf(dx * dx + dy * dy);
4311
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004312 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004313 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004314
4315 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004316 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004317 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004318
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004319 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004320 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004321
4322 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004323 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004324 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004325 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004326 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004327
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004328 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004329 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004330 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004331 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004332 wl_list_init(&shsurf->rotation.transform.link);
4333 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004334 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004335 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004336
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004337 /* We need to adjust the position of the surface
4338 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004339 cposx = shsurf->view->geometry.x + cx;
4340 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004341 dposx = rotate->center.x - cposx;
4342 dposy = rotate->center.y - cposy;
4343 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004344 weston_view_set_position(shsurf->view,
4345 shsurf->view->geometry.x + dposx,
4346 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004347 }
4348
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004349 /* Repaint implies weston_surface_update_transform(), which
4350 * lazily applies the damage due to rotation update.
4351 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004352 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004353}
4354
4355static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004356rotate_grab_button(struct weston_pointer_grab *grab,
4357 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004358{
4359 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004360 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004361 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004362 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004363 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004364
Daniel Stone4dbadb12012-05-30 16:31:51 +01004365 if (pointer->button_count == 0 &&
4366 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004367 if (shsurf)
4368 weston_matrix_multiply(&shsurf->rotation.rotation,
4369 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004370 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004371 free(rotate);
4372 }
4373}
4374
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004375static void
4376rotate_grab_cancel(struct weston_pointer_grab *grab)
4377{
4378 struct rotate_grab *rotate =
4379 container_of(grab, struct rotate_grab, base.grab);
4380
4381 shell_grab_end(&rotate->base);
4382 free(rotate);
4383}
4384
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004385static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004386 noop_grab_focus,
4387 rotate_grab_motion,
4388 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004389 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004390};
4391
4392static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004393surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004394{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004395 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004396 float dx, dy;
4397 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004398
Pekka Paalanen460099f2012-01-20 16:48:25 +02004399 rotate = malloc(sizeof *rotate);
4400 if (!rotate)
4401 return;
4402
Jason Ekstranda7af7042013-10-12 22:38:11 -05004403 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004404 surface->surface->width * 0.5f,
4405 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004407
Daniel Stone37816df2012-05-16 18:45:18 +01004408 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4409 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004410 r = sqrtf(dx * dx + dy * dy);
4411 if (r > 20.0f) {
4412 struct weston_matrix inverse;
4413
4414 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004415 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004416 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004417
4418 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004419 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004420 } else {
4421 weston_matrix_init(&surface->rotation.rotation);
4422 weston_matrix_init(&rotate->rotation);
4423 }
4424
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004425 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4426 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004427}
4428
4429static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004430rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004431 void *data)
4432{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004433 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004434 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004435 struct shell_surface *surface;
4436
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004437 if (seat->pointer->focus == NULL)
4438 return;
4439
4440 focus = seat->pointer->focus->surface;
4441
Pekka Paalanen01388e22013-04-25 13:57:44 +03004442 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004443 if (base_surface == NULL)
4444 return;
4445
4446 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004447 if (surface == NULL || surface->state.fullscreen ||
4448 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004449 return;
4450
4451 surface_rotate(surface, seat);
4452}
4453
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004454/* Move all fullscreen layers down to the current workspace and hide their
4455 * black views. The surfaces' state is set to both fullscreen and lowered,
4456 * and this is reversed when such a surface is re-configured, see
4457 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4458 *
4459 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004460 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004461void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004462lower_fullscreen_layer(struct desktop_shell *shell)
4463{
4464 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004465 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004466
4467 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004468 wl_list_for_each_reverse_safe(view, prev,
4469 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004470 layer_link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004471 struct shell_surface *shsurf = get_shell_surface(view->surface);
4472
4473 if (!shsurf)
4474 continue;
4475
4476 /* We can have a non-fullscreen popup for a fullscreen surface
4477 * in the fullscreen layer. */
4478 if (shsurf->state.fullscreen) {
4479 /* Hide the black view */
4480 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
4481 wl_list_init(&shsurf->fullscreen.black_view->layer_link);
4482 }
4483
4484 /* Lower the view to the workspace layer */
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004485 wl_list_remove(&view->layer_link);
4486 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4487 weston_view_damage_below(view);
4488 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004489
4490 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004491 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004492}
4493
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004494void
Tiago Vignattibe143262012-04-16 17:31:41 +03004495activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004496 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004497{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004498 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004499 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004500 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004501 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004502 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004503
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004504 lower_fullscreen_layer(shell);
4505
Pekka Paalanen01388e22013-04-25 13:57:44 +03004506 main_surface = weston_surface_get_main_surface(es);
4507
Daniel Stone37816df2012-05-16 18:45:18 +01004508 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004509
Jonas Ådahl8538b222012-08-29 22:13:03 +02004510 state = ensure_focus_state(shell, seat);
4511 if (state == NULL)
4512 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004513
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004514 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004515 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004516
Rafael Antognolli03b16592013-12-03 15:35:42 -02004517 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004518 assert(shsurf);
4519
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004520 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004521 shell_configure_fullscreen(shsurf);
4522 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004523 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004524
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004525 /* Update the surface’s layer. This brings it to the top of the stacking
4526 * order as appropriate. */
4527 shell_surface_update_layer(shsurf);
4528
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004529 if (shell->focus_animation_type != ANIMATION_NONE) {
4530 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004531 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004532 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004533}
4534
Alex Wu21858432012-04-01 20:13:08 +08004535/* no-op func for checking black surface */
4536static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004537black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004538{
4539}
4540
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004541static bool
Alex Wu21858432012-04-01 20:13:08 +08004542is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4543{
4544 if (es->configure == black_surface_configure) {
4545 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004546 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004547 return true;
4548 }
4549 return false;
4550}
4551
Kristian Høgsberg75840622011-09-06 13:48:16 -04004552static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004553activate_binding(struct weston_seat *seat,
4554 struct desktop_shell *shell,
4555 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004556{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004557 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004558
Alex Wu9c35e6b2012-03-05 14:13:13 +08004559 if (!focus)
4560 return;
4561
Pekka Paalanen01388e22013-04-25 13:57:44 +03004562 if (is_black_surface(focus, &main_surface))
4563 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004564
Pekka Paalanen01388e22013-04-25 13:57:44 +03004565 main_surface = weston_surface_get_main_surface(focus);
4566 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004567 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004568
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004569 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004570}
4571
4572static void
4573click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4574 void *data)
4575{
4576 if (seat->pointer->grab != &seat->pointer->default_grab)
4577 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004578 if (seat->pointer->focus == NULL)
4579 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004580
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004581 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004582}
4583
4584static void
4585touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4586{
4587 if (seat->touch->grab != &seat->touch->default_grab)
4588 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004589 if (seat->touch->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->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004593}
4594
4595static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004596unfocus_all_seats(struct desktop_shell *shell)
4597{
4598 struct weston_seat *seat, *next;
4599
4600 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4601 if (seat->keyboard == NULL)
4602 continue;
4603
4604 weston_keyboard_set_focus(seat->keyboard, NULL);
4605 }
4606}
4607
4608static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004609lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004610{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004611 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004612
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004613 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004614 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004615 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004616 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004617
4618 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004619
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004620 /* Hide all surfaces by removing the fullscreen, panel and
4621 * toplevel layers. This way nothing else can show or receive
4622 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004623
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004624 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004625 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004626 if (shell->showing_input_panels)
4627 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004628 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004629 wl_list_insert(&shell->compositor->cursor_layer.link,
4630 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004631
Pekka Paalanen77346a62011-11-30 16:26:35 +02004632 launch_screensaver(shell);
4633
Neil Robertsb4a91702014-04-09 16:33:32 +01004634 /* Remove the keyboard focus on all seats. This will be
4635 * restored to the workspace's saved state via
4636 * restore_focus_state when the compositor is unlocked */
4637 unfocus_all_seats(shell);
4638
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004639 /* TODO: disable bindings that should not work while locked. */
4640
4641 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004642}
4643
4644static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004645unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004646{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004647 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004648 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004649 return;
4650 }
4651
4652 /* If desktop-shell client has gone away, unlock immediately. */
4653 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004654 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004655 return;
4656 }
4657
4658 if (shell->prepare_event_sent)
4659 return;
4660
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004661 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004662 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004663}
4664
4665static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004666shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004667{
4668 struct desktop_shell *shell = data;
4669
4670 shell->fade.animation = NULL;
4671
4672 switch (shell->fade.type) {
4673 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004674 weston_surface_destroy(shell->fade.view->surface);
4675 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004676 break;
4677 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004678 lock(shell);
4679 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004680 default:
4681 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004682 }
4683}
4684
Jason Ekstranda7af7042013-10-12 22:38:11 -05004685static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004686shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004687{
4688 struct weston_compositor *compositor = shell->compositor;
4689 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004690 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004691
4692 surface = weston_surface_create(compositor);
4693 if (!surface)
4694 return NULL;
4695
Jason Ekstranda7af7042013-10-12 22:38:11 -05004696 view = weston_view_create(surface);
4697 if (!view) {
4698 weston_surface_destroy(surface);
4699 return NULL;
4700 }
4701
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004702 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004703 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004704 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004705 wl_list_insert(&compositor->fade_layer.view_list,
4706 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004707 pixman_region32_init(&surface->input);
4708
Jason Ekstranda7af7042013-10-12 22:38:11 -05004709 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004710}
4711
4712static void
4713shell_fade(struct desktop_shell *shell, enum fade_type type)
4714{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004715 float tint;
4716
4717 switch (type) {
4718 case FADE_IN:
4719 tint = 0.0;
4720 break;
4721 case FADE_OUT:
4722 tint = 1.0;
4723 break;
4724 default:
4725 weston_log("shell: invalid fade type\n");
4726 return;
4727 }
4728
4729 shell->fade.type = type;
4730
Jason Ekstranda7af7042013-10-12 22:38:11 -05004731 if (shell->fade.view == NULL) {
4732 shell->fade.view = shell_fade_create_surface(shell);
4733 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004734 return;
4735
Jason Ekstranda7af7042013-10-12 22:38:11 -05004736 shell->fade.view->alpha = 1.0 - tint;
4737 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004738 }
4739
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004740 if (shell->fade.view->output == NULL) {
4741 /* If the black view gets a NULL output, we lost the
4742 * last output and we'll just cancel the fade. This
4743 * happens when you close the last window under the
4744 * X11 or Wayland backends. */
4745 shell->locked = false;
4746 weston_surface_destroy(shell->fade.view->surface);
4747 shell->fade.view = NULL;
4748 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004749 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004750 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004751 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004752 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004753 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004754 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004755 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004756}
4757
4758static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004759do_shell_fade_startup(void *data)
4760{
4761 struct desktop_shell *shell = data;
4762
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004763 if (shell->startup_animation_type == ANIMATION_FADE)
4764 shell_fade(shell, FADE_IN);
4765 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004766 weston_surface_destroy(shell->fade.view->surface);
4767 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004768 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004769}
4770
4771static void
4772shell_fade_startup(struct desktop_shell *shell)
4773{
4774 struct wl_event_loop *loop;
4775
4776 if (!shell->fade.startup_timer)
4777 return;
4778
4779 wl_event_source_remove(shell->fade.startup_timer);
4780 shell->fade.startup_timer = NULL;
4781
4782 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4783 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4784}
4785
4786static int
4787fade_startup_timeout(void *data)
4788{
4789 struct desktop_shell *shell = data;
4790
4791 shell_fade_startup(shell);
4792 return 0;
4793}
4794
4795static void
4796shell_fade_init(struct desktop_shell *shell)
4797{
4798 /* Make compositor output all black, and wait for the desktop-shell
4799 * client to signal it is ready, then fade in. The timer triggers a
4800 * fade-in, in case the desktop-shell client takes too long.
4801 */
4802
4803 struct wl_event_loop *loop;
4804
Jason Ekstranda7af7042013-10-12 22:38:11 -05004805 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004806 weston_log("%s: warning: fade surface already exists\n",
4807 __func__);
4808 return;
4809 }
4810
Jason Ekstranda7af7042013-10-12 22:38:11 -05004811 shell->fade.view = shell_fade_create_surface(shell);
4812 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004813 return;
4814
Jason Ekstranda7af7042013-10-12 22:38:11 -05004815 weston_view_update_transform(shell->fade.view);
4816 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004817
4818 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4819 shell->fade.startup_timer =
4820 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4821 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4822}
4823
4824static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004825idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004826{
4827 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004828 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004829 struct weston_seat *seat;
4830
4831 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4832 if (seat->pointer)
4833 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004834
4835 shell_fade(shell, FADE_OUT);
4836 /* lock() is called from shell_fade_done() */
4837}
4838
4839static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004840wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004841{
4842 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004843 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004844
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004845 unlock(shell);
4846}
4847
4848static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004849center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004850{
Giulio Camuffob8366642013-04-25 13:57:46 +03004851 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004852 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004853
Jason Ekstranda7af7042013-10-12 22:38:11 -05004854 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004855
4856 x = output->x + (output->width - width) / 2 - surf_x / 2;
4857 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004858
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004859 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004860}
4861
4862static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004863weston_view_set_initial_position(struct weston_view *view,
4864 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004865{
4866 struct weston_compositor *compositor = shell->compositor;
4867 int ix = 0, iy = 0;
4868 int range_x, range_y;
4869 int dx, dy, x, y, panel_height;
4870 struct weston_output *output, *target_output = NULL;
4871 struct weston_seat *seat;
4872
4873 /* As a heuristic place the new window on the same output as the
4874 * pointer. Falling back to the output containing 0, 0.
4875 *
4876 * TODO: Do something clever for touch too?
4877 */
4878 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004879 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004880 ix = wl_fixed_to_int(seat->pointer->x);
4881 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004882 break;
4883 }
4884 }
4885
4886 wl_list_for_each(output, &compositor->output_list, link) {
4887 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4888 target_output = output;
4889 break;
4890 }
4891 }
4892
4893 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004894 weston_view_set_position(view, 10 + random() % 400,
4895 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004896 return;
4897 }
4898
4899 /* Valid range within output where the surface will still be onscreen.
4900 * If this is negative it means that the surface is bigger than
4901 * output.
4902 */
4903 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004904 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004905 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004906 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004907
Rob Bradford4cb88c72012-08-13 15:18:44 +01004908 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004909 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004910 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004911 dx = 0;
4912
4913 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004914 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004915 else
4916 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004917
4918 x = target_output->x + dx;
4919 y = target_output->y + dy;
4920
Jason Ekstranda7af7042013-10-12 22:38:11 -05004921 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004922}
4923
4924static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004925map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004926 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004927{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004928 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004929 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004930 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004931 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004932
Pekka Paalanen77346a62011-11-30 16:26:35 +02004933 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004934 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004935 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004936 if (shsurf->state.fullscreen) {
4937 center_on_output(shsurf->view, shsurf->fullscreen_output);
4938 shell_map_fullscreen(shsurf);
4939 } else if (shsurf->state.maximized) {
4940 /* use surface configure to set the geometry */
4941 panel_height = get_output_panel_height(shell, shsurf->output);
4942 surface_subsurfaces_boundingbox(shsurf->surface,
4943 &surf_x, &surf_y, NULL, NULL);
4944 weston_view_set_position(shsurf->view,
4945 shsurf->output->x - surf_x,
4946 shsurf->output->y +
4947 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004948 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004949 weston_view_set_initial_position(shsurf->view, shell);
4950 }
Juan Zhao96879df2012-02-07 08:45:41 +08004951 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004952 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004953 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004954 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004955 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004956 weston_view_set_position(shsurf->view,
4957 shsurf->view->geometry.x + sx,
4958 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004959 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004960 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004961 default:
4962 ;
4963 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004964
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004965 /* Surface stacking order, see also activate(). */
4966 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004967
Jason Ekstranda7af7042013-10-12 22:38:11 -05004968 if (shsurf->type != SHELL_SURFACE_NONE) {
4969 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004970 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004971 shsurf->surface->output = shsurf->output;
4972 shsurf->view->output = shsurf->output;
4973 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004974 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004975
Jason Ekstranda7af7042013-10-12 22:38:11 -05004976 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004977 /* XXX: xwayland's using the same fields for transient type */
4978 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004979 if (shsurf->transient.flags ==
4980 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4981 break;
4982 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004983 if (shsurf->state.relative &&
4984 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4985 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004986 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004987 break;
4988 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004989 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004990 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004991 case SHELL_SURFACE_POPUP:
4992 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004993 default:
4994 break;
4995 }
4996
Rafael Antognolli03b16592013-12-03 15:35:42 -02004997 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4998 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004999 {
5000 switch (shell->win_animation_type) {
5001 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005002 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005003 break;
5004 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005005 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005006 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005007 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005008 default:
5009 break;
5010 }
5011 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005012}
5013
5014static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005015configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005016 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005017{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005018 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005019 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005020 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005021
Pekka Paalanen77346a62011-11-30 16:26:35 +02005022 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005023
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005024 assert(shsurf);
5025
Rafael Antognolli03b16592013-12-03 15:35:42 -02005026 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005027 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005028 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005029 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005030 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5031 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005032 mx = shsurf->output->x - surf_x;
5033 my = shsurf->output->y +
5034 get_output_panel_height(shell,shsurf->output) - surf_y;
5035 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005036 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005037 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005038 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005039
Alex Wu4539b082012-03-01 12:57:46 +08005040 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005041 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005042 wl_list_for_each(view, &surface->views, surface_link)
5043 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005044
Rafael Antognolli03b16592013-12-03 15:35:42 -02005045 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005046 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005047 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005048}
5049
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005050static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005051shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005052{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005053 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005054 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005055 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005056
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005057 assert(shsurf);
5058
5059 shell = shsurf->shell;
5060
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005061 if (!weston_surface_is_mapped(es) &&
5062 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005063 remove_popup_grab(shsurf);
5064 }
5065
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005066 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005067 return;
5068
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005069 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005070 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005071 type_changed = 1;
5072 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005073
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005074 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005075 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005076 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005077 shsurf->last_width != es->width ||
5078 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005079 float from_x, from_y;
5080 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005081
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005082 if (shsurf->resize_edges) {
5083 sx = 0;
5084 sy = 0;
5085 }
5086
5087 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5088 sx = shsurf->last_width - es->width;
5089 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5090 sy = shsurf->last_height - es->height;
5091
5092 shsurf->last_width = es->width;
5093 shsurf->last_height = es->height;
5094
Jason Ekstranda7af7042013-10-12 22:38:11 -05005095 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5096 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005097 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005098 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005099 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005100 }
5101}
5102
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005103static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005104
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005105static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005106desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005107{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005108 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005109 struct desktop_shell *shell =
5110 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005111
5112 shell->child.process.pid = 0;
5113 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005114
5115 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5116 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005117 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005118 shell->child.deathstamp = time;
5119 shell->child.deathcount = 0;
5120 }
5121
5122 shell->child.deathcount++;
5123 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005124 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005125 return;
5126 }
5127
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005128 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005129 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005130 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005131}
5132
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005133static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005134desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5135{
5136 struct desktop_shell *shell;
5137
5138 shell = container_of(listener, struct desktop_shell,
5139 child.client_destroy_listener);
5140
5141 shell->child.client = NULL;
5142}
5143
5144static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005145launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005146{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005147 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005148
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005149 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005150 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005151 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005152 desktop_shell_sigchld);
5153
5154 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005155 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005156
5157 shell->child.client_destroy_listener.notify =
5158 desktop_shell_client_destroy;
5159 wl_client_add_destroy_listener(shell->child.client,
5160 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005161}
5162
5163static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005164handle_shell_client_destroy(struct wl_listener *listener, void *data)
5165{
5166 struct shell_client *sc =
5167 container_of(listener, struct shell_client, destroy_listener);
5168
5169 if (sc->ping_timer)
5170 wl_event_source_remove(sc->ping_timer);
5171 free(sc);
5172}
5173
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005174static struct shell_client *
5175shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5176 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005177{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005178 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005179
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005180 sc = zalloc(sizeof *sc);
5181 if (sc == NULL) {
5182 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005183 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005184 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005185
5186 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005187 if (sc->resource == NULL) {
5188 free(sc);
5189 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005190 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005191 }
5192
5193 sc->client = client;
5194 sc->shell = shell;
5195 sc->destroy_listener.notify = handle_shell_client_destroy;
5196 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5197
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005198 return sc;
5199}
5200
5201static void
5202bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5203{
5204 struct desktop_shell *shell = data;
5205 struct shell_client *sc;
5206
5207 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5208 if (sc)
5209 wl_resource_set_implementation(sc->resource,
5210 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005211 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005212}
5213
5214static void
5215bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5216{
5217 struct desktop_shell *shell = data;
5218 struct shell_client *sc;
5219
5220 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5221 if (sc)
5222 wl_resource_set_dispatcher(sc->resource,
5223 xdg_shell_unversioned_dispatch,
5224 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005225}
5226
5227static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005228unbind_desktop_shell(struct wl_resource *resource)
5229{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005230 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005231
5232 if (shell->locked)
5233 resume_desktop(shell);
5234
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005235 shell->child.desktop_shell = NULL;
5236 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005237}
5238
5239static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005240bind_desktop_shell(struct wl_client *client,
5241 void *data, uint32_t version, uint32_t id)
5242{
Tiago Vignattibe143262012-04-16 17:31:41 +03005243 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005244 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005245
Jason Ekstranda85118c2013-06-27 20:17:02 -05005246 resource = wl_resource_create(client, &desktop_shell_interface,
5247 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005248
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005249 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005250 wl_resource_set_implementation(resource,
5251 &desktop_shell_implementation,
5252 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005253 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005254
5255 if (version < 2)
5256 shell_fade_startup(shell);
5257
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005258 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005259 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005260
5261 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5262 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005263 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005264}
5265
Pekka Paalanen6e168112011-11-24 11:34:05 +02005266static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005267screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005268{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005269 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005270 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005271
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005272 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005273 return;
5274
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005275 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005276 if (!shell->locked)
5277 return;
5278
Jason Ekstranda7af7042013-10-12 22:38:11 -05005279 view = container_of(surface->views.next, struct weston_view, surface_link);
5280 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005281
Jason Ekstranda7af7042013-10-12 22:38:11 -05005282 if (wl_list_empty(&view->layer_link)) {
5283 wl_list_insert(shell->lock_layer.view_list.prev,
5284 &view->layer_link);
5285 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005286 wl_event_source_timer_update(shell->screensaver.timer,
5287 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005288 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005289 }
5290}
5291
5292static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005293screensaver_set_surface(struct wl_client *client,
5294 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005295 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005296 struct wl_resource *output_resource)
5297{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005298 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005299 struct weston_surface *surface =
5300 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005301 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005302 struct weston_view *view, *next;
5303
5304 /* Make sure we only have one view */
5305 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5306 weston_view_destroy(view);
5307 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005308
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005309 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005310 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005311 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005312}
5313
5314static const struct screensaver_interface screensaver_implementation = {
5315 screensaver_set_surface
5316};
5317
5318static void
5319unbind_screensaver(struct wl_resource *resource)
5320{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005321 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005322
Pekka Paalanen77346a62011-11-30 16:26:35 +02005323 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005324}
5325
5326static void
5327bind_screensaver(struct wl_client *client,
5328 void *data, uint32_t version, uint32_t id)
5329{
Tiago Vignattibe143262012-04-16 17:31:41 +03005330 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005331 struct wl_resource *resource;
5332
Jason Ekstranda85118c2013-06-27 20:17:02 -05005333 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005334
Pekka Paalanen77346a62011-11-30 16:26:35 +02005335 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005336 wl_resource_set_implementation(resource,
5337 &screensaver_implementation,
5338 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005339 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005340 return;
5341 }
5342
5343 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5344 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005345 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005346}
5347
Kristian Høgsberg07045392012-02-19 18:52:44 -05005348struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005349 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005350 struct weston_surface *current;
5351 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005352 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005353 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005354};
5355
5356static void
5357switcher_next(struct switcher *switcher)
5358{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005359 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005360 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005361 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005362 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005363
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005364 /* temporary re-display minimized surfaces */
5365 struct weston_view *tmp;
5366 struct weston_view **minimized;
5367 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5368 wl_list_remove(&view->layer_link);
5369 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5370 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5371 *minimized = view;
5372 }
5373
Jason Ekstranda7af7042013-10-12 22:38:11 -05005374 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005375 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005376 if (shsurf &&
5377 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5378 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005379 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005380 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005381 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005382 next = view->surface;
5383 prev = view->surface;
5384 view->alpha = 0.25;
5385 weston_view_geometry_dirty(view);
5386 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005387 }
Alex Wu1659daa2012-04-01 20:13:09 +08005388
Jason Ekstranda7af7042013-10-12 22:38:11 -05005389 if (is_black_surface(view->surface, NULL)) {
5390 view->alpha = 0.25;
5391 weston_view_geometry_dirty(view);
5392 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005393 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005394 }
5395
5396 if (next == NULL)
5397 next = first;
5398
Alex Wu07b26062012-03-12 16:06:01 +08005399 if (next == NULL)
5400 return;
5401
Kristian Høgsberg07045392012-02-19 18:52:44 -05005402 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005403 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005404
5405 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005406 wl_list_for_each(view, &next->views, surface_link)
5407 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005408
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005409 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005410 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005411 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005412}
5413
5414static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005415switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005416{
5417 struct switcher *switcher =
5418 container_of(listener, struct switcher, listener);
5419
5420 switcher_next(switcher);
5421}
5422
5423static void
Daniel Stone351eb612012-05-31 15:27:47 -04005424switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005425{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005426 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005427 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005428 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005429
Jason Ekstranda7af7042013-10-12 22:38:11 -05005430 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005431 if (is_focus_view(view))
5432 continue;
5433
Jason Ekstranda7af7042013-10-12 22:38:11 -05005434 view->alpha = 1.0;
5435 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005436 }
5437
Alex Wu07b26062012-03-12 16:06:01 +08005438 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005439 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005440 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005441 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005442 weston_keyboard_end_grab(keyboard);
5443 if (keyboard->input_method_resource)
5444 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005445
5446 /* re-hide surfaces that were temporary shown during the switch */
5447 struct weston_view **minimized;
5448 wl_array_for_each(minimized, &switcher->minimized_array) {
5449 /* with the exception of the current selected */
5450 if ((*minimized)->surface != switcher->current) {
5451 wl_list_remove(&(*minimized)->layer_link);
5452 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5453 weston_view_damage_below(*minimized);
5454 }
5455 }
5456 wl_array_release(&switcher->minimized_array);
5457
Kristian Høgsberg07045392012-02-19 18:52:44 -05005458 free(switcher);
5459}
5460
5461static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005462switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005463 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005464{
5465 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005466 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005467
Daniel Stonec9785ea2012-05-30 16:31:52 +01005468 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005469 switcher_next(switcher);
5470}
5471
5472static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005473switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005474 uint32_t mods_depressed, uint32_t mods_latched,
5475 uint32_t mods_locked, uint32_t group)
5476{
5477 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005478 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005479
Daniel Stone351eb612012-05-31 15:27:47 -04005480 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5481 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005482}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005483
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005484static void
5485switcher_cancel(struct weston_keyboard_grab *grab)
5486{
5487 struct switcher *switcher = container_of(grab, struct switcher, grab);
5488
5489 switcher_destroy(switcher);
5490}
5491
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005492static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005493 switcher_key,
5494 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005495 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005496};
5497
5498static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005499switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005500 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005501{
Tiago Vignattibe143262012-04-16 17:31:41 +03005502 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005503 struct switcher *switcher;
5504
5505 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005506 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005507 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005508 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005509 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005510 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005511
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005512 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005513 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005514 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005515 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5516 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005517 switcher_next(switcher);
5518}
5519
Pekka Paalanen3c647232011-12-22 13:43:43 +02005520static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005521backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005522 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005523{
5524 struct weston_compositor *compositor = data;
5525 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005526 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005527
5528 /* TODO: we're limiting to simple use cases, where we assume just
5529 * control on the primary display. We'd have to extend later if we
5530 * ever get support for setting backlights on random desktop LCD
5531 * panels though */
5532 output = get_default_output(compositor);
5533 if (!output)
5534 return;
5535
5536 if (!output->set_backlight)
5537 return;
5538
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005539 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5540 backlight_new = output->backlight_current - 25;
5541 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5542 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005543
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005544 if (backlight_new < 5)
5545 backlight_new = 5;
5546 if (backlight_new > 255)
5547 backlight_new = 255;
5548
5549 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005550 output->set_backlight(output, output->backlight_current);
5551}
5552
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005553struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005554 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005555 struct weston_seat *seat;
5556 uint32_t key[2];
5557 int key_released[2];
5558};
5559
5560static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005561debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005562 uint32_t key, uint32_t state)
5563{
5564 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005565 struct weston_compositor *ec = db->seat->compositor;
5566 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005567 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005568 uint32_t serial;
5569 int send = 0, terminate = 0;
5570 int check_binding = 1;
5571 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005572 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005573
5574 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5575 /* Do not run bindings on key releases */
5576 check_binding = 0;
5577
5578 for (i = 0; i < 2; i++)
5579 if (key == db->key[i])
5580 db->key_released[i] = 1;
5581
5582 if (db->key_released[0] && db->key_released[1]) {
5583 /* All key releases been swalled so end the grab */
5584 terminate = 1;
5585 } else if (key != db->key[0] && key != db->key[1]) {
5586 /* Should not swallow release of other keys */
5587 send = 1;
5588 }
5589 } else if (key == db->key[0] && !db->key_released[0]) {
5590 /* Do not check bindings for the first press of the binding
5591 * key. This allows it to be used as a debug shortcut.
5592 * We still need to swallow this event. */
5593 check_binding = 0;
5594 } else if (db->key[1]) {
5595 /* If we already ran a binding don't process another one since
5596 * we can't keep track of all the binding keys that were
5597 * pressed in order to swallow the release events. */
5598 send = 1;
5599 check_binding = 0;
5600 }
5601
5602 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005603 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5604 key, state)) {
5605 /* We ran a binding so swallow the press and keep the
5606 * grab to swallow the released too. */
5607 send = 0;
5608 terminate = 0;
5609 db->key[1] = key;
5610 } else {
5611 /* Terminate the grab since the key pressed is not a
5612 * debug binding key. */
5613 send = 1;
5614 terminate = 1;
5615 }
5616 }
5617
5618 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005619 serial = wl_display_next_serial(display);
5620 resource_list = &grab->keyboard->focus_resource_list;
5621 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005622 wl_keyboard_send_key(resource, serial, time, key, state);
5623 }
5624 }
5625
5626 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005627 weston_keyboard_end_grab(grab->keyboard);
5628 if (grab->keyboard->input_method_resource)
5629 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005630 free(db);
5631 }
5632}
5633
5634static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005635debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005636 uint32_t mods_depressed, uint32_t mods_latched,
5637 uint32_t mods_locked, uint32_t group)
5638{
5639 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005640 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005641
Neil Roberts96d790e2013-09-19 17:32:00 +01005642 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005643
Neil Roberts96d790e2013-09-19 17:32:00 +01005644 wl_resource_for_each(resource, resource_list) {
5645 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5646 mods_latched, mods_locked, group);
5647 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005648}
5649
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005650static void
5651debug_binding_cancel(struct weston_keyboard_grab *grab)
5652{
5653 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5654
5655 weston_keyboard_end_grab(grab->keyboard);
5656 free(db);
5657}
5658
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005659struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005660 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005661 debug_binding_modifiers,
5662 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005663};
5664
5665static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005666debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005667{
5668 struct debug_binding_grab *grab;
5669
5670 grab = calloc(1, sizeof *grab);
5671 if (!grab)
5672 return;
5673
5674 grab->seat = (struct weston_seat *) seat;
5675 grab->key[0] = key;
5676 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005677 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005678}
5679
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005680static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005681force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005682 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005683{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005684 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005685 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005686 struct desktop_shell *shell = data;
5687 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005688 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005689
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005690 focus_surface = seat->keyboard->focus;
5691 if (!focus_surface)
5692 return;
5693
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005694 wl_signal_emit(&compositor->kill_signal, focus_surface);
5695
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005696 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005697 wl_client_get_credentials(client, &pid, NULL, NULL);
5698
5699 /* Skip clients that we launched ourselves (the credentials of
5700 * the socketpair is ours) */
5701 if (pid == getpid())
5702 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005703
Daniel Stone325fc2d2012-05-30 16:31:58 +01005704 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005705}
5706
5707static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005708workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005709 uint32_t key, void *data)
5710{
5711 struct desktop_shell *shell = data;
5712 unsigned int new_index = shell->workspaces.current;
5713
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005714 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005715 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005716 if (new_index != 0)
5717 new_index--;
5718
5719 change_workspace(shell, new_index);
5720}
5721
5722static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005723workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005724 uint32_t key, void *data)
5725{
5726 struct desktop_shell *shell = data;
5727 unsigned int new_index = shell->workspaces.current;
5728
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005729 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005730 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005731 if (new_index < shell->workspaces.num - 1)
5732 new_index++;
5733
5734 change_workspace(shell, new_index);
5735}
5736
5737static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005738workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005739 uint32_t key, void *data)
5740{
5741 struct desktop_shell *shell = data;
5742 unsigned int new_index;
5743
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005744 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005745 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005746 new_index = key - KEY_F1;
5747 if (new_index >= shell->workspaces.num)
5748 new_index = shell->workspaces.num - 1;
5749
5750 change_workspace(shell, new_index);
5751}
5752
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005753static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005754workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005755 uint32_t key, void *data)
5756{
5757 struct desktop_shell *shell = data;
5758 unsigned int new_index = shell->workspaces.current;
5759
5760 if (shell->locked)
5761 return;
5762
5763 if (new_index != 0)
5764 new_index--;
5765
5766 take_surface_to_workspace_by_seat(shell, seat, new_index);
5767}
5768
5769static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005770workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005771 uint32_t key, void *data)
5772{
5773 struct desktop_shell *shell = data;
5774 unsigned int new_index = shell->workspaces.current;
5775
5776 if (shell->locked)
5777 return;
5778
5779 if (new_index < shell->workspaces.num - 1)
5780 new_index++;
5781
5782 take_surface_to_workspace_by_seat(shell, seat, new_index);
5783}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005784
5785static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005786shell_reposition_view_on_output_destroy(struct weston_view *view)
5787{
5788 struct weston_output *output, *first_output;
5789 struct weston_compositor *ec = view->surface->compositor;
5790 struct shell_surface *shsurf;
5791 float x, y;
5792 int visible;
5793
5794 x = view->geometry.x;
5795 y = view->geometry.y;
5796
5797 /* At this point the destroyed output is not in the list anymore.
5798 * If the view is still visible somewhere, we leave where it is,
5799 * otherwise, move it to the first output. */
5800 visible = 0;
5801 wl_list_for_each(output, &ec->output_list, link) {
5802 if (pixman_region32_contains_point(&output->region,
5803 x, y, NULL)) {
5804 visible = 1;
5805 break;
5806 }
5807 }
5808
5809 if (!visible) {
5810 first_output = container_of(ec->output_list.next,
5811 struct weston_output, link);
5812
5813 x = first_output->x + first_output->width / 4;
5814 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005815
5816 weston_view_set_position(view, x, y);
5817 } else {
5818 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005819 }
5820
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005821
5822 shsurf = get_shell_surface(view->surface);
5823
5824 if (shsurf) {
5825 shsurf->saved_position_valid = false;
5826 shsurf->next_state.maximized = false;
5827 shsurf->next_state.fullscreen = false;
5828 shsurf->state_changed = true;
5829 }
5830}
5831
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005832void
5833shell_for_each_layer(struct desktop_shell *shell,
5834 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005835{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005836 struct workspace **ws;
5837
5838 func(shell, &shell->fullscreen_layer, data);
5839 func(shell, &shell->panel_layer, data);
5840 func(shell, &shell->background_layer, data);
5841 func(shell, &shell->lock_layer, data);
5842 func(shell, &shell->input_panel_layer, data);
5843
5844 wl_array_for_each(ws, &shell->workspaces.array)
5845 func(shell, &(*ws)->layer, data);
5846}
5847
5848static void
5849shell_output_destroy_move_layer(struct desktop_shell *shell,
5850 struct weston_layer *layer,
5851 void *data)
5852{
5853 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005854 struct weston_view *view;
5855
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005856 wl_list_for_each(view, &layer->view_list, layer_link) {
5857 if (view->output != output)
5858 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005859
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005860 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005861 }
5862}
5863
5864static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005865handle_output_destroy(struct wl_listener *listener, void *data)
5866{
5867 struct shell_output *output_listener =
5868 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005869 struct weston_output *output = output_listener->output;
5870 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005871
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005872 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005873
Xiong Zhang6b481422013-10-23 13:58:32 +08005874 wl_list_remove(&output_listener->destroy_listener.link);
5875 wl_list_remove(&output_listener->link);
5876 free(output_listener);
5877}
5878
5879static void
5880create_shell_output(struct desktop_shell *shell,
5881 struct weston_output *output)
5882{
5883 struct shell_output *shell_output;
5884
5885 shell_output = zalloc(sizeof *shell_output);
5886 if (shell_output == NULL)
5887 return;
5888
5889 shell_output->output = output;
5890 shell_output->shell = shell;
5891 shell_output->destroy_listener.notify = handle_output_destroy;
5892 wl_signal_add(&output->destroy_signal,
5893 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005894 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005895}
5896
5897static void
5898handle_output_create(struct wl_listener *listener, void *data)
5899{
5900 struct desktop_shell *shell =
5901 container_of(listener, struct desktop_shell, output_create_listener);
5902 struct weston_output *output = (struct weston_output *)data;
5903
5904 create_shell_output(shell, output);
5905}
5906
5907static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005908handle_output_move_layer(struct desktop_shell *shell,
5909 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005910{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005911 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005912 struct weston_view *view;
5913 float x, y;
5914
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005915 wl_list_for_each(view, &layer->view_list, layer_link) {
5916 if (view->output != output)
5917 continue;
5918
5919 x = view->geometry.x + output->move_x;
5920 y = view->geometry.y + output->move_y;
5921 weston_view_set_position(view, x, y);
5922 }
5923}
5924
5925static void
5926handle_output_move(struct wl_listener *listener, void *data)
5927{
5928 struct desktop_shell *shell;
5929
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005930 shell = container_of(listener, struct desktop_shell,
5931 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005932
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005933 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005934}
5935
5936static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005937setup_output_destroy_handler(struct weston_compositor *ec,
5938 struct desktop_shell *shell)
5939{
5940 struct weston_output *output;
5941
5942 wl_list_init(&shell->output_list);
5943 wl_list_for_each(output, &ec->output_list, link)
5944 create_shell_output(shell, output);
5945
5946 shell->output_create_listener.notify = handle_output_create;
5947 wl_signal_add(&ec->output_created_signal,
5948 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005949
5950 shell->output_move_listener.notify = handle_output_move;
5951 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005952}
5953
5954static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005955shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005956{
Tiago Vignattibe143262012-04-16 17:31:41 +03005957 struct desktop_shell *shell =
5958 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005959 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005960 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005961
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005962 /* Force state to unlocked so we don't try to fade */
5963 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005964 if (shell->child.client)
5965 wl_client_destroy(shell->child.client);
5966
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005967 wl_list_remove(&shell->idle_listener.link);
5968 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005969
5970 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005971
Xiong Zhang6b481422013-10-23 13:58:32 +08005972 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5973 wl_list_remove(&shell_output->destroy_listener.link);
5974 wl_list_remove(&shell_output->link);
5975 free(shell_output);
5976 }
5977
5978 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07005979 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005980
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005981 wl_array_for_each(ws, &shell->workspaces.array)
5982 workspace_destroy(*ws);
5983 wl_array_release(&shell->workspaces.array);
5984
Pekka Paalanen3c647232011-12-22 13:43:43 +02005985 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005986 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005987 free(shell);
5988}
5989
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005990static void
5991shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5992{
5993 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005994 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005995
5996 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005997 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5998 MODIFIER_CTRL | MODIFIER_ALT,
5999 terminate_binding, ec);
6000 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6001 click_to_activate_binding,
6002 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006003 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6004 click_to_activate_binding,
6005 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006006 weston_compositor_add_touch_binding(ec, 0,
6007 touch_to_activate_binding,
6008 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006009 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6010 MODIFIER_SUPER | MODIFIER_ALT,
6011 surface_opacity_binding, NULL);
6012 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6013 MODIFIER_SUPER, zoom_axis_binding,
6014 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006015
6016 /* configurable bindings */
6017 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006018 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6019 zoom_key_binding, NULL);
6020 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6021 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006022 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6023 maximize_binding, NULL);
6024 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6025 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006026 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6027 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006028 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006029 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6030 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006031 weston_compositor_add_button_binding(ec, BTN_LEFT,
6032 mod | MODIFIER_SHIFT,
6033 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006034
6035 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6036 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6037 rotate_binding, NULL);
6038
Daniel Stone325fc2d2012-05-30 16:31:58 +01006039 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6040 shell);
6041 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6042 ec);
6043 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6044 backlight_binding, ec);
6045 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6046 ec);
6047 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6048 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006049 weston_compositor_add_key_binding(ec, KEY_K, mod,
6050 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006051 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6052 workspace_up_binding, shell);
6053 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6054 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006055 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6056 workspace_move_surface_up_binding,
6057 shell);
6058 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6059 workspace_move_surface_down_binding,
6060 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006061
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006062 if (shell->exposay_modifier)
6063 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6064 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006065
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006066 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6067 if (shell->workspaces.num > 1) {
6068 num_workspace_bindings = shell->workspaces.num;
6069 if (num_workspace_bindings > 6)
6070 num_workspace_bindings = 6;
6071 for (i = 0; i < num_workspace_bindings; i++)
6072 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6073 workspace_f_binding,
6074 shell);
6075 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006076
6077 /* Debug bindings */
6078 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6079 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006080}
6081
Jason Ekstrand024177c2014-04-21 19:42:58 -05006082static void
6083handle_seat_created(struct wl_listener *listener, void *data)
6084{
6085 struct weston_seat *seat = data;
6086
6087 create_shell_seat(seat);
6088}
6089
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006090WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006091module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006092 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006093{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006094 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006095 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006096 struct workspace **pws;
6097 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006098 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006099
Peter Huttererf3d62272013-08-08 11:57:05 +10006100 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006101 if (shell == NULL)
6102 return -1;
6103
Kristian Høgsberg75840622011-09-06 13:48:16 -04006104 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006105
6106 shell->destroy_listener.notify = shell_destroy;
6107 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006108 shell->idle_listener.notify = idle_handler;
6109 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6110 shell->wake_listener.notify = wake_handler;
6111 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006112
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006113 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006114 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006115 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006116 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006117 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006118 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006119 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006120 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006121 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006122 ec->shell_interface.set_title = set_title;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07006123 ec->shell_interface.set_margin = set_margin;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006124
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006125 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6126 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006127 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6128 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006129 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006130
6131 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006132 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006133
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006134 if (input_panel_setup(shell) < 0)
6135 return -1;
6136
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006137 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006138
Daniel Stonedf8133b2013-11-19 11:37:14 +01006139 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6140 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6141
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006142 for (i = 0; i < shell->workspaces.num; i++) {
6143 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6144 if (pws == NULL)
6145 return -1;
6146
6147 *pws = workspace_create();
6148 if (*pws == NULL)
6149 return -1;
6150 }
6151 activate_workspace(shell, 0);
6152
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006153 weston_layer_init(&shell->minimized_layer, NULL);
6154
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006155 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006156 wl_list_init(&shell->workspaces.animation.link);
6157 shell->workspaces.animation.frame = animate_workspace_change_frame;
6158
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006159 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006160 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006161 return -1;
6162
Rafael Antognollie2a34552013-12-03 15:35:45 -02006163 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6164 shell, bind_xdg_shell) == NULL)
6165 return -1;
6166
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006167 if (wl_global_create(ec->wl_display,
6168 &desktop_shell_interface, 2,
6169 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006170 return -1;
6171
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006172 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6173 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006174 return -1;
6175
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006176 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6177 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006178 return -1;
6179
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006180 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006181
Xiong Zhang6b481422013-10-23 13:58:32 +08006182 setup_output_destroy_handler(ec, shell);
6183
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006184 loop = wl_display_get_event_loop(ec->wl_display);
6185 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006186
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006187 shell->screensaver.timer =
6188 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6189
Jason Ekstrand024177c2014-04-21 19:42:58 -05006190 wl_list_for_each(seat, &ec->seat_list, link)
6191 handle_seat_created(NULL, seat);
6192 shell->seat_create_listener.notify = handle_seat_created;
6193 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006194
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006195 screenshooter_create(ec);
6196
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006197 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006198
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006199 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006200
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006201 return 0;
6202}