blob: fac3120944a90b94f496dff085360401aa365007 [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{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001030 struct weston_view *view;
1031
Scott Moreau4272e632012-08-13 09:58:41 -06001032 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001034 /* Views that extend past the bottom of the output are still
1035 * visible after the workspace animation ends but before its layer
1036 * is hidden. In that case, we need to damage below those views so
1037 * that the screen is properly repainted. */
1038 wl_list_for_each(view, &from->layer.view_list, layer_link)
1039 weston_view_damage_below(view);
1040
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001041 wl_list_remove(&shell->workspaces.animation.link);
1042 workspace_deactivate_transforms(from);
1043 workspace_deactivate_transforms(to);
1044 shell->workspaces.anim_to = NULL;
1045
1046 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1047}
1048
1049static void
1050animate_workspace_change_frame(struct weston_animation *animation,
1051 struct weston_output *output, uint32_t msecs)
1052{
1053 struct desktop_shell *shell =
1054 container_of(animation, struct desktop_shell,
1055 workspaces.animation);
1056 struct workspace *from = shell->workspaces.anim_from;
1057 struct workspace *to = shell->workspaces.anim_to;
1058 uint32_t t;
1059 double x, y;
1060
1061 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1062 finish_workspace_change_animation(shell, from, to);
1063 return;
1064 }
1065
1066 if (shell->workspaces.anim_timestamp == 0) {
1067 if (shell->workspaces.anim_current == 0.0)
1068 shell->workspaces.anim_timestamp = msecs;
1069 else
1070 shell->workspaces.anim_timestamp =
1071 msecs -
1072 /* Invers of movement function 'y' below. */
1073 (asin(1.0 - shell->workspaces.anim_current) *
1074 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1075 M_2_PI);
1076 }
1077
1078 t = msecs - shell->workspaces.anim_timestamp;
1079
1080 /*
1081 * x = [0, π/2]
1082 * y(x) = sin(x)
1083 */
1084 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1085 y = sin(x);
1086
1087 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001088 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001089
1090 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1091 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1092 shell->workspaces.anim_current = y;
1093
Scott Moreau4272e632012-08-13 09:58:41 -06001094 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001095 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001096 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098}
1099
1100static void
1101animate_workspace_change(struct desktop_shell *shell,
1102 unsigned int index,
1103 struct workspace *from,
1104 struct workspace *to)
1105{
1106 struct weston_output *output;
1107
1108 int dir;
1109
1110 if (index > shell->workspaces.current)
1111 dir = -1;
1112 else
1113 dir = 1;
1114
1115 shell->workspaces.current = index;
1116
1117 shell->workspaces.anim_dir = dir;
1118 shell->workspaces.anim_from = from;
1119 shell->workspaces.anim_to = to;
1120 shell->workspaces.anim_current = 0.0;
1121 shell->workspaces.anim_timestamp = 0;
1122
1123 output = container_of(shell->compositor->output_list.next,
1124 struct weston_output, link);
1125 wl_list_insert(&output->animation_list,
1126 &shell->workspaces.animation.link);
1127
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001128 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129
1130 workspace_translate_in(to, 0);
1131
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001132 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001133
Scott Moreau4272e632012-08-13 09:58:41 -06001134 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001135}
1136
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001137static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001138update_workspace(struct desktop_shell *shell, unsigned int index,
1139 struct workspace *from, struct workspace *to)
1140{
1141 shell->workspaces.current = index;
1142 wl_list_insert(&from->layer.link, &to->layer.link);
1143 wl_list_remove(&from->layer.link);
1144}
1145
1146static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001147change_workspace(struct desktop_shell *shell, unsigned int index)
1148{
1149 struct workspace *from;
1150 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001151 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001152
1153 if (index == shell->workspaces.current)
1154 return;
1155
1156 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001158 return;
1159
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001160 from = get_current_workspace(shell);
1161 to = get_workspace(shell, index);
1162
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163 if (shell->workspaces.anim_from == to &&
1164 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001165 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001166 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001167 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001168 return;
1169 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001170
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001171 if (shell->workspaces.anim_to != NULL)
1172 finish_workspace_change_animation(shell,
1173 shell->workspaces.anim_from,
1174 shell->workspaces.anim_to);
1175
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001176 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001177
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001178 if (shell->focus_animation_type != ANIMATION_NONE) {
1179 wl_list_for_each(state, &from->focus_list, link)
1180 if (state->keyboard_focus)
1181 animate_focus_change(shell, from,
1182 get_default_view(state->keyboard_focus), NULL);
1183
1184 wl_list_for_each(state, &to->focus_list, link)
1185 if (state->keyboard_focus)
1186 animate_focus_change(shell, to,
1187 NULL, get_default_view(state->keyboard_focus));
1188 }
1189
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001190 if (workspace_is_empty(to) && workspace_is_empty(from))
1191 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001192 else
1193 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001194
1195 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001196}
1197
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198static bool
1199workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1200{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001202 struct wl_list *e;
1203
1204 if (wl_list_empty(list))
1205 return false;
1206
1207 e = list->next;
1208
1209 if (e->next != list)
1210 return false;
1211
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001213}
1214
1215static void
Philip Withnall659163d2013-11-25 18:01:36 +00001216move_surface_to_workspace(struct desktop_shell *shell,
1217 struct shell_surface *shsurf,
1218 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001219{
1220 struct workspace *from;
1221 struct workspace *to;
1222 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001223 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001224 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001225
1226 if (workspace == shell->workspaces.current)
1227 return;
1228
Philip Withnall659163d2013-11-25 18:01:36 +00001229 view = get_default_view(shsurf->surface);
1230 if (!view)
1231 return;
1232
1233 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1234
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001235 if (workspace >= shell->workspaces.num)
1236 workspace = shell->workspaces.num - 1;
1237
Jonas Ådahle9d22502012-08-29 22:13:01 +02001238 from = get_current_workspace(shell);
1239 to = get_workspace(shell, workspace);
1240
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 wl_list_remove(&view->layer_link);
1242 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001243
Philip Withnall648a4dd2013-11-25 18:01:44 +00001244 shell_surface_update_child_surface_layers(shsurf);
1245
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001247 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1248 if (!seat->keyboard)
1249 continue;
1250
1251 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001253 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001254 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001255
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001257}
1258
1259static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001260take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001261 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001262 unsigned int index)
1263{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001264 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001266 struct shell_surface *shsurf;
1267 struct workspace *from;
1268 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001269 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270
Pekka Paalanen01388e22013-04-25 13:57:44 +03001271 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 view = get_default_view(surface);
1273 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001274 index == shell->workspaces.current ||
1275 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001276 return;
1277
1278 from = get_current_workspace(shell);
1279 to = get_workspace(shell, index);
1280
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 wl_list_remove(&view->layer_link);
1282 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001283
Philip Withnall659163d2013-11-25 18:01:36 +00001284 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001285 if (shsurf != NULL)
1286 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001287
Jonas Ådahle9d22502012-08-29 22:13:01 +02001288 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289 drop_focus_state(shell, from, surface);
1290
1291 if (shell->workspaces.anim_from == to &&
1292 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001293 wl_list_remove(&to->layer.link);
1294 wl_list_insert(from->layer.link.prev, &to->layer.link);
1295
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001296 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001297 broadcast_current_workspace_state(shell);
1298
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001299 return;
1300 }
1301
1302 if (shell->workspaces.anim_to != NULL)
1303 finish_workspace_change_animation(shell,
1304 shell->workspaces.anim_from,
1305 shell->workspaces.anim_to);
1306
1307 if (workspace_is_empty(from) &&
1308 workspace_has_only(to, surface))
1309 update_workspace(shell, index, from, to);
1310 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001311 if (shsurf != NULL &&
1312 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001313 wl_list_insert(&shell->workspaces.anim_sticky_list,
1314 &shsurf->workspace_transform.link);
1315
1316 animate_workspace_change(shell, index, from, to);
1317 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001318
1319 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001320
1321 state = ensure_focus_state(shell, seat);
1322 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001323 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001324}
1325
1326static void
1327workspace_manager_move_surface(struct wl_client *client,
1328 struct wl_resource *resource,
1329 struct wl_resource *surface_resource,
1330 uint32_t workspace)
1331{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001332 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001333 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001334 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001335 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001336 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001337
Pekka Paalanen01388e22013-04-25 13:57:44 +03001338 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001339 shell_surface = get_shell_surface(main_surface);
1340 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001342
1343 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001344}
1345
1346static const struct workspace_manager_interface workspace_manager_implementation = {
1347 workspace_manager_move_surface,
1348};
1349
1350static void
1351unbind_resource(struct wl_resource *resource)
1352{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001353 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001354}
1355
1356static void
1357bind_workspace_manager(struct wl_client *client,
1358 void *data, uint32_t version, uint32_t id)
1359{
1360 struct desktop_shell *shell = data;
1361 struct wl_resource *resource;
1362
Jason Ekstranda85118c2013-06-27 20:17:02 -05001363 resource = wl_resource_create(client,
1364 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001365
1366 if (resource == NULL) {
1367 weston_log("couldn't add workspace manager object");
1368 return;
1369 }
1370
Jason Ekstranda85118c2013-06-27 20:17:02 -05001371 wl_resource_set_implementation(resource,
1372 &workspace_manager_implementation,
1373 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001374 wl_list_insert(&shell->workspaces.client_list,
1375 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001376
1377 workspace_manager_send_state(resource,
1378 shell->workspaces.current,
1379 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001380}
1381
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001382static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001383touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1384 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1385{
1386}
1387
1388static void
1389touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1390{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001391 struct weston_touch_move_grab *move =
1392 (struct weston_touch_move_grab *) container_of(
1393 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001394
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001395 if (touch_id == 0)
1396 move->active = 0;
1397
Jonas Ådahl9484b692013-12-02 22:05:03 +01001398 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001399 shell_touch_grab_end(&move->base);
1400 free(move);
1401 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001402}
1403
1404static void
1405touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1406 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1407{
1408 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1409 struct shell_surface *shsurf = move->base.shsurf;
1410 struct weston_surface *es;
1411 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1412 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1413
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001414 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001415 return;
1416
1417 es = shsurf->surface;
1418
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001419 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001420
1421 weston_compositor_schedule_repaint(es->compositor);
1422}
1423
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001424static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001425touch_move_grab_frame(struct weston_touch_grab *grab)
1426{
1427}
1428
1429static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001430touch_move_grab_cancel(struct weston_touch_grab *grab)
1431{
1432 struct weston_touch_move_grab *move =
1433 (struct weston_touch_move_grab *) container_of(
1434 grab, struct shell_touch_grab, grab);
1435
1436 shell_touch_grab_end(&move->base);
1437 free(move);
1438}
1439
Rusty Lynch1084da52013-08-15 09:10:08 -07001440static const struct weston_touch_grab_interface touch_move_grab_interface = {
1441 touch_move_grab_down,
1442 touch_move_grab_up,
1443 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001444 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001445 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001446};
1447
1448static int
1449surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1450{
1451 struct weston_touch_move_grab *move;
1452
1453 if (!shsurf)
1454 return -1;
1455
Rafael Antognolli03b16592013-12-03 15:35:42 -02001456 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001457 return 0;
1458
1459 move = malloc(sizeof *move);
1460 if (!move)
1461 return -1;
1462
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001463 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001465 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001467 seat->touch->grab_y;
1468
1469 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1470 seat->touch);
1471
1472 return 0;
1473}
1474
1475static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001476noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001477{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001478}
1479
1480static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001481constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1482{
1483 struct shell_surface *shsurf = move->base.shsurf;
1484 struct weston_pointer *pointer = move->base.grab.pointer;
Kristian Høgsberg3434b332014-04-29 16:38:23 -07001485 int x, y, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001486 const int safety = 50;
1487
1488 x = wl_fixed_to_int(pointer->x + move->dx);
1489 y = wl_fixed_to_int(pointer->y + move->dy);
1490
1491 panel_height = get_output_panel_height(shsurf->shell,
1492 shsurf->surface->output);
1493 bottom = y + shsurf->surface->height - shsurf->margin.bottom;
1494 if (bottom - panel_height < safety)
1495 y = panel_height + safety -
1496 shsurf->surface->height + shsurf->margin.bottom;
1497
1498 if (move->client_initiated &&
1499 y + shsurf->margin.top < panel_height)
1500 y = panel_height - shsurf->margin.top;
1501
1502 *cx = x;
1503 *cy = y;
1504}
1505
1506static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001507move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1508 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001509{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001510 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001511 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001512 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001513 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001514
1515 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001516 if (!shsurf)
1517 return;
1518
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001519 constrain_position(move, &cx, &cy);
1520
1521 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001522
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001524}
1525
1526static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001527move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001528 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001529{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001530 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1531 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001532 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001533 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001534
Daniel Stone4dbadb12012-05-30 16:31:51 +01001535 if (pointer->button_count == 0 &&
1536 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001537 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001538 free(grab);
1539 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001540}
1541
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001542static void
1543move_grab_cancel(struct weston_pointer_grab *grab)
1544{
1545 struct shell_grab *shell_grab =
1546 container_of(grab, struct shell_grab, grab);
1547
1548 shell_grab_end(shell_grab);
1549 free(grab);
1550}
1551
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001552static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001553 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001554 move_grab_motion,
1555 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001556 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001557};
1558
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001559static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001560surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1561 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001562{
1563 struct weston_move_grab *move;
1564
1565 if (!shsurf)
1566 return -1;
1567
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001568 if (shsurf->grabbed ||
1569 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001570 return 0;
1571
1572 move = malloc(sizeof *move);
1573 if (!move)
1574 return -1;
1575
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001577 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001579 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001580 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581
1582 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001583 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584
1585 return 0;
1586}
1587
1588static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001589common_surface_move(struct wl_resource *resource,
1590 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001591{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001592 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001593 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001594 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001595
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001596 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001597 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001598 seat->pointer->button_count > 0 &&
1599 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001601 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001602 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001603 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001604 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001605 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001606 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001608 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001609 (surface_touch_move(shsurf, seat) < 0))
1610 wl_resource_post_no_memory(resource);
1611 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001612}
1613
Rafael Antognollie2a34552013-12-03 15:35:45 -02001614static void
1615shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1616 struct wl_resource *seat_resource, uint32_t serial)
1617{
1618 common_surface_move(resource, seat_resource, serial);
1619}
1620
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621struct weston_resize_grab {
1622 struct shell_grab base;
1623 uint32_t edges;
1624 int32_t width, height;
1625};
1626
1627static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001628resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1629 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001630{
1631 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001632 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633 struct shell_surface *shsurf = resize->base.shsurf;
1634 int32_t width, height;
1635 wl_fixed_t from_x, from_y;
1636 wl_fixed_t to_x, to_y;
1637
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001638 weston_pointer_move(pointer, x, y);
1639
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001640 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001641 return;
1642
Jason Ekstranda7af7042013-10-12 22:38:11 -05001643 weston_view_from_global_fixed(shsurf->view,
1644 pointer->grab_x, pointer->grab_y,
1645 &from_x, &from_y);
1646 weston_view_from_global_fixed(shsurf->view,
1647 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001648
1649 width = resize->width;
1650 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1651 width += wl_fixed_to_int(from_x - to_x);
1652 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1653 width += wl_fixed_to_int(to_x - from_x);
1654 }
1655
1656 height = resize->height;
1657 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1658 height += wl_fixed_to_int(from_y - to_y);
1659 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1660 height += wl_fixed_to_int(to_y - from_y);
1661 }
1662
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001663 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001664}
1665
1666static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001667send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001668{
1669 struct shell_surface *shsurf = get_shell_surface(surface);
1670
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001671 assert(shsurf);
1672
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001673 if (shsurf->resource)
1674 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001675 shsurf->resize_edges,
1676 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677}
1678
1679static const struct weston_shell_client shell_client = {
1680 send_configure
1681};
1682
1683static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001684resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001685 uint32_t time, uint32_t button, uint32_t state_w)
1686{
1687 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001688 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001689 enum wl_pointer_button_state state = state_w;
1690
1691 if (pointer->button_count == 0 &&
1692 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1693 shell_grab_end(&resize->base);
1694 free(grab);
1695 }
1696}
1697
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001698static void
1699resize_grab_cancel(struct weston_pointer_grab *grab)
1700{
1701 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1702
1703 shell_grab_end(&resize->base);
1704 free(grab);
1705}
1706
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001707static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708 noop_grab_focus,
1709 resize_grab_motion,
1710 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001711 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001712};
1713
Giulio Camuffob8366642013-04-25 13:57:46 +03001714/*
1715 * Returns the bounding box of a surface and all its sub-surfaces,
1716 * in the surface coordinates system. */
1717static void
1718surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1719 int32_t *y, int32_t *w, int32_t *h) {
1720 pixman_region32_t region;
1721 pixman_box32_t *box;
1722 struct weston_subsurface *subsurface;
1723
1724 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725 surface->width,
1726 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001727
1728 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1729 pixman_region32_union_rect(&region, &region,
1730 subsurface->position.x,
1731 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001732 subsurface->surface->width,
1733 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001734 }
1735
1736 box = pixman_region32_extents(&region);
1737 if (x)
1738 *x = box->x1;
1739 if (y)
1740 *y = box->y1;
1741 if (w)
1742 *w = box->x2 - box->x1;
1743 if (h)
1744 *h = box->y2 - box->y1;
1745
1746 pixman_region32_fini(&region);
1747}
1748
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001749static int
1750surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001751 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001752{
1753 struct weston_resize_grab *resize;
1754
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001755 if (shsurf->grabbed ||
1756 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001757 return 0;
1758
1759 if (edges == 0 || edges > 15 ||
1760 (edges & 3) == 3 || (edges & 12) == 12)
1761 return 0;
1762
1763 resize = malloc(sizeof *resize);
1764 if (!resize)
1765 return -1;
1766
1767 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001768 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1769 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001770
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001771 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001772 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001773 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774
1775 return 0;
1776}
1777
1778static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001779common_surface_resize(struct wl_resource *resource,
1780 struct wl_resource *seat_resource, uint32_t serial,
1781 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001782{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001783 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001784 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001785 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001786
Kristian Høgsberge3148752013-05-06 23:19:49 -04001787 if (seat->pointer->button_count == 0 ||
1788 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001789 seat->pointer->focus == NULL)
1790 return;
1791
1792 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1793 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001794 return;
1795
Kristian Høgsberge3148752013-05-06 23:19:49 -04001796 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001797 wl_resource_post_no_memory(resource);
1798}
1799
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001800static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001801shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1802 struct wl_resource *seat_resource, uint32_t serial,
1803 uint32_t edges)
1804{
1805 common_surface_resize(resource, seat_resource, serial, edges);
1806}
1807
1808static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001809busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001810{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001811 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001812 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001814 wl_fixed_t sx, sy;
1815
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 view = weston_compositor_pick_view(pointer->seat->compositor,
1817 pointer->x, pointer->y,
1818 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001819
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001820 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1821 shell_grab_end(grab);
1822 free(grab);
1823 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001824}
1825
1826static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001827busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1828 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001829{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001830 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001831}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001832
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001833static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001834busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001835 uint32_t time, uint32_t button, uint32_t state)
1836{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001837 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001838 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001839 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001840
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001841 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001842 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001843 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001844 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001845 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001846 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001847 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001848}
1849
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001850static void
1851busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1852{
1853 struct shell_grab *grab = (struct shell_grab *) base;
1854
1855 shell_grab_end(grab);
1856 free(grab);
1857}
1858
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001859static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001860 busy_cursor_grab_focus,
1861 busy_cursor_grab_motion,
1862 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001863 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001864};
1865
1866static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001867set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001868{
1869 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001870
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001871 if (pointer->grab->interface == &busy_cursor_grab_interface)
1872 return;
1873
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001874 grab = malloc(sizeof *grab);
1875 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001876 return;
1877
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001878 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1879 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001880 /* Mark the shsurf as ungrabbed so that button binding is able
1881 * to move it. */
1882 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001883}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001884
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001885static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001886end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001887{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001888 struct shell_grab *grab;
1889 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001890
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001891 wl_list_for_each(seat, &compositor->seat_list, link) {
1892 if (seat->pointer == NULL)
1893 continue;
1894
1895 grab = (struct shell_grab *) seat->pointer->grab;
1896 if (grab->grab.interface == &busy_cursor_grab_interface &&
1897 wl_resource_get_client(grab->shsurf->resource) == client) {
1898 shell_grab_end(grab);
1899 free(grab);
1900 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001901 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001902}
1903
Scott Moreau9521d5e2012-04-19 13:06:17 -06001904static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001905handle_shell_client_destroy(struct wl_listener *listener, void *data);
1906
1907static int
1908xdg_ping_timeout_handler(void *data)
1909{
1910 struct shell_client *sc = data;
1911 struct weston_seat *seat;
1912 struct shell_surface *shsurf;
1913
1914 /* Client is not responding */
1915 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001916 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1917 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1918 continue;
1919 if (seat->pointer->focus->surface->resource == NULL)
1920 continue;
1921
1922 shsurf = get_shell_surface(seat->pointer->focus->surface);
1923 if (shsurf &&
1924 wl_resource_get_client(shsurf->resource) == sc->client)
1925 set_busy_cursor(shsurf, seat->pointer);
1926 }
1927
1928 return 1;
1929}
1930
1931static void
1932handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1933{
1934 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05001935 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001936 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001937 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001938
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001939 if (sc->unresponsive) {
1940 xdg_ping_timeout_handler(sc);
1941 return;
1942 }
1943
1944 sc->ping_serial = serial;
1945 loop = wl_display_get_event_loop(compositor->wl_display);
1946 if (sc->ping_timer == NULL)
1947 sc->ping_timer =
1948 wl_event_loop_add_timer(loop,
1949 xdg_ping_timeout_handler, sc);
1950 if (sc->ping_timer == NULL)
1951 return;
1952
1953 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1954
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001955 if (shell_surface_is_xdg_surface(shsurf) ||
1956 shell_surface_is_xdg_popup(shsurf))
1957 xdg_shell_send_ping(sc->resource, serial);
1958 else if (shell_surface_is_wl_shell_surface(shsurf))
1959 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001960}
1961
Scott Moreauff1db4a2012-04-17 19:06:18 -06001962static void
1963ping_handler(struct weston_surface *surface, uint32_t serial)
1964{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001965 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001966
1967 if (!shsurf)
1968 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001969 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001970 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001971 if (shsurf->surface == shsurf->shell->grab_surface)
1972 return;
1973
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001974 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001975}
1976
1977static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001978handle_pointer_focus(struct wl_listener *listener, void *data)
1979{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001980 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001981 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001982 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001983 uint32_t serial;
1984
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001986 return;
1987
Jason Ekstranda7af7042013-10-12 22:38:11 -05001988 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001989 serial = wl_display_next_serial(compositor->wl_display);
1990 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001991}
1992
1993static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001994shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1995{
1996 if (--shsurf->focus_count == 0)
1997 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001998 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001999}
2000
2001static void
2002shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2003{
2004 if (shsurf->focus_count++ == 0)
2005 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05002006 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002007}
2008
2009static void
2010handle_keyboard_focus(struct wl_listener *listener, void *data)
2011{
2012 struct weston_keyboard *keyboard = data;
2013 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2014
2015 if (seat->focused_surface) {
2016 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2017 if (shsurf)
2018 shell_surface_lose_keyboard_focus(shsurf);
2019 }
2020
2021 seat->focused_surface = keyboard->focus;
2022
2023 if (seat->focused_surface) {
2024 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2025 if (shsurf)
2026 shell_surface_gain_keyboard_focus(shsurf);
2027 }
2028}
2029
2030static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002031shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002032{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002033 if (sc->ping_serial != serial)
2034 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002035
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002036 sc->unresponsive = 0;
2037 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002038
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002039 if (sc->ping_timer) {
2040 wl_event_source_remove(sc->ping_timer);
2041 sc->ping_timer = NULL;
2042 }
2043
2044}
2045
2046static void
2047shell_surface_pong(struct wl_client *client,
2048 struct wl_resource *resource, uint32_t serial)
2049{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002050 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2051 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002052
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002053 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002054}
2055
2056static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002057set_title(struct shell_surface *shsurf, const char *title)
2058{
2059 free(shsurf->title);
2060 shsurf->title = strdup(title);
2061}
2062
2063static void
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002064set_margin(struct shell_surface *shsurf,
2065 int32_t left, int32_t right, int32_t top, int32_t bottom)
2066{
2067 shsurf->margin.left = left;
2068 shsurf->margin.right = right;
2069 shsurf->margin.top = top;
2070 shsurf->margin.bottom = bottom;
2071}
2072
2073static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002074shell_surface_set_title(struct wl_client *client,
2075 struct wl_resource *resource, const char *title)
2076{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002077 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002078
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002079 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002080}
2081
2082static void
2083shell_surface_set_class(struct wl_client *client,
2084 struct wl_resource *resource, const char *class)
2085{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002086 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002087
2088 free(shsurf->class);
2089 shsurf->class = strdup(class);
2090}
2091
Alex Wu4539b082012-03-01 12:57:46 +08002092static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002093restore_output_mode(struct weston_output *output)
2094{
Hardening57388e42013-09-18 23:56:36 +02002095 if (output->current_mode != output->original_mode ||
2096 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002097 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002098 output->original_mode,
2099 output->original_scale,
2100 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002101}
2102
2103static void
2104restore_all_output_modes(struct weston_compositor *compositor)
2105{
2106 struct weston_output *output;
2107
2108 wl_list_for_each(output, &compositor->output_list, link)
2109 restore_output_mode(output);
2110}
2111
Philip Withnallbecb77e2013-11-25 18:01:30 +00002112static int
2113get_output_panel_height(struct desktop_shell *shell,
2114 struct weston_output *output)
2115{
2116 struct weston_view *view;
2117 int panel_height = 0;
2118
2119 if (!output)
2120 return 0;
2121
2122 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2123 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002124 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002125 break;
2126 }
2127 }
2128
2129 return panel_height;
2130}
2131
Philip Withnall07926d92013-11-25 18:01:40 +00002132/* The surface will be inserted into the list immediately after the link
2133 * returned by this function (i.e. will be stacked immediately above the
2134 * returned link). */
2135static struct wl_list *
2136shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2137{
2138 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002139 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002140
2141 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002142 case SHELL_SURFACE_XWAYLAND:
2143 return &shsurf->shell->fullscreen_layer.view_list;
2144
2145 case SHELL_SURFACE_NONE:
2146 return NULL;
2147
Kristian Høgsbergead52422014-01-01 13:51:52 -08002148 case SHELL_SURFACE_POPUP:
2149 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002150 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002151 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002152 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002153 /* Move the surface to its parent layer so
2154 * that surfaces which are transient for
2155 * fullscreen surfaces don't get hidden by the
2156 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002157
2158 /* TODO: Handle a parent with multiple views */
2159 parent = get_default_view(shsurf->parent);
2160 if (parent)
2161 return parent->layer_link.prev;
2162 }
Philip Withnall07926d92013-11-25 18:01:40 +00002163
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002164 /* Move the surface to a normal workspace layer so that surfaces
2165 * which were previously fullscreen or transient are no longer
2166 * rendered on top. */
2167 ws = get_current_workspace(shsurf->shell);
2168 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002169 }
2170
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002171 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002172}
2173
Philip Withnall648a4dd2013-11-25 18:01:44 +00002174static void
2175shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2176{
2177 struct shell_surface *child;
2178
2179 /* Move the child layers to the same workspace as shsurf. They will be
2180 * stacked above shsurf. */
2181 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2182 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002183 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002184 weston_view_geometry_dirty(child->view);
2185 wl_list_remove(&child->view->layer_link);
2186 wl_list_insert(shsurf->view->layer_link.prev,
2187 &child->view->layer_link);
2188 weston_view_geometry_dirty(child->view);
2189 weston_surface_damage(child->surface);
2190
2191 /* Recurse. We don’t expect this to recurse very far (if
2192 * at all) because that would imply we have transient
2193 * (or popup) children of transient surfaces, which
2194 * would be unusual. */
2195 shell_surface_update_child_surface_layers(child);
2196 }
2197 }
2198}
2199
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002200/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002201 * geometry to ensure the changes are redrawn.
2202 *
2203 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2204 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002205static void
2206shell_surface_update_layer(struct shell_surface *shsurf)
2207{
2208 struct wl_list *new_layer_link;
2209
2210 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2211
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002212 if (new_layer_link == NULL)
2213 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002214 if (new_layer_link == &shsurf->view->layer_link)
2215 return;
2216
2217 weston_view_geometry_dirty(shsurf->view);
2218 wl_list_remove(&shsurf->view->layer_link);
2219 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2220 weston_view_geometry_dirty(shsurf->view);
2221 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002222
2223 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002224}
2225
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002226static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002227shell_surface_set_parent(struct shell_surface *shsurf,
2228 struct weston_surface *parent)
2229{
2230 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002231
2232 wl_list_remove(&shsurf->children_link);
2233 wl_list_init(&shsurf->children_link);
2234
2235 /* Insert into the parent surface’s child list. */
2236 if (parent != NULL) {
2237 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2238 if (parent_shsurf != NULL)
2239 wl_list_insert(&parent_shsurf->children_list,
2240 &shsurf->children_link);
2241 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002242}
2243
2244static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002245shell_surface_set_output(struct shell_surface *shsurf,
2246 struct weston_output *output)
2247{
2248 struct weston_surface *es = shsurf->surface;
2249
2250 /* get the default output, if the client set it as NULL
2251 check whether the ouput is available */
2252 if (output)
2253 shsurf->output = output;
2254 else if (es->output)
2255 shsurf->output = es->output;
2256 else
2257 shsurf->output = get_default_output(es->compositor);
2258}
2259
2260static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002261surface_clear_next_states(struct shell_surface *shsurf)
2262{
2263 shsurf->next_state.maximized = false;
2264 shsurf->next_state.fullscreen = false;
2265
2266 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2267 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2268 shsurf->state_changed = true;
2269}
2270
2271static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002272set_toplevel(struct shell_surface *shsurf)
2273{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002274 shell_surface_set_parent(shsurf, NULL);
2275 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002276 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002277
2278 /* The layer_link is updated in set_surface_type(),
2279 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002280}
2281
2282static void
2283shell_surface_set_toplevel(struct wl_client *client,
2284 struct wl_resource *resource)
2285{
2286 struct shell_surface *surface = wl_resource_get_user_data(resource);
2287
2288 set_toplevel(surface);
2289}
2290
2291static void
2292set_transient(struct shell_surface *shsurf,
2293 struct weston_surface *parent, int x, int y, uint32_t flags)
2294{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002295 assert(parent != NULL);
2296
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002297 shell_surface_set_parent(shsurf, parent);
2298
2299 surface_clear_next_states(shsurf);
2300
Philip Withnallbecb77e2013-11-25 18:01:30 +00002301 shsurf->transient.x = x;
2302 shsurf->transient.y = y;
2303 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002304
Rafael Antognollied207b42013-12-03 15:35:43 -02002305 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002306 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002307 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002308
2309 /* The layer_link is updated in set_surface_type(),
2310 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002311}
2312
2313static void
2314shell_surface_set_transient(struct wl_client *client,
2315 struct wl_resource *resource,
2316 struct wl_resource *parent_resource,
2317 int x, int y, uint32_t flags)
2318{
2319 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2320 struct weston_surface *parent =
2321 wl_resource_get_user_data(parent_resource);
2322
2323 set_transient(shsurf, parent, x, y, flags);
2324}
2325
2326static void
2327set_fullscreen(struct shell_surface *shsurf,
2328 uint32_t method,
2329 uint32_t framerate,
2330 struct weston_output *output)
2331{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002332 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002333
2334 shsurf->fullscreen_output = shsurf->output;
2335 shsurf->fullscreen.type = method;
2336 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002337
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002338 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002339
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002340 shsurf->client->send_configure(shsurf->surface,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002341 shsurf->output->width,
2342 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002343
2344 /* The layer_link is updated in set_surface_type(),
2345 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002346}
2347
2348static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002349weston_view_set_initial_position(struct weston_view *view,
2350 struct desktop_shell *shell);
2351
2352static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002353unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002354{
Philip Withnallf85fe842013-11-25 18:01:37 +00002355 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002356 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2357 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002358 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002359 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002360
Alex Wu4539b082012-03-01 12:57:46 +08002361 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2362 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002363
Alex Wu4539b082012-03-01 12:57:46 +08002364 wl_list_remove(&shsurf->fullscreen.transform.link);
2365 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002366
Jason Ekstranda7af7042013-10-12 22:38:11 -05002367 if (shsurf->fullscreen.black_view)
2368 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2369 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002370
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002371 if (shsurf->saved_position_valid)
2372 weston_view_set_position(shsurf->view,
2373 shsurf->saved_x, shsurf->saved_y);
2374 else
2375 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2376
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002377 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002378 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002379 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002380 shsurf->saved_rotation_valid = false;
2381 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002382
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002383 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002384}
2385
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002386static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002387shell_surface_set_fullscreen(struct wl_client *client,
2388 struct wl_resource *resource,
2389 uint32_t method,
2390 uint32_t framerate,
2391 struct wl_resource *output_resource)
2392{
2393 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2394 struct weston_output *output;
2395
2396 if (output_resource)
2397 output = wl_resource_get_user_data(output_resource);
2398 else
2399 output = NULL;
2400
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002401 shell_surface_set_parent(shsurf, NULL);
2402
Rafael Antognolli03b16592013-12-03 15:35:42 -02002403 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002404 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002405
2406 shsurf->next_state.fullscreen = true;
2407 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002408}
2409
2410static void
2411set_popup(struct shell_surface *shsurf,
2412 struct weston_surface *parent,
2413 struct weston_seat *seat,
2414 uint32_t serial,
2415 int32_t x,
2416 int32_t y)
2417{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002418 assert(parent != NULL);
2419
Philip Withnallbecb77e2013-11-25 18:01:30 +00002420 shsurf->popup.shseat = get_shell_seat(seat);
2421 shsurf->popup.serial = serial;
2422 shsurf->popup.x = x;
2423 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002424
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002425 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002426}
2427
2428static void
2429shell_surface_set_popup(struct wl_client *client,
2430 struct wl_resource *resource,
2431 struct wl_resource *seat_resource,
2432 uint32_t serial,
2433 struct wl_resource *parent_resource,
2434 int32_t x, int32_t y, uint32_t flags)
2435{
2436 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002437 struct weston_surface *parent =
2438 wl_resource_get_user_data(parent_resource);
2439
2440 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002441
Rafael Antognolli03b16592013-12-03 15:35:42 -02002442 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002443 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002444 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002445 wl_resource_get_user_data(seat_resource),
2446 serial, x, y);
2447}
2448
2449static void
2450set_maximized(struct shell_surface *shsurf,
2451 struct weston_output *output)
2452{
2453 struct desktop_shell *shell;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002454 uint32_t panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002455
Philip Withnall352e7ed2013-11-25 18:01:35 +00002456 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002457
2458 shell = shell_surface_get_shell(shsurf);
2459 panel_height = get_output_panel_height(shell, shsurf->output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002460
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002461 shsurf->client->send_configure(shsurf->surface,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002462 shsurf->output->width,
2463 shsurf->output->height - panel_height);
2464
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002465 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002466}
2467
2468static void
2469unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002470{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002471 /* undo all maximized things here */
2472 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002473
2474 if (shsurf->saved_position_valid)
2475 weston_view_set_position(shsurf->view,
2476 shsurf->saved_x, shsurf->saved_y);
2477 else
2478 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002479
2480 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002481 wl_list_insert(&shsurf->view->geometry.transformation_list,
2482 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002483 shsurf->saved_rotation_valid = false;
2484 }
2485
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002486 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002487}
2488
Philip Withnallbecb77e2013-11-25 18:01:30 +00002489static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002490set_minimized(struct weston_surface *surface, uint32_t is_true)
2491{
2492 struct shell_surface *shsurf;
2493 struct workspace *current_ws;
2494 struct weston_seat *seat;
2495 struct weston_surface *focus;
2496 struct weston_view *view;
2497
2498 view = get_default_view(surface);
2499 if (!view)
2500 return;
2501
2502 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2503
2504 shsurf = get_shell_surface(surface);
2505 current_ws = get_current_workspace(shsurf->shell);
2506
2507 wl_list_remove(&view->layer_link);
2508 /* hide or show, depending on the state */
2509 if (is_true) {
2510 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2511
2512 drop_focus_state(shsurf->shell, current_ws, view->surface);
2513 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2514 if (!seat->keyboard)
2515 continue;
2516 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2517 if (focus == view->surface)
2518 weston_keyboard_set_focus(seat->keyboard, NULL);
2519 }
2520 }
2521 else {
2522 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2523
2524 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2525 if (!seat->keyboard)
2526 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002527 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002528 }
2529 }
2530
2531 shell_surface_update_child_surface_layers(shsurf);
2532
2533 weston_view_damage_below(view);
2534}
2535
2536static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002537shell_surface_set_maximized(struct wl_client *client,
2538 struct wl_resource *resource,
2539 struct wl_resource *output_resource)
2540{
2541 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2542 struct weston_output *output;
2543
2544 if (output_resource)
2545 output = wl_resource_get_user_data(output_resource);
2546 else
2547 output = NULL;
2548
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002549 shell_surface_set_parent(shsurf, NULL);
2550
Rafael Antognolli03b16592013-12-03 15:35:42 -02002551 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002552 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002553
2554 shsurf->next_state.maximized = true;
2555 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002556}
2557
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002558/* This is only ever called from set_surface_type(), so there’s no need to
2559 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002560static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002561reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002562{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002563 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002564 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002565 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002566 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002567
Pekka Paalanen98262232011-12-01 10:42:22 +02002568 return 0;
2569}
2570
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002571static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002572set_full_output(struct shell_surface *shsurf)
2573{
2574 shsurf->saved_x = shsurf->view->geometry.x;
2575 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002576 shsurf->saved_width = shsurf->surface->width;
2577 shsurf->saved_height = shsurf->surface->height;
2578 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002579 shsurf->saved_position_valid = true;
2580
2581 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2582 wl_list_remove(&shsurf->rotation.transform.link);
2583 wl_list_init(&shsurf->rotation.transform.link);
2584 weston_view_geometry_dirty(shsurf->view);
2585 shsurf->saved_rotation_valid = true;
2586 }
2587}
2588
2589static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002590set_surface_type(struct shell_surface *shsurf)
2591{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002592 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002594
Philip Withnallbecb77e2013-11-25 18:01:30 +00002595 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002596
Rafael Antognolli03b16592013-12-03 15:35:42 -02002597 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002598 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002599
2600 switch (shsurf->type) {
2601 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002602 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002603 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002604 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002605 weston_view_set_position(shsurf->view,
2606 pev->geometry.x + shsurf->transient.x,
2607 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002608 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002609 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002610
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002611 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002612 weston_view_set_position(shsurf->view, shsurf->transient.x,
2613 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002614 break;
2615
Philip Withnall0f640e22013-11-25 18:01:31 +00002616 case SHELL_SURFACE_POPUP:
2617 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002618 default:
2619 break;
2620 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002621
2622 /* Update the surface’s layer. */
2623 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002624}
2625
Tiago Vignattibe143262012-04-16 17:31:41 +03002626static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002627shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002628{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002629 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002630}
2631
Alex Wu21858432012-04-01 20:13:08 +08002632static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002633black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002634
Jason Ekstranda7af7042013-10-12 22:38:11 -05002635static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002636create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002637 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002638 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002639{
2640 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002641 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002642
2643 surface = weston_surface_create(ec);
2644 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002645 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002646 return NULL;
2647 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002648 view = weston_view_create(surface);
2649 if (surface == NULL) {
2650 weston_log("no memory\n");
2651 weston_surface_destroy(surface);
2652 return NULL;
2653 }
Alex Wu4539b082012-03-01 12:57:46 +08002654
Alex Wu21858432012-04-01 20:13:08 +08002655 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002656 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002657 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002658 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002659 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002660 pixman_region32_fini(&surface->input);
2661 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002662
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002663 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002664 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002665
2666 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002667}
2668
Philip Withnalled8f1a92013-11-25 18:01:43 +00002669static void
2670shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2671{
2672 struct weston_output *output = shsurf->fullscreen_output;
2673
Rafael Antognolli03b16592013-12-03 15:35:42 -02002674 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002675
2676 if (!shsurf->fullscreen.black_view)
2677 shsurf->fullscreen.black_view =
2678 create_black_surface(shsurf->surface->compositor,
2679 shsurf->surface,
2680 output->x, output->y,
2681 output->width,
2682 output->height);
2683
2684 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2685 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2686 wl_list_insert(&shsurf->view->layer_link,
2687 &shsurf->fullscreen.black_view->layer_link);
2688 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2689 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002690
2691 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002692}
2693
Alex Wu4539b082012-03-01 12:57:46 +08002694/* Create black surface and append it to the associated fullscreen surface.
2695 * Handle size dismatch and positioning according to the method. */
2696static void
2697shell_configure_fullscreen(struct shell_surface *shsurf)
2698{
2699 struct weston_output *output = shsurf->fullscreen_output;
2700 struct weston_surface *surface = shsurf->surface;
2701 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002702 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002703 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002704
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002705 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2706 restore_output_mode(output);
2707
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002708 /* Reverse the effect of lower_fullscreen_layer() */
2709 wl_list_remove(&shsurf->view->layer_link);
2710 wl_list_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2711
Philip Withnalled8f1a92013-11-25 18:01:43 +00002712 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002713
Jason Ekstranda7af7042013-10-12 22:38:11 -05002714 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002715 &surf_width, &surf_height);
2716
Alex Wu4539b082012-03-01 12:57:46 +08002717 switch (shsurf->fullscreen.type) {
2718 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002719 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002720 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002721 break;
2722 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002723 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002724 if (output->width == surf_width &&
2725 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002726 weston_view_set_position(shsurf->view,
2727 output->x - surf_x,
2728 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002729 break;
2730 }
2731
Alex Wu4539b082012-03-01 12:57:46 +08002732 matrix = &shsurf->fullscreen.transform.matrix;
2733 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002734
Scott Moreau1bad5db2012-08-18 01:04:05 -06002735 output_aspect = (float) output->width /
2736 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002737 /* XXX: Use surf_width and surf_height here? */
2738 surface_aspect = (float) surface->width /
2739 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002740 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002741 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002742 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002743 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002744 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002745 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002746
Alex Wu4539b082012-03-01 12:57:46 +08002747 weston_matrix_scale(matrix, scale, scale, 1);
2748 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002749 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002750 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002751 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2752 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002753 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002754
Alex Wu4539b082012-03-01 12:57:46 +08002755 break;
2756 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002757 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002758 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002759 surf_width * surface->buffer_viewport.buffer.scale,
2760 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002761 shsurf->fullscreen.framerate};
2762
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002763 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002764 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002765 weston_view_set_position(shsurf->view,
2766 output->x - surf_x,
2767 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002768 shsurf->fullscreen.black_view->surface->width = output->width;
2769 shsurf->fullscreen.black_view->surface->height = output->height;
2770 weston_view_set_position(shsurf->fullscreen.black_view,
2771 output->x - surf_x,
2772 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002773 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002774 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002775 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002776 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002777 }
Alex Wubd3354b2012-04-17 17:20:49 +08002778 }
Alex Wu4539b082012-03-01 12:57:46 +08002779 break;
2780 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002781 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002782 break;
2783 default:
2784 break;
2785 }
2786}
2787
Alex Wu4539b082012-03-01 12:57:46 +08002788static void
2789shell_map_fullscreen(struct shell_surface *shsurf)
2790{
Alex Wubd3354b2012-04-17 17:20:49 +08002791 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002792}
2793
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002794static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002795set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2796{
2797 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002798 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002799 shsurf->transient.x = x;
2800 shsurf->transient.y = y;
2801 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002802
2803 shell_surface_set_parent(shsurf, NULL);
2804
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002805 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002806 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002807}
2808
Kristian Høgsberg47792412014-05-04 13:47:06 -07002809static void
2810shell_interface_set_fullscreen(struct shell_surface *shsurf,
2811 uint32_t method,
2812 uint32_t framerate,
2813 struct weston_output *output)
2814{
2815 surface_clear_next_states(shsurf);
2816 set_fullscreen(shsurf, method, framerate, output);
2817
2818 shsurf->next_state.fullscreen = true;
2819 shsurf->state_changed = true;
2820}
2821
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002822static int
2823shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2824{
2825 return surface_move(shsurf, ws, 1);
2826}
2827
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002828static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002829
2830static void
2831destroy_shell_seat(struct wl_listener *listener, void *data)
2832{
2833 struct shell_seat *shseat =
2834 container_of(listener,
2835 struct shell_seat, seat_destroy_listener);
2836 struct shell_surface *shsurf, *prev = NULL;
2837
2838 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002839 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002840 shseat->popup_grab.client = NULL;
2841
2842 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2843 shsurf->popup.shseat = NULL;
2844 if (prev) {
2845 wl_list_init(&prev->popup.grab_link);
2846 }
2847 prev = shsurf;
2848 }
2849 wl_list_init(&prev->popup.grab_link);
2850 }
2851
2852 wl_list_remove(&shseat->seat_destroy_listener.link);
2853 free(shseat);
2854}
2855
Jason Ekstrand024177c2014-04-21 19:42:58 -05002856static void
2857shell_seat_caps_changed(struct wl_listener *listener, void *data)
2858{
2859 struct shell_seat *seat;
2860
2861 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2862
2863 if (seat->seat->keyboard &&
2864 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2865 wl_signal_add(&seat->seat->keyboard->focus_signal,
2866 &seat->keyboard_focus_listener);
2867 } else if (!seat->seat->keyboard) {
2868 wl_list_init(&seat->keyboard_focus_listener.link);
2869 }
2870
2871 if (seat->seat->pointer &&
2872 wl_list_empty(&seat->pointer_focus_listener.link)) {
2873 wl_signal_add(&seat->seat->pointer->focus_signal,
2874 &seat->pointer_focus_listener);
2875 } else if (!seat->seat->pointer) {
2876 wl_list_init(&seat->pointer_focus_listener.link);
2877 }
2878}
2879
Giulio Camuffo5085a752013-03-25 21:42:45 +01002880static struct shell_seat *
2881create_shell_seat(struct weston_seat *seat)
2882{
2883 struct shell_seat *shseat;
2884
2885 shseat = calloc(1, sizeof *shseat);
2886 if (!shseat) {
2887 weston_log("no memory to allocate shell seat\n");
2888 return NULL;
2889 }
2890
2891 shseat->seat = seat;
2892 wl_list_init(&shseat->popup_grab.surfaces_list);
2893
2894 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2895 wl_signal_add(&seat->destroy_signal,
2896 &shseat->seat_destroy_listener);
2897
Jason Ekstrand024177c2014-04-21 19:42:58 -05002898 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2899 wl_list_init(&shseat->keyboard_focus_listener.link);
2900
2901 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2902 wl_list_init(&shseat->pointer_focus_listener.link);
2903
2904 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2905 wl_signal_add(&seat->updated_caps_signal,
2906 &shseat->caps_changed_listener);
2907 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2908
Giulio Camuffo5085a752013-03-25 21:42:45 +01002909 return shseat;
2910}
2911
2912static struct shell_seat *
2913get_shell_seat(struct weston_seat *seat)
2914{
2915 struct wl_listener *listener;
2916
2917 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002918 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002919
2920 return container_of(listener,
2921 struct shell_seat, seat_destroy_listener);
2922}
2923
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002924static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002925popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002926{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002927 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002929 struct shell_seat *shseat =
2930 container_of(grab, struct shell_seat, popup_grab.grab);
2931 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002932 wl_fixed_t sx, sy;
2933
Jason Ekstranda7af7042013-10-12 22:38:11 -05002934 view = weston_compositor_pick_view(pointer->seat->compositor,
2935 pointer->x, pointer->y,
2936 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002937
Jason Ekstranda7af7042013-10-12 22:38:11 -05002938 if (view && view->surface->resource &&
2939 wl_resource_get_client(view->surface->resource) == client) {
2940 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002941 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002942 weston_pointer_set_focus(pointer, NULL,
2943 wl_fixed_from_int(0),
2944 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002945 }
2946}
2947
2948static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002949popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2950 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002951{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002952 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002953 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002954 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002955
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002956 weston_pointer_move(pointer, x, y);
2957
Neil Roberts96d790e2013-09-19 17:32:00 +01002958 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002959 weston_view_from_global_fixed(pointer->focus,
2960 pointer->x, pointer->y,
2961 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002962 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002963 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002964}
2965
2966static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002967popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002968 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002969{
2970 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002971 struct shell_seat *shseat =
2972 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002973 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002974 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002975 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002976 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002977
Neil Roberts96d790e2013-09-19 17:32:00 +01002978 resource_list = &grab->pointer->focus_resource_list;
2979 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002980 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002981 wl_resource_for_each(resource, resource_list) {
2982 wl_pointer_send_button(resource, serial,
2983 time, button, state);
2984 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002985 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002986 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002987 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002988 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002989 }
2990
Daniel Stone4dbadb12012-05-30 16:31:51 +01002991 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002992 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002993}
2994
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002995static void
2996popup_grab_cancel(struct weston_pointer_grab *grab)
2997{
2998 popup_grab_end(grab->pointer);
2999}
3000
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003001static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003002 popup_grab_focus,
3003 popup_grab_motion,
3004 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003005 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003006};
3007
3008static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003009shell_surface_send_popup_done(struct shell_surface *shsurf)
3010{
3011 if (shell_surface_is_wl_shell_surface(shsurf))
3012 wl_shell_surface_send_popup_done(shsurf->resource);
3013 else if (shell_surface_is_xdg_popup(shsurf))
3014 xdg_popup_send_popup_done(shsurf->resource,
3015 shsurf->popup.serial);
3016}
3017
3018static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003019popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003020{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003021 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003022 struct shell_seat *shseat =
3023 container_of(grab, struct shell_seat, popup_grab.grab);
3024 struct shell_surface *shsurf;
3025 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003026
3027 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003028 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003029 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003030 shseat->popup_grab.grab.interface = NULL;
3031 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003032 /* Send the popup_done event to all the popups open */
3033 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003034 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003035 shsurf->popup.shseat = NULL;
3036 if (prev) {
3037 wl_list_init(&prev->popup.grab_link);
3038 }
3039 prev = shsurf;
3040 }
3041 wl_list_init(&prev->popup.grab_link);
3042 wl_list_init(&shseat->popup_grab.surfaces_list);
3043 }
3044}
3045
3046static void
3047add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3048{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003049 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003050
3051 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003052 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003053 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003054 /* We must make sure here that this popup was opened after
3055 * a mouse press, and not just by moving around with other
3056 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003057 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003058 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003059
Rob Bradforddfe31052013-06-26 19:49:11 +01003060 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003061 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003062 } else {
3063 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003064 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003065}
3066
3067static void
3068remove_popup_grab(struct shell_surface *shsurf)
3069{
3070 struct shell_seat *shseat = shsurf->popup.shseat;
3071
3072 wl_list_remove(&shsurf->popup.grab_link);
3073 wl_list_init(&shsurf->popup.grab_link);
3074 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003075 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003076 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003077 }
3078}
3079
3080static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003081shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003082{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003083 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003084 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003085
Jason Ekstranda7af7042013-10-12 22:38:11 -05003086 shsurf->surface->output = parent_view->output;
3087 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003088
Jason Ekstranda7af7042013-10-12 22:38:11 -05003089 weston_view_set_transform_parent(shsurf->view, parent_view);
3090 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3091 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003092
Kristian Høgsberge3148752013-05-06 23:19:49 -04003093 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003094 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003095 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003096 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003097 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003098 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003099}
3100
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003101static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003102 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003103 shell_surface_move,
3104 shell_surface_resize,
3105 shell_surface_set_toplevel,
3106 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003107 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003108 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003109 shell_surface_set_maximized,
3110 shell_surface_set_title,
3111 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003112};
3113
3114static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003115destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003116{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003117 struct shell_surface *child, *next;
3118
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003119 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3120
Giulio Camuffo5085a752013-03-25 21:42:45 +01003121 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3122 remove_popup_grab(shsurf);
3123 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003124
Alex Wubd3354b2012-04-17 17:20:49 +08003125 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003126 shell_surface_is_top_fullscreen(shsurf))
3127 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003128
Jason Ekstranda7af7042013-10-12 22:38:11 -05003129 if (shsurf->fullscreen.black_view)
3130 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003131
Alex Wubd3354b2012-04-17 17:20:49 +08003132 /* As destroy_resource() use wl_list_for_each_safe(),
3133 * we can always remove the listener.
3134 */
3135 wl_list_remove(&shsurf->surface_destroy_listener.link);
3136 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003137 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003138
Jason Ekstranda7af7042013-10-12 22:38:11 -05003139 weston_view_destroy(shsurf->view);
3140
Philip Withnall648a4dd2013-11-25 18:01:44 +00003141 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003142 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3143 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003144
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003145 wl_list_remove(&shsurf->link);
3146 free(shsurf);
3147}
3148
3149static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003150shell_destroy_shell_surface(struct wl_resource *resource)
3151{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003152 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003153
Bryan Caina46b9462014-04-04 17:41:24 -05003154 if (!wl_list_empty(&shsurf->popup.grab_link))
3155 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003156 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003157}
3158
3159static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003160shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003161{
3162 struct shell_surface *shsurf = container_of(listener,
3163 struct shell_surface,
3164 surface_destroy_listener);
3165
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003166 if (shsurf->resource)
3167 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003168
3169 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003170}
3171
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003172static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003173fade_out_done(struct weston_view_animation *animation, void *data)
3174{
3175 struct shell_surface *shsurf = data;
3176
3177 weston_surface_destroy(shsurf->surface);
3178}
3179
3180static void
3181handle_resource_destroy(struct wl_listener *listener, void *data)
3182{
3183 struct shell_surface *shsurf =
3184 container_of(listener, struct shell_surface,
3185 resource_destroy_listener);
3186
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003187 if (!weston_surface_is_mapped(shsurf->surface))
3188 return;
3189
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003190 shsurf->surface->ref_count++;
3191
3192 pixman_region32_fini(&shsurf->surface->pending.input);
3193 pixman_region32_init(&shsurf->surface->pending.input);
3194 pixman_region32_fini(&shsurf->surface->input);
3195 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003196 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3197 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003198}
3199
3200static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003201shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003202
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003203struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003204get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003205{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003206 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003207 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003208 else
3209 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003210}
3211
Rafael Antognollie2a34552013-12-03 15:35:45 -02003212static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003213create_common_surface(struct shell_client *owner, void *shell,
3214 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003215 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003216{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003217 struct shell_surface *shsurf;
3218
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003219 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003220 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003221 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003222 }
3223
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003224 shsurf = calloc(1, sizeof *shsurf);
3225 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003226 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003227 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003228 }
3229
Jason Ekstranda7af7042013-10-12 22:38:11 -05003230 shsurf->view = weston_view_create(surface);
3231 if (!shsurf->view) {
3232 weston_log("no memory to allocate shell surface\n");
3233 free(shsurf);
3234 return NULL;
3235 }
3236
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003237 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003238 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003239
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003240 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3241 wl_resource_add_destroy_listener(surface->resource,
3242 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003243 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003244
Tiago Vignattibc052c92012-04-19 16:18:18 +03003245 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003246 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003247 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003248 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003249 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003250 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003251 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3252 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003253 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003254 wl_list_init(&shsurf->fullscreen.transform.link);
3255
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003256 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003257 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003258 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003259 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003260
3261 /* init link so its safe to always remove it in destroy_shell_surface */
3262 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003263 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003264
Pekka Paalanen460099f2012-01-20 16:48:25 +02003265 /* empty when not in use */
3266 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003267 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003268
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003269 wl_list_init(&shsurf->workspace_transform.link);
3270
Philip Withnall648a4dd2013-11-25 18:01:44 +00003271 wl_list_init(&shsurf->children_link);
3272 wl_list_init(&shsurf->children_list);
3273 shsurf->parent = NULL;
3274
Pekka Paalanen98262232011-12-01 10:42:22 +02003275 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003276
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003277 shsurf->client = client;
3278
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003279 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003280}
3281
Rafael Antognollie2a34552013-12-03 15:35:45 -02003282static struct shell_surface *
3283create_shell_surface(void *shell, struct weston_surface *surface,
3284 const struct weston_shell_client *client)
3285{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003286 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003287}
3288
Jason Ekstranda7af7042013-10-12 22:38:11 -05003289static struct weston_view *
3290get_primary_view(void *shell, struct shell_surface *shsurf)
3291{
3292 return shsurf->view;
3293}
3294
Tiago Vignattibc052c92012-04-19 16:18:18 +03003295static void
3296shell_get_shell_surface(struct wl_client *client,
3297 struct wl_resource *resource,
3298 uint32_t id,
3299 struct wl_resource *surface_resource)
3300{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003301 struct weston_surface *surface =
3302 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003303 struct shell_client *sc = wl_resource_get_user_data(resource);
3304 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003305 struct shell_surface *shsurf;
3306
3307 if (get_shell_surface(surface)) {
3308 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003309 WL_DISPLAY_ERROR_INVALID_OBJECT,
3310 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003311 return;
3312 }
3313
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003314 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003315 if (!shsurf) {
3316 wl_resource_post_error(surface_resource,
3317 WL_DISPLAY_ERROR_INVALID_OBJECT,
3318 "surface->configure already set");
3319 return;
3320 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003321
Jason Ekstranda85118c2013-06-27 20:17:02 -05003322 shsurf->resource =
3323 wl_resource_create(client,
3324 &wl_shell_surface_interface, 1, id);
3325 wl_resource_set_implementation(shsurf->resource,
3326 &shell_surface_implementation,
3327 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003328}
3329
Rafael Antognollie2a34552013-12-03 15:35:45 -02003330static bool
3331shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3332{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003333 /* A shell surface without a resource is created from xwayland
3334 * and is considered a wl_shell surface for now. */
3335
3336 return shsurf->resource == NULL ||
3337 wl_resource_instance_of(shsurf->resource,
3338 &wl_shell_surface_interface,
3339 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003340}
3341
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003342static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003343 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003344};
3345
Rafael Antognollie2a34552013-12-03 15:35:45 -02003346/****************************
3347 * xdg-shell implementation */
3348
3349static void
3350xdg_surface_destroy(struct wl_client *client,
3351 struct wl_resource *resource)
3352{
3353 wl_resource_destroy(resource);
3354}
3355
3356static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003357xdg_surface_set_transient_for(struct wl_client *client,
3358 struct wl_resource *resource,
3359 struct wl_resource *parent_resource)
3360{
3361 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3362 struct weston_surface *parent;
3363
3364 if (parent_resource)
3365 parent = wl_resource_get_user_data(parent_resource);
3366 else
3367 parent = NULL;
3368
3369 shell_surface_set_parent(shsurf, parent);
3370}
3371
3372static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003373xdg_surface_set_margin(struct wl_client *client,
3374 struct wl_resource *resource,
3375 int32_t left,
3376 int32_t right,
3377 int32_t top,
3378 int32_t bottom)
3379{
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003380 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3381
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07003382 set_margin(shsurf, left, right, top, bottom);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003383}
3384
3385static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003386xdg_surface_set_app_id(struct wl_client *client,
3387 struct wl_resource *resource,
3388 const char *app_id)
3389{
3390 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3391
3392 free(shsurf->class);
3393 shsurf->class = strdup(app_id);
3394}
3395
3396static void
3397xdg_surface_set_title(struct wl_client *client,
3398 struct wl_resource *resource, const char *title)
3399{
3400 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3401
3402 set_title(shsurf, title);
3403}
3404
3405static void
3406xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3407 struct wl_resource *seat_resource, uint32_t serial)
3408{
3409 common_surface_move(resource, seat_resource, serial);
3410}
3411
3412static void
3413xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3414 struct wl_resource *seat_resource, uint32_t serial,
3415 uint32_t edges)
3416{
3417 common_surface_resize(resource, seat_resource, serial, edges);
3418}
3419
3420static void
3421xdg_surface_set_output(struct wl_client *client,
3422 struct wl_resource *resource,
3423 struct wl_resource *output_resource)
3424{
3425 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3426 struct weston_output *output;
3427
3428 if (output_resource)
3429 output = wl_resource_get_user_data(output_resource);
3430 else
3431 output = NULL;
3432
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003433 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003434}
3435
3436static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003437xdg_surface_change_state(struct shell_surface *shsurf,
3438 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003439{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003440 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003441 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003442
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003443 switch (state) {
3444 case XDG_SURFACE_STATE_MAXIMIZED:
3445 shsurf->requested_state.maximized = value;
3446 if (value)
3447 set_maximized(shsurf, NULL);
3448 break;
3449 case XDG_SURFACE_STATE_FULLSCREEN:
3450 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003451
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003452 if (value)
3453 set_fullscreen(shsurf,
3454 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3455 0, shsurf->recommended_output);
3456 break;
3457 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003458}
3459
3460static void
3461xdg_surface_request_change_state(struct wl_client *client,
3462 struct wl_resource *resource,
3463 uint32_t state,
3464 uint32_t value,
3465 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003466{
3467 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003468
3469 /* The client can't know what the current state is, so we need
3470 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003471
3472 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3473 return;
3474
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003475 switch (state) {
3476 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003477 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003478 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003479 default:
3480 /* send error? ignore? send change state with value 0? */
3481 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003482 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003483
3484 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003485}
3486
3487static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003488xdg_surface_ack_change_state(struct wl_client *client,
3489 struct wl_resource *resource,
3490 uint32_t state,
3491 uint32_t value,
3492 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003493{
3494 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3495
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003496 if (shsurf->state_requested) {
3497 shsurf->next_state = shsurf->requested_state;
3498 shsurf->state_changed = true;
3499 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003500 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003501}
3502
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003503static void
3504xdg_surface_set_minimized(struct wl_client *client,
3505 struct wl_resource *resource)
3506{
3507 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3508
3509 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3510 return;
3511
3512 /* apply compositor's own minimization logic (hide) */
3513 set_minimized(shsurf->surface, 1);
3514}
3515
Rafael Antognollie2a34552013-12-03 15:35:45 -02003516static const struct xdg_surface_interface xdg_surface_implementation = {
3517 xdg_surface_destroy,
3518 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003519 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003520 xdg_surface_set_title,
3521 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003522 xdg_surface_move,
3523 xdg_surface_resize,
3524 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003525 xdg_surface_request_change_state,
3526 xdg_surface_ack_change_state,
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003527 xdg_surface_set_minimized
Rafael Antognollie2a34552013-12-03 15:35:45 -02003528};
3529
3530static void
3531xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003532 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003533{
3534 struct shell_surface *shsurf = get_shell_surface(surface);
3535
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003536 assert(shsurf);
3537
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07003538 if (shsurf->resource)
3539 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003540}
3541
3542static const struct weston_shell_client xdg_client = {
3543 xdg_send_configure
3544};
3545
3546static void
3547xdg_use_unstable_version(struct wl_client *client,
3548 struct wl_resource *resource,
3549 int32_t version)
3550{
3551 if (version > 1) {
3552 wl_resource_post_error(resource,
3553 1,
3554 "xdg-shell:: version not implemented yet.");
3555 return;
3556 }
3557}
3558
3559static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003560create_xdg_surface(struct shell_client *owner, void *shell,
3561 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003562 const struct weston_shell_client *client)
3563{
3564 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003565
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003566 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003567 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003568
3569 return shsurf;
3570}
3571
3572static void
3573xdg_get_xdg_surface(struct wl_client *client,
3574 struct wl_resource *resource,
3575 uint32_t id,
3576 struct wl_resource *surface_resource)
3577{
3578 struct weston_surface *surface =
3579 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003580 struct shell_client *sc = wl_resource_get_user_data(resource);
3581 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003582 struct shell_surface *shsurf;
3583
3584 if (get_shell_surface(surface)) {
3585 wl_resource_post_error(surface_resource,
3586 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003587 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003588 return;
3589 }
3590
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003591 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003592 if (!shsurf) {
3593 wl_resource_post_error(surface_resource,
3594 WL_DISPLAY_ERROR_INVALID_OBJECT,
3595 "surface->configure already set");
3596 return;
3597 }
3598
3599 shsurf->resource =
3600 wl_resource_create(client,
3601 &xdg_surface_interface, 1, id);
3602 wl_resource_set_implementation(shsurf->resource,
3603 &xdg_surface_implementation,
3604 shsurf, shell_destroy_shell_surface);
3605}
3606
3607static bool
3608shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3609{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003610 return shsurf->resource &&
3611 wl_resource_instance_of(shsurf->resource,
3612 &xdg_surface_interface,
3613 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003614}
3615
3616/* xdg-popup implementation */
3617
3618static void
3619xdg_popup_destroy(struct wl_client *client,
3620 struct wl_resource *resource)
3621{
3622 wl_resource_destroy(resource);
3623}
3624
Rafael Antognollie2a34552013-12-03 15:35:45 -02003625static const struct xdg_popup_interface xdg_popup_implementation = {
3626 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003627};
3628
3629static void
3630xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003631 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003632{
3633}
3634
3635static const struct weston_shell_client xdg_popup_client = {
3636 xdg_popup_send_configure
3637};
3638
3639static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003640create_xdg_popup(struct shell_client *owner, void *shell,
3641 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003642 const struct weston_shell_client *client,
3643 struct weston_surface *parent,
3644 struct shell_seat *seat,
3645 uint32_t serial,
3646 int32_t x, int32_t y)
3647{
3648 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003649
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003650 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003651 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003652 shsurf->popup.shseat = seat;
3653 shsurf->popup.serial = serial;
3654 shsurf->popup.x = x;
3655 shsurf->popup.y = y;
3656 shell_surface_set_parent(shsurf, parent);
3657
3658 return shsurf;
3659}
3660
3661static void
3662xdg_get_xdg_popup(struct wl_client *client,
3663 struct wl_resource *resource,
3664 uint32_t id,
3665 struct wl_resource *surface_resource,
3666 struct wl_resource *parent_resource,
3667 struct wl_resource *seat_resource,
3668 uint32_t serial,
3669 int32_t x, int32_t y, uint32_t flags)
3670{
3671 struct weston_surface *surface =
3672 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003673 struct shell_client *sc = wl_resource_get_user_data(resource);
3674 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003675 struct shell_surface *shsurf;
3676 struct weston_surface *parent;
3677 struct shell_seat *seat;
3678
3679 if (get_shell_surface(surface)) {
3680 wl_resource_post_error(surface_resource,
3681 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003682 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003683 return;
3684 }
3685
3686 if (!parent_resource) {
3687 wl_resource_post_error(surface_resource,
3688 WL_DISPLAY_ERROR_INVALID_OBJECT,
3689 "xdg_shell::get_xdg_popup requires a parent shell surface");
3690 }
3691
3692 parent = wl_resource_get_user_data(parent_resource);
3693 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3694
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003695 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003696 parent, seat, serial, x, y);
3697 if (!shsurf) {
3698 wl_resource_post_error(surface_resource,
3699 WL_DISPLAY_ERROR_INVALID_OBJECT,
3700 "surface->configure already set");
3701 return;
3702 }
3703
3704 shsurf->resource =
3705 wl_resource_create(client,
3706 &xdg_popup_interface, 1, id);
3707 wl_resource_set_implementation(shsurf->resource,
3708 &xdg_popup_implementation,
3709 shsurf, shell_destroy_shell_surface);
3710}
3711
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003712static void
3713xdg_pong(struct wl_client *client,
3714 struct wl_resource *resource, uint32_t serial)
3715{
3716 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003717
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003718 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003719}
3720
Rafael Antognollie2a34552013-12-03 15:35:45 -02003721static bool
3722shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3723{
3724 return wl_resource_instance_of(shsurf->resource,
3725 &xdg_popup_interface,
3726 &xdg_popup_implementation);
3727}
3728
3729static const struct xdg_shell_interface xdg_implementation = {
3730 xdg_use_unstable_version,
3731 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003732 xdg_get_xdg_popup,
3733 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003734};
3735
3736static int
3737xdg_shell_unversioned_dispatch(const void *implementation,
3738 void *_target, uint32_t opcode,
3739 const struct wl_message *message,
3740 union wl_argument *args)
3741{
3742 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003743 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003744
3745 if (opcode != 0) {
3746 wl_resource_post_error(resource,
3747 WL_DISPLAY_ERROR_INVALID_OBJECT,
3748 "must call use_unstable_version first");
3749 return 0;
3750 }
3751
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003752#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003753
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003754 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3755 "shell implementation doesn't match protocol version");
3756
Rafael Antognollie2a34552013-12-03 15:35:45 -02003757 if (args[0].i != XDG_SERVER_VERSION) {
3758 wl_resource_post_error(resource,
3759 WL_DISPLAY_ERROR_INVALID_OBJECT,
3760 "incompatible version, server is %d "
3761 "client wants %d",
3762 XDG_SERVER_VERSION, args[0].i);
3763 return 0;
3764 }
3765
3766 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003767 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003768
3769 return 1;
3770}
3771
3772/* end of xdg-shell implementation */
3773/***********************************/
3774
Kristian Høgsberg07937562011-04-12 17:25:42 -04003775static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003776shell_fade(struct desktop_shell *shell, enum fade_type type);
3777
3778static int
3779screensaver_timeout(void *data)
3780{
3781 struct desktop_shell *shell = data;
3782
3783 shell_fade(shell, FADE_OUT);
3784
3785 return 1;
3786}
3787
3788static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003789handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003790{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003791 struct desktop_shell *shell =
3792 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003793
Pekka Paalanen18027e52011-12-02 16:31:49 +02003794 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003795
3796 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003797 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003798}
3799
3800static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003801launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003802{
3803 if (shell->screensaver.binding)
3804 return;
3805
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003806 if (!shell->screensaver.path) {
3807 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003808 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003809 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003810
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003811 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003812 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003813 return;
3814 }
3815
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003816 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003817 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003818 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003819 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003820}
3821
3822static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003823terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003824{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003825 if (shell->screensaver.process.pid == 0)
3826 return;
3827
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003828 /* Disarm the screensaver timer, otherwise it may fire when the
3829 * compositor is not in the idle state. In that case, the screen will
3830 * be locked, but the wake_signal won't fire on user input, making the
3831 * system unresponsive. */
3832 wl_event_source_timer_update(shell->screensaver.timer, 0);
3833
Pekka Paalanen18027e52011-12-02 16:31:49 +02003834 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003835}
3836
3837static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003838configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003839{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003840 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003841
Jason Ekstranda7af7042013-10-12 22:38:11 -05003842 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3843 if (v->output == ev->output && v != ev) {
3844 weston_view_unmap(v);
3845 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003846 }
3847 }
3848
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003849 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003850
Jason Ekstranda7af7042013-10-12 22:38:11 -05003851 if (wl_list_empty(&ev->layer_link)) {
3852 wl_list_insert(&layer->view_list, &ev->layer_link);
3853 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003854 }
3855}
3856
3857static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003858background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003859{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003860 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003861 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003862
Jason Ekstranda7af7042013-10-12 22:38:11 -05003863 view = container_of(es->views.next, struct weston_view, surface_link);
3864
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003865 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003866}
3867
3868static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003869desktop_shell_set_background(struct wl_client *client,
3870 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003871 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003872 struct wl_resource *surface_resource)
3873{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003874 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003875 struct weston_surface *surface =
3876 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003877 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003878
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003879 if (surface->configure) {
3880 wl_resource_post_error(surface_resource,
3881 WL_DISPLAY_ERROR_INVALID_OBJECT,
3882 "surface role already assigned");
3883 return;
3884 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003885
Jason Ekstranda7af7042013-10-12 22:38:11 -05003886 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3887 weston_view_destroy(view);
3888 view = weston_view_create(surface);
3889
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003890 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003891 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003892 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003893 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003894 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003895 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003896 surface->output->width,
3897 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003898}
3899
3900static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003901panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003902{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003903 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003904 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003905
Jason Ekstranda7af7042013-10-12 22:38:11 -05003906 view = container_of(es->views.next, struct weston_view, surface_link);
3907
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003908 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003909}
3910
3911static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003912desktop_shell_set_panel(struct wl_client *client,
3913 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003914 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003915 struct wl_resource *surface_resource)
3916{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003917 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003918 struct weston_surface *surface =
3919 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003920 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003921
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003922 if (surface->configure) {
3923 wl_resource_post_error(surface_resource,
3924 WL_DISPLAY_ERROR_INVALID_OBJECT,
3925 "surface role already assigned");
3926 return;
3927 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003928
Jason Ekstranda7af7042013-10-12 22:38:11 -05003929 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3930 weston_view_destroy(view);
3931 view = weston_view_create(surface);
3932
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003933 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003934 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003935 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003936 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003937 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003938 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003939 surface->output->width,
3940 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003941}
3942
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003943static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003944lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003945{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003946 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003947 struct weston_view *view;
3948
3949 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003950
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003951 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003952 return;
3953
Jason Ekstranda7af7042013-10-12 22:38:11 -05003954 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003955
3956 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003957 wl_list_insert(&shell->lock_layer.view_list,
3958 &view->layer_link);
3959 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003960 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003961 }
3962}
3963
3964static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003965handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003966{
Tiago Vignattibe143262012-04-16 17:31:41 +03003967 struct desktop_shell *shell =
3968 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003969
Martin Minarik6d118362012-06-07 18:01:59 +02003970 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003971 shell->lock_surface = NULL;
3972}
3973
3974static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003975desktop_shell_set_lock_surface(struct wl_client *client,
3976 struct wl_resource *resource,
3977 struct wl_resource *surface_resource)
3978{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003979 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003980 struct weston_surface *surface =
3981 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003982
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003983 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003984
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003985 if (!shell->locked)
3986 return;
3987
Pekka Paalanen98262232011-12-01 10:42:22 +02003988 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003989
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003990 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003991 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003992 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003993
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003994 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003995 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003996 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003997}
3998
3999static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004000resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004001{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004002 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004003
Pekka Paalanen77346a62011-11-30 16:26:35 +02004004 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004005
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004006 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004007 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004008 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004009 &shell->input_panel_layer.link);
4010 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004011 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004012 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004013 wl_list_insert(&shell->compositor->cursor_layer.link,
4014 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004015 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004016 wl_list_insert(&shell->fullscreen_layer.link,
4017 &shell->panel_layer.link);
4018 wl_list_insert(&shell->panel_layer.link,
4019 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004020
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004021 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004022
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004023 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004024 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004025 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004026}
4027
4028static void
4029desktop_shell_unlock(struct wl_client *client,
4030 struct wl_resource *resource)
4031{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004032 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004033
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004034 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004035
4036 if (shell->locked)
4037 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004038}
4039
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004040static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004041desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004042 struct wl_resource *resource,
4043 struct wl_resource *surface_resource)
4044{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004045 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004046
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004047 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004048 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004049}
4050
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004051static void
4052desktop_shell_desktop_ready(struct wl_client *client,
4053 struct wl_resource *resource)
4054{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004055 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004056
4057 shell_fade_startup(shell);
4058}
4059
Kristian Høgsberg75840622011-09-06 13:48:16 -04004060static const struct desktop_shell_interface desktop_shell_implementation = {
4061 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004062 desktop_shell_set_panel,
4063 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004064 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004065 desktop_shell_set_grab_surface,
4066 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004067};
4068
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004069static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004070get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004071{
4072 struct shell_surface *shsurf;
4073
4074 shsurf = get_shell_surface(surface);
4075 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004076 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004077 return shsurf->type;
4078}
4079
Kristian Høgsberg75840622011-09-06 13:48:16 -04004080static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004081move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004082{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004083 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004084 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004085 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004086
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004087 if (seat->pointer->focus == NULL)
4088 return;
4089
4090 focus = seat->pointer->focus->surface;
4091
Pekka Paalanen01388e22013-04-25 13:57:44 +03004092 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004093 if (surface == NULL)
4094 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004095
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004096 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004097 if (shsurf == NULL || shsurf->state.fullscreen ||
4098 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004099 return;
4100
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004101 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004102}
4103
4104static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004105maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4106{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004107 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004108 struct weston_surface *surface;
4109 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004110 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004111
4112 surface = weston_surface_get_main_surface(focus);
4113 if (surface == NULL)
4114 return;
4115
4116 shsurf = get_shell_surface(surface);
4117 if (shsurf == NULL)
4118 return;
4119
4120 if (!shell_surface_is_xdg_surface(shsurf))
4121 return;
4122
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004123 serial = wl_display_next_serial(seat->compositor->wl_display);
4124 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4125 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004126}
4127
4128static void
4129fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4130{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004131 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004132 struct weston_surface *surface;
4133 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004134 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004135
4136 surface = weston_surface_get_main_surface(focus);
4137 if (surface == NULL)
4138 return;
4139
4140 shsurf = get_shell_surface(surface);
4141 if (shsurf == NULL)
4142 return;
4143
4144 if (!shell_surface_is_xdg_surface(shsurf))
4145 return;
4146
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004147 serial = wl_display_next_serial(seat->compositor->wl_display);
4148 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4149 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004150}
4151
4152static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004153touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4154{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004155 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004156 struct weston_surface *surface;
4157 struct shell_surface *shsurf;
4158
4159 surface = weston_surface_get_main_surface(focus);
4160 if (surface == NULL)
4161 return;
4162
4163 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004164 if (shsurf == NULL || shsurf->state.fullscreen ||
4165 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004166 return;
4167
4168 surface_touch_move(shsurf, (struct weston_seat *) seat);
4169}
4170
4171static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004172resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004173{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004174 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004175 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004176 uint32_t edges = 0;
4177 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004178 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004179
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004180 if (seat->pointer->focus == NULL)
4181 return;
4182
4183 focus = seat->pointer->focus->surface;
4184
Pekka Paalanen01388e22013-04-25 13:57:44 +03004185 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004186 if (surface == NULL)
4187 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004188
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004189 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004190 if (shsurf == NULL || shsurf->state.fullscreen ||
4191 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004192 return;
4193
Jason Ekstranda7af7042013-10-12 22:38:11 -05004194 weston_view_from_global(shsurf->view,
4195 wl_fixed_to_int(seat->pointer->grab_x),
4196 wl_fixed_to_int(seat->pointer->grab_y),
4197 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004198
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004199 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004200 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004201 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004202 edges |= 0;
4203 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004204 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004205
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004206 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004207 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004208 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004209 edges |= 0;
4210 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004211 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004212
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004213 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004214}
4215
4216static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004217surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004218 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004219{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004220 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004221 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004222 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004223 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004224
Pekka Paalanen01388e22013-04-25 13:57:44 +03004225 /* XXX: broken for windows containing sub-surfaces */
4226 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004227 if (surface == NULL)
4228 return;
4229
4230 shsurf = get_shell_surface(surface);
4231 if (!shsurf)
4232 return;
4233
Jason Ekstranda7af7042013-10-12 22:38:11 -05004234 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004235
Jason Ekstranda7af7042013-10-12 22:38:11 -05004236 if (shsurf->view->alpha > 1.0)
4237 shsurf->view->alpha = 1.0;
4238 if (shsurf->view->alpha < step)
4239 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004240
Jason Ekstranda7af7042013-10-12 22:38:11 -05004241 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004242 weston_surface_damage(surface);
4243}
4244
4245static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004246do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004247 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004248{
Daniel Stone37816df2012-05-16 18:45:18 +01004249 struct weston_seat *ws = (struct weston_seat *) seat;
4250 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004251 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004252 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004253
4254 wl_list_for_each(output, &compositor->output_list, link) {
4255 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004256 wl_fixed_to_double(seat->pointer->x),
4257 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004258 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004259 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004260 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004261 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004262 increment = -output->zoom.increment;
4263 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004264 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004265 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004266 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004267 else
4268 increment = 0;
4269
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004270 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004271
Scott Moreaue6603982012-06-11 13:07:51 -06004272 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004273 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004274 else if (output->zoom.level > output->zoom.max_level)
4275 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004276 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004277 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004278 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004279
Scott Moreaue6603982012-06-11 13:07:51 -06004280 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004281
Jason Ekstranda7af7042013-10-12 22:38:11 -05004282 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004283 }
4284 }
4285}
4286
Scott Moreauccbf29d2012-02-22 14:21:41 -07004287static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004288zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004289 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004290{
4291 do_zoom(seat, time, 0, axis, value);
4292}
4293
4294static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004295zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004296 void *data)
4297{
4298 do_zoom(seat, time, key, 0, 0);
4299}
4300
4301static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004302terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004303 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004304{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004305 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004306
Daniel Stone325fc2d2012-05-30 16:31:58 +01004307 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004308}
4309
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004310static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004311rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4312 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004313{
4314 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004315 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004316 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004317 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004318 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004319
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004320 weston_pointer_move(pointer, x, y);
4321
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004322 if (!shsurf)
4323 return;
4324
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004325 cx = 0.5f * shsurf->surface->width;
4326 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004327
Daniel Stone37816df2012-05-16 18:45:18 +01004328 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4329 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004330 r = sqrtf(dx * dx + dy * dy);
4331
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004332 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004333 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004334
4335 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004336 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004337 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004338
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004339 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004340 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004341
4342 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004343 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004344 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004345 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004346 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004347
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004348 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004349 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004350 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004351 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004352 wl_list_init(&shsurf->rotation.transform.link);
4353 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004354 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004355 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004356
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004357 /* We need to adjust the position of the surface
4358 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004359 cposx = shsurf->view->geometry.x + cx;
4360 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004361 dposx = rotate->center.x - cposx;
4362 dposy = rotate->center.y - cposy;
4363 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004364 weston_view_set_position(shsurf->view,
4365 shsurf->view->geometry.x + dposx,
4366 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004367 }
4368
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004369 /* Repaint implies weston_surface_update_transform(), which
4370 * lazily applies the damage due to rotation update.
4371 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004372 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004373}
4374
4375static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004376rotate_grab_button(struct weston_pointer_grab *grab,
4377 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004378{
4379 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004380 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004381 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004382 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004383 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004384
Daniel Stone4dbadb12012-05-30 16:31:51 +01004385 if (pointer->button_count == 0 &&
4386 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004387 if (shsurf)
4388 weston_matrix_multiply(&shsurf->rotation.rotation,
4389 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004390 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004391 free(rotate);
4392 }
4393}
4394
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004395static void
4396rotate_grab_cancel(struct weston_pointer_grab *grab)
4397{
4398 struct rotate_grab *rotate =
4399 container_of(grab, struct rotate_grab, base.grab);
4400
4401 shell_grab_end(&rotate->base);
4402 free(rotate);
4403}
4404
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004405static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004406 noop_grab_focus,
4407 rotate_grab_motion,
4408 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004409 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004410};
4411
4412static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004413surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004414{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004415 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004416 float dx, dy;
4417 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004418
Pekka Paalanen460099f2012-01-20 16:48:25 +02004419 rotate = malloc(sizeof *rotate);
4420 if (!rotate)
4421 return;
4422
Jason Ekstranda7af7042013-10-12 22:38:11 -05004423 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004424 surface->surface->width * 0.5f,
4425 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004426 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004427
Daniel Stone37816df2012-05-16 18:45:18 +01004428 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4429 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004430 r = sqrtf(dx * dx + dy * dy);
4431 if (r > 20.0f) {
4432 struct weston_matrix inverse;
4433
4434 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004435 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004436 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004437
4438 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004439 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004440 } else {
4441 weston_matrix_init(&surface->rotation.rotation);
4442 weston_matrix_init(&rotate->rotation);
4443 }
4444
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004445 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4446 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004447}
4448
4449static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004450rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004451 void *data)
4452{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004453 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004454 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004455 struct shell_surface *surface;
4456
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004457 if (seat->pointer->focus == NULL)
4458 return;
4459
4460 focus = seat->pointer->focus->surface;
4461
Pekka Paalanen01388e22013-04-25 13:57:44 +03004462 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004463 if (base_surface == NULL)
4464 return;
4465
4466 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004467 if (surface == NULL || surface->state.fullscreen ||
4468 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004469 return;
4470
4471 surface_rotate(surface, seat);
4472}
4473
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004474/* Move all fullscreen layers down to the current workspace and hide their
4475 * black views. The surfaces' state is set to both fullscreen and lowered,
4476 * and this is reversed when such a surface is re-configured, see
4477 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4478 *
4479 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004480 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004481void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004482lower_fullscreen_layer(struct desktop_shell *shell)
4483{
4484 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004485 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004486
4487 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004488 wl_list_for_each_reverse_safe(view, prev,
4489 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004490 layer_link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004491 struct shell_surface *shsurf = get_shell_surface(view->surface);
4492
4493 if (!shsurf)
4494 continue;
4495
4496 /* We can have a non-fullscreen popup for a fullscreen surface
4497 * in the fullscreen layer. */
4498 if (shsurf->state.fullscreen) {
4499 /* Hide the black view */
4500 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
4501 wl_list_init(&shsurf->fullscreen.black_view->layer_link);
4502 }
4503
4504 /* Lower the view to the workspace layer */
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004505 wl_list_remove(&view->layer_link);
4506 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4507 weston_view_damage_below(view);
4508 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004509
4510 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004511 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004512}
4513
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004514void
Tiago Vignattibe143262012-04-16 17:31:41 +03004515activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004516 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004517{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004518 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004519 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004520 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004521 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004522 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004523
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004524 lower_fullscreen_layer(shell);
4525
Pekka Paalanen01388e22013-04-25 13:57:44 +03004526 main_surface = weston_surface_get_main_surface(es);
4527
Daniel Stone37816df2012-05-16 18:45:18 +01004528 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004529
Jonas Ådahl8538b222012-08-29 22:13:03 +02004530 state = ensure_focus_state(shell, seat);
4531 if (state == NULL)
4532 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004533
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004534 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004535 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004536
Rafael Antognolli03b16592013-12-03 15:35:42 -02004537 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004538 assert(shsurf);
4539
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004540 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004541 shell_configure_fullscreen(shsurf);
4542 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004543 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004544
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004545 /* Update the surface’s layer. This brings it to the top of the stacking
4546 * order as appropriate. */
4547 shell_surface_update_layer(shsurf);
4548
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004549 if (shell->focus_animation_type != ANIMATION_NONE) {
4550 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004551 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004552 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004553}
4554
Alex Wu21858432012-04-01 20:13:08 +08004555/* no-op func for checking black surface */
4556static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004557black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004558{
4559}
4560
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004561static bool
Alex Wu21858432012-04-01 20:13:08 +08004562is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4563{
4564 if (es->configure == black_surface_configure) {
4565 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004566 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004567 return true;
4568 }
4569 return false;
4570}
4571
Kristian Høgsberg75840622011-09-06 13:48:16 -04004572static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004573activate_binding(struct weston_seat *seat,
4574 struct desktop_shell *shell,
4575 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004576{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004577 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004578
Alex Wu9c35e6b2012-03-05 14:13:13 +08004579 if (!focus)
4580 return;
4581
Pekka Paalanen01388e22013-04-25 13:57:44 +03004582 if (is_black_surface(focus, &main_surface))
4583 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004584
Pekka Paalanen01388e22013-04-25 13:57:44 +03004585 main_surface = weston_surface_get_main_surface(focus);
4586 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004587 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004588
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004589 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004590}
4591
4592static void
4593click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4594 void *data)
4595{
4596 if (seat->pointer->grab != &seat->pointer->default_grab)
4597 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004598 if (seat->pointer->focus == NULL)
4599 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004600
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004601 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004602}
4603
4604static void
4605touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4606{
4607 if (seat->touch->grab != &seat->touch->default_grab)
4608 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004609 if (seat->touch->focus == NULL)
4610 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004611
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004612 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004613}
4614
4615static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004616unfocus_all_seats(struct desktop_shell *shell)
4617{
4618 struct weston_seat *seat, *next;
4619
4620 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4621 if (seat->keyboard == NULL)
4622 continue;
4623
4624 weston_keyboard_set_focus(seat->keyboard, NULL);
4625 }
4626}
4627
4628static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004629lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004630{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004631 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004632
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004633 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004634 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004635 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004636 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004637
4638 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004639
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004640 /* Hide all surfaces by removing the fullscreen, panel and
4641 * toplevel layers. This way nothing else can show or receive
4642 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004643
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004644 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004645 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004646 if (shell->showing_input_panels)
4647 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004648 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004649 wl_list_insert(&shell->compositor->cursor_layer.link,
4650 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004651
Pekka Paalanen77346a62011-11-30 16:26:35 +02004652 launch_screensaver(shell);
4653
Neil Robertsb4a91702014-04-09 16:33:32 +01004654 /* Remove the keyboard focus on all seats. This will be
4655 * restored to the workspace's saved state via
4656 * restore_focus_state when the compositor is unlocked */
4657 unfocus_all_seats(shell);
4658
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004659 /* TODO: disable bindings that should not work while locked. */
4660
4661 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004662}
4663
4664static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004665unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004666{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004667 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004668 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004669 return;
4670 }
4671
4672 /* If desktop-shell client has gone away, unlock immediately. */
4673 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004674 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004675 return;
4676 }
4677
4678 if (shell->prepare_event_sent)
4679 return;
4680
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004681 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004682 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004683}
4684
4685static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004686shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004687{
4688 struct desktop_shell *shell = data;
4689
4690 shell->fade.animation = NULL;
4691
4692 switch (shell->fade.type) {
4693 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004694 weston_surface_destroy(shell->fade.view->surface);
4695 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004696 break;
4697 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004698 lock(shell);
4699 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004700 default:
4701 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004702 }
4703}
4704
Jason Ekstranda7af7042013-10-12 22:38:11 -05004705static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004706shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004707{
4708 struct weston_compositor *compositor = shell->compositor;
4709 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004710 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004711
4712 surface = weston_surface_create(compositor);
4713 if (!surface)
4714 return NULL;
4715
Jason Ekstranda7af7042013-10-12 22:38:11 -05004716 view = weston_view_create(surface);
4717 if (!view) {
4718 weston_surface_destroy(surface);
4719 return NULL;
4720 }
4721
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004722 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004723 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004724 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004725 wl_list_insert(&compositor->fade_layer.view_list,
4726 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004727 pixman_region32_init(&surface->input);
4728
Jason Ekstranda7af7042013-10-12 22:38:11 -05004729 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004730}
4731
4732static void
4733shell_fade(struct desktop_shell *shell, enum fade_type type)
4734{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004735 float tint;
4736
4737 switch (type) {
4738 case FADE_IN:
4739 tint = 0.0;
4740 break;
4741 case FADE_OUT:
4742 tint = 1.0;
4743 break;
4744 default:
4745 weston_log("shell: invalid fade type\n");
4746 return;
4747 }
4748
4749 shell->fade.type = type;
4750
Jason Ekstranda7af7042013-10-12 22:38:11 -05004751 if (shell->fade.view == NULL) {
4752 shell->fade.view = shell_fade_create_surface(shell);
4753 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004754 return;
4755
Jason Ekstranda7af7042013-10-12 22:38:11 -05004756 shell->fade.view->alpha = 1.0 - tint;
4757 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004758 }
4759
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004760 if (shell->fade.view->output == NULL) {
4761 /* If the black view gets a NULL output, we lost the
4762 * last output and we'll just cancel the fade. This
4763 * happens when you close the last window under the
4764 * X11 or Wayland backends. */
4765 shell->locked = false;
4766 weston_surface_destroy(shell->fade.view->surface);
4767 shell->fade.view = NULL;
4768 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004769 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004770 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004771 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004772 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004773 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004774 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004775 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004776}
4777
4778static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004779do_shell_fade_startup(void *data)
4780{
4781 struct desktop_shell *shell = data;
4782
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004783 if (shell->startup_animation_type == ANIMATION_FADE)
4784 shell_fade(shell, FADE_IN);
4785 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004786 weston_surface_destroy(shell->fade.view->surface);
4787 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004788 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004789}
4790
4791static void
4792shell_fade_startup(struct desktop_shell *shell)
4793{
4794 struct wl_event_loop *loop;
4795
4796 if (!shell->fade.startup_timer)
4797 return;
4798
4799 wl_event_source_remove(shell->fade.startup_timer);
4800 shell->fade.startup_timer = NULL;
4801
4802 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4803 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4804}
4805
4806static int
4807fade_startup_timeout(void *data)
4808{
4809 struct desktop_shell *shell = data;
4810
4811 shell_fade_startup(shell);
4812 return 0;
4813}
4814
4815static void
4816shell_fade_init(struct desktop_shell *shell)
4817{
4818 /* Make compositor output all black, and wait for the desktop-shell
4819 * client to signal it is ready, then fade in. The timer triggers a
4820 * fade-in, in case the desktop-shell client takes too long.
4821 */
4822
4823 struct wl_event_loop *loop;
4824
Jason Ekstranda7af7042013-10-12 22:38:11 -05004825 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004826 weston_log("%s: warning: fade surface already exists\n",
4827 __func__);
4828 return;
4829 }
4830
Jason Ekstranda7af7042013-10-12 22:38:11 -05004831 shell->fade.view = shell_fade_create_surface(shell);
4832 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004833 return;
4834
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835 weston_view_update_transform(shell->fade.view);
4836 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004837
4838 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4839 shell->fade.startup_timer =
4840 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4841 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4842}
4843
4844static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004845idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004846{
4847 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004848 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004849 struct weston_seat *seat;
4850
4851 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4852 if (seat->pointer)
4853 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004854
4855 shell_fade(shell, FADE_OUT);
4856 /* lock() is called from shell_fade_done() */
4857}
4858
4859static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004860wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004861{
4862 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004863 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004864
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004865 unlock(shell);
4866}
4867
4868static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004869center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004870{
Giulio Camuffob8366642013-04-25 13:57:46 +03004871 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004872 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004873
Jason Ekstranda7af7042013-10-12 22:38:11 -05004874 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004875
4876 x = output->x + (output->width - width) / 2 - surf_x / 2;
4877 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004878
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004879 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004880}
4881
4882static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004883weston_view_set_initial_position(struct weston_view *view,
4884 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004885{
4886 struct weston_compositor *compositor = shell->compositor;
4887 int ix = 0, iy = 0;
4888 int range_x, range_y;
4889 int dx, dy, x, y, panel_height;
4890 struct weston_output *output, *target_output = NULL;
4891 struct weston_seat *seat;
4892
4893 /* As a heuristic place the new window on the same output as the
4894 * pointer. Falling back to the output containing 0, 0.
4895 *
4896 * TODO: Do something clever for touch too?
4897 */
4898 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004899 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004900 ix = wl_fixed_to_int(seat->pointer->x);
4901 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004902 break;
4903 }
4904 }
4905
4906 wl_list_for_each(output, &compositor->output_list, link) {
4907 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4908 target_output = output;
4909 break;
4910 }
4911 }
4912
4913 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004914 weston_view_set_position(view, 10 + random() % 400,
4915 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004916 return;
4917 }
4918
4919 /* Valid range within output where the surface will still be onscreen.
4920 * If this is negative it means that the surface is bigger than
4921 * output.
4922 */
4923 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004924 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004925 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004926 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004927
Rob Bradford4cb88c72012-08-13 15:18:44 +01004928 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004929 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004930 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004931 dx = 0;
4932
4933 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004934 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004935 else
4936 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004937
4938 x = target_output->x + dx;
4939 y = target_output->y + dy;
4940
Jason Ekstranda7af7042013-10-12 22:38:11 -05004941 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004942}
4943
4944static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004945map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004946 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004947{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004948 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004949 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004950 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004951 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004952
Pekka Paalanen77346a62011-11-30 16:26:35 +02004953 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004954 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004955 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004956 if (shsurf->state.fullscreen) {
4957 center_on_output(shsurf->view, shsurf->fullscreen_output);
4958 shell_map_fullscreen(shsurf);
4959 } else if (shsurf->state.maximized) {
4960 /* use surface configure to set the geometry */
4961 panel_height = get_output_panel_height(shell, shsurf->output);
4962 surface_subsurfaces_boundingbox(shsurf->surface,
4963 &surf_x, &surf_y, NULL, NULL);
4964 weston_view_set_position(shsurf->view,
4965 shsurf->output->x - surf_x,
4966 shsurf->output->y +
4967 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004968 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004969 weston_view_set_initial_position(shsurf->view, shell);
4970 }
Juan Zhao96879df2012-02-07 08:45:41 +08004971 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004972 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004973 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004974 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004975 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004976 weston_view_set_position(shsurf->view,
4977 shsurf->view->geometry.x + sx,
4978 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004979 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004980 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004981 default:
4982 ;
4983 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004984
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004985 /* Surface stacking order, see also activate(). */
4986 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004987
Jason Ekstranda7af7042013-10-12 22:38:11 -05004988 if (shsurf->type != SHELL_SURFACE_NONE) {
4989 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004990 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004991 shsurf->surface->output = shsurf->output;
4992 shsurf->view->output = shsurf->output;
4993 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004994 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004995
Jason Ekstranda7af7042013-10-12 22:38:11 -05004996 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004997 /* XXX: xwayland's using the same fields for transient type */
4998 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004999 if (shsurf->transient.flags ==
5000 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5001 break;
5002 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005003 if (shsurf->state.relative &&
5004 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5005 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005006 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005007 break;
5008 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005009 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005010 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005011 case SHELL_SURFACE_POPUP:
5012 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005013 default:
5014 break;
5015 }
5016
Rafael Antognolli03b16592013-12-03 15:35:42 -02005017 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5018 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005019 {
5020 switch (shell->win_animation_type) {
5021 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005022 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005023 break;
5024 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005025 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005026 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005027 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005028 default:
5029 break;
5030 }
5031 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005032}
5033
5034static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005035configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005036 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005037{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005038 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005039 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005040 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005041
Pekka Paalanen77346a62011-11-30 16:26:35 +02005042 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005043
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005044 assert(shsurf);
5045
Rafael Antognolli03b16592013-12-03 15:35:42 -02005046 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005047 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005048 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005049 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005050 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5051 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005052 mx = shsurf->output->x - surf_x;
5053 my = shsurf->output->y +
5054 get_output_panel_height(shell,shsurf->output) - surf_y;
5055 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005056 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005057 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005058 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005059
Alex Wu4539b082012-03-01 12:57:46 +08005060 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005061 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005062 wl_list_for_each(view, &surface->views, surface_link)
5063 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005064
Rafael Antognolli03b16592013-12-03 15:35:42 -02005065 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005066 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005067 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005068}
5069
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005070static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005071shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005072{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005073 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005074 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005075 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005076
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005077 assert(shsurf);
5078
5079 shell = shsurf->shell;
5080
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005081 if (!weston_surface_is_mapped(es) &&
5082 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005083 remove_popup_grab(shsurf);
5084 }
5085
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005086 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005087 return;
5088
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005089 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005090 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005091 type_changed = 1;
5092 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005093
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005094 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005095 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005096 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005097 shsurf->last_width != es->width ||
5098 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005099 float from_x, from_y;
5100 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005101
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005102 if (shsurf->resize_edges) {
5103 sx = 0;
5104 sy = 0;
5105 }
5106
5107 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5108 sx = shsurf->last_width - es->width;
5109 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5110 sy = shsurf->last_height - es->height;
5111
5112 shsurf->last_width = es->width;
5113 shsurf->last_height = es->height;
5114
Jason Ekstranda7af7042013-10-12 22:38:11 -05005115 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5116 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005117 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005118 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005119 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005120 }
5121}
5122
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005123static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005124
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005125static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005126desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005127{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005128 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005129 struct desktop_shell *shell =
5130 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005131
5132 shell->child.process.pid = 0;
5133 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005134
5135 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5136 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005137 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005138 shell->child.deathstamp = time;
5139 shell->child.deathcount = 0;
5140 }
5141
5142 shell->child.deathcount++;
5143 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005144 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005145 return;
5146 }
5147
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005148 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005149 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005150 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005151}
5152
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005153static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005154desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5155{
5156 struct desktop_shell *shell;
5157
5158 shell = container_of(listener, struct desktop_shell,
5159 child.client_destroy_listener);
5160
5161 shell->child.client = NULL;
5162}
5163
5164static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005165launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005166{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005167 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005168
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005169 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005170 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005171 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005172 desktop_shell_sigchld);
5173
5174 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005175 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005176
5177 shell->child.client_destroy_listener.notify =
5178 desktop_shell_client_destroy;
5179 wl_client_add_destroy_listener(shell->child.client,
5180 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005181}
5182
5183static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005184handle_shell_client_destroy(struct wl_listener *listener, void *data)
5185{
5186 struct shell_client *sc =
5187 container_of(listener, struct shell_client, destroy_listener);
5188
5189 if (sc->ping_timer)
5190 wl_event_source_remove(sc->ping_timer);
5191 free(sc);
5192}
5193
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005194static struct shell_client *
5195shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5196 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005197{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005198 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005199
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005200 sc = zalloc(sizeof *sc);
5201 if (sc == NULL) {
5202 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005203 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005204 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005205
5206 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005207 if (sc->resource == NULL) {
5208 free(sc);
5209 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005210 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005211 }
5212
5213 sc->client = client;
5214 sc->shell = shell;
5215 sc->destroy_listener.notify = handle_shell_client_destroy;
5216 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5217
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005218 return sc;
5219}
5220
5221static void
5222bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5223{
5224 struct desktop_shell *shell = data;
5225 struct shell_client *sc;
5226
5227 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5228 if (sc)
5229 wl_resource_set_implementation(sc->resource,
5230 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005231 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005232}
5233
5234static void
5235bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5236{
5237 struct desktop_shell *shell = data;
5238 struct shell_client *sc;
5239
5240 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5241 if (sc)
5242 wl_resource_set_dispatcher(sc->resource,
5243 xdg_shell_unversioned_dispatch,
5244 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005245}
5246
5247static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005248unbind_desktop_shell(struct wl_resource *resource)
5249{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005250 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005251
5252 if (shell->locked)
5253 resume_desktop(shell);
5254
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005255 shell->child.desktop_shell = NULL;
5256 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005257}
5258
5259static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005260bind_desktop_shell(struct wl_client *client,
5261 void *data, uint32_t version, uint32_t id)
5262{
Tiago Vignattibe143262012-04-16 17:31:41 +03005263 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005264 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005265
Jason Ekstranda85118c2013-06-27 20:17:02 -05005266 resource = wl_resource_create(client, &desktop_shell_interface,
5267 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005268
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005269 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005270 wl_resource_set_implementation(resource,
5271 &desktop_shell_implementation,
5272 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005273 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005274
5275 if (version < 2)
5276 shell_fade_startup(shell);
5277
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005278 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005279 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005280
5281 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5282 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005283 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005284}
5285
Pekka Paalanen6e168112011-11-24 11:34:05 +02005286static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005287screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005288{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005289 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005290 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005291
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005292 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005293 return;
5294
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005295 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005296 if (!shell->locked)
5297 return;
5298
Jason Ekstranda7af7042013-10-12 22:38:11 -05005299 view = container_of(surface->views.next, struct weston_view, surface_link);
5300 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005301
Jason Ekstranda7af7042013-10-12 22:38:11 -05005302 if (wl_list_empty(&view->layer_link)) {
5303 wl_list_insert(shell->lock_layer.view_list.prev,
5304 &view->layer_link);
5305 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005306 wl_event_source_timer_update(shell->screensaver.timer,
5307 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005308 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005309 }
5310}
5311
5312static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005313screensaver_set_surface(struct wl_client *client,
5314 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005315 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005316 struct wl_resource *output_resource)
5317{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005318 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005319 struct weston_surface *surface =
5320 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005321 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005322 struct weston_view *view, *next;
5323
5324 /* Make sure we only have one view */
5325 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5326 weston_view_destroy(view);
5327 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005328
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005329 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005330 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005331 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005332}
5333
5334static const struct screensaver_interface screensaver_implementation = {
5335 screensaver_set_surface
5336};
5337
5338static void
5339unbind_screensaver(struct wl_resource *resource)
5340{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005341 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005342
Pekka Paalanen77346a62011-11-30 16:26:35 +02005343 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005344}
5345
5346static void
5347bind_screensaver(struct wl_client *client,
5348 void *data, uint32_t version, uint32_t id)
5349{
Tiago Vignattibe143262012-04-16 17:31:41 +03005350 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005351 struct wl_resource *resource;
5352
Jason Ekstranda85118c2013-06-27 20:17:02 -05005353 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005354
Pekka Paalanen77346a62011-11-30 16:26:35 +02005355 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005356 wl_resource_set_implementation(resource,
5357 &screensaver_implementation,
5358 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005359 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005360 return;
5361 }
5362
5363 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5364 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005365 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005366}
5367
Kristian Høgsberg07045392012-02-19 18:52:44 -05005368struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005369 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005370 struct weston_surface *current;
5371 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005372 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005373 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005374};
5375
5376static void
5377switcher_next(struct switcher *switcher)
5378{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005379 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005380 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005381 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005382 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005383
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005384 /* temporary re-display minimized surfaces */
5385 struct weston_view *tmp;
5386 struct weston_view **minimized;
5387 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5388 wl_list_remove(&view->layer_link);
5389 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5390 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5391 *minimized = view;
5392 }
5393
Jason Ekstranda7af7042013-10-12 22:38:11 -05005394 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005395 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005396 if (shsurf &&
5397 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5398 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005399 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005400 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005401 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005402 next = view->surface;
5403 prev = view->surface;
5404 view->alpha = 0.25;
5405 weston_view_geometry_dirty(view);
5406 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005407 }
Alex Wu1659daa2012-04-01 20:13:09 +08005408
Jason Ekstranda7af7042013-10-12 22:38:11 -05005409 if (is_black_surface(view->surface, NULL)) {
5410 view->alpha = 0.25;
5411 weston_view_geometry_dirty(view);
5412 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005413 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005414 }
5415
5416 if (next == NULL)
5417 next = first;
5418
Alex Wu07b26062012-03-12 16:06:01 +08005419 if (next == NULL)
5420 return;
5421
Kristian Høgsberg07045392012-02-19 18:52:44 -05005422 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005423 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005424
5425 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005426 wl_list_for_each(view, &next->views, surface_link)
5427 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005428
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005429 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005430 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005431 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005432}
5433
5434static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005435switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005436{
5437 struct switcher *switcher =
5438 container_of(listener, struct switcher, listener);
5439
5440 switcher_next(switcher);
5441}
5442
5443static void
Daniel Stone351eb612012-05-31 15:27:47 -04005444switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005445{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005446 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005447 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005448 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005449
Jason Ekstranda7af7042013-10-12 22:38:11 -05005450 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005451 if (is_focus_view(view))
5452 continue;
5453
Jason Ekstranda7af7042013-10-12 22:38:11 -05005454 view->alpha = 1.0;
5455 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005456 }
5457
Alex Wu07b26062012-03-12 16:06:01 +08005458 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005459 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005460 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005461 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005462 weston_keyboard_end_grab(keyboard);
5463 if (keyboard->input_method_resource)
5464 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005465
5466 /* re-hide surfaces that were temporary shown during the switch */
5467 struct weston_view **minimized;
5468 wl_array_for_each(minimized, &switcher->minimized_array) {
5469 /* with the exception of the current selected */
5470 if ((*minimized)->surface != switcher->current) {
5471 wl_list_remove(&(*minimized)->layer_link);
5472 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5473 weston_view_damage_below(*minimized);
5474 }
5475 }
5476 wl_array_release(&switcher->minimized_array);
5477
Kristian Høgsberg07045392012-02-19 18:52:44 -05005478 free(switcher);
5479}
5480
5481static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005482switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005483 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005484{
5485 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005486 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005487
Daniel Stonec9785ea2012-05-30 16:31:52 +01005488 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005489 switcher_next(switcher);
5490}
5491
5492static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005493switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005494 uint32_t mods_depressed, uint32_t mods_latched,
5495 uint32_t mods_locked, uint32_t group)
5496{
5497 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005498 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005499
Daniel Stone351eb612012-05-31 15:27:47 -04005500 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5501 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005502}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005503
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005504static void
5505switcher_cancel(struct weston_keyboard_grab *grab)
5506{
5507 struct switcher *switcher = container_of(grab, struct switcher, grab);
5508
5509 switcher_destroy(switcher);
5510}
5511
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005512static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005513 switcher_key,
5514 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005515 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005516};
5517
5518static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005519switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005520 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005521{
Tiago Vignattibe143262012-04-16 17:31:41 +03005522 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005523 struct switcher *switcher;
5524
5525 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005526 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005527 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005528 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005529 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005530 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005531
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005532 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005533 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005534 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005535 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5536 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005537 switcher_next(switcher);
5538}
5539
Pekka Paalanen3c647232011-12-22 13:43:43 +02005540static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005541backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005542 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005543{
5544 struct weston_compositor *compositor = data;
5545 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005546 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005547
5548 /* TODO: we're limiting to simple use cases, where we assume just
5549 * control on the primary display. We'd have to extend later if we
5550 * ever get support for setting backlights on random desktop LCD
5551 * panels though */
5552 output = get_default_output(compositor);
5553 if (!output)
5554 return;
5555
5556 if (!output->set_backlight)
5557 return;
5558
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005559 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5560 backlight_new = output->backlight_current - 25;
5561 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5562 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005563
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005564 if (backlight_new < 5)
5565 backlight_new = 5;
5566 if (backlight_new > 255)
5567 backlight_new = 255;
5568
5569 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005570 output->set_backlight(output, output->backlight_current);
5571}
5572
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005573struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005574 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005575 struct weston_seat *seat;
5576 uint32_t key[2];
5577 int key_released[2];
5578};
5579
5580static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005581debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005582 uint32_t key, uint32_t state)
5583{
5584 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005585 struct weston_compositor *ec = db->seat->compositor;
5586 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005587 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005588 uint32_t serial;
5589 int send = 0, terminate = 0;
5590 int check_binding = 1;
5591 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005592 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005593
5594 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5595 /* Do not run bindings on key releases */
5596 check_binding = 0;
5597
5598 for (i = 0; i < 2; i++)
5599 if (key == db->key[i])
5600 db->key_released[i] = 1;
5601
5602 if (db->key_released[0] && db->key_released[1]) {
5603 /* All key releases been swalled so end the grab */
5604 terminate = 1;
5605 } else if (key != db->key[0] && key != db->key[1]) {
5606 /* Should not swallow release of other keys */
5607 send = 1;
5608 }
5609 } else if (key == db->key[0] && !db->key_released[0]) {
5610 /* Do not check bindings for the first press of the binding
5611 * key. This allows it to be used as a debug shortcut.
5612 * We still need to swallow this event. */
5613 check_binding = 0;
5614 } else if (db->key[1]) {
5615 /* If we already ran a binding don't process another one since
5616 * we can't keep track of all the binding keys that were
5617 * pressed in order to swallow the release events. */
5618 send = 1;
5619 check_binding = 0;
5620 }
5621
5622 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005623 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5624 key, state)) {
5625 /* We ran a binding so swallow the press and keep the
5626 * grab to swallow the released too. */
5627 send = 0;
5628 terminate = 0;
5629 db->key[1] = key;
5630 } else {
5631 /* Terminate the grab since the key pressed is not a
5632 * debug binding key. */
5633 send = 1;
5634 terminate = 1;
5635 }
5636 }
5637
5638 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005639 serial = wl_display_next_serial(display);
5640 resource_list = &grab->keyboard->focus_resource_list;
5641 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005642 wl_keyboard_send_key(resource, serial, time, key, state);
5643 }
5644 }
5645
5646 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005647 weston_keyboard_end_grab(grab->keyboard);
5648 if (grab->keyboard->input_method_resource)
5649 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005650 free(db);
5651 }
5652}
5653
5654static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005655debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005656 uint32_t mods_depressed, uint32_t mods_latched,
5657 uint32_t mods_locked, uint32_t group)
5658{
5659 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005660 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005661
Neil Roberts96d790e2013-09-19 17:32:00 +01005662 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005663
Neil Roberts96d790e2013-09-19 17:32:00 +01005664 wl_resource_for_each(resource, resource_list) {
5665 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5666 mods_latched, mods_locked, group);
5667 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005668}
5669
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005670static void
5671debug_binding_cancel(struct weston_keyboard_grab *grab)
5672{
5673 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5674
5675 weston_keyboard_end_grab(grab->keyboard);
5676 free(db);
5677}
5678
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005679struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005680 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005681 debug_binding_modifiers,
5682 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005683};
5684
5685static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005686debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005687{
5688 struct debug_binding_grab *grab;
5689
5690 grab = calloc(1, sizeof *grab);
5691 if (!grab)
5692 return;
5693
5694 grab->seat = (struct weston_seat *) seat;
5695 grab->key[0] = key;
5696 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005697 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005698}
5699
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005700static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005701force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005702 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005703{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005704 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005705 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005706 struct desktop_shell *shell = data;
5707 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005708 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005709
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005710 focus_surface = seat->keyboard->focus;
5711 if (!focus_surface)
5712 return;
5713
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005714 wl_signal_emit(&compositor->kill_signal, focus_surface);
5715
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005716 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005717 wl_client_get_credentials(client, &pid, NULL, NULL);
5718
5719 /* Skip clients that we launched ourselves (the credentials of
5720 * the socketpair is ours) */
5721 if (pid == getpid())
5722 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005723
Daniel Stone325fc2d2012-05-30 16:31:58 +01005724 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005725}
5726
5727static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005728workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005729 uint32_t key, void *data)
5730{
5731 struct desktop_shell *shell = data;
5732 unsigned int new_index = shell->workspaces.current;
5733
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005734 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005735 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005736 if (new_index != 0)
5737 new_index--;
5738
5739 change_workspace(shell, new_index);
5740}
5741
5742static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005743workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005744 uint32_t key, void *data)
5745{
5746 struct desktop_shell *shell = data;
5747 unsigned int new_index = shell->workspaces.current;
5748
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005749 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005750 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005751 if (new_index < shell->workspaces.num - 1)
5752 new_index++;
5753
5754 change_workspace(shell, new_index);
5755}
5756
5757static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005758workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005759 uint32_t key, void *data)
5760{
5761 struct desktop_shell *shell = data;
5762 unsigned int new_index;
5763
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005764 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005765 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005766 new_index = key - KEY_F1;
5767 if (new_index >= shell->workspaces.num)
5768 new_index = shell->workspaces.num - 1;
5769
5770 change_workspace(shell, new_index);
5771}
5772
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005773static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005774workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005775 uint32_t key, void *data)
5776{
5777 struct desktop_shell *shell = data;
5778 unsigned int new_index = shell->workspaces.current;
5779
5780 if (shell->locked)
5781 return;
5782
5783 if (new_index != 0)
5784 new_index--;
5785
5786 take_surface_to_workspace_by_seat(shell, seat, new_index);
5787}
5788
5789static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005790workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005791 uint32_t key, void *data)
5792{
5793 struct desktop_shell *shell = data;
5794 unsigned int new_index = shell->workspaces.current;
5795
5796 if (shell->locked)
5797 return;
5798
5799 if (new_index < shell->workspaces.num - 1)
5800 new_index++;
5801
5802 take_surface_to_workspace_by_seat(shell, seat, new_index);
5803}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005804
5805static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005806shell_reposition_view_on_output_destroy(struct weston_view *view)
5807{
5808 struct weston_output *output, *first_output;
5809 struct weston_compositor *ec = view->surface->compositor;
5810 struct shell_surface *shsurf;
5811 float x, y;
5812 int visible;
5813
5814 x = view->geometry.x;
5815 y = view->geometry.y;
5816
5817 /* At this point the destroyed output is not in the list anymore.
5818 * If the view is still visible somewhere, we leave where it is,
5819 * otherwise, move it to the first output. */
5820 visible = 0;
5821 wl_list_for_each(output, &ec->output_list, link) {
5822 if (pixman_region32_contains_point(&output->region,
5823 x, y, NULL)) {
5824 visible = 1;
5825 break;
5826 }
5827 }
5828
5829 if (!visible) {
5830 first_output = container_of(ec->output_list.next,
5831 struct weston_output, link);
5832
5833 x = first_output->x + first_output->width / 4;
5834 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005835
5836 weston_view_set_position(view, x, y);
5837 } else {
5838 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005839 }
5840
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005841
5842 shsurf = get_shell_surface(view->surface);
5843
5844 if (shsurf) {
5845 shsurf->saved_position_valid = false;
5846 shsurf->next_state.maximized = false;
5847 shsurf->next_state.fullscreen = false;
5848 shsurf->state_changed = true;
5849 }
5850}
5851
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005852void
5853shell_for_each_layer(struct desktop_shell *shell,
5854 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005855{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005856 struct workspace **ws;
5857
5858 func(shell, &shell->fullscreen_layer, data);
5859 func(shell, &shell->panel_layer, data);
5860 func(shell, &shell->background_layer, data);
5861 func(shell, &shell->lock_layer, data);
5862 func(shell, &shell->input_panel_layer, data);
5863
5864 wl_array_for_each(ws, &shell->workspaces.array)
5865 func(shell, &(*ws)->layer, data);
5866}
5867
5868static void
5869shell_output_destroy_move_layer(struct desktop_shell *shell,
5870 struct weston_layer *layer,
5871 void *data)
5872{
5873 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005874 struct weston_view *view;
5875
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005876 wl_list_for_each(view, &layer->view_list, layer_link) {
5877 if (view->output != output)
5878 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005879
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005880 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005881 }
5882}
5883
5884static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005885handle_output_destroy(struct wl_listener *listener, void *data)
5886{
5887 struct shell_output *output_listener =
5888 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005889 struct weston_output *output = output_listener->output;
5890 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005891
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005892 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005893
Xiong Zhang6b481422013-10-23 13:58:32 +08005894 wl_list_remove(&output_listener->destroy_listener.link);
5895 wl_list_remove(&output_listener->link);
5896 free(output_listener);
5897}
5898
5899static void
5900create_shell_output(struct desktop_shell *shell,
5901 struct weston_output *output)
5902{
5903 struct shell_output *shell_output;
5904
5905 shell_output = zalloc(sizeof *shell_output);
5906 if (shell_output == NULL)
5907 return;
5908
5909 shell_output->output = output;
5910 shell_output->shell = shell;
5911 shell_output->destroy_listener.notify = handle_output_destroy;
5912 wl_signal_add(&output->destroy_signal,
5913 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005914 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005915}
5916
5917static void
5918handle_output_create(struct wl_listener *listener, void *data)
5919{
5920 struct desktop_shell *shell =
5921 container_of(listener, struct desktop_shell, output_create_listener);
5922 struct weston_output *output = (struct weston_output *)data;
5923
5924 create_shell_output(shell, output);
5925}
5926
5927static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005928handle_output_move_layer(struct desktop_shell *shell,
5929 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005930{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005931 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005932 struct weston_view *view;
5933 float x, y;
5934
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005935 wl_list_for_each(view, &layer->view_list, layer_link) {
5936 if (view->output != output)
5937 continue;
5938
5939 x = view->geometry.x + output->move_x;
5940 y = view->geometry.y + output->move_y;
5941 weston_view_set_position(view, x, y);
5942 }
5943}
5944
5945static void
5946handle_output_move(struct wl_listener *listener, void *data)
5947{
5948 struct desktop_shell *shell;
5949
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005950 shell = container_of(listener, struct desktop_shell,
5951 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005952
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005953 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005954}
5955
5956static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005957setup_output_destroy_handler(struct weston_compositor *ec,
5958 struct desktop_shell *shell)
5959{
5960 struct weston_output *output;
5961
5962 wl_list_init(&shell->output_list);
5963 wl_list_for_each(output, &ec->output_list, link)
5964 create_shell_output(shell, output);
5965
5966 shell->output_create_listener.notify = handle_output_create;
5967 wl_signal_add(&ec->output_created_signal,
5968 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005969
5970 shell->output_move_listener.notify = handle_output_move;
5971 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005972}
5973
5974static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005975shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005976{
Tiago Vignattibe143262012-04-16 17:31:41 +03005977 struct desktop_shell *shell =
5978 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005979 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005980 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005981
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005982 /* Force state to unlocked so we don't try to fade */
5983 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005984 if (shell->child.client)
5985 wl_client_destroy(shell->child.client);
5986
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005987 wl_list_remove(&shell->idle_listener.link);
5988 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005989
5990 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005991
Xiong Zhang6b481422013-10-23 13:58:32 +08005992 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5993 wl_list_remove(&shell_output->destroy_listener.link);
5994 wl_list_remove(&shell_output->link);
5995 free(shell_output);
5996 }
5997
5998 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07005999 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006000
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006001 wl_array_for_each(ws, &shell->workspaces.array)
6002 workspace_destroy(*ws);
6003 wl_array_release(&shell->workspaces.array);
6004
Pekka Paalanen3c647232011-12-22 13:43:43 +02006005 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006006 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006007 free(shell);
6008}
6009
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006010static void
6011shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6012{
6013 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006014 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006015
6016 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006017 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6018 MODIFIER_CTRL | MODIFIER_ALT,
6019 terminate_binding, ec);
6020 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6021 click_to_activate_binding,
6022 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006023 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6024 click_to_activate_binding,
6025 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006026 weston_compositor_add_touch_binding(ec, 0,
6027 touch_to_activate_binding,
6028 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006029 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6030 MODIFIER_SUPER | MODIFIER_ALT,
6031 surface_opacity_binding, NULL);
6032 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6033 MODIFIER_SUPER, zoom_axis_binding,
6034 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006035
6036 /* configurable bindings */
6037 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006038 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6039 zoom_key_binding, NULL);
6040 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6041 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006042 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6043 maximize_binding, NULL);
6044 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6045 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006046 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6047 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006048 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006049 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6050 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006051 weston_compositor_add_button_binding(ec, BTN_LEFT,
6052 mod | MODIFIER_SHIFT,
6053 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006054
6055 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6056 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6057 rotate_binding, NULL);
6058
Daniel Stone325fc2d2012-05-30 16:31:58 +01006059 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6060 shell);
6061 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6062 ec);
6063 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6064 backlight_binding, ec);
6065 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6066 ec);
6067 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6068 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006069 weston_compositor_add_key_binding(ec, KEY_K, mod,
6070 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006071 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6072 workspace_up_binding, shell);
6073 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6074 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006075 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6076 workspace_move_surface_up_binding,
6077 shell);
6078 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6079 workspace_move_surface_down_binding,
6080 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006081
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006082 if (shell->exposay_modifier)
6083 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6084 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006085
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006086 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6087 if (shell->workspaces.num > 1) {
6088 num_workspace_bindings = shell->workspaces.num;
6089 if (num_workspace_bindings > 6)
6090 num_workspace_bindings = 6;
6091 for (i = 0; i < num_workspace_bindings; i++)
6092 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6093 workspace_f_binding,
6094 shell);
6095 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006096
6097 /* Debug bindings */
6098 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6099 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006100}
6101
Jason Ekstrand024177c2014-04-21 19:42:58 -05006102static void
6103handle_seat_created(struct wl_listener *listener, void *data)
6104{
6105 struct weston_seat *seat = data;
6106
6107 create_shell_seat(seat);
6108}
6109
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006110WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006111module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006112 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006113{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006114 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006115 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006116 struct workspace **pws;
6117 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006118 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006119
Peter Huttererf3d62272013-08-08 11:57:05 +10006120 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006121 if (shell == NULL)
6122 return -1;
6123
Kristian Høgsberg75840622011-09-06 13:48:16 -04006124 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006125
6126 shell->destroy_listener.notify = shell_destroy;
6127 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006128 shell->idle_listener.notify = idle_handler;
6129 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6130 shell->wake_listener.notify = wake_handler;
6131 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006132
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006133 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006134 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006135 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006136 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006137 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006138 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006139 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006140 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006141 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006142 ec->shell_interface.set_title = set_title;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07006143 ec->shell_interface.set_margin = set_margin;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006144
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006145 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6146 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006147 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6148 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006149 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006150
6151 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006152 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006153
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006154 if (input_panel_setup(shell) < 0)
6155 return -1;
6156
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006157 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006158
Daniel Stonedf8133b2013-11-19 11:37:14 +01006159 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6160 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6161
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006162 for (i = 0; i < shell->workspaces.num; i++) {
6163 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6164 if (pws == NULL)
6165 return -1;
6166
6167 *pws = workspace_create();
6168 if (*pws == NULL)
6169 return -1;
6170 }
6171 activate_workspace(shell, 0);
6172
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006173 weston_layer_init(&shell->minimized_layer, NULL);
6174
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006175 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006176 wl_list_init(&shell->workspaces.animation.link);
6177 shell->workspaces.animation.frame = animate_workspace_change_frame;
6178
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006179 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006180 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006181 return -1;
6182
Rafael Antognollie2a34552013-12-03 15:35:45 -02006183 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6184 shell, bind_xdg_shell) == NULL)
6185 return -1;
6186
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006187 if (wl_global_create(ec->wl_display,
6188 &desktop_shell_interface, 2,
6189 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006190 return -1;
6191
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006192 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6193 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006194 return -1;
6195
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006196 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6197 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006198 return -1;
6199
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006200 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006201
Xiong Zhang6b481422013-10-23 13:58:32 +08006202 setup_output_destroy_handler(ec, shell);
6203
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006204 loop = wl_display_get_event_loop(ec->wl_display);
6205 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006206
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006207 shell->screensaver.timer =
6208 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6209
Jason Ekstrand024177c2014-04-21 19:42:58 -05006210 wl_list_for_each(seat, &ec->seat_list, link)
6211 handle_seat_created(NULL, seat);
6212 shell->seat_create_listener.notify = handle_seat_created;
6213 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006214
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006215 screenshooter_create(ec);
6216
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006217 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006218
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006219 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006220
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006221 return 0;
6222}