blob: fe03420ef5fc01174695146b63e818715cd0afda [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
Philip Withnall648a4dd2013-11-25 18:01:44 +000066/*
67 * Surface stacking and ordering.
68 *
69 * This is handled using several linked lists of surfaces, organised into
70 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
71 * above all of the surfaces in the layer below. The set of layers is static and
72 * in the following order (top-most first):
73 * • Lock layer (only ever displayed on its own)
74 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010075 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000076 * • Fullscreen layer
77 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000078 * • Workspace layers
79 * • Background layer
80 *
81 * The list of layers may be manipulated to remove whole layers of surfaces from
82 * display. For example, when locking the screen, all layers except the lock
83 * layer are removed.
84 *
85 * A surface’s layer is modified on configuring the surface, in
86 * set_surface_type() (which is only called when the surface’s type change is
87 * _committed_). If a surface’s type changes (e.g. when making a window
88 * fullscreen) its layer changes too.
89 *
90 * In order to allow popup and transient surfaces to be correctly stacked above
91 * their parent surfaces, each surface tracks both its parent surface, and a
92 * linked list of its children. When a surface’s layer is updated, so are the
93 * layers of its children. Note that child surfaces are *not* the same as
94 * subsurfaces — child/parent surfaces are purely for maintaining stacking
95 * order.
96 *
97 * The children_link list of siblings of a surface (i.e. those surfaces which
98 * have the same parent) only contains weston_surfaces which have a
99 * shell_surface. Stacking is not implemented for non-shell_surface
100 * weston_surfaces. This means that the following implication does *not* hold:
101 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
102 */
103
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200104struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500105 struct wl_resource *resource;
106 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200107
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500109 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600110 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700112 struct wl_listener resource_destroy_listener;
113
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400114 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000115 struct wl_list children_list; /* child surfaces of this one */
116 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300117 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200118
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800119 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400120 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500121 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200122 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800123 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200124 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800125 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700126 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800127 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100128
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500129 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200130 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500131 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200132 } rotation;
133
134 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100135 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500136 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100137 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400138 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 } popup;
140
Alex Wu4539b082012-03-01 12:57:46 +0800141 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300142 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400143 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300144 } transient;
145
146 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800147 enum wl_shell_surface_fullscreen_method type;
148 struct weston_transform transform; /* matrix from x, y */
149 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500150 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800151 } fullscreen;
152
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200153 struct weston_transform workspace_transform;
154
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500155 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200157 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100158 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400159
160 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200161
162 struct {
163 bool maximized;
164 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200165 bool relative;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500166 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200167 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500168 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500169
170 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200171};
172
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300173struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400174 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300175 struct shell_surface *shsurf;
176 struct wl_listener shsurf_destroy_listener;
177};
178
Rusty Lynch1084da52013-08-15 09:10:08 -0700179struct shell_touch_grab {
180 struct weston_touch_grab grab;
181 struct shell_surface *shsurf;
182 struct wl_listener shsurf_destroy_listener;
183 struct weston_touch *touch;
184};
185
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300186struct weston_move_grab {
187 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100188 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500189};
190
Rusty Lynch1084da52013-08-15 09:10:08 -0700191struct weston_touch_move_grab {
192 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800193 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700194 wl_fixed_t dx, dy;
195};
196
Pekka Paalanen460099f2012-01-20 16:48:25 +0200197struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300198 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500199 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200200 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200201 float x;
202 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 } center;
204};
205
Giulio Camuffo5085a752013-03-25 21:42:45 +0100206struct shell_seat {
207 struct weston_seat *seat;
208 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500209 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100210
211 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400212 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100213 struct wl_list surfaces_list;
214 struct wl_client *client;
215 int32_t initial_up;
216 } popup_grab;
217};
218
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800219struct shell_client {
220 struct wl_resource *resource;
221 struct wl_client *client;
222 struct desktop_shell *shell;
223 struct wl_listener destroy_listener;
224 struct wl_event_source *ping_timer;
225 uint32_t ping_serial;
226 int unresponsive;
227};
228
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100229void
230set_alpha_if_fullscreen(struct shell_surface *shsurf)
231{
232 if (shsurf && shsurf->state.fullscreen)
233 shsurf->fullscreen.black_view->alpha = 0.25;
234}
235
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -0800236static struct shell_client *
237get_shell_client(struct wl_client *client);
238
Alex Wubd3354b2012-04-17 17:20:49 +0800239static struct desktop_shell *
240shell_surface_get_shell(struct shell_surface *shsurf);
241
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500242static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400243surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500244
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300245static void
246shell_fade_startup(struct desktop_shell *shell);
247
Philip Withnallbecb77e2013-11-25 18:01:30 +0000248static struct shell_seat *
249get_shell_seat(struct weston_seat *seat);
250
Philip Withnall648a4dd2013-11-25 18:01:44 +0000251static void
252shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
253
Alex Wubd3354b2012-04-17 17:20:49 +0800254static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200255shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
256
257static bool
258shell_surface_is_xdg_surface(struct shell_surface *shsurf);
259
260static bool
261shell_surface_is_xdg_popup(struct shell_surface *shsurf);
262
263static void
264shell_surface_set_parent(struct shell_surface *shsurf,
265 struct weston_surface *parent);
266
267static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800268shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
269{
270 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800272
273 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100274
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800276 return false;
277
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
279 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800280 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800282}
283
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500284static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400285destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300286{
287 struct shell_grab *grab;
288
289 grab = container_of(listener, struct shell_grab,
290 shsurf_destroy_listener);
291
292 grab->shsurf = NULL;
293}
294
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800295struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500296get_default_view(struct weston_surface *surface)
297{
298 struct shell_surface *shsurf;
299 struct weston_view *view;
300
301 if (!surface || wl_list_empty(&surface->views))
302 return NULL;
303
304 shsurf = get_shell_surface(surface);
305 if (shsurf)
306 return shsurf->view;
307
308 wl_list_for_each(view, &surface->views, surface_link)
309 if (weston_view_is_mapped(view))
310 return view;
311
312 return container_of(surface->views.next, struct weston_view, surface_link);
313}
314
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300315static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400316popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400317
318static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400322 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300324{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300325 struct desktop_shell *shell = shsurf->shell;
326
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400327 popup_grab_end(pointer);
328
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300329 grab->grab.interface = interface;
330 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400331 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500332 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400333 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300334
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700335 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400336 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400337 if (shell->child.desktop_shell) {
338 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
339 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 weston_pointer_set_focus(pointer,
341 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400342 wl_fixed_from_int(0),
343 wl_fixed_from_int(0));
344 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345}
346
347static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300348shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300349{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700350 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400351 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700352 grab->shsurf->grabbed = 0;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800353 grab->shsurf->resize_edges = 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700354 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300355
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700356 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300357}
358
359static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700360shell_touch_grab_start(struct shell_touch_grab *grab,
361 const struct weston_touch_grab_interface *interface,
362 struct shell_surface *shsurf,
363 struct weston_touch *touch)
364{
365 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800366
Rusty Lynch1084da52013-08-15 09:10:08 -0700367 grab->grab.interface = interface;
368 grab->shsurf = shsurf;
369 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
370 wl_signal_add(&shsurf->destroy_signal,
371 &grab->shsurf_destroy_listener);
372
373 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700374 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700375
376 weston_touch_start_grab(touch, &grab->grab);
377 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500378 weston_touch_set_focus(touch->seat,
379 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700380}
381
382static void
383shell_touch_grab_end(struct shell_touch_grab *grab)
384{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700385 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700386 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700387 grab->shsurf->grabbed = 0;
388 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700389
390 weston_touch_end_grab(grab->touch);
391}
392
393static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500394center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800395 struct weston_output *output);
396
Daniel Stone496ca172012-05-30 16:31:42 +0100397static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300398get_modifier(char *modifier)
399{
400 if (!modifier)
401 return MODIFIER_SUPER;
402
403 if (!strcmp("ctrl", modifier))
404 return MODIFIER_CTRL;
405 else if (!strcmp("alt", modifier))
406 return MODIFIER_ALT;
407 else if (!strcmp("super", modifier))
408 return MODIFIER_SUPER;
409 else
410 return MODIFIER_SUPER;
411}
412
Juan Zhaoe10d2792012-04-25 19:09:52 +0800413static enum animation_type
414get_animation_type(char *animation)
415{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800416 if (!animation)
417 return ANIMATION_NONE;
418
Juan Zhaoe10d2792012-04-25 19:09:52 +0800419 if (!strcmp("zoom", animation))
420 return ANIMATION_ZOOM;
421 else if (!strcmp("fade", animation))
422 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100423 else if (!strcmp("dim-layer", animation))
424 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800425 else
426 return ANIMATION_NONE;
427}
428
Alex Wu4539b082012-03-01 12:57:46 +0800429static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400430shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200431{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400432 struct weston_config_section *section;
433 int duration;
434 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200435
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400436 section = weston_config_get_section(shell->compositor->config,
437 "screensaver", NULL, NULL);
438 weston_config_section_get_string(section,
439 "path", &shell->screensaver.path, NULL);
440 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200441 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400442
443 section = weston_config_get_section(shell->compositor->config,
444 "shell", NULL, NULL);
445 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100446 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100447 shell->client = s;
448 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400449 "binding-modifier", &s, "super");
450 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200451 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800452
453 weston_config_section_get_string(section,
454 "exposay-modifier", &s, "none");
455 if (strcmp(s, "none") == 0)
456 shell->exposay_modifier = 0;
457 else
458 shell->exposay_modifier = get_modifier(s);
459 free(s);
460
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400461 weston_config_section_get_string(section, "animation", &s, "none");
462 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200463 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700464 weston_config_section_get_string(section,
465 "startup-animation", &s, "fade");
466 shell->startup_animation_type = get_animation_type(s);
467 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700468 if (shell->startup_animation_type == ANIMATION_ZOOM)
469 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100470 weston_config_section_get_string(section, "focus-animation", &s, "none");
471 shell->focus_animation_type = get_animation_type(s);
472 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400473 weston_config_section_get_uint(section, "num-workspaces",
474 &shell->workspaces.num,
475 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200476}
477
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800478struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100479get_default_output(struct weston_compositor *compositor)
480{
481 return container_of(compositor->output_list.next,
482 struct weston_output, link);
483}
484
485
486/* no-op func for checking focus surface */
487static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600488focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100489{
490}
491
492static struct focus_surface *
493get_focus_surface(struct weston_surface *surface)
494{
495 if (surface->configure == focus_surface_configure)
496 return surface->configure_private;
497 else
498 return NULL;
499}
500
501static bool
502is_focus_surface (struct weston_surface *es)
503{
504 return (es->configure == focus_surface_configure);
505}
506
507static bool
508is_focus_view (struct weston_view *view)
509{
510 return is_focus_surface (view->surface);
511}
512
513static struct focus_surface *
514create_focus_surface(struct weston_compositor *ec,
515 struct weston_output *output)
516{
517 struct focus_surface *fsurf = NULL;
518 struct weston_surface *surface = NULL;
519
520 fsurf = malloc(sizeof *fsurf);
521 if (!fsurf)
522 return NULL;
523
524 fsurf->surface = weston_surface_create(ec);
525 surface = fsurf->surface;
526 if (surface == NULL) {
527 free(fsurf);
528 return NULL;
529 }
530
531 surface->configure = focus_surface_configure;
532 surface->output = output;
533 surface->configure_private = fsurf;
534
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800535 fsurf->view = weston_view_create(surface);
536 if (fsurf->view == NULL) {
537 weston_surface_destroy(surface);
538 free(fsurf);
539 return NULL;
540 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100541 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100542
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600543 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600544 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100545 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
546 pixman_region32_fini(&surface->opaque);
547 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
548 output->width, output->height);
549 pixman_region32_fini(&surface->input);
550 pixman_region32_init(&surface->input);
551
552 wl_list_init(&fsurf->workspace_transform.link);
553
554 return fsurf;
555}
556
557static void
558focus_surface_destroy(struct focus_surface *fsurf)
559{
560 weston_surface_destroy(fsurf->surface);
561 free(fsurf);
562}
563
564static void
565focus_animation_done(struct weston_view_animation *animation, void *data)
566{
567 struct workspace *ws = data;
568
569 ws->focus_animation = NULL;
570}
571
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200572static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200573focus_state_destroy(struct focus_state *state)
574{
575 wl_list_remove(&state->seat_destroy_listener.link);
576 wl_list_remove(&state->surface_destroy_listener.link);
577 free(state);
578}
579
580static void
581focus_state_seat_destroy(struct wl_listener *listener, void *data)
582{
583 struct focus_state *state = container_of(listener,
584 struct focus_state,
585 seat_destroy_listener);
586
587 wl_list_remove(&state->link);
588 focus_state_destroy(state);
589}
590
591static void
592focus_state_surface_destroy(struct wl_listener *listener, void *data)
593{
594 struct focus_state *state = container_of(listener,
595 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400596 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400597 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500598 struct weston_surface *main_surface, *next;
599 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200600
Pekka Paalanen01388e22013-04-25 13:57:44 +0300601 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
602
Kristian Høgsberge3778222012-07-31 17:29:30 -0400603 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500604 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
605 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400606 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100607 if (is_focus_view(view))
608 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400609
Jason Ekstranda7af7042013-10-12 22:38:11 -0500610 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400611 break;
612 }
613
Pekka Paalanen01388e22013-04-25 13:57:44 +0300614 /* if the focus was a sub-surface, activate its main surface */
615 if (main_surface != state->keyboard_focus)
616 next = main_surface;
617
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100618 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400619 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100620 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400621 activate(shell, next, state->seat);
622 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100623 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
624 if (state->ws->focus_animation)
625 weston_view_animation_destroy(state->ws->focus_animation);
626
627 state->ws->focus_animation = weston_fade_run(
628 state->ws->fsurf_front->view,
629 state->ws->fsurf_front->view->alpha, 0.0, 300,
630 focus_animation_done, state->ws);
631 }
632
Kristian Høgsberge3778222012-07-31 17:29:30 -0400633 wl_list_remove(&state->link);
634 focus_state_destroy(state);
635 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200636}
637
638static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400639focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200640{
Jonas Ådahl04769742012-06-13 00:01:24 +0200641 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200642
643 state = malloc(sizeof *state);
644 if (state == NULL)
645 return NULL;
646
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100647 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400648 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200649 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400650 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200651
652 state->seat_destroy_listener.notify = focus_state_seat_destroy;
653 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400654 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200655 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400656 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200657
658 return state;
659}
660
Jonas Ådahl8538b222012-08-29 22:13:03 +0200661static struct focus_state *
662ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
663{
664 struct workspace *ws = get_current_workspace(shell);
665 struct focus_state *state;
666
667 wl_list_for_each(state, &ws->focus_list, link)
668 if (state->seat == seat)
669 break;
670
671 if (&state->link == &ws->focus_list)
672 state = focus_state_create(seat, ws);
673
674 return state;
675}
676
Jonas Ådahl04769742012-06-13 00:01:24 +0200677static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800678focus_state_set_focus(struct focus_state *state,
679 struct weston_surface *surface)
680{
681 if (state->keyboard_focus) {
682 wl_list_remove(&state->surface_destroy_listener.link);
683 wl_list_init(&state->surface_destroy_listener.link);
684 }
685
686 state->keyboard_focus = surface;
687 if (surface)
688 wl_signal_add(&surface->destroy_signal,
689 &state->surface_destroy_listener);
690}
691
692static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400693restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200694{
695 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400696 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200697
698 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800699 if (state->seat->keyboard == NULL)
700 continue;
701
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400702 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200703
Kristian Høgsberge3148752013-05-06 23:19:49 -0400704 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200705 }
706}
707
708static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200709replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
710 struct weston_seat *seat)
711{
712 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200713
714 wl_list_for_each(state, &ws->focus_list, link) {
715 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800716 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200717 return;
718 }
719 }
720}
721
722static void
723drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
724 struct weston_surface *surface)
725{
726 struct focus_state *state;
727
728 wl_list_for_each(state, &ws->focus_list, link)
729 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800730 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200731}
732
733static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100734animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
735 struct weston_view *from, struct weston_view *to)
736{
737 struct weston_output *output;
738 bool focus_surface_created = false;
739
740 /* FIXME: Only support dim animation using two layers */
741 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
742 return;
743
744 output = get_default_output(shell->compositor);
745 if (ws->fsurf_front == NULL && (from || to)) {
746 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800747 if (ws->fsurf_front == NULL)
748 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100749 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800750
751 ws->fsurf_back = create_focus_surface(shell->compositor, output);
752 if (ws->fsurf_back == NULL) {
753 focus_surface_destroy(ws->fsurf_front);
754 return;
755 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100756 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800757
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100758 focus_surface_created = true;
759 } else {
760 wl_list_remove(&ws->fsurf_front->view->layer_link);
761 wl_list_remove(&ws->fsurf_back->view->layer_link);
762 }
763
764 if (ws->focus_animation) {
765 weston_view_animation_destroy(ws->focus_animation);
766 ws->focus_animation = NULL;
767 }
768
769 if (to)
770 wl_list_insert(&to->layer_link,
771 &ws->fsurf_front->view->layer_link);
772 else if (from)
773 wl_list_insert(&ws->layer.view_list,
774 &ws->fsurf_front->view->layer_link);
775
776 if (focus_surface_created) {
777 ws->focus_animation = weston_fade_run(
778 ws->fsurf_front->view,
779 ws->fsurf_front->view->alpha, 0.6, 300,
780 focus_animation_done, ws);
781 } else if (from) {
782 wl_list_insert(&from->layer_link,
783 &ws->fsurf_back->view->layer_link);
784 ws->focus_animation = weston_stable_fade_run(
785 ws->fsurf_front->view, 0.0,
786 ws->fsurf_back->view, 0.6,
787 focus_animation_done, ws);
788 } else if (to) {
789 wl_list_insert(&ws->layer.view_list,
790 &ws->fsurf_back->view->layer_link);
791 ws->focus_animation = weston_stable_fade_run(
792 ws->fsurf_front->view, 0.0,
793 ws->fsurf_back->view, 0.6,
794 focus_animation_done, ws);
795 }
796}
797
798static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200799workspace_destroy(struct workspace *ws)
800{
Jonas Ådahl04769742012-06-13 00:01:24 +0200801 struct focus_state *state, *next;
802
803 wl_list_for_each_safe(state, next, &ws->focus_list, link)
804 focus_state_destroy(state);
805
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100806 if (ws->fsurf_front)
807 focus_surface_destroy(ws->fsurf_front);
808 if (ws->fsurf_back)
809 focus_surface_destroy(ws->fsurf_back);
810
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200811 free(ws);
812}
813
Jonas Ådahl04769742012-06-13 00:01:24 +0200814static void
815seat_destroyed(struct wl_listener *listener, void *data)
816{
817 struct weston_seat *seat = data;
818 struct focus_state *state, *next;
819 struct workspace *ws = container_of(listener,
820 struct workspace,
821 seat_destroyed_listener);
822
823 wl_list_for_each_safe(state, next, &ws->focus_list, link)
824 if (state->seat == seat)
825 wl_list_remove(&state->link);
826}
827
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200828static struct workspace *
829workspace_create(void)
830{
831 struct workspace *ws = malloc(sizeof *ws);
832 if (ws == NULL)
833 return NULL;
834
835 weston_layer_init(&ws->layer, NULL);
836
Jonas Ådahl04769742012-06-13 00:01:24 +0200837 wl_list_init(&ws->focus_list);
838 wl_list_init(&ws->seat_destroyed_listener.link);
839 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100840 ws->fsurf_front = NULL;
841 ws->fsurf_back = NULL;
842 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200843
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200844 return ws;
845}
846
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200847static int
848workspace_is_empty(struct workspace *ws)
849{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500850 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200851}
852
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200853static struct workspace *
854get_workspace(struct desktop_shell *shell, unsigned int index)
855{
856 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200857 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200858 pws += index;
859 return *pws;
860}
861
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800862struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200863get_current_workspace(struct desktop_shell *shell)
864{
865 return get_workspace(shell, shell->workspaces.current);
866}
867
868static void
869activate_workspace(struct desktop_shell *shell, unsigned int index)
870{
871 struct workspace *ws;
872
873 ws = get_workspace(shell, index);
874 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
875
876 shell->workspaces.current = index;
877}
878
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200879static unsigned int
880get_output_height(struct weston_output *output)
881{
882 return abs(output->region.extents.y1 - output->region.extents.y2);
883}
884
885static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100886view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200887{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200888 struct weston_transform *transform;
889
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100890 if (is_focus_view(view)) {
891 struct focus_surface *fsurf = get_focus_surface(view->surface);
892 transform = &fsurf->workspace_transform;
893 } else {
894 struct shell_surface *shsurf = get_shell_surface(view->surface);
895 transform = &shsurf->workspace_transform;
896 }
897
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200898 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500899 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100900 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200901
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100902 weston_matrix_init(&transform->matrix);
903 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200904 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500905 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200906}
907
908static void
909workspace_translate_out(struct workspace *ws, double fraction)
910{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912 unsigned int height;
913 double d;
914
Jason Ekstranda7af7042013-10-12 22:38:11 -0500915 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
916 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200917 d = height * fraction;
918
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100919 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200920 }
921}
922
923static void
924workspace_translate_in(struct workspace *ws, double fraction)
925{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200927 unsigned int height;
928 double d;
929
Jason Ekstranda7af7042013-10-12 22:38:11 -0500930 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
931 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200932
933 if (fraction > 0)
934 d = -(height - height * fraction);
935 else
936 d = height + height * fraction;
937
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100938 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200939 }
940}
941
942static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200943broadcast_current_workspace_state(struct desktop_shell *shell)
944{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700945 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200946
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700947 wl_resource_for_each(resource, &shell->workspaces.client_list)
948 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200949 shell->workspaces.current,
950 shell->workspaces.num);
951}
952
953static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200954reverse_workspace_change_animation(struct desktop_shell *shell,
955 unsigned int index,
956 struct workspace *from,
957 struct workspace *to)
958{
959 shell->workspaces.current = index;
960
961 shell->workspaces.anim_to = to;
962 shell->workspaces.anim_from = from;
963 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
964 shell->workspaces.anim_timestamp = 0;
965
Scott Moreau4272e632012-08-13 09:58:41 -0600966 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967}
968
969static void
970workspace_deactivate_transforms(struct workspace *ws)
971{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100973 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200974
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100976 if (is_focus_view(view)) {
977 struct focus_surface *fsurf = get_focus_surface(view->surface);
978 transform = &fsurf->workspace_transform;
979 } else {
980 struct shell_surface *shsurf = get_shell_surface(view->surface);
981 transform = &shsurf->workspace_transform;
982 }
983
984 if (!wl_list_empty(&transform->link)) {
985 wl_list_remove(&transform->link);
986 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200987 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200989 }
990}
991
992static void
993finish_workspace_change_animation(struct desktop_shell *shell,
994 struct workspace *from,
995 struct workspace *to)
996{
Scott Moreau4272e632012-08-13 09:58:41 -0600997 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998
999 wl_list_remove(&shell->workspaces.animation.link);
1000 workspace_deactivate_transforms(from);
1001 workspace_deactivate_transforms(to);
1002 shell->workspaces.anim_to = NULL;
1003
1004 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1005}
1006
1007static void
1008animate_workspace_change_frame(struct weston_animation *animation,
1009 struct weston_output *output, uint32_t msecs)
1010{
1011 struct desktop_shell *shell =
1012 container_of(animation, struct desktop_shell,
1013 workspaces.animation);
1014 struct workspace *from = shell->workspaces.anim_from;
1015 struct workspace *to = shell->workspaces.anim_to;
1016 uint32_t t;
1017 double x, y;
1018
1019 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1020 finish_workspace_change_animation(shell, from, to);
1021 return;
1022 }
1023
1024 if (shell->workspaces.anim_timestamp == 0) {
1025 if (shell->workspaces.anim_current == 0.0)
1026 shell->workspaces.anim_timestamp = msecs;
1027 else
1028 shell->workspaces.anim_timestamp =
1029 msecs -
1030 /* Invers of movement function 'y' below. */
1031 (asin(1.0 - shell->workspaces.anim_current) *
1032 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1033 M_2_PI);
1034 }
1035
1036 t = msecs - shell->workspaces.anim_timestamp;
1037
1038 /*
1039 * x = [0, π/2]
1040 * y(x) = sin(x)
1041 */
1042 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1043 y = sin(x);
1044
1045 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001046 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001047
1048 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1049 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1050 shell->workspaces.anim_current = y;
1051
Scott Moreau4272e632012-08-13 09:58:41 -06001052 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001053 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001054 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001056}
1057
1058static void
1059animate_workspace_change(struct desktop_shell *shell,
1060 unsigned int index,
1061 struct workspace *from,
1062 struct workspace *to)
1063{
1064 struct weston_output *output;
1065
1066 int dir;
1067
1068 if (index > shell->workspaces.current)
1069 dir = -1;
1070 else
1071 dir = 1;
1072
1073 shell->workspaces.current = index;
1074
1075 shell->workspaces.anim_dir = dir;
1076 shell->workspaces.anim_from = from;
1077 shell->workspaces.anim_to = to;
1078 shell->workspaces.anim_current = 0.0;
1079 shell->workspaces.anim_timestamp = 0;
1080
1081 output = container_of(shell->compositor->output_list.next,
1082 struct weston_output, link);
1083 wl_list_insert(&output->animation_list,
1084 &shell->workspaces.animation.link);
1085
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001086 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001087
1088 workspace_translate_in(to, 0);
1089
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001090 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001091
Scott Moreau4272e632012-08-13 09:58:41 -06001092 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001093}
1094
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001095static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001096update_workspace(struct desktop_shell *shell, unsigned int index,
1097 struct workspace *from, struct workspace *to)
1098{
1099 shell->workspaces.current = index;
1100 wl_list_insert(&from->layer.link, &to->layer.link);
1101 wl_list_remove(&from->layer.link);
1102}
1103
1104static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001105change_workspace(struct desktop_shell *shell, unsigned int index)
1106{
1107 struct workspace *from;
1108 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001109 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001110
1111 if (index == shell->workspaces.current)
1112 return;
1113
1114 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001116 return;
1117
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001118 from = get_current_workspace(shell);
1119 to = get_workspace(shell, index);
1120
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001121 if (shell->workspaces.anim_from == to &&
1122 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001123 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001124 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001125 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126 return;
1127 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001128
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129 if (shell->workspaces.anim_to != NULL)
1130 finish_workspace_change_animation(shell,
1131 shell->workspaces.anim_from,
1132 shell->workspaces.anim_to);
1133
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001134 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001135
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001136 if (shell->focus_animation_type != ANIMATION_NONE) {
1137 wl_list_for_each(state, &from->focus_list, link)
1138 if (state->keyboard_focus)
1139 animate_focus_change(shell, from,
1140 get_default_view(state->keyboard_focus), NULL);
1141
1142 wl_list_for_each(state, &to->focus_list, link)
1143 if (state->keyboard_focus)
1144 animate_focus_change(shell, to,
1145 NULL, get_default_view(state->keyboard_focus));
1146 }
1147
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001148 if (workspace_is_empty(to) && workspace_is_empty(from))
1149 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001150 else
1151 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001152
1153 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001154}
1155
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001156static bool
1157workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1158{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001160 struct wl_list *e;
1161
1162 if (wl_list_empty(list))
1163 return false;
1164
1165 e = list->next;
1166
1167 if (e->next != list)
1168 return false;
1169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001171}
1172
1173static void
Philip Withnall659163d2013-11-25 18:01:36 +00001174move_surface_to_workspace(struct desktop_shell *shell,
1175 struct shell_surface *shsurf,
1176 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001177{
1178 struct workspace *from;
1179 struct workspace *to;
1180 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001181 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001182 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001183
1184 if (workspace == shell->workspaces.current)
1185 return;
1186
Philip Withnall659163d2013-11-25 18:01:36 +00001187 view = get_default_view(shsurf->surface);
1188 if (!view)
1189 return;
1190
1191 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1192
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001193 if (workspace >= shell->workspaces.num)
1194 workspace = shell->workspaces.num - 1;
1195
Jonas Ådahle9d22502012-08-29 22:13:01 +02001196 from = get_current_workspace(shell);
1197 to = get_workspace(shell, workspace);
1198
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 wl_list_remove(&view->layer_link);
1200 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001201
Philip Withnall648a4dd2013-11-25 18:01:44 +00001202 shell_surface_update_child_surface_layers(shsurf);
1203
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001205 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1206 if (!seat->keyboard)
1207 continue;
1208
1209 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001211 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001212 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001213
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001215}
1216
1217static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001218take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001219 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001220 unsigned int index)
1221{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001222 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224 struct shell_surface *shsurf;
1225 struct workspace *from;
1226 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001227 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001228
Pekka Paalanen01388e22013-04-25 13:57:44 +03001229 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 view = get_default_view(surface);
1231 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001232 index == shell->workspaces.current ||
1233 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001234 return;
1235
1236 from = get_current_workspace(shell);
1237 to = get_workspace(shell, index);
1238
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 wl_list_remove(&view->layer_link);
1240 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001241
Philip Withnall659163d2013-11-25 18:01:36 +00001242 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001243 if (shsurf != NULL)
1244 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001245
Jonas Ådahle9d22502012-08-29 22:13:01 +02001246 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001247 drop_focus_state(shell, from, surface);
1248
1249 if (shell->workspaces.anim_from == to &&
1250 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001251 wl_list_remove(&to->layer.link);
1252 wl_list_insert(from->layer.link.prev, &to->layer.link);
1253
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001254 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001255 broadcast_current_workspace_state(shell);
1256
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257 return;
1258 }
1259
1260 if (shell->workspaces.anim_to != NULL)
1261 finish_workspace_change_animation(shell,
1262 shell->workspaces.anim_from,
1263 shell->workspaces.anim_to);
1264
1265 if (workspace_is_empty(from) &&
1266 workspace_has_only(to, surface))
1267 update_workspace(shell, index, from, to);
1268 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001269 if (shsurf != NULL &&
1270 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001271 wl_list_insert(&shell->workspaces.anim_sticky_list,
1272 &shsurf->workspace_transform.link);
1273
1274 animate_workspace_change(shell, index, from, to);
1275 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001276
1277 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001278
1279 state = ensure_focus_state(shell, seat);
1280 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001281 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001282}
1283
1284static void
1285workspace_manager_move_surface(struct wl_client *client,
1286 struct wl_resource *resource,
1287 struct wl_resource *surface_resource,
1288 uint32_t workspace)
1289{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001290 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001291 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001292 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001293 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001294 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001295
Pekka Paalanen01388e22013-04-25 13:57:44 +03001296 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001297 shell_surface = get_shell_surface(main_surface);
1298 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001300
1301 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001302}
1303
1304static const struct workspace_manager_interface workspace_manager_implementation = {
1305 workspace_manager_move_surface,
1306};
1307
1308static void
1309unbind_resource(struct wl_resource *resource)
1310{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001311 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001312}
1313
1314static void
1315bind_workspace_manager(struct wl_client *client,
1316 void *data, uint32_t version, uint32_t id)
1317{
1318 struct desktop_shell *shell = data;
1319 struct wl_resource *resource;
1320
Jason Ekstranda85118c2013-06-27 20:17:02 -05001321 resource = wl_resource_create(client,
1322 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323
1324 if (resource == NULL) {
1325 weston_log("couldn't add workspace manager object");
1326 return;
1327 }
1328
Jason Ekstranda85118c2013-06-27 20:17:02 -05001329 wl_resource_set_implementation(resource,
1330 &workspace_manager_implementation,
1331 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001332 wl_list_insert(&shell->workspaces.client_list,
1333 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001334
1335 workspace_manager_send_state(resource,
1336 shell->workspaces.current,
1337 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001338}
1339
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001340static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001341touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1342 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1343{
1344}
1345
1346static void
1347touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1348{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001349 struct weston_touch_move_grab *move =
1350 (struct weston_touch_move_grab *) container_of(
1351 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001352
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001353 if (touch_id == 0)
1354 move->active = 0;
1355
Jonas Ådahl9484b692013-12-02 22:05:03 +01001356 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001357 shell_touch_grab_end(&move->base);
1358 free(move);
1359 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001360}
1361
1362static void
1363touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1364 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1365{
1366 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1367 struct shell_surface *shsurf = move->base.shsurf;
1368 struct weston_surface *es;
1369 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1370 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1371
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001372 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001373 return;
1374
1375 es = shsurf->surface;
1376
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001377 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001378
1379 weston_compositor_schedule_repaint(es->compositor);
1380}
1381
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001382static void
1383touch_move_grab_cancel(struct weston_touch_grab *grab)
1384{
1385 struct weston_touch_move_grab *move =
1386 (struct weston_touch_move_grab *) container_of(
1387 grab, struct shell_touch_grab, grab);
1388
1389 shell_touch_grab_end(&move->base);
1390 free(move);
1391}
1392
Rusty Lynch1084da52013-08-15 09:10:08 -07001393static const struct weston_touch_grab_interface touch_move_grab_interface = {
1394 touch_move_grab_down,
1395 touch_move_grab_up,
1396 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001397 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001398};
1399
1400static int
1401surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1402{
1403 struct weston_touch_move_grab *move;
1404
1405 if (!shsurf)
1406 return -1;
1407
Rafael Antognolli03b16592013-12-03 15:35:42 -02001408 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001409 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001410 if (shsurf->grabbed)
1411 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001412
1413 move = malloc(sizeof *move);
1414 if (!move)
1415 return -1;
1416
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001417 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001419 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001421 seat->touch->grab_y;
1422
1423 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1424 seat->touch);
1425
1426 return 0;
1427}
1428
1429static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001430noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001431{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001432}
1433
1434static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001435move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1436 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001437{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001438 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001439 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001440 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001441 int dx, dy;
1442
1443 weston_pointer_move(pointer, x, y);
1444 dx = wl_fixed_to_int(pointer->x + move->dx);
1445 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001446
1447 if (!shsurf)
1448 return;
1449
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001450 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001451
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001453}
1454
1455static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001456move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001457 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001458{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001459 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1460 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001461 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001462 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001463
Daniel Stone4dbadb12012-05-30 16:31:51 +01001464 if (pointer->button_count == 0 &&
1465 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001466 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001467 free(grab);
1468 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001469}
1470
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001471static void
1472move_grab_cancel(struct weston_pointer_grab *grab)
1473{
1474 struct shell_grab *shell_grab =
1475 container_of(grab, struct shell_grab, grab);
1476
1477 shell_grab_end(shell_grab);
1478 free(grab);
1479}
1480
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001481static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001482 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001483 move_grab_motion,
1484 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001485 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001486};
1487
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001488static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001489surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001490{
1491 struct weston_move_grab *move;
1492
1493 if (!shsurf)
1494 return -1;
1495
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001496 if (shsurf->grabbed)
1497 return 0;
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001498 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001499 return 0;
1500
1501 move = malloc(sizeof *move);
1502 if (!move)
1503 return -1;
1504
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001506 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001508 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001509
1510 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001511 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001512
1513 return 0;
1514}
1515
1516static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001517common_surface_move(struct wl_resource *resource,
1518 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001519{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001520 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001521 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001522 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001523
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001524 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001525 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001526 seat->pointer->button_count > 0 &&
1527 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001529 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001530 (surface_move(shsurf, seat) < 0))
1531 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001532 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001533 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001534 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001536 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001537 (surface_touch_move(shsurf, seat) < 0))
1538 wl_resource_post_no_memory(resource);
1539 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001540}
1541
Rafael Antognollie2a34552013-12-03 15:35:45 -02001542static void
1543shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1544 struct wl_resource *seat_resource, uint32_t serial)
1545{
1546 common_surface_move(resource, seat_resource, serial);
1547}
1548
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001549struct weston_resize_grab {
1550 struct shell_grab base;
1551 uint32_t edges;
1552 int32_t width, height;
1553};
1554
1555static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001556resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1557 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001558{
1559 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001560 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561 struct shell_surface *shsurf = resize->base.shsurf;
1562 int32_t width, height;
1563 wl_fixed_t from_x, from_y;
1564 wl_fixed_t to_x, to_y;
1565
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001566 weston_pointer_move(pointer, x, y);
1567
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001568 if (!shsurf)
1569 return;
1570
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571 weston_view_from_global_fixed(shsurf->view,
1572 pointer->grab_x, pointer->grab_y,
1573 &from_x, &from_y);
1574 weston_view_from_global_fixed(shsurf->view,
1575 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001576
1577 width = resize->width;
1578 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1579 width += wl_fixed_to_int(from_x - to_x);
1580 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1581 width += wl_fixed_to_int(to_x - from_x);
1582 }
1583
1584 height = resize->height;
1585 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1586 height += wl_fixed_to_int(from_y - to_y);
1587 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1588 height += wl_fixed_to_int(to_y - from_y);
1589 }
1590
1591 shsurf->client->send_configure(shsurf->surface,
1592 resize->edges, width, height);
1593}
1594
1595static void
1596send_configure(struct weston_surface *surface,
1597 uint32_t edges, int32_t width, int32_t height)
1598{
1599 struct shell_surface *shsurf = get_shell_surface(surface);
1600
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001601 assert(shsurf);
1602
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001603 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001604 edges, width, height);
1605}
1606
1607static const struct weston_shell_client shell_client = {
1608 send_configure
1609};
1610
1611static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001612resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001613 uint32_t time, uint32_t button, uint32_t state_w)
1614{
1615 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001616 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617 enum wl_pointer_button_state state = state_w;
1618
1619 if (pointer->button_count == 0 &&
1620 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1621 shell_grab_end(&resize->base);
1622 free(grab);
1623 }
1624}
1625
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001626static void
1627resize_grab_cancel(struct weston_pointer_grab *grab)
1628{
1629 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1630
1631 shell_grab_end(&resize->base);
1632 free(grab);
1633}
1634
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001635static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001636 noop_grab_focus,
1637 resize_grab_motion,
1638 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001639 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001640};
1641
Giulio Camuffob8366642013-04-25 13:57:46 +03001642/*
1643 * Returns the bounding box of a surface and all its sub-surfaces,
1644 * in the surface coordinates system. */
1645static void
1646surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1647 int32_t *y, int32_t *w, int32_t *h) {
1648 pixman_region32_t region;
1649 pixman_box32_t *box;
1650 struct weston_subsurface *subsurface;
1651
1652 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 surface->width,
1654 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001655
1656 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1657 pixman_region32_union_rect(&region, &region,
1658 subsurface->position.x,
1659 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660 subsurface->surface->width,
1661 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001662 }
1663
1664 box = pixman_region32_extents(&region);
1665 if (x)
1666 *x = box->x1;
1667 if (y)
1668 *y = box->y1;
1669 if (w)
1670 *w = box->x2 - box->x1;
1671 if (h)
1672 *h = box->y2 - box->y1;
1673
1674 pixman_region32_fini(&region);
1675}
1676
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677static int
1678surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001679 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001680{
1681 struct weston_resize_grab *resize;
1682
Rafael Antognolli03b16592013-12-03 15:35:42 -02001683 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001684 return 0;
1685
1686 if (edges == 0 || edges > 15 ||
1687 (edges & 3) == 3 || (edges & 12) == 12)
1688 return 0;
1689
1690 resize = malloc(sizeof *resize);
1691 if (!resize)
1692 return -1;
1693
1694 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001695 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1696 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001697
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001698 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001700 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701
1702 return 0;
1703}
1704
1705static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001706common_surface_resize(struct wl_resource *resource,
1707 struct wl_resource *seat_resource, uint32_t serial,
1708 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001710 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001711 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001712 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713
Rafael Antognolli03b16592013-12-03 15:35:42 -02001714 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715 return;
1716
Kristian Høgsberge3148752013-05-06 23:19:49 -04001717 if (seat->pointer->button_count == 0 ||
1718 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001719 seat->pointer->focus == NULL)
1720 return;
1721
1722 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1723 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001724 return;
1725
Kristian Høgsberge3148752013-05-06 23:19:49 -04001726 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727 wl_resource_post_no_memory(resource);
1728}
1729
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001730static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001731shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1732 struct wl_resource *seat_resource, uint32_t serial,
1733 uint32_t edges)
1734{
1735 common_surface_resize(resource, seat_resource, serial, edges);
1736}
1737
1738static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001739busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001740{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001741 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001742 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001744 wl_fixed_t sx, sy;
1745
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 view = weston_compositor_pick_view(pointer->seat->compositor,
1747 pointer->x, pointer->y,
1748 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001749
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001750 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1751 shell_grab_end(grab);
1752 free(grab);
1753 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001754}
1755
1756static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001757busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1758 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001759{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001760 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001761}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001762
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001763static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001764busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001765 uint32_t time, uint32_t button, uint32_t state)
1766{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001767 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001768 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001769 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001770
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001771 if (shsurf && button == BTN_LEFT && state) {
1772 activate(shsurf->shell, shsurf->surface, seat);
1773 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001774 } else if (shsurf && button == BTN_RIGHT && state) {
1775 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001776 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001777 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001778}
1779
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001780static void
1781busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1782{
1783 struct shell_grab *grab = (struct shell_grab *) base;
1784
1785 shell_grab_end(grab);
1786 free(grab);
1787}
1788
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001789static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001790 busy_cursor_grab_focus,
1791 busy_cursor_grab_motion,
1792 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001793 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794};
1795
1796static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001797set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001798{
1799 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001800
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001801 if (pointer->grab->interface == &busy_cursor_grab_interface)
1802 return;
1803
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804 grab = malloc(sizeof *grab);
1805 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001806 return;
1807
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001808 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1809 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001810}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001811
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001812static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001813end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001814{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001815 struct shell_grab *grab;
1816 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001817
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001818 wl_list_for_each(seat, &compositor->seat_list, link) {
1819 if (seat->pointer == NULL)
1820 continue;
1821
1822 grab = (struct shell_grab *) seat->pointer->grab;
1823 if (grab->grab.interface == &busy_cursor_grab_interface &&
1824 wl_resource_get_client(grab->shsurf->resource) == client) {
1825 shell_grab_end(grab);
1826 free(grab);
1827 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001828 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001829}
1830
Scott Moreau9521d5e2012-04-19 13:06:17 -06001831static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001832handle_shell_client_destroy(struct wl_listener *listener, void *data);
1833
1834static int
1835xdg_ping_timeout_handler(void *data)
1836{
1837 struct shell_client *sc = data;
1838 struct weston_seat *seat;
1839 struct shell_surface *shsurf;
1840
1841 /* Client is not responding */
1842 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001843 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1844 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1845 continue;
1846 if (seat->pointer->focus->surface->resource == NULL)
1847 continue;
1848
1849 shsurf = get_shell_surface(seat->pointer->focus->surface);
1850 if (shsurf &&
1851 wl_resource_get_client(shsurf->resource) == sc->client)
1852 set_busy_cursor(shsurf, seat->pointer);
1853 }
1854
1855 return 1;
1856}
1857
1858static void
1859handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1860{
1861 struct weston_compositor *compositor = shsurf->shell->compositor;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001862 struct wl_client *client = wl_resource_get_client(shsurf->resource);
1863 struct shell_client *sc;
1864 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001865 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001866
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001867 sc = get_shell_client(client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001868 if (sc->unresponsive) {
1869 xdg_ping_timeout_handler(sc);
1870 return;
1871 }
1872
1873 sc->ping_serial = serial;
1874 loop = wl_display_get_event_loop(compositor->wl_display);
1875 if (sc->ping_timer == NULL)
1876 sc->ping_timer =
1877 wl_event_loop_add_timer(loop,
1878 xdg_ping_timeout_handler, sc);
1879 if (sc->ping_timer == NULL)
1880 return;
1881
1882 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1883
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001884 if (shell_surface_is_xdg_surface(shsurf) ||
1885 shell_surface_is_xdg_popup(shsurf))
1886 xdg_shell_send_ping(sc->resource, serial);
1887 else if (shell_surface_is_wl_shell_surface(shsurf))
1888 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001889}
1890
Scott Moreauff1db4a2012-04-17 19:06:18 -06001891static void
1892ping_handler(struct weston_surface *surface, uint32_t serial)
1893{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001894 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001895
1896 if (!shsurf)
1897 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001898 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001899 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001900 if (shsurf->surface == shsurf->shell->grab_surface)
1901 return;
1902
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001903 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001904}
1905
1906static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001907handle_pointer_focus(struct wl_listener *listener, void *data)
1908{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001909 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001910 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001911 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001912 uint32_t serial;
1913
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001915 return;
1916
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001918 serial = wl_display_next_serial(compositor->wl_display);
1919 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001920}
1921
1922static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001923create_pointer_focus_listener(struct weston_seat *seat)
1924{
1925 struct wl_listener *listener;
1926
Kristian Høgsberge3148752013-05-06 23:19:49 -04001927 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001928 return;
1929
1930 listener = malloc(sizeof *listener);
1931 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001932 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001933}
1934
1935static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001936shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1937{
1938 if (--shsurf->focus_count == 0)
1939 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001940 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001941}
1942
1943static void
1944shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1945{
1946 if (shsurf->focus_count++ == 0)
1947 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001948 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001949}
1950
1951static void
1952handle_keyboard_focus(struct wl_listener *listener, void *data)
1953{
1954 struct weston_keyboard *keyboard = data;
1955 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1956
1957 if (seat->focused_surface) {
1958 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1959 if (shsurf)
1960 shell_surface_lose_keyboard_focus(shsurf);
1961 }
1962
1963 seat->focused_surface = keyboard->focus;
1964
1965 if (seat->focused_surface) {
1966 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1967 if (shsurf)
1968 shell_surface_gain_keyboard_focus(shsurf);
1969 }
1970}
1971
1972static void
1973create_keyboard_focus_listener(struct weston_seat *seat)
1974{
1975 struct wl_listener *listener;
1976
1977 if (!seat->keyboard)
1978 return;
1979
1980 listener = malloc(sizeof *listener);
1981 listener->notify = handle_keyboard_focus;
1982 wl_signal_add(&seat->keyboard->focus_signal, listener);
1983}
1984
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001985static struct shell_client *
1986get_shell_client(struct wl_client *client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02001987{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001988 struct wl_listener *listener;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001989
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001990 listener = wl_client_get_destroy_listener(client,
1991 handle_shell_client_destroy);
1992 if (listener == NULL)
1993 return NULL;
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001994
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001995 return container_of(listener, struct shell_client, destroy_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001996}
1997
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001998static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001999shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002000{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002001 if (sc->ping_serial != serial)
2002 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002003
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002004 sc->unresponsive = 0;
2005 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002006
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002007 if (sc->ping_timer) {
2008 wl_event_source_remove(sc->ping_timer);
2009 sc->ping_timer = NULL;
2010 }
2011
2012}
2013
2014static void
2015shell_surface_pong(struct wl_client *client,
2016 struct wl_resource *resource, uint32_t serial)
2017{
2018 struct shell_client *sc;
2019
2020 sc = get_shell_client(client);
2021 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002022}
2023
2024static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002025set_title(struct shell_surface *shsurf, const char *title)
2026{
2027 free(shsurf->title);
2028 shsurf->title = strdup(title);
2029}
2030
2031static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002032shell_surface_set_title(struct wl_client *client,
2033 struct wl_resource *resource, const char *title)
2034{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002035 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002036
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002037 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002038}
2039
2040static void
2041shell_surface_set_class(struct wl_client *client,
2042 struct wl_resource *resource, const char *class)
2043{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002044 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002045
2046 free(shsurf->class);
2047 shsurf->class = strdup(class);
2048}
2049
Alex Wu4539b082012-03-01 12:57:46 +08002050static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002051restore_output_mode(struct weston_output *output)
2052{
Hardening57388e42013-09-18 23:56:36 +02002053 if (output->current_mode != output->original_mode ||
2054 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002055 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002056 output->original_mode,
2057 output->original_scale,
2058 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002059}
2060
2061static void
2062restore_all_output_modes(struct weston_compositor *compositor)
2063{
2064 struct weston_output *output;
2065
2066 wl_list_for_each(output, &compositor->output_list, link)
2067 restore_output_mode(output);
2068}
2069
Philip Withnallbecb77e2013-11-25 18:01:30 +00002070static int
2071get_output_panel_height(struct desktop_shell *shell,
2072 struct weston_output *output)
2073{
2074 struct weston_view *view;
2075 int panel_height = 0;
2076
2077 if (!output)
2078 return 0;
2079
2080 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2081 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002082 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002083 break;
2084 }
2085 }
2086
2087 return panel_height;
2088}
2089
Philip Withnall07926d92013-11-25 18:01:40 +00002090/* The surface will be inserted into the list immediately after the link
2091 * returned by this function (i.e. will be stacked immediately above the
2092 * returned link). */
2093static struct wl_list *
2094shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2095{
2096 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002097 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002098
2099 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002100 case SHELL_SURFACE_POPUP:
2101 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002102 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002103 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002104 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002105 /* Move the surface to its parent layer so
2106 * that surfaces which are transient for
2107 * fullscreen surfaces don't get hidden by the
2108 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002109
2110 /* TODO: Handle a parent with multiple views */
2111 parent = get_default_view(shsurf->parent);
2112 if (parent)
2113 return parent->layer_link.prev;
2114 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002115 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002116
2117 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002118 return &shsurf->shell->fullscreen_layer.view_list;
2119
Philip Withnall07926d92013-11-25 18:01:40 +00002120 case SHELL_SURFACE_NONE:
2121 default:
2122 /* Go to the fallback, below. */
2123 break;
2124 }
2125
2126 /* Move the surface to a normal workspace layer so that surfaces
2127 * which were previously fullscreen or transient are no longer
2128 * rendered on top. */
2129 ws = get_current_workspace(shsurf->shell);
2130 return &ws->layer.view_list;
2131}
2132
Philip Withnall648a4dd2013-11-25 18:01:44 +00002133static void
2134shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2135{
2136 struct shell_surface *child;
2137
2138 /* Move the child layers to the same workspace as shsurf. They will be
2139 * stacked above shsurf. */
2140 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2141 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2142 weston_view_geometry_dirty(child->view);
2143 wl_list_remove(&child->view->layer_link);
2144 wl_list_insert(shsurf->view->layer_link.prev,
2145 &child->view->layer_link);
2146 weston_view_geometry_dirty(child->view);
2147 weston_surface_damage(child->surface);
2148
2149 /* Recurse. We don’t expect this to recurse very far (if
2150 * at all) because that would imply we have transient
2151 * (or popup) children of transient surfaces, which
2152 * would be unusual. */
2153 shell_surface_update_child_surface_layers(child);
2154 }
2155 }
2156}
2157
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002158/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002159 * geometry to ensure the changes are redrawn.
2160 *
2161 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2162 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002163static void
2164shell_surface_update_layer(struct shell_surface *shsurf)
2165{
2166 struct wl_list *new_layer_link;
2167
2168 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2169
2170 if (new_layer_link == &shsurf->view->layer_link)
2171 return;
2172
2173 weston_view_geometry_dirty(shsurf->view);
2174 wl_list_remove(&shsurf->view->layer_link);
2175 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2176 weston_view_geometry_dirty(shsurf->view);
2177 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002178
2179 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002180}
2181
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002182static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002183shell_surface_set_parent(struct shell_surface *shsurf,
2184 struct weston_surface *parent)
2185{
2186 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002187
2188 wl_list_remove(&shsurf->children_link);
2189 wl_list_init(&shsurf->children_link);
2190
2191 /* Insert into the parent surface’s child list. */
2192 if (parent != NULL) {
2193 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2194 if (parent_shsurf != NULL)
2195 wl_list_insert(&parent_shsurf->children_list,
2196 &shsurf->children_link);
2197 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002198}
2199
2200static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002201shell_surface_set_output(struct shell_surface *shsurf,
2202 struct weston_output *output)
2203{
2204 struct weston_surface *es = shsurf->surface;
2205
2206 /* get the default output, if the client set it as NULL
2207 check whether the ouput is available */
2208 if (output)
2209 shsurf->output = output;
2210 else if (es->output)
2211 shsurf->output = es->output;
2212 else
2213 shsurf->output = get_default_output(es->compositor);
2214}
2215
2216static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002217surface_clear_next_states(struct shell_surface *shsurf)
2218{
2219 shsurf->next_state.maximized = false;
2220 shsurf->next_state.fullscreen = false;
2221
2222 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2223 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2224 shsurf->state_changed = true;
2225}
2226
2227static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002228set_toplevel(struct shell_surface *shsurf)
2229{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002230 shell_surface_set_parent(shsurf, NULL);
2231 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002232 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002233
2234 /* The layer_link is updated in set_surface_type(),
2235 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002236}
2237
2238static void
2239shell_surface_set_toplevel(struct wl_client *client,
2240 struct wl_resource *resource)
2241{
2242 struct shell_surface *surface = wl_resource_get_user_data(resource);
2243
2244 set_toplevel(surface);
2245}
2246
2247static void
2248set_transient(struct shell_surface *shsurf,
2249 struct weston_surface *parent, int x, int y, uint32_t flags)
2250{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002251 assert(parent != NULL);
2252
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002253 shell_surface_set_parent(shsurf, parent);
2254
2255 surface_clear_next_states(shsurf);
2256
Philip Withnallbecb77e2013-11-25 18:01:30 +00002257 shsurf->transient.x = x;
2258 shsurf->transient.y = y;
2259 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002260
Rafael Antognollied207b42013-12-03 15:35:43 -02002261 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002262 shsurf->state_changed = true;
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_transient(struct wl_client *client,
2271 struct wl_resource *resource,
2272 struct wl_resource *parent_resource,
2273 int x, int y, uint32_t flags)
2274{
2275 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2276 struct weston_surface *parent =
2277 wl_resource_get_user_data(parent_resource);
2278
2279 set_transient(shsurf, parent, x, y, flags);
2280}
2281
2282static void
2283set_fullscreen(struct shell_surface *shsurf,
2284 uint32_t method,
2285 uint32_t framerate,
2286 struct weston_output *output)
2287{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002288 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002289
2290 shsurf->fullscreen_output = shsurf->output;
2291 shsurf->fullscreen.type = method;
2292 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002293
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002294 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002295
2296 shsurf->client->send_configure(shsurf->surface, 0,
2297 shsurf->output->width,
2298 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002299
2300 /* The layer_link is updated in set_surface_type(),
2301 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002302}
2303
2304static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002305weston_view_set_initial_position(struct weston_view *view,
2306 struct desktop_shell *shell);
2307
2308static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002309unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002310{
Philip Withnallf85fe842013-11-25 18:01:37 +00002311 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002312 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2313 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002314 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002315 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002316 shsurf->fullscreen_output = NULL;
2317
Alex Wu4539b082012-03-01 12:57:46 +08002318 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2319 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002320
Alex Wu4539b082012-03-01 12:57:46 +08002321 wl_list_remove(&shsurf->fullscreen.transform.link);
2322 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002323
Jason Ekstranda7af7042013-10-12 22:38:11 -05002324 if (shsurf->fullscreen.black_view)
2325 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2326 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002327
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002328 if (shsurf->saved_position_valid)
2329 weston_view_set_position(shsurf->view,
2330 shsurf->saved_x, shsurf->saved_y);
2331 else
2332 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2333
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002334 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002335 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002336 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002337 shsurf->saved_rotation_valid = false;
2338 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002339
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002340 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002341}
2342
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002343static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002344shell_surface_set_fullscreen(struct wl_client *client,
2345 struct wl_resource *resource,
2346 uint32_t method,
2347 uint32_t framerate,
2348 struct wl_resource *output_resource)
2349{
2350 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2351 struct weston_output *output;
2352
2353 if (output_resource)
2354 output = wl_resource_get_user_data(output_resource);
2355 else
2356 output = NULL;
2357
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002358 shell_surface_set_parent(shsurf, NULL);
2359
Rafael Antognolli03b16592013-12-03 15:35:42 -02002360 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002361 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002362
2363 shsurf->next_state.fullscreen = true;
2364 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002365}
2366
2367static void
2368set_popup(struct shell_surface *shsurf,
2369 struct weston_surface *parent,
2370 struct weston_seat *seat,
2371 uint32_t serial,
2372 int32_t x,
2373 int32_t y)
2374{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002375 assert(parent != NULL);
2376
Philip Withnallbecb77e2013-11-25 18:01:30 +00002377 shsurf->popup.shseat = get_shell_seat(seat);
2378 shsurf->popup.serial = serial;
2379 shsurf->popup.x = x;
2380 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002381
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002382 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002383}
2384
2385static void
2386shell_surface_set_popup(struct wl_client *client,
2387 struct wl_resource *resource,
2388 struct wl_resource *seat_resource,
2389 uint32_t serial,
2390 struct wl_resource *parent_resource,
2391 int32_t x, int32_t y, uint32_t flags)
2392{
2393 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002394 struct weston_surface *parent =
2395 wl_resource_get_user_data(parent_resource);
2396
2397 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002398
Rafael Antognolli03b16592013-12-03 15:35:42 -02002399 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002400 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002401 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002402 wl_resource_get_user_data(seat_resource),
2403 serial, x, y);
2404}
2405
2406static void
2407set_maximized(struct shell_surface *shsurf,
2408 struct weston_output *output)
2409{
2410 struct desktop_shell *shell;
2411 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002412
Philip Withnall352e7ed2013-11-25 18:01:35 +00002413 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002414
2415 shell = shell_surface_get_shell(shsurf);
2416 panel_height = get_output_panel_height(shell, shsurf->output);
2417 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2418
2419 shsurf->client->send_configure(shsurf->surface, edges,
2420 shsurf->output->width,
2421 shsurf->output->height - panel_height);
2422
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002423 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002424}
2425
2426static void
2427unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002428{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002429 /* undo all maximized things here */
2430 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002431
2432 if (shsurf->saved_position_valid)
2433 weston_view_set_position(shsurf->view,
2434 shsurf->saved_x, shsurf->saved_y);
2435 else
2436 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002437
2438 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002439 wl_list_insert(&shsurf->view->geometry.transformation_list,
2440 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002441 shsurf->saved_rotation_valid = false;
2442 }
2443
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002444 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002445}
2446
Philip Withnallbecb77e2013-11-25 18:01:30 +00002447static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002448set_minimized(struct weston_surface *surface, uint32_t is_true)
2449{
2450 struct shell_surface *shsurf;
2451 struct workspace *current_ws;
2452 struct weston_seat *seat;
2453 struct weston_surface *focus;
2454 struct weston_view *view;
2455
2456 view = get_default_view(surface);
2457 if (!view)
2458 return;
2459
2460 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2461
2462 shsurf = get_shell_surface(surface);
2463 current_ws = get_current_workspace(shsurf->shell);
2464
2465 wl_list_remove(&view->layer_link);
2466 /* hide or show, depending on the state */
2467 if (is_true) {
2468 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2469
2470 drop_focus_state(shsurf->shell, current_ws, view->surface);
2471 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2472 if (!seat->keyboard)
2473 continue;
2474 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2475 if (focus == view->surface)
2476 weston_keyboard_set_focus(seat->keyboard, NULL);
2477 }
2478 }
2479 else {
2480 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2481
2482 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2483 if (!seat->keyboard)
2484 continue;
2485 activate(shsurf->shell, view->surface, seat);
2486 }
2487 }
2488
2489 shell_surface_update_child_surface_layers(shsurf);
2490
2491 weston_view_damage_below(view);
2492}
2493
2494static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002495shell_surface_set_maximized(struct wl_client *client,
2496 struct wl_resource *resource,
2497 struct wl_resource *output_resource)
2498{
2499 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2500 struct weston_output *output;
2501
2502 if (output_resource)
2503 output = wl_resource_get_user_data(output_resource);
2504 else
2505 output = NULL;
2506
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002507 shell_surface_set_parent(shsurf, NULL);
2508
Rafael Antognolli03b16592013-12-03 15:35:42 -02002509 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002510 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002511
2512 shsurf->next_state.maximized = true;
2513 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002514}
2515
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002516/* This is only ever called from set_surface_type(), so there’s no need to
2517 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002518static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002519reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002520{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002521 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002522 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002523 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002524 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002525
Pekka Paalanen98262232011-12-01 10:42:22 +02002526 return 0;
2527}
2528
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002529static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002530set_full_output(struct shell_surface *shsurf)
2531{
2532 shsurf->saved_x = shsurf->view->geometry.x;
2533 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002534 shsurf->saved_width = shsurf->surface->width;
2535 shsurf->saved_height = shsurf->surface->height;
2536 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002537 shsurf->saved_position_valid = true;
2538
2539 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2540 wl_list_remove(&shsurf->rotation.transform.link);
2541 wl_list_init(&shsurf->rotation.transform.link);
2542 weston_view_geometry_dirty(shsurf->view);
2543 shsurf->saved_rotation_valid = true;
2544 }
2545}
2546
2547static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002548set_surface_type(struct shell_surface *shsurf)
2549{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002550 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002551 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002552
Philip Withnallbecb77e2013-11-25 18:01:30 +00002553 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002554
Rafael Antognolli03b16592013-12-03 15:35:42 -02002555 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002556 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002557
2558 switch (shsurf->type) {
2559 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002560 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002561 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002562 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002563 weston_view_set_position(shsurf->view,
2564 pev->geometry.x + shsurf->transient.x,
2565 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002566 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002567 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002568
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002569 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002570 weston_view_set_position(shsurf->view, shsurf->transient.x,
2571 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002572 break;
2573
Philip Withnall0f640e22013-11-25 18:01:31 +00002574 case SHELL_SURFACE_POPUP:
2575 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002576 default:
2577 break;
2578 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002579
2580 /* Update the surface’s layer. */
2581 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002582}
2583
Tiago Vignattibe143262012-04-16 17:31:41 +03002584static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002585shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002586{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002587 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002588}
2589
Alex Wu21858432012-04-01 20:13:08 +08002590static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002591black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002592
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002594create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002595 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002596 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002597{
2598 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002599 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002600
2601 surface = weston_surface_create(ec);
2602 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002603 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002604 return NULL;
2605 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002606 view = weston_view_create(surface);
2607 if (surface == NULL) {
2608 weston_log("no memory\n");
2609 weston_surface_destroy(surface);
2610 return NULL;
2611 }
Alex Wu4539b082012-03-01 12:57:46 +08002612
Alex Wu21858432012-04-01 20:13:08 +08002613 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002614 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002615 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002616 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002617 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002618 pixman_region32_fini(&surface->input);
2619 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002620
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002621 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002622 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002623
2624 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002625}
2626
Philip Withnalled8f1a92013-11-25 18:01:43 +00002627static void
2628shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2629{
2630 struct weston_output *output = shsurf->fullscreen_output;
2631
Rafael Antognolli03b16592013-12-03 15:35:42 -02002632 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002633
2634 if (!shsurf->fullscreen.black_view)
2635 shsurf->fullscreen.black_view =
2636 create_black_surface(shsurf->surface->compositor,
2637 shsurf->surface,
2638 output->x, output->y,
2639 output->width,
2640 output->height);
2641
2642 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2643 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2644 wl_list_insert(&shsurf->view->layer_link,
2645 &shsurf->fullscreen.black_view->layer_link);
2646 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2647 weston_surface_damage(shsurf->surface);
2648}
2649
Alex Wu4539b082012-03-01 12:57:46 +08002650/* Create black surface and append it to the associated fullscreen surface.
2651 * Handle size dismatch and positioning according to the method. */
2652static void
2653shell_configure_fullscreen(struct shell_surface *shsurf)
2654{
2655 struct weston_output *output = shsurf->fullscreen_output;
2656 struct weston_surface *surface = shsurf->surface;
2657 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002658 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002659 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002660
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002661 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2662 restore_output_mode(output);
2663
Philip Withnalled8f1a92013-11-25 18:01:43 +00002664 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002665
Jason Ekstranda7af7042013-10-12 22:38:11 -05002666 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002667 &surf_width, &surf_height);
2668
Alex Wu4539b082012-03-01 12:57:46 +08002669 switch (shsurf->fullscreen.type) {
2670 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002671 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002673 break;
2674 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002675 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002676 if (output->width == surf_width &&
2677 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002678 weston_view_set_position(shsurf->view,
2679 output->x - surf_x,
2680 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002681 break;
2682 }
2683
Alex Wu4539b082012-03-01 12:57:46 +08002684 matrix = &shsurf->fullscreen.transform.matrix;
2685 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002686
Scott Moreau1bad5db2012-08-18 01:04:05 -06002687 output_aspect = (float) output->width /
2688 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002689 /* XXX: Use surf_width and surf_height here? */
2690 surface_aspect = (float) surface->width /
2691 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002692 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002693 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002694 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002695 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002696 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002697 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002698
Alex Wu4539b082012-03-01 12:57:46 +08002699 weston_matrix_scale(matrix, scale, scale, 1);
2700 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002701 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002702 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002703 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2704 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002705 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002706
Alex Wu4539b082012-03-01 12:57:46 +08002707 break;
2708 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002709 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002710 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002711 surf_width * surface->buffer_viewport.buffer.scale,
2712 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002713 shsurf->fullscreen.framerate};
2714
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002715 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002716 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002717 weston_view_set_position(shsurf->view,
2718 output->x - surf_x,
2719 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002720 shsurf->fullscreen.black_view->surface->width = output->width;
2721 shsurf->fullscreen.black_view->surface->height = output->height;
2722 weston_view_set_position(shsurf->fullscreen.black_view,
2723 output->x - surf_x,
2724 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002725 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002726 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002727 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002728 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002729 }
Alex Wubd3354b2012-04-17 17:20:49 +08002730 }
Alex Wu4539b082012-03-01 12:57:46 +08002731 break;
2732 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002733 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002734 break;
2735 default:
2736 break;
2737 }
2738}
2739
Alex Wu4539b082012-03-01 12:57:46 +08002740static void
2741shell_map_fullscreen(struct shell_surface *shsurf)
2742{
Alex Wubd3354b2012-04-17 17:20:49 +08002743 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002744}
2745
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002746static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002747set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2748{
2749 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002750 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002751 shsurf->transient.x = x;
2752 shsurf->transient.y = y;
2753 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002754
2755 shell_surface_set_parent(shsurf, NULL);
2756
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002757 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002758 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002759}
2760
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002761static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002762
2763static void
2764destroy_shell_seat(struct wl_listener *listener, void *data)
2765{
2766 struct shell_seat *shseat =
2767 container_of(listener,
2768 struct shell_seat, seat_destroy_listener);
2769 struct shell_surface *shsurf, *prev = NULL;
2770
2771 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002772 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002773 shseat->popup_grab.client = NULL;
2774
2775 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2776 shsurf->popup.shseat = NULL;
2777 if (prev) {
2778 wl_list_init(&prev->popup.grab_link);
2779 }
2780 prev = shsurf;
2781 }
2782 wl_list_init(&prev->popup.grab_link);
2783 }
2784
2785 wl_list_remove(&shseat->seat_destroy_listener.link);
2786 free(shseat);
2787}
2788
2789static struct shell_seat *
2790create_shell_seat(struct weston_seat *seat)
2791{
2792 struct shell_seat *shseat;
2793
2794 shseat = calloc(1, sizeof *shseat);
2795 if (!shseat) {
2796 weston_log("no memory to allocate shell seat\n");
2797 return NULL;
2798 }
2799
2800 shseat->seat = seat;
2801 wl_list_init(&shseat->popup_grab.surfaces_list);
2802
2803 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2804 wl_signal_add(&seat->destroy_signal,
2805 &shseat->seat_destroy_listener);
2806
2807 return shseat;
2808}
2809
2810static struct shell_seat *
2811get_shell_seat(struct weston_seat *seat)
2812{
2813 struct wl_listener *listener;
2814
2815 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2816 if (listener == NULL)
2817 return create_shell_seat(seat);
2818
2819 return container_of(listener,
2820 struct shell_seat, seat_destroy_listener);
2821}
2822
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002823static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002824popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002825{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002826 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002828 struct shell_seat *shseat =
2829 container_of(grab, struct shell_seat, popup_grab.grab);
2830 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002831 wl_fixed_t sx, sy;
2832
Jason Ekstranda7af7042013-10-12 22:38:11 -05002833 view = weston_compositor_pick_view(pointer->seat->compositor,
2834 pointer->x, pointer->y,
2835 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002836
Jason Ekstranda7af7042013-10-12 22:38:11 -05002837 if (view && view->surface->resource &&
2838 wl_resource_get_client(view->surface->resource) == client) {
2839 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002840 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002841 weston_pointer_set_focus(pointer, NULL,
2842 wl_fixed_from_int(0),
2843 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002844 }
2845}
2846
2847static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002848popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2849 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002850{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002851 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002852 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002853 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002854
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002855 weston_pointer_move(pointer, x, y);
2856
Neil Roberts96d790e2013-09-19 17:32:00 +01002857 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002858 weston_view_from_global_fixed(pointer->focus,
2859 pointer->x, pointer->y,
2860 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002861 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002862 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002863}
2864
2865static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002866popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002867 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002868{
2869 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002870 struct shell_seat *shseat =
2871 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002872 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002873 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002874 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002875 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002876
Neil Roberts96d790e2013-09-19 17:32:00 +01002877 resource_list = &grab->pointer->focus_resource_list;
2878 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002879 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002880 wl_resource_for_each(resource, resource_list) {
2881 wl_pointer_send_button(resource, serial,
2882 time, button, state);
2883 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002884 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002885 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002886 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002887 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002888 }
2889
Daniel Stone4dbadb12012-05-30 16:31:51 +01002890 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002891 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002892}
2893
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002894static void
2895popup_grab_cancel(struct weston_pointer_grab *grab)
2896{
2897 popup_grab_end(grab->pointer);
2898}
2899
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002900static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002901 popup_grab_focus,
2902 popup_grab_motion,
2903 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002904 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002905};
2906
2907static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002908shell_surface_send_popup_done(struct shell_surface *shsurf)
2909{
2910 if (shell_surface_is_wl_shell_surface(shsurf))
2911 wl_shell_surface_send_popup_done(shsurf->resource);
2912 else if (shell_surface_is_xdg_popup(shsurf))
2913 xdg_popup_send_popup_done(shsurf->resource,
2914 shsurf->popup.serial);
2915}
2916
2917static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002918popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002919{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002920 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002921 struct shell_seat *shseat =
2922 container_of(grab, struct shell_seat, popup_grab.grab);
2923 struct shell_surface *shsurf;
2924 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002925
2926 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002927 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002928 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002929 shseat->popup_grab.grab.interface = NULL;
2930 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002931 /* Send the popup_done event to all the popups open */
2932 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002933 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002934 shsurf->popup.shseat = NULL;
2935 if (prev) {
2936 wl_list_init(&prev->popup.grab_link);
2937 }
2938 prev = shsurf;
2939 }
2940 wl_list_init(&prev->popup.grab_link);
2941 wl_list_init(&shseat->popup_grab.surfaces_list);
2942 }
2943}
2944
2945static void
2946add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2947{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002948 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002949
2950 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002951 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002952 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002953 /* We must make sure here that this popup was opened after
2954 * a mouse press, and not just by moving around with other
2955 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002956 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002957 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002958
Rob Bradforddfe31052013-06-26 19:49:11 +01002959 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002960 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002961 } else {
2962 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002963 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002964}
2965
2966static void
2967remove_popup_grab(struct shell_surface *shsurf)
2968{
2969 struct shell_seat *shseat = shsurf->popup.shseat;
2970
2971 wl_list_remove(&shsurf->popup.grab_link);
2972 wl_list_init(&shsurf->popup.grab_link);
2973 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002974 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002975 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002976 }
2977}
2978
2979static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002980shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002981{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002982 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002983 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002984
Jason Ekstranda7af7042013-10-12 22:38:11 -05002985 shsurf->surface->output = parent_view->output;
2986 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002987
Jason Ekstranda7af7042013-10-12 22:38:11 -05002988 weston_view_set_transform_parent(shsurf->view, parent_view);
2989 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2990 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002991
Kristian Høgsberge3148752013-05-06 23:19:49 -04002992 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002993 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002994 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002995 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002996 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002997 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002998}
2999
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003000static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003001 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003002 shell_surface_move,
3003 shell_surface_resize,
3004 shell_surface_set_toplevel,
3005 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003006 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003007 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003008 shell_surface_set_maximized,
3009 shell_surface_set_title,
3010 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003011};
3012
3013static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003014destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003015{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003016 struct shell_surface *child, *next;
3017
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003018 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3019
Giulio Camuffo5085a752013-03-25 21:42:45 +01003020 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3021 remove_popup_grab(shsurf);
3022 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003023
Alex Wubd3354b2012-04-17 17:20:49 +08003024 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003025 shell_surface_is_top_fullscreen(shsurf))
3026 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003027
Jason Ekstranda7af7042013-10-12 22:38:11 -05003028 if (shsurf->fullscreen.black_view)
3029 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003030
Alex Wubd3354b2012-04-17 17:20:49 +08003031 /* As destroy_resource() use wl_list_for_each_safe(),
3032 * we can always remove the listener.
3033 */
3034 wl_list_remove(&shsurf->surface_destroy_listener.link);
3035 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003036 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003037
Jason Ekstranda7af7042013-10-12 22:38:11 -05003038 weston_view_destroy(shsurf->view);
3039
Philip Withnall648a4dd2013-11-25 18:01:44 +00003040 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003041 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3042 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003043
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003044 wl_list_remove(&shsurf->link);
3045 free(shsurf);
3046}
3047
3048static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003049shell_destroy_shell_surface(struct wl_resource *resource)
3050{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003051 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003052
Bryan Caina46b9462014-04-04 17:41:24 -05003053 if (!wl_list_empty(&shsurf->popup.grab_link))
3054 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003055 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003056}
3057
3058static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003059shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003060{
3061 struct shell_surface *shsurf = container_of(listener,
3062 struct shell_surface,
3063 surface_destroy_listener);
3064
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003065 if (shsurf->resource)
3066 wl_resource_destroy(shsurf->resource);
3067 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003068 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003069}
3070
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003071static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003072fade_out_done(struct weston_view_animation *animation, void *data)
3073{
3074 struct shell_surface *shsurf = data;
3075
3076 weston_surface_destroy(shsurf->surface);
3077}
3078
3079static void
3080handle_resource_destroy(struct wl_listener *listener, void *data)
3081{
3082 struct shell_surface *shsurf =
3083 container_of(listener, struct shell_surface,
3084 resource_destroy_listener);
3085
3086 shsurf->surface->ref_count++;
3087
3088 pixman_region32_fini(&shsurf->surface->pending.input);
3089 pixman_region32_init(&shsurf->surface->pending.input);
3090 pixman_region32_fini(&shsurf->surface->input);
3091 pixman_region32_init(&shsurf->surface->input);
Kristian Høgsbergb033c5e2014-03-20 14:49:07 -07003092 if (weston_surface_is_mapped(shsurf->surface))
3093 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3094 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003095}
3096
3097static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003098shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003099
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003100struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003101get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003102{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003103 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003104 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003105 else
3106 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003107}
3108
Rafael Antognollie2a34552013-12-03 15:35:45 -02003109static struct shell_surface *
3110create_common_surface(void *shell, struct weston_surface *surface,
3111 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003112{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003113 struct shell_surface *shsurf;
3114
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003115 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003116 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003117 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003118 }
3119
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003120 shsurf = calloc(1, sizeof *shsurf);
3121 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003122 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003123 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003124 }
3125
Jason Ekstranda7af7042013-10-12 22:38:11 -05003126 shsurf->view = weston_view_create(surface);
3127 if (!shsurf->view) {
3128 weston_log("no memory to allocate shell surface\n");
3129 free(shsurf);
3130 return NULL;
3131 }
3132
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003133 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003134 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003135
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003136 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3137 wl_resource_add_destroy_listener(surface->resource,
3138 &shsurf->resource_destroy_listener);
3139
Tiago Vignattibc052c92012-04-19 16:18:18 +03003140 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003141 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003142 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003143 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003144 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003145 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003146 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3147 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003148 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003149 wl_list_init(&shsurf->fullscreen.transform.link);
3150
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003151 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003152 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003153 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003154 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003155
3156 /* init link so its safe to always remove it in destroy_shell_surface */
3157 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003158 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003159
Pekka Paalanen460099f2012-01-20 16:48:25 +02003160 /* empty when not in use */
3161 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003162 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003163
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003164 wl_list_init(&shsurf->workspace_transform.link);
3165
Philip Withnall648a4dd2013-11-25 18:01:44 +00003166 wl_list_init(&shsurf->children_link);
3167 wl_list_init(&shsurf->children_list);
3168 shsurf->parent = NULL;
3169
Pekka Paalanen98262232011-12-01 10:42:22 +02003170 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003171
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003172 shsurf->client = client;
3173
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003174 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003175}
3176
Rafael Antognollie2a34552013-12-03 15:35:45 -02003177static struct shell_surface *
3178create_shell_surface(void *shell, struct weston_surface *surface,
3179 const struct weston_shell_client *client)
3180{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003181 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003182}
3183
Jason Ekstranda7af7042013-10-12 22:38:11 -05003184static struct weston_view *
3185get_primary_view(void *shell, struct shell_surface *shsurf)
3186{
3187 return shsurf->view;
3188}
3189
Tiago Vignattibc052c92012-04-19 16:18:18 +03003190static void
3191shell_get_shell_surface(struct wl_client *client,
3192 struct wl_resource *resource,
3193 uint32_t id,
3194 struct wl_resource *surface_resource)
3195{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003196 struct weston_surface *surface =
3197 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003198 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003199 struct shell_surface *shsurf;
3200
3201 if (get_shell_surface(surface)) {
3202 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003203 WL_DISPLAY_ERROR_INVALID_OBJECT,
3204 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003205 return;
3206 }
3207
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003208 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003209 if (!shsurf) {
3210 wl_resource_post_error(surface_resource,
3211 WL_DISPLAY_ERROR_INVALID_OBJECT,
3212 "surface->configure already set");
3213 return;
3214 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003215
Jason Ekstranda85118c2013-06-27 20:17:02 -05003216 shsurf->resource =
3217 wl_resource_create(client,
3218 &wl_shell_surface_interface, 1, id);
3219 wl_resource_set_implementation(shsurf->resource,
3220 &shell_surface_implementation,
3221 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003222}
3223
Rafael Antognollie2a34552013-12-03 15:35:45 -02003224static bool
3225shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3226{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003227 /* A shell surface without a resource is created from xwayland
3228 * and is considered a wl_shell surface for now. */
3229
3230 return shsurf->resource == NULL ||
3231 wl_resource_instance_of(shsurf->resource,
3232 &wl_shell_surface_interface,
3233 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003234}
3235
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003236static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003237 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003238};
3239
Rafael Antognollie2a34552013-12-03 15:35:45 -02003240/****************************
3241 * xdg-shell implementation */
3242
3243static void
3244xdg_surface_destroy(struct wl_client *client,
3245 struct wl_resource *resource)
3246{
3247 wl_resource_destroy(resource);
3248}
3249
3250static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003251xdg_surface_set_transient_for(struct wl_client *client,
3252 struct wl_resource *resource,
3253 struct wl_resource *parent_resource)
3254{
3255 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3256 struct weston_surface *parent;
3257
3258 if (parent_resource)
3259 parent = wl_resource_get_user_data(parent_resource);
3260 else
3261 parent = NULL;
3262
3263 shell_surface_set_parent(shsurf, parent);
3264}
3265
3266static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003267xdg_surface_set_margin(struct wl_client *client,
3268 struct wl_resource *resource,
3269 int32_t left,
3270 int32_t right,
3271 int32_t top,
3272 int32_t bottom)
3273{
3274 /* Do nothing, Weston doesn't try to constrain or place
3275 * surfaces in any special manner... */
3276}
3277
3278static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003279xdg_surface_set_app_id(struct wl_client *client,
3280 struct wl_resource *resource,
3281 const char *app_id)
3282{
3283 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3284
3285 free(shsurf->class);
3286 shsurf->class = strdup(app_id);
3287}
3288
3289static void
3290xdg_surface_set_title(struct wl_client *client,
3291 struct wl_resource *resource, const char *title)
3292{
3293 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3294
3295 set_title(shsurf, title);
3296}
3297
3298static void
3299xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3300 struct wl_resource *seat_resource, uint32_t serial)
3301{
3302 common_surface_move(resource, seat_resource, serial);
3303}
3304
3305static void
3306xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3307 struct wl_resource *seat_resource, uint32_t serial,
3308 uint32_t edges)
3309{
3310 common_surface_resize(resource, seat_resource, serial, edges);
3311}
3312
3313static void
3314xdg_surface_set_output(struct wl_client *client,
3315 struct wl_resource *resource,
3316 struct wl_resource *output_resource)
3317{
3318 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3319 struct weston_output *output;
3320
3321 if (output_resource)
3322 output = wl_resource_get_user_data(output_resource);
3323 else
3324 output = NULL;
3325
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003326 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003327}
3328
3329static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003330xdg_surface_change_state(struct shell_surface *shsurf,
3331 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003332{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003333 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003334 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003335
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003336 switch (state) {
3337 case XDG_SURFACE_STATE_MAXIMIZED:
3338 shsurf->requested_state.maximized = value;
3339 if (value)
3340 set_maximized(shsurf, NULL);
3341 break;
3342 case XDG_SURFACE_STATE_FULLSCREEN:
3343 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003344
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003345 if (value)
3346 set_fullscreen(shsurf,
3347 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3348 0, shsurf->recommended_output);
3349 break;
3350 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003351}
3352
3353static void
3354xdg_surface_request_change_state(struct wl_client *client,
3355 struct wl_resource *resource,
3356 uint32_t state,
3357 uint32_t value,
3358 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003359{
3360 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003361
3362 /* The client can't know what the current state is, so we need
3363 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003364
3365 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3366 return;
3367
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003368 switch (state) {
3369 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003370 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003371 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003372 default:
3373 /* send error? ignore? send change state with value 0? */
3374 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003375 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003376
3377 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003378}
3379
3380static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003381xdg_surface_ack_change_state(struct wl_client *client,
3382 struct wl_resource *resource,
3383 uint32_t state,
3384 uint32_t value,
3385 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003386{
3387 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3388
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003389 if (shsurf->state_requested) {
3390 shsurf->next_state = shsurf->requested_state;
3391 shsurf->state_changed = true;
3392 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003393 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003394}
3395
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003396static void
3397xdg_surface_set_minimized(struct wl_client *client,
3398 struct wl_resource *resource)
3399{
3400 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3401
3402 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3403 return;
3404
3405 /* apply compositor's own minimization logic (hide) */
3406 set_minimized(shsurf->surface, 1);
3407}
3408
Rafael Antognollie2a34552013-12-03 15:35:45 -02003409static const struct xdg_surface_interface xdg_surface_implementation = {
3410 xdg_surface_destroy,
3411 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003412 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003413 xdg_surface_set_title,
3414 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003415 xdg_surface_move,
3416 xdg_surface_resize,
3417 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003418 xdg_surface_request_change_state,
3419 xdg_surface_ack_change_state,
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003420 xdg_surface_set_minimized
Rafael Antognollie2a34552013-12-03 15:35:45 -02003421};
3422
3423static void
3424xdg_send_configure(struct weston_surface *surface,
3425 uint32_t edges, int32_t width, int32_t height)
3426{
3427 struct shell_surface *shsurf = get_shell_surface(surface);
3428
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003429 assert(shsurf);
3430
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003431 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003432}
3433
3434static const struct weston_shell_client xdg_client = {
3435 xdg_send_configure
3436};
3437
3438static void
3439xdg_use_unstable_version(struct wl_client *client,
3440 struct wl_resource *resource,
3441 int32_t version)
3442{
3443 if (version > 1) {
3444 wl_resource_post_error(resource,
3445 1,
3446 "xdg-shell:: version not implemented yet.");
3447 return;
3448 }
3449}
3450
3451static struct shell_surface *
3452create_xdg_surface(void *shell, struct weston_surface *surface,
3453 const struct weston_shell_client *client)
3454{
3455 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003456
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003457 shsurf = create_common_surface(shell, surface, client);
3458 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003459
3460 return shsurf;
3461}
3462
3463static void
3464xdg_get_xdg_surface(struct wl_client *client,
3465 struct wl_resource *resource,
3466 uint32_t id,
3467 struct wl_resource *surface_resource)
3468{
3469 struct weston_surface *surface =
3470 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003471 struct shell_client *sc = wl_resource_get_user_data(resource);
3472 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003473 struct shell_surface *shsurf;
3474
3475 if (get_shell_surface(surface)) {
3476 wl_resource_post_error(surface_resource,
3477 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003478 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003479 return;
3480 }
3481
3482 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3483 if (!shsurf) {
3484 wl_resource_post_error(surface_resource,
3485 WL_DISPLAY_ERROR_INVALID_OBJECT,
3486 "surface->configure already set");
3487 return;
3488 }
3489
3490 shsurf->resource =
3491 wl_resource_create(client,
3492 &xdg_surface_interface, 1, id);
3493 wl_resource_set_implementation(shsurf->resource,
3494 &xdg_surface_implementation,
3495 shsurf, shell_destroy_shell_surface);
3496}
3497
3498static bool
3499shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3500{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003501 return shsurf->resource &&
3502 wl_resource_instance_of(shsurf->resource,
3503 &xdg_surface_interface,
3504 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003505}
3506
3507/* xdg-popup implementation */
3508
3509static void
3510xdg_popup_destroy(struct wl_client *client,
3511 struct wl_resource *resource)
3512{
3513 wl_resource_destroy(resource);
3514}
3515
Rafael Antognollie2a34552013-12-03 15:35:45 -02003516static const struct xdg_popup_interface xdg_popup_implementation = {
3517 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003518};
3519
3520static void
3521xdg_popup_send_configure(struct weston_surface *surface,
3522 uint32_t edges, int32_t width, int32_t height)
3523{
3524}
3525
3526static const struct weston_shell_client xdg_popup_client = {
3527 xdg_popup_send_configure
3528};
3529
3530static struct shell_surface *
3531create_xdg_popup(void *shell, struct weston_surface *surface,
3532 const struct weston_shell_client *client,
3533 struct weston_surface *parent,
3534 struct shell_seat *seat,
3535 uint32_t serial,
3536 int32_t x, int32_t y)
3537{
3538 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003539
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003540 shsurf = create_common_surface(shell, surface, client);
3541 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003542 shsurf->popup.shseat = seat;
3543 shsurf->popup.serial = serial;
3544 shsurf->popup.x = x;
3545 shsurf->popup.y = y;
3546 shell_surface_set_parent(shsurf, parent);
3547
3548 return shsurf;
3549}
3550
3551static void
3552xdg_get_xdg_popup(struct wl_client *client,
3553 struct wl_resource *resource,
3554 uint32_t id,
3555 struct wl_resource *surface_resource,
3556 struct wl_resource *parent_resource,
3557 struct wl_resource *seat_resource,
3558 uint32_t serial,
3559 int32_t x, int32_t y, uint32_t flags)
3560{
3561 struct weston_surface *surface =
3562 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003563 struct shell_client *sc = wl_resource_get_user_data(resource);
3564 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003565 struct shell_surface *shsurf;
3566 struct weston_surface *parent;
3567 struct shell_seat *seat;
3568
3569 if (get_shell_surface(surface)) {
3570 wl_resource_post_error(surface_resource,
3571 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003572 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003573 return;
3574 }
3575
3576 if (!parent_resource) {
3577 wl_resource_post_error(surface_resource,
3578 WL_DISPLAY_ERROR_INVALID_OBJECT,
3579 "xdg_shell::get_xdg_popup requires a parent shell surface");
3580 }
3581
3582 parent = wl_resource_get_user_data(parent_resource);
3583 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3584
3585 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3586 parent, seat, serial, x, y);
3587 if (!shsurf) {
3588 wl_resource_post_error(surface_resource,
3589 WL_DISPLAY_ERROR_INVALID_OBJECT,
3590 "surface->configure already set");
3591 return;
3592 }
3593
3594 shsurf->resource =
3595 wl_resource_create(client,
3596 &xdg_popup_interface, 1, id);
3597 wl_resource_set_implementation(shsurf->resource,
3598 &xdg_popup_implementation,
3599 shsurf, shell_destroy_shell_surface);
3600}
3601
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003602static void
3603xdg_pong(struct wl_client *client,
3604 struct wl_resource *resource, uint32_t serial)
3605{
3606 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003607
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003608 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003609}
3610
Rafael Antognollie2a34552013-12-03 15:35:45 -02003611static bool
3612shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3613{
3614 return wl_resource_instance_of(shsurf->resource,
3615 &xdg_popup_interface,
3616 &xdg_popup_implementation);
3617}
3618
3619static const struct xdg_shell_interface xdg_implementation = {
3620 xdg_use_unstable_version,
3621 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003622 xdg_get_xdg_popup,
3623 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003624};
3625
3626static int
3627xdg_shell_unversioned_dispatch(const void *implementation,
3628 void *_target, uint32_t opcode,
3629 const struct wl_message *message,
3630 union wl_argument *args)
3631{
3632 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003633 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003634
3635 if (opcode != 0) {
3636 wl_resource_post_error(resource,
3637 WL_DISPLAY_ERROR_INVALID_OBJECT,
3638 "must call use_unstable_version first");
3639 return 0;
3640 }
3641
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003642#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003643
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003644 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3645 "shell implementation doesn't match protocol version");
3646
Rafael Antognollie2a34552013-12-03 15:35:45 -02003647 if (args[0].i != XDG_SERVER_VERSION) {
3648 wl_resource_post_error(resource,
3649 WL_DISPLAY_ERROR_INVALID_OBJECT,
3650 "incompatible version, server is %d "
3651 "client wants %d",
3652 XDG_SERVER_VERSION, args[0].i);
3653 return 0;
3654 }
3655
3656 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003657 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003658
3659 return 1;
3660}
3661
3662/* end of xdg-shell implementation */
3663/***********************************/
3664
Kristian Høgsberg07937562011-04-12 17:25:42 -04003665static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003666shell_fade(struct desktop_shell *shell, enum fade_type type);
3667
3668static int
3669screensaver_timeout(void *data)
3670{
3671 struct desktop_shell *shell = data;
3672
3673 shell_fade(shell, FADE_OUT);
3674
3675 return 1;
3676}
3677
3678static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003679handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003680{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003681 struct desktop_shell *shell =
3682 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003683
Pekka Paalanen18027e52011-12-02 16:31:49 +02003684 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003685
3686 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003687 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003688}
3689
3690static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003691launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003692{
3693 if (shell->screensaver.binding)
3694 return;
3695
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003696 if (!shell->screensaver.path) {
3697 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003698 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003699 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003700
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003701 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003702 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003703 return;
3704 }
3705
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003706 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003707 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003708 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003709 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003710}
3711
3712static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003713terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003714{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003715 if (shell->screensaver.process.pid == 0)
3716 return;
3717
3718 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003719}
3720
3721static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003722configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003723{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003724 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003725
Jason Ekstranda7af7042013-10-12 22:38:11 -05003726 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3727 if (v->output == ev->output && v != ev) {
3728 weston_view_unmap(v);
3729 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003730 }
3731 }
3732
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003733 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003734
Jason Ekstranda7af7042013-10-12 22:38:11 -05003735 if (wl_list_empty(&ev->layer_link)) {
3736 wl_list_insert(&layer->view_list, &ev->layer_link);
3737 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003738 }
3739}
3740
3741static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003742background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003743{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003744 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003745 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003746
Jason Ekstranda7af7042013-10-12 22:38:11 -05003747 view = container_of(es->views.next, struct weston_view, surface_link);
3748
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003749 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003750}
3751
3752static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003753desktop_shell_set_background(struct wl_client *client,
3754 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003755 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003756 struct wl_resource *surface_resource)
3757{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003758 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003759 struct weston_surface *surface =
3760 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003761 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003762
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003763 if (surface->configure) {
3764 wl_resource_post_error(surface_resource,
3765 WL_DISPLAY_ERROR_INVALID_OBJECT,
3766 "surface role already assigned");
3767 return;
3768 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003769
Jason Ekstranda7af7042013-10-12 22:38:11 -05003770 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3771 weston_view_destroy(view);
3772 view = weston_view_create(surface);
3773
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003774 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003775 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003776 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003777 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003778 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003779 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003780 surface->output->width,
3781 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003782}
3783
3784static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003785panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003786{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003787 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003788 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003789
Jason Ekstranda7af7042013-10-12 22:38:11 -05003790 view = container_of(es->views.next, struct weston_view, surface_link);
3791
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003792 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003793}
3794
3795static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003796desktop_shell_set_panel(struct wl_client *client,
3797 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003798 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003799 struct wl_resource *surface_resource)
3800{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003801 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003802 struct weston_surface *surface =
3803 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003804 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003805
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003806 if (surface->configure) {
3807 wl_resource_post_error(surface_resource,
3808 WL_DISPLAY_ERROR_INVALID_OBJECT,
3809 "surface role already assigned");
3810 return;
3811 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003812
Jason Ekstranda7af7042013-10-12 22:38:11 -05003813 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3814 weston_view_destroy(view);
3815 view = weston_view_create(surface);
3816
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003817 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003818 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003819 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003820 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003821 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003822 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003823 surface->output->width,
3824 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003825}
3826
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003827static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003828lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003829{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003830 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003831 struct weston_view *view;
3832
3833 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003834
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003835 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003836 return;
3837
Jason Ekstranda7af7042013-10-12 22:38:11 -05003838 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003839
3840 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003841 wl_list_insert(&shell->lock_layer.view_list,
3842 &view->layer_link);
3843 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003844 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003845 }
3846}
3847
3848static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003849handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003850{
Tiago Vignattibe143262012-04-16 17:31:41 +03003851 struct desktop_shell *shell =
3852 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003853
Martin Minarik6d118362012-06-07 18:01:59 +02003854 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003855 shell->lock_surface = NULL;
3856}
3857
3858static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003859desktop_shell_set_lock_surface(struct wl_client *client,
3860 struct wl_resource *resource,
3861 struct wl_resource *surface_resource)
3862{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003863 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003864 struct weston_surface *surface =
3865 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003866
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003867 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003868
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003869 if (!shell->locked)
3870 return;
3871
Pekka Paalanen98262232011-12-01 10:42:22 +02003872 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003873
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003874 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003875 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003876 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003877
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003878 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003879 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003880 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003881}
3882
3883static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003884resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003885{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003886 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003887
Pekka Paalanen77346a62011-11-30 16:26:35 +02003888 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003889
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003890 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003891 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003892 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003893 &shell->input_panel_layer.link);
3894 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003895 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003896 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003897 wl_list_insert(&shell->compositor->cursor_layer.link,
3898 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003899 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003900 wl_list_insert(&shell->fullscreen_layer.link,
3901 &shell->panel_layer.link);
3902 wl_list_insert(&shell->panel_layer.link,
3903 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003904
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003905 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003906
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003907 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003908 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003909 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003910}
3911
3912static void
3913desktop_shell_unlock(struct wl_client *client,
3914 struct wl_resource *resource)
3915{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003916 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003917
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003918 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003919
3920 if (shell->locked)
3921 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003922}
3923
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003924static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003925desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003926 struct wl_resource *resource,
3927 struct wl_resource *surface_resource)
3928{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003929 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003930
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003931 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003932 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003933}
3934
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003935static void
3936desktop_shell_desktop_ready(struct wl_client *client,
3937 struct wl_resource *resource)
3938{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003939 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003940
3941 shell_fade_startup(shell);
3942}
3943
Kristian Høgsberg75840622011-09-06 13:48:16 -04003944static const struct desktop_shell_interface desktop_shell_implementation = {
3945 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003946 desktop_shell_set_panel,
3947 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003948 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003949 desktop_shell_set_grab_surface,
3950 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003951};
3952
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003953static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003954get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003955{
3956 struct shell_surface *shsurf;
3957
3958 shsurf = get_shell_surface(surface);
3959 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003960 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003961 return shsurf->type;
3962}
3963
Kristian Høgsberg75840622011-09-06 13:48:16 -04003964static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003965move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003966{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003967 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003968 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003969 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003970
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003971 if (seat->pointer->focus == NULL)
3972 return;
3973
3974 focus = seat->pointer->focus->surface;
3975
Pekka Paalanen01388e22013-04-25 13:57:44 +03003976 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003977 if (surface == NULL)
3978 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003979
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003980 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003981 if (shsurf == NULL || shsurf->state.fullscreen ||
3982 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003983 return;
3984
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003985 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003986}
3987
3988static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003989maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3990{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003991 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003992 struct weston_surface *surface;
3993 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003994 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003995
3996 surface = weston_surface_get_main_surface(focus);
3997 if (surface == NULL)
3998 return;
3999
4000 shsurf = get_shell_surface(surface);
4001 if (shsurf == NULL)
4002 return;
4003
4004 if (!shell_surface_is_xdg_surface(shsurf))
4005 return;
4006
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004007 serial = wl_display_next_serial(seat->compositor->wl_display);
4008 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4009 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004010}
4011
4012static void
4013fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4014{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004015 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004016 struct weston_surface *surface;
4017 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004018 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004019
4020 surface = weston_surface_get_main_surface(focus);
4021 if (surface == NULL)
4022 return;
4023
4024 shsurf = get_shell_surface(surface);
4025 if (shsurf == NULL)
4026 return;
4027
4028 if (!shell_surface_is_xdg_surface(shsurf))
4029 return;
4030
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004031 serial = wl_display_next_serial(seat->compositor->wl_display);
4032 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4033 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004034}
4035
4036static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004037touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4038{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004039 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004040 struct weston_surface *surface;
4041 struct shell_surface *shsurf;
4042
4043 surface = weston_surface_get_main_surface(focus);
4044 if (surface == NULL)
4045 return;
4046
4047 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004048 if (shsurf == NULL || shsurf->state.fullscreen ||
4049 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004050 return;
4051
4052 surface_touch_move(shsurf, (struct weston_seat *) seat);
4053}
4054
4055static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004056resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004057{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004058 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004059 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004060 uint32_t edges = 0;
4061 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004062 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;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004072
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004073 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004074 if (shsurf == NULL || shsurf->state.fullscreen ||
4075 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004076 return;
4077
Jason Ekstranda7af7042013-10-12 22:38:11 -05004078 weston_view_from_global(shsurf->view,
4079 wl_fixed_to_int(seat->pointer->grab_x),
4080 wl_fixed_to_int(seat->pointer->grab_y),
4081 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004082
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004083 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004084 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004085 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004086 edges |= 0;
4087 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004088 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004089
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004090 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004091 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004092 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004093 edges |= 0;
4094 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004095 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004096
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004097 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004098}
4099
4100static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004101surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004102 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004103{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004104 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004105 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004106 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004107 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004108
Pekka Paalanen01388e22013-04-25 13:57:44 +03004109 /* XXX: broken for windows containing sub-surfaces */
4110 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004111 if (surface == NULL)
4112 return;
4113
4114 shsurf = get_shell_surface(surface);
4115 if (!shsurf)
4116 return;
4117
Jason Ekstranda7af7042013-10-12 22:38:11 -05004118 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004119
Jason Ekstranda7af7042013-10-12 22:38:11 -05004120 if (shsurf->view->alpha > 1.0)
4121 shsurf->view->alpha = 1.0;
4122 if (shsurf->view->alpha < step)
4123 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004124
Jason Ekstranda7af7042013-10-12 22:38:11 -05004125 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004126 weston_surface_damage(surface);
4127}
4128
4129static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004130do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004131 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004132{
Daniel Stone37816df2012-05-16 18:45:18 +01004133 struct weston_seat *ws = (struct weston_seat *) seat;
4134 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004135 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004136 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004137
4138 wl_list_for_each(output, &compositor->output_list, link) {
4139 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004140 wl_fixed_to_double(seat->pointer->x),
4141 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004142 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004143 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004144 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004145 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004146 increment = -output->zoom.increment;
4147 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004148 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004149 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004150 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004151 else
4152 increment = 0;
4153
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004154 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004155
Scott Moreaue6603982012-06-11 13:07:51 -06004156 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004157 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004158 else if (output->zoom.level > output->zoom.max_level)
4159 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004160 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004161 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004162 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004163
Scott Moreaue6603982012-06-11 13:07:51 -06004164 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004165
Jason Ekstranda7af7042013-10-12 22:38:11 -05004166 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004167 }
4168 }
4169}
4170
Scott Moreauccbf29d2012-02-22 14:21:41 -07004171static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004172zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004173 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004174{
4175 do_zoom(seat, time, 0, axis, value);
4176}
4177
4178static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004179zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004180 void *data)
4181{
4182 do_zoom(seat, time, key, 0, 0);
4183}
4184
4185static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004186terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004187 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004188{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004189 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004190
Daniel Stone325fc2d2012-05-30 16:31:58 +01004191 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004192}
4193
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004194static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004195rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4196 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004197{
4198 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004199 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004200 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004201 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004202 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004203
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004204 weston_pointer_move(pointer, x, y);
4205
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004206 if (!shsurf)
4207 return;
4208
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004209 cx = 0.5f * shsurf->surface->width;
4210 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004211
Daniel Stone37816df2012-05-16 18:45:18 +01004212 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4213 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004214 r = sqrtf(dx * dx + dy * dy);
4215
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004216 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004217 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004218
4219 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004220 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004221 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004222
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004223 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004224 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004225
4226 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004227 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004228 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004229 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004230 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004231
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004232 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004233 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004234 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004235 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004236 wl_list_init(&shsurf->rotation.transform.link);
4237 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004238 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004239 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004240
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004241 /* We need to adjust the position of the surface
4242 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004243 cposx = shsurf->view->geometry.x + cx;
4244 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004245 dposx = rotate->center.x - cposx;
4246 dposy = rotate->center.y - cposy;
4247 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004248 weston_view_set_position(shsurf->view,
4249 shsurf->view->geometry.x + dposx,
4250 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004251 }
4252
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004253 /* Repaint implies weston_surface_update_transform(), which
4254 * lazily applies the damage due to rotation update.
4255 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004256 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004257}
4258
4259static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004260rotate_grab_button(struct weston_pointer_grab *grab,
4261 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004262{
4263 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004264 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004265 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004266 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004267 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004268
Daniel Stone4dbadb12012-05-30 16:31:51 +01004269 if (pointer->button_count == 0 &&
4270 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004271 if (shsurf)
4272 weston_matrix_multiply(&shsurf->rotation.rotation,
4273 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004274 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004275 free(rotate);
4276 }
4277}
4278
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004279static void
4280rotate_grab_cancel(struct weston_pointer_grab *grab)
4281{
4282 struct rotate_grab *rotate =
4283 container_of(grab, struct rotate_grab, base.grab);
4284
4285 shell_grab_end(&rotate->base);
4286 free(rotate);
4287}
4288
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004289static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004290 noop_grab_focus,
4291 rotate_grab_motion,
4292 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004293 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004294};
4295
4296static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004297surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004298{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004299 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004300 float dx, dy;
4301 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004302
Pekka Paalanen460099f2012-01-20 16:48:25 +02004303 rotate = malloc(sizeof *rotate);
4304 if (!rotate)
4305 return;
4306
Jason Ekstranda7af7042013-10-12 22:38:11 -05004307 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004308 surface->surface->width * 0.5f,
4309 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004310 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004311
Daniel Stone37816df2012-05-16 18:45:18 +01004312 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4313 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004314 r = sqrtf(dx * dx + dy * dy);
4315 if (r > 20.0f) {
4316 struct weston_matrix inverse;
4317
4318 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004319 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004320 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004321
4322 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004323 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004324 } else {
4325 weston_matrix_init(&surface->rotation.rotation);
4326 weston_matrix_init(&rotate->rotation);
4327 }
4328
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004329 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4330 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004331}
4332
4333static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004334rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004335 void *data)
4336{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004337 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004338 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004339 struct shell_surface *surface;
4340
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004341 if (seat->pointer->focus == NULL)
4342 return;
4343
4344 focus = seat->pointer->focus->surface;
4345
Pekka Paalanen01388e22013-04-25 13:57:44 +03004346 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004347 if (base_surface == NULL)
4348 return;
4349
4350 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004351 if (surface == NULL || surface->state.fullscreen ||
4352 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004353 return;
4354
4355 surface_rotate(surface, seat);
4356}
4357
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004358/* Move all fullscreen layers down to the current workspace in a non-reversible
4359 * manner. This should be used when implementing shell-wide overlays, such as
4360 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004361void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004362lower_fullscreen_layer(struct desktop_shell *shell)
4363{
4364 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004365 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004366
4367 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004368 wl_list_for_each_reverse_safe(view, prev,
4369 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004370 layer_link) {
4371 wl_list_remove(&view->layer_link);
4372 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4373 weston_view_damage_below(view);
4374 weston_surface_damage(view->surface);
4375 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004376}
4377
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004378void
Tiago Vignattibe143262012-04-16 17:31:41 +03004379activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004380 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004381{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004382 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004383 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004384 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004385 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004386 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004387
Pekka Paalanen01388e22013-04-25 13:57:44 +03004388 main_surface = weston_surface_get_main_surface(es);
4389
Daniel Stone37816df2012-05-16 18:45:18 +01004390 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004391
Jonas Ådahl8538b222012-08-29 22:13:03 +02004392 state = ensure_focus_state(shell, seat);
4393 if (state == NULL)
4394 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004395
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004396 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004397 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004398
Rafael Antognolli03b16592013-12-03 15:35:42 -02004399 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004400 assert(shsurf);
4401
Rafael Antognolli03b16592013-12-03 15:35:42 -02004402 if (shsurf->state.fullscreen)
4403 shell_configure_fullscreen(shsurf);
4404 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004405 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004406
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004407 /* Update the surface’s layer. This brings it to the top of the stacking
4408 * order as appropriate. */
4409 shell_surface_update_layer(shsurf);
4410
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004411 if (shell->focus_animation_type != ANIMATION_NONE) {
4412 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004413 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004414 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004415}
4416
Alex Wu21858432012-04-01 20:13:08 +08004417/* no-op func for checking black surface */
4418static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004419black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004420{
4421}
4422
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004423static bool
Alex Wu21858432012-04-01 20:13:08 +08004424is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4425{
4426 if (es->configure == black_surface_configure) {
4427 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004428 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004429 return true;
4430 }
4431 return false;
4432}
4433
Kristian Høgsberg75840622011-09-06 13:48:16 -04004434static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004435activate_binding(struct weston_seat *seat,
4436 struct desktop_shell *shell,
4437 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004438{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004439 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004440
Alex Wu9c35e6b2012-03-05 14:13:13 +08004441 if (!focus)
4442 return;
4443
Pekka Paalanen01388e22013-04-25 13:57:44 +03004444 if (is_black_surface(focus, &main_surface))
4445 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004446
Pekka Paalanen01388e22013-04-25 13:57:44 +03004447 main_surface = weston_surface_get_main_surface(focus);
4448 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004449 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004450
Neil Robertsa28c6932013-10-03 16:43:04 +01004451 activate(shell, focus, seat);
4452}
4453
4454static void
4455click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4456 void *data)
4457{
4458 if (seat->pointer->grab != &seat->pointer->default_grab)
4459 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004460 if (seat->pointer->focus == NULL)
4461 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004462
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004463 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004464}
4465
4466static void
4467touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4468{
4469 if (seat->touch->grab != &seat->touch->default_grab)
4470 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004471 if (seat->touch->focus == NULL)
4472 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004473
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004474 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004475}
4476
4477static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004478lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004479{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004480 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004481
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004482 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004483 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004484 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004485 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004486
4487 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004488
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004489 /* Hide all surfaces by removing the fullscreen, panel and
4490 * toplevel layers. This way nothing else can show or receive
4491 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004492
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004493 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004494 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004495 if (shell->showing_input_panels)
4496 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004497 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004498 wl_list_insert(&shell->compositor->cursor_layer.link,
4499 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004500
Pekka Paalanen77346a62011-11-30 16:26:35 +02004501 launch_screensaver(shell);
4502
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004503 /* TODO: disable bindings that should not work while locked. */
4504
4505 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004506}
4507
4508static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004509unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004510{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004511 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004512 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004513 return;
4514 }
4515
4516 /* If desktop-shell client has gone away, unlock immediately. */
4517 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004518 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004519 return;
4520 }
4521
4522 if (shell->prepare_event_sent)
4523 return;
4524
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004525 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004526 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004527}
4528
4529static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004530shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004531{
4532 struct desktop_shell *shell = data;
4533
4534 shell->fade.animation = NULL;
4535
4536 switch (shell->fade.type) {
4537 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004538 weston_surface_destroy(shell->fade.view->surface);
4539 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004540 break;
4541 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004542 lock(shell);
4543 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004544 default:
4545 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004546 }
4547}
4548
Jason Ekstranda7af7042013-10-12 22:38:11 -05004549static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004550shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004551{
4552 struct weston_compositor *compositor = shell->compositor;
4553 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004554 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004555
4556 surface = weston_surface_create(compositor);
4557 if (!surface)
4558 return NULL;
4559
Jason Ekstranda7af7042013-10-12 22:38:11 -05004560 view = weston_view_create(surface);
4561 if (!view) {
4562 weston_surface_destroy(surface);
4563 return NULL;
4564 }
4565
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004566 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004567 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004568 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004569 wl_list_insert(&compositor->fade_layer.view_list,
4570 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004571 pixman_region32_init(&surface->input);
4572
Jason Ekstranda7af7042013-10-12 22:38:11 -05004573 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004574}
4575
4576static void
4577shell_fade(struct desktop_shell *shell, enum fade_type type)
4578{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004579 float tint;
4580
4581 switch (type) {
4582 case FADE_IN:
4583 tint = 0.0;
4584 break;
4585 case FADE_OUT:
4586 tint = 1.0;
4587 break;
4588 default:
4589 weston_log("shell: invalid fade type\n");
4590 return;
4591 }
4592
4593 shell->fade.type = type;
4594
Jason Ekstranda7af7042013-10-12 22:38:11 -05004595 if (shell->fade.view == NULL) {
4596 shell->fade.view = shell_fade_create_surface(shell);
4597 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004598 return;
4599
Jason Ekstranda7af7042013-10-12 22:38:11 -05004600 shell->fade.view->alpha = 1.0 - tint;
4601 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004602 }
4603
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004604 if (shell->fade.view->output == NULL) {
4605 /* If the black view gets a NULL output, we lost the
4606 * last output and we'll just cancel the fade. This
4607 * happens when you close the last window under the
4608 * X11 or Wayland backends. */
4609 shell->locked = false;
4610 weston_surface_destroy(shell->fade.view->surface);
4611 shell->fade.view = NULL;
4612 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004613 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004614 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004615 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004616 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004617 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004618 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004619 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004620}
4621
4622static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004623do_shell_fade_startup(void *data)
4624{
4625 struct desktop_shell *shell = data;
4626
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004627 if (shell->startup_animation_type == ANIMATION_FADE)
4628 shell_fade(shell, FADE_IN);
4629 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004630 weston_surface_destroy(shell->fade.view->surface);
4631 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004632 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004633}
4634
4635static void
4636shell_fade_startup(struct desktop_shell *shell)
4637{
4638 struct wl_event_loop *loop;
4639
4640 if (!shell->fade.startup_timer)
4641 return;
4642
4643 wl_event_source_remove(shell->fade.startup_timer);
4644 shell->fade.startup_timer = NULL;
4645
4646 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4647 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4648}
4649
4650static int
4651fade_startup_timeout(void *data)
4652{
4653 struct desktop_shell *shell = data;
4654
4655 shell_fade_startup(shell);
4656 return 0;
4657}
4658
4659static void
4660shell_fade_init(struct desktop_shell *shell)
4661{
4662 /* Make compositor output all black, and wait for the desktop-shell
4663 * client to signal it is ready, then fade in. The timer triggers a
4664 * fade-in, in case the desktop-shell client takes too long.
4665 */
4666
4667 struct wl_event_loop *loop;
4668
Jason Ekstranda7af7042013-10-12 22:38:11 -05004669 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004670 weston_log("%s: warning: fade surface already exists\n",
4671 __func__);
4672 return;
4673 }
4674
Jason Ekstranda7af7042013-10-12 22:38:11 -05004675 shell->fade.view = shell_fade_create_surface(shell);
4676 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004677 return;
4678
Jason Ekstranda7af7042013-10-12 22:38:11 -05004679 weston_view_update_transform(shell->fade.view);
4680 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004681
4682 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4683 shell->fade.startup_timer =
4684 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4685 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4686}
4687
4688static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004689idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004690{
4691 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004692 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004693 struct weston_seat *seat;
4694
4695 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4696 if (seat->pointer)
4697 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004698
4699 shell_fade(shell, FADE_OUT);
4700 /* lock() is called from shell_fade_done() */
4701}
4702
4703static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004704wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004705{
4706 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004707 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004708
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004709 unlock(shell);
4710}
4711
4712static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004713center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004714{
Giulio Camuffob8366642013-04-25 13:57:46 +03004715 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004716 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004717
Jason Ekstranda7af7042013-10-12 22:38:11 -05004718 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004719
4720 x = output->x + (output->width - width) / 2 - surf_x / 2;
4721 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004722
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004723 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004724}
4725
4726static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004727weston_view_set_initial_position(struct weston_view *view,
4728 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004729{
4730 struct weston_compositor *compositor = shell->compositor;
4731 int ix = 0, iy = 0;
4732 int range_x, range_y;
4733 int dx, dy, x, y, panel_height;
4734 struct weston_output *output, *target_output = NULL;
4735 struct weston_seat *seat;
4736
4737 /* As a heuristic place the new window on the same output as the
4738 * pointer. Falling back to the output containing 0, 0.
4739 *
4740 * TODO: Do something clever for touch too?
4741 */
4742 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004743 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004744 ix = wl_fixed_to_int(seat->pointer->x);
4745 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004746 break;
4747 }
4748 }
4749
4750 wl_list_for_each(output, &compositor->output_list, link) {
4751 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4752 target_output = output;
4753 break;
4754 }
4755 }
4756
4757 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004758 weston_view_set_position(view, 10 + random() % 400,
4759 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004760 return;
4761 }
4762
4763 /* Valid range within output where the surface will still be onscreen.
4764 * If this is negative it means that the surface is bigger than
4765 * output.
4766 */
4767 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004768 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004769 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004770 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004771
Rob Bradford4cb88c72012-08-13 15:18:44 +01004772 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004773 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004774 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004775 dx = 0;
4776
4777 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004778 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004779 else
4780 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004781
4782 x = target_output->x + dx;
4783 y = target_output->y + dy;
4784
Jason Ekstranda7af7042013-10-12 22:38:11 -05004785 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004786}
4787
4788static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004789map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004790 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004791{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004792 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004793 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004794 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004795 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004796
Pekka Paalanen77346a62011-11-30 16:26:35 +02004797 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004798 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004799 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004800 if (shsurf->state.fullscreen) {
4801 center_on_output(shsurf->view, shsurf->fullscreen_output);
4802 shell_map_fullscreen(shsurf);
4803 } else if (shsurf->state.maximized) {
4804 /* use surface configure to set the geometry */
4805 panel_height = get_output_panel_height(shell, shsurf->output);
4806 surface_subsurfaces_boundingbox(shsurf->surface,
4807 &surf_x, &surf_y, NULL, NULL);
4808 weston_view_set_position(shsurf->view,
4809 shsurf->output->x - surf_x,
4810 shsurf->output->y +
4811 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004812 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004813 weston_view_set_initial_position(shsurf->view, shell);
4814 }
Juan Zhao96879df2012-02-07 08:45:41 +08004815 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004816 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004817 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004818 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004819 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004820 weston_view_set_position(shsurf->view,
4821 shsurf->view->geometry.x + sx,
4822 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004823 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004824 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004825 default:
4826 ;
4827 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004828
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004829 /* Surface stacking order, see also activate(). */
4830 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004831
Jason Ekstranda7af7042013-10-12 22:38:11 -05004832 if (shsurf->type != SHELL_SURFACE_NONE) {
4833 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004834 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835 shsurf->surface->output = shsurf->output;
4836 shsurf->view->output = shsurf->output;
4837 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004838 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004839
Jason Ekstranda7af7042013-10-12 22:38:11 -05004840 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004841 /* XXX: xwayland's using the same fields for transient type */
4842 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004843 if (shsurf->transient.flags ==
4844 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4845 break;
4846 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004847 if (shsurf->state.relative &&
4848 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4849 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004850 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004851 break;
4852 wl_list_for_each(seat, &compositor->seat_list, link)
4853 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004854 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004855 case SHELL_SURFACE_POPUP:
4856 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004857 default:
4858 break;
4859 }
4860
Rafael Antognolli03b16592013-12-03 15:35:42 -02004861 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4862 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004863 {
4864 switch (shell->win_animation_type) {
4865 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004866 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004867 break;
4868 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004869 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004870 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004871 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004872 default:
4873 break;
4874 }
4875 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004876}
4877
4878static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004879configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004880 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004881{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004882 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004883 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004884 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004885
Pekka Paalanen77346a62011-11-30 16:26:35 +02004886 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004887
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004888 assert(shsurf);
4889
Rafael Antognolli03b16592013-12-03 15:35:42 -02004890 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004891 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004892 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004893 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004894 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4895 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004896 mx = shsurf->output->x - surf_x;
4897 my = shsurf->output->y +
4898 get_output_panel_height(shell,shsurf->output) - surf_y;
4899 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004900 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004901 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004902 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004903
Alex Wu4539b082012-03-01 12:57:46 +08004904 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004905 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004906 wl_list_for_each(view, &surface->views, surface_link)
4907 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004908
Rafael Antognolli03b16592013-12-03 15:35:42 -02004909 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004910 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004911 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004912}
4913
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004914static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004915shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004916{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004917 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004918 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004919 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004920
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004921 assert(shsurf);
4922
4923 shell = shsurf->shell;
4924
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004925 if (!weston_surface_is_mapped(es) &&
4926 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004927 remove_popup_grab(shsurf);
4928 }
4929
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004930 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004931 return;
4932
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004933 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004934 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004935 type_changed = 1;
4936 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004937
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004938 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004939 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004940 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004941 shsurf->last_width != es->width ||
4942 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004943 float from_x, from_y;
4944 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004945
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08004946 if (shsurf->resize_edges) {
4947 sx = 0;
4948 sy = 0;
4949 }
4950
4951 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
4952 sx = shsurf->last_width - es->width;
4953 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
4954 sy = shsurf->last_height - es->height;
4955
4956 shsurf->last_width = es->width;
4957 shsurf->last_height = es->height;
4958
Jason Ekstranda7af7042013-10-12 22:38:11 -05004959 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4960 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004961 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004962 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004963 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004964 }
4965}
4966
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004967static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004968
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004969static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004970desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004971{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004972 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004973 struct desktop_shell *shell =
4974 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004975
4976 shell->child.process.pid = 0;
4977 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004978
4979 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4980 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004981 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004982 shell->child.deathstamp = time;
4983 shell->child.deathcount = 0;
4984 }
4985
4986 shell->child.deathcount++;
4987 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004988 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004989 return;
4990 }
4991
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004992 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004993 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004994 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004995}
4996
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004997static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004998desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4999{
5000 struct desktop_shell *shell;
5001
5002 shell = container_of(listener, struct desktop_shell,
5003 child.client_destroy_listener);
5004
5005 shell->child.client = NULL;
5006}
5007
5008static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005009launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005010{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005011 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005012
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005013 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005014 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005015 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005016 desktop_shell_sigchld);
5017
5018 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005019 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005020
5021 shell->child.client_destroy_listener.notify =
5022 desktop_shell_client_destroy;
5023 wl_client_add_destroy_listener(shell->child.client,
5024 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005025}
5026
5027static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005028handle_shell_client_destroy(struct wl_listener *listener, void *data)
5029{
5030 struct shell_client *sc =
5031 container_of(listener, struct shell_client, destroy_listener);
5032
5033 if (sc->ping_timer)
5034 wl_event_source_remove(sc->ping_timer);
5035 free(sc);
5036}
5037
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005038static struct shell_client *
5039shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5040 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005041{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005042 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005043
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005044 sc = zalloc(sizeof *sc);
5045 if (sc == NULL) {
5046 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005047 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005048 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005049
5050 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005051 if (sc->resource == NULL) {
5052 free(sc);
5053 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005054 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005055 }
5056
5057 sc->client = client;
5058 sc->shell = shell;
5059 sc->destroy_listener.notify = handle_shell_client_destroy;
5060 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5061
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005062 return sc;
5063}
5064
5065static void
5066bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5067{
5068 struct desktop_shell *shell = data;
5069 struct shell_client *sc;
5070
5071 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5072 if (sc)
5073 wl_resource_set_implementation(sc->resource,
5074 &shell_implementation,
5075 shell, NULL);
5076}
5077
5078static void
5079bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5080{
5081 struct desktop_shell *shell = data;
5082 struct shell_client *sc;
5083
5084 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5085 if (sc)
5086 wl_resource_set_dispatcher(sc->resource,
5087 xdg_shell_unversioned_dispatch,
5088 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005089}
5090
5091static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005092unbind_desktop_shell(struct wl_resource *resource)
5093{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005094 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005095
5096 if (shell->locked)
5097 resume_desktop(shell);
5098
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005099 shell->child.desktop_shell = NULL;
5100 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005101}
5102
5103static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005104bind_desktop_shell(struct wl_client *client,
5105 void *data, uint32_t version, uint32_t id)
5106{
Tiago Vignattibe143262012-04-16 17:31:41 +03005107 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005108 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005109
Jason Ekstranda85118c2013-06-27 20:17:02 -05005110 resource = wl_resource_create(client, &desktop_shell_interface,
5111 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005112
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005113 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005114 wl_resource_set_implementation(resource,
5115 &desktop_shell_implementation,
5116 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005117 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005118
5119 if (version < 2)
5120 shell_fade_startup(shell);
5121
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005122 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005123 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005124
5125 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5126 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005127 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005128}
5129
Pekka Paalanen6e168112011-11-24 11:34:05 +02005130static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005131screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005132{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005133 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005134 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005135
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005136 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005137 return;
5138
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005139 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005140 if (!shell->locked)
5141 return;
5142
Jason Ekstranda7af7042013-10-12 22:38:11 -05005143 view = container_of(surface->views.next, struct weston_view, surface_link);
5144 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005145
Jason Ekstranda7af7042013-10-12 22:38:11 -05005146 if (wl_list_empty(&view->layer_link)) {
5147 wl_list_insert(shell->lock_layer.view_list.prev,
5148 &view->layer_link);
5149 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005150 wl_event_source_timer_update(shell->screensaver.timer,
5151 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005152 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005153 }
5154}
5155
5156static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005157screensaver_set_surface(struct wl_client *client,
5158 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005159 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005160 struct wl_resource *output_resource)
5161{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005162 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005163 struct weston_surface *surface =
5164 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005165 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005166 struct weston_view *view, *next;
5167
5168 /* Make sure we only have one view */
5169 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5170 weston_view_destroy(view);
5171 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005172
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005173 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005174 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005175 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005176}
5177
5178static const struct screensaver_interface screensaver_implementation = {
5179 screensaver_set_surface
5180};
5181
5182static void
5183unbind_screensaver(struct wl_resource *resource)
5184{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005185 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005186
Pekka Paalanen77346a62011-11-30 16:26:35 +02005187 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005188}
5189
5190static void
5191bind_screensaver(struct wl_client *client,
5192 void *data, uint32_t version, uint32_t id)
5193{
Tiago Vignattibe143262012-04-16 17:31:41 +03005194 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005195 struct wl_resource *resource;
5196
Jason Ekstranda85118c2013-06-27 20:17:02 -05005197 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005198
Pekka Paalanen77346a62011-11-30 16:26:35 +02005199 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005200 wl_resource_set_implementation(resource,
5201 &screensaver_implementation,
5202 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005203 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005204 return;
5205 }
5206
5207 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5208 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005209 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005210}
5211
Kristian Høgsberg07045392012-02-19 18:52:44 -05005212struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005213 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005214 struct weston_surface *current;
5215 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005216 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005217 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005218};
5219
5220static void
5221switcher_next(struct switcher *switcher)
5222{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005223 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005224 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005225 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005226 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005227
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005228 /* temporary re-display minimized surfaces */
5229 struct weston_view *tmp;
5230 struct weston_view **minimized;
5231 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5232 wl_list_remove(&view->layer_link);
5233 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5234 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5235 *minimized = view;
5236 }
5237
Jason Ekstranda7af7042013-10-12 22:38:11 -05005238 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005239 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005240 if (shsurf &&
5241 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5242 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005243 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005244 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005245 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005246 next = view->surface;
5247 prev = view->surface;
5248 view->alpha = 0.25;
5249 weston_view_geometry_dirty(view);
5250 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005251 }
Alex Wu1659daa2012-04-01 20:13:09 +08005252
Jason Ekstranda7af7042013-10-12 22:38:11 -05005253 if (is_black_surface(view->surface, NULL)) {
5254 view->alpha = 0.25;
5255 weston_view_geometry_dirty(view);
5256 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005257 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005258 }
5259
5260 if (next == NULL)
5261 next = first;
5262
Alex Wu07b26062012-03-12 16:06:01 +08005263 if (next == NULL)
5264 return;
5265
Kristian Høgsberg07045392012-02-19 18:52:44 -05005266 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005267 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005268
5269 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005270 wl_list_for_each(view, &next->views, surface_link)
5271 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005272
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005273 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005274 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005275 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005276}
5277
5278static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005279switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005280{
5281 struct switcher *switcher =
5282 container_of(listener, struct switcher, listener);
5283
5284 switcher_next(switcher);
5285}
5286
5287static void
Daniel Stone351eb612012-05-31 15:27:47 -04005288switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005289{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005290 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005291 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005292 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005293
Jason Ekstranda7af7042013-10-12 22:38:11 -05005294 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005295 if (is_focus_view(view))
5296 continue;
5297
Jason Ekstranda7af7042013-10-12 22:38:11 -05005298 view->alpha = 1.0;
5299 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005300 }
5301
Alex Wu07b26062012-03-12 16:06:01 +08005302 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005303 activate(switcher->shell, switcher->current,
5304 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005305 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005306 weston_keyboard_end_grab(keyboard);
5307 if (keyboard->input_method_resource)
5308 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005309
5310 /* re-hide surfaces that were temporary shown during the switch */
5311 struct weston_view **minimized;
5312 wl_array_for_each(minimized, &switcher->minimized_array) {
5313 /* with the exception of the current selected */
5314 if ((*minimized)->surface != switcher->current) {
5315 wl_list_remove(&(*minimized)->layer_link);
5316 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5317 weston_view_damage_below(*minimized);
5318 }
5319 }
5320 wl_array_release(&switcher->minimized_array);
5321
Kristian Høgsberg07045392012-02-19 18:52:44 -05005322 free(switcher);
5323}
5324
5325static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005326switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005327 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005328{
5329 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005330 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005331
Daniel Stonec9785ea2012-05-30 16:31:52 +01005332 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005333 switcher_next(switcher);
5334}
5335
5336static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005337switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005338 uint32_t mods_depressed, uint32_t mods_latched,
5339 uint32_t mods_locked, uint32_t group)
5340{
5341 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005342 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005343
Daniel Stone351eb612012-05-31 15:27:47 -04005344 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5345 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005346}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005347
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005348static void
5349switcher_cancel(struct weston_keyboard_grab *grab)
5350{
5351 struct switcher *switcher = container_of(grab, struct switcher, grab);
5352
5353 switcher_destroy(switcher);
5354}
5355
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005356static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005357 switcher_key,
5358 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005359 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005360};
5361
5362static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005363switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005364 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005365{
Tiago Vignattibe143262012-04-16 17:31:41 +03005366 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005367 struct switcher *switcher;
5368
5369 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005370 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005371 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005372 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005373 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005374 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005375
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005376 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005377 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005378 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005379 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5380 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005381 switcher_next(switcher);
5382}
5383
Pekka Paalanen3c647232011-12-22 13:43:43 +02005384static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005385backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005386 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005387{
5388 struct weston_compositor *compositor = data;
5389 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005390 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005391
5392 /* TODO: we're limiting to simple use cases, where we assume just
5393 * control on the primary display. We'd have to extend later if we
5394 * ever get support for setting backlights on random desktop LCD
5395 * panels though */
5396 output = get_default_output(compositor);
5397 if (!output)
5398 return;
5399
5400 if (!output->set_backlight)
5401 return;
5402
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005403 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5404 backlight_new = output->backlight_current - 25;
5405 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5406 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005407
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005408 if (backlight_new < 5)
5409 backlight_new = 5;
5410 if (backlight_new > 255)
5411 backlight_new = 255;
5412
5413 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005414 output->set_backlight(output, output->backlight_current);
5415}
5416
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005417struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005418 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005419 struct weston_seat *seat;
5420 uint32_t key[2];
5421 int key_released[2];
5422};
5423
5424static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005425debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005426 uint32_t key, uint32_t state)
5427{
5428 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005429 struct weston_compositor *ec = db->seat->compositor;
5430 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005431 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005432 uint32_t serial;
5433 int send = 0, terminate = 0;
5434 int check_binding = 1;
5435 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005436 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005437
5438 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5439 /* Do not run bindings on key releases */
5440 check_binding = 0;
5441
5442 for (i = 0; i < 2; i++)
5443 if (key == db->key[i])
5444 db->key_released[i] = 1;
5445
5446 if (db->key_released[0] && db->key_released[1]) {
5447 /* All key releases been swalled so end the grab */
5448 terminate = 1;
5449 } else if (key != db->key[0] && key != db->key[1]) {
5450 /* Should not swallow release of other keys */
5451 send = 1;
5452 }
5453 } else if (key == db->key[0] && !db->key_released[0]) {
5454 /* Do not check bindings for the first press of the binding
5455 * key. This allows it to be used as a debug shortcut.
5456 * We still need to swallow this event. */
5457 check_binding = 0;
5458 } else if (db->key[1]) {
5459 /* If we already ran a binding don't process another one since
5460 * we can't keep track of all the binding keys that were
5461 * pressed in order to swallow the release events. */
5462 send = 1;
5463 check_binding = 0;
5464 }
5465
5466 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005467 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5468 key, state)) {
5469 /* We ran a binding so swallow the press and keep the
5470 * grab to swallow the released too. */
5471 send = 0;
5472 terminate = 0;
5473 db->key[1] = key;
5474 } else {
5475 /* Terminate the grab since the key pressed is not a
5476 * debug binding key. */
5477 send = 1;
5478 terminate = 1;
5479 }
5480 }
5481
5482 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005483 serial = wl_display_next_serial(display);
5484 resource_list = &grab->keyboard->focus_resource_list;
5485 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005486 wl_keyboard_send_key(resource, serial, time, key, state);
5487 }
5488 }
5489
5490 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005491 weston_keyboard_end_grab(grab->keyboard);
5492 if (grab->keyboard->input_method_resource)
5493 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005494 free(db);
5495 }
5496}
5497
5498static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005499debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005500 uint32_t mods_depressed, uint32_t mods_latched,
5501 uint32_t mods_locked, uint32_t group)
5502{
5503 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005504 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005505
Neil Roberts96d790e2013-09-19 17:32:00 +01005506 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005507
Neil Roberts96d790e2013-09-19 17:32:00 +01005508 wl_resource_for_each(resource, resource_list) {
5509 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5510 mods_latched, mods_locked, group);
5511 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005512}
5513
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005514static void
5515debug_binding_cancel(struct weston_keyboard_grab *grab)
5516{
5517 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5518
5519 weston_keyboard_end_grab(grab->keyboard);
5520 free(db);
5521}
5522
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005523struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005524 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005525 debug_binding_modifiers,
5526 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005527};
5528
5529static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005530debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005531{
5532 struct debug_binding_grab *grab;
5533
5534 grab = calloc(1, sizeof *grab);
5535 if (!grab)
5536 return;
5537
5538 grab->seat = (struct weston_seat *) seat;
5539 grab->key[0] = key;
5540 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005541 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005542}
5543
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005544static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005545force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005546 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005547{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005548 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005549 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005550 struct desktop_shell *shell = data;
5551 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005552 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005553
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005554 focus_surface = seat->keyboard->focus;
5555 if (!focus_surface)
5556 return;
5557
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005558 wl_signal_emit(&compositor->kill_signal, focus_surface);
5559
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005560 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005561 wl_client_get_credentials(client, &pid, NULL, NULL);
5562
5563 /* Skip clients that we launched ourselves (the credentials of
5564 * the socketpair is ours) */
5565 if (pid == getpid())
5566 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005567
Daniel Stone325fc2d2012-05-30 16:31:58 +01005568 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005569}
5570
5571static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005572workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005573 uint32_t key, void *data)
5574{
5575 struct desktop_shell *shell = data;
5576 unsigned int new_index = shell->workspaces.current;
5577
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005578 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005579 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005580 if (new_index != 0)
5581 new_index--;
5582
5583 change_workspace(shell, new_index);
5584}
5585
5586static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005587workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005588 uint32_t key, void *data)
5589{
5590 struct desktop_shell *shell = data;
5591 unsigned int new_index = shell->workspaces.current;
5592
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005593 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005594 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005595 if (new_index < shell->workspaces.num - 1)
5596 new_index++;
5597
5598 change_workspace(shell, new_index);
5599}
5600
5601static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005602workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005603 uint32_t key, void *data)
5604{
5605 struct desktop_shell *shell = data;
5606 unsigned int new_index;
5607
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005608 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005609 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005610 new_index = key - KEY_F1;
5611 if (new_index >= shell->workspaces.num)
5612 new_index = shell->workspaces.num - 1;
5613
5614 change_workspace(shell, new_index);
5615}
5616
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005617static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005618workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005619 uint32_t key, void *data)
5620{
5621 struct desktop_shell *shell = data;
5622 unsigned int new_index = shell->workspaces.current;
5623
5624 if (shell->locked)
5625 return;
5626
5627 if (new_index != 0)
5628 new_index--;
5629
5630 take_surface_to_workspace_by_seat(shell, seat, new_index);
5631}
5632
5633static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005634workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005635 uint32_t key, void *data)
5636{
5637 struct desktop_shell *shell = data;
5638 unsigned int new_index = shell->workspaces.current;
5639
5640 if (shell->locked)
5641 return;
5642
5643 if (new_index < shell->workspaces.num - 1)
5644 new_index++;
5645
5646 take_surface_to_workspace_by_seat(shell, seat, new_index);
5647}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005648
5649static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005650shell_reposition_view_on_output_destroy(struct weston_view *view)
5651{
5652 struct weston_output *output, *first_output;
5653 struct weston_compositor *ec = view->surface->compositor;
5654 struct shell_surface *shsurf;
5655 float x, y;
5656 int visible;
5657
5658 x = view->geometry.x;
5659 y = view->geometry.y;
5660
5661 /* At this point the destroyed output is not in the list anymore.
5662 * If the view is still visible somewhere, we leave where it is,
5663 * otherwise, move it to the first output. */
5664 visible = 0;
5665 wl_list_for_each(output, &ec->output_list, link) {
5666 if (pixman_region32_contains_point(&output->region,
5667 x, y, NULL)) {
5668 visible = 1;
5669 break;
5670 }
5671 }
5672
5673 if (!visible) {
5674 first_output = container_of(ec->output_list.next,
5675 struct weston_output, link);
5676
5677 x = first_output->x + first_output->width / 4;
5678 y = first_output->y + first_output->height / 4;
5679 }
5680
5681 weston_view_set_position(view, x, y);
5682
5683 shsurf = get_shell_surface(view->surface);
5684
5685 if (shsurf) {
5686 shsurf->saved_position_valid = false;
5687 shsurf->next_state.maximized = false;
5688 shsurf->next_state.fullscreen = false;
5689 shsurf->state_changed = true;
5690 }
5691}
5692
5693static void
5694shell_reposition_views_on_output_destroy(struct shell_output *shell_output)
5695{
5696 struct desktop_shell *shell = shell_output->shell;
5697 struct weston_output *output = shell_output->output;
5698 struct weston_layer *layer;
5699 struct weston_view *view;
5700
5701 /* Move all views in the layers owned by the shell */
5702 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5703 wl_list_for_each(view, &layer->view_list, layer_link) {
5704 if (view->output != output)
5705 continue;
5706
5707 shell_reposition_view_on_output_destroy(view);
5708 }
5709
5710 /* We don't start from the beggining of the layer list, so
5711 * make sure we don't wrap around it. */
5712 if (layer == &shell->background_layer)
5713 break;
5714 }
5715}
5716
5717static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005718handle_output_destroy(struct wl_listener *listener, void *data)
5719{
5720 struct shell_output *output_listener =
5721 container_of(listener, struct shell_output, destroy_listener);
5722
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005723 shell_reposition_views_on_output_destroy(output_listener);
5724
Xiong Zhang6b481422013-10-23 13:58:32 +08005725 wl_list_remove(&output_listener->destroy_listener.link);
5726 wl_list_remove(&output_listener->link);
5727 free(output_listener);
5728}
5729
5730static void
5731create_shell_output(struct desktop_shell *shell,
5732 struct weston_output *output)
5733{
5734 struct shell_output *shell_output;
5735
5736 shell_output = zalloc(sizeof *shell_output);
5737 if (shell_output == NULL)
5738 return;
5739
5740 shell_output->output = output;
5741 shell_output->shell = shell;
5742 shell_output->destroy_listener.notify = handle_output_destroy;
5743 wl_signal_add(&output->destroy_signal,
5744 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005745 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005746}
5747
5748static void
5749handle_output_create(struct wl_listener *listener, void *data)
5750{
5751 struct desktop_shell *shell =
5752 container_of(listener, struct desktop_shell, output_create_listener);
5753 struct weston_output *output = (struct weston_output *)data;
5754
5755 create_shell_output(shell, output);
5756}
5757
5758static void
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005759handle_output_move(struct wl_listener *listener, void *data)
5760{
5761 struct desktop_shell *shell;
5762 struct weston_output *output;
5763 struct weston_layer *layer;
5764 struct weston_view *view;
5765 float x, y;
5766
5767 shell = container_of(listener, struct desktop_shell,
5768 output_move_listener);
5769 output = data;
5770
5771 /* Move all views in the layers owned by the shell */
5772 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5773 wl_list_for_each(view, &layer->view_list, layer_link) {
5774 if (view->output != output)
5775 continue;
5776
5777 x = view->geometry.x + output->move_x;
5778 y = view->geometry.y + output->move_y;
5779 weston_view_set_position(view, x, y);
5780 }
5781
5782 /* We don't start from the beggining of the layer list, so
5783 * make sure we don't wrap around it. */
5784 if (layer == &shell->background_layer)
5785 break;
5786 }
5787}
5788
5789static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005790setup_output_destroy_handler(struct weston_compositor *ec,
5791 struct desktop_shell *shell)
5792{
5793 struct weston_output *output;
5794
5795 wl_list_init(&shell->output_list);
5796 wl_list_for_each(output, &ec->output_list, link)
5797 create_shell_output(shell, output);
5798
5799 shell->output_create_listener.notify = handle_output_create;
5800 wl_signal_add(&ec->output_created_signal,
5801 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005802
5803 shell->output_move_listener.notify = handle_output_move;
5804 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005805}
5806
5807static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005808shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005809{
Tiago Vignattibe143262012-04-16 17:31:41 +03005810 struct desktop_shell *shell =
5811 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005812 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005813 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005814
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005815 /* Force state to unlocked so we don't try to fade */
5816 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005817 if (shell->child.client)
5818 wl_client_destroy(shell->child.client);
5819
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005820 wl_list_remove(&shell->idle_listener.link);
5821 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005822
5823 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005824
Xiong Zhang6b481422013-10-23 13:58:32 +08005825 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5826 wl_list_remove(&shell_output->destroy_listener.link);
5827 wl_list_remove(&shell_output->link);
5828 free(shell_output);
5829 }
5830
5831 wl_list_remove(&shell->output_create_listener.link);
5832
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005833 wl_array_for_each(ws, &shell->workspaces.array)
5834 workspace_destroy(*ws);
5835 wl_array_release(&shell->workspaces.array);
5836
Pekka Paalanen3c647232011-12-22 13:43:43 +02005837 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005838 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005839 free(shell);
5840}
5841
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005842static void
5843shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5844{
5845 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005846 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005847
5848 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005849 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5850 MODIFIER_CTRL | MODIFIER_ALT,
5851 terminate_binding, ec);
5852 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5853 click_to_activate_binding,
5854 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005855 weston_compositor_add_touch_binding(ec, 0,
5856 touch_to_activate_binding,
5857 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005858 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5859 MODIFIER_SUPER | MODIFIER_ALT,
5860 surface_opacity_binding, NULL);
5861 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5862 MODIFIER_SUPER, zoom_axis_binding,
5863 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005864
5865 /* configurable bindings */
5866 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005867 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5868 zoom_key_binding, NULL);
5869 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5870 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005871 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5872 maximize_binding, NULL);
5873 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5874 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005875 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5876 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005877 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005878 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5879 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005880 weston_compositor_add_button_binding(ec, BTN_LEFT,
5881 mod | MODIFIER_SHIFT,
5882 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005883
5884 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5885 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5886 rotate_binding, NULL);
5887
Daniel Stone325fc2d2012-05-30 16:31:58 +01005888 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5889 shell);
5890 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5891 ec);
5892 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5893 backlight_binding, ec);
5894 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5895 ec);
5896 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5897 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005898 weston_compositor_add_key_binding(ec, KEY_K, mod,
5899 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005900 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5901 workspace_up_binding, shell);
5902 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5903 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005904 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5905 workspace_move_surface_up_binding,
5906 shell);
5907 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5908 workspace_move_surface_down_binding,
5909 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005910
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005911 if (shell->exposay_modifier)
5912 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5913 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005914
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005915 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5916 if (shell->workspaces.num > 1) {
5917 num_workspace_bindings = shell->workspaces.num;
5918 if (num_workspace_bindings > 6)
5919 num_workspace_bindings = 6;
5920 for (i = 0; i < num_workspace_bindings; i++)
5921 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5922 workspace_f_binding,
5923 shell);
5924 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005925
5926 /* Debug bindings */
5927 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5928 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005929}
5930
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005931WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005932module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005933 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005934{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005935 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005936 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005937 struct workspace **pws;
5938 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005939 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005940
Peter Huttererf3d62272013-08-08 11:57:05 +10005941 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005942 if (shell == NULL)
5943 return -1;
5944
Kristian Høgsberg75840622011-09-06 13:48:16 -04005945 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005946
5947 shell->destroy_listener.notify = shell_destroy;
5948 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005949 shell->idle_listener.notify = idle_handler;
5950 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5951 shell->wake_listener.notify = wake_handler;
5952 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005953
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005954 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005955 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005956 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005957 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005958 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005959 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005960 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005961 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005962 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005963 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005964
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005965 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5966 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005967 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5968 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005969 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005970
5971 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005972 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005973
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005974 if (input_panel_setup(shell) < 0)
5975 return -1;
5976
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005977 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005978
Daniel Stonedf8133b2013-11-19 11:37:14 +01005979 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5980 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5981
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005982 for (i = 0; i < shell->workspaces.num; i++) {
5983 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5984 if (pws == NULL)
5985 return -1;
5986
5987 *pws = workspace_create();
5988 if (*pws == NULL)
5989 return -1;
5990 }
5991 activate_workspace(shell, 0);
5992
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005993 weston_layer_init(&shell->minimized_layer, NULL);
5994
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005995 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005996 wl_list_init(&shell->workspaces.animation.link);
5997 shell->workspaces.animation.frame = animate_workspace_change_frame;
5998
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005999 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006000 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006001 return -1;
6002
Rafael Antognollie2a34552013-12-03 15:35:45 -02006003 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6004 shell, bind_xdg_shell) == NULL)
6005 return -1;
6006
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006007 if (wl_global_create(ec->wl_display,
6008 &desktop_shell_interface, 2,
6009 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006010 return -1;
6011
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006012 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6013 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006014 return -1;
6015
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006016 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6017 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006018 return -1;
6019
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006020 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006021
Xiong Zhang6b481422013-10-23 13:58:32 +08006022 setup_output_destroy_handler(ec, shell);
6023
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006024 loop = wl_display_get_event_loop(ec->wl_display);
6025 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006026
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006027 shell->screensaver.timer =
6028 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6029
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006030 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006031 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006032 create_keyboard_focus_listener(seat);
6033 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006034
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006035 screenshooter_create(ec);
6036
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006037 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006038
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006039 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006040
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006041 return 0;
6042}