blob: ba5f2f3cee2dfabfb45ad0ed4fa5faec836ceef7 [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);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001810 /* Mark the shsurf as ungrabbed so that button binding is able
1811 * to move it. */
1812 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001813}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001814
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001815static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001816end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001817{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001818 struct shell_grab *grab;
1819 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001820
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001821 wl_list_for_each(seat, &compositor->seat_list, link) {
1822 if (seat->pointer == NULL)
1823 continue;
1824
1825 grab = (struct shell_grab *) seat->pointer->grab;
1826 if (grab->grab.interface == &busy_cursor_grab_interface &&
1827 wl_resource_get_client(grab->shsurf->resource) == client) {
1828 shell_grab_end(grab);
1829 free(grab);
1830 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001831 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001832}
1833
Scott Moreau9521d5e2012-04-19 13:06:17 -06001834static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001835handle_shell_client_destroy(struct wl_listener *listener, void *data);
1836
1837static int
1838xdg_ping_timeout_handler(void *data)
1839{
1840 struct shell_client *sc = data;
1841 struct weston_seat *seat;
1842 struct shell_surface *shsurf;
1843
1844 /* Client is not responding */
1845 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001846 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1847 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1848 continue;
1849 if (seat->pointer->focus->surface->resource == NULL)
1850 continue;
1851
1852 shsurf = get_shell_surface(seat->pointer->focus->surface);
1853 if (shsurf &&
1854 wl_resource_get_client(shsurf->resource) == sc->client)
1855 set_busy_cursor(shsurf, seat->pointer);
1856 }
1857
1858 return 1;
1859}
1860
1861static void
1862handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1863{
1864 struct weston_compositor *compositor = shsurf->shell->compositor;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001865 struct wl_client *client = wl_resource_get_client(shsurf->resource);
1866 struct shell_client *sc;
1867 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001868 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001869
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001870 sc = get_shell_client(client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001871 if (sc->unresponsive) {
1872 xdg_ping_timeout_handler(sc);
1873 return;
1874 }
1875
1876 sc->ping_serial = serial;
1877 loop = wl_display_get_event_loop(compositor->wl_display);
1878 if (sc->ping_timer == NULL)
1879 sc->ping_timer =
1880 wl_event_loop_add_timer(loop,
1881 xdg_ping_timeout_handler, sc);
1882 if (sc->ping_timer == NULL)
1883 return;
1884
1885 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1886
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001887 if (shell_surface_is_xdg_surface(shsurf) ||
1888 shell_surface_is_xdg_popup(shsurf))
1889 xdg_shell_send_ping(sc->resource, serial);
1890 else if (shell_surface_is_wl_shell_surface(shsurf))
1891 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001892}
1893
Scott Moreauff1db4a2012-04-17 19:06:18 -06001894static void
1895ping_handler(struct weston_surface *surface, uint32_t serial)
1896{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001897 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001898
1899 if (!shsurf)
1900 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001901 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001902 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001903 if (shsurf->surface == shsurf->shell->grab_surface)
1904 return;
1905
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001906 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001907}
1908
1909static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001910handle_pointer_focus(struct wl_listener *listener, void *data)
1911{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001912 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001913 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001914 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001915 uint32_t serial;
1916
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001918 return;
1919
Jason Ekstranda7af7042013-10-12 22:38:11 -05001920 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001921 serial = wl_display_next_serial(compositor->wl_display);
1922 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001923}
1924
1925static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001926create_pointer_focus_listener(struct weston_seat *seat)
1927{
1928 struct wl_listener *listener;
1929
Kristian Høgsberge3148752013-05-06 23:19:49 -04001930 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001931 return;
1932
1933 listener = malloc(sizeof *listener);
1934 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001935 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001936}
1937
1938static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001939shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1940{
1941 if (--shsurf->focus_count == 0)
1942 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001943 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001944}
1945
1946static void
1947shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1948{
1949 if (shsurf->focus_count++ == 0)
1950 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001951 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001952}
1953
1954static void
1955handle_keyboard_focus(struct wl_listener *listener, void *data)
1956{
1957 struct weston_keyboard *keyboard = data;
1958 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1959
1960 if (seat->focused_surface) {
1961 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1962 if (shsurf)
1963 shell_surface_lose_keyboard_focus(shsurf);
1964 }
1965
1966 seat->focused_surface = keyboard->focus;
1967
1968 if (seat->focused_surface) {
1969 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1970 if (shsurf)
1971 shell_surface_gain_keyboard_focus(shsurf);
1972 }
1973}
1974
1975static void
1976create_keyboard_focus_listener(struct weston_seat *seat)
1977{
1978 struct wl_listener *listener;
1979
1980 if (!seat->keyboard)
1981 return;
1982
1983 listener = malloc(sizeof *listener);
1984 listener->notify = handle_keyboard_focus;
1985 wl_signal_add(&seat->keyboard->focus_signal, listener);
1986}
1987
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001988static struct shell_client *
1989get_shell_client(struct wl_client *client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02001990{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001991 struct wl_listener *listener;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001992
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001993 listener = wl_client_get_destroy_listener(client,
1994 handle_shell_client_destroy);
1995 if (listener == NULL)
1996 return NULL;
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001997
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001998 return container_of(listener, struct shell_client, destroy_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001999}
2000
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002001static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002002shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002003{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002004 if (sc->ping_serial != serial)
2005 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002006
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002007 sc->unresponsive = 0;
2008 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002009
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002010 if (sc->ping_timer) {
2011 wl_event_source_remove(sc->ping_timer);
2012 sc->ping_timer = NULL;
2013 }
2014
2015}
2016
2017static void
2018shell_surface_pong(struct wl_client *client,
2019 struct wl_resource *resource, uint32_t serial)
2020{
2021 struct shell_client *sc;
2022
2023 sc = get_shell_client(client);
2024 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002025}
2026
2027static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002028set_title(struct shell_surface *shsurf, const char *title)
2029{
2030 free(shsurf->title);
2031 shsurf->title = strdup(title);
2032}
2033
2034static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002035shell_surface_set_title(struct wl_client *client,
2036 struct wl_resource *resource, const char *title)
2037{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002038 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002039
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002040 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002041}
2042
2043static void
2044shell_surface_set_class(struct wl_client *client,
2045 struct wl_resource *resource, const char *class)
2046{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002047 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002048
2049 free(shsurf->class);
2050 shsurf->class = strdup(class);
2051}
2052
Alex Wu4539b082012-03-01 12:57:46 +08002053static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002054restore_output_mode(struct weston_output *output)
2055{
Hardening57388e42013-09-18 23:56:36 +02002056 if (output->current_mode != output->original_mode ||
2057 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002058 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002059 output->original_mode,
2060 output->original_scale,
2061 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002062}
2063
2064static void
2065restore_all_output_modes(struct weston_compositor *compositor)
2066{
2067 struct weston_output *output;
2068
2069 wl_list_for_each(output, &compositor->output_list, link)
2070 restore_output_mode(output);
2071}
2072
Philip Withnallbecb77e2013-11-25 18:01:30 +00002073static int
2074get_output_panel_height(struct desktop_shell *shell,
2075 struct weston_output *output)
2076{
2077 struct weston_view *view;
2078 int panel_height = 0;
2079
2080 if (!output)
2081 return 0;
2082
2083 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2084 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002085 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002086 break;
2087 }
2088 }
2089
2090 return panel_height;
2091}
2092
Philip Withnall07926d92013-11-25 18:01:40 +00002093/* The surface will be inserted into the list immediately after the link
2094 * returned by this function (i.e. will be stacked immediately above the
2095 * returned link). */
2096static struct wl_list *
2097shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2098{
2099 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002100 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002101
2102 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002103 case SHELL_SURFACE_POPUP:
2104 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002105 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002106 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002107 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002108 /* Move the surface to its parent layer so
2109 * that surfaces which are transient for
2110 * fullscreen surfaces don't get hidden by the
2111 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002112
2113 /* TODO: Handle a parent with multiple views */
2114 parent = get_default_view(shsurf->parent);
2115 if (parent)
2116 return parent->layer_link.prev;
2117 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002118 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002119
2120 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002121 return &shsurf->shell->fullscreen_layer.view_list;
2122
Philip Withnall07926d92013-11-25 18:01:40 +00002123 case SHELL_SURFACE_NONE:
2124 default:
2125 /* Go to the fallback, below. */
2126 break;
2127 }
2128
2129 /* Move the surface to a normal workspace layer so that surfaces
2130 * which were previously fullscreen or transient are no longer
2131 * rendered on top. */
2132 ws = get_current_workspace(shsurf->shell);
2133 return &ws->layer.view_list;
2134}
2135
Philip Withnall648a4dd2013-11-25 18:01:44 +00002136static void
2137shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2138{
2139 struct shell_surface *child;
2140
2141 /* Move the child layers to the same workspace as shsurf. They will be
2142 * stacked above shsurf. */
2143 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2144 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2145 weston_view_geometry_dirty(child->view);
2146 wl_list_remove(&child->view->layer_link);
2147 wl_list_insert(shsurf->view->layer_link.prev,
2148 &child->view->layer_link);
2149 weston_view_geometry_dirty(child->view);
2150 weston_surface_damage(child->surface);
2151
2152 /* Recurse. We don’t expect this to recurse very far (if
2153 * at all) because that would imply we have transient
2154 * (or popup) children of transient surfaces, which
2155 * would be unusual. */
2156 shell_surface_update_child_surface_layers(child);
2157 }
2158 }
2159}
2160
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002161/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002162 * geometry to ensure the changes are redrawn.
2163 *
2164 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2165 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002166static void
2167shell_surface_update_layer(struct shell_surface *shsurf)
2168{
2169 struct wl_list *new_layer_link;
2170
2171 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2172
2173 if (new_layer_link == &shsurf->view->layer_link)
2174 return;
2175
2176 weston_view_geometry_dirty(shsurf->view);
2177 wl_list_remove(&shsurf->view->layer_link);
2178 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2179 weston_view_geometry_dirty(shsurf->view);
2180 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002181
2182 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002183}
2184
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002185static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002186shell_surface_set_parent(struct shell_surface *shsurf,
2187 struct weston_surface *parent)
2188{
2189 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002190
2191 wl_list_remove(&shsurf->children_link);
2192 wl_list_init(&shsurf->children_link);
2193
2194 /* Insert into the parent surface’s child list. */
2195 if (parent != NULL) {
2196 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2197 if (parent_shsurf != NULL)
2198 wl_list_insert(&parent_shsurf->children_list,
2199 &shsurf->children_link);
2200 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002201}
2202
2203static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002204shell_surface_set_output(struct shell_surface *shsurf,
2205 struct weston_output *output)
2206{
2207 struct weston_surface *es = shsurf->surface;
2208
2209 /* get the default output, if the client set it as NULL
2210 check whether the ouput is available */
2211 if (output)
2212 shsurf->output = output;
2213 else if (es->output)
2214 shsurf->output = es->output;
2215 else
2216 shsurf->output = get_default_output(es->compositor);
2217}
2218
2219static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002220surface_clear_next_states(struct shell_surface *shsurf)
2221{
2222 shsurf->next_state.maximized = false;
2223 shsurf->next_state.fullscreen = false;
2224
2225 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2226 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2227 shsurf->state_changed = true;
2228}
2229
2230static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002231set_toplevel(struct shell_surface *shsurf)
2232{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002233 shell_surface_set_parent(shsurf, NULL);
2234 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002235 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002236
2237 /* The layer_link is updated in set_surface_type(),
2238 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002239}
2240
2241static void
2242shell_surface_set_toplevel(struct wl_client *client,
2243 struct wl_resource *resource)
2244{
2245 struct shell_surface *surface = wl_resource_get_user_data(resource);
2246
2247 set_toplevel(surface);
2248}
2249
2250static void
2251set_transient(struct shell_surface *shsurf,
2252 struct weston_surface *parent, int x, int y, uint32_t flags)
2253{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002254 assert(parent != NULL);
2255
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002256 shell_surface_set_parent(shsurf, parent);
2257
2258 surface_clear_next_states(shsurf);
2259
Philip Withnallbecb77e2013-11-25 18:01:30 +00002260 shsurf->transient.x = x;
2261 shsurf->transient.y = y;
2262 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002263
Rafael Antognollied207b42013-12-03 15:35:43 -02002264 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002265 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002266 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002267
2268 /* The layer_link is updated in set_surface_type(),
2269 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002270}
2271
2272static void
2273shell_surface_set_transient(struct wl_client *client,
2274 struct wl_resource *resource,
2275 struct wl_resource *parent_resource,
2276 int x, int y, uint32_t flags)
2277{
2278 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2279 struct weston_surface *parent =
2280 wl_resource_get_user_data(parent_resource);
2281
2282 set_transient(shsurf, parent, x, y, flags);
2283}
2284
2285static void
2286set_fullscreen(struct shell_surface *shsurf,
2287 uint32_t method,
2288 uint32_t framerate,
2289 struct weston_output *output)
2290{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002291 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002292
2293 shsurf->fullscreen_output = shsurf->output;
2294 shsurf->fullscreen.type = method;
2295 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002296
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002297 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002298
2299 shsurf->client->send_configure(shsurf->surface, 0,
2300 shsurf->output->width,
2301 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002302
2303 /* The layer_link is updated in set_surface_type(),
2304 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002305}
2306
2307static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002308weston_view_set_initial_position(struct weston_view *view,
2309 struct desktop_shell *shell);
2310
2311static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002312unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002313{
Philip Withnallf85fe842013-11-25 18:01:37 +00002314 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002315 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2316 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002317 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002318 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002319 shsurf->fullscreen_output = NULL;
2320
Alex Wu4539b082012-03-01 12:57:46 +08002321 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2322 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002323
Alex Wu4539b082012-03-01 12:57:46 +08002324 wl_list_remove(&shsurf->fullscreen.transform.link);
2325 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002326
Jason Ekstranda7af7042013-10-12 22:38:11 -05002327 if (shsurf->fullscreen.black_view)
2328 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2329 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002330
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002331 if (shsurf->saved_position_valid)
2332 weston_view_set_position(shsurf->view,
2333 shsurf->saved_x, shsurf->saved_y);
2334 else
2335 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2336
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002337 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002338 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002339 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002340 shsurf->saved_rotation_valid = false;
2341 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002342
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002343 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002344}
2345
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002346static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002347shell_surface_set_fullscreen(struct wl_client *client,
2348 struct wl_resource *resource,
2349 uint32_t method,
2350 uint32_t framerate,
2351 struct wl_resource *output_resource)
2352{
2353 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2354 struct weston_output *output;
2355
2356 if (output_resource)
2357 output = wl_resource_get_user_data(output_resource);
2358 else
2359 output = NULL;
2360
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002361 shell_surface_set_parent(shsurf, NULL);
2362
Rafael Antognolli03b16592013-12-03 15:35:42 -02002363 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002364 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002365
2366 shsurf->next_state.fullscreen = true;
2367 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002368}
2369
2370static void
2371set_popup(struct shell_surface *shsurf,
2372 struct weston_surface *parent,
2373 struct weston_seat *seat,
2374 uint32_t serial,
2375 int32_t x,
2376 int32_t y)
2377{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002378 assert(parent != NULL);
2379
Philip Withnallbecb77e2013-11-25 18:01:30 +00002380 shsurf->popup.shseat = get_shell_seat(seat);
2381 shsurf->popup.serial = serial;
2382 shsurf->popup.x = x;
2383 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002384
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002385 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002386}
2387
2388static void
2389shell_surface_set_popup(struct wl_client *client,
2390 struct wl_resource *resource,
2391 struct wl_resource *seat_resource,
2392 uint32_t serial,
2393 struct wl_resource *parent_resource,
2394 int32_t x, int32_t y, uint32_t flags)
2395{
2396 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002397 struct weston_surface *parent =
2398 wl_resource_get_user_data(parent_resource);
2399
2400 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002401
Rafael Antognolli03b16592013-12-03 15:35:42 -02002402 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002403 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002404 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002405 wl_resource_get_user_data(seat_resource),
2406 serial, x, y);
2407}
2408
2409static void
2410set_maximized(struct shell_surface *shsurf,
2411 struct weston_output *output)
2412{
2413 struct desktop_shell *shell;
2414 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002415
Philip Withnall352e7ed2013-11-25 18:01:35 +00002416 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002417
2418 shell = shell_surface_get_shell(shsurf);
2419 panel_height = get_output_panel_height(shell, shsurf->output);
2420 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2421
2422 shsurf->client->send_configure(shsurf->surface, edges,
2423 shsurf->output->width,
2424 shsurf->output->height - panel_height);
2425
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002426 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002427}
2428
2429static void
2430unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002431{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002432 /* undo all maximized things here */
2433 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002434
2435 if (shsurf->saved_position_valid)
2436 weston_view_set_position(shsurf->view,
2437 shsurf->saved_x, shsurf->saved_y);
2438 else
2439 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002440
2441 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002442 wl_list_insert(&shsurf->view->geometry.transformation_list,
2443 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002444 shsurf->saved_rotation_valid = false;
2445 }
2446
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002447 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002448}
2449
Philip Withnallbecb77e2013-11-25 18:01:30 +00002450static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002451set_minimized(struct weston_surface *surface, uint32_t is_true)
2452{
2453 struct shell_surface *shsurf;
2454 struct workspace *current_ws;
2455 struct weston_seat *seat;
2456 struct weston_surface *focus;
2457 struct weston_view *view;
2458
2459 view = get_default_view(surface);
2460 if (!view)
2461 return;
2462
2463 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2464
2465 shsurf = get_shell_surface(surface);
2466 current_ws = get_current_workspace(shsurf->shell);
2467
2468 wl_list_remove(&view->layer_link);
2469 /* hide or show, depending on the state */
2470 if (is_true) {
2471 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2472
2473 drop_focus_state(shsurf->shell, current_ws, view->surface);
2474 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2475 if (!seat->keyboard)
2476 continue;
2477 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2478 if (focus == view->surface)
2479 weston_keyboard_set_focus(seat->keyboard, NULL);
2480 }
2481 }
2482 else {
2483 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2484
2485 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2486 if (!seat->keyboard)
2487 continue;
2488 activate(shsurf->shell, view->surface, seat);
2489 }
2490 }
2491
2492 shell_surface_update_child_surface_layers(shsurf);
2493
2494 weston_view_damage_below(view);
2495}
2496
2497static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002498shell_surface_set_maximized(struct wl_client *client,
2499 struct wl_resource *resource,
2500 struct wl_resource *output_resource)
2501{
2502 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2503 struct weston_output *output;
2504
2505 if (output_resource)
2506 output = wl_resource_get_user_data(output_resource);
2507 else
2508 output = NULL;
2509
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002510 shell_surface_set_parent(shsurf, NULL);
2511
Rafael Antognolli03b16592013-12-03 15:35:42 -02002512 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002513 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002514
2515 shsurf->next_state.maximized = true;
2516 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002517}
2518
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002519/* This is only ever called from set_surface_type(), so there’s no need to
2520 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002521static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002522reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002523{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002524 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002525 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002526 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002527 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002528
Pekka Paalanen98262232011-12-01 10:42:22 +02002529 return 0;
2530}
2531
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002532static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002533set_full_output(struct shell_surface *shsurf)
2534{
2535 shsurf->saved_x = shsurf->view->geometry.x;
2536 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002537 shsurf->saved_width = shsurf->surface->width;
2538 shsurf->saved_height = shsurf->surface->height;
2539 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002540 shsurf->saved_position_valid = true;
2541
2542 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2543 wl_list_remove(&shsurf->rotation.transform.link);
2544 wl_list_init(&shsurf->rotation.transform.link);
2545 weston_view_geometry_dirty(shsurf->view);
2546 shsurf->saved_rotation_valid = true;
2547 }
2548}
2549
2550static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002551set_surface_type(struct shell_surface *shsurf)
2552{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002553 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002554 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002555
Philip Withnallbecb77e2013-11-25 18:01:30 +00002556 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002557
Rafael Antognolli03b16592013-12-03 15:35:42 -02002558 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002559 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002560
2561 switch (shsurf->type) {
2562 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002563 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002564 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002565 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002566 weston_view_set_position(shsurf->view,
2567 pev->geometry.x + shsurf->transient.x,
2568 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002569 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002570 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002571
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002572 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002573 weston_view_set_position(shsurf->view, shsurf->transient.x,
2574 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002575 break;
2576
Philip Withnall0f640e22013-11-25 18:01:31 +00002577 case SHELL_SURFACE_POPUP:
2578 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002579 default:
2580 break;
2581 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002582
2583 /* Update the surface’s layer. */
2584 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002585}
2586
Tiago Vignattibe143262012-04-16 17:31:41 +03002587static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002588shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002589{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002590 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002591}
2592
Alex Wu21858432012-04-01 20:13:08 +08002593static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002594black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002595
Jason Ekstranda7af7042013-10-12 22:38:11 -05002596static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002597create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002598 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002599 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002600{
2601 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002602 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002603
2604 surface = weston_surface_create(ec);
2605 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002606 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002607 return NULL;
2608 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002609 view = weston_view_create(surface);
2610 if (surface == NULL) {
2611 weston_log("no memory\n");
2612 weston_surface_destroy(surface);
2613 return NULL;
2614 }
Alex Wu4539b082012-03-01 12:57:46 +08002615
Alex Wu21858432012-04-01 20:13:08 +08002616 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002617 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002618 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002619 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002620 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002621 pixman_region32_fini(&surface->input);
2622 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002623
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002624 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002625 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002626
2627 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002628}
2629
Philip Withnalled8f1a92013-11-25 18:01:43 +00002630static void
2631shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2632{
2633 struct weston_output *output = shsurf->fullscreen_output;
2634
Rafael Antognolli03b16592013-12-03 15:35:42 -02002635 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002636
2637 if (!shsurf->fullscreen.black_view)
2638 shsurf->fullscreen.black_view =
2639 create_black_surface(shsurf->surface->compositor,
2640 shsurf->surface,
2641 output->x, output->y,
2642 output->width,
2643 output->height);
2644
2645 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2646 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2647 wl_list_insert(&shsurf->view->layer_link,
2648 &shsurf->fullscreen.black_view->layer_link);
2649 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2650 weston_surface_damage(shsurf->surface);
2651}
2652
Alex Wu4539b082012-03-01 12:57:46 +08002653/* Create black surface and append it to the associated fullscreen surface.
2654 * Handle size dismatch and positioning according to the method. */
2655static void
2656shell_configure_fullscreen(struct shell_surface *shsurf)
2657{
2658 struct weston_output *output = shsurf->fullscreen_output;
2659 struct weston_surface *surface = shsurf->surface;
2660 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002661 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002662 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002663
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002664 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2665 restore_output_mode(output);
2666
Philip Withnalled8f1a92013-11-25 18:01:43 +00002667 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002668
Jason Ekstranda7af7042013-10-12 22:38:11 -05002669 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002670 &surf_width, &surf_height);
2671
Alex Wu4539b082012-03-01 12:57:46 +08002672 switch (shsurf->fullscreen.type) {
2673 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002674 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002675 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002676 break;
2677 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002678 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002679 if (output->width == surf_width &&
2680 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002681 weston_view_set_position(shsurf->view,
2682 output->x - surf_x,
2683 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002684 break;
2685 }
2686
Alex Wu4539b082012-03-01 12:57:46 +08002687 matrix = &shsurf->fullscreen.transform.matrix;
2688 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002689
Scott Moreau1bad5db2012-08-18 01:04:05 -06002690 output_aspect = (float) output->width /
2691 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002692 /* XXX: Use surf_width and surf_height here? */
2693 surface_aspect = (float) surface->width /
2694 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002695 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002696 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002697 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002698 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002699 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002700 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002701
Alex Wu4539b082012-03-01 12:57:46 +08002702 weston_matrix_scale(matrix, scale, scale, 1);
2703 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002704 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002705 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002706 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2707 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002708 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002709
Alex Wu4539b082012-03-01 12:57:46 +08002710 break;
2711 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002712 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002713 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002714 surf_width * surface->buffer_viewport.buffer.scale,
2715 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002716 shsurf->fullscreen.framerate};
2717
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002718 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002719 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002720 weston_view_set_position(shsurf->view,
2721 output->x - surf_x,
2722 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002723 shsurf->fullscreen.black_view->surface->width = output->width;
2724 shsurf->fullscreen.black_view->surface->height = output->height;
2725 weston_view_set_position(shsurf->fullscreen.black_view,
2726 output->x - surf_x,
2727 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002728 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002729 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002730 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002731 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002732 }
Alex Wubd3354b2012-04-17 17:20:49 +08002733 }
Alex Wu4539b082012-03-01 12:57:46 +08002734 break;
2735 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002736 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002737 break;
2738 default:
2739 break;
2740 }
2741}
2742
Alex Wu4539b082012-03-01 12:57:46 +08002743static void
2744shell_map_fullscreen(struct shell_surface *shsurf)
2745{
Alex Wubd3354b2012-04-17 17:20:49 +08002746 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002747}
2748
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002749static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002750set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2751{
2752 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002753 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002754 shsurf->transient.x = x;
2755 shsurf->transient.y = y;
2756 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002757
2758 shell_surface_set_parent(shsurf, NULL);
2759
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002760 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002761 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002762}
2763
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002764static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002765
2766static void
2767destroy_shell_seat(struct wl_listener *listener, void *data)
2768{
2769 struct shell_seat *shseat =
2770 container_of(listener,
2771 struct shell_seat, seat_destroy_listener);
2772 struct shell_surface *shsurf, *prev = NULL;
2773
2774 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002775 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002776 shseat->popup_grab.client = NULL;
2777
2778 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2779 shsurf->popup.shseat = NULL;
2780 if (prev) {
2781 wl_list_init(&prev->popup.grab_link);
2782 }
2783 prev = shsurf;
2784 }
2785 wl_list_init(&prev->popup.grab_link);
2786 }
2787
2788 wl_list_remove(&shseat->seat_destroy_listener.link);
2789 free(shseat);
2790}
2791
2792static struct shell_seat *
2793create_shell_seat(struct weston_seat *seat)
2794{
2795 struct shell_seat *shseat;
2796
2797 shseat = calloc(1, sizeof *shseat);
2798 if (!shseat) {
2799 weston_log("no memory to allocate shell seat\n");
2800 return NULL;
2801 }
2802
2803 shseat->seat = seat;
2804 wl_list_init(&shseat->popup_grab.surfaces_list);
2805
2806 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2807 wl_signal_add(&seat->destroy_signal,
2808 &shseat->seat_destroy_listener);
2809
2810 return shseat;
2811}
2812
2813static struct shell_seat *
2814get_shell_seat(struct weston_seat *seat)
2815{
2816 struct wl_listener *listener;
2817
2818 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2819 if (listener == NULL)
2820 return create_shell_seat(seat);
2821
2822 return container_of(listener,
2823 struct shell_seat, seat_destroy_listener);
2824}
2825
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002826static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002827popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002828{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002829 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002830 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002831 struct shell_seat *shseat =
2832 container_of(grab, struct shell_seat, popup_grab.grab);
2833 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002834 wl_fixed_t sx, sy;
2835
Jason Ekstranda7af7042013-10-12 22:38:11 -05002836 view = weston_compositor_pick_view(pointer->seat->compositor,
2837 pointer->x, pointer->y,
2838 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002839
Jason Ekstranda7af7042013-10-12 22:38:11 -05002840 if (view && view->surface->resource &&
2841 wl_resource_get_client(view->surface->resource) == client) {
2842 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002843 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002844 weston_pointer_set_focus(pointer, NULL,
2845 wl_fixed_from_int(0),
2846 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002847 }
2848}
2849
2850static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002851popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2852 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002853{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002854 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002855 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002856 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002857
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002858 weston_pointer_move(pointer, x, y);
2859
Neil Roberts96d790e2013-09-19 17:32:00 +01002860 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002861 weston_view_from_global_fixed(pointer->focus,
2862 pointer->x, pointer->y,
2863 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002864 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002865 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002866}
2867
2868static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002869popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002870 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002871{
2872 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002873 struct shell_seat *shseat =
2874 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002875 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002876 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002877 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002878 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002879
Neil Roberts96d790e2013-09-19 17:32:00 +01002880 resource_list = &grab->pointer->focus_resource_list;
2881 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002882 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002883 wl_resource_for_each(resource, resource_list) {
2884 wl_pointer_send_button(resource, serial,
2885 time, button, state);
2886 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002887 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002888 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002889 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002890 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002891 }
2892
Daniel Stone4dbadb12012-05-30 16:31:51 +01002893 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002894 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002895}
2896
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002897static void
2898popup_grab_cancel(struct weston_pointer_grab *grab)
2899{
2900 popup_grab_end(grab->pointer);
2901}
2902
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002903static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002904 popup_grab_focus,
2905 popup_grab_motion,
2906 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002907 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002908};
2909
2910static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002911shell_surface_send_popup_done(struct shell_surface *shsurf)
2912{
2913 if (shell_surface_is_wl_shell_surface(shsurf))
2914 wl_shell_surface_send_popup_done(shsurf->resource);
2915 else if (shell_surface_is_xdg_popup(shsurf))
2916 xdg_popup_send_popup_done(shsurf->resource,
2917 shsurf->popup.serial);
2918}
2919
2920static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002921popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002922{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002923 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002924 struct shell_seat *shseat =
2925 container_of(grab, struct shell_seat, popup_grab.grab);
2926 struct shell_surface *shsurf;
2927 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002928
2929 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002930 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002931 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002932 shseat->popup_grab.grab.interface = NULL;
2933 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002934 /* Send the popup_done event to all the popups open */
2935 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002936 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002937 shsurf->popup.shseat = NULL;
2938 if (prev) {
2939 wl_list_init(&prev->popup.grab_link);
2940 }
2941 prev = shsurf;
2942 }
2943 wl_list_init(&prev->popup.grab_link);
2944 wl_list_init(&shseat->popup_grab.surfaces_list);
2945 }
2946}
2947
2948static void
2949add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2950{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002951 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002952
2953 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002954 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002955 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002956 /* We must make sure here that this popup was opened after
2957 * a mouse press, and not just by moving around with other
2958 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002959 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002960 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002961
Rob Bradforddfe31052013-06-26 19:49:11 +01002962 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002963 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002964 } else {
2965 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002966 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002967}
2968
2969static void
2970remove_popup_grab(struct shell_surface *shsurf)
2971{
2972 struct shell_seat *shseat = shsurf->popup.shseat;
2973
2974 wl_list_remove(&shsurf->popup.grab_link);
2975 wl_list_init(&shsurf->popup.grab_link);
2976 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002977 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002978 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002979 }
2980}
2981
2982static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002983shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002984{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002985 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002986 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002987
Jason Ekstranda7af7042013-10-12 22:38:11 -05002988 shsurf->surface->output = parent_view->output;
2989 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002990
Jason Ekstranda7af7042013-10-12 22:38:11 -05002991 weston_view_set_transform_parent(shsurf->view, parent_view);
2992 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2993 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002994
Kristian Høgsberge3148752013-05-06 23:19:49 -04002995 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002996 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002997 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002998 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002999 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003000 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003001}
3002
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003003static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003004 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003005 shell_surface_move,
3006 shell_surface_resize,
3007 shell_surface_set_toplevel,
3008 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003009 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003010 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003011 shell_surface_set_maximized,
3012 shell_surface_set_title,
3013 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003014};
3015
3016static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003017destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003018{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003019 struct shell_surface *child, *next;
3020
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003021 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3022
Giulio Camuffo5085a752013-03-25 21:42:45 +01003023 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3024 remove_popup_grab(shsurf);
3025 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003026
Alex Wubd3354b2012-04-17 17:20:49 +08003027 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003028 shell_surface_is_top_fullscreen(shsurf))
3029 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003030
Jason Ekstranda7af7042013-10-12 22:38:11 -05003031 if (shsurf->fullscreen.black_view)
3032 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003033
Alex Wubd3354b2012-04-17 17:20:49 +08003034 /* As destroy_resource() use wl_list_for_each_safe(),
3035 * we can always remove the listener.
3036 */
3037 wl_list_remove(&shsurf->surface_destroy_listener.link);
3038 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003039 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003040
Jason Ekstranda7af7042013-10-12 22:38:11 -05003041 weston_view_destroy(shsurf->view);
3042
Philip Withnall648a4dd2013-11-25 18:01:44 +00003043 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003044 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3045 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003046
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003047 wl_list_remove(&shsurf->link);
3048 free(shsurf);
3049}
3050
3051static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003052shell_destroy_shell_surface(struct wl_resource *resource)
3053{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003054 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003055
Bryan Caina46b9462014-04-04 17:41:24 -05003056 if (!wl_list_empty(&shsurf->popup.grab_link))
3057 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003058 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003059}
3060
3061static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003062shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003063{
3064 struct shell_surface *shsurf = container_of(listener,
3065 struct shell_surface,
3066 surface_destroy_listener);
3067
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003068 if (shsurf->resource)
3069 wl_resource_destroy(shsurf->resource);
3070 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003071 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003072}
3073
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003074static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003075fade_out_done(struct weston_view_animation *animation, void *data)
3076{
3077 struct shell_surface *shsurf = data;
3078
3079 weston_surface_destroy(shsurf->surface);
3080}
3081
3082static void
3083handle_resource_destroy(struct wl_listener *listener, void *data)
3084{
3085 struct shell_surface *shsurf =
3086 container_of(listener, struct shell_surface,
3087 resource_destroy_listener);
3088
3089 shsurf->surface->ref_count++;
3090
3091 pixman_region32_fini(&shsurf->surface->pending.input);
3092 pixman_region32_init(&shsurf->surface->pending.input);
3093 pixman_region32_fini(&shsurf->surface->input);
3094 pixman_region32_init(&shsurf->surface->input);
Kristian Høgsbergb033c5e2014-03-20 14:49:07 -07003095 if (weston_surface_is_mapped(shsurf->surface))
3096 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3097 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003098}
3099
3100static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003101shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003102
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003103struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003104get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003105{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003106 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003107 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003108 else
3109 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003110}
3111
Rafael Antognollie2a34552013-12-03 15:35:45 -02003112static struct shell_surface *
3113create_common_surface(void *shell, struct weston_surface *surface,
3114 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003115{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003116 struct shell_surface *shsurf;
3117
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003118 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003119 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003120 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003121 }
3122
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003123 shsurf = calloc(1, sizeof *shsurf);
3124 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003125 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003126 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003127 }
3128
Jason Ekstranda7af7042013-10-12 22:38:11 -05003129 shsurf->view = weston_view_create(surface);
3130 if (!shsurf->view) {
3131 weston_log("no memory to allocate shell surface\n");
3132 free(shsurf);
3133 return NULL;
3134 }
3135
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003136 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003137 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003138
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003139 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3140 wl_resource_add_destroy_listener(surface->resource,
3141 &shsurf->resource_destroy_listener);
3142
Tiago Vignattibc052c92012-04-19 16:18:18 +03003143 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003144 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003145 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003146 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003147 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003148 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003149 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3150 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003151 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003152 wl_list_init(&shsurf->fullscreen.transform.link);
3153
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003154 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003155 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003156 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003157 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003158
3159 /* init link so its safe to always remove it in destroy_shell_surface */
3160 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003161 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003162
Pekka Paalanen460099f2012-01-20 16:48:25 +02003163 /* empty when not in use */
3164 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003165 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003166
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003167 wl_list_init(&shsurf->workspace_transform.link);
3168
Philip Withnall648a4dd2013-11-25 18:01:44 +00003169 wl_list_init(&shsurf->children_link);
3170 wl_list_init(&shsurf->children_list);
3171 shsurf->parent = NULL;
3172
Pekka Paalanen98262232011-12-01 10:42:22 +02003173 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003174
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003175 shsurf->client = client;
3176
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003177 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003178}
3179
Rafael Antognollie2a34552013-12-03 15:35:45 -02003180static struct shell_surface *
3181create_shell_surface(void *shell, struct weston_surface *surface,
3182 const struct weston_shell_client *client)
3183{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003184 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003185}
3186
Jason Ekstranda7af7042013-10-12 22:38:11 -05003187static struct weston_view *
3188get_primary_view(void *shell, struct shell_surface *shsurf)
3189{
3190 return shsurf->view;
3191}
3192
Tiago Vignattibc052c92012-04-19 16:18:18 +03003193static void
3194shell_get_shell_surface(struct wl_client *client,
3195 struct wl_resource *resource,
3196 uint32_t id,
3197 struct wl_resource *surface_resource)
3198{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003199 struct weston_surface *surface =
3200 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003201 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003202 struct shell_surface *shsurf;
3203
3204 if (get_shell_surface(surface)) {
3205 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003206 WL_DISPLAY_ERROR_INVALID_OBJECT,
3207 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003208 return;
3209 }
3210
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003211 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003212 if (!shsurf) {
3213 wl_resource_post_error(surface_resource,
3214 WL_DISPLAY_ERROR_INVALID_OBJECT,
3215 "surface->configure already set");
3216 return;
3217 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003218
Jason Ekstranda85118c2013-06-27 20:17:02 -05003219 shsurf->resource =
3220 wl_resource_create(client,
3221 &wl_shell_surface_interface, 1, id);
3222 wl_resource_set_implementation(shsurf->resource,
3223 &shell_surface_implementation,
3224 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003225}
3226
Rafael Antognollie2a34552013-12-03 15:35:45 -02003227static bool
3228shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3229{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003230 /* A shell surface without a resource is created from xwayland
3231 * and is considered a wl_shell surface for now. */
3232
3233 return shsurf->resource == NULL ||
3234 wl_resource_instance_of(shsurf->resource,
3235 &wl_shell_surface_interface,
3236 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003237}
3238
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003239static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003240 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003241};
3242
Rafael Antognollie2a34552013-12-03 15:35:45 -02003243/****************************
3244 * xdg-shell implementation */
3245
3246static void
3247xdg_surface_destroy(struct wl_client *client,
3248 struct wl_resource *resource)
3249{
3250 wl_resource_destroy(resource);
3251}
3252
3253static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003254xdg_surface_set_transient_for(struct wl_client *client,
3255 struct wl_resource *resource,
3256 struct wl_resource *parent_resource)
3257{
3258 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3259 struct weston_surface *parent;
3260
3261 if (parent_resource)
3262 parent = wl_resource_get_user_data(parent_resource);
3263 else
3264 parent = NULL;
3265
3266 shell_surface_set_parent(shsurf, parent);
3267}
3268
3269static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003270xdg_surface_set_margin(struct wl_client *client,
3271 struct wl_resource *resource,
3272 int32_t left,
3273 int32_t right,
3274 int32_t top,
3275 int32_t bottom)
3276{
3277 /* Do nothing, Weston doesn't try to constrain or place
3278 * surfaces in any special manner... */
3279}
3280
3281static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003282xdg_surface_set_app_id(struct wl_client *client,
3283 struct wl_resource *resource,
3284 const char *app_id)
3285{
3286 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3287
3288 free(shsurf->class);
3289 shsurf->class = strdup(app_id);
3290}
3291
3292static void
3293xdg_surface_set_title(struct wl_client *client,
3294 struct wl_resource *resource, const char *title)
3295{
3296 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3297
3298 set_title(shsurf, title);
3299}
3300
3301static void
3302xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3303 struct wl_resource *seat_resource, uint32_t serial)
3304{
3305 common_surface_move(resource, seat_resource, serial);
3306}
3307
3308static void
3309xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3310 struct wl_resource *seat_resource, uint32_t serial,
3311 uint32_t edges)
3312{
3313 common_surface_resize(resource, seat_resource, serial, edges);
3314}
3315
3316static void
3317xdg_surface_set_output(struct wl_client *client,
3318 struct wl_resource *resource,
3319 struct wl_resource *output_resource)
3320{
3321 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3322 struct weston_output *output;
3323
3324 if (output_resource)
3325 output = wl_resource_get_user_data(output_resource);
3326 else
3327 output = NULL;
3328
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003329 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003330}
3331
3332static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003333xdg_surface_change_state(struct shell_surface *shsurf,
3334 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003335{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003336 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003337 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003338
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003339 switch (state) {
3340 case XDG_SURFACE_STATE_MAXIMIZED:
3341 shsurf->requested_state.maximized = value;
3342 if (value)
3343 set_maximized(shsurf, NULL);
3344 break;
3345 case XDG_SURFACE_STATE_FULLSCREEN:
3346 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003347
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003348 if (value)
3349 set_fullscreen(shsurf,
3350 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3351 0, shsurf->recommended_output);
3352 break;
3353 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003354}
3355
3356static void
3357xdg_surface_request_change_state(struct wl_client *client,
3358 struct wl_resource *resource,
3359 uint32_t state,
3360 uint32_t value,
3361 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003362{
3363 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003364
3365 /* The client can't know what the current state is, so we need
3366 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003367
3368 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3369 return;
3370
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003371 switch (state) {
3372 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003373 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003374 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003375 default:
3376 /* send error? ignore? send change state with value 0? */
3377 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003378 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003379
3380 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003381}
3382
3383static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003384xdg_surface_ack_change_state(struct wl_client *client,
3385 struct wl_resource *resource,
3386 uint32_t state,
3387 uint32_t value,
3388 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003389{
3390 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3391
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003392 if (shsurf->state_requested) {
3393 shsurf->next_state = shsurf->requested_state;
3394 shsurf->state_changed = true;
3395 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003396 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003397}
3398
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003399static void
3400xdg_surface_set_minimized(struct wl_client *client,
3401 struct wl_resource *resource)
3402{
3403 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3404
3405 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3406 return;
3407
3408 /* apply compositor's own minimization logic (hide) */
3409 set_minimized(shsurf->surface, 1);
3410}
3411
Rafael Antognollie2a34552013-12-03 15:35:45 -02003412static const struct xdg_surface_interface xdg_surface_implementation = {
3413 xdg_surface_destroy,
3414 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003415 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003416 xdg_surface_set_title,
3417 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003418 xdg_surface_move,
3419 xdg_surface_resize,
3420 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003421 xdg_surface_request_change_state,
3422 xdg_surface_ack_change_state,
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003423 xdg_surface_set_minimized
Rafael Antognollie2a34552013-12-03 15:35:45 -02003424};
3425
3426static void
3427xdg_send_configure(struct weston_surface *surface,
3428 uint32_t edges, int32_t width, int32_t height)
3429{
3430 struct shell_surface *shsurf = get_shell_surface(surface);
3431
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003432 assert(shsurf);
3433
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003434 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003435}
3436
3437static const struct weston_shell_client xdg_client = {
3438 xdg_send_configure
3439};
3440
3441static void
3442xdg_use_unstable_version(struct wl_client *client,
3443 struct wl_resource *resource,
3444 int32_t version)
3445{
3446 if (version > 1) {
3447 wl_resource_post_error(resource,
3448 1,
3449 "xdg-shell:: version not implemented yet.");
3450 return;
3451 }
3452}
3453
3454static struct shell_surface *
3455create_xdg_surface(void *shell, struct weston_surface *surface,
3456 const struct weston_shell_client *client)
3457{
3458 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003459
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003460 shsurf = create_common_surface(shell, surface, client);
3461 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003462
3463 return shsurf;
3464}
3465
3466static void
3467xdg_get_xdg_surface(struct wl_client *client,
3468 struct wl_resource *resource,
3469 uint32_t id,
3470 struct wl_resource *surface_resource)
3471{
3472 struct weston_surface *surface =
3473 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003474 struct shell_client *sc = wl_resource_get_user_data(resource);
3475 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003476 struct shell_surface *shsurf;
3477
3478 if (get_shell_surface(surface)) {
3479 wl_resource_post_error(surface_resource,
3480 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003481 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003482 return;
3483 }
3484
3485 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3486 if (!shsurf) {
3487 wl_resource_post_error(surface_resource,
3488 WL_DISPLAY_ERROR_INVALID_OBJECT,
3489 "surface->configure already set");
3490 return;
3491 }
3492
3493 shsurf->resource =
3494 wl_resource_create(client,
3495 &xdg_surface_interface, 1, id);
3496 wl_resource_set_implementation(shsurf->resource,
3497 &xdg_surface_implementation,
3498 shsurf, shell_destroy_shell_surface);
3499}
3500
3501static bool
3502shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3503{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003504 return shsurf->resource &&
3505 wl_resource_instance_of(shsurf->resource,
3506 &xdg_surface_interface,
3507 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003508}
3509
3510/* xdg-popup implementation */
3511
3512static void
3513xdg_popup_destroy(struct wl_client *client,
3514 struct wl_resource *resource)
3515{
3516 wl_resource_destroy(resource);
3517}
3518
Rafael Antognollie2a34552013-12-03 15:35:45 -02003519static const struct xdg_popup_interface xdg_popup_implementation = {
3520 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003521};
3522
3523static void
3524xdg_popup_send_configure(struct weston_surface *surface,
3525 uint32_t edges, int32_t width, int32_t height)
3526{
3527}
3528
3529static const struct weston_shell_client xdg_popup_client = {
3530 xdg_popup_send_configure
3531};
3532
3533static struct shell_surface *
3534create_xdg_popup(void *shell, struct weston_surface *surface,
3535 const struct weston_shell_client *client,
3536 struct weston_surface *parent,
3537 struct shell_seat *seat,
3538 uint32_t serial,
3539 int32_t x, int32_t y)
3540{
3541 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003542
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003543 shsurf = create_common_surface(shell, surface, client);
3544 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003545 shsurf->popup.shseat = seat;
3546 shsurf->popup.serial = serial;
3547 shsurf->popup.x = x;
3548 shsurf->popup.y = y;
3549 shell_surface_set_parent(shsurf, parent);
3550
3551 return shsurf;
3552}
3553
3554static void
3555xdg_get_xdg_popup(struct wl_client *client,
3556 struct wl_resource *resource,
3557 uint32_t id,
3558 struct wl_resource *surface_resource,
3559 struct wl_resource *parent_resource,
3560 struct wl_resource *seat_resource,
3561 uint32_t serial,
3562 int32_t x, int32_t y, uint32_t flags)
3563{
3564 struct weston_surface *surface =
3565 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003566 struct shell_client *sc = wl_resource_get_user_data(resource);
3567 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003568 struct shell_surface *shsurf;
3569 struct weston_surface *parent;
3570 struct shell_seat *seat;
3571
3572 if (get_shell_surface(surface)) {
3573 wl_resource_post_error(surface_resource,
3574 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003575 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003576 return;
3577 }
3578
3579 if (!parent_resource) {
3580 wl_resource_post_error(surface_resource,
3581 WL_DISPLAY_ERROR_INVALID_OBJECT,
3582 "xdg_shell::get_xdg_popup requires a parent shell surface");
3583 }
3584
3585 parent = wl_resource_get_user_data(parent_resource);
3586 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3587
3588 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3589 parent, seat, serial, x, y);
3590 if (!shsurf) {
3591 wl_resource_post_error(surface_resource,
3592 WL_DISPLAY_ERROR_INVALID_OBJECT,
3593 "surface->configure already set");
3594 return;
3595 }
3596
3597 shsurf->resource =
3598 wl_resource_create(client,
3599 &xdg_popup_interface, 1, id);
3600 wl_resource_set_implementation(shsurf->resource,
3601 &xdg_popup_implementation,
3602 shsurf, shell_destroy_shell_surface);
3603}
3604
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003605static void
3606xdg_pong(struct wl_client *client,
3607 struct wl_resource *resource, uint32_t serial)
3608{
3609 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003610
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003611 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003612}
3613
Rafael Antognollie2a34552013-12-03 15:35:45 -02003614static bool
3615shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3616{
3617 return wl_resource_instance_of(shsurf->resource,
3618 &xdg_popup_interface,
3619 &xdg_popup_implementation);
3620}
3621
3622static const struct xdg_shell_interface xdg_implementation = {
3623 xdg_use_unstable_version,
3624 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003625 xdg_get_xdg_popup,
3626 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003627};
3628
3629static int
3630xdg_shell_unversioned_dispatch(const void *implementation,
3631 void *_target, uint32_t opcode,
3632 const struct wl_message *message,
3633 union wl_argument *args)
3634{
3635 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003636 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003637
3638 if (opcode != 0) {
3639 wl_resource_post_error(resource,
3640 WL_DISPLAY_ERROR_INVALID_OBJECT,
3641 "must call use_unstable_version first");
3642 return 0;
3643 }
3644
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003645#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003646
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003647 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3648 "shell implementation doesn't match protocol version");
3649
Rafael Antognollie2a34552013-12-03 15:35:45 -02003650 if (args[0].i != XDG_SERVER_VERSION) {
3651 wl_resource_post_error(resource,
3652 WL_DISPLAY_ERROR_INVALID_OBJECT,
3653 "incompatible version, server is %d "
3654 "client wants %d",
3655 XDG_SERVER_VERSION, args[0].i);
3656 return 0;
3657 }
3658
3659 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003660 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003661
3662 return 1;
3663}
3664
3665/* end of xdg-shell implementation */
3666/***********************************/
3667
Kristian Høgsberg07937562011-04-12 17:25:42 -04003668static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003669shell_fade(struct desktop_shell *shell, enum fade_type type);
3670
3671static int
3672screensaver_timeout(void *data)
3673{
3674 struct desktop_shell *shell = data;
3675
3676 shell_fade(shell, FADE_OUT);
3677
3678 return 1;
3679}
3680
3681static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003682handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003683{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003684 struct desktop_shell *shell =
3685 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003686
Pekka Paalanen18027e52011-12-02 16:31:49 +02003687 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003688
3689 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003690 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003691}
3692
3693static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003694launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003695{
3696 if (shell->screensaver.binding)
3697 return;
3698
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003699 if (!shell->screensaver.path) {
3700 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003701 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003702 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003703
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003704 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003705 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003706 return;
3707 }
3708
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003709 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003710 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003711 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003712 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003713}
3714
3715static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003716terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003717{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003718 if (shell->screensaver.process.pid == 0)
3719 return;
3720
3721 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003722}
3723
3724static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003725configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003726{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003727 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003728
Jason Ekstranda7af7042013-10-12 22:38:11 -05003729 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3730 if (v->output == ev->output && v != ev) {
3731 weston_view_unmap(v);
3732 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003733 }
3734 }
3735
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003736 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003737
Jason Ekstranda7af7042013-10-12 22:38:11 -05003738 if (wl_list_empty(&ev->layer_link)) {
3739 wl_list_insert(&layer->view_list, &ev->layer_link);
3740 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003741 }
3742}
3743
3744static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003745background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003746{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003747 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003748 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003749
Jason Ekstranda7af7042013-10-12 22:38:11 -05003750 view = container_of(es->views.next, struct weston_view, surface_link);
3751
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003752 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003753}
3754
3755static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003756desktop_shell_set_background(struct wl_client *client,
3757 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003758 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003759 struct wl_resource *surface_resource)
3760{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003761 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003762 struct weston_surface *surface =
3763 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003764 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003765
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003766 if (surface->configure) {
3767 wl_resource_post_error(surface_resource,
3768 WL_DISPLAY_ERROR_INVALID_OBJECT,
3769 "surface role already assigned");
3770 return;
3771 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003772
Jason Ekstranda7af7042013-10-12 22:38:11 -05003773 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3774 weston_view_destroy(view);
3775 view = weston_view_create(surface);
3776
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003777 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003778 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003779 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003780 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003781 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003782 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003783 surface->output->width,
3784 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003785}
3786
3787static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003788panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003789{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003790 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003791 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003792
Jason Ekstranda7af7042013-10-12 22:38:11 -05003793 view = container_of(es->views.next, struct weston_view, surface_link);
3794
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003795 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003796}
3797
3798static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003799desktop_shell_set_panel(struct wl_client *client,
3800 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003801 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003802 struct wl_resource *surface_resource)
3803{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003804 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003805 struct weston_surface *surface =
3806 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003807 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003808
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003809 if (surface->configure) {
3810 wl_resource_post_error(surface_resource,
3811 WL_DISPLAY_ERROR_INVALID_OBJECT,
3812 "surface role already assigned");
3813 return;
3814 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003815
Jason Ekstranda7af7042013-10-12 22:38:11 -05003816 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3817 weston_view_destroy(view);
3818 view = weston_view_create(surface);
3819
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003820 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003821 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003822 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003823 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003824 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003825 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003826 surface->output->width,
3827 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003828}
3829
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003830static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003831lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003832{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003833 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003834 struct weston_view *view;
3835
3836 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003837
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003838 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003839 return;
3840
Jason Ekstranda7af7042013-10-12 22:38:11 -05003841 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003842
3843 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003844 wl_list_insert(&shell->lock_layer.view_list,
3845 &view->layer_link);
3846 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003847 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003848 }
3849}
3850
3851static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003852handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003853{
Tiago Vignattibe143262012-04-16 17:31:41 +03003854 struct desktop_shell *shell =
3855 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003856
Martin Minarik6d118362012-06-07 18:01:59 +02003857 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003858 shell->lock_surface = NULL;
3859}
3860
3861static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003862desktop_shell_set_lock_surface(struct wl_client *client,
3863 struct wl_resource *resource,
3864 struct wl_resource *surface_resource)
3865{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003866 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003867 struct weston_surface *surface =
3868 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003869
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003870 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003871
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003872 if (!shell->locked)
3873 return;
3874
Pekka Paalanen98262232011-12-01 10:42:22 +02003875 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003876
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003877 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003878 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003879 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003880
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003881 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003882 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003883 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003884}
3885
3886static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003887resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003888{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003889 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003890
Pekka Paalanen77346a62011-11-30 16:26:35 +02003891 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003892
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003893 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003894 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003895 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003896 &shell->input_panel_layer.link);
3897 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003898 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003899 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003900 wl_list_insert(&shell->compositor->cursor_layer.link,
3901 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003902 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003903 wl_list_insert(&shell->fullscreen_layer.link,
3904 &shell->panel_layer.link);
3905 wl_list_insert(&shell->panel_layer.link,
3906 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003907
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003908 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003909
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003910 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003911 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003912 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003913}
3914
3915static void
3916desktop_shell_unlock(struct wl_client *client,
3917 struct wl_resource *resource)
3918{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003919 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003920
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003921 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003922
3923 if (shell->locked)
3924 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003925}
3926
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003927static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003928desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003929 struct wl_resource *resource,
3930 struct wl_resource *surface_resource)
3931{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003932 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003933
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003934 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003935 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003936}
3937
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003938static void
3939desktop_shell_desktop_ready(struct wl_client *client,
3940 struct wl_resource *resource)
3941{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003942 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003943
3944 shell_fade_startup(shell);
3945}
3946
Kristian Høgsberg75840622011-09-06 13:48:16 -04003947static const struct desktop_shell_interface desktop_shell_implementation = {
3948 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003949 desktop_shell_set_panel,
3950 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003951 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003952 desktop_shell_set_grab_surface,
3953 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003954};
3955
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003956static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003957get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003958{
3959 struct shell_surface *shsurf;
3960
3961 shsurf = get_shell_surface(surface);
3962 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003963 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003964 return shsurf->type;
3965}
3966
Kristian Høgsberg75840622011-09-06 13:48:16 -04003967static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003968move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003969{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003970 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003971 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003972 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003973
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003974 if (seat->pointer->focus == NULL)
3975 return;
3976
3977 focus = seat->pointer->focus->surface;
3978
Pekka Paalanen01388e22013-04-25 13:57:44 +03003979 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003980 if (surface == NULL)
3981 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003982
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003983 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003984 if (shsurf == NULL || shsurf->state.fullscreen ||
3985 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003986 return;
3987
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003988 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003989}
3990
3991static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003992maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3993{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003994 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003995 struct weston_surface *surface;
3996 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003997 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003998
3999 surface = weston_surface_get_main_surface(focus);
4000 if (surface == NULL)
4001 return;
4002
4003 shsurf = get_shell_surface(surface);
4004 if (shsurf == NULL)
4005 return;
4006
4007 if (!shell_surface_is_xdg_surface(shsurf))
4008 return;
4009
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004010 serial = wl_display_next_serial(seat->compositor->wl_display);
4011 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4012 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004013}
4014
4015static void
4016fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4017{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004018 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004019 struct weston_surface *surface;
4020 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004021 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004022
4023 surface = weston_surface_get_main_surface(focus);
4024 if (surface == NULL)
4025 return;
4026
4027 shsurf = get_shell_surface(surface);
4028 if (shsurf == NULL)
4029 return;
4030
4031 if (!shell_surface_is_xdg_surface(shsurf))
4032 return;
4033
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004034 serial = wl_display_next_serial(seat->compositor->wl_display);
4035 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4036 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004037}
4038
4039static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004040touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4041{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004042 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004043 struct weston_surface *surface;
4044 struct shell_surface *shsurf;
4045
4046 surface = weston_surface_get_main_surface(focus);
4047 if (surface == NULL)
4048 return;
4049
4050 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004051 if (shsurf == NULL || shsurf->state.fullscreen ||
4052 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004053 return;
4054
4055 surface_touch_move(shsurf, (struct weston_seat *) seat);
4056}
4057
4058static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004059resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004060{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004061 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004062 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004063 uint32_t edges = 0;
4064 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004065 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004066
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004067 if (seat->pointer->focus == NULL)
4068 return;
4069
4070 focus = seat->pointer->focus->surface;
4071
Pekka Paalanen01388e22013-04-25 13:57:44 +03004072 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004073 if (surface == NULL)
4074 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004075
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004076 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004077 if (shsurf == NULL || shsurf->state.fullscreen ||
4078 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004079 return;
4080
Jason Ekstranda7af7042013-10-12 22:38:11 -05004081 weston_view_from_global(shsurf->view,
4082 wl_fixed_to_int(seat->pointer->grab_x),
4083 wl_fixed_to_int(seat->pointer->grab_y),
4084 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004085
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004086 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004087 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004088 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004089 edges |= 0;
4090 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004091 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004092
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004093 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004094 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004095 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004096 edges |= 0;
4097 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004098 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004099
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004100 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004101}
4102
4103static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004104surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004105 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004106{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004107 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004108 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004109 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004110 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004111
Pekka Paalanen01388e22013-04-25 13:57:44 +03004112 /* XXX: broken for windows containing sub-surfaces */
4113 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004114 if (surface == NULL)
4115 return;
4116
4117 shsurf = get_shell_surface(surface);
4118 if (!shsurf)
4119 return;
4120
Jason Ekstranda7af7042013-10-12 22:38:11 -05004121 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004122
Jason Ekstranda7af7042013-10-12 22:38:11 -05004123 if (shsurf->view->alpha > 1.0)
4124 shsurf->view->alpha = 1.0;
4125 if (shsurf->view->alpha < step)
4126 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004127
Jason Ekstranda7af7042013-10-12 22:38:11 -05004128 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004129 weston_surface_damage(surface);
4130}
4131
4132static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004133do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004134 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004135{
Daniel Stone37816df2012-05-16 18:45:18 +01004136 struct weston_seat *ws = (struct weston_seat *) seat;
4137 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004138 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004139 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004140
4141 wl_list_for_each(output, &compositor->output_list, link) {
4142 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004143 wl_fixed_to_double(seat->pointer->x),
4144 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004145 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004146 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004147 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004148 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004149 increment = -output->zoom.increment;
4150 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004151 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004152 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004153 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004154 else
4155 increment = 0;
4156
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004157 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004158
Scott Moreaue6603982012-06-11 13:07:51 -06004159 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004160 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004161 else if (output->zoom.level > output->zoom.max_level)
4162 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004163 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004164 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004165 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004166
Scott Moreaue6603982012-06-11 13:07:51 -06004167 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004168
Jason Ekstranda7af7042013-10-12 22:38:11 -05004169 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004170 }
4171 }
4172}
4173
Scott Moreauccbf29d2012-02-22 14:21:41 -07004174static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004175zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004176 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004177{
4178 do_zoom(seat, time, 0, axis, value);
4179}
4180
4181static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004182zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004183 void *data)
4184{
4185 do_zoom(seat, time, key, 0, 0);
4186}
4187
4188static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004189terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004190 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004191{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004192 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004193
Daniel Stone325fc2d2012-05-30 16:31:58 +01004194 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004195}
4196
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004197static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004198rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4199 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004200{
4201 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004202 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004203 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004204 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004205 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004206
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004207 weston_pointer_move(pointer, x, y);
4208
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004209 if (!shsurf)
4210 return;
4211
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004212 cx = 0.5f * shsurf->surface->width;
4213 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004214
Daniel Stone37816df2012-05-16 18:45:18 +01004215 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4216 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004217 r = sqrtf(dx * dx + dy * dy);
4218
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004219 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004220 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004221
4222 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004223 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004224 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004225
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004226 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004227 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004228
4229 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004230 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004231 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004232 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004233 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004234
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004235 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004236 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004237 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004238 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004239 wl_list_init(&shsurf->rotation.transform.link);
4240 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004241 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004242 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004243
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004244 /* We need to adjust the position of the surface
4245 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004246 cposx = shsurf->view->geometry.x + cx;
4247 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004248 dposx = rotate->center.x - cposx;
4249 dposy = rotate->center.y - cposy;
4250 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004251 weston_view_set_position(shsurf->view,
4252 shsurf->view->geometry.x + dposx,
4253 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004254 }
4255
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004256 /* Repaint implies weston_surface_update_transform(), which
4257 * lazily applies the damage due to rotation update.
4258 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004259 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004260}
4261
4262static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004263rotate_grab_button(struct weston_pointer_grab *grab,
4264 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004265{
4266 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004267 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004268 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004269 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004270 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004271
Daniel Stone4dbadb12012-05-30 16:31:51 +01004272 if (pointer->button_count == 0 &&
4273 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004274 if (shsurf)
4275 weston_matrix_multiply(&shsurf->rotation.rotation,
4276 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004277 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004278 free(rotate);
4279 }
4280}
4281
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004282static void
4283rotate_grab_cancel(struct weston_pointer_grab *grab)
4284{
4285 struct rotate_grab *rotate =
4286 container_of(grab, struct rotate_grab, base.grab);
4287
4288 shell_grab_end(&rotate->base);
4289 free(rotate);
4290}
4291
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004292static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004293 noop_grab_focus,
4294 rotate_grab_motion,
4295 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004296 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004297};
4298
4299static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004300surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004301{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004302 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004303 float dx, dy;
4304 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004305
Pekka Paalanen460099f2012-01-20 16:48:25 +02004306 rotate = malloc(sizeof *rotate);
4307 if (!rotate)
4308 return;
4309
Jason Ekstranda7af7042013-10-12 22:38:11 -05004310 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004311 surface->surface->width * 0.5f,
4312 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004313 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004314
Daniel Stone37816df2012-05-16 18:45:18 +01004315 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4316 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004317 r = sqrtf(dx * dx + dy * dy);
4318 if (r > 20.0f) {
4319 struct weston_matrix inverse;
4320
4321 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004322 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004323 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004324
4325 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004326 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004327 } else {
4328 weston_matrix_init(&surface->rotation.rotation);
4329 weston_matrix_init(&rotate->rotation);
4330 }
4331
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004332 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4333 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004334}
4335
4336static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004337rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004338 void *data)
4339{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004340 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004341 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004342 struct shell_surface *surface;
4343
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004344 if (seat->pointer->focus == NULL)
4345 return;
4346
4347 focus = seat->pointer->focus->surface;
4348
Pekka Paalanen01388e22013-04-25 13:57:44 +03004349 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004350 if (base_surface == NULL)
4351 return;
4352
4353 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004354 if (surface == NULL || surface->state.fullscreen ||
4355 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004356 return;
4357
4358 surface_rotate(surface, seat);
4359}
4360
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004361/* Move all fullscreen layers down to the current workspace in a non-reversible
4362 * manner. This should be used when implementing shell-wide overlays, such as
4363 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004364void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004365lower_fullscreen_layer(struct desktop_shell *shell)
4366{
4367 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004368 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004369
4370 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004371 wl_list_for_each_reverse_safe(view, prev,
4372 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004373 layer_link) {
4374 wl_list_remove(&view->layer_link);
4375 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4376 weston_view_damage_below(view);
4377 weston_surface_damage(view->surface);
4378 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004379}
4380
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004381void
Tiago Vignattibe143262012-04-16 17:31:41 +03004382activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004383 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004384{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004385 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004386 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004387 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004388 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004389 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004390
Pekka Paalanen01388e22013-04-25 13:57:44 +03004391 main_surface = weston_surface_get_main_surface(es);
4392
Daniel Stone37816df2012-05-16 18:45:18 +01004393 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004394
Jonas Ådahl8538b222012-08-29 22:13:03 +02004395 state = ensure_focus_state(shell, seat);
4396 if (state == NULL)
4397 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004398
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004399 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004400 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004401
Rafael Antognolli03b16592013-12-03 15:35:42 -02004402 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004403 assert(shsurf);
4404
Rafael Antognolli03b16592013-12-03 15:35:42 -02004405 if (shsurf->state.fullscreen)
4406 shell_configure_fullscreen(shsurf);
4407 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004408 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004409
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004410 /* Update the surface’s layer. This brings it to the top of the stacking
4411 * order as appropriate. */
4412 shell_surface_update_layer(shsurf);
4413
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004414 if (shell->focus_animation_type != ANIMATION_NONE) {
4415 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004416 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004417 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004418}
4419
Alex Wu21858432012-04-01 20:13:08 +08004420/* no-op func for checking black surface */
4421static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004422black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004423{
4424}
4425
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004426static bool
Alex Wu21858432012-04-01 20:13:08 +08004427is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4428{
4429 if (es->configure == black_surface_configure) {
4430 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004431 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004432 return true;
4433 }
4434 return false;
4435}
4436
Kristian Høgsberg75840622011-09-06 13:48:16 -04004437static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004438activate_binding(struct weston_seat *seat,
4439 struct desktop_shell *shell,
4440 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004441{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004442 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004443
Alex Wu9c35e6b2012-03-05 14:13:13 +08004444 if (!focus)
4445 return;
4446
Pekka Paalanen01388e22013-04-25 13:57:44 +03004447 if (is_black_surface(focus, &main_surface))
4448 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004449
Pekka Paalanen01388e22013-04-25 13:57:44 +03004450 main_surface = weston_surface_get_main_surface(focus);
4451 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004452 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004453
Neil Robertsa28c6932013-10-03 16:43:04 +01004454 activate(shell, focus, seat);
4455}
4456
4457static void
4458click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4459 void *data)
4460{
4461 if (seat->pointer->grab != &seat->pointer->default_grab)
4462 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004463 if (seat->pointer->focus == NULL)
4464 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004465
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004466 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004467}
4468
4469static void
4470touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4471{
4472 if (seat->touch->grab != &seat->touch->default_grab)
4473 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004474 if (seat->touch->focus == NULL)
4475 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004476
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004477 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004478}
4479
4480static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004481lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004482{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004483 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004484
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004485 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004486 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004487 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004488 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004489
4490 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004491
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004492 /* Hide all surfaces by removing the fullscreen, panel and
4493 * toplevel layers. This way nothing else can show or receive
4494 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004495
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004496 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004497 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004498 if (shell->showing_input_panels)
4499 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004500 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004501 wl_list_insert(&shell->compositor->cursor_layer.link,
4502 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004503
Pekka Paalanen77346a62011-11-30 16:26:35 +02004504 launch_screensaver(shell);
4505
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004506 /* TODO: disable bindings that should not work while locked. */
4507
4508 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004509}
4510
4511static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004512unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004513{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004514 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004515 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004516 return;
4517 }
4518
4519 /* If desktop-shell client has gone away, unlock immediately. */
4520 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004521 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004522 return;
4523 }
4524
4525 if (shell->prepare_event_sent)
4526 return;
4527
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004528 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004529 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004530}
4531
4532static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004533shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004534{
4535 struct desktop_shell *shell = data;
4536
4537 shell->fade.animation = NULL;
4538
4539 switch (shell->fade.type) {
4540 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004541 weston_surface_destroy(shell->fade.view->surface);
4542 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004543 break;
4544 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004545 lock(shell);
4546 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004547 default:
4548 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004549 }
4550}
4551
Jason Ekstranda7af7042013-10-12 22:38:11 -05004552static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004553shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004554{
4555 struct weston_compositor *compositor = shell->compositor;
4556 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004557 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004558
4559 surface = weston_surface_create(compositor);
4560 if (!surface)
4561 return NULL;
4562
Jason Ekstranda7af7042013-10-12 22:38:11 -05004563 view = weston_view_create(surface);
4564 if (!view) {
4565 weston_surface_destroy(surface);
4566 return NULL;
4567 }
4568
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004569 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004570 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004571 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004572 wl_list_insert(&compositor->fade_layer.view_list,
4573 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004574 pixman_region32_init(&surface->input);
4575
Jason Ekstranda7af7042013-10-12 22:38:11 -05004576 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004577}
4578
4579static void
4580shell_fade(struct desktop_shell *shell, enum fade_type type)
4581{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004582 float tint;
4583
4584 switch (type) {
4585 case FADE_IN:
4586 tint = 0.0;
4587 break;
4588 case FADE_OUT:
4589 tint = 1.0;
4590 break;
4591 default:
4592 weston_log("shell: invalid fade type\n");
4593 return;
4594 }
4595
4596 shell->fade.type = type;
4597
Jason Ekstranda7af7042013-10-12 22:38:11 -05004598 if (shell->fade.view == NULL) {
4599 shell->fade.view = shell_fade_create_surface(shell);
4600 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004601 return;
4602
Jason Ekstranda7af7042013-10-12 22:38:11 -05004603 shell->fade.view->alpha = 1.0 - tint;
4604 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004605 }
4606
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004607 if (shell->fade.view->output == NULL) {
4608 /* If the black view gets a NULL output, we lost the
4609 * last output and we'll just cancel the fade. This
4610 * happens when you close the last window under the
4611 * X11 or Wayland backends. */
4612 shell->locked = false;
4613 weston_surface_destroy(shell->fade.view->surface);
4614 shell->fade.view = NULL;
4615 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004616 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004617 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004618 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004619 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004620 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004621 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004622 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004623}
4624
4625static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004626do_shell_fade_startup(void *data)
4627{
4628 struct desktop_shell *shell = data;
4629
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004630 if (shell->startup_animation_type == ANIMATION_FADE)
4631 shell_fade(shell, FADE_IN);
4632 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004633 weston_surface_destroy(shell->fade.view->surface);
4634 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004635 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004636}
4637
4638static void
4639shell_fade_startup(struct desktop_shell *shell)
4640{
4641 struct wl_event_loop *loop;
4642
4643 if (!shell->fade.startup_timer)
4644 return;
4645
4646 wl_event_source_remove(shell->fade.startup_timer);
4647 shell->fade.startup_timer = NULL;
4648
4649 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4650 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4651}
4652
4653static int
4654fade_startup_timeout(void *data)
4655{
4656 struct desktop_shell *shell = data;
4657
4658 shell_fade_startup(shell);
4659 return 0;
4660}
4661
4662static void
4663shell_fade_init(struct desktop_shell *shell)
4664{
4665 /* Make compositor output all black, and wait for the desktop-shell
4666 * client to signal it is ready, then fade in. The timer triggers a
4667 * fade-in, in case the desktop-shell client takes too long.
4668 */
4669
4670 struct wl_event_loop *loop;
4671
Jason Ekstranda7af7042013-10-12 22:38:11 -05004672 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004673 weston_log("%s: warning: fade surface already exists\n",
4674 __func__);
4675 return;
4676 }
4677
Jason Ekstranda7af7042013-10-12 22:38:11 -05004678 shell->fade.view = shell_fade_create_surface(shell);
4679 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004680 return;
4681
Jason Ekstranda7af7042013-10-12 22:38:11 -05004682 weston_view_update_transform(shell->fade.view);
4683 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004684
4685 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4686 shell->fade.startup_timer =
4687 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4688 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4689}
4690
4691static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004692idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004693{
4694 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004695 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004696 struct weston_seat *seat;
4697
4698 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4699 if (seat->pointer)
4700 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004701
4702 shell_fade(shell, FADE_OUT);
4703 /* lock() is called from shell_fade_done() */
4704}
4705
4706static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004707wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004708{
4709 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004710 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004711
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004712 unlock(shell);
4713}
4714
4715static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004716center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004717{
Giulio Camuffob8366642013-04-25 13:57:46 +03004718 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004719 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004720
Jason Ekstranda7af7042013-10-12 22:38:11 -05004721 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004722
4723 x = output->x + (output->width - width) / 2 - surf_x / 2;
4724 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004725
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004726 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004727}
4728
4729static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004730weston_view_set_initial_position(struct weston_view *view,
4731 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004732{
4733 struct weston_compositor *compositor = shell->compositor;
4734 int ix = 0, iy = 0;
4735 int range_x, range_y;
4736 int dx, dy, x, y, panel_height;
4737 struct weston_output *output, *target_output = NULL;
4738 struct weston_seat *seat;
4739
4740 /* As a heuristic place the new window on the same output as the
4741 * pointer. Falling back to the output containing 0, 0.
4742 *
4743 * TODO: Do something clever for touch too?
4744 */
4745 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004746 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004747 ix = wl_fixed_to_int(seat->pointer->x);
4748 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004749 break;
4750 }
4751 }
4752
4753 wl_list_for_each(output, &compositor->output_list, link) {
4754 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4755 target_output = output;
4756 break;
4757 }
4758 }
4759
4760 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004761 weston_view_set_position(view, 10 + random() % 400,
4762 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004763 return;
4764 }
4765
4766 /* Valid range within output where the surface will still be onscreen.
4767 * If this is negative it means that the surface is bigger than
4768 * output.
4769 */
4770 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004771 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004772 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004773 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004774
Rob Bradford4cb88c72012-08-13 15:18:44 +01004775 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004776 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004777 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004778 dx = 0;
4779
4780 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004781 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004782 else
4783 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004784
4785 x = target_output->x + dx;
4786 y = target_output->y + dy;
4787
Jason Ekstranda7af7042013-10-12 22:38:11 -05004788 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004789}
4790
4791static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004792map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004793 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004794{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004795 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004796 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004797 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004798 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004799
Pekka Paalanen77346a62011-11-30 16:26:35 +02004800 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004801 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004802 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004803 if (shsurf->state.fullscreen) {
4804 center_on_output(shsurf->view, shsurf->fullscreen_output);
4805 shell_map_fullscreen(shsurf);
4806 } else if (shsurf->state.maximized) {
4807 /* use surface configure to set the geometry */
4808 panel_height = get_output_panel_height(shell, shsurf->output);
4809 surface_subsurfaces_boundingbox(shsurf->surface,
4810 &surf_x, &surf_y, NULL, NULL);
4811 weston_view_set_position(shsurf->view,
4812 shsurf->output->x - surf_x,
4813 shsurf->output->y +
4814 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004815 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004816 weston_view_set_initial_position(shsurf->view, shell);
4817 }
Juan Zhao96879df2012-02-07 08:45:41 +08004818 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004819 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004820 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004821 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004822 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004823 weston_view_set_position(shsurf->view,
4824 shsurf->view->geometry.x + sx,
4825 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004826 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004827 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004828 default:
4829 ;
4830 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004831
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004832 /* Surface stacking order, see also activate(). */
4833 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004834
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835 if (shsurf->type != SHELL_SURFACE_NONE) {
4836 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004837 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004838 shsurf->surface->output = shsurf->output;
4839 shsurf->view->output = shsurf->output;
4840 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004841 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004842
Jason Ekstranda7af7042013-10-12 22:38:11 -05004843 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004844 /* XXX: xwayland's using the same fields for transient type */
4845 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004846 if (shsurf->transient.flags ==
4847 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4848 break;
4849 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004850 if (shsurf->state.relative &&
4851 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4852 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004853 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004854 break;
4855 wl_list_for_each(seat, &compositor->seat_list, link)
4856 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004857 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004858 case SHELL_SURFACE_POPUP:
4859 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004860 default:
4861 break;
4862 }
4863
Rafael Antognolli03b16592013-12-03 15:35:42 -02004864 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4865 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004866 {
4867 switch (shell->win_animation_type) {
4868 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004869 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004870 break;
4871 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004872 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004873 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004874 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004875 default:
4876 break;
4877 }
4878 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004879}
4880
4881static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004882configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004883 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004884{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004885 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004886 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004887 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004888
Pekka Paalanen77346a62011-11-30 16:26:35 +02004889 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004890
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004891 assert(shsurf);
4892
Rafael Antognolli03b16592013-12-03 15:35:42 -02004893 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004894 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004895 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004896 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004897 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4898 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004899 mx = shsurf->output->x - surf_x;
4900 my = shsurf->output->y +
4901 get_output_panel_height(shell,shsurf->output) - surf_y;
4902 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004903 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004904 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004905 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004906
Alex Wu4539b082012-03-01 12:57:46 +08004907 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004908 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004909 wl_list_for_each(view, &surface->views, surface_link)
4910 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004911
Rafael Antognolli03b16592013-12-03 15:35:42 -02004912 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004913 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004914 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004915}
4916
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004917static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004918shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004919{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004920 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004921 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004922 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004923
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004924 assert(shsurf);
4925
4926 shell = shsurf->shell;
4927
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004928 if (!weston_surface_is_mapped(es) &&
4929 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004930 remove_popup_grab(shsurf);
4931 }
4932
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004933 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004934 return;
4935
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004936 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004937 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004938 type_changed = 1;
4939 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004940
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004941 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004942 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004943 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004944 shsurf->last_width != es->width ||
4945 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004946 float from_x, from_y;
4947 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004948
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08004949 if (shsurf->resize_edges) {
4950 sx = 0;
4951 sy = 0;
4952 }
4953
4954 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
4955 sx = shsurf->last_width - es->width;
4956 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
4957 sy = shsurf->last_height - es->height;
4958
4959 shsurf->last_width = es->width;
4960 shsurf->last_height = es->height;
4961
Jason Ekstranda7af7042013-10-12 22:38:11 -05004962 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4963 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004964 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004965 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004966 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004967 }
4968}
4969
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004970static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004971
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004972static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004973desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004974{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004975 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004976 struct desktop_shell *shell =
4977 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004978
4979 shell->child.process.pid = 0;
4980 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004981
4982 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4983 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004984 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004985 shell->child.deathstamp = time;
4986 shell->child.deathcount = 0;
4987 }
4988
4989 shell->child.deathcount++;
4990 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004991 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004992 return;
4993 }
4994
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004995 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004996 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004997 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004998}
4999
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005000static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005001desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5002{
5003 struct desktop_shell *shell;
5004
5005 shell = container_of(listener, struct desktop_shell,
5006 child.client_destroy_listener);
5007
5008 shell->child.client = NULL;
5009}
5010
5011static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005012launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005013{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005014 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005015
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005016 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005017 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005018 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005019 desktop_shell_sigchld);
5020
5021 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005022 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005023
5024 shell->child.client_destroy_listener.notify =
5025 desktop_shell_client_destroy;
5026 wl_client_add_destroy_listener(shell->child.client,
5027 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005028}
5029
5030static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005031handle_shell_client_destroy(struct wl_listener *listener, void *data)
5032{
5033 struct shell_client *sc =
5034 container_of(listener, struct shell_client, destroy_listener);
5035
5036 if (sc->ping_timer)
5037 wl_event_source_remove(sc->ping_timer);
5038 free(sc);
5039}
5040
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005041static struct shell_client *
5042shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5043 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005044{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005045 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005046
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005047 sc = zalloc(sizeof *sc);
5048 if (sc == NULL) {
5049 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005050 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005051 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005052
5053 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005054 if (sc->resource == NULL) {
5055 free(sc);
5056 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005057 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005058 }
5059
5060 sc->client = client;
5061 sc->shell = shell;
5062 sc->destroy_listener.notify = handle_shell_client_destroy;
5063 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5064
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005065 return sc;
5066}
5067
5068static void
5069bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5070{
5071 struct desktop_shell *shell = data;
5072 struct shell_client *sc;
5073
5074 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5075 if (sc)
5076 wl_resource_set_implementation(sc->resource,
5077 &shell_implementation,
5078 shell, NULL);
5079}
5080
5081static void
5082bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5083{
5084 struct desktop_shell *shell = data;
5085 struct shell_client *sc;
5086
5087 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5088 if (sc)
5089 wl_resource_set_dispatcher(sc->resource,
5090 xdg_shell_unversioned_dispatch,
5091 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005092}
5093
5094static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005095unbind_desktop_shell(struct wl_resource *resource)
5096{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005097 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005098
5099 if (shell->locked)
5100 resume_desktop(shell);
5101
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005102 shell->child.desktop_shell = NULL;
5103 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005104}
5105
5106static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005107bind_desktop_shell(struct wl_client *client,
5108 void *data, uint32_t version, uint32_t id)
5109{
Tiago Vignattibe143262012-04-16 17:31:41 +03005110 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005111 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005112
Jason Ekstranda85118c2013-06-27 20:17:02 -05005113 resource = wl_resource_create(client, &desktop_shell_interface,
5114 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005115
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005116 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005117 wl_resource_set_implementation(resource,
5118 &desktop_shell_implementation,
5119 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005120 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005121
5122 if (version < 2)
5123 shell_fade_startup(shell);
5124
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005125 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005126 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005127
5128 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5129 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005130 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005131}
5132
Pekka Paalanen6e168112011-11-24 11:34:05 +02005133static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005134screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005135{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005136 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005137 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005138
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005139 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005140 return;
5141
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005142 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005143 if (!shell->locked)
5144 return;
5145
Jason Ekstranda7af7042013-10-12 22:38:11 -05005146 view = container_of(surface->views.next, struct weston_view, surface_link);
5147 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005148
Jason Ekstranda7af7042013-10-12 22:38:11 -05005149 if (wl_list_empty(&view->layer_link)) {
5150 wl_list_insert(shell->lock_layer.view_list.prev,
5151 &view->layer_link);
5152 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005153 wl_event_source_timer_update(shell->screensaver.timer,
5154 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005155 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005156 }
5157}
5158
5159static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005160screensaver_set_surface(struct wl_client *client,
5161 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005162 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005163 struct wl_resource *output_resource)
5164{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005165 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005166 struct weston_surface *surface =
5167 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005168 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005169 struct weston_view *view, *next;
5170
5171 /* Make sure we only have one view */
5172 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5173 weston_view_destroy(view);
5174 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005175
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005176 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005177 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005178 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005179}
5180
5181static const struct screensaver_interface screensaver_implementation = {
5182 screensaver_set_surface
5183};
5184
5185static void
5186unbind_screensaver(struct wl_resource *resource)
5187{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005188 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005189
Pekka Paalanen77346a62011-11-30 16:26:35 +02005190 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005191}
5192
5193static void
5194bind_screensaver(struct wl_client *client,
5195 void *data, uint32_t version, uint32_t id)
5196{
Tiago Vignattibe143262012-04-16 17:31:41 +03005197 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005198 struct wl_resource *resource;
5199
Jason Ekstranda85118c2013-06-27 20:17:02 -05005200 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005201
Pekka Paalanen77346a62011-11-30 16:26:35 +02005202 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005203 wl_resource_set_implementation(resource,
5204 &screensaver_implementation,
5205 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005206 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005207 return;
5208 }
5209
5210 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5211 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005212 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005213}
5214
Kristian Høgsberg07045392012-02-19 18:52:44 -05005215struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005216 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005217 struct weston_surface *current;
5218 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005219 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005220 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005221};
5222
5223static void
5224switcher_next(struct switcher *switcher)
5225{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005226 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005227 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005228 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005229 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005230
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005231 /* temporary re-display minimized surfaces */
5232 struct weston_view *tmp;
5233 struct weston_view **minimized;
5234 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5235 wl_list_remove(&view->layer_link);
5236 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5237 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5238 *minimized = view;
5239 }
5240
Jason Ekstranda7af7042013-10-12 22:38:11 -05005241 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005242 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005243 if (shsurf &&
5244 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5245 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005246 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005247 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005248 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005249 next = view->surface;
5250 prev = view->surface;
5251 view->alpha = 0.25;
5252 weston_view_geometry_dirty(view);
5253 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005254 }
Alex Wu1659daa2012-04-01 20:13:09 +08005255
Jason Ekstranda7af7042013-10-12 22:38:11 -05005256 if (is_black_surface(view->surface, NULL)) {
5257 view->alpha = 0.25;
5258 weston_view_geometry_dirty(view);
5259 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005260 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005261 }
5262
5263 if (next == NULL)
5264 next = first;
5265
Alex Wu07b26062012-03-12 16:06:01 +08005266 if (next == NULL)
5267 return;
5268
Kristian Høgsberg07045392012-02-19 18:52:44 -05005269 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005270 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005271
5272 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005273 wl_list_for_each(view, &next->views, surface_link)
5274 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005275
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005276 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005277 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005278 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005279}
5280
5281static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005282switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005283{
5284 struct switcher *switcher =
5285 container_of(listener, struct switcher, listener);
5286
5287 switcher_next(switcher);
5288}
5289
5290static void
Daniel Stone351eb612012-05-31 15:27:47 -04005291switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005292{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005293 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005294 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005295 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005296
Jason Ekstranda7af7042013-10-12 22:38:11 -05005297 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005298 if (is_focus_view(view))
5299 continue;
5300
Jason Ekstranda7af7042013-10-12 22:38:11 -05005301 view->alpha = 1.0;
5302 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005303 }
5304
Alex Wu07b26062012-03-12 16:06:01 +08005305 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005306 activate(switcher->shell, switcher->current,
5307 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005308 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005309 weston_keyboard_end_grab(keyboard);
5310 if (keyboard->input_method_resource)
5311 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005312
5313 /* re-hide surfaces that were temporary shown during the switch */
5314 struct weston_view **minimized;
5315 wl_array_for_each(minimized, &switcher->minimized_array) {
5316 /* with the exception of the current selected */
5317 if ((*minimized)->surface != switcher->current) {
5318 wl_list_remove(&(*minimized)->layer_link);
5319 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5320 weston_view_damage_below(*minimized);
5321 }
5322 }
5323 wl_array_release(&switcher->minimized_array);
5324
Kristian Høgsberg07045392012-02-19 18:52:44 -05005325 free(switcher);
5326}
5327
5328static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005329switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005330 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005331{
5332 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005333 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005334
Daniel Stonec9785ea2012-05-30 16:31:52 +01005335 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005336 switcher_next(switcher);
5337}
5338
5339static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005340switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005341 uint32_t mods_depressed, uint32_t mods_latched,
5342 uint32_t mods_locked, uint32_t group)
5343{
5344 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005345 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005346
Daniel Stone351eb612012-05-31 15:27:47 -04005347 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5348 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005349}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005350
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005351static void
5352switcher_cancel(struct weston_keyboard_grab *grab)
5353{
5354 struct switcher *switcher = container_of(grab, struct switcher, grab);
5355
5356 switcher_destroy(switcher);
5357}
5358
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005359static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005360 switcher_key,
5361 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005362 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005363};
5364
5365static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005366switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005367 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005368{
Tiago Vignattibe143262012-04-16 17:31:41 +03005369 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005370 struct switcher *switcher;
5371
5372 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005373 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005374 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005375 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005376 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005377 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005378
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005379 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005380 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005381 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005382 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5383 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005384 switcher_next(switcher);
5385}
5386
Pekka Paalanen3c647232011-12-22 13:43:43 +02005387static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005388backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005389 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005390{
5391 struct weston_compositor *compositor = data;
5392 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005393 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005394
5395 /* TODO: we're limiting to simple use cases, where we assume just
5396 * control on the primary display. We'd have to extend later if we
5397 * ever get support for setting backlights on random desktop LCD
5398 * panels though */
5399 output = get_default_output(compositor);
5400 if (!output)
5401 return;
5402
5403 if (!output->set_backlight)
5404 return;
5405
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005406 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5407 backlight_new = output->backlight_current - 25;
5408 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5409 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005410
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005411 if (backlight_new < 5)
5412 backlight_new = 5;
5413 if (backlight_new > 255)
5414 backlight_new = 255;
5415
5416 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005417 output->set_backlight(output, output->backlight_current);
5418}
5419
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005420struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005421 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005422 struct weston_seat *seat;
5423 uint32_t key[2];
5424 int key_released[2];
5425};
5426
5427static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005428debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005429 uint32_t key, uint32_t state)
5430{
5431 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005432 struct weston_compositor *ec = db->seat->compositor;
5433 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005434 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005435 uint32_t serial;
5436 int send = 0, terminate = 0;
5437 int check_binding = 1;
5438 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005439 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005440
5441 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5442 /* Do not run bindings on key releases */
5443 check_binding = 0;
5444
5445 for (i = 0; i < 2; i++)
5446 if (key == db->key[i])
5447 db->key_released[i] = 1;
5448
5449 if (db->key_released[0] && db->key_released[1]) {
5450 /* All key releases been swalled so end the grab */
5451 terminate = 1;
5452 } else if (key != db->key[0] && key != db->key[1]) {
5453 /* Should not swallow release of other keys */
5454 send = 1;
5455 }
5456 } else if (key == db->key[0] && !db->key_released[0]) {
5457 /* Do not check bindings for the first press of the binding
5458 * key. This allows it to be used as a debug shortcut.
5459 * We still need to swallow this event. */
5460 check_binding = 0;
5461 } else if (db->key[1]) {
5462 /* If we already ran a binding don't process another one since
5463 * we can't keep track of all the binding keys that were
5464 * pressed in order to swallow the release events. */
5465 send = 1;
5466 check_binding = 0;
5467 }
5468
5469 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005470 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5471 key, state)) {
5472 /* We ran a binding so swallow the press and keep the
5473 * grab to swallow the released too. */
5474 send = 0;
5475 terminate = 0;
5476 db->key[1] = key;
5477 } else {
5478 /* Terminate the grab since the key pressed is not a
5479 * debug binding key. */
5480 send = 1;
5481 terminate = 1;
5482 }
5483 }
5484
5485 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005486 serial = wl_display_next_serial(display);
5487 resource_list = &grab->keyboard->focus_resource_list;
5488 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005489 wl_keyboard_send_key(resource, serial, time, key, state);
5490 }
5491 }
5492
5493 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005494 weston_keyboard_end_grab(grab->keyboard);
5495 if (grab->keyboard->input_method_resource)
5496 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005497 free(db);
5498 }
5499}
5500
5501static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005502debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005503 uint32_t mods_depressed, uint32_t mods_latched,
5504 uint32_t mods_locked, uint32_t group)
5505{
5506 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005507 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005508
Neil Roberts96d790e2013-09-19 17:32:00 +01005509 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005510
Neil Roberts96d790e2013-09-19 17:32:00 +01005511 wl_resource_for_each(resource, resource_list) {
5512 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5513 mods_latched, mods_locked, group);
5514 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005515}
5516
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005517static void
5518debug_binding_cancel(struct weston_keyboard_grab *grab)
5519{
5520 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5521
5522 weston_keyboard_end_grab(grab->keyboard);
5523 free(db);
5524}
5525
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005526struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005527 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005528 debug_binding_modifiers,
5529 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005530};
5531
5532static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005533debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005534{
5535 struct debug_binding_grab *grab;
5536
5537 grab = calloc(1, sizeof *grab);
5538 if (!grab)
5539 return;
5540
5541 grab->seat = (struct weston_seat *) seat;
5542 grab->key[0] = key;
5543 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005544 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005545}
5546
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005547static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005548force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005549 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005550{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005551 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005552 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005553 struct desktop_shell *shell = data;
5554 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005555 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005556
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005557 focus_surface = seat->keyboard->focus;
5558 if (!focus_surface)
5559 return;
5560
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005561 wl_signal_emit(&compositor->kill_signal, focus_surface);
5562
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005563 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005564 wl_client_get_credentials(client, &pid, NULL, NULL);
5565
5566 /* Skip clients that we launched ourselves (the credentials of
5567 * the socketpair is ours) */
5568 if (pid == getpid())
5569 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005570
Daniel Stone325fc2d2012-05-30 16:31:58 +01005571 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005572}
5573
5574static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005575workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005576 uint32_t key, void *data)
5577{
5578 struct desktop_shell *shell = data;
5579 unsigned int new_index = shell->workspaces.current;
5580
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005581 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005582 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005583 if (new_index != 0)
5584 new_index--;
5585
5586 change_workspace(shell, new_index);
5587}
5588
5589static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005590workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005591 uint32_t key, void *data)
5592{
5593 struct desktop_shell *shell = data;
5594 unsigned int new_index = shell->workspaces.current;
5595
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005596 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005597 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005598 if (new_index < shell->workspaces.num - 1)
5599 new_index++;
5600
5601 change_workspace(shell, new_index);
5602}
5603
5604static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005605workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005606 uint32_t key, void *data)
5607{
5608 struct desktop_shell *shell = data;
5609 unsigned int new_index;
5610
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005611 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005612 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005613 new_index = key - KEY_F1;
5614 if (new_index >= shell->workspaces.num)
5615 new_index = shell->workspaces.num - 1;
5616
5617 change_workspace(shell, new_index);
5618}
5619
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005620static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005621workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005622 uint32_t key, void *data)
5623{
5624 struct desktop_shell *shell = data;
5625 unsigned int new_index = shell->workspaces.current;
5626
5627 if (shell->locked)
5628 return;
5629
5630 if (new_index != 0)
5631 new_index--;
5632
5633 take_surface_to_workspace_by_seat(shell, seat, new_index);
5634}
5635
5636static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005637workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005638 uint32_t key, void *data)
5639{
5640 struct desktop_shell *shell = data;
5641 unsigned int new_index = shell->workspaces.current;
5642
5643 if (shell->locked)
5644 return;
5645
5646 if (new_index < shell->workspaces.num - 1)
5647 new_index++;
5648
5649 take_surface_to_workspace_by_seat(shell, seat, new_index);
5650}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005651
5652static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005653shell_reposition_view_on_output_destroy(struct weston_view *view)
5654{
5655 struct weston_output *output, *first_output;
5656 struct weston_compositor *ec = view->surface->compositor;
5657 struct shell_surface *shsurf;
5658 float x, y;
5659 int visible;
5660
5661 x = view->geometry.x;
5662 y = view->geometry.y;
5663
5664 /* At this point the destroyed output is not in the list anymore.
5665 * If the view is still visible somewhere, we leave where it is,
5666 * otherwise, move it to the first output. */
5667 visible = 0;
5668 wl_list_for_each(output, &ec->output_list, link) {
5669 if (pixman_region32_contains_point(&output->region,
5670 x, y, NULL)) {
5671 visible = 1;
5672 break;
5673 }
5674 }
5675
5676 if (!visible) {
5677 first_output = container_of(ec->output_list.next,
5678 struct weston_output, link);
5679
5680 x = first_output->x + first_output->width / 4;
5681 y = first_output->y + first_output->height / 4;
5682 }
5683
5684 weston_view_set_position(view, x, y);
5685
5686 shsurf = get_shell_surface(view->surface);
5687
5688 if (shsurf) {
5689 shsurf->saved_position_valid = false;
5690 shsurf->next_state.maximized = false;
5691 shsurf->next_state.fullscreen = false;
5692 shsurf->state_changed = true;
5693 }
5694}
5695
5696static void
5697shell_reposition_views_on_output_destroy(struct shell_output *shell_output)
5698{
5699 struct desktop_shell *shell = shell_output->shell;
5700 struct weston_output *output = shell_output->output;
5701 struct weston_layer *layer;
5702 struct weston_view *view;
5703
5704 /* Move all views in the layers owned by the shell */
5705 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5706 wl_list_for_each(view, &layer->view_list, layer_link) {
5707 if (view->output != output)
5708 continue;
5709
5710 shell_reposition_view_on_output_destroy(view);
5711 }
5712
5713 /* We don't start from the beggining of the layer list, so
5714 * make sure we don't wrap around it. */
5715 if (layer == &shell->background_layer)
5716 break;
5717 }
5718}
5719
5720static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005721handle_output_destroy(struct wl_listener *listener, void *data)
5722{
5723 struct shell_output *output_listener =
5724 container_of(listener, struct shell_output, destroy_listener);
5725
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005726 shell_reposition_views_on_output_destroy(output_listener);
5727
Xiong Zhang6b481422013-10-23 13:58:32 +08005728 wl_list_remove(&output_listener->destroy_listener.link);
5729 wl_list_remove(&output_listener->link);
5730 free(output_listener);
5731}
5732
5733static void
5734create_shell_output(struct desktop_shell *shell,
5735 struct weston_output *output)
5736{
5737 struct shell_output *shell_output;
5738
5739 shell_output = zalloc(sizeof *shell_output);
5740 if (shell_output == NULL)
5741 return;
5742
5743 shell_output->output = output;
5744 shell_output->shell = shell;
5745 shell_output->destroy_listener.notify = handle_output_destroy;
5746 wl_signal_add(&output->destroy_signal,
5747 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005748 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005749}
5750
5751static void
5752handle_output_create(struct wl_listener *listener, void *data)
5753{
5754 struct desktop_shell *shell =
5755 container_of(listener, struct desktop_shell, output_create_listener);
5756 struct weston_output *output = (struct weston_output *)data;
5757
5758 create_shell_output(shell, output);
5759}
5760
5761static void
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005762handle_output_move(struct wl_listener *listener, void *data)
5763{
5764 struct desktop_shell *shell;
5765 struct weston_output *output;
5766 struct weston_layer *layer;
5767 struct weston_view *view;
5768 float x, y;
5769
5770 shell = container_of(listener, struct desktop_shell,
5771 output_move_listener);
5772 output = data;
5773
5774 /* Move all views in the layers owned by the shell */
5775 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5776 wl_list_for_each(view, &layer->view_list, layer_link) {
5777 if (view->output != output)
5778 continue;
5779
5780 x = view->geometry.x + output->move_x;
5781 y = view->geometry.y + output->move_y;
5782 weston_view_set_position(view, x, y);
5783 }
5784
5785 /* We don't start from the beggining of the layer list, so
5786 * make sure we don't wrap around it. */
5787 if (layer == &shell->background_layer)
5788 break;
5789 }
5790}
5791
5792static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005793setup_output_destroy_handler(struct weston_compositor *ec,
5794 struct desktop_shell *shell)
5795{
5796 struct weston_output *output;
5797
5798 wl_list_init(&shell->output_list);
5799 wl_list_for_each(output, &ec->output_list, link)
5800 create_shell_output(shell, output);
5801
5802 shell->output_create_listener.notify = handle_output_create;
5803 wl_signal_add(&ec->output_created_signal,
5804 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005805
5806 shell->output_move_listener.notify = handle_output_move;
5807 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005808}
5809
5810static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005811shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005812{
Tiago Vignattibe143262012-04-16 17:31:41 +03005813 struct desktop_shell *shell =
5814 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005815 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005816 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005817
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005818 /* Force state to unlocked so we don't try to fade */
5819 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005820 if (shell->child.client)
5821 wl_client_destroy(shell->child.client);
5822
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005823 wl_list_remove(&shell->idle_listener.link);
5824 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005825
5826 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005827
Xiong Zhang6b481422013-10-23 13:58:32 +08005828 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5829 wl_list_remove(&shell_output->destroy_listener.link);
5830 wl_list_remove(&shell_output->link);
5831 free(shell_output);
5832 }
5833
5834 wl_list_remove(&shell->output_create_listener.link);
5835
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005836 wl_array_for_each(ws, &shell->workspaces.array)
5837 workspace_destroy(*ws);
5838 wl_array_release(&shell->workspaces.array);
5839
Pekka Paalanen3c647232011-12-22 13:43:43 +02005840 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005841 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005842 free(shell);
5843}
5844
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005845static void
5846shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5847{
5848 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005849 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005850
5851 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005852 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5853 MODIFIER_CTRL | MODIFIER_ALT,
5854 terminate_binding, ec);
5855 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5856 click_to_activate_binding,
5857 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005858 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5859 click_to_activate_binding,
5860 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005861 weston_compositor_add_touch_binding(ec, 0,
5862 touch_to_activate_binding,
5863 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005864 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5865 MODIFIER_SUPER | MODIFIER_ALT,
5866 surface_opacity_binding, NULL);
5867 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5868 MODIFIER_SUPER, zoom_axis_binding,
5869 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005870
5871 /* configurable bindings */
5872 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005873 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5874 zoom_key_binding, NULL);
5875 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5876 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005877 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5878 maximize_binding, NULL);
5879 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5880 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005881 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5882 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005883 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005884 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5885 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005886 weston_compositor_add_button_binding(ec, BTN_LEFT,
5887 mod | MODIFIER_SHIFT,
5888 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005889
5890 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5891 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5892 rotate_binding, NULL);
5893
Daniel Stone325fc2d2012-05-30 16:31:58 +01005894 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5895 shell);
5896 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5897 ec);
5898 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5899 backlight_binding, ec);
5900 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5901 ec);
5902 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5903 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005904 weston_compositor_add_key_binding(ec, KEY_K, mod,
5905 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005906 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5907 workspace_up_binding, shell);
5908 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5909 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005910 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5911 workspace_move_surface_up_binding,
5912 shell);
5913 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5914 workspace_move_surface_down_binding,
5915 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005916
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005917 if (shell->exposay_modifier)
5918 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5919 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005920
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005921 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5922 if (shell->workspaces.num > 1) {
5923 num_workspace_bindings = shell->workspaces.num;
5924 if (num_workspace_bindings > 6)
5925 num_workspace_bindings = 6;
5926 for (i = 0; i < num_workspace_bindings; i++)
5927 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5928 workspace_f_binding,
5929 shell);
5930 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005931
5932 /* Debug bindings */
5933 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5934 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005935}
5936
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005937WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005938module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005939 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005940{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005941 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005942 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005943 struct workspace **pws;
5944 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005945 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005946
Peter Huttererf3d62272013-08-08 11:57:05 +10005947 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005948 if (shell == NULL)
5949 return -1;
5950
Kristian Høgsberg75840622011-09-06 13:48:16 -04005951 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005952
5953 shell->destroy_listener.notify = shell_destroy;
5954 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005955 shell->idle_listener.notify = idle_handler;
5956 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5957 shell->wake_listener.notify = wake_handler;
5958 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005959
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005960 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005961 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005962 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005963 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005964 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005965 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005966 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005967 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005968 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005969 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005970
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005971 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5972 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005973 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5974 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005975 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005976
5977 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005978 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005979
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005980 if (input_panel_setup(shell) < 0)
5981 return -1;
5982
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005983 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005984
Daniel Stonedf8133b2013-11-19 11:37:14 +01005985 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5986 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5987
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005988 for (i = 0; i < shell->workspaces.num; i++) {
5989 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5990 if (pws == NULL)
5991 return -1;
5992
5993 *pws = workspace_create();
5994 if (*pws == NULL)
5995 return -1;
5996 }
5997 activate_workspace(shell, 0);
5998
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005999 weston_layer_init(&shell->minimized_layer, NULL);
6000
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006001 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006002 wl_list_init(&shell->workspaces.animation.link);
6003 shell->workspaces.animation.frame = animate_workspace_change_frame;
6004
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006005 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006006 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006007 return -1;
6008
Rafael Antognollie2a34552013-12-03 15:35:45 -02006009 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6010 shell, bind_xdg_shell) == NULL)
6011 return -1;
6012
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006013 if (wl_global_create(ec->wl_display,
6014 &desktop_shell_interface, 2,
6015 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006016 return -1;
6017
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006018 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6019 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006020 return -1;
6021
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006022 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6023 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006024 return -1;
6025
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006026 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006027
Xiong Zhang6b481422013-10-23 13:58:32 +08006028 setup_output_destroy_handler(ec, shell);
6029
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006030 loop = wl_display_get_event_loop(ec->wl_display);
6031 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006032
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006033 shell->screensaver.timer =
6034 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6035
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006036 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006037 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006038 create_keyboard_focus_listener(seat);
6039 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006040
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006041 screenshooter_create(ec);
6042
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006043 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006044
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006045 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006046
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006047 return 0;
6048}