blob: 7631f1b860cb7f8330288f9e694c80105df6eb9b [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050059enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020060 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030062 SHELL_SURFACE_POPUP,
63 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020064};
65
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050066struct shell_client;
67
Philip Withnall648a4dd2013-11-25 18:01:44 +000068/*
69 * Surface stacking and ordering.
70 *
71 * This is handled using several linked lists of surfaces, organised into
72 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
73 * above all of the surfaces in the layer below. The set of layers is static and
74 * in the following order (top-most first):
75 * • Lock layer (only ever displayed on its own)
76 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010077 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000078 * • Fullscreen layer
79 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Workspace layers
81 * • Background layer
82 *
83 * The list of layers may be manipulated to remove whole layers of surfaces from
84 * display. For example, when locking the screen, all layers except the lock
85 * layer are removed.
86 *
87 * A surface’s layer is modified on configuring the surface, in
88 * set_surface_type() (which is only called when the surface’s type change is
89 * _committed_). If a surface’s type changes (e.g. when making a window
90 * fullscreen) its layer changes too.
91 *
92 * In order to allow popup and transient surfaces to be correctly stacked above
93 * their parent surfaces, each surface tracks both its parent surface, and a
94 * linked list of its children. When a surface’s layer is updated, so are the
95 * layers of its children. Note that child surfaces are *not* the same as
96 * subsurfaces — child/parent surfaces are purely for maintaining stacking
97 * order.
98 *
99 * The children_link list of siblings of a surface (i.e. those surfaces which
100 * have the same parent) only contains weston_surfaces which have a
101 * shell_surface. Stacking is not implemented for non-shell_surface
102 * weston_surfaces. This means that the following implication does *not* hold:
103 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
104 */
105
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200106struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500107 struct wl_resource *resource;
108 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500109 struct shell_client *owner;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200110
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500111 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500112 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600113 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700115 struct wl_listener resource_destroy_listener;
116
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400117 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000118 struct wl_list children_list; /* child surfaces of this one */
119 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300120 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200121
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800122 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400123 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500124 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200125 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800126 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200127 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800128 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700129 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800130 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100131
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200133 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500134 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200135 } rotation;
136
137 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100138 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100140 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400141 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 } popup;
143
Alex Wu4539b082012-03-01 12:57:46 +0800144 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300145 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400146 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 } transient;
148
149 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800150 enum wl_shell_surface_fullscreen_method type;
151 struct weston_transform transform; /* matrix from x, y */
152 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800154 } fullscreen;
155
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200156 struct weston_transform workspace_transform;
157
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500158 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200160 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100161 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400162
163 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200164
165 struct {
166 bool maximized;
167 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200168 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100169 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500170 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200171 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500172 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500173
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700174 struct {
175 int left, right, top, bottom;
176 } margin;
177
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500178 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179};
180
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300181struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400182 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300183 struct shell_surface *shsurf;
184 struct wl_listener shsurf_destroy_listener;
185};
186
Rusty Lynch1084da52013-08-15 09:10:08 -0700187struct shell_touch_grab {
188 struct weston_touch_grab grab;
189 struct shell_surface *shsurf;
190 struct wl_listener shsurf_destroy_listener;
191 struct weston_touch *touch;
192};
193
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300194struct weston_move_grab {
195 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100196 wl_fixed_t dx, dy;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700197 int client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500198};
199
Rusty Lynch1084da52013-08-15 09:10:08 -0700200struct weston_touch_move_grab {
201 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800202 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700203 wl_fixed_t dx, dy;
204};
205
Pekka Paalanen460099f2012-01-20 16:48:25 +0200206struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300207 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500208 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200209 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200210 float x;
211 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200212 } center;
213};
214
Giulio Camuffo5085a752013-03-25 21:42:45 +0100215struct shell_seat {
216 struct weston_seat *seat;
217 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500218 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100219
Jason Ekstrand024177c2014-04-21 19:42:58 -0500220 struct wl_listener caps_changed_listener;
221 struct wl_listener pointer_focus_listener;
222 struct wl_listener keyboard_focus_listener;
223
Giulio Camuffo5085a752013-03-25 21:42:45 +0100224 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400225 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100226 struct wl_list surfaces_list;
227 struct wl_client *client;
228 int32_t initial_up;
229 } popup_grab;
230};
231
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800232struct shell_client {
233 struct wl_resource *resource;
234 struct wl_client *client;
235 struct desktop_shell *shell;
236 struct wl_listener destroy_listener;
237 struct wl_event_source *ping_timer;
238 uint32_t ping_serial;
239 int unresponsive;
240};
241
Alex Wubd3354b2012-04-17 17:20:49 +0800242static struct desktop_shell *
243shell_surface_get_shell(struct shell_surface *shsurf);
244
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500245static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400246surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500247
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300248static void
249shell_fade_startup(struct desktop_shell *shell);
250
Philip Withnallbecb77e2013-11-25 18:01:30 +0000251static struct shell_seat *
252get_shell_seat(struct weston_seat *seat);
253
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700254static int
255get_output_panel_height(struct desktop_shell *shell,
256 struct weston_output *output);
257
Philip Withnall648a4dd2013-11-25 18:01:44 +0000258static void
259shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
260
Alex Wubd3354b2012-04-17 17:20:49 +0800261static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200262shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
263
264static bool
265shell_surface_is_xdg_surface(struct shell_surface *shsurf);
266
267static bool
268shell_surface_is_xdg_popup(struct shell_surface *shsurf);
269
270static void
271shell_surface_set_parent(struct shell_surface *shsurf,
272 struct weston_surface *parent);
273
274static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800275shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
276{
277 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800279
280 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100281
Jason Ekstranda7af7042013-10-12 22:38:11 -0500282 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800283 return false;
284
Jason Ekstranda7af7042013-10-12 22:38:11 -0500285 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
286 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800287 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500288 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800289}
290
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500291static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400292destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300293{
294 struct shell_grab *grab;
295
296 grab = container_of(listener, struct shell_grab,
297 shsurf_destroy_listener);
298
299 grab->shsurf = NULL;
300}
301
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800302struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500303get_default_view(struct weston_surface *surface)
304{
305 struct shell_surface *shsurf;
306 struct weston_view *view;
307
308 if (!surface || wl_list_empty(&surface->views))
309 return NULL;
310
311 shsurf = get_shell_surface(surface);
312 if (shsurf)
313 return shsurf->view;
314
315 wl_list_for_each(view, &surface->views, surface_link)
316 if (weston_view_is_mapped(view))
317 return view;
318
319 return container_of(surface->views.next, struct weston_view, surface_link);
320}
321
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400323popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400324
325static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300326shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400327 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300328 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400329 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300330 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300331{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300332 struct desktop_shell *shell = shsurf->shell;
333
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400334 popup_grab_end(pointer);
335
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300336 grab->grab.interface = interface;
337 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400338 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500339 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400340 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700342 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400343 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400344 if (shell->child.desktop_shell) {
345 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
346 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500347 weston_pointer_set_focus(pointer,
348 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400349 wl_fixed_from_int(0),
350 wl_fixed_from_int(0));
351 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300352}
353
354static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300355shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300356{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700357 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400358 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700359 grab->shsurf->grabbed = 0;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800360 grab->shsurf->resize_edges = 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700361 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300362
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700363 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300364}
365
366static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700367shell_touch_grab_start(struct shell_touch_grab *grab,
368 const struct weston_touch_grab_interface *interface,
369 struct shell_surface *shsurf,
370 struct weston_touch *touch)
371{
372 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800373
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700374 if (touch->seat->pointer)
375 popup_grab_end(touch->seat->pointer);
376
Rusty Lynch1084da52013-08-15 09:10:08 -0700377 grab->grab.interface = interface;
378 grab->shsurf = shsurf;
379 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
380 wl_signal_add(&shsurf->destroy_signal,
381 &grab->shsurf_destroy_listener);
382
383 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700384 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700385
386 weston_touch_start_grab(touch, &grab->grab);
387 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500388 weston_touch_set_focus(touch->seat,
389 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700390}
391
392static void
393shell_touch_grab_end(struct shell_touch_grab *grab)
394{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700395 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700396 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700397 grab->shsurf->grabbed = 0;
398 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700399
400 weston_touch_end_grab(grab->touch);
401}
402
403static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500404center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800405 struct weston_output *output);
406
Daniel Stone496ca172012-05-30 16:31:42 +0100407static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300408get_modifier(char *modifier)
409{
410 if (!modifier)
411 return MODIFIER_SUPER;
412
413 if (!strcmp("ctrl", modifier))
414 return MODIFIER_CTRL;
415 else if (!strcmp("alt", modifier))
416 return MODIFIER_ALT;
417 else if (!strcmp("super", modifier))
418 return MODIFIER_SUPER;
419 else
420 return MODIFIER_SUPER;
421}
422
Juan Zhaoe10d2792012-04-25 19:09:52 +0800423static enum animation_type
424get_animation_type(char *animation)
425{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800426 if (!animation)
427 return ANIMATION_NONE;
428
Juan Zhaoe10d2792012-04-25 19:09:52 +0800429 if (!strcmp("zoom", animation))
430 return ANIMATION_ZOOM;
431 else if (!strcmp("fade", animation))
432 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100433 else if (!strcmp("dim-layer", animation))
434 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800435 else
436 return ANIMATION_NONE;
437}
438
Alex Wu4539b082012-03-01 12:57:46 +0800439static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400440shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200441{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400442 struct weston_config_section *section;
443 int duration;
444 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200445
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400446 section = weston_config_get_section(shell->compositor->config,
447 "screensaver", NULL, NULL);
448 weston_config_section_get_string(section,
449 "path", &shell->screensaver.path, NULL);
450 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200451 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400452
453 section = weston_config_get_section(shell->compositor->config,
454 "shell", NULL, NULL);
455 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100456 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100457 shell->client = s;
458 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400459 "binding-modifier", &s, "super");
460 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200461 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800462
463 weston_config_section_get_string(section,
464 "exposay-modifier", &s, "none");
465 if (strcmp(s, "none") == 0)
466 shell->exposay_modifier = 0;
467 else
468 shell->exposay_modifier = get_modifier(s);
469 free(s);
470
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400471 weston_config_section_get_string(section, "animation", &s, "none");
472 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200473 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700474 weston_config_section_get_string(section,
475 "startup-animation", &s, "fade");
476 shell->startup_animation_type = get_animation_type(s);
477 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700478 if (shell->startup_animation_type == ANIMATION_ZOOM)
479 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100480 weston_config_section_get_string(section, "focus-animation", &s, "none");
481 shell->focus_animation_type = get_animation_type(s);
482 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400483 weston_config_section_get_uint(section, "num-workspaces",
484 &shell->workspaces.num,
485 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200486}
487
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800488struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100489get_default_output(struct weston_compositor *compositor)
490{
491 return container_of(compositor->output_list.next,
492 struct weston_output, link);
493}
494
495
496/* no-op func for checking focus surface */
497static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600498focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100499{
500}
501
502static struct focus_surface *
503get_focus_surface(struct weston_surface *surface)
504{
505 if (surface->configure == focus_surface_configure)
506 return surface->configure_private;
507 else
508 return NULL;
509}
510
511static bool
512is_focus_surface (struct weston_surface *es)
513{
514 return (es->configure == focus_surface_configure);
515}
516
517static bool
518is_focus_view (struct weston_view *view)
519{
520 return is_focus_surface (view->surface);
521}
522
523static struct focus_surface *
524create_focus_surface(struct weston_compositor *ec,
525 struct weston_output *output)
526{
527 struct focus_surface *fsurf = NULL;
528 struct weston_surface *surface = NULL;
529
530 fsurf = malloc(sizeof *fsurf);
531 if (!fsurf)
532 return NULL;
533
534 fsurf->surface = weston_surface_create(ec);
535 surface = fsurf->surface;
536 if (surface == NULL) {
537 free(fsurf);
538 return NULL;
539 }
540
541 surface->configure = focus_surface_configure;
542 surface->output = output;
543 surface->configure_private = fsurf;
544
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800545 fsurf->view = weston_view_create(surface);
546 if (fsurf->view == NULL) {
547 weston_surface_destroy(surface);
548 free(fsurf);
549 return NULL;
550 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100551 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100552
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600553 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600554 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100555 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
556 pixman_region32_fini(&surface->opaque);
557 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
558 output->width, output->height);
559 pixman_region32_fini(&surface->input);
560 pixman_region32_init(&surface->input);
561
562 wl_list_init(&fsurf->workspace_transform.link);
563
564 return fsurf;
565}
566
567static void
568focus_surface_destroy(struct focus_surface *fsurf)
569{
570 weston_surface_destroy(fsurf->surface);
571 free(fsurf);
572}
573
574static void
575focus_animation_done(struct weston_view_animation *animation, void *data)
576{
577 struct workspace *ws = data;
578
579 ws->focus_animation = NULL;
580}
581
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200582static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200583focus_state_destroy(struct focus_state *state)
584{
585 wl_list_remove(&state->seat_destroy_listener.link);
586 wl_list_remove(&state->surface_destroy_listener.link);
587 free(state);
588}
589
590static void
591focus_state_seat_destroy(struct wl_listener *listener, void *data)
592{
593 struct focus_state *state = container_of(listener,
594 struct focus_state,
595 seat_destroy_listener);
596
597 wl_list_remove(&state->link);
598 focus_state_destroy(state);
599}
600
601static void
602focus_state_surface_destroy(struct wl_listener *listener, void *data)
603{
604 struct focus_state *state = container_of(listener,
605 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400606 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400607 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500608 struct weston_surface *main_surface, *next;
609 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200610
Pekka Paalanen01388e22013-04-25 13:57:44 +0300611 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
612
Kristian Høgsberge3778222012-07-31 17:29:30 -0400613 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500614 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
615 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400616 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100617 if (is_focus_view(view))
618 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400619
Jason Ekstranda7af7042013-10-12 22:38:11 -0500620 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400621 break;
622 }
623
Pekka Paalanen01388e22013-04-25 13:57:44 +0300624 /* if the focus was a sub-surface, activate its main surface */
625 if (main_surface != state->keyboard_focus)
626 next = main_surface;
627
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100628 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400629 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100630 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100631 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400632 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100633 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
634 if (state->ws->focus_animation)
635 weston_view_animation_destroy(state->ws->focus_animation);
636
637 state->ws->focus_animation = weston_fade_run(
638 state->ws->fsurf_front->view,
639 state->ws->fsurf_front->view->alpha, 0.0, 300,
640 focus_animation_done, state->ws);
641 }
642
Kristian Høgsberge3778222012-07-31 17:29:30 -0400643 wl_list_remove(&state->link);
644 focus_state_destroy(state);
645 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200646}
647
648static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400649focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200650{
Jonas Ådahl04769742012-06-13 00:01:24 +0200651 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200652
653 state = malloc(sizeof *state);
654 if (state == NULL)
655 return NULL;
656
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100657 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400658 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200659 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400660 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200661
662 state->seat_destroy_listener.notify = focus_state_seat_destroy;
663 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400664 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200665 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400666 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200667
668 return state;
669}
670
Jonas Ådahl8538b222012-08-29 22:13:03 +0200671static struct focus_state *
672ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
673{
674 struct workspace *ws = get_current_workspace(shell);
675 struct focus_state *state;
676
677 wl_list_for_each(state, &ws->focus_list, link)
678 if (state->seat == seat)
679 break;
680
681 if (&state->link == &ws->focus_list)
682 state = focus_state_create(seat, ws);
683
684 return state;
685}
686
Jonas Ådahl04769742012-06-13 00:01:24 +0200687static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800688focus_state_set_focus(struct focus_state *state,
689 struct weston_surface *surface)
690{
691 if (state->keyboard_focus) {
692 wl_list_remove(&state->surface_destroy_listener.link);
693 wl_list_init(&state->surface_destroy_listener.link);
694 }
695
696 state->keyboard_focus = surface;
697 if (surface)
698 wl_signal_add(&surface->destroy_signal,
699 &state->surface_destroy_listener);
700}
701
702static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400703restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200704{
705 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400706 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100707 struct wl_list pending_seat_list;
708 struct weston_seat *seat, *next_seat;
709
710 /* Temporarily steal the list of seats so that we can keep
711 * track of the seats we've already processed */
712 wl_list_init(&pending_seat_list);
713 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
714 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200715
716 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100717 wl_list_remove(&state->seat->link);
718 wl_list_insert(&shell->compositor->seat_list,
719 &state->seat->link);
720
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800721 if (state->seat->keyboard == NULL)
722 continue;
723
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400724 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200725
Kristian Høgsberge3148752013-05-06 23:19:49 -0400726 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200727 }
Neil Roberts4237f502014-04-09 16:33:31 +0100728
729 /* For any remaining seats that we don't have a focus state
730 * for we'll reset the keyboard focus to NULL */
731 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
732 wl_list_insert(&shell->compositor->seat_list, &seat->link);
733
734 if (state->seat->keyboard == NULL)
735 continue;
736
737 weston_keyboard_set_focus(seat->keyboard, NULL);
738 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200739}
740
741static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200742replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
743 struct weston_seat *seat)
744{
745 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200746
747 wl_list_for_each(state, &ws->focus_list, link) {
748 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800749 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200750 return;
751 }
752 }
753}
754
755static void
756drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
757 struct weston_surface *surface)
758{
759 struct focus_state *state;
760
761 wl_list_for_each(state, &ws->focus_list, link)
762 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800763 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200764}
765
766static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100767animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
768 struct weston_view *from, struct weston_view *to)
769{
770 struct weston_output *output;
771 bool focus_surface_created = false;
772
773 /* FIXME: Only support dim animation using two layers */
774 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
775 return;
776
777 output = get_default_output(shell->compositor);
778 if (ws->fsurf_front == NULL && (from || to)) {
779 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800780 if (ws->fsurf_front == NULL)
781 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100782 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800783
784 ws->fsurf_back = create_focus_surface(shell->compositor, output);
785 if (ws->fsurf_back == NULL) {
786 focus_surface_destroy(ws->fsurf_front);
787 return;
788 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100789 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800790
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100791 focus_surface_created = true;
792 } else {
793 wl_list_remove(&ws->fsurf_front->view->layer_link);
794 wl_list_remove(&ws->fsurf_back->view->layer_link);
795 }
796
797 if (ws->focus_animation) {
798 weston_view_animation_destroy(ws->focus_animation);
799 ws->focus_animation = NULL;
800 }
801
802 if (to)
803 wl_list_insert(&to->layer_link,
804 &ws->fsurf_front->view->layer_link);
805 else if (from)
806 wl_list_insert(&ws->layer.view_list,
807 &ws->fsurf_front->view->layer_link);
808
809 if (focus_surface_created) {
810 ws->focus_animation = weston_fade_run(
811 ws->fsurf_front->view,
812 ws->fsurf_front->view->alpha, 0.6, 300,
813 focus_animation_done, ws);
814 } else if (from) {
815 wl_list_insert(&from->layer_link,
816 &ws->fsurf_back->view->layer_link);
817 ws->focus_animation = weston_stable_fade_run(
818 ws->fsurf_front->view, 0.0,
819 ws->fsurf_back->view, 0.6,
820 focus_animation_done, ws);
821 } else if (to) {
822 wl_list_insert(&ws->layer.view_list,
823 &ws->fsurf_back->view->layer_link);
824 ws->focus_animation = weston_stable_fade_run(
825 ws->fsurf_front->view, 0.0,
826 ws->fsurf_back->view, 0.6,
827 focus_animation_done, ws);
828 }
829}
830
831static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200832workspace_destroy(struct workspace *ws)
833{
Jonas Ådahl04769742012-06-13 00:01:24 +0200834 struct focus_state *state, *next;
835
836 wl_list_for_each_safe(state, next, &ws->focus_list, link)
837 focus_state_destroy(state);
838
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100839 if (ws->fsurf_front)
840 focus_surface_destroy(ws->fsurf_front);
841 if (ws->fsurf_back)
842 focus_surface_destroy(ws->fsurf_back);
843
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200844 free(ws);
845}
846
Jonas Ådahl04769742012-06-13 00:01:24 +0200847static void
848seat_destroyed(struct wl_listener *listener, void *data)
849{
850 struct weston_seat *seat = data;
851 struct focus_state *state, *next;
852 struct workspace *ws = container_of(listener,
853 struct workspace,
854 seat_destroyed_listener);
855
856 wl_list_for_each_safe(state, next, &ws->focus_list, link)
857 if (state->seat == seat)
858 wl_list_remove(&state->link);
859}
860
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200861static struct workspace *
862workspace_create(void)
863{
864 struct workspace *ws = malloc(sizeof *ws);
865 if (ws == NULL)
866 return NULL;
867
868 weston_layer_init(&ws->layer, NULL);
869
Jonas Ådahl04769742012-06-13 00:01:24 +0200870 wl_list_init(&ws->focus_list);
871 wl_list_init(&ws->seat_destroyed_listener.link);
872 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100873 ws->fsurf_front = NULL;
874 ws->fsurf_back = NULL;
875 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200876
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200877 return ws;
878}
879
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200880static int
881workspace_is_empty(struct workspace *ws)
882{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200884}
885
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200886static struct workspace *
887get_workspace(struct desktop_shell *shell, unsigned int index)
888{
889 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200890 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200891 pws += index;
892 return *pws;
893}
894
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800895struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200896get_current_workspace(struct desktop_shell *shell)
897{
898 return get_workspace(shell, shell->workspaces.current);
899}
900
901static void
902activate_workspace(struct desktop_shell *shell, unsigned int index)
903{
904 struct workspace *ws;
905
906 ws = get_workspace(shell, index);
907 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
908
909 shell->workspaces.current = index;
910}
911
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912static unsigned int
913get_output_height(struct weston_output *output)
914{
915 return abs(output->region.extents.y1 - output->region.extents.y2);
916}
917
918static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100919view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200920{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200921 struct weston_transform *transform;
922
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100923 if (is_focus_view(view)) {
924 struct focus_surface *fsurf = get_focus_surface(view->surface);
925 transform = &fsurf->workspace_transform;
926 } else {
927 struct shell_surface *shsurf = get_shell_surface(view->surface);
928 transform = &shsurf->workspace_transform;
929 }
930
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200931 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100933 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200934
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100935 weston_matrix_init(&transform->matrix);
936 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200937 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200939}
940
941static void
942workspace_translate_out(struct workspace *ws, double fraction)
943{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200945 unsigned int height;
946 double d;
947
Jason Ekstranda7af7042013-10-12 22:38:11 -0500948 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
949 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200950 d = height * fraction;
951
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100952 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200953 }
954}
955
956static void
957workspace_translate_in(struct workspace *ws, double fraction)
958{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200960 unsigned int height;
961 double d;
962
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
964 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965
966 if (fraction > 0)
967 d = -(height - height * fraction);
968 else
969 d = height + height * fraction;
970
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100971 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200972 }
973}
974
975static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200976broadcast_current_workspace_state(struct desktop_shell *shell)
977{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700978 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200979
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700980 wl_resource_for_each(resource, &shell->workspaces.client_list)
981 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200982 shell->workspaces.current,
983 shell->workspaces.num);
984}
985
986static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200987reverse_workspace_change_animation(struct desktop_shell *shell,
988 unsigned int index,
989 struct workspace *from,
990 struct workspace *to)
991{
992 shell->workspaces.current = index;
993
994 shell->workspaces.anim_to = to;
995 shell->workspaces.anim_from = from;
996 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
997 shell->workspaces.anim_timestamp = 0;
998
Scott Moreau4272e632012-08-13 09:58:41 -0600999 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001000}
1001
1002static void
1003workspace_deactivate_transforms(struct workspace *ws)
1004{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001006 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001009 if (is_focus_view(view)) {
1010 struct focus_surface *fsurf = get_focus_surface(view->surface);
1011 transform = &fsurf->workspace_transform;
1012 } else {
1013 struct shell_surface *shsurf = get_shell_surface(view->surface);
1014 transform = &shsurf->workspace_transform;
1015 }
1016
1017 if (!wl_list_empty(&transform->link)) {
1018 wl_list_remove(&transform->link);
1019 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001020 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001021 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001022 }
1023}
1024
1025static void
1026finish_workspace_change_animation(struct desktop_shell *shell,
1027 struct workspace *from,
1028 struct workspace *to)
1029{
Scott Moreau4272e632012-08-13 09:58:41 -06001030 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001031
1032 wl_list_remove(&shell->workspaces.animation.link);
1033 workspace_deactivate_transforms(from);
1034 workspace_deactivate_transforms(to);
1035 shell->workspaces.anim_to = NULL;
1036
1037 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1038}
1039
1040static void
1041animate_workspace_change_frame(struct weston_animation *animation,
1042 struct weston_output *output, uint32_t msecs)
1043{
1044 struct desktop_shell *shell =
1045 container_of(animation, struct desktop_shell,
1046 workspaces.animation);
1047 struct workspace *from = shell->workspaces.anim_from;
1048 struct workspace *to = shell->workspaces.anim_to;
1049 uint32_t t;
1050 double x, y;
1051
1052 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1053 finish_workspace_change_animation(shell, from, to);
1054 return;
1055 }
1056
1057 if (shell->workspaces.anim_timestamp == 0) {
1058 if (shell->workspaces.anim_current == 0.0)
1059 shell->workspaces.anim_timestamp = msecs;
1060 else
1061 shell->workspaces.anim_timestamp =
1062 msecs -
1063 /* Invers of movement function 'y' below. */
1064 (asin(1.0 - shell->workspaces.anim_current) *
1065 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1066 M_2_PI);
1067 }
1068
1069 t = msecs - shell->workspaces.anim_timestamp;
1070
1071 /*
1072 * x = [0, π/2]
1073 * y(x) = sin(x)
1074 */
1075 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1076 y = sin(x);
1077
1078 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001079 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080
1081 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1082 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1083 shell->workspaces.anim_current = y;
1084
Scott Moreau4272e632012-08-13 09:58:41 -06001085 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001086 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001087 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001088 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001089}
1090
1091static void
1092animate_workspace_change(struct desktop_shell *shell,
1093 unsigned int index,
1094 struct workspace *from,
1095 struct workspace *to)
1096{
1097 struct weston_output *output;
1098
1099 int dir;
1100
1101 if (index > shell->workspaces.current)
1102 dir = -1;
1103 else
1104 dir = 1;
1105
1106 shell->workspaces.current = index;
1107
1108 shell->workspaces.anim_dir = dir;
1109 shell->workspaces.anim_from = from;
1110 shell->workspaces.anim_to = to;
1111 shell->workspaces.anim_current = 0.0;
1112 shell->workspaces.anim_timestamp = 0;
1113
1114 output = container_of(shell->compositor->output_list.next,
1115 struct weston_output, link);
1116 wl_list_insert(&output->animation_list,
1117 &shell->workspaces.animation.link);
1118
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001119 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120
1121 workspace_translate_in(to, 0);
1122
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001123 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001124
Scott Moreau4272e632012-08-13 09:58:41 -06001125 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126}
1127
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001128static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001129update_workspace(struct desktop_shell *shell, unsigned int index,
1130 struct workspace *from, struct workspace *to)
1131{
1132 shell->workspaces.current = index;
1133 wl_list_insert(&from->layer.link, &to->layer.link);
1134 wl_list_remove(&from->layer.link);
1135}
1136
1137static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001138change_workspace(struct desktop_shell *shell, unsigned int index)
1139{
1140 struct workspace *from;
1141 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001142 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001143
1144 if (index == shell->workspaces.current)
1145 return;
1146
1147 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001149 return;
1150
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001151 from = get_current_workspace(shell);
1152 to = get_workspace(shell, index);
1153
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154 if (shell->workspaces.anim_from == to &&
1155 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001156 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001157 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001158 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001159 return;
1160 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001161
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001162 if (shell->workspaces.anim_to != NULL)
1163 finish_workspace_change_animation(shell,
1164 shell->workspaces.anim_from,
1165 shell->workspaces.anim_to);
1166
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001167 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001168
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001169 if (shell->focus_animation_type != ANIMATION_NONE) {
1170 wl_list_for_each(state, &from->focus_list, link)
1171 if (state->keyboard_focus)
1172 animate_focus_change(shell, from,
1173 get_default_view(state->keyboard_focus), NULL);
1174
1175 wl_list_for_each(state, &to->focus_list, link)
1176 if (state->keyboard_focus)
1177 animate_focus_change(shell, to,
1178 NULL, get_default_view(state->keyboard_focus));
1179 }
1180
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001181 if (workspace_is_empty(to) && workspace_is_empty(from))
1182 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001183 else
1184 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001185
1186 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001187}
1188
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001189static bool
1190workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1191{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001193 struct wl_list *e;
1194
1195 if (wl_list_empty(list))
1196 return false;
1197
1198 e = list->next;
1199
1200 if (e->next != list)
1201 return false;
1202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204}
1205
1206static void
Philip Withnall659163d2013-11-25 18:01:36 +00001207move_surface_to_workspace(struct desktop_shell *shell,
1208 struct shell_surface *shsurf,
1209 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001210{
1211 struct workspace *from;
1212 struct workspace *to;
1213 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001214 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001215 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001216
1217 if (workspace == shell->workspaces.current)
1218 return;
1219
Philip Withnall659163d2013-11-25 18:01:36 +00001220 view = get_default_view(shsurf->surface);
1221 if (!view)
1222 return;
1223
1224 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1225
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001226 if (workspace >= shell->workspaces.num)
1227 workspace = shell->workspaces.num - 1;
1228
Jonas Ådahle9d22502012-08-29 22:13:01 +02001229 from = get_current_workspace(shell);
1230 to = get_workspace(shell, workspace);
1231
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 wl_list_remove(&view->layer_link);
1233 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001234
Philip Withnall648a4dd2013-11-25 18:01:44 +00001235 shell_surface_update_child_surface_layers(shsurf);
1236
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001238 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1239 if (!seat->keyboard)
1240 continue;
1241
1242 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001244 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001245 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001248}
1249
1250static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001251take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001252 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001253 unsigned int index)
1254{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001255 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257 struct shell_surface *shsurf;
1258 struct workspace *from;
1259 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001260 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001261
Pekka Paalanen01388e22013-04-25 13:57:44 +03001262 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 view = get_default_view(surface);
1264 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001265 index == shell->workspaces.current ||
1266 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001267 return;
1268
1269 from = get_current_workspace(shell);
1270 to = get_workspace(shell, index);
1271
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 wl_list_remove(&view->layer_link);
1273 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001274
Philip Withnall659163d2013-11-25 18:01:36 +00001275 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001276 if (shsurf != NULL)
1277 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001278
Jonas Ådahle9d22502012-08-29 22:13:01 +02001279 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001280 drop_focus_state(shell, from, surface);
1281
1282 if (shell->workspaces.anim_from == to &&
1283 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001284 wl_list_remove(&to->layer.link);
1285 wl_list_insert(from->layer.link.prev, &to->layer.link);
1286
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001287 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001288 broadcast_current_workspace_state(shell);
1289
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001290 return;
1291 }
1292
1293 if (shell->workspaces.anim_to != NULL)
1294 finish_workspace_change_animation(shell,
1295 shell->workspaces.anim_from,
1296 shell->workspaces.anim_to);
1297
1298 if (workspace_is_empty(from) &&
1299 workspace_has_only(to, surface))
1300 update_workspace(shell, index, from, to);
1301 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001302 if (shsurf != NULL &&
1303 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001304 wl_list_insert(&shell->workspaces.anim_sticky_list,
1305 &shsurf->workspace_transform.link);
1306
1307 animate_workspace_change(shell, index, from, to);
1308 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001309
1310 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001311
1312 state = ensure_focus_state(shell, seat);
1313 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001314 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001315}
1316
1317static void
1318workspace_manager_move_surface(struct wl_client *client,
1319 struct wl_resource *resource,
1320 struct wl_resource *surface_resource,
1321 uint32_t workspace)
1322{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001323 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001324 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001325 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001326 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001327 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001328
Pekka Paalanen01388e22013-04-25 13:57:44 +03001329 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001330 shell_surface = get_shell_surface(main_surface);
1331 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001333
1334 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001335}
1336
1337static const struct workspace_manager_interface workspace_manager_implementation = {
1338 workspace_manager_move_surface,
1339};
1340
1341static void
1342unbind_resource(struct wl_resource *resource)
1343{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001344 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001345}
1346
1347static void
1348bind_workspace_manager(struct wl_client *client,
1349 void *data, uint32_t version, uint32_t id)
1350{
1351 struct desktop_shell *shell = data;
1352 struct wl_resource *resource;
1353
Jason Ekstranda85118c2013-06-27 20:17:02 -05001354 resource = wl_resource_create(client,
1355 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001356
1357 if (resource == NULL) {
1358 weston_log("couldn't add workspace manager object");
1359 return;
1360 }
1361
Jason Ekstranda85118c2013-06-27 20:17:02 -05001362 wl_resource_set_implementation(resource,
1363 &workspace_manager_implementation,
1364 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001365 wl_list_insert(&shell->workspaces.client_list,
1366 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001367
1368 workspace_manager_send_state(resource,
1369 shell->workspaces.current,
1370 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001371}
1372
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001373static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001374touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1375 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1376{
1377}
1378
1379static void
1380touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1381{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001382 struct weston_touch_move_grab *move =
1383 (struct weston_touch_move_grab *) container_of(
1384 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001385
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001386 if (touch_id == 0)
1387 move->active = 0;
1388
Jonas Ådahl9484b692013-12-02 22:05:03 +01001389 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001390 shell_touch_grab_end(&move->base);
1391 free(move);
1392 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001393}
1394
1395static void
1396touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1397 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1398{
1399 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1400 struct shell_surface *shsurf = move->base.shsurf;
1401 struct weston_surface *es;
1402 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1403 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1404
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001405 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001406 return;
1407
1408 es = shsurf->surface;
1409
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001410 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001411
1412 weston_compositor_schedule_repaint(es->compositor);
1413}
1414
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001415static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001416touch_move_grab_frame(struct weston_touch_grab *grab)
1417{
1418}
1419
1420static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001421touch_move_grab_cancel(struct weston_touch_grab *grab)
1422{
1423 struct weston_touch_move_grab *move =
1424 (struct weston_touch_move_grab *) container_of(
1425 grab, struct shell_touch_grab, grab);
1426
1427 shell_touch_grab_end(&move->base);
1428 free(move);
1429}
1430
Rusty Lynch1084da52013-08-15 09:10:08 -07001431static const struct weston_touch_grab_interface touch_move_grab_interface = {
1432 touch_move_grab_down,
1433 touch_move_grab_up,
1434 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001435 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001436 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001437};
1438
1439static int
1440surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1441{
1442 struct weston_touch_move_grab *move;
1443
1444 if (!shsurf)
1445 return -1;
1446
Rafael Antognolli03b16592013-12-03 15:35:42 -02001447 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001448 return 0;
1449
1450 move = malloc(sizeof *move);
1451 if (!move)
1452 return -1;
1453
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001454 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001456 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001458 seat->touch->grab_y;
1459
1460 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1461 seat->touch);
1462
1463 return 0;
1464}
1465
1466static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001467noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001468{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001469}
1470
1471static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001472constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1473{
1474 struct shell_surface *shsurf = move->base.shsurf;
1475 struct weston_pointer *pointer = move->base.grab.pointer;
Kristian Høgsberg3434b332014-04-29 16:38:23 -07001476 int x, y, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001477 const int safety = 50;
1478
1479 x = wl_fixed_to_int(pointer->x + move->dx);
1480 y = wl_fixed_to_int(pointer->y + move->dy);
1481
1482 panel_height = get_output_panel_height(shsurf->shell,
1483 shsurf->surface->output);
1484 bottom = y + shsurf->surface->height - shsurf->margin.bottom;
1485 if (bottom - panel_height < safety)
1486 y = panel_height + safety -
1487 shsurf->surface->height + shsurf->margin.bottom;
1488
1489 if (move->client_initiated &&
1490 y + shsurf->margin.top < panel_height)
1491 y = panel_height - shsurf->margin.top;
1492
1493 *cx = x;
1494 *cy = y;
1495}
1496
1497static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001498move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1499 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001500{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001501 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001502 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001503 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001504 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001505
1506 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001507 if (!shsurf)
1508 return;
1509
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001510 constrain_position(move, &cx, &cy);
1511
1512 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001513
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001515}
1516
1517static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001518move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001519 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001520{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001521 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1522 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001523 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001524 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001525
Daniel Stone4dbadb12012-05-30 16:31:51 +01001526 if (pointer->button_count == 0 &&
1527 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001528 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001529 free(grab);
1530 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001531}
1532
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001533static void
1534move_grab_cancel(struct weston_pointer_grab *grab)
1535{
1536 struct shell_grab *shell_grab =
1537 container_of(grab, struct shell_grab, grab);
1538
1539 shell_grab_end(shell_grab);
1540 free(grab);
1541}
1542
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001543static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001544 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001545 move_grab_motion,
1546 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001547 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001548};
1549
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001550static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001551surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1552 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001553{
1554 struct weston_move_grab *move;
1555
1556 if (!shsurf)
1557 return -1;
1558
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001559 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001560 return 0;
1561
1562 move = malloc(sizeof *move);
1563 if (!move)
1564 return -1;
1565
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001567 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001568 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001569 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001570 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001571
1572 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001573 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001574
1575 return 0;
1576}
1577
1578static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001579common_surface_move(struct wl_resource *resource,
1580 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001582 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001583 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001584 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001585
Kristian Høgsbergd0b40ed2014-04-29 14:47:46 -07001586 if (shsurf->grabbed)
1587 return;
1588
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001589 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001590 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001591 seat->pointer->button_count > 0 &&
1592 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001594 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001595 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001596 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001597 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001598 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001599 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001601 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001602 (surface_touch_move(shsurf, seat) < 0))
1603 wl_resource_post_no_memory(resource);
1604 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001605}
1606
Rafael Antognollie2a34552013-12-03 15:35:45 -02001607static void
1608shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1609 struct wl_resource *seat_resource, uint32_t serial)
1610{
1611 common_surface_move(resource, seat_resource, serial);
1612}
1613
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001614struct weston_resize_grab {
1615 struct shell_grab base;
1616 uint32_t edges;
1617 int32_t width, height;
1618};
1619
1620static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001621resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1622 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001623{
1624 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001625 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001626 struct shell_surface *shsurf = resize->base.shsurf;
1627 int32_t width, height;
1628 wl_fixed_t from_x, from_y;
1629 wl_fixed_t to_x, to_y;
1630
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001631 weston_pointer_move(pointer, x, y);
1632
Ander Conselvan de Oliveira9c376b52014-04-29 17:54:03 +03001633 if (!shsurf || !shsurf->resource)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001634 return;
1635
Jason Ekstranda7af7042013-10-12 22:38:11 -05001636 weston_view_from_global_fixed(shsurf->view,
1637 pointer->grab_x, pointer->grab_y,
1638 &from_x, &from_y);
1639 weston_view_from_global_fixed(shsurf->view,
1640 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001641
1642 width = resize->width;
1643 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1644 width += wl_fixed_to_int(from_x - to_x);
1645 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1646 width += wl_fixed_to_int(to_x - from_x);
1647 }
1648
1649 height = resize->height;
1650 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1651 height += wl_fixed_to_int(from_y - to_y);
1652 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1653 height += wl_fixed_to_int(to_y - from_y);
1654 }
1655
1656 shsurf->client->send_configure(shsurf->surface,
1657 resize->edges, width, height);
1658}
1659
1660static void
1661send_configure(struct weston_surface *surface,
1662 uint32_t edges, int32_t width, int32_t height)
1663{
1664 struct shell_surface *shsurf = get_shell_surface(surface);
1665
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001666 assert(shsurf);
1667
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001668 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001669 edges, width, height);
1670}
1671
1672static const struct weston_shell_client shell_client = {
1673 send_configure
1674};
1675
1676static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001677resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001678 uint32_t time, uint32_t button, uint32_t state_w)
1679{
1680 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001681 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001682 enum wl_pointer_button_state state = state_w;
1683
1684 if (pointer->button_count == 0 &&
1685 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1686 shell_grab_end(&resize->base);
1687 free(grab);
1688 }
1689}
1690
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001691static void
1692resize_grab_cancel(struct weston_pointer_grab *grab)
1693{
1694 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1695
1696 shell_grab_end(&resize->base);
1697 free(grab);
1698}
1699
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001700static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701 noop_grab_focus,
1702 resize_grab_motion,
1703 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001704 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001705};
1706
Giulio Camuffob8366642013-04-25 13:57:46 +03001707/*
1708 * Returns the bounding box of a surface and all its sub-surfaces,
1709 * in the surface coordinates system. */
1710static void
1711surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1712 int32_t *y, int32_t *w, int32_t *h) {
1713 pixman_region32_t region;
1714 pixman_box32_t *box;
1715 struct weston_subsurface *subsurface;
1716
1717 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001718 surface->width,
1719 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001720
1721 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1722 pixman_region32_union_rect(&region, &region,
1723 subsurface->position.x,
1724 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725 subsurface->surface->width,
1726 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001727 }
1728
1729 box = pixman_region32_extents(&region);
1730 if (x)
1731 *x = box->x1;
1732 if (y)
1733 *y = box->y1;
1734 if (w)
1735 *w = box->x2 - box->x1;
1736 if (h)
1737 *h = box->y2 - box->y1;
1738
1739 pixman_region32_fini(&region);
1740}
1741
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001742static int
1743surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001744 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001745{
1746 struct weston_resize_grab *resize;
1747
Rafael Antognolli03b16592013-12-03 15:35:42 -02001748 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001749 return 0;
1750
1751 if (edges == 0 || edges > 15 ||
1752 (edges & 3) == 3 || (edges & 12) == 12)
1753 return 0;
1754
1755 resize = malloc(sizeof *resize);
1756 if (!resize)
1757 return -1;
1758
1759 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001760 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1761 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001763 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001764 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001765 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001766
1767 return 0;
1768}
1769
1770static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001771common_surface_resize(struct wl_resource *resource,
1772 struct wl_resource *seat_resource, uint32_t serial,
1773 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001775 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001776 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001777 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001778
Rafael Antognolli03b16592013-12-03 15:35:42 -02001779 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001780 return;
1781
Kristian Høgsberg7b83ae42014-04-29 14:50:25 -07001782 if (shsurf->grabbed)
1783 return;
1784
Kristian Høgsberge3148752013-05-06 23:19:49 -04001785 if (seat->pointer->button_count == 0 ||
1786 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001787 seat->pointer->focus == NULL)
1788 return;
1789
1790 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1791 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001792 return;
1793
Kristian Høgsberge3148752013-05-06 23:19:49 -04001794 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001795 wl_resource_post_no_memory(resource);
1796}
1797
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001798static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001799shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1800 struct wl_resource *seat_resource, uint32_t serial,
1801 uint32_t edges)
1802{
1803 common_surface_resize(resource, seat_resource, serial, edges);
1804}
1805
1806static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001807busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001808{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001809 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001810 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001811 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001812 wl_fixed_t sx, sy;
1813
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814 view = weston_compositor_pick_view(pointer->seat->compositor,
1815 pointer->x, pointer->y,
1816 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001817
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001818 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1819 shell_grab_end(grab);
1820 free(grab);
1821 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001822}
1823
1824static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001825busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1826 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001827{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001828 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001829}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001830
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001831static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001832busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001833 uint32_t time, uint32_t button, uint32_t state)
1834{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001835 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001836 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001837 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001838
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001839 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001840 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001841 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001842 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001843 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001844 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001845 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001846}
1847
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001848static void
1849busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1850{
1851 struct shell_grab *grab = (struct shell_grab *) base;
1852
1853 shell_grab_end(grab);
1854 free(grab);
1855}
1856
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001857static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001858 busy_cursor_grab_focus,
1859 busy_cursor_grab_motion,
1860 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001861 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001862};
1863
1864static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001865set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001866{
1867 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001868
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001869 if (pointer->grab->interface == &busy_cursor_grab_interface)
1870 return;
1871
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001872 grab = malloc(sizeof *grab);
1873 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001874 return;
1875
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001876 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1877 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001878 /* Mark the shsurf as ungrabbed so that button binding is able
1879 * to move it. */
1880 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001881}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001882
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001883static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001884end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001885{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001886 struct shell_grab *grab;
1887 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001888
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001889 wl_list_for_each(seat, &compositor->seat_list, link) {
1890 if (seat->pointer == NULL)
1891 continue;
1892
1893 grab = (struct shell_grab *) seat->pointer->grab;
1894 if (grab->grab.interface == &busy_cursor_grab_interface &&
1895 wl_resource_get_client(grab->shsurf->resource) == client) {
1896 shell_grab_end(grab);
1897 free(grab);
1898 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001899 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001900}
1901
Scott Moreau9521d5e2012-04-19 13:06:17 -06001902static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001903handle_shell_client_destroy(struct wl_listener *listener, void *data);
1904
1905static int
1906xdg_ping_timeout_handler(void *data)
1907{
1908 struct shell_client *sc = data;
1909 struct weston_seat *seat;
1910 struct shell_surface *shsurf;
1911
1912 /* Client is not responding */
1913 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001914 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1915 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1916 continue;
1917 if (seat->pointer->focus->surface->resource == NULL)
1918 continue;
1919
1920 shsurf = get_shell_surface(seat->pointer->focus->surface);
1921 if (shsurf &&
1922 wl_resource_get_client(shsurf->resource) == sc->client)
1923 set_busy_cursor(shsurf, seat->pointer);
1924 }
1925
1926 return 1;
1927}
1928
1929static void
1930handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1931{
1932 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05001933 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001934 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001935 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001936
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001937 if (sc->unresponsive) {
1938 xdg_ping_timeout_handler(sc);
1939 return;
1940 }
1941
1942 sc->ping_serial = serial;
1943 loop = wl_display_get_event_loop(compositor->wl_display);
1944 if (sc->ping_timer == NULL)
1945 sc->ping_timer =
1946 wl_event_loop_add_timer(loop,
1947 xdg_ping_timeout_handler, sc);
1948 if (sc->ping_timer == NULL)
1949 return;
1950
1951 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1952
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001953 if (shell_surface_is_xdg_surface(shsurf) ||
1954 shell_surface_is_xdg_popup(shsurf))
1955 xdg_shell_send_ping(sc->resource, serial);
1956 else if (shell_surface_is_wl_shell_surface(shsurf))
1957 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001958}
1959
Scott Moreauff1db4a2012-04-17 19:06:18 -06001960static void
1961ping_handler(struct weston_surface *surface, uint32_t serial)
1962{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001963 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001964
1965 if (!shsurf)
1966 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001967 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001968 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001969 if (shsurf->surface == shsurf->shell->grab_surface)
1970 return;
1971
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001972 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001973}
1974
1975static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001976handle_pointer_focus(struct wl_listener *listener, void *data)
1977{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001978 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001979 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001980 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001981 uint32_t serial;
1982
Jason Ekstranda7af7042013-10-12 22:38:11 -05001983 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001984 return;
1985
Jason Ekstranda7af7042013-10-12 22:38:11 -05001986 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001987 serial = wl_display_next_serial(compositor->wl_display);
1988 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001989}
1990
1991static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001992shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1993{
1994 if (--shsurf->focus_count == 0)
1995 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001996 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001997}
1998
1999static void
2000shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2001{
2002 if (shsurf->focus_count++ == 0)
2003 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05002004 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002005}
2006
2007static void
2008handle_keyboard_focus(struct wl_listener *listener, void *data)
2009{
2010 struct weston_keyboard *keyboard = data;
2011 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2012
2013 if (seat->focused_surface) {
2014 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2015 if (shsurf)
2016 shell_surface_lose_keyboard_focus(shsurf);
2017 }
2018
2019 seat->focused_surface = keyboard->focus;
2020
2021 if (seat->focused_surface) {
2022 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2023 if (shsurf)
2024 shell_surface_gain_keyboard_focus(shsurf);
2025 }
2026}
2027
2028static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002029shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002030{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002031 if (sc->ping_serial != serial)
2032 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002033
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002034 sc->unresponsive = 0;
2035 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002036
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002037 if (sc->ping_timer) {
2038 wl_event_source_remove(sc->ping_timer);
2039 sc->ping_timer = NULL;
2040 }
2041
2042}
2043
2044static void
2045shell_surface_pong(struct wl_client *client,
2046 struct wl_resource *resource, uint32_t serial)
2047{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002048 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2049 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002050
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002051 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002052}
2053
2054static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002055set_title(struct shell_surface *shsurf, const char *title)
2056{
2057 free(shsurf->title);
2058 shsurf->title = strdup(title);
2059}
2060
2061static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002062shell_surface_set_title(struct wl_client *client,
2063 struct wl_resource *resource, const char *title)
2064{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002065 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002066
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002067 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002068}
2069
2070static void
2071shell_surface_set_class(struct wl_client *client,
2072 struct wl_resource *resource, const char *class)
2073{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002074 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002075
2076 free(shsurf->class);
2077 shsurf->class = strdup(class);
2078}
2079
Alex Wu4539b082012-03-01 12:57:46 +08002080static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002081restore_output_mode(struct weston_output *output)
2082{
Hardening57388e42013-09-18 23:56:36 +02002083 if (output->current_mode != output->original_mode ||
2084 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002085 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002086 output->original_mode,
2087 output->original_scale,
2088 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002089}
2090
2091static void
2092restore_all_output_modes(struct weston_compositor *compositor)
2093{
2094 struct weston_output *output;
2095
2096 wl_list_for_each(output, &compositor->output_list, link)
2097 restore_output_mode(output);
2098}
2099
Philip Withnallbecb77e2013-11-25 18:01:30 +00002100static int
2101get_output_panel_height(struct desktop_shell *shell,
2102 struct weston_output *output)
2103{
2104 struct weston_view *view;
2105 int panel_height = 0;
2106
2107 if (!output)
2108 return 0;
2109
2110 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2111 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002112 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002113 break;
2114 }
2115 }
2116
2117 return panel_height;
2118}
2119
Philip Withnall07926d92013-11-25 18:01:40 +00002120/* The surface will be inserted into the list immediately after the link
2121 * returned by this function (i.e. will be stacked immediately above the
2122 * returned link). */
2123static struct wl_list *
2124shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2125{
2126 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002127 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002128
2129 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002130 case SHELL_SURFACE_POPUP:
2131 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002132 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002133 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002134 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002135 /* Move the surface to its parent layer so
2136 * that surfaces which are transient for
2137 * fullscreen surfaces don't get hidden by the
2138 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002139
2140 /* TODO: Handle a parent with multiple views */
2141 parent = get_default_view(shsurf->parent);
2142 if (parent)
2143 return parent->layer_link.prev;
2144 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002145 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002146
2147 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002148 return &shsurf->shell->fullscreen_layer.view_list;
2149
Philip Withnall07926d92013-11-25 18:01:40 +00002150 case SHELL_SURFACE_NONE:
2151 default:
2152 /* Go to the fallback, below. */
2153 break;
2154 }
2155
2156 /* Move the surface to a normal workspace layer so that surfaces
2157 * which were previously fullscreen or transient are no longer
2158 * rendered on top. */
2159 ws = get_current_workspace(shsurf->shell);
2160 return &ws->layer.view_list;
2161}
2162
Philip Withnall648a4dd2013-11-25 18:01:44 +00002163static void
2164shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2165{
2166 struct shell_surface *child;
2167
2168 /* Move the child layers to the same workspace as shsurf. They will be
2169 * stacked above shsurf. */
2170 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2171 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002172 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002173 weston_view_geometry_dirty(child->view);
2174 wl_list_remove(&child->view->layer_link);
2175 wl_list_insert(shsurf->view->layer_link.prev,
2176 &child->view->layer_link);
2177 weston_view_geometry_dirty(child->view);
2178 weston_surface_damage(child->surface);
2179
2180 /* Recurse. We don’t expect this to recurse very far (if
2181 * at all) because that would imply we have transient
2182 * (or popup) children of transient surfaces, which
2183 * would be unusual. */
2184 shell_surface_update_child_surface_layers(child);
2185 }
2186 }
2187}
2188
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002189/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002190 * geometry to ensure the changes are redrawn.
2191 *
2192 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2193 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002194static void
2195shell_surface_update_layer(struct shell_surface *shsurf)
2196{
2197 struct wl_list *new_layer_link;
2198
2199 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2200
2201 if (new_layer_link == &shsurf->view->layer_link)
2202 return;
2203
2204 weston_view_geometry_dirty(shsurf->view);
2205 wl_list_remove(&shsurf->view->layer_link);
2206 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2207 weston_view_geometry_dirty(shsurf->view);
2208 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002209
2210 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002211}
2212
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002213static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002214shell_surface_set_parent(struct shell_surface *shsurf,
2215 struct weston_surface *parent)
2216{
2217 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002218
2219 wl_list_remove(&shsurf->children_link);
2220 wl_list_init(&shsurf->children_link);
2221
2222 /* Insert into the parent surface’s child list. */
2223 if (parent != NULL) {
2224 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2225 if (parent_shsurf != NULL)
2226 wl_list_insert(&parent_shsurf->children_list,
2227 &shsurf->children_link);
2228 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002229}
2230
2231static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002232shell_surface_set_output(struct shell_surface *shsurf,
2233 struct weston_output *output)
2234{
2235 struct weston_surface *es = shsurf->surface;
2236
2237 /* get the default output, if the client set it as NULL
2238 check whether the ouput is available */
2239 if (output)
2240 shsurf->output = output;
2241 else if (es->output)
2242 shsurf->output = es->output;
2243 else
2244 shsurf->output = get_default_output(es->compositor);
2245}
2246
2247static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002248surface_clear_next_states(struct shell_surface *shsurf)
2249{
2250 shsurf->next_state.maximized = false;
2251 shsurf->next_state.fullscreen = false;
2252
2253 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2254 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2255 shsurf->state_changed = true;
2256}
2257
2258static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002259set_toplevel(struct shell_surface *shsurf)
2260{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002261 shell_surface_set_parent(shsurf, NULL);
2262 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002263 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002264
2265 /* The layer_link is updated in set_surface_type(),
2266 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002267}
2268
2269static void
2270shell_surface_set_toplevel(struct wl_client *client,
2271 struct wl_resource *resource)
2272{
2273 struct shell_surface *surface = wl_resource_get_user_data(resource);
2274
2275 set_toplevel(surface);
2276}
2277
2278static void
2279set_transient(struct shell_surface *shsurf,
2280 struct weston_surface *parent, int x, int y, uint32_t flags)
2281{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002282 assert(parent != NULL);
2283
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002284 shell_surface_set_parent(shsurf, parent);
2285
2286 surface_clear_next_states(shsurf);
2287
Philip Withnallbecb77e2013-11-25 18:01:30 +00002288 shsurf->transient.x = x;
2289 shsurf->transient.y = y;
2290 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002291
Rafael Antognollied207b42013-12-03 15:35:43 -02002292 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002293 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002294 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002295
2296 /* The layer_link is updated in set_surface_type(),
2297 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002298}
2299
2300static void
2301shell_surface_set_transient(struct wl_client *client,
2302 struct wl_resource *resource,
2303 struct wl_resource *parent_resource,
2304 int x, int y, uint32_t flags)
2305{
2306 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2307 struct weston_surface *parent =
2308 wl_resource_get_user_data(parent_resource);
2309
2310 set_transient(shsurf, parent, x, y, flags);
2311}
2312
2313static void
2314set_fullscreen(struct shell_surface *shsurf,
2315 uint32_t method,
2316 uint32_t framerate,
2317 struct weston_output *output)
2318{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002319 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002320
2321 shsurf->fullscreen_output = shsurf->output;
2322 shsurf->fullscreen.type = method;
2323 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002324
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002325 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002326
2327 shsurf->client->send_configure(shsurf->surface, 0,
2328 shsurf->output->width,
2329 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002330
2331 /* The layer_link is updated in set_surface_type(),
2332 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002333}
2334
2335static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002336weston_view_set_initial_position(struct weston_view *view,
2337 struct desktop_shell *shell);
2338
2339static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002340unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002341{
Philip Withnallf85fe842013-11-25 18:01:37 +00002342 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002343 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2344 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002345 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002346 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002347
Alex Wu4539b082012-03-01 12:57:46 +08002348 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2349 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002350
Alex Wu4539b082012-03-01 12:57:46 +08002351 wl_list_remove(&shsurf->fullscreen.transform.link);
2352 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002353
Jason Ekstranda7af7042013-10-12 22:38:11 -05002354 if (shsurf->fullscreen.black_view)
2355 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2356 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002357
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002358 if (shsurf->saved_position_valid)
2359 weston_view_set_position(shsurf->view,
2360 shsurf->saved_x, shsurf->saved_y);
2361 else
2362 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2363
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002364 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002365 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002366 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002367 shsurf->saved_rotation_valid = false;
2368 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002369
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002370 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002371}
2372
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002373static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002374shell_surface_set_fullscreen(struct wl_client *client,
2375 struct wl_resource *resource,
2376 uint32_t method,
2377 uint32_t framerate,
2378 struct wl_resource *output_resource)
2379{
2380 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2381 struct weston_output *output;
2382
2383 if (output_resource)
2384 output = wl_resource_get_user_data(output_resource);
2385 else
2386 output = NULL;
2387
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002388 shell_surface_set_parent(shsurf, NULL);
2389
Rafael Antognolli03b16592013-12-03 15:35:42 -02002390 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002391 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002392
2393 shsurf->next_state.fullscreen = true;
2394 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002395}
2396
2397static void
2398set_popup(struct shell_surface *shsurf,
2399 struct weston_surface *parent,
2400 struct weston_seat *seat,
2401 uint32_t serial,
2402 int32_t x,
2403 int32_t y)
2404{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002405 assert(parent != NULL);
2406
Philip Withnallbecb77e2013-11-25 18:01:30 +00002407 shsurf->popup.shseat = get_shell_seat(seat);
2408 shsurf->popup.serial = serial;
2409 shsurf->popup.x = x;
2410 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002411
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002412 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002413}
2414
2415static void
2416shell_surface_set_popup(struct wl_client *client,
2417 struct wl_resource *resource,
2418 struct wl_resource *seat_resource,
2419 uint32_t serial,
2420 struct wl_resource *parent_resource,
2421 int32_t x, int32_t y, uint32_t flags)
2422{
2423 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002424 struct weston_surface *parent =
2425 wl_resource_get_user_data(parent_resource);
2426
2427 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002428
Rafael Antognolli03b16592013-12-03 15:35:42 -02002429 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002430 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002431 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002432 wl_resource_get_user_data(seat_resource),
2433 serial, x, y);
2434}
2435
2436static void
2437set_maximized(struct shell_surface *shsurf,
2438 struct weston_output *output)
2439{
2440 struct desktop_shell *shell;
2441 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002442
Philip Withnall352e7ed2013-11-25 18:01:35 +00002443 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002444
2445 shell = shell_surface_get_shell(shsurf);
2446 panel_height = get_output_panel_height(shell, shsurf->output);
2447 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2448
2449 shsurf->client->send_configure(shsurf->surface, edges,
2450 shsurf->output->width,
2451 shsurf->output->height - panel_height);
2452
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002453 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002454}
2455
2456static void
2457unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002458{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002459 /* undo all maximized things here */
2460 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002461
2462 if (shsurf->saved_position_valid)
2463 weston_view_set_position(shsurf->view,
2464 shsurf->saved_x, shsurf->saved_y);
2465 else
2466 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002467
2468 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002469 wl_list_insert(&shsurf->view->geometry.transformation_list,
2470 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002471 shsurf->saved_rotation_valid = false;
2472 }
2473
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002474 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002475}
2476
Philip Withnallbecb77e2013-11-25 18:01:30 +00002477static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002478set_minimized(struct weston_surface *surface, uint32_t is_true)
2479{
2480 struct shell_surface *shsurf;
2481 struct workspace *current_ws;
2482 struct weston_seat *seat;
2483 struct weston_surface *focus;
2484 struct weston_view *view;
2485
2486 view = get_default_view(surface);
2487 if (!view)
2488 return;
2489
2490 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2491
2492 shsurf = get_shell_surface(surface);
2493 current_ws = get_current_workspace(shsurf->shell);
2494
2495 wl_list_remove(&view->layer_link);
2496 /* hide or show, depending on the state */
2497 if (is_true) {
2498 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2499
2500 drop_focus_state(shsurf->shell, current_ws, view->surface);
2501 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2502 if (!seat->keyboard)
2503 continue;
2504 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2505 if (focus == view->surface)
2506 weston_keyboard_set_focus(seat->keyboard, NULL);
2507 }
2508 }
2509 else {
2510 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2511
2512 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2513 if (!seat->keyboard)
2514 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002515 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002516 }
2517 }
2518
2519 shell_surface_update_child_surface_layers(shsurf);
2520
2521 weston_view_damage_below(view);
2522}
2523
2524static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002525shell_surface_set_maximized(struct wl_client *client,
2526 struct wl_resource *resource,
2527 struct wl_resource *output_resource)
2528{
2529 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2530 struct weston_output *output;
2531
2532 if (output_resource)
2533 output = wl_resource_get_user_data(output_resource);
2534 else
2535 output = NULL;
2536
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002537 shell_surface_set_parent(shsurf, NULL);
2538
Rafael Antognolli03b16592013-12-03 15:35:42 -02002539 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002540 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002541
2542 shsurf->next_state.maximized = true;
2543 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002544}
2545
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002546/* This is only ever called from set_surface_type(), so there’s no need to
2547 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002548static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002549reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002550{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002551 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002552 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002553 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002554 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002555
Pekka Paalanen98262232011-12-01 10:42:22 +02002556 return 0;
2557}
2558
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002559static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002560set_full_output(struct shell_surface *shsurf)
2561{
2562 shsurf->saved_x = shsurf->view->geometry.x;
2563 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002564 shsurf->saved_width = shsurf->surface->width;
2565 shsurf->saved_height = shsurf->surface->height;
2566 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002567 shsurf->saved_position_valid = true;
2568
2569 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2570 wl_list_remove(&shsurf->rotation.transform.link);
2571 wl_list_init(&shsurf->rotation.transform.link);
2572 weston_view_geometry_dirty(shsurf->view);
2573 shsurf->saved_rotation_valid = true;
2574 }
2575}
2576
2577static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002578set_surface_type(struct shell_surface *shsurf)
2579{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002580 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002581 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002582
Philip Withnallbecb77e2013-11-25 18:01:30 +00002583 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002584
Rafael Antognolli03b16592013-12-03 15:35:42 -02002585 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002586 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002587
2588 switch (shsurf->type) {
2589 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002590 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002591 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002592 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593 weston_view_set_position(shsurf->view,
2594 pev->geometry.x + shsurf->transient.x,
2595 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002596 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002597 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002598
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002599 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002600 weston_view_set_position(shsurf->view, shsurf->transient.x,
2601 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002602 break;
2603
Philip Withnall0f640e22013-11-25 18:01:31 +00002604 case SHELL_SURFACE_POPUP:
2605 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002606 default:
2607 break;
2608 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002609
2610 /* Update the surface’s layer. */
2611 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002612}
2613
Tiago Vignattibe143262012-04-16 17:31:41 +03002614static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002615shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002616{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002617 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002618}
2619
Alex Wu21858432012-04-01 20:13:08 +08002620static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002621black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002622
Jason Ekstranda7af7042013-10-12 22:38:11 -05002623static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002624create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002625 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002626 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002627{
2628 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002629 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002630
2631 surface = weston_surface_create(ec);
2632 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002633 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002634 return NULL;
2635 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002636 view = weston_view_create(surface);
2637 if (surface == NULL) {
2638 weston_log("no memory\n");
2639 weston_surface_destroy(surface);
2640 return NULL;
2641 }
Alex Wu4539b082012-03-01 12:57:46 +08002642
Alex Wu21858432012-04-01 20:13:08 +08002643 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002644 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002645 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002646 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002647 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002648 pixman_region32_fini(&surface->input);
2649 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002650
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002651 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002652 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002653
2654 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002655}
2656
Philip Withnalled8f1a92013-11-25 18:01:43 +00002657static void
2658shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2659{
2660 struct weston_output *output = shsurf->fullscreen_output;
2661
Rafael Antognolli03b16592013-12-03 15:35:42 -02002662 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002663
2664 if (!shsurf->fullscreen.black_view)
2665 shsurf->fullscreen.black_view =
2666 create_black_surface(shsurf->surface->compositor,
2667 shsurf->surface,
2668 output->x, output->y,
2669 output->width,
2670 output->height);
2671
2672 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2673 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2674 wl_list_insert(&shsurf->view->layer_link,
2675 &shsurf->fullscreen.black_view->layer_link);
2676 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2677 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002678
2679 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002680}
2681
Alex Wu4539b082012-03-01 12:57:46 +08002682/* Create black surface and append it to the associated fullscreen surface.
2683 * Handle size dismatch and positioning according to the method. */
2684static void
2685shell_configure_fullscreen(struct shell_surface *shsurf)
2686{
2687 struct weston_output *output = shsurf->fullscreen_output;
2688 struct weston_surface *surface = shsurf->surface;
2689 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002690 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002691 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002692
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002693 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2694 restore_output_mode(output);
2695
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002696 /* Reverse the effect of lower_fullscreen_layer() */
2697 wl_list_remove(&shsurf->view->layer_link);
2698 wl_list_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2699
Philip Withnalled8f1a92013-11-25 18:01:43 +00002700 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002701
Jason Ekstranda7af7042013-10-12 22:38:11 -05002702 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002703 &surf_width, &surf_height);
2704
Alex Wu4539b082012-03-01 12:57:46 +08002705 switch (shsurf->fullscreen.type) {
2706 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002707 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002708 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002709 break;
2710 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002711 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002712 if (output->width == surf_width &&
2713 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002714 weston_view_set_position(shsurf->view,
2715 output->x - surf_x,
2716 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002717 break;
2718 }
2719
Alex Wu4539b082012-03-01 12:57:46 +08002720 matrix = &shsurf->fullscreen.transform.matrix;
2721 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002722
Scott Moreau1bad5db2012-08-18 01:04:05 -06002723 output_aspect = (float) output->width /
2724 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002725 /* XXX: Use surf_width and surf_height here? */
2726 surface_aspect = (float) surface->width /
2727 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002728 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002729 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002730 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002731 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002732 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002733 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002734
Alex Wu4539b082012-03-01 12:57:46 +08002735 weston_matrix_scale(matrix, scale, scale, 1);
2736 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002737 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002738 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002739 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2740 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002741 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002742
Alex Wu4539b082012-03-01 12:57:46 +08002743 break;
2744 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002745 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002746 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002747 surf_width * surface->buffer_viewport.buffer.scale,
2748 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002749 shsurf->fullscreen.framerate};
2750
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002751 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002752 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002753 weston_view_set_position(shsurf->view,
2754 output->x - surf_x,
2755 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002756 shsurf->fullscreen.black_view->surface->width = output->width;
2757 shsurf->fullscreen.black_view->surface->height = output->height;
2758 weston_view_set_position(shsurf->fullscreen.black_view,
2759 output->x - surf_x,
2760 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002761 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002762 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002763 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002764 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002765 }
Alex Wubd3354b2012-04-17 17:20:49 +08002766 }
Alex Wu4539b082012-03-01 12:57:46 +08002767 break;
2768 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002769 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002770 break;
2771 default:
2772 break;
2773 }
2774}
2775
Alex Wu4539b082012-03-01 12:57:46 +08002776static void
2777shell_map_fullscreen(struct shell_surface *shsurf)
2778{
Alex Wubd3354b2012-04-17 17:20:49 +08002779 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002780}
2781
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002782static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002783set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2784{
2785 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002786 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002787 shsurf->transient.x = x;
2788 shsurf->transient.y = y;
2789 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002790
2791 shell_surface_set_parent(shsurf, NULL);
2792
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002793 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002794 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002795}
2796
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002797static int
2798shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2799{
2800 return surface_move(shsurf, ws, 1);
2801}
2802
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002803static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002804
2805static void
2806destroy_shell_seat(struct wl_listener *listener, void *data)
2807{
2808 struct shell_seat *shseat =
2809 container_of(listener,
2810 struct shell_seat, seat_destroy_listener);
2811 struct shell_surface *shsurf, *prev = NULL;
2812
2813 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002814 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002815 shseat->popup_grab.client = NULL;
2816
2817 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2818 shsurf->popup.shseat = NULL;
2819 if (prev) {
2820 wl_list_init(&prev->popup.grab_link);
2821 }
2822 prev = shsurf;
2823 }
2824 wl_list_init(&prev->popup.grab_link);
2825 }
2826
2827 wl_list_remove(&shseat->seat_destroy_listener.link);
2828 free(shseat);
2829}
2830
Jason Ekstrand024177c2014-04-21 19:42:58 -05002831static void
2832shell_seat_caps_changed(struct wl_listener *listener, void *data)
2833{
2834 struct shell_seat *seat;
2835
2836 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2837
2838 if (seat->seat->keyboard &&
2839 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2840 wl_signal_add(&seat->seat->keyboard->focus_signal,
2841 &seat->keyboard_focus_listener);
2842 } else if (!seat->seat->keyboard) {
2843 wl_list_init(&seat->keyboard_focus_listener.link);
2844 }
2845
2846 if (seat->seat->pointer &&
2847 wl_list_empty(&seat->pointer_focus_listener.link)) {
2848 wl_signal_add(&seat->seat->pointer->focus_signal,
2849 &seat->pointer_focus_listener);
2850 } else if (!seat->seat->pointer) {
2851 wl_list_init(&seat->pointer_focus_listener.link);
2852 }
2853}
2854
Giulio Camuffo5085a752013-03-25 21:42:45 +01002855static struct shell_seat *
2856create_shell_seat(struct weston_seat *seat)
2857{
2858 struct shell_seat *shseat;
2859
2860 shseat = calloc(1, sizeof *shseat);
2861 if (!shseat) {
2862 weston_log("no memory to allocate shell seat\n");
2863 return NULL;
2864 }
2865
2866 shseat->seat = seat;
2867 wl_list_init(&shseat->popup_grab.surfaces_list);
2868
2869 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2870 wl_signal_add(&seat->destroy_signal,
2871 &shseat->seat_destroy_listener);
2872
Jason Ekstrand024177c2014-04-21 19:42:58 -05002873 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2874 wl_list_init(&shseat->keyboard_focus_listener.link);
2875
2876 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2877 wl_list_init(&shseat->pointer_focus_listener.link);
2878
2879 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2880 wl_signal_add(&seat->updated_caps_signal,
2881 &shseat->caps_changed_listener);
2882 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2883
Giulio Camuffo5085a752013-03-25 21:42:45 +01002884 return shseat;
2885}
2886
2887static struct shell_seat *
2888get_shell_seat(struct weston_seat *seat)
2889{
2890 struct wl_listener *listener;
2891
2892 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002893 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002894
2895 return container_of(listener,
2896 struct shell_seat, seat_destroy_listener);
2897}
2898
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002899static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002900popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002901{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002902 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002903 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002904 struct shell_seat *shseat =
2905 container_of(grab, struct shell_seat, popup_grab.grab);
2906 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002907 wl_fixed_t sx, sy;
2908
Jason Ekstranda7af7042013-10-12 22:38:11 -05002909 view = weston_compositor_pick_view(pointer->seat->compositor,
2910 pointer->x, pointer->y,
2911 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002912
Jason Ekstranda7af7042013-10-12 22:38:11 -05002913 if (view && view->surface->resource &&
2914 wl_resource_get_client(view->surface->resource) == client) {
2915 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002916 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002917 weston_pointer_set_focus(pointer, NULL,
2918 wl_fixed_from_int(0),
2919 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002920 }
2921}
2922
2923static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002924popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2925 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002926{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002927 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002928 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002929 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002930
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002931 weston_pointer_move(pointer, x, y);
2932
Neil Roberts96d790e2013-09-19 17:32:00 +01002933 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002934 weston_view_from_global_fixed(pointer->focus,
2935 pointer->x, pointer->y,
2936 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002937 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002938 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002939}
2940
2941static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002942popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002943 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002944{
2945 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002946 struct shell_seat *shseat =
2947 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002948 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002949 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002950 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002951 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002952
Neil Roberts96d790e2013-09-19 17:32:00 +01002953 resource_list = &grab->pointer->focus_resource_list;
2954 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002955 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002956 wl_resource_for_each(resource, resource_list) {
2957 wl_pointer_send_button(resource, serial,
2958 time, button, state);
2959 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002960 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002961 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002962 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002963 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002964 }
2965
Daniel Stone4dbadb12012-05-30 16:31:51 +01002966 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002967 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002968}
2969
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002970static void
2971popup_grab_cancel(struct weston_pointer_grab *grab)
2972{
2973 popup_grab_end(grab->pointer);
2974}
2975
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002976static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002977 popup_grab_focus,
2978 popup_grab_motion,
2979 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002980 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002981};
2982
2983static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002984shell_surface_send_popup_done(struct shell_surface *shsurf)
2985{
2986 if (shell_surface_is_wl_shell_surface(shsurf))
2987 wl_shell_surface_send_popup_done(shsurf->resource);
2988 else if (shell_surface_is_xdg_popup(shsurf))
2989 xdg_popup_send_popup_done(shsurf->resource,
2990 shsurf->popup.serial);
2991}
2992
2993static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002994popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002995{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002996 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002997 struct shell_seat *shseat =
2998 container_of(grab, struct shell_seat, popup_grab.grab);
2999 struct shell_surface *shsurf;
3000 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003001
3002 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003003 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003004 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003005 shseat->popup_grab.grab.interface = NULL;
3006 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003007 /* Send the popup_done event to all the popups open */
3008 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003009 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003010 shsurf->popup.shseat = NULL;
3011 if (prev) {
3012 wl_list_init(&prev->popup.grab_link);
3013 }
3014 prev = shsurf;
3015 }
3016 wl_list_init(&prev->popup.grab_link);
3017 wl_list_init(&shseat->popup_grab.surfaces_list);
3018 }
3019}
3020
3021static void
3022add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3023{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003024 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003025
3026 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003027 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003028 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003029 /* We must make sure here that this popup was opened after
3030 * a mouse press, and not just by moving around with other
3031 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003032 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003033 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003034
Rob Bradforddfe31052013-06-26 19:49:11 +01003035 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003036 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003037 } else {
3038 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003039 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003040}
3041
3042static void
3043remove_popup_grab(struct shell_surface *shsurf)
3044{
3045 struct shell_seat *shseat = shsurf->popup.shseat;
3046
3047 wl_list_remove(&shsurf->popup.grab_link);
3048 wl_list_init(&shsurf->popup.grab_link);
3049 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003050 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003051 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003052 }
3053}
3054
3055static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003056shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003057{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003058 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003059 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003060
Jason Ekstranda7af7042013-10-12 22:38:11 -05003061 shsurf->surface->output = parent_view->output;
3062 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003063
Jason Ekstranda7af7042013-10-12 22:38:11 -05003064 weston_view_set_transform_parent(shsurf->view, parent_view);
3065 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3066 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003067
Kristian Høgsberge3148752013-05-06 23:19:49 -04003068 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003069 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003070 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003071 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003072 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003073 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003074}
3075
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003076static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003077 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003078 shell_surface_move,
3079 shell_surface_resize,
3080 shell_surface_set_toplevel,
3081 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003082 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003083 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003084 shell_surface_set_maximized,
3085 shell_surface_set_title,
3086 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003087};
3088
3089static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003090destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003091{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003092 struct shell_surface *child, *next;
3093
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003094 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3095
Giulio Camuffo5085a752013-03-25 21:42:45 +01003096 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3097 remove_popup_grab(shsurf);
3098 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003099
Alex Wubd3354b2012-04-17 17:20:49 +08003100 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003101 shell_surface_is_top_fullscreen(shsurf))
3102 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003103
Jason Ekstranda7af7042013-10-12 22:38:11 -05003104 if (shsurf->fullscreen.black_view)
3105 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003106
Alex Wubd3354b2012-04-17 17:20:49 +08003107 /* As destroy_resource() use wl_list_for_each_safe(),
3108 * we can always remove the listener.
3109 */
3110 wl_list_remove(&shsurf->surface_destroy_listener.link);
3111 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003112 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003113
Jason Ekstranda7af7042013-10-12 22:38:11 -05003114 weston_view_destroy(shsurf->view);
3115
Philip Withnall648a4dd2013-11-25 18:01:44 +00003116 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003117 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3118 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003119
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003120 wl_list_remove(&shsurf->link);
3121 free(shsurf);
3122}
3123
3124static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003125shell_destroy_shell_surface(struct wl_resource *resource)
3126{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003127 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003128
Bryan Caina46b9462014-04-04 17:41:24 -05003129 if (!wl_list_empty(&shsurf->popup.grab_link))
3130 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003131 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003132}
3133
3134static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003135shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003136{
3137 struct shell_surface *shsurf = container_of(listener,
3138 struct shell_surface,
3139 surface_destroy_listener);
3140
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003141 if (shsurf->resource)
3142 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003143
3144 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003145}
3146
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003147static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003148fade_out_done(struct weston_view_animation *animation, void *data)
3149{
3150 struct shell_surface *shsurf = data;
3151
3152 weston_surface_destroy(shsurf->surface);
3153}
3154
3155static void
3156handle_resource_destroy(struct wl_listener *listener, void *data)
3157{
3158 struct shell_surface *shsurf =
3159 container_of(listener, struct shell_surface,
3160 resource_destroy_listener);
3161
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003162 if (!weston_surface_is_mapped(shsurf->surface))
3163 return;
3164
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003165 shsurf->surface->ref_count++;
3166
3167 pixman_region32_fini(&shsurf->surface->pending.input);
3168 pixman_region32_init(&shsurf->surface->pending.input);
3169 pixman_region32_fini(&shsurf->surface->input);
3170 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003171 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3172 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003173}
3174
3175static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003176shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003177
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003178struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003179get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003180{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003181 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003182 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003183 else
3184 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003185}
3186
Rafael Antognollie2a34552013-12-03 15:35:45 -02003187static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003188create_common_surface(struct shell_client *owner, void *shell,
3189 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003190 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003191{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003192 struct shell_surface *shsurf;
3193
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003194 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003195 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003196 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003197 }
3198
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003199 shsurf = calloc(1, sizeof *shsurf);
3200 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003201 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003202 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003203 }
3204
Jason Ekstranda7af7042013-10-12 22:38:11 -05003205 shsurf->view = weston_view_create(surface);
3206 if (!shsurf->view) {
3207 weston_log("no memory to allocate shell surface\n");
3208 free(shsurf);
3209 return NULL;
3210 }
3211
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003212 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003213 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003214
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003215 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3216 wl_resource_add_destroy_listener(surface->resource,
3217 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003218 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003219
Tiago Vignattibc052c92012-04-19 16:18:18 +03003220 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003221 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003222 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003223 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003224 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003225 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003226 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3227 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003228 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003229 wl_list_init(&shsurf->fullscreen.transform.link);
3230
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003231 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003232 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003233 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003234 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003235
3236 /* init link so its safe to always remove it in destroy_shell_surface */
3237 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003238 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003239
Pekka Paalanen460099f2012-01-20 16:48:25 +02003240 /* empty when not in use */
3241 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003242 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003243
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003244 wl_list_init(&shsurf->workspace_transform.link);
3245
Philip Withnall648a4dd2013-11-25 18:01:44 +00003246 wl_list_init(&shsurf->children_link);
3247 wl_list_init(&shsurf->children_list);
3248 shsurf->parent = NULL;
3249
Pekka Paalanen98262232011-12-01 10:42:22 +02003250 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003251
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003252 shsurf->client = client;
3253
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003254 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003255}
3256
Rafael Antognollie2a34552013-12-03 15:35:45 -02003257static struct shell_surface *
3258create_shell_surface(void *shell, struct weston_surface *surface,
3259 const struct weston_shell_client *client)
3260{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003261 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003262}
3263
Jason Ekstranda7af7042013-10-12 22:38:11 -05003264static struct weston_view *
3265get_primary_view(void *shell, struct shell_surface *shsurf)
3266{
3267 return shsurf->view;
3268}
3269
Tiago Vignattibc052c92012-04-19 16:18:18 +03003270static void
3271shell_get_shell_surface(struct wl_client *client,
3272 struct wl_resource *resource,
3273 uint32_t id,
3274 struct wl_resource *surface_resource)
3275{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003276 struct weston_surface *surface =
3277 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003278 struct shell_client *sc = wl_resource_get_user_data(resource);
3279 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003280 struct shell_surface *shsurf;
3281
3282 if (get_shell_surface(surface)) {
3283 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003284 WL_DISPLAY_ERROR_INVALID_OBJECT,
3285 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003286 return;
3287 }
3288
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003289 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003290 if (!shsurf) {
3291 wl_resource_post_error(surface_resource,
3292 WL_DISPLAY_ERROR_INVALID_OBJECT,
3293 "surface->configure already set");
3294 return;
3295 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003296
Jason Ekstranda85118c2013-06-27 20:17:02 -05003297 shsurf->resource =
3298 wl_resource_create(client,
3299 &wl_shell_surface_interface, 1, id);
3300 wl_resource_set_implementation(shsurf->resource,
3301 &shell_surface_implementation,
3302 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003303}
3304
Rafael Antognollie2a34552013-12-03 15:35:45 -02003305static bool
3306shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3307{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003308 /* A shell surface without a resource is created from xwayland
3309 * and is considered a wl_shell surface for now. */
3310
3311 return shsurf->resource == NULL ||
3312 wl_resource_instance_of(shsurf->resource,
3313 &wl_shell_surface_interface,
3314 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003315}
3316
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003317static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003318 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003319};
3320
Rafael Antognollie2a34552013-12-03 15:35:45 -02003321/****************************
3322 * xdg-shell implementation */
3323
3324static void
3325xdg_surface_destroy(struct wl_client *client,
3326 struct wl_resource *resource)
3327{
3328 wl_resource_destroy(resource);
3329}
3330
3331static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003332xdg_surface_set_transient_for(struct wl_client *client,
3333 struct wl_resource *resource,
3334 struct wl_resource *parent_resource)
3335{
3336 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3337 struct weston_surface *parent;
3338
3339 if (parent_resource)
3340 parent = wl_resource_get_user_data(parent_resource);
3341 else
3342 parent = NULL;
3343
3344 shell_surface_set_parent(shsurf, parent);
3345}
3346
3347static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003348xdg_surface_set_margin(struct wl_client *client,
3349 struct wl_resource *resource,
3350 int32_t left,
3351 int32_t right,
3352 int32_t top,
3353 int32_t bottom)
3354{
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003355 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3356
3357 shsurf->margin.left = left;
3358 shsurf->margin.right = right;
3359 shsurf->margin.top = top;
3360 shsurf->margin.bottom = bottom;
Jasper St. Pierre74073452014-02-01 18:36:41 -05003361}
3362
3363static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003364xdg_surface_set_app_id(struct wl_client *client,
3365 struct wl_resource *resource,
3366 const char *app_id)
3367{
3368 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3369
3370 free(shsurf->class);
3371 shsurf->class = strdup(app_id);
3372}
3373
3374static void
3375xdg_surface_set_title(struct wl_client *client,
3376 struct wl_resource *resource, const char *title)
3377{
3378 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3379
3380 set_title(shsurf, title);
3381}
3382
3383static void
3384xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3385 struct wl_resource *seat_resource, uint32_t serial)
3386{
3387 common_surface_move(resource, seat_resource, serial);
3388}
3389
3390static void
3391xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3392 struct wl_resource *seat_resource, uint32_t serial,
3393 uint32_t edges)
3394{
3395 common_surface_resize(resource, seat_resource, serial, edges);
3396}
3397
3398static void
3399xdg_surface_set_output(struct wl_client *client,
3400 struct wl_resource *resource,
3401 struct wl_resource *output_resource)
3402{
3403 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3404 struct weston_output *output;
3405
3406 if (output_resource)
3407 output = wl_resource_get_user_data(output_resource);
3408 else
3409 output = NULL;
3410
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003411 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003412}
3413
3414static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003415xdg_surface_change_state(struct shell_surface *shsurf,
3416 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003417{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003418 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003419 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003420
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003421 switch (state) {
3422 case XDG_SURFACE_STATE_MAXIMIZED:
3423 shsurf->requested_state.maximized = value;
3424 if (value)
3425 set_maximized(shsurf, NULL);
3426 break;
3427 case XDG_SURFACE_STATE_FULLSCREEN:
3428 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003429
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003430 if (value)
3431 set_fullscreen(shsurf,
3432 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3433 0, shsurf->recommended_output);
3434 break;
3435 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003436}
3437
3438static void
3439xdg_surface_request_change_state(struct wl_client *client,
3440 struct wl_resource *resource,
3441 uint32_t state,
3442 uint32_t value,
3443 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003444{
3445 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003446
3447 /* The client can't know what the current state is, so we need
3448 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003449
3450 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3451 return;
3452
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003453 switch (state) {
3454 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003455 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003456 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003457 default:
3458 /* send error? ignore? send change state with value 0? */
3459 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003460 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003461
3462 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003463}
3464
3465static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003466xdg_surface_ack_change_state(struct wl_client *client,
3467 struct wl_resource *resource,
3468 uint32_t state,
3469 uint32_t value,
3470 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003471{
3472 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3473
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003474 if (shsurf->state_requested) {
3475 shsurf->next_state = shsurf->requested_state;
3476 shsurf->state_changed = true;
3477 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003478 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003479}
3480
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003481static void
3482xdg_surface_set_minimized(struct wl_client *client,
3483 struct wl_resource *resource)
3484{
3485 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3486
3487 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3488 return;
3489
3490 /* apply compositor's own minimization logic (hide) */
3491 set_minimized(shsurf->surface, 1);
3492}
3493
Rafael Antognollie2a34552013-12-03 15:35:45 -02003494static const struct xdg_surface_interface xdg_surface_implementation = {
3495 xdg_surface_destroy,
3496 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003497 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003498 xdg_surface_set_title,
3499 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003500 xdg_surface_move,
3501 xdg_surface_resize,
3502 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003503 xdg_surface_request_change_state,
3504 xdg_surface_ack_change_state,
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003505 xdg_surface_set_minimized
Rafael Antognollie2a34552013-12-03 15:35:45 -02003506};
3507
3508static void
3509xdg_send_configure(struct weston_surface *surface,
3510 uint32_t edges, int32_t width, int32_t height)
3511{
3512 struct shell_surface *shsurf = get_shell_surface(surface);
3513
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003514 assert(shsurf);
3515
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003516 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003517}
3518
3519static const struct weston_shell_client xdg_client = {
3520 xdg_send_configure
3521};
3522
3523static void
3524xdg_use_unstable_version(struct wl_client *client,
3525 struct wl_resource *resource,
3526 int32_t version)
3527{
3528 if (version > 1) {
3529 wl_resource_post_error(resource,
3530 1,
3531 "xdg-shell:: version not implemented yet.");
3532 return;
3533 }
3534}
3535
3536static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003537create_xdg_surface(struct shell_client *owner, void *shell,
3538 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003539 const struct weston_shell_client *client)
3540{
3541 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003542
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003543 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003544 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003545
3546 return shsurf;
3547}
3548
3549static void
3550xdg_get_xdg_surface(struct wl_client *client,
3551 struct wl_resource *resource,
3552 uint32_t id,
3553 struct wl_resource *surface_resource)
3554{
3555 struct weston_surface *surface =
3556 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003557 struct shell_client *sc = wl_resource_get_user_data(resource);
3558 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003559 struct shell_surface *shsurf;
3560
3561 if (get_shell_surface(surface)) {
3562 wl_resource_post_error(surface_resource,
3563 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003564 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003565 return;
3566 }
3567
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003568 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003569 if (!shsurf) {
3570 wl_resource_post_error(surface_resource,
3571 WL_DISPLAY_ERROR_INVALID_OBJECT,
3572 "surface->configure already set");
3573 return;
3574 }
3575
3576 shsurf->resource =
3577 wl_resource_create(client,
3578 &xdg_surface_interface, 1, id);
3579 wl_resource_set_implementation(shsurf->resource,
3580 &xdg_surface_implementation,
3581 shsurf, shell_destroy_shell_surface);
3582}
3583
3584static bool
3585shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3586{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003587 return shsurf->resource &&
3588 wl_resource_instance_of(shsurf->resource,
3589 &xdg_surface_interface,
3590 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003591}
3592
3593/* xdg-popup implementation */
3594
3595static void
3596xdg_popup_destroy(struct wl_client *client,
3597 struct wl_resource *resource)
3598{
3599 wl_resource_destroy(resource);
3600}
3601
Rafael Antognollie2a34552013-12-03 15:35:45 -02003602static const struct xdg_popup_interface xdg_popup_implementation = {
3603 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003604};
3605
3606static void
3607xdg_popup_send_configure(struct weston_surface *surface,
3608 uint32_t edges, int32_t width, int32_t height)
3609{
3610}
3611
3612static const struct weston_shell_client xdg_popup_client = {
3613 xdg_popup_send_configure
3614};
3615
3616static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003617create_xdg_popup(struct shell_client *owner, void *shell,
3618 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003619 const struct weston_shell_client *client,
3620 struct weston_surface *parent,
3621 struct shell_seat *seat,
3622 uint32_t serial,
3623 int32_t x, int32_t y)
3624{
3625 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003626
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003627 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003628 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003629 shsurf->popup.shseat = seat;
3630 shsurf->popup.serial = serial;
3631 shsurf->popup.x = x;
3632 shsurf->popup.y = y;
3633 shell_surface_set_parent(shsurf, parent);
3634
3635 return shsurf;
3636}
3637
3638static void
3639xdg_get_xdg_popup(struct wl_client *client,
3640 struct wl_resource *resource,
3641 uint32_t id,
3642 struct wl_resource *surface_resource,
3643 struct wl_resource *parent_resource,
3644 struct wl_resource *seat_resource,
3645 uint32_t serial,
3646 int32_t x, int32_t y, uint32_t flags)
3647{
3648 struct weston_surface *surface =
3649 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003650 struct shell_client *sc = wl_resource_get_user_data(resource);
3651 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003652 struct shell_surface *shsurf;
3653 struct weston_surface *parent;
3654 struct shell_seat *seat;
3655
3656 if (get_shell_surface(surface)) {
3657 wl_resource_post_error(surface_resource,
3658 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003659 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003660 return;
3661 }
3662
3663 if (!parent_resource) {
3664 wl_resource_post_error(surface_resource,
3665 WL_DISPLAY_ERROR_INVALID_OBJECT,
3666 "xdg_shell::get_xdg_popup requires a parent shell surface");
3667 }
3668
3669 parent = wl_resource_get_user_data(parent_resource);
3670 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3671
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003672 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003673 parent, seat, serial, x, y);
3674 if (!shsurf) {
3675 wl_resource_post_error(surface_resource,
3676 WL_DISPLAY_ERROR_INVALID_OBJECT,
3677 "surface->configure already set");
3678 return;
3679 }
3680
3681 shsurf->resource =
3682 wl_resource_create(client,
3683 &xdg_popup_interface, 1, id);
3684 wl_resource_set_implementation(shsurf->resource,
3685 &xdg_popup_implementation,
3686 shsurf, shell_destroy_shell_surface);
3687}
3688
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003689static void
3690xdg_pong(struct wl_client *client,
3691 struct wl_resource *resource, uint32_t serial)
3692{
3693 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003694
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003695 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003696}
3697
Rafael Antognollie2a34552013-12-03 15:35:45 -02003698static bool
3699shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3700{
3701 return wl_resource_instance_of(shsurf->resource,
3702 &xdg_popup_interface,
3703 &xdg_popup_implementation);
3704}
3705
3706static const struct xdg_shell_interface xdg_implementation = {
3707 xdg_use_unstable_version,
3708 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003709 xdg_get_xdg_popup,
3710 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003711};
3712
3713static int
3714xdg_shell_unversioned_dispatch(const void *implementation,
3715 void *_target, uint32_t opcode,
3716 const struct wl_message *message,
3717 union wl_argument *args)
3718{
3719 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003720 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003721
3722 if (opcode != 0) {
3723 wl_resource_post_error(resource,
3724 WL_DISPLAY_ERROR_INVALID_OBJECT,
3725 "must call use_unstable_version first");
3726 return 0;
3727 }
3728
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003729#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003730
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003731 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3732 "shell implementation doesn't match protocol version");
3733
Rafael Antognollie2a34552013-12-03 15:35:45 -02003734 if (args[0].i != XDG_SERVER_VERSION) {
3735 wl_resource_post_error(resource,
3736 WL_DISPLAY_ERROR_INVALID_OBJECT,
3737 "incompatible version, server is %d "
3738 "client wants %d",
3739 XDG_SERVER_VERSION, args[0].i);
3740 return 0;
3741 }
3742
3743 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003744 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003745
3746 return 1;
3747}
3748
3749/* end of xdg-shell implementation */
3750/***********************************/
3751
Kristian Høgsberg07937562011-04-12 17:25:42 -04003752static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003753shell_fade(struct desktop_shell *shell, enum fade_type type);
3754
3755static int
3756screensaver_timeout(void *data)
3757{
3758 struct desktop_shell *shell = data;
3759
3760 shell_fade(shell, FADE_OUT);
3761
3762 return 1;
3763}
3764
3765static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003766handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003767{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003768 struct desktop_shell *shell =
3769 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003770
Pekka Paalanen18027e52011-12-02 16:31:49 +02003771 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003772
3773 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003774 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003775}
3776
3777static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003778launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003779{
3780 if (shell->screensaver.binding)
3781 return;
3782
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003783 if (!shell->screensaver.path) {
3784 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003785 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003786 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003787
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003788 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003789 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003790 return;
3791 }
3792
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003793 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003794 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003795 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003796 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003797}
3798
3799static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003800terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003801{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003802 if (shell->screensaver.process.pid == 0)
3803 return;
3804
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003805 /* Disarm the screensaver timer, otherwise it may fire when the
3806 * compositor is not in the idle state. In that case, the screen will
3807 * be locked, but the wake_signal won't fire on user input, making the
3808 * system unresponsive. */
3809 wl_event_source_timer_update(shell->screensaver.timer, 0);
3810
Pekka Paalanen18027e52011-12-02 16:31:49 +02003811 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003812}
3813
3814static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003815configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003816{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003817 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003818
Jason Ekstranda7af7042013-10-12 22:38:11 -05003819 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3820 if (v->output == ev->output && v != ev) {
3821 weston_view_unmap(v);
3822 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003823 }
3824 }
3825
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003826 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003827
Jason Ekstranda7af7042013-10-12 22:38:11 -05003828 if (wl_list_empty(&ev->layer_link)) {
3829 wl_list_insert(&layer->view_list, &ev->layer_link);
3830 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003831 }
3832}
3833
3834static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003835background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003836{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003837 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003838 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003839
Jason Ekstranda7af7042013-10-12 22:38:11 -05003840 view = container_of(es->views.next, struct weston_view, surface_link);
3841
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003842 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003843}
3844
3845static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003846desktop_shell_set_background(struct wl_client *client,
3847 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003848 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003849 struct wl_resource *surface_resource)
3850{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003851 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003852 struct weston_surface *surface =
3853 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003854 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003855
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003856 if (surface->configure) {
3857 wl_resource_post_error(surface_resource,
3858 WL_DISPLAY_ERROR_INVALID_OBJECT,
3859 "surface role already assigned");
3860 return;
3861 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003862
Jason Ekstranda7af7042013-10-12 22:38:11 -05003863 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3864 weston_view_destroy(view);
3865 view = weston_view_create(surface);
3866
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003867 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003868 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003869 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003870 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003871 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003872 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003873 surface->output->width,
3874 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003875}
3876
3877static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003878panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003879{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003880 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003881 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003882
Jason Ekstranda7af7042013-10-12 22:38:11 -05003883 view = container_of(es->views.next, struct weston_view, surface_link);
3884
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003885 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003886}
3887
3888static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003889desktop_shell_set_panel(struct wl_client *client,
3890 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003891 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003892 struct wl_resource *surface_resource)
3893{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003894 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003895 struct weston_surface *surface =
3896 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003897 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003898
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003899 if (surface->configure) {
3900 wl_resource_post_error(surface_resource,
3901 WL_DISPLAY_ERROR_INVALID_OBJECT,
3902 "surface role already assigned");
3903 return;
3904 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003905
Jason Ekstranda7af7042013-10-12 22:38:11 -05003906 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3907 weston_view_destroy(view);
3908 view = weston_view_create(surface);
3909
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003910 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003911 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003912 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003913 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003914 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003915 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003916 surface->output->width,
3917 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003918}
3919
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003920static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003921lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003922{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003923 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003924 struct weston_view *view;
3925
3926 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003927
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003928 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003929 return;
3930
Jason Ekstranda7af7042013-10-12 22:38:11 -05003931 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003932
3933 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003934 wl_list_insert(&shell->lock_layer.view_list,
3935 &view->layer_link);
3936 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003937 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003938 }
3939}
3940
3941static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003942handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003943{
Tiago Vignattibe143262012-04-16 17:31:41 +03003944 struct desktop_shell *shell =
3945 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003946
Martin Minarik6d118362012-06-07 18:01:59 +02003947 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003948 shell->lock_surface = NULL;
3949}
3950
3951static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003952desktop_shell_set_lock_surface(struct wl_client *client,
3953 struct wl_resource *resource,
3954 struct wl_resource *surface_resource)
3955{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003956 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003957 struct weston_surface *surface =
3958 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003959
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003960 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003961
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003962 if (!shell->locked)
3963 return;
3964
Pekka Paalanen98262232011-12-01 10:42:22 +02003965 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003966
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003967 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003968 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003969 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003970
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003971 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003972 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003973 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003974}
3975
3976static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003977resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003978{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003979 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003980
Pekka Paalanen77346a62011-11-30 16:26:35 +02003981 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003982
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003983 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003984 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003985 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003986 &shell->input_panel_layer.link);
3987 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003988 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003989 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003990 wl_list_insert(&shell->compositor->cursor_layer.link,
3991 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003992 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003993 wl_list_insert(&shell->fullscreen_layer.link,
3994 &shell->panel_layer.link);
3995 wl_list_insert(&shell->panel_layer.link,
3996 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003997
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003998 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003999
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004000 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004001 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004002 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004003}
4004
4005static void
4006desktop_shell_unlock(struct wl_client *client,
4007 struct wl_resource *resource)
4008{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004009 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004010
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004011 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004012
4013 if (shell->locked)
4014 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004015}
4016
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004017static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004018desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004019 struct wl_resource *resource,
4020 struct wl_resource *surface_resource)
4021{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004022 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004023
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004024 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004025 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004026}
4027
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004028static void
4029desktop_shell_desktop_ready(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 Paalanen79346ab2013-05-22 18:03:09 +03004033
4034 shell_fade_startup(shell);
4035}
4036
Kristian Høgsberg75840622011-09-06 13:48:16 -04004037static const struct desktop_shell_interface desktop_shell_implementation = {
4038 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004039 desktop_shell_set_panel,
4040 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004041 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004042 desktop_shell_set_grab_surface,
4043 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004044};
4045
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004046static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004047get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004048{
4049 struct shell_surface *shsurf;
4050
4051 shsurf = get_shell_surface(surface);
4052 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004053 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004054 return shsurf->type;
4055}
4056
Kristian Høgsberg75840622011-09-06 13:48:16 -04004057static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004058move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004059{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004060 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004061 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004062 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004063
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004064 if (seat->pointer->focus == NULL)
4065 return;
4066
4067 focus = seat->pointer->focus->surface;
4068
Pekka Paalanen01388e22013-04-25 13:57:44 +03004069 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004070 if (surface == NULL)
4071 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004072
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004073 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004074 if (shsurf == NULL || shsurf->state.fullscreen ||
4075 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004076 return;
4077
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004078 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004079}
4080
4081static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004082maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4083{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004084 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004085 struct weston_surface *surface;
4086 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004087 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004088
4089 surface = weston_surface_get_main_surface(focus);
4090 if (surface == NULL)
4091 return;
4092
4093 shsurf = get_shell_surface(surface);
4094 if (shsurf == NULL)
4095 return;
4096
4097 if (!shell_surface_is_xdg_surface(shsurf))
4098 return;
4099
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004100 serial = wl_display_next_serial(seat->compositor->wl_display);
4101 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4102 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004103}
4104
4105static void
4106fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4107{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004108 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004109 struct weston_surface *surface;
4110 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004111 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004112
4113 surface = weston_surface_get_main_surface(focus);
4114 if (surface == NULL)
4115 return;
4116
4117 shsurf = get_shell_surface(surface);
4118 if (shsurf == NULL)
4119 return;
4120
4121 if (!shell_surface_is_xdg_surface(shsurf))
4122 return;
4123
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004124 serial = wl_display_next_serial(seat->compositor->wl_display);
4125 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4126 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004127}
4128
4129static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004130touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4131{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004132 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004133 struct weston_surface *surface;
4134 struct shell_surface *shsurf;
4135
4136 surface = weston_surface_get_main_surface(focus);
4137 if (surface == NULL)
4138 return;
4139
4140 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004141 if (shsurf == NULL || shsurf->state.fullscreen ||
4142 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004143 return;
4144
4145 surface_touch_move(shsurf, (struct weston_seat *) seat);
4146}
4147
4148static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004149resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004150{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004151 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004152 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004153 uint32_t edges = 0;
4154 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004155 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004156
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004157 if (seat->pointer->focus == NULL)
4158 return;
4159
4160 focus = seat->pointer->focus->surface;
4161
Pekka Paalanen01388e22013-04-25 13:57:44 +03004162 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004163 if (surface == NULL)
4164 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004165
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004166 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004167 if (shsurf == NULL || shsurf->state.fullscreen ||
4168 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004169 return;
4170
Jason Ekstranda7af7042013-10-12 22:38:11 -05004171 weston_view_from_global(shsurf->view,
4172 wl_fixed_to_int(seat->pointer->grab_x),
4173 wl_fixed_to_int(seat->pointer->grab_y),
4174 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004175
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004176 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004177 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004178 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004179 edges |= 0;
4180 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004181 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004182
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004183 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004184 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004185 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004186 edges |= 0;
4187 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004188 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004189
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004190 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004191}
4192
4193static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004194surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004195 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004196{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004197 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004198 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004199 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004200 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004201
Pekka Paalanen01388e22013-04-25 13:57:44 +03004202 /* XXX: broken for windows containing sub-surfaces */
4203 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004204 if (surface == NULL)
4205 return;
4206
4207 shsurf = get_shell_surface(surface);
4208 if (!shsurf)
4209 return;
4210
Jason Ekstranda7af7042013-10-12 22:38:11 -05004211 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004212
Jason Ekstranda7af7042013-10-12 22:38:11 -05004213 if (shsurf->view->alpha > 1.0)
4214 shsurf->view->alpha = 1.0;
4215 if (shsurf->view->alpha < step)
4216 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004217
Jason Ekstranda7af7042013-10-12 22:38:11 -05004218 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004219 weston_surface_damage(surface);
4220}
4221
4222static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004223do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004224 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004225{
Daniel Stone37816df2012-05-16 18:45:18 +01004226 struct weston_seat *ws = (struct weston_seat *) seat;
4227 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004228 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004229 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004230
4231 wl_list_for_each(output, &compositor->output_list, link) {
4232 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004233 wl_fixed_to_double(seat->pointer->x),
4234 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004235 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004236 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004237 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004238 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004239 increment = -output->zoom.increment;
4240 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004241 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004242 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004243 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004244 else
4245 increment = 0;
4246
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004247 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004248
Scott Moreaue6603982012-06-11 13:07:51 -06004249 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004250 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004251 else if (output->zoom.level > output->zoom.max_level)
4252 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004253 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004254 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004255 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004256
Scott Moreaue6603982012-06-11 13:07:51 -06004257 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004258
Jason Ekstranda7af7042013-10-12 22:38:11 -05004259 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004260 }
4261 }
4262}
4263
Scott Moreauccbf29d2012-02-22 14:21:41 -07004264static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004265zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004266 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004267{
4268 do_zoom(seat, time, 0, axis, value);
4269}
4270
4271static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004272zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004273 void *data)
4274{
4275 do_zoom(seat, time, key, 0, 0);
4276}
4277
4278static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004279terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004280 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004281{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004282 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004283
Daniel Stone325fc2d2012-05-30 16:31:58 +01004284 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004285}
4286
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004287static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004288rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4289 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004290{
4291 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004292 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004293 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004294 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004295 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004296
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004297 weston_pointer_move(pointer, x, y);
4298
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004299 if (!shsurf)
4300 return;
4301
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004302 cx = 0.5f * shsurf->surface->width;
4303 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004304
Daniel Stone37816df2012-05-16 18:45:18 +01004305 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4306 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004307 r = sqrtf(dx * dx + dy * dy);
4308
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004309 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004310 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004311
4312 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004313 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004314 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004315
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004316 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004317 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004318
4319 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004320 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004321 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004322 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004323 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004324
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004325 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004326 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004327 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004328 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004329 wl_list_init(&shsurf->rotation.transform.link);
4330 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004331 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004332 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004333
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004334 /* We need to adjust the position of the surface
4335 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004336 cposx = shsurf->view->geometry.x + cx;
4337 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004338 dposx = rotate->center.x - cposx;
4339 dposy = rotate->center.y - cposy;
4340 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004341 weston_view_set_position(shsurf->view,
4342 shsurf->view->geometry.x + dposx,
4343 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004344 }
4345
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004346 /* Repaint implies weston_surface_update_transform(), which
4347 * lazily applies the damage due to rotation update.
4348 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004349 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004350}
4351
4352static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004353rotate_grab_button(struct weston_pointer_grab *grab,
4354 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004355{
4356 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004357 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004358 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004359 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004360 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004361
Daniel Stone4dbadb12012-05-30 16:31:51 +01004362 if (pointer->button_count == 0 &&
4363 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004364 if (shsurf)
4365 weston_matrix_multiply(&shsurf->rotation.rotation,
4366 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004367 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004368 free(rotate);
4369 }
4370}
4371
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004372static void
4373rotate_grab_cancel(struct weston_pointer_grab *grab)
4374{
4375 struct rotate_grab *rotate =
4376 container_of(grab, struct rotate_grab, base.grab);
4377
4378 shell_grab_end(&rotate->base);
4379 free(rotate);
4380}
4381
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004382static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004383 noop_grab_focus,
4384 rotate_grab_motion,
4385 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004386 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004387};
4388
4389static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004390surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004391{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004392 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004393 float dx, dy;
4394 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004395
Pekka Paalanen460099f2012-01-20 16:48:25 +02004396 rotate = malloc(sizeof *rotate);
4397 if (!rotate)
4398 return;
4399
Jason Ekstranda7af7042013-10-12 22:38:11 -05004400 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004401 surface->surface->width * 0.5f,
4402 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004403 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004404
Daniel Stone37816df2012-05-16 18:45:18 +01004405 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4406 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004407 r = sqrtf(dx * dx + dy * dy);
4408 if (r > 20.0f) {
4409 struct weston_matrix inverse;
4410
4411 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004412 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004413 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004414
4415 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004416 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004417 } else {
4418 weston_matrix_init(&surface->rotation.rotation);
4419 weston_matrix_init(&rotate->rotation);
4420 }
4421
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004422 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4423 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004424}
4425
4426static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004427rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004428 void *data)
4429{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004430 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004431 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004432 struct shell_surface *surface;
4433
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004434 if (seat->pointer->focus == NULL)
4435 return;
4436
4437 focus = seat->pointer->focus->surface;
4438
Pekka Paalanen01388e22013-04-25 13:57:44 +03004439 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004440 if (base_surface == NULL)
4441 return;
4442
4443 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004444 if (surface == NULL || surface->state.fullscreen ||
4445 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004446 return;
4447
4448 surface_rotate(surface, seat);
4449}
4450
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004451/* Move all fullscreen layers down to the current workspace and hide their
4452 * black views. The surfaces' state is set to both fullscreen and lowered,
4453 * and this is reversed when such a surface is re-configured, see
4454 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4455 *
4456 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004457 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004458void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004459lower_fullscreen_layer(struct desktop_shell *shell)
4460{
4461 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004462 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004463
4464 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004465 wl_list_for_each_reverse_safe(view, prev,
4466 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004467 layer_link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004468 struct shell_surface *shsurf = get_shell_surface(view->surface);
4469
4470 if (!shsurf)
4471 continue;
4472
4473 /* We can have a non-fullscreen popup for a fullscreen surface
4474 * in the fullscreen layer. */
4475 if (shsurf->state.fullscreen) {
4476 /* Hide the black view */
4477 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
4478 wl_list_init(&shsurf->fullscreen.black_view->layer_link);
4479 }
4480
4481 /* Lower the view to the workspace layer */
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004482 wl_list_remove(&view->layer_link);
4483 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4484 weston_view_damage_below(view);
4485 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004486
4487 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004488 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004489}
4490
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004491void
Tiago Vignattibe143262012-04-16 17:31:41 +03004492activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004493 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004494{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004495 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004496 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004497 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004498 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004499 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004500
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004501 lower_fullscreen_layer(shell);
4502
Pekka Paalanen01388e22013-04-25 13:57:44 +03004503 main_surface = weston_surface_get_main_surface(es);
4504
Daniel Stone37816df2012-05-16 18:45:18 +01004505 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004506
Jonas Ådahl8538b222012-08-29 22:13:03 +02004507 state = ensure_focus_state(shell, seat);
4508 if (state == NULL)
4509 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004510
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004511 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004512 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004513
Rafael Antognolli03b16592013-12-03 15:35:42 -02004514 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004515 assert(shsurf);
4516
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004517 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004518 shell_configure_fullscreen(shsurf);
4519 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004520 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004521
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004522 /* Update the surface’s layer. This brings it to the top of the stacking
4523 * order as appropriate. */
4524 shell_surface_update_layer(shsurf);
4525
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004526 if (shell->focus_animation_type != ANIMATION_NONE) {
4527 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004528 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004529 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004530}
4531
Alex Wu21858432012-04-01 20:13:08 +08004532/* no-op func for checking black surface */
4533static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004534black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004535{
4536}
4537
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004538static bool
Alex Wu21858432012-04-01 20:13:08 +08004539is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4540{
4541 if (es->configure == black_surface_configure) {
4542 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004543 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004544 return true;
4545 }
4546 return false;
4547}
4548
Kristian Høgsberg75840622011-09-06 13:48:16 -04004549static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004550activate_binding(struct weston_seat *seat,
4551 struct desktop_shell *shell,
4552 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004553{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004554 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004555
Alex Wu9c35e6b2012-03-05 14:13:13 +08004556 if (!focus)
4557 return;
4558
Pekka Paalanen01388e22013-04-25 13:57:44 +03004559 if (is_black_surface(focus, &main_surface))
4560 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004561
Pekka Paalanen01388e22013-04-25 13:57:44 +03004562 main_surface = weston_surface_get_main_surface(focus);
4563 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004564 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004565
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004566 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004567}
4568
4569static void
4570click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4571 void *data)
4572{
4573 if (seat->pointer->grab != &seat->pointer->default_grab)
4574 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004575 if (seat->pointer->focus == NULL)
4576 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004577
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004578 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004579}
4580
4581static void
4582touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4583{
4584 if (seat->touch->grab != &seat->touch->default_grab)
4585 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004586 if (seat->touch->focus == NULL)
4587 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004588
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004589 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004590}
4591
4592static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004593unfocus_all_seats(struct desktop_shell *shell)
4594{
4595 struct weston_seat *seat, *next;
4596
4597 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4598 if (seat->keyboard == NULL)
4599 continue;
4600
4601 weston_keyboard_set_focus(seat->keyboard, NULL);
4602 }
4603}
4604
4605static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004606lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004607{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004608 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004609
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004610 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004611 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004612 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004613 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004614
4615 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004616
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004617 /* Hide all surfaces by removing the fullscreen, panel and
4618 * toplevel layers. This way nothing else can show or receive
4619 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004620
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004621 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004622 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004623 if (shell->showing_input_panels)
4624 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004625 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004626 wl_list_insert(&shell->compositor->cursor_layer.link,
4627 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004628
Pekka Paalanen77346a62011-11-30 16:26:35 +02004629 launch_screensaver(shell);
4630
Neil Robertsb4a91702014-04-09 16:33:32 +01004631 /* Remove the keyboard focus on all seats. This will be
4632 * restored to the workspace's saved state via
4633 * restore_focus_state when the compositor is unlocked */
4634 unfocus_all_seats(shell);
4635
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004636 /* TODO: disable bindings that should not work while locked. */
4637
4638 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004639}
4640
4641static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004642unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004643{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004644 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004645 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004646 return;
4647 }
4648
4649 /* If desktop-shell client has gone away, unlock immediately. */
4650 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004651 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004652 return;
4653 }
4654
4655 if (shell->prepare_event_sent)
4656 return;
4657
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004658 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004659 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004660}
4661
4662static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004663shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004664{
4665 struct desktop_shell *shell = data;
4666
4667 shell->fade.animation = NULL;
4668
4669 switch (shell->fade.type) {
4670 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004671 weston_surface_destroy(shell->fade.view->surface);
4672 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004673 break;
4674 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004675 lock(shell);
4676 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004677 default:
4678 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004679 }
4680}
4681
Jason Ekstranda7af7042013-10-12 22:38:11 -05004682static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004683shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004684{
4685 struct weston_compositor *compositor = shell->compositor;
4686 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004687 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004688
4689 surface = weston_surface_create(compositor);
4690 if (!surface)
4691 return NULL;
4692
Jason Ekstranda7af7042013-10-12 22:38:11 -05004693 view = weston_view_create(surface);
4694 if (!view) {
4695 weston_surface_destroy(surface);
4696 return NULL;
4697 }
4698
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004699 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004700 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004701 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004702 wl_list_insert(&compositor->fade_layer.view_list,
4703 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004704 pixman_region32_init(&surface->input);
4705
Jason Ekstranda7af7042013-10-12 22:38:11 -05004706 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004707}
4708
4709static void
4710shell_fade(struct desktop_shell *shell, enum fade_type type)
4711{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004712 float tint;
4713
4714 switch (type) {
4715 case FADE_IN:
4716 tint = 0.0;
4717 break;
4718 case FADE_OUT:
4719 tint = 1.0;
4720 break;
4721 default:
4722 weston_log("shell: invalid fade type\n");
4723 return;
4724 }
4725
4726 shell->fade.type = type;
4727
Jason Ekstranda7af7042013-10-12 22:38:11 -05004728 if (shell->fade.view == NULL) {
4729 shell->fade.view = shell_fade_create_surface(shell);
4730 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004731 return;
4732
Jason Ekstranda7af7042013-10-12 22:38:11 -05004733 shell->fade.view->alpha = 1.0 - tint;
4734 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004735 }
4736
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004737 if (shell->fade.view->output == NULL) {
4738 /* If the black view gets a NULL output, we lost the
4739 * last output and we'll just cancel the fade. This
4740 * happens when you close the last window under the
4741 * X11 or Wayland backends. */
4742 shell->locked = false;
4743 weston_surface_destroy(shell->fade.view->surface);
4744 shell->fade.view = NULL;
4745 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004746 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004747 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004748 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004749 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004750 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004751 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004752 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004753}
4754
4755static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004756do_shell_fade_startup(void *data)
4757{
4758 struct desktop_shell *shell = data;
4759
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004760 if (shell->startup_animation_type == ANIMATION_FADE)
4761 shell_fade(shell, FADE_IN);
4762 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004763 weston_surface_destroy(shell->fade.view->surface);
4764 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004765 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004766}
4767
4768static void
4769shell_fade_startup(struct desktop_shell *shell)
4770{
4771 struct wl_event_loop *loop;
4772
4773 if (!shell->fade.startup_timer)
4774 return;
4775
4776 wl_event_source_remove(shell->fade.startup_timer);
4777 shell->fade.startup_timer = NULL;
4778
4779 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4780 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4781}
4782
4783static int
4784fade_startup_timeout(void *data)
4785{
4786 struct desktop_shell *shell = data;
4787
4788 shell_fade_startup(shell);
4789 return 0;
4790}
4791
4792static void
4793shell_fade_init(struct desktop_shell *shell)
4794{
4795 /* Make compositor output all black, and wait for the desktop-shell
4796 * client to signal it is ready, then fade in. The timer triggers a
4797 * fade-in, in case the desktop-shell client takes too long.
4798 */
4799
4800 struct wl_event_loop *loop;
4801
Jason Ekstranda7af7042013-10-12 22:38:11 -05004802 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004803 weston_log("%s: warning: fade surface already exists\n",
4804 __func__);
4805 return;
4806 }
4807
Jason Ekstranda7af7042013-10-12 22:38:11 -05004808 shell->fade.view = shell_fade_create_surface(shell);
4809 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004810 return;
4811
Jason Ekstranda7af7042013-10-12 22:38:11 -05004812 weston_view_update_transform(shell->fade.view);
4813 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004814
4815 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4816 shell->fade.startup_timer =
4817 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4818 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4819}
4820
4821static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004822idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004823{
4824 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004825 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004826 struct weston_seat *seat;
4827
4828 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4829 if (seat->pointer)
4830 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004831
4832 shell_fade(shell, FADE_OUT);
4833 /* lock() is called from shell_fade_done() */
4834}
4835
4836static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004837wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004838{
4839 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004840 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004841
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004842 unlock(shell);
4843}
4844
4845static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004846center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004847{
Giulio Camuffob8366642013-04-25 13:57:46 +03004848 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004849 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004850
Jason Ekstranda7af7042013-10-12 22:38:11 -05004851 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004852
4853 x = output->x + (output->width - width) / 2 - surf_x / 2;
4854 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004855
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004856 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004857}
4858
4859static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004860weston_view_set_initial_position(struct weston_view *view,
4861 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004862{
4863 struct weston_compositor *compositor = shell->compositor;
4864 int ix = 0, iy = 0;
4865 int range_x, range_y;
4866 int dx, dy, x, y, panel_height;
4867 struct weston_output *output, *target_output = NULL;
4868 struct weston_seat *seat;
4869
4870 /* As a heuristic place the new window on the same output as the
4871 * pointer. Falling back to the output containing 0, 0.
4872 *
4873 * TODO: Do something clever for touch too?
4874 */
4875 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004876 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004877 ix = wl_fixed_to_int(seat->pointer->x);
4878 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004879 break;
4880 }
4881 }
4882
4883 wl_list_for_each(output, &compositor->output_list, link) {
4884 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4885 target_output = output;
4886 break;
4887 }
4888 }
4889
4890 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004891 weston_view_set_position(view, 10 + random() % 400,
4892 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004893 return;
4894 }
4895
4896 /* Valid range within output where the surface will still be onscreen.
4897 * If this is negative it means that the surface is bigger than
4898 * output.
4899 */
4900 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004901 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004902 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004903 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004904
Rob Bradford4cb88c72012-08-13 15:18:44 +01004905 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004906 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004907 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004908 dx = 0;
4909
4910 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004911 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004912 else
4913 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004914
4915 x = target_output->x + dx;
4916 y = target_output->y + dy;
4917
Jason Ekstranda7af7042013-10-12 22:38:11 -05004918 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004919}
4920
4921static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004922map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004923 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004924{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004925 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004926 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004927 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004928 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004929
Pekka Paalanen77346a62011-11-30 16:26:35 +02004930 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004931 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004932 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004933 if (shsurf->state.fullscreen) {
4934 center_on_output(shsurf->view, shsurf->fullscreen_output);
4935 shell_map_fullscreen(shsurf);
4936 } else if (shsurf->state.maximized) {
4937 /* use surface configure to set the geometry */
4938 panel_height = get_output_panel_height(shell, shsurf->output);
4939 surface_subsurfaces_boundingbox(shsurf->surface,
4940 &surf_x, &surf_y, NULL, NULL);
4941 weston_view_set_position(shsurf->view,
4942 shsurf->output->x - surf_x,
4943 shsurf->output->y +
4944 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004945 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004946 weston_view_set_initial_position(shsurf->view, shell);
4947 }
Juan Zhao96879df2012-02-07 08:45:41 +08004948 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004949 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004950 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004951 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004952 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004953 weston_view_set_position(shsurf->view,
4954 shsurf->view->geometry.x + sx,
4955 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004956 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004957 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004958 default:
4959 ;
4960 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004961
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004962 /* Surface stacking order, see also activate(). */
4963 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004964
Jason Ekstranda7af7042013-10-12 22:38:11 -05004965 if (shsurf->type != SHELL_SURFACE_NONE) {
4966 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004967 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004968 shsurf->surface->output = shsurf->output;
4969 shsurf->view->output = shsurf->output;
4970 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004971 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004972
Jason Ekstranda7af7042013-10-12 22:38:11 -05004973 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004974 /* XXX: xwayland's using the same fields for transient type */
4975 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004976 if (shsurf->transient.flags ==
4977 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4978 break;
4979 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004980 if (shsurf->state.relative &&
4981 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4982 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004983 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004984 break;
4985 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004986 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004987 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004988 case SHELL_SURFACE_POPUP:
4989 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004990 default:
4991 break;
4992 }
4993
Rafael Antognolli03b16592013-12-03 15:35:42 -02004994 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4995 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004996 {
4997 switch (shell->win_animation_type) {
4998 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004999 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005000 break;
5001 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005002 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005003 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005004 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005005 default:
5006 break;
5007 }
5008 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005009}
5010
5011static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005012configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005013 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005014{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005015 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005016 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005017 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005018
Pekka Paalanen77346a62011-11-30 16:26:35 +02005019 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005020
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005021 assert(shsurf);
5022
Rafael Antognolli03b16592013-12-03 15:35:42 -02005023 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005024 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005025 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005026 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005027 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5028 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005029 mx = shsurf->output->x - surf_x;
5030 my = shsurf->output->y +
5031 get_output_panel_height(shell,shsurf->output) - surf_y;
5032 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005033 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005034 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005035 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005036
Alex Wu4539b082012-03-01 12:57:46 +08005037 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005038 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005039 wl_list_for_each(view, &surface->views, surface_link)
5040 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005041
Rafael Antognolli03b16592013-12-03 15:35:42 -02005042 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005043 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005044 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005045}
5046
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005047static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005048shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005049{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005050 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005051 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005052 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005053
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005054 assert(shsurf);
5055
5056 shell = shsurf->shell;
5057
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005058 if (!weston_surface_is_mapped(es) &&
5059 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005060 remove_popup_grab(shsurf);
5061 }
5062
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005063 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005064 return;
5065
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005066 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005067 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005068 type_changed = 1;
5069 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005070
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005071 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005072 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005073 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005074 shsurf->last_width != es->width ||
5075 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005076 float from_x, from_y;
5077 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005078
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005079 if (shsurf->resize_edges) {
5080 sx = 0;
5081 sy = 0;
5082 }
5083
5084 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5085 sx = shsurf->last_width - es->width;
5086 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5087 sy = shsurf->last_height - es->height;
5088
5089 shsurf->last_width = es->width;
5090 shsurf->last_height = es->height;
5091
Jason Ekstranda7af7042013-10-12 22:38:11 -05005092 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5093 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005094 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005095 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005096 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005097 }
5098}
5099
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005100static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005101
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005102static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005103desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005104{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005105 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005106 struct desktop_shell *shell =
5107 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005108
5109 shell->child.process.pid = 0;
5110 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005111
5112 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5113 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005114 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005115 shell->child.deathstamp = time;
5116 shell->child.deathcount = 0;
5117 }
5118
5119 shell->child.deathcount++;
5120 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005121 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005122 return;
5123 }
5124
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005125 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005126 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005127 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005128}
5129
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005130static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005131desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5132{
5133 struct desktop_shell *shell;
5134
5135 shell = container_of(listener, struct desktop_shell,
5136 child.client_destroy_listener);
5137
5138 shell->child.client = NULL;
5139}
5140
5141static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005142launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005143{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005144 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005145
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005146 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005147 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005148 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005149 desktop_shell_sigchld);
5150
5151 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005152 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005153
5154 shell->child.client_destroy_listener.notify =
5155 desktop_shell_client_destroy;
5156 wl_client_add_destroy_listener(shell->child.client,
5157 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005158}
5159
5160static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005161handle_shell_client_destroy(struct wl_listener *listener, void *data)
5162{
5163 struct shell_client *sc =
5164 container_of(listener, struct shell_client, destroy_listener);
5165
5166 if (sc->ping_timer)
5167 wl_event_source_remove(sc->ping_timer);
5168 free(sc);
5169}
5170
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005171static struct shell_client *
5172shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5173 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005174{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005175 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005176
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005177 sc = zalloc(sizeof *sc);
5178 if (sc == NULL) {
5179 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005180 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005181 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005182
5183 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005184 if (sc->resource == NULL) {
5185 free(sc);
5186 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005187 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005188 }
5189
5190 sc->client = client;
5191 sc->shell = shell;
5192 sc->destroy_listener.notify = handle_shell_client_destroy;
5193 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5194
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005195 return sc;
5196}
5197
5198static void
5199bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5200{
5201 struct desktop_shell *shell = data;
5202 struct shell_client *sc;
5203
5204 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5205 if (sc)
5206 wl_resource_set_implementation(sc->resource,
5207 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005208 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005209}
5210
5211static void
5212bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5213{
5214 struct desktop_shell *shell = data;
5215 struct shell_client *sc;
5216
5217 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5218 if (sc)
5219 wl_resource_set_dispatcher(sc->resource,
5220 xdg_shell_unversioned_dispatch,
5221 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005222}
5223
5224static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005225unbind_desktop_shell(struct wl_resource *resource)
5226{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005227 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005228
5229 if (shell->locked)
5230 resume_desktop(shell);
5231
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005232 shell->child.desktop_shell = NULL;
5233 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005234}
5235
5236static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005237bind_desktop_shell(struct wl_client *client,
5238 void *data, uint32_t version, uint32_t id)
5239{
Tiago Vignattibe143262012-04-16 17:31:41 +03005240 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005241 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005242
Jason Ekstranda85118c2013-06-27 20:17:02 -05005243 resource = wl_resource_create(client, &desktop_shell_interface,
5244 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005245
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005246 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005247 wl_resource_set_implementation(resource,
5248 &desktop_shell_implementation,
5249 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005250 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005251
5252 if (version < 2)
5253 shell_fade_startup(shell);
5254
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005255 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005256 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005257
5258 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5259 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005260 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005261}
5262
Pekka Paalanen6e168112011-11-24 11:34:05 +02005263static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005264screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005265{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005266 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005267 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005268
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005269 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005270 return;
5271
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005272 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005273 if (!shell->locked)
5274 return;
5275
Jason Ekstranda7af7042013-10-12 22:38:11 -05005276 view = container_of(surface->views.next, struct weston_view, surface_link);
5277 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005278
Jason Ekstranda7af7042013-10-12 22:38:11 -05005279 if (wl_list_empty(&view->layer_link)) {
5280 wl_list_insert(shell->lock_layer.view_list.prev,
5281 &view->layer_link);
5282 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005283 wl_event_source_timer_update(shell->screensaver.timer,
5284 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005285 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005286 }
5287}
5288
5289static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005290screensaver_set_surface(struct wl_client *client,
5291 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005292 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005293 struct wl_resource *output_resource)
5294{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005295 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005296 struct weston_surface *surface =
5297 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005298 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005299 struct weston_view *view, *next;
5300
5301 /* Make sure we only have one view */
5302 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5303 weston_view_destroy(view);
5304 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005305
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005306 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005307 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005308 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005309}
5310
5311static const struct screensaver_interface screensaver_implementation = {
5312 screensaver_set_surface
5313};
5314
5315static void
5316unbind_screensaver(struct wl_resource *resource)
5317{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005318 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005319
Pekka Paalanen77346a62011-11-30 16:26:35 +02005320 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005321}
5322
5323static void
5324bind_screensaver(struct wl_client *client,
5325 void *data, uint32_t version, uint32_t id)
5326{
Tiago Vignattibe143262012-04-16 17:31:41 +03005327 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005328 struct wl_resource *resource;
5329
Jason Ekstranda85118c2013-06-27 20:17:02 -05005330 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005331
Pekka Paalanen77346a62011-11-30 16:26:35 +02005332 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005333 wl_resource_set_implementation(resource,
5334 &screensaver_implementation,
5335 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005336 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005337 return;
5338 }
5339
5340 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5341 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005342 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005343}
5344
Kristian Høgsberg07045392012-02-19 18:52:44 -05005345struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005346 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005347 struct weston_surface *current;
5348 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005349 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005350 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005351};
5352
5353static void
5354switcher_next(struct switcher *switcher)
5355{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005356 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005357 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005358 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005359 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005360
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005361 /* temporary re-display minimized surfaces */
5362 struct weston_view *tmp;
5363 struct weston_view **minimized;
5364 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5365 wl_list_remove(&view->layer_link);
5366 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5367 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5368 *minimized = view;
5369 }
5370
Jason Ekstranda7af7042013-10-12 22:38:11 -05005371 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005372 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005373 if (shsurf &&
5374 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5375 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005376 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005377 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005378 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005379 next = view->surface;
5380 prev = view->surface;
5381 view->alpha = 0.25;
5382 weston_view_geometry_dirty(view);
5383 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005384 }
Alex Wu1659daa2012-04-01 20:13:09 +08005385
Jason Ekstranda7af7042013-10-12 22:38:11 -05005386 if (is_black_surface(view->surface, NULL)) {
5387 view->alpha = 0.25;
5388 weston_view_geometry_dirty(view);
5389 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005390 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005391 }
5392
5393 if (next == NULL)
5394 next = first;
5395
Alex Wu07b26062012-03-12 16:06:01 +08005396 if (next == NULL)
5397 return;
5398
Kristian Høgsberg07045392012-02-19 18:52:44 -05005399 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005400 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005401
5402 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005403 wl_list_for_each(view, &next->views, surface_link)
5404 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005405
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005406 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005407 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005408 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005409}
5410
5411static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005412switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005413{
5414 struct switcher *switcher =
5415 container_of(listener, struct switcher, listener);
5416
5417 switcher_next(switcher);
5418}
5419
5420static void
Daniel Stone351eb612012-05-31 15:27:47 -04005421switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005422{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005423 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005424 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005425 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005426
Jason Ekstranda7af7042013-10-12 22:38:11 -05005427 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005428 if (is_focus_view(view))
5429 continue;
5430
Jason Ekstranda7af7042013-10-12 22:38:11 -05005431 view->alpha = 1.0;
5432 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005433 }
5434
Alex Wu07b26062012-03-12 16:06:01 +08005435 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005436 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005437 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005438 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005439 weston_keyboard_end_grab(keyboard);
5440 if (keyboard->input_method_resource)
5441 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005442
5443 /* re-hide surfaces that were temporary shown during the switch */
5444 struct weston_view **minimized;
5445 wl_array_for_each(minimized, &switcher->minimized_array) {
5446 /* with the exception of the current selected */
5447 if ((*minimized)->surface != switcher->current) {
5448 wl_list_remove(&(*minimized)->layer_link);
5449 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5450 weston_view_damage_below(*minimized);
5451 }
5452 }
5453 wl_array_release(&switcher->minimized_array);
5454
Kristian Høgsberg07045392012-02-19 18:52:44 -05005455 free(switcher);
5456}
5457
5458static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005459switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005460 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005461{
5462 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005463 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005464
Daniel Stonec9785ea2012-05-30 16:31:52 +01005465 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005466 switcher_next(switcher);
5467}
5468
5469static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005470switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005471 uint32_t mods_depressed, uint32_t mods_latched,
5472 uint32_t mods_locked, uint32_t group)
5473{
5474 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005475 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005476
Daniel Stone351eb612012-05-31 15:27:47 -04005477 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5478 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005479}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005480
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005481static void
5482switcher_cancel(struct weston_keyboard_grab *grab)
5483{
5484 struct switcher *switcher = container_of(grab, struct switcher, grab);
5485
5486 switcher_destroy(switcher);
5487}
5488
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005489static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005490 switcher_key,
5491 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005492 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005493};
5494
5495static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005496switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005497 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005498{
Tiago Vignattibe143262012-04-16 17:31:41 +03005499 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005500 struct switcher *switcher;
5501
5502 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005503 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005504 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005505 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005506 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005507 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005508
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005509 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005510 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005511 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005512 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5513 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005514 switcher_next(switcher);
5515}
5516
Pekka Paalanen3c647232011-12-22 13:43:43 +02005517static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005518backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005519 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005520{
5521 struct weston_compositor *compositor = data;
5522 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005523 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005524
5525 /* TODO: we're limiting to simple use cases, where we assume just
5526 * control on the primary display. We'd have to extend later if we
5527 * ever get support for setting backlights on random desktop LCD
5528 * panels though */
5529 output = get_default_output(compositor);
5530 if (!output)
5531 return;
5532
5533 if (!output->set_backlight)
5534 return;
5535
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005536 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5537 backlight_new = output->backlight_current - 25;
5538 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5539 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005540
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005541 if (backlight_new < 5)
5542 backlight_new = 5;
5543 if (backlight_new > 255)
5544 backlight_new = 255;
5545
5546 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005547 output->set_backlight(output, output->backlight_current);
5548}
5549
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005550struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005551 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005552 struct weston_seat *seat;
5553 uint32_t key[2];
5554 int key_released[2];
5555};
5556
5557static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005558debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005559 uint32_t key, uint32_t state)
5560{
5561 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005562 struct weston_compositor *ec = db->seat->compositor;
5563 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005564 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005565 uint32_t serial;
5566 int send = 0, terminate = 0;
5567 int check_binding = 1;
5568 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005569 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005570
5571 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5572 /* Do not run bindings on key releases */
5573 check_binding = 0;
5574
5575 for (i = 0; i < 2; i++)
5576 if (key == db->key[i])
5577 db->key_released[i] = 1;
5578
5579 if (db->key_released[0] && db->key_released[1]) {
5580 /* All key releases been swalled so end the grab */
5581 terminate = 1;
5582 } else if (key != db->key[0] && key != db->key[1]) {
5583 /* Should not swallow release of other keys */
5584 send = 1;
5585 }
5586 } else if (key == db->key[0] && !db->key_released[0]) {
5587 /* Do not check bindings for the first press of the binding
5588 * key. This allows it to be used as a debug shortcut.
5589 * We still need to swallow this event. */
5590 check_binding = 0;
5591 } else if (db->key[1]) {
5592 /* If we already ran a binding don't process another one since
5593 * we can't keep track of all the binding keys that were
5594 * pressed in order to swallow the release events. */
5595 send = 1;
5596 check_binding = 0;
5597 }
5598
5599 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005600 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5601 key, state)) {
5602 /* We ran a binding so swallow the press and keep the
5603 * grab to swallow the released too. */
5604 send = 0;
5605 terminate = 0;
5606 db->key[1] = key;
5607 } else {
5608 /* Terminate the grab since the key pressed is not a
5609 * debug binding key. */
5610 send = 1;
5611 terminate = 1;
5612 }
5613 }
5614
5615 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005616 serial = wl_display_next_serial(display);
5617 resource_list = &grab->keyboard->focus_resource_list;
5618 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005619 wl_keyboard_send_key(resource, serial, time, key, state);
5620 }
5621 }
5622
5623 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005624 weston_keyboard_end_grab(grab->keyboard);
5625 if (grab->keyboard->input_method_resource)
5626 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005627 free(db);
5628 }
5629}
5630
5631static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005632debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005633 uint32_t mods_depressed, uint32_t mods_latched,
5634 uint32_t mods_locked, uint32_t group)
5635{
5636 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005637 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005638
Neil Roberts96d790e2013-09-19 17:32:00 +01005639 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005640
Neil Roberts96d790e2013-09-19 17:32:00 +01005641 wl_resource_for_each(resource, resource_list) {
5642 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5643 mods_latched, mods_locked, group);
5644 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005645}
5646
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005647static void
5648debug_binding_cancel(struct weston_keyboard_grab *grab)
5649{
5650 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5651
5652 weston_keyboard_end_grab(grab->keyboard);
5653 free(db);
5654}
5655
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005656struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005657 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005658 debug_binding_modifiers,
5659 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005660};
5661
5662static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005663debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005664{
5665 struct debug_binding_grab *grab;
5666
5667 grab = calloc(1, sizeof *grab);
5668 if (!grab)
5669 return;
5670
5671 grab->seat = (struct weston_seat *) seat;
5672 grab->key[0] = key;
5673 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005674 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005675}
5676
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005677static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005678force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005679 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005680{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005681 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005682 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005683 struct desktop_shell *shell = data;
5684 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005685 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005686
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005687 focus_surface = seat->keyboard->focus;
5688 if (!focus_surface)
5689 return;
5690
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005691 wl_signal_emit(&compositor->kill_signal, focus_surface);
5692
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005693 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005694 wl_client_get_credentials(client, &pid, NULL, NULL);
5695
5696 /* Skip clients that we launched ourselves (the credentials of
5697 * the socketpair is ours) */
5698 if (pid == getpid())
5699 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005700
Daniel Stone325fc2d2012-05-30 16:31:58 +01005701 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005702}
5703
5704static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005705workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005706 uint32_t key, void *data)
5707{
5708 struct desktop_shell *shell = data;
5709 unsigned int new_index = shell->workspaces.current;
5710
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005711 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005712 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005713 if (new_index != 0)
5714 new_index--;
5715
5716 change_workspace(shell, new_index);
5717}
5718
5719static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005720workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005721 uint32_t key, void *data)
5722{
5723 struct desktop_shell *shell = data;
5724 unsigned int new_index = shell->workspaces.current;
5725
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005726 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005727 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005728 if (new_index < shell->workspaces.num - 1)
5729 new_index++;
5730
5731 change_workspace(shell, new_index);
5732}
5733
5734static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005735workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005736 uint32_t key, void *data)
5737{
5738 struct desktop_shell *shell = data;
5739 unsigned int new_index;
5740
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005741 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005742 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005743 new_index = key - KEY_F1;
5744 if (new_index >= shell->workspaces.num)
5745 new_index = shell->workspaces.num - 1;
5746
5747 change_workspace(shell, new_index);
5748}
5749
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005750static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005751workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005752 uint32_t key, void *data)
5753{
5754 struct desktop_shell *shell = data;
5755 unsigned int new_index = shell->workspaces.current;
5756
5757 if (shell->locked)
5758 return;
5759
5760 if (new_index != 0)
5761 new_index--;
5762
5763 take_surface_to_workspace_by_seat(shell, seat, new_index);
5764}
5765
5766static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005767workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005768 uint32_t key, void *data)
5769{
5770 struct desktop_shell *shell = data;
5771 unsigned int new_index = shell->workspaces.current;
5772
5773 if (shell->locked)
5774 return;
5775
5776 if (new_index < shell->workspaces.num - 1)
5777 new_index++;
5778
5779 take_surface_to_workspace_by_seat(shell, seat, new_index);
5780}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005781
5782static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005783shell_reposition_view_on_output_destroy(struct weston_view *view)
5784{
5785 struct weston_output *output, *first_output;
5786 struct weston_compositor *ec = view->surface->compositor;
5787 struct shell_surface *shsurf;
5788 float x, y;
5789 int visible;
5790
5791 x = view->geometry.x;
5792 y = view->geometry.y;
5793
5794 /* At this point the destroyed output is not in the list anymore.
5795 * If the view is still visible somewhere, we leave where it is,
5796 * otherwise, move it to the first output. */
5797 visible = 0;
5798 wl_list_for_each(output, &ec->output_list, link) {
5799 if (pixman_region32_contains_point(&output->region,
5800 x, y, NULL)) {
5801 visible = 1;
5802 break;
5803 }
5804 }
5805
5806 if (!visible) {
5807 first_output = container_of(ec->output_list.next,
5808 struct weston_output, link);
5809
5810 x = first_output->x + first_output->width / 4;
5811 y = first_output->y + first_output->height / 4;
5812 }
5813
5814 weston_view_set_position(view, x, y);
5815
5816 shsurf = get_shell_surface(view->surface);
5817
5818 if (shsurf) {
5819 shsurf->saved_position_valid = false;
5820 shsurf->next_state.maximized = false;
5821 shsurf->next_state.fullscreen = false;
5822 shsurf->state_changed = true;
5823 }
5824}
5825
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005826void
5827shell_for_each_layer(struct desktop_shell *shell,
5828 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005829{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005830 struct workspace **ws;
5831
5832 func(shell, &shell->fullscreen_layer, data);
5833 func(shell, &shell->panel_layer, data);
5834 func(shell, &shell->background_layer, data);
5835 func(shell, &shell->lock_layer, data);
5836 func(shell, &shell->input_panel_layer, data);
5837
5838 wl_array_for_each(ws, &shell->workspaces.array)
5839 func(shell, &(*ws)->layer, data);
5840}
5841
5842static void
5843shell_output_destroy_move_layer(struct desktop_shell *shell,
5844 struct weston_layer *layer,
5845 void *data)
5846{
5847 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005848 struct weston_view *view;
5849
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005850 wl_list_for_each(view, &layer->view_list, layer_link) {
5851 if (view->output != output)
5852 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005853
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005854 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005855 }
5856}
5857
5858static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005859handle_output_destroy(struct wl_listener *listener, void *data)
5860{
5861 struct shell_output *output_listener =
5862 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005863 struct weston_output *output = output_listener->output;
5864 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005865
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005866 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005867
Xiong Zhang6b481422013-10-23 13:58:32 +08005868 wl_list_remove(&output_listener->destroy_listener.link);
5869 wl_list_remove(&output_listener->link);
5870 free(output_listener);
5871}
5872
5873static void
5874create_shell_output(struct desktop_shell *shell,
5875 struct weston_output *output)
5876{
5877 struct shell_output *shell_output;
5878
5879 shell_output = zalloc(sizeof *shell_output);
5880 if (shell_output == NULL)
5881 return;
5882
5883 shell_output->output = output;
5884 shell_output->shell = shell;
5885 shell_output->destroy_listener.notify = handle_output_destroy;
5886 wl_signal_add(&output->destroy_signal,
5887 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005888 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005889}
5890
5891static void
5892handle_output_create(struct wl_listener *listener, void *data)
5893{
5894 struct desktop_shell *shell =
5895 container_of(listener, struct desktop_shell, output_create_listener);
5896 struct weston_output *output = (struct weston_output *)data;
5897
5898 create_shell_output(shell, output);
5899}
5900
5901static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005902handle_output_move_layer(struct desktop_shell *shell,
5903 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005904{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005905 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005906 struct weston_view *view;
5907 float x, y;
5908
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005909 wl_list_for_each(view, &layer->view_list, layer_link) {
5910 if (view->output != output)
5911 continue;
5912
5913 x = view->geometry.x + output->move_x;
5914 y = view->geometry.y + output->move_y;
5915 weston_view_set_position(view, x, y);
5916 }
5917}
5918
5919static void
5920handle_output_move(struct wl_listener *listener, void *data)
5921{
5922 struct desktop_shell *shell;
5923
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005924 shell = container_of(listener, struct desktop_shell,
5925 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005926
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005927 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005928}
5929
5930static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005931setup_output_destroy_handler(struct weston_compositor *ec,
5932 struct desktop_shell *shell)
5933{
5934 struct weston_output *output;
5935
5936 wl_list_init(&shell->output_list);
5937 wl_list_for_each(output, &ec->output_list, link)
5938 create_shell_output(shell, output);
5939
5940 shell->output_create_listener.notify = handle_output_create;
5941 wl_signal_add(&ec->output_created_signal,
5942 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005943
5944 shell->output_move_listener.notify = handle_output_move;
5945 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005946}
5947
5948static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005949shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005950{
Tiago Vignattibe143262012-04-16 17:31:41 +03005951 struct desktop_shell *shell =
5952 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005953 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005954 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005955
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005956 /* Force state to unlocked so we don't try to fade */
5957 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005958 if (shell->child.client)
5959 wl_client_destroy(shell->child.client);
5960
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005961 wl_list_remove(&shell->idle_listener.link);
5962 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005963
5964 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005965
Xiong Zhang6b481422013-10-23 13:58:32 +08005966 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5967 wl_list_remove(&shell_output->destroy_listener.link);
5968 wl_list_remove(&shell_output->link);
5969 free(shell_output);
5970 }
5971
5972 wl_list_remove(&shell->output_create_listener.link);
5973
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005974 wl_array_for_each(ws, &shell->workspaces.array)
5975 workspace_destroy(*ws);
5976 wl_array_release(&shell->workspaces.array);
5977
Pekka Paalanen3c647232011-12-22 13:43:43 +02005978 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005979 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005980 free(shell);
5981}
5982
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005983static void
5984shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5985{
5986 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005987 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005988
5989 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005990 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5991 MODIFIER_CTRL | MODIFIER_ALT,
5992 terminate_binding, ec);
5993 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5994 click_to_activate_binding,
5995 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005996 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5997 click_to_activate_binding,
5998 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005999 weston_compositor_add_touch_binding(ec, 0,
6000 touch_to_activate_binding,
6001 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006002 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6003 MODIFIER_SUPER | MODIFIER_ALT,
6004 surface_opacity_binding, NULL);
6005 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6006 MODIFIER_SUPER, zoom_axis_binding,
6007 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006008
6009 /* configurable bindings */
6010 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006011 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6012 zoom_key_binding, NULL);
6013 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6014 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006015 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6016 maximize_binding, NULL);
6017 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6018 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006019 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6020 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006021 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006022 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6023 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006024 weston_compositor_add_button_binding(ec, BTN_LEFT,
6025 mod | MODIFIER_SHIFT,
6026 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006027
6028 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6029 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6030 rotate_binding, NULL);
6031
Daniel Stone325fc2d2012-05-30 16:31:58 +01006032 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6033 shell);
6034 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6035 ec);
6036 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6037 backlight_binding, ec);
6038 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6039 ec);
6040 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6041 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006042 weston_compositor_add_key_binding(ec, KEY_K, mod,
6043 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006044 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6045 workspace_up_binding, shell);
6046 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6047 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006048 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6049 workspace_move_surface_up_binding,
6050 shell);
6051 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6052 workspace_move_surface_down_binding,
6053 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006054
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006055 if (shell->exposay_modifier)
6056 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6057 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006058
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006059 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6060 if (shell->workspaces.num > 1) {
6061 num_workspace_bindings = shell->workspaces.num;
6062 if (num_workspace_bindings > 6)
6063 num_workspace_bindings = 6;
6064 for (i = 0; i < num_workspace_bindings; i++)
6065 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6066 workspace_f_binding,
6067 shell);
6068 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006069
6070 /* Debug bindings */
6071 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6072 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006073}
6074
Jason Ekstrand024177c2014-04-21 19:42:58 -05006075static void
6076handle_seat_created(struct wl_listener *listener, void *data)
6077{
6078 struct weston_seat *seat = data;
6079
6080 create_shell_seat(seat);
6081}
6082
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006083WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006084module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006085 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006086{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006087 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006088 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006089 struct workspace **pws;
6090 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006091 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006092
Peter Huttererf3d62272013-08-08 11:57:05 +10006093 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006094 if (shell == NULL)
6095 return -1;
6096
Kristian Høgsberg75840622011-09-06 13:48:16 -04006097 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006098
6099 shell->destroy_listener.notify = shell_destroy;
6100 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006101 shell->idle_listener.notify = idle_handler;
6102 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6103 shell->wake_listener.notify = wake_handler;
6104 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006105
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006106 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006107 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006108 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006109 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006110 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006111 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006112 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006113 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006114 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006115 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006116
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006117 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6118 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006119 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6120 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006121 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006122
6123 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006124 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006125
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006126 if (input_panel_setup(shell) < 0)
6127 return -1;
6128
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006129 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006130
Daniel Stonedf8133b2013-11-19 11:37:14 +01006131 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6132 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6133
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006134 for (i = 0; i < shell->workspaces.num; i++) {
6135 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6136 if (pws == NULL)
6137 return -1;
6138
6139 *pws = workspace_create();
6140 if (*pws == NULL)
6141 return -1;
6142 }
6143 activate_workspace(shell, 0);
6144
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006145 weston_layer_init(&shell->minimized_layer, NULL);
6146
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006147 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006148 wl_list_init(&shell->workspaces.animation.link);
6149 shell->workspaces.animation.frame = animate_workspace_change_frame;
6150
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006151 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006152 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006153 return -1;
6154
Rafael Antognollie2a34552013-12-03 15:35:45 -02006155 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6156 shell, bind_xdg_shell) == NULL)
6157 return -1;
6158
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006159 if (wl_global_create(ec->wl_display,
6160 &desktop_shell_interface, 2,
6161 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006162 return -1;
6163
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006164 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6165 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006166 return -1;
6167
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006168 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6169 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006170 return -1;
6171
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006172 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006173
Xiong Zhang6b481422013-10-23 13:58:32 +08006174 setup_output_destroy_handler(ec, shell);
6175
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006176 loop = wl_display_get_event_loop(ec->wl_display);
6177 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006178
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006179 shell->screensaver.timer =
6180 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6181
Jason Ekstrand024177c2014-04-21 19:42:58 -05006182 wl_list_for_each(seat, &ec->seat_list, link)
6183 handle_seat_created(NULL, seat);
6184 shell->seat_create_listener.notify = handle_seat_created;
6185 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006186
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006187 screenshooter_create(ec);
6188
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006189 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006190
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006191 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006192
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006193 return 0;
6194}