blob: f9fedacd65d8e39f628ca282b6da0101c4bf6b5d [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
75 * • Fullscreen layer
76 * • Panel layer
77 * • Input panel layer
78 * • Workspace layers
79 * • Background layer
80 *
81 * The list of layers may be manipulated to remove whole layers of surfaces from
82 * display. For example, when locking the screen, all layers except the lock
83 * layer are removed.
84 *
85 * A surface’s layer is modified on configuring the surface, in
86 * set_surface_type() (which is only called when the surface’s type change is
87 * _committed_). If a surface’s type changes (e.g. when making a window
88 * fullscreen) its layer changes too.
89 *
90 * In order to allow popup and transient surfaces to be correctly stacked above
91 * their parent surfaces, each surface tracks both its parent surface, and a
92 * linked list of its children. When a surface’s layer is updated, so are the
93 * layers of its children. Note that child surfaces are *not* the same as
94 * subsurfaces — child/parent surfaces are purely for maintaining stacking
95 * order.
96 *
97 * The children_link list of siblings of a surface (i.e. those surfaces which
98 * have the same parent) only contains weston_surfaces which have a
99 * shell_surface. Stacking is not implemented for non-shell_surface
100 * weston_surfaces. This means that the following implication does *not* hold:
101 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
102 */
103
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200104struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500105 struct wl_resource *resource;
106 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200107
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500109 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600110 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700112 struct wl_listener resource_destroy_listener;
113
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400114 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000115 struct wl_list children_list; /* child surfaces of this one */
116 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300117 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200118
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800119 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400120 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500121 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200122 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800123 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200124 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800125 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700126 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800127 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100128
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500129 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200130 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500131 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200132 } rotation;
133
134 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100135 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500136 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100137 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400138 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 } popup;
140
Alex Wu4539b082012-03-01 12:57:46 +0800141 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300142 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400143 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300144 } transient;
145
146 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800147 enum wl_shell_surface_fullscreen_method type;
148 struct weston_transform transform; /* matrix from x, y */
149 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500150 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800151 } fullscreen;
152
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200153 struct weston_transform workspace_transform;
154
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500155 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200157 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100158 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400159
160 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200161
162 struct {
163 bool maximized;
164 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200165 bool relative;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500166 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200167 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500168 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500169
170 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200171};
172
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300173struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400174 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300175 struct shell_surface *shsurf;
176 struct wl_listener shsurf_destroy_listener;
177};
178
Rusty Lynch1084da52013-08-15 09:10:08 -0700179struct shell_touch_grab {
180 struct weston_touch_grab grab;
181 struct shell_surface *shsurf;
182 struct wl_listener shsurf_destroy_listener;
183 struct weston_touch *touch;
184};
185
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300186struct weston_move_grab {
187 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100188 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500189};
190
Rusty Lynch1084da52013-08-15 09:10:08 -0700191struct weston_touch_move_grab {
192 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800193 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700194 wl_fixed_t dx, dy;
195};
196
Pekka Paalanen460099f2012-01-20 16:48:25 +0200197struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300198 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500199 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200200 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200201 float x;
202 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 } center;
204};
205
Giulio Camuffo5085a752013-03-25 21:42:45 +0100206struct shell_seat {
207 struct weston_seat *seat;
208 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500209 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100210
211 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400212 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100213 struct wl_list surfaces_list;
214 struct wl_client *client;
215 int32_t initial_up;
216 } popup_grab;
217};
218
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800219struct shell_client {
220 struct wl_resource *resource;
221 struct wl_client *client;
222 struct desktop_shell *shell;
223 struct wl_listener destroy_listener;
224 struct wl_event_source *ping_timer;
225 uint32_t ping_serial;
226 int unresponsive;
227};
228
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100229void
230set_alpha_if_fullscreen(struct shell_surface *shsurf)
231{
232 if (shsurf && shsurf->state.fullscreen)
233 shsurf->fullscreen.black_view->alpha = 0.25;
234}
235
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -0800236static struct shell_client *
237get_shell_client(struct wl_client *client);
238
Alex Wubd3354b2012-04-17 17:20:49 +0800239static struct desktop_shell *
240shell_surface_get_shell(struct shell_surface *shsurf);
241
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500242static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400243surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500244
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300245static void
246shell_fade_startup(struct desktop_shell *shell);
247
Philip Withnallbecb77e2013-11-25 18:01:30 +0000248static struct shell_seat *
249get_shell_seat(struct weston_seat *seat);
250
Philip Withnall648a4dd2013-11-25 18:01:44 +0000251static void
252shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
253
Alex Wubd3354b2012-04-17 17:20:49 +0800254static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200255shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
256
257static bool
258shell_surface_is_xdg_surface(struct shell_surface *shsurf);
259
260static bool
261shell_surface_is_xdg_popup(struct shell_surface *shsurf);
262
263static void
264shell_surface_set_parent(struct shell_surface *shsurf,
265 struct weston_surface *parent);
266
267static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800268shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
269{
270 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800272
273 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100274
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800276 return false;
277
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
279 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800280 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800282}
283
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500284static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400285destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300286{
287 struct shell_grab *grab;
288
289 grab = container_of(listener, struct shell_grab,
290 shsurf_destroy_listener);
291
292 grab->shsurf = NULL;
293}
294
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800295struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500296get_default_view(struct weston_surface *surface)
297{
298 struct shell_surface *shsurf;
299 struct weston_view *view;
300
301 if (!surface || wl_list_empty(&surface->views))
302 return NULL;
303
304 shsurf = get_shell_surface(surface);
305 if (shsurf)
306 return shsurf->view;
307
308 wl_list_for_each(view, &surface->views, surface_link)
309 if (weston_view_is_mapped(view))
310 return view;
311
312 return container_of(surface->views.next, struct weston_view, surface_link);
313}
314
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300315static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400316popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400317
318static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400322 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300324{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300325 struct desktop_shell *shell = shsurf->shell;
326
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400327 popup_grab_end(pointer);
328
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300329 grab->grab.interface = interface;
330 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400331 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500332 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400333 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300334
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700335 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400336 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400337 if (shell->child.desktop_shell) {
338 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
339 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 weston_pointer_set_focus(pointer,
341 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400342 wl_fixed_from_int(0),
343 wl_fixed_from_int(0));
344 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345}
346
347static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300348shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300349{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700350 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400351 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700352 grab->shsurf->grabbed = 0;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800353 grab->shsurf->resize_edges = 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700354 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300355
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700356 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300357}
358
359static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700360shell_touch_grab_start(struct shell_touch_grab *grab,
361 const struct weston_touch_grab_interface *interface,
362 struct shell_surface *shsurf,
363 struct weston_touch *touch)
364{
365 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800366
Rusty Lynch1084da52013-08-15 09:10:08 -0700367 grab->grab.interface = interface;
368 grab->shsurf = shsurf;
369 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
370 wl_signal_add(&shsurf->destroy_signal,
371 &grab->shsurf_destroy_listener);
372
373 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700374 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700375
376 weston_touch_start_grab(touch, &grab->grab);
377 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500378 weston_touch_set_focus(touch->seat,
379 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700380}
381
382static void
383shell_touch_grab_end(struct shell_touch_grab *grab)
384{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700385 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700386 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700387 grab->shsurf->grabbed = 0;
388 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700389
390 weston_touch_end_grab(grab->touch);
391}
392
393static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500394center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800395 struct weston_output *output);
396
Daniel Stone496ca172012-05-30 16:31:42 +0100397static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300398get_modifier(char *modifier)
399{
400 if (!modifier)
401 return MODIFIER_SUPER;
402
403 if (!strcmp("ctrl", modifier))
404 return MODIFIER_CTRL;
405 else if (!strcmp("alt", modifier))
406 return MODIFIER_ALT;
407 else if (!strcmp("super", modifier))
408 return MODIFIER_SUPER;
409 else
410 return MODIFIER_SUPER;
411}
412
Juan Zhaoe10d2792012-04-25 19:09:52 +0800413static enum animation_type
414get_animation_type(char *animation)
415{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800416 if (!animation)
417 return ANIMATION_NONE;
418
Juan Zhaoe10d2792012-04-25 19:09:52 +0800419 if (!strcmp("zoom", animation))
420 return ANIMATION_ZOOM;
421 else if (!strcmp("fade", animation))
422 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100423 else if (!strcmp("dim-layer", animation))
424 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800425 else
426 return ANIMATION_NONE;
427}
428
Alex Wu4539b082012-03-01 12:57:46 +0800429static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400430shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200431{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400432 struct weston_config_section *section;
433 int duration;
434 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200435
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400436 section = weston_config_get_section(shell->compositor->config,
437 "screensaver", NULL, NULL);
438 weston_config_section_get_string(section,
439 "path", &shell->screensaver.path, NULL);
440 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200441 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400442
443 section = weston_config_get_section(shell->compositor->config,
444 "shell", NULL, NULL);
445 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100446 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100447 shell->client = s;
448 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400449 "binding-modifier", &s, "super");
450 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200451 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800452
453 weston_config_section_get_string(section,
454 "exposay-modifier", &s, "none");
455 if (strcmp(s, "none") == 0)
456 shell->exposay_modifier = 0;
457 else
458 shell->exposay_modifier = get_modifier(s);
459 free(s);
460
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400461 weston_config_section_get_string(section, "animation", &s, "none");
462 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200463 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700464 weston_config_section_get_string(section,
465 "startup-animation", &s, "fade");
466 shell->startup_animation_type = get_animation_type(s);
467 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700468 if (shell->startup_animation_type == ANIMATION_ZOOM)
469 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100470 weston_config_section_get_string(section, "focus-animation", &s, "none");
471 shell->focus_animation_type = get_animation_type(s);
472 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400473 weston_config_section_get_uint(section, "num-workspaces",
474 &shell->workspaces.num,
475 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200476}
477
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800478struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100479get_default_output(struct weston_compositor *compositor)
480{
481 return container_of(compositor->output_list.next,
482 struct weston_output, link);
483}
484
485
486/* no-op func for checking focus surface */
487static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600488focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100489{
490}
491
492static struct focus_surface *
493get_focus_surface(struct weston_surface *surface)
494{
495 if (surface->configure == focus_surface_configure)
496 return surface->configure_private;
497 else
498 return NULL;
499}
500
501static bool
502is_focus_surface (struct weston_surface *es)
503{
504 return (es->configure == focus_surface_configure);
505}
506
507static bool
508is_focus_view (struct weston_view *view)
509{
510 return is_focus_surface (view->surface);
511}
512
513static struct focus_surface *
514create_focus_surface(struct weston_compositor *ec,
515 struct weston_output *output)
516{
517 struct focus_surface *fsurf = NULL;
518 struct weston_surface *surface = NULL;
519
520 fsurf = malloc(sizeof *fsurf);
521 if (!fsurf)
522 return NULL;
523
524 fsurf->surface = weston_surface_create(ec);
525 surface = fsurf->surface;
526 if (surface == NULL) {
527 free(fsurf);
528 return NULL;
529 }
530
531 surface->configure = focus_surface_configure;
532 surface->output = output;
533 surface->configure_private = fsurf;
534
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800535 fsurf->view = weston_view_create(surface);
536 if (fsurf->view == NULL) {
537 weston_surface_destroy(surface);
538 free(fsurf);
539 return NULL;
540 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100541 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100542
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600543 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600544 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100545 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
546 pixman_region32_fini(&surface->opaque);
547 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
548 output->width, output->height);
549 pixman_region32_fini(&surface->input);
550 pixman_region32_init(&surface->input);
551
552 wl_list_init(&fsurf->workspace_transform.link);
553
554 return fsurf;
555}
556
557static void
558focus_surface_destroy(struct focus_surface *fsurf)
559{
560 weston_surface_destroy(fsurf->surface);
561 free(fsurf);
562}
563
564static void
565focus_animation_done(struct weston_view_animation *animation, void *data)
566{
567 struct workspace *ws = data;
568
569 ws->focus_animation = NULL;
570}
571
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200572static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200573focus_state_destroy(struct focus_state *state)
574{
575 wl_list_remove(&state->seat_destroy_listener.link);
576 wl_list_remove(&state->surface_destroy_listener.link);
577 free(state);
578}
579
580static void
581focus_state_seat_destroy(struct wl_listener *listener, void *data)
582{
583 struct focus_state *state = container_of(listener,
584 struct focus_state,
585 seat_destroy_listener);
586
587 wl_list_remove(&state->link);
588 focus_state_destroy(state);
589}
590
591static void
592focus_state_surface_destroy(struct wl_listener *listener, void *data)
593{
594 struct focus_state *state = container_of(listener,
595 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400596 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400597 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500598 struct weston_surface *main_surface, *next;
599 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200600
Pekka Paalanen01388e22013-04-25 13:57:44 +0300601 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
602
Kristian Høgsberge3778222012-07-31 17:29:30 -0400603 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500604 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
605 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400606 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100607 if (is_focus_view(view))
608 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400609
Jason Ekstranda7af7042013-10-12 22:38:11 -0500610 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400611 break;
612 }
613
Pekka Paalanen01388e22013-04-25 13:57:44 +0300614 /* if the focus was a sub-surface, activate its main surface */
615 if (main_surface != state->keyboard_focus)
616 next = main_surface;
617
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100618 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400619 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100620 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400621 activate(shell, next, state->seat);
622 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100623 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
624 if (state->ws->focus_animation)
625 weston_view_animation_destroy(state->ws->focus_animation);
626
627 state->ws->focus_animation = weston_fade_run(
628 state->ws->fsurf_front->view,
629 state->ws->fsurf_front->view->alpha, 0.0, 300,
630 focus_animation_done, state->ws);
631 }
632
Kristian Høgsberge3778222012-07-31 17:29:30 -0400633 wl_list_remove(&state->link);
634 focus_state_destroy(state);
635 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200636}
637
638static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400639focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200640{
Jonas Ådahl04769742012-06-13 00:01:24 +0200641 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200642
643 state = malloc(sizeof *state);
644 if (state == NULL)
645 return NULL;
646
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100647 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400648 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200649 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400650 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200651
652 state->seat_destroy_listener.notify = focus_state_seat_destroy;
653 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400654 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200655 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400656 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200657
658 return state;
659}
660
Jonas Ådahl8538b222012-08-29 22:13:03 +0200661static struct focus_state *
662ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
663{
664 struct workspace *ws = get_current_workspace(shell);
665 struct focus_state *state;
666
667 wl_list_for_each(state, &ws->focus_list, link)
668 if (state->seat == seat)
669 break;
670
671 if (&state->link == &ws->focus_list)
672 state = focus_state_create(seat, ws);
673
674 return state;
675}
676
Jonas Ådahl04769742012-06-13 00:01:24 +0200677static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800678focus_state_set_focus(struct focus_state *state,
679 struct weston_surface *surface)
680{
681 if (state->keyboard_focus) {
682 wl_list_remove(&state->surface_destroy_listener.link);
683 wl_list_init(&state->surface_destroy_listener.link);
684 }
685
686 state->keyboard_focus = surface;
687 if (surface)
688 wl_signal_add(&surface->destroy_signal,
689 &state->surface_destroy_listener);
690}
691
692static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400693restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200694{
695 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400696 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200697
698 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800699 if (state->seat->keyboard == NULL)
700 continue;
701
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400702 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200703
Kristian Høgsberge3148752013-05-06 23:19:49 -0400704 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200705 }
706}
707
708static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200709replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
710 struct weston_seat *seat)
711{
712 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200713
714 wl_list_for_each(state, &ws->focus_list, link) {
715 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800716 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200717 return;
718 }
719 }
720}
721
722static void
723drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
724 struct weston_surface *surface)
725{
726 struct focus_state *state;
727
728 wl_list_for_each(state, &ws->focus_list, link)
729 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800730 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200731}
732
733static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100734animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
735 struct weston_view *from, struct weston_view *to)
736{
737 struct weston_output *output;
738 bool focus_surface_created = false;
739
740 /* FIXME: Only support dim animation using two layers */
741 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
742 return;
743
744 output = get_default_output(shell->compositor);
745 if (ws->fsurf_front == NULL && (from || to)) {
746 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800747 if (ws->fsurf_front == NULL)
748 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100749 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800750
751 ws->fsurf_back = create_focus_surface(shell->compositor, output);
752 if (ws->fsurf_back == NULL) {
753 focus_surface_destroy(ws->fsurf_front);
754 return;
755 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100756 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800757
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100758 focus_surface_created = true;
759 } else {
760 wl_list_remove(&ws->fsurf_front->view->layer_link);
761 wl_list_remove(&ws->fsurf_back->view->layer_link);
762 }
763
764 if (ws->focus_animation) {
765 weston_view_animation_destroy(ws->focus_animation);
766 ws->focus_animation = NULL;
767 }
768
769 if (to)
770 wl_list_insert(&to->layer_link,
771 &ws->fsurf_front->view->layer_link);
772 else if (from)
773 wl_list_insert(&ws->layer.view_list,
774 &ws->fsurf_front->view->layer_link);
775
776 if (focus_surface_created) {
777 ws->focus_animation = weston_fade_run(
778 ws->fsurf_front->view,
779 ws->fsurf_front->view->alpha, 0.6, 300,
780 focus_animation_done, ws);
781 } else if (from) {
782 wl_list_insert(&from->layer_link,
783 &ws->fsurf_back->view->layer_link);
784 ws->focus_animation = weston_stable_fade_run(
785 ws->fsurf_front->view, 0.0,
786 ws->fsurf_back->view, 0.6,
787 focus_animation_done, ws);
788 } else if (to) {
789 wl_list_insert(&ws->layer.view_list,
790 &ws->fsurf_back->view->layer_link);
791 ws->focus_animation = weston_stable_fade_run(
792 ws->fsurf_front->view, 0.0,
793 ws->fsurf_back->view, 0.6,
794 focus_animation_done, ws);
795 }
796}
797
798static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200799workspace_destroy(struct workspace *ws)
800{
Jonas Ådahl04769742012-06-13 00:01:24 +0200801 struct focus_state *state, *next;
802
803 wl_list_for_each_safe(state, next, &ws->focus_list, link)
804 focus_state_destroy(state);
805
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100806 if (ws->fsurf_front)
807 focus_surface_destroy(ws->fsurf_front);
808 if (ws->fsurf_back)
809 focus_surface_destroy(ws->fsurf_back);
810
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200811 free(ws);
812}
813
Jonas Ådahl04769742012-06-13 00:01:24 +0200814static void
815seat_destroyed(struct wl_listener *listener, void *data)
816{
817 struct weston_seat *seat = data;
818 struct focus_state *state, *next;
819 struct workspace *ws = container_of(listener,
820 struct workspace,
821 seat_destroyed_listener);
822
823 wl_list_for_each_safe(state, next, &ws->focus_list, link)
824 if (state->seat == seat)
825 wl_list_remove(&state->link);
826}
827
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200828static struct workspace *
829workspace_create(void)
830{
831 struct workspace *ws = malloc(sizeof *ws);
832 if (ws == NULL)
833 return NULL;
834
835 weston_layer_init(&ws->layer, NULL);
836
Jonas Ådahl04769742012-06-13 00:01:24 +0200837 wl_list_init(&ws->focus_list);
838 wl_list_init(&ws->seat_destroyed_listener.link);
839 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100840 ws->fsurf_front = NULL;
841 ws->fsurf_back = NULL;
842 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200843
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200844 return ws;
845}
846
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200847static int
848workspace_is_empty(struct workspace *ws)
849{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500850 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200851}
852
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200853static struct workspace *
854get_workspace(struct desktop_shell *shell, unsigned int index)
855{
856 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200857 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200858 pws += index;
859 return *pws;
860}
861
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800862struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200863get_current_workspace(struct desktop_shell *shell)
864{
865 return get_workspace(shell, shell->workspaces.current);
866}
867
868static void
869activate_workspace(struct desktop_shell *shell, unsigned int index)
870{
871 struct workspace *ws;
872
873 ws = get_workspace(shell, index);
874 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
875
876 shell->workspaces.current = index;
877}
878
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200879static unsigned int
880get_output_height(struct weston_output *output)
881{
882 return abs(output->region.extents.y1 - output->region.extents.y2);
883}
884
885static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100886view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200887{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200888 struct weston_transform *transform;
889
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100890 if (is_focus_view(view)) {
891 struct focus_surface *fsurf = get_focus_surface(view->surface);
892 transform = &fsurf->workspace_transform;
893 } else {
894 struct shell_surface *shsurf = get_shell_surface(view->surface);
895 transform = &shsurf->workspace_transform;
896 }
897
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200898 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500899 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100900 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200901
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100902 weston_matrix_init(&transform->matrix);
903 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200904 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500905 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200906}
907
908static void
909workspace_translate_out(struct workspace *ws, double fraction)
910{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912 unsigned int height;
913 double d;
914
Jason Ekstranda7af7042013-10-12 22:38:11 -0500915 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
916 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200917 d = height * fraction;
918
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100919 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200920 }
921}
922
923static void
924workspace_translate_in(struct workspace *ws, double fraction)
925{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200927 unsigned int height;
928 double d;
929
Jason Ekstranda7af7042013-10-12 22:38:11 -0500930 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
931 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200932
933 if (fraction > 0)
934 d = -(height - height * fraction);
935 else
936 d = height + height * fraction;
937
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100938 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200939 }
940}
941
942static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200943broadcast_current_workspace_state(struct desktop_shell *shell)
944{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700945 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200946
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700947 wl_resource_for_each(resource, &shell->workspaces.client_list)
948 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200949 shell->workspaces.current,
950 shell->workspaces.num);
951}
952
953static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200954reverse_workspace_change_animation(struct desktop_shell *shell,
955 unsigned int index,
956 struct workspace *from,
957 struct workspace *to)
958{
959 shell->workspaces.current = index;
960
961 shell->workspaces.anim_to = to;
962 shell->workspaces.anim_from = from;
963 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
964 shell->workspaces.anim_timestamp = 0;
965
Scott Moreau4272e632012-08-13 09:58:41 -0600966 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967}
968
969static void
970workspace_deactivate_transforms(struct workspace *ws)
971{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100973 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200974
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100976 if (is_focus_view(view)) {
977 struct focus_surface *fsurf = get_focus_surface(view->surface);
978 transform = &fsurf->workspace_transform;
979 } else {
980 struct shell_surface *shsurf = get_shell_surface(view->surface);
981 transform = &shsurf->workspace_transform;
982 }
983
984 if (!wl_list_empty(&transform->link)) {
985 wl_list_remove(&transform->link);
986 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200987 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200989 }
990}
991
992static void
993finish_workspace_change_animation(struct desktop_shell *shell,
994 struct workspace *from,
995 struct workspace *to)
996{
Scott Moreau4272e632012-08-13 09:58:41 -0600997 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998
999 wl_list_remove(&shell->workspaces.animation.link);
1000 workspace_deactivate_transforms(from);
1001 workspace_deactivate_transforms(to);
1002 shell->workspaces.anim_to = NULL;
1003
1004 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1005}
1006
1007static void
1008animate_workspace_change_frame(struct weston_animation *animation,
1009 struct weston_output *output, uint32_t msecs)
1010{
1011 struct desktop_shell *shell =
1012 container_of(animation, struct desktop_shell,
1013 workspaces.animation);
1014 struct workspace *from = shell->workspaces.anim_from;
1015 struct workspace *to = shell->workspaces.anim_to;
1016 uint32_t t;
1017 double x, y;
1018
1019 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1020 finish_workspace_change_animation(shell, from, to);
1021 return;
1022 }
1023
1024 if (shell->workspaces.anim_timestamp == 0) {
1025 if (shell->workspaces.anim_current == 0.0)
1026 shell->workspaces.anim_timestamp = msecs;
1027 else
1028 shell->workspaces.anim_timestamp =
1029 msecs -
1030 /* Invers of movement function 'y' below. */
1031 (asin(1.0 - shell->workspaces.anim_current) *
1032 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1033 M_2_PI);
1034 }
1035
1036 t = msecs - shell->workspaces.anim_timestamp;
1037
1038 /*
1039 * x = [0, π/2]
1040 * y(x) = sin(x)
1041 */
1042 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1043 y = sin(x);
1044
1045 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001046 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001047
1048 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1049 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1050 shell->workspaces.anim_current = y;
1051
Scott Moreau4272e632012-08-13 09:58:41 -06001052 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001053 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001054 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001056}
1057
1058static void
1059animate_workspace_change(struct desktop_shell *shell,
1060 unsigned int index,
1061 struct workspace *from,
1062 struct workspace *to)
1063{
1064 struct weston_output *output;
1065
1066 int dir;
1067
1068 if (index > shell->workspaces.current)
1069 dir = -1;
1070 else
1071 dir = 1;
1072
1073 shell->workspaces.current = index;
1074
1075 shell->workspaces.anim_dir = dir;
1076 shell->workspaces.anim_from = from;
1077 shell->workspaces.anim_to = to;
1078 shell->workspaces.anim_current = 0.0;
1079 shell->workspaces.anim_timestamp = 0;
1080
1081 output = container_of(shell->compositor->output_list.next,
1082 struct weston_output, link);
1083 wl_list_insert(&output->animation_list,
1084 &shell->workspaces.animation.link);
1085
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001086 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001087
1088 workspace_translate_in(to, 0);
1089
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001090 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001091
Scott Moreau4272e632012-08-13 09:58:41 -06001092 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001093}
1094
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001095static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001096update_workspace(struct desktop_shell *shell, unsigned int index,
1097 struct workspace *from, struct workspace *to)
1098{
1099 shell->workspaces.current = index;
1100 wl_list_insert(&from->layer.link, &to->layer.link);
1101 wl_list_remove(&from->layer.link);
1102}
1103
1104static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001105change_workspace(struct desktop_shell *shell, unsigned int index)
1106{
1107 struct workspace *from;
1108 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001109 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001110
1111 if (index == shell->workspaces.current)
1112 return;
1113
1114 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001116 return;
1117
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001118 from = get_current_workspace(shell);
1119 to = get_workspace(shell, index);
1120
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001121 if (shell->workspaces.anim_from == to &&
1122 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001123 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001124 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001125 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126 return;
1127 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001128
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129 if (shell->workspaces.anim_to != NULL)
1130 finish_workspace_change_animation(shell,
1131 shell->workspaces.anim_from,
1132 shell->workspaces.anim_to);
1133
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001134 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001135
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001136 if (shell->focus_animation_type != ANIMATION_NONE) {
1137 wl_list_for_each(state, &from->focus_list, link)
1138 if (state->keyboard_focus)
1139 animate_focus_change(shell, from,
1140 get_default_view(state->keyboard_focus), NULL);
1141
1142 wl_list_for_each(state, &to->focus_list, link)
1143 if (state->keyboard_focus)
1144 animate_focus_change(shell, to,
1145 NULL, get_default_view(state->keyboard_focus));
1146 }
1147
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001148 if (workspace_is_empty(to) && workspace_is_empty(from))
1149 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001150 else
1151 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001152
1153 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001154}
1155
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001156static bool
1157workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1158{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001160 struct wl_list *e;
1161
1162 if (wl_list_empty(list))
1163 return false;
1164
1165 e = list->next;
1166
1167 if (e->next != list)
1168 return false;
1169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001171}
1172
1173static void
Philip Withnall659163d2013-11-25 18:01:36 +00001174move_surface_to_workspace(struct desktop_shell *shell,
1175 struct shell_surface *shsurf,
1176 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001177{
1178 struct workspace *from;
1179 struct workspace *to;
1180 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001181 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001182 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001183
1184 if (workspace == shell->workspaces.current)
1185 return;
1186
Philip Withnall659163d2013-11-25 18:01:36 +00001187 view = get_default_view(shsurf->surface);
1188 if (!view)
1189 return;
1190
1191 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1192
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001193 if (workspace >= shell->workspaces.num)
1194 workspace = shell->workspaces.num - 1;
1195
Jonas Ådahle9d22502012-08-29 22:13:01 +02001196 from = get_current_workspace(shell);
1197 to = get_workspace(shell, workspace);
1198
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 wl_list_remove(&view->layer_link);
1200 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001201
Philip Withnall648a4dd2013-11-25 18:01:44 +00001202 shell_surface_update_child_surface_layers(shsurf);
1203
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001205 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1206 if (!seat->keyboard)
1207 continue;
1208
1209 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001211 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001212 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001213
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001215}
1216
1217static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001218take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001219 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001220 unsigned int index)
1221{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001222 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224 struct shell_surface *shsurf;
1225 struct workspace *from;
1226 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001227 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001228
Pekka Paalanen01388e22013-04-25 13:57:44 +03001229 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 view = get_default_view(surface);
1231 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001232 index == shell->workspaces.current ||
1233 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001234 return;
1235
1236 from = get_current_workspace(shell);
1237 to = get_workspace(shell, index);
1238
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 wl_list_remove(&view->layer_link);
1240 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001241
Philip Withnall659163d2013-11-25 18:01:36 +00001242 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001243 if (shsurf != NULL)
1244 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001245
Jonas Ådahle9d22502012-08-29 22:13:01 +02001246 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001247 drop_focus_state(shell, from, surface);
1248
1249 if (shell->workspaces.anim_from == to &&
1250 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001251 wl_list_remove(&to->layer.link);
1252 wl_list_insert(from->layer.link.prev, &to->layer.link);
1253
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001254 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001255 broadcast_current_workspace_state(shell);
1256
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257 return;
1258 }
1259
1260 if (shell->workspaces.anim_to != NULL)
1261 finish_workspace_change_animation(shell,
1262 shell->workspaces.anim_from,
1263 shell->workspaces.anim_to);
1264
1265 if (workspace_is_empty(from) &&
1266 workspace_has_only(to, surface))
1267 update_workspace(shell, index, from, to);
1268 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001269 if (shsurf != NULL &&
1270 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001271 wl_list_insert(&shell->workspaces.anim_sticky_list,
1272 &shsurf->workspace_transform.link);
1273
1274 animate_workspace_change(shell, index, from, to);
1275 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001276
1277 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001278
1279 state = ensure_focus_state(shell, seat);
1280 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001281 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001282}
1283
1284static void
1285workspace_manager_move_surface(struct wl_client *client,
1286 struct wl_resource *resource,
1287 struct wl_resource *surface_resource,
1288 uint32_t workspace)
1289{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001290 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001291 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001292 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001293 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001294 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001295
Pekka Paalanen01388e22013-04-25 13:57:44 +03001296 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001297 shell_surface = get_shell_surface(main_surface);
1298 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001300
1301 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001302}
1303
1304static const struct workspace_manager_interface workspace_manager_implementation = {
1305 workspace_manager_move_surface,
1306};
1307
1308static void
1309unbind_resource(struct wl_resource *resource)
1310{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001311 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001312}
1313
1314static void
1315bind_workspace_manager(struct wl_client *client,
1316 void *data, uint32_t version, uint32_t id)
1317{
1318 struct desktop_shell *shell = data;
1319 struct wl_resource *resource;
1320
Jason Ekstranda85118c2013-06-27 20:17:02 -05001321 resource = wl_resource_create(client,
1322 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323
1324 if (resource == NULL) {
1325 weston_log("couldn't add workspace manager object");
1326 return;
1327 }
1328
Jason Ekstranda85118c2013-06-27 20:17:02 -05001329 wl_resource_set_implementation(resource,
1330 &workspace_manager_implementation,
1331 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001332 wl_list_insert(&shell->workspaces.client_list,
1333 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001334
1335 workspace_manager_send_state(resource,
1336 shell->workspaces.current,
1337 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001338}
1339
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001340static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001341touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1342 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1343{
1344}
1345
1346static void
1347touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1348{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001349 struct weston_touch_move_grab *move =
1350 (struct weston_touch_move_grab *) container_of(
1351 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001352
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001353 if (touch_id == 0)
1354 move->active = 0;
1355
Jonas Ådahl9484b692013-12-02 22:05:03 +01001356 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001357 shell_touch_grab_end(&move->base);
1358 free(move);
1359 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001360}
1361
1362static void
1363touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1364 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1365{
1366 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1367 struct shell_surface *shsurf = move->base.shsurf;
1368 struct weston_surface *es;
1369 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1370 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1371
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001372 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001373 return;
1374
1375 es = shsurf->surface;
1376
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001377 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001378
1379 weston_compositor_schedule_repaint(es->compositor);
1380}
1381
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001382static void
1383touch_move_grab_cancel(struct weston_touch_grab *grab)
1384{
1385 struct weston_touch_move_grab *move =
1386 (struct weston_touch_move_grab *) container_of(
1387 grab, struct shell_touch_grab, grab);
1388
1389 shell_touch_grab_end(&move->base);
1390 free(move);
1391}
1392
Rusty Lynch1084da52013-08-15 09:10:08 -07001393static const struct weston_touch_grab_interface touch_move_grab_interface = {
1394 touch_move_grab_down,
1395 touch_move_grab_up,
1396 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001397 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001398};
1399
1400static int
1401surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1402{
1403 struct weston_touch_move_grab *move;
1404
1405 if (!shsurf)
1406 return -1;
1407
Rafael Antognolli03b16592013-12-03 15:35:42 -02001408 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001409 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001410 if (shsurf->grabbed)
1411 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001412
1413 move = malloc(sizeof *move);
1414 if (!move)
1415 return -1;
1416
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001417 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001419 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001421 seat->touch->grab_y;
1422
1423 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1424 seat->touch);
1425
1426 return 0;
1427}
1428
1429static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001430noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001431{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001432}
1433
1434static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001435move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1436 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001437{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001438 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001439 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001440 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001441 int dx, dy;
1442
1443 weston_pointer_move(pointer, x, y);
1444 dx = wl_fixed_to_int(pointer->x + move->dx);
1445 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001446
1447 if (!shsurf)
1448 return;
1449
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001450 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001451
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001453}
1454
1455static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001456move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001457 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001458{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001459 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1460 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001461 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001462 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001463
Daniel Stone4dbadb12012-05-30 16:31:51 +01001464 if (pointer->button_count == 0 &&
1465 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001466 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001467 free(grab);
1468 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001469}
1470
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001471static void
1472move_grab_cancel(struct weston_pointer_grab *grab)
1473{
1474 struct shell_grab *shell_grab =
1475 container_of(grab, struct shell_grab, grab);
1476
1477 shell_grab_end(shell_grab);
1478 free(grab);
1479}
1480
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001481static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001482 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001483 move_grab_motion,
1484 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001485 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001486};
1487
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001488static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001489surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001490{
1491 struct weston_move_grab *move;
1492
1493 if (!shsurf)
1494 return -1;
1495
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001496 if (shsurf->grabbed)
1497 return 0;
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001498 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001499 return 0;
1500
1501 move = malloc(sizeof *move);
1502 if (!move)
1503 return -1;
1504
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001506 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001508 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001509
1510 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001511 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001512
1513 return 0;
1514}
1515
1516static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001517common_surface_move(struct wl_resource *resource,
1518 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001519{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001520 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001521 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001522 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001523
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001524 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001525 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001526 seat->pointer->button_count > 0 &&
1527 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001529 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001530 (surface_move(shsurf, seat) < 0))
1531 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001532 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001533 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001534 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001536 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001537 (surface_touch_move(shsurf, seat) < 0))
1538 wl_resource_post_no_memory(resource);
1539 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001540}
1541
Rafael Antognollie2a34552013-12-03 15:35:45 -02001542static void
1543shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1544 struct wl_resource *seat_resource, uint32_t serial)
1545{
1546 common_surface_move(resource, seat_resource, serial);
1547}
1548
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001549struct weston_resize_grab {
1550 struct shell_grab base;
1551 uint32_t edges;
1552 int32_t width, height;
1553};
1554
1555static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001556resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1557 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001558{
1559 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001560 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561 struct shell_surface *shsurf = resize->base.shsurf;
1562 int32_t width, height;
1563 wl_fixed_t from_x, from_y;
1564 wl_fixed_t to_x, to_y;
1565
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001566 weston_pointer_move(pointer, x, y);
1567
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001568 if (!shsurf)
1569 return;
1570
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571 weston_view_from_global_fixed(shsurf->view,
1572 pointer->grab_x, pointer->grab_y,
1573 &from_x, &from_y);
1574 weston_view_from_global_fixed(shsurf->view,
1575 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001576
1577 width = resize->width;
1578 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1579 width += wl_fixed_to_int(from_x - to_x);
1580 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1581 width += wl_fixed_to_int(to_x - from_x);
1582 }
1583
1584 height = resize->height;
1585 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1586 height += wl_fixed_to_int(from_y - to_y);
1587 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1588 height += wl_fixed_to_int(to_y - from_y);
1589 }
1590
1591 shsurf->client->send_configure(shsurf->surface,
1592 resize->edges, width, height);
1593}
1594
1595static void
1596send_configure(struct weston_surface *surface,
1597 uint32_t edges, int32_t width, int32_t height)
1598{
1599 struct shell_surface *shsurf = get_shell_surface(surface);
1600
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001601 assert(shsurf);
1602
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001603 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001604 edges, width, height);
1605}
1606
1607static const struct weston_shell_client shell_client = {
1608 send_configure
1609};
1610
1611static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001612resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001613 uint32_t time, uint32_t button, uint32_t state_w)
1614{
1615 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001616 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617 enum wl_pointer_button_state state = state_w;
1618
1619 if (pointer->button_count == 0 &&
1620 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1621 shell_grab_end(&resize->base);
1622 free(grab);
1623 }
1624}
1625
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001626static void
1627resize_grab_cancel(struct weston_pointer_grab *grab)
1628{
1629 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1630
1631 shell_grab_end(&resize->base);
1632 free(grab);
1633}
1634
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001635static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001636 noop_grab_focus,
1637 resize_grab_motion,
1638 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001639 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001640};
1641
Giulio Camuffob8366642013-04-25 13:57:46 +03001642/*
1643 * Returns the bounding box of a surface and all its sub-surfaces,
1644 * in the surface coordinates system. */
1645static void
1646surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1647 int32_t *y, int32_t *w, int32_t *h) {
1648 pixman_region32_t region;
1649 pixman_box32_t *box;
1650 struct weston_subsurface *subsurface;
1651
1652 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 surface->width,
1654 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001655
1656 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1657 pixman_region32_union_rect(&region, &region,
1658 subsurface->position.x,
1659 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660 subsurface->surface->width,
1661 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001662 }
1663
1664 box = pixman_region32_extents(&region);
1665 if (x)
1666 *x = box->x1;
1667 if (y)
1668 *y = box->y1;
1669 if (w)
1670 *w = box->x2 - box->x1;
1671 if (h)
1672 *h = box->y2 - box->y1;
1673
1674 pixman_region32_fini(&region);
1675}
1676
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677static int
1678surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001679 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001680{
1681 struct weston_resize_grab *resize;
1682
Rafael Antognolli03b16592013-12-03 15:35:42 -02001683 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001684 return 0;
1685
1686 if (edges == 0 || edges > 15 ||
1687 (edges & 3) == 3 || (edges & 12) == 12)
1688 return 0;
1689
1690 resize = malloc(sizeof *resize);
1691 if (!resize)
1692 return -1;
1693
1694 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001695 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1696 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001697
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001698 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001700 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701
1702 return 0;
1703}
1704
1705static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001706common_surface_resize(struct wl_resource *resource,
1707 struct wl_resource *seat_resource, uint32_t serial,
1708 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001710 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001711 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001712 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713
Rafael Antognolli03b16592013-12-03 15:35:42 -02001714 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715 return;
1716
Kristian Høgsberge3148752013-05-06 23:19:49 -04001717 if (seat->pointer->button_count == 0 ||
1718 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001719 seat->pointer->focus == NULL)
1720 return;
1721
1722 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1723 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001724 return;
1725
Kristian Høgsberge3148752013-05-06 23:19:49 -04001726 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727 wl_resource_post_no_memory(resource);
1728}
1729
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001730static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001731shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1732 struct wl_resource *seat_resource, uint32_t serial,
1733 uint32_t edges)
1734{
1735 common_surface_resize(resource, seat_resource, serial, edges);
1736}
1737
1738static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001739busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001740{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001741 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001742 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001744 wl_fixed_t sx, sy;
1745
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 view = weston_compositor_pick_view(pointer->seat->compositor,
1747 pointer->x, pointer->y,
1748 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001749
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001750 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1751 shell_grab_end(grab);
1752 free(grab);
1753 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001754}
1755
1756static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001757busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1758 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001759{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001760 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001761}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001762
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001763static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001764busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001765 uint32_t time, uint32_t button, uint32_t state)
1766{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001767 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001768 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001769 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001770
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001771 if (shsurf && button == BTN_LEFT && state) {
1772 activate(shsurf->shell, shsurf->surface, seat);
1773 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001774 } else if (shsurf && button == BTN_RIGHT && state) {
1775 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001776 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001777 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001778}
1779
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001780static void
1781busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1782{
1783 struct shell_grab *grab = (struct shell_grab *) base;
1784
1785 shell_grab_end(grab);
1786 free(grab);
1787}
1788
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001789static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001790 busy_cursor_grab_focus,
1791 busy_cursor_grab_motion,
1792 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001793 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794};
1795
1796static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001797set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001798{
1799 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001800
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001801 if (pointer->grab->interface == &busy_cursor_grab_interface)
1802 return;
1803
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804 grab = malloc(sizeof *grab);
1805 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001806 return;
1807
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001808 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1809 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001810}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001811
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001812static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001813end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001814{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001815 struct shell_grab *grab;
1816 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001817
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001818 wl_list_for_each(seat, &compositor->seat_list, link) {
1819 if (seat->pointer == NULL)
1820 continue;
1821
1822 grab = (struct shell_grab *) seat->pointer->grab;
1823 if (grab->grab.interface == &busy_cursor_grab_interface &&
1824 wl_resource_get_client(grab->shsurf->resource) == client) {
1825 shell_grab_end(grab);
1826 free(grab);
1827 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001828 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001829}
1830
Scott Moreau9521d5e2012-04-19 13:06:17 -06001831static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001832handle_shell_client_destroy(struct wl_listener *listener, void *data);
1833
1834static int
1835xdg_ping_timeout_handler(void *data)
1836{
1837 struct shell_client *sc = data;
1838 struct weston_seat *seat;
1839 struct shell_surface *shsurf;
1840
1841 /* Client is not responding */
1842 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001843 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1844 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1845 continue;
1846 if (seat->pointer->focus->surface->resource == NULL)
1847 continue;
1848
1849 shsurf = get_shell_surface(seat->pointer->focus->surface);
1850 if (shsurf &&
1851 wl_resource_get_client(shsurf->resource) == sc->client)
1852 set_busy_cursor(shsurf, seat->pointer);
1853 }
1854
1855 return 1;
1856}
1857
1858static void
1859handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1860{
1861 struct weston_compositor *compositor = shsurf->shell->compositor;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001862 struct wl_client *client = wl_resource_get_client(shsurf->resource);
1863 struct shell_client *sc;
1864 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001865 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001866
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001867 sc = get_shell_client(client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001868 if (sc->unresponsive) {
1869 xdg_ping_timeout_handler(sc);
1870 return;
1871 }
1872
1873 sc->ping_serial = serial;
1874 loop = wl_display_get_event_loop(compositor->wl_display);
1875 if (sc->ping_timer == NULL)
1876 sc->ping_timer =
1877 wl_event_loop_add_timer(loop,
1878 xdg_ping_timeout_handler, sc);
1879 if (sc->ping_timer == NULL)
1880 return;
1881
1882 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1883
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001884 if (shell_surface_is_xdg_surface(shsurf) ||
1885 shell_surface_is_xdg_popup(shsurf))
1886 xdg_shell_send_ping(sc->resource, serial);
1887 else if (shell_surface_is_wl_shell_surface(shsurf))
1888 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001889}
1890
Scott Moreauff1db4a2012-04-17 19:06:18 -06001891static void
1892ping_handler(struct weston_surface *surface, uint32_t serial)
1893{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001894 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001895
1896 if (!shsurf)
1897 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001898 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001899 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001900 if (shsurf->surface == shsurf->shell->grab_surface)
1901 return;
1902
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001903 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001904}
1905
1906static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001907handle_pointer_focus(struct wl_listener *listener, void *data)
1908{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001909 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001910 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001911 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001912 uint32_t serial;
1913
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001915 return;
1916
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001918 serial = wl_display_next_serial(compositor->wl_display);
1919 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001920}
1921
1922static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001923create_pointer_focus_listener(struct weston_seat *seat)
1924{
1925 struct wl_listener *listener;
1926
Kristian Høgsberge3148752013-05-06 23:19:49 -04001927 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001928 return;
1929
1930 listener = malloc(sizeof *listener);
1931 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001932 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001933}
1934
1935static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001936shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1937{
1938 if (--shsurf->focus_count == 0)
1939 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001940 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001941}
1942
1943static void
1944shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1945{
1946 if (shsurf->focus_count++ == 0)
1947 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001948 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001949}
1950
1951static void
1952handle_keyboard_focus(struct wl_listener *listener, void *data)
1953{
1954 struct weston_keyboard *keyboard = data;
1955 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1956
1957 if (seat->focused_surface) {
1958 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1959 if (shsurf)
1960 shell_surface_lose_keyboard_focus(shsurf);
1961 }
1962
1963 seat->focused_surface = keyboard->focus;
1964
1965 if (seat->focused_surface) {
1966 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1967 if (shsurf)
1968 shell_surface_gain_keyboard_focus(shsurf);
1969 }
1970}
1971
1972static void
1973create_keyboard_focus_listener(struct weston_seat *seat)
1974{
1975 struct wl_listener *listener;
1976
1977 if (!seat->keyboard)
1978 return;
1979
1980 listener = malloc(sizeof *listener);
1981 listener->notify = handle_keyboard_focus;
1982 wl_signal_add(&seat->keyboard->focus_signal, listener);
1983}
1984
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001985static struct shell_client *
1986get_shell_client(struct wl_client *client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02001987{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001988 struct wl_listener *listener;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001989
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001990 listener = wl_client_get_destroy_listener(client,
1991 handle_shell_client_destroy);
1992 if (listener == NULL)
1993 return NULL;
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001994
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001995 return container_of(listener, struct shell_client, destroy_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001996}
1997
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001998static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001999shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002000{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002001 if (sc->ping_serial != serial)
2002 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002003
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002004 sc->unresponsive = 0;
2005 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002006
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002007 if (sc->ping_timer) {
2008 wl_event_source_remove(sc->ping_timer);
2009 sc->ping_timer = NULL;
2010 }
2011
2012}
2013
2014static void
2015shell_surface_pong(struct wl_client *client,
2016 struct wl_resource *resource, uint32_t serial)
2017{
2018 struct shell_client *sc;
2019
2020 sc = get_shell_client(client);
2021 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002022}
2023
2024static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002025set_title(struct shell_surface *shsurf, const char *title)
2026{
2027 free(shsurf->title);
2028 shsurf->title = strdup(title);
2029}
2030
2031static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002032shell_surface_set_title(struct wl_client *client,
2033 struct wl_resource *resource, const char *title)
2034{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002035 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002036
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002037 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002038}
2039
2040static void
2041shell_surface_set_class(struct wl_client *client,
2042 struct wl_resource *resource, const char *class)
2043{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002044 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002045
2046 free(shsurf->class);
2047 shsurf->class = strdup(class);
2048}
2049
Alex Wu4539b082012-03-01 12:57:46 +08002050static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002051restore_output_mode(struct weston_output *output)
2052{
Hardening57388e42013-09-18 23:56:36 +02002053 if (output->current_mode != output->original_mode ||
2054 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002055 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002056 output->original_mode,
2057 output->original_scale,
2058 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002059}
2060
2061static void
2062restore_all_output_modes(struct weston_compositor *compositor)
2063{
2064 struct weston_output *output;
2065
2066 wl_list_for_each(output, &compositor->output_list, link)
2067 restore_output_mode(output);
2068}
2069
Philip Withnallbecb77e2013-11-25 18:01:30 +00002070static int
2071get_output_panel_height(struct desktop_shell *shell,
2072 struct weston_output *output)
2073{
2074 struct weston_view *view;
2075 int panel_height = 0;
2076
2077 if (!output)
2078 return 0;
2079
2080 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2081 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002082 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002083 break;
2084 }
2085 }
2086
2087 return panel_height;
2088}
2089
Philip Withnall07926d92013-11-25 18:01:40 +00002090/* The surface will be inserted into the list immediately after the link
2091 * returned by this function (i.e. will be stacked immediately above the
2092 * returned link). */
2093static struct wl_list *
2094shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2095{
2096 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002097 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002098
2099 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002100 case SHELL_SURFACE_POPUP:
2101 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002102 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002103 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002104 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002105 /* Move the surface to its parent layer so
2106 * that surfaces which are transient for
2107 * fullscreen surfaces don't get hidden by the
2108 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002109
2110 /* TODO: Handle a parent with multiple views */
2111 parent = get_default_view(shsurf->parent);
2112 if (parent)
2113 return parent->layer_link.prev;
2114 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002115 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002116
2117 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002118 return &shsurf->shell->fullscreen_layer.view_list;
2119
Philip Withnall07926d92013-11-25 18:01:40 +00002120 case SHELL_SURFACE_NONE:
2121 default:
2122 /* Go to the fallback, below. */
2123 break;
2124 }
2125
2126 /* Move the surface to a normal workspace layer so that surfaces
2127 * which were previously fullscreen or transient are no longer
2128 * rendered on top. */
2129 ws = get_current_workspace(shsurf->shell);
2130 return &ws->layer.view_list;
2131}
2132
Philip Withnall648a4dd2013-11-25 18:01:44 +00002133static void
2134shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2135{
2136 struct shell_surface *child;
2137
2138 /* Move the child layers to the same workspace as shsurf. They will be
2139 * stacked above shsurf. */
2140 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2141 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2142 weston_view_geometry_dirty(child->view);
2143 wl_list_remove(&child->view->layer_link);
2144 wl_list_insert(shsurf->view->layer_link.prev,
2145 &child->view->layer_link);
2146 weston_view_geometry_dirty(child->view);
2147 weston_surface_damage(child->surface);
2148
2149 /* Recurse. We don’t expect this to recurse very far (if
2150 * at all) because that would imply we have transient
2151 * (or popup) children of transient surfaces, which
2152 * would be unusual. */
2153 shell_surface_update_child_surface_layers(child);
2154 }
2155 }
2156}
2157
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002158/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002159 * geometry to ensure the changes are redrawn.
2160 *
2161 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2162 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002163static void
2164shell_surface_update_layer(struct shell_surface *shsurf)
2165{
2166 struct wl_list *new_layer_link;
2167
2168 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2169
2170 if (new_layer_link == &shsurf->view->layer_link)
2171 return;
2172
2173 weston_view_geometry_dirty(shsurf->view);
2174 wl_list_remove(&shsurf->view->layer_link);
2175 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2176 weston_view_geometry_dirty(shsurf->view);
2177 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002178
2179 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002180}
2181
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002182static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002183shell_surface_set_parent(struct shell_surface *shsurf,
2184 struct weston_surface *parent)
2185{
2186 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002187
2188 wl_list_remove(&shsurf->children_link);
2189 wl_list_init(&shsurf->children_link);
2190
2191 /* Insert into the parent surface’s child list. */
2192 if (parent != NULL) {
2193 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2194 if (parent_shsurf != NULL)
2195 wl_list_insert(&parent_shsurf->children_list,
2196 &shsurf->children_link);
2197 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002198}
2199
2200static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002201shell_surface_set_output(struct shell_surface *shsurf,
2202 struct weston_output *output)
2203{
2204 struct weston_surface *es = shsurf->surface;
2205
2206 /* get the default output, if the client set it as NULL
2207 check whether the ouput is available */
2208 if (output)
2209 shsurf->output = output;
2210 else if (es->output)
2211 shsurf->output = es->output;
2212 else
2213 shsurf->output = get_default_output(es->compositor);
2214}
2215
2216static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002217surface_clear_next_states(struct shell_surface *shsurf)
2218{
2219 shsurf->next_state.maximized = false;
2220 shsurf->next_state.fullscreen = false;
2221
2222 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2223 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2224 shsurf->state_changed = true;
2225}
2226
2227static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002228set_toplevel(struct shell_surface *shsurf)
2229{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002230 shell_surface_set_parent(shsurf, NULL);
2231 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002232 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002233
2234 /* The layer_link is updated in set_surface_type(),
2235 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002236}
2237
2238static void
2239shell_surface_set_toplevel(struct wl_client *client,
2240 struct wl_resource *resource)
2241{
2242 struct shell_surface *surface = wl_resource_get_user_data(resource);
2243
2244 set_toplevel(surface);
2245}
2246
2247static void
2248set_transient(struct shell_surface *shsurf,
2249 struct weston_surface *parent, int x, int y, uint32_t flags)
2250{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002251 assert(parent != NULL);
2252
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002253 shell_surface_set_parent(shsurf, parent);
2254
2255 surface_clear_next_states(shsurf);
2256
Philip Withnallbecb77e2013-11-25 18:01:30 +00002257 shsurf->transient.x = x;
2258 shsurf->transient.y = y;
2259 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002260
Rafael Antognollied207b42013-12-03 15:35:43 -02002261 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002262 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002263 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002264
2265 /* The layer_link is updated in set_surface_type(),
2266 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002267}
2268
2269static void
2270shell_surface_set_transient(struct wl_client *client,
2271 struct wl_resource *resource,
2272 struct wl_resource *parent_resource,
2273 int x, int y, uint32_t flags)
2274{
2275 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2276 struct weston_surface *parent =
2277 wl_resource_get_user_data(parent_resource);
2278
2279 set_transient(shsurf, parent, x, y, flags);
2280}
2281
2282static void
2283set_fullscreen(struct shell_surface *shsurf,
2284 uint32_t method,
2285 uint32_t framerate,
2286 struct weston_output *output)
2287{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002288 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002289
2290 shsurf->fullscreen_output = shsurf->output;
2291 shsurf->fullscreen.type = method;
2292 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002293
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002294 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002295
2296 shsurf->client->send_configure(shsurf->surface, 0,
2297 shsurf->output->width,
2298 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002299
2300 /* The layer_link is updated in set_surface_type(),
2301 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002302}
2303
2304static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002305weston_view_set_initial_position(struct weston_view *view,
2306 struct desktop_shell *shell);
2307
2308static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002309unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002310{
Philip Withnallf85fe842013-11-25 18:01:37 +00002311 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002312 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2313 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002314 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002315 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002316 shsurf->fullscreen_output = NULL;
2317
Alex Wu4539b082012-03-01 12:57:46 +08002318 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2319 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002320
Alex Wu4539b082012-03-01 12:57:46 +08002321 wl_list_remove(&shsurf->fullscreen.transform.link);
2322 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002323
Jason Ekstranda7af7042013-10-12 22:38:11 -05002324 if (shsurf->fullscreen.black_view)
2325 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2326 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002327
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002328 if (shsurf->saved_position_valid)
2329 weston_view_set_position(shsurf->view,
2330 shsurf->saved_x, shsurf->saved_y);
2331 else
2332 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2333
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002334 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002335 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002336 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002337 shsurf->saved_rotation_valid = false;
2338 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002339
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002340 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002341}
2342
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002343static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002344shell_surface_set_fullscreen(struct wl_client *client,
2345 struct wl_resource *resource,
2346 uint32_t method,
2347 uint32_t framerate,
2348 struct wl_resource *output_resource)
2349{
2350 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2351 struct weston_output *output;
2352
2353 if (output_resource)
2354 output = wl_resource_get_user_data(output_resource);
2355 else
2356 output = NULL;
2357
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002358 shell_surface_set_parent(shsurf, NULL);
2359
Rafael Antognolli03b16592013-12-03 15:35:42 -02002360 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002361 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002362
2363 shsurf->next_state.fullscreen = true;
2364 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002365}
2366
2367static void
2368set_popup(struct shell_surface *shsurf,
2369 struct weston_surface *parent,
2370 struct weston_seat *seat,
2371 uint32_t serial,
2372 int32_t x,
2373 int32_t y)
2374{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002375 assert(parent != NULL);
2376
Philip Withnallbecb77e2013-11-25 18:01:30 +00002377 shsurf->popup.shseat = get_shell_seat(seat);
2378 shsurf->popup.serial = serial;
2379 shsurf->popup.x = x;
2380 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002381
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002382 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002383}
2384
2385static void
2386shell_surface_set_popup(struct wl_client *client,
2387 struct wl_resource *resource,
2388 struct wl_resource *seat_resource,
2389 uint32_t serial,
2390 struct wl_resource *parent_resource,
2391 int32_t x, int32_t y, uint32_t flags)
2392{
2393 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002394 struct weston_surface *parent =
2395 wl_resource_get_user_data(parent_resource);
2396
2397 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002398
Rafael Antognolli03b16592013-12-03 15:35:42 -02002399 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002400 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002401 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002402 wl_resource_get_user_data(seat_resource),
2403 serial, x, y);
2404}
2405
2406static void
2407set_maximized(struct shell_surface *shsurf,
2408 struct weston_output *output)
2409{
2410 struct desktop_shell *shell;
2411 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002412
Philip Withnall352e7ed2013-11-25 18:01:35 +00002413 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002414
2415 shell = shell_surface_get_shell(shsurf);
2416 panel_height = get_output_panel_height(shell, shsurf->output);
2417 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2418
2419 shsurf->client->send_configure(shsurf->surface, edges,
2420 shsurf->output->width,
2421 shsurf->output->height - panel_height);
2422
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002423 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002424}
2425
2426static void
2427unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002428{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002429 /* undo all maximized things here */
2430 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002431
2432 if (shsurf->saved_position_valid)
2433 weston_view_set_position(shsurf->view,
2434 shsurf->saved_x, shsurf->saved_y);
2435 else
2436 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002437
2438 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002439 wl_list_insert(&shsurf->view->geometry.transformation_list,
2440 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002441 shsurf->saved_rotation_valid = false;
2442 }
2443
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002444 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002445}
2446
Philip Withnallbecb77e2013-11-25 18:01:30 +00002447static void
2448shell_surface_set_maximized(struct wl_client *client,
2449 struct wl_resource *resource,
2450 struct wl_resource *output_resource)
2451{
2452 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2453 struct weston_output *output;
2454
2455 if (output_resource)
2456 output = wl_resource_get_user_data(output_resource);
2457 else
2458 output = NULL;
2459
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002460 shell_surface_set_parent(shsurf, NULL);
2461
Rafael Antognolli03b16592013-12-03 15:35:42 -02002462 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002463 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002464
2465 shsurf->next_state.maximized = true;
2466 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002467}
2468
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002469/* This is only ever called from set_surface_type(), so there’s no need to
2470 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002471static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002472reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002473{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002474 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002475 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002476 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002477 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002478
Pekka Paalanen98262232011-12-01 10:42:22 +02002479 return 0;
2480}
2481
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002482static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002483set_full_output(struct shell_surface *shsurf)
2484{
2485 shsurf->saved_x = shsurf->view->geometry.x;
2486 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002487 shsurf->saved_width = shsurf->surface->width;
2488 shsurf->saved_height = shsurf->surface->height;
2489 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002490 shsurf->saved_position_valid = true;
2491
2492 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2493 wl_list_remove(&shsurf->rotation.transform.link);
2494 wl_list_init(&shsurf->rotation.transform.link);
2495 weston_view_geometry_dirty(shsurf->view);
2496 shsurf->saved_rotation_valid = true;
2497 }
2498}
2499
2500static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002501set_surface_type(struct shell_surface *shsurf)
2502{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002503 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002504 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002505
Philip Withnallbecb77e2013-11-25 18:01:30 +00002506 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002507
Rafael Antognolli03b16592013-12-03 15:35:42 -02002508 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002509 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002510
2511 switch (shsurf->type) {
2512 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002513 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002514 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002515 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002516 weston_view_set_position(shsurf->view,
2517 pev->geometry.x + shsurf->transient.x,
2518 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002519 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002520 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002521
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002522 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002523 weston_view_set_position(shsurf->view, shsurf->transient.x,
2524 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002525 break;
2526
Philip Withnall0f640e22013-11-25 18:01:31 +00002527 case SHELL_SURFACE_POPUP:
2528 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002529 default:
2530 break;
2531 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002532
2533 /* Update the surface’s layer. */
2534 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002535}
2536
Tiago Vignattibe143262012-04-16 17:31:41 +03002537static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002538shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002539{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002540 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002541}
2542
Alex Wu21858432012-04-01 20:13:08 +08002543static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002544black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002545
Jason Ekstranda7af7042013-10-12 22:38:11 -05002546static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002547create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002548 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002549 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002550{
2551 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002552 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002553
2554 surface = weston_surface_create(ec);
2555 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002556 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002557 return NULL;
2558 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002559 view = weston_view_create(surface);
2560 if (surface == NULL) {
2561 weston_log("no memory\n");
2562 weston_surface_destroy(surface);
2563 return NULL;
2564 }
Alex Wu4539b082012-03-01 12:57:46 +08002565
Alex Wu21858432012-04-01 20:13:08 +08002566 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002567 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002568 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002569 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002570 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002571 pixman_region32_fini(&surface->input);
2572 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002573
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002574 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002575 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002576
2577 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002578}
2579
Philip Withnalled8f1a92013-11-25 18:01:43 +00002580static void
2581shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2582{
2583 struct weston_output *output = shsurf->fullscreen_output;
2584
Rafael Antognolli03b16592013-12-03 15:35:42 -02002585 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002586
2587 if (!shsurf->fullscreen.black_view)
2588 shsurf->fullscreen.black_view =
2589 create_black_surface(shsurf->surface->compositor,
2590 shsurf->surface,
2591 output->x, output->y,
2592 output->width,
2593 output->height);
2594
2595 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2596 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2597 wl_list_insert(&shsurf->view->layer_link,
2598 &shsurf->fullscreen.black_view->layer_link);
2599 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2600 weston_surface_damage(shsurf->surface);
2601}
2602
Alex Wu4539b082012-03-01 12:57:46 +08002603/* Create black surface and append it to the associated fullscreen surface.
2604 * Handle size dismatch and positioning according to the method. */
2605static void
2606shell_configure_fullscreen(struct shell_surface *shsurf)
2607{
2608 struct weston_output *output = shsurf->fullscreen_output;
2609 struct weston_surface *surface = shsurf->surface;
2610 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002611 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002612 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002613
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002614 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2615 restore_output_mode(output);
2616
Philip Withnalled8f1a92013-11-25 18:01:43 +00002617 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002618
Jason Ekstranda7af7042013-10-12 22:38:11 -05002619 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002620 &surf_width, &surf_height);
2621
Alex Wu4539b082012-03-01 12:57:46 +08002622 switch (shsurf->fullscreen.type) {
2623 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002624 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002625 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002626 break;
2627 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002628 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002629 if (output->width == surf_width &&
2630 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002631 weston_view_set_position(shsurf->view,
2632 output->x - surf_x,
2633 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002634 break;
2635 }
2636
Alex Wu4539b082012-03-01 12:57:46 +08002637 matrix = &shsurf->fullscreen.transform.matrix;
2638 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002639
Scott Moreau1bad5db2012-08-18 01:04:05 -06002640 output_aspect = (float) output->width /
2641 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002642 /* XXX: Use surf_width and surf_height here? */
2643 surface_aspect = (float) surface->width /
2644 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002645 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002646 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002647 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002648 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002649 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002650 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002651
Alex Wu4539b082012-03-01 12:57:46 +08002652 weston_matrix_scale(matrix, scale, scale, 1);
2653 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002654 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002655 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002656 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2657 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002658 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002659
Alex Wu4539b082012-03-01 12:57:46 +08002660 break;
2661 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002662 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002663 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002664 surf_width * surface->buffer_viewport.scale,
2665 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002666 shsurf->fullscreen.framerate};
2667
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002668 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002669 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002670 weston_view_set_position(shsurf->view,
2671 output->x - surf_x,
2672 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002673 shsurf->fullscreen.black_view->surface->width = output->width;
2674 shsurf->fullscreen.black_view->surface->height = output->height;
2675 weston_view_set_position(shsurf->fullscreen.black_view,
2676 output->x - surf_x,
2677 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002678 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002679 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002680 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002681 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002682 }
Alex Wubd3354b2012-04-17 17:20:49 +08002683 }
Alex Wu4539b082012-03-01 12:57:46 +08002684 break;
2685 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002686 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002687 break;
2688 default:
2689 break;
2690 }
2691}
2692
Alex Wu4539b082012-03-01 12:57:46 +08002693static void
2694shell_map_fullscreen(struct shell_surface *shsurf)
2695{
Alex Wubd3354b2012-04-17 17:20:49 +08002696 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002697}
2698
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002699static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002700set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2701{
2702 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002703 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002704 shsurf->transient.x = x;
2705 shsurf->transient.y = y;
2706 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002707
2708 shell_surface_set_parent(shsurf, NULL);
2709
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002710 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002711 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002712}
2713
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002714static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002715
2716static void
2717destroy_shell_seat(struct wl_listener *listener, void *data)
2718{
2719 struct shell_seat *shseat =
2720 container_of(listener,
2721 struct shell_seat, seat_destroy_listener);
2722 struct shell_surface *shsurf, *prev = NULL;
2723
2724 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002725 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002726 shseat->popup_grab.client = NULL;
2727
2728 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2729 shsurf->popup.shseat = NULL;
2730 if (prev) {
2731 wl_list_init(&prev->popup.grab_link);
2732 }
2733 prev = shsurf;
2734 }
2735 wl_list_init(&prev->popup.grab_link);
2736 }
2737
2738 wl_list_remove(&shseat->seat_destroy_listener.link);
2739 free(shseat);
2740}
2741
2742static struct shell_seat *
2743create_shell_seat(struct weston_seat *seat)
2744{
2745 struct shell_seat *shseat;
2746
2747 shseat = calloc(1, sizeof *shseat);
2748 if (!shseat) {
2749 weston_log("no memory to allocate shell seat\n");
2750 return NULL;
2751 }
2752
2753 shseat->seat = seat;
2754 wl_list_init(&shseat->popup_grab.surfaces_list);
2755
2756 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2757 wl_signal_add(&seat->destroy_signal,
2758 &shseat->seat_destroy_listener);
2759
2760 return shseat;
2761}
2762
2763static struct shell_seat *
2764get_shell_seat(struct weston_seat *seat)
2765{
2766 struct wl_listener *listener;
2767
2768 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2769 if (listener == NULL)
2770 return create_shell_seat(seat);
2771
2772 return container_of(listener,
2773 struct shell_seat, seat_destroy_listener);
2774}
2775
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002776static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002777popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002778{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002779 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002780 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002781 struct shell_seat *shseat =
2782 container_of(grab, struct shell_seat, popup_grab.grab);
2783 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002784 wl_fixed_t sx, sy;
2785
Jason Ekstranda7af7042013-10-12 22:38:11 -05002786 view = weston_compositor_pick_view(pointer->seat->compositor,
2787 pointer->x, pointer->y,
2788 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002789
Jason Ekstranda7af7042013-10-12 22:38:11 -05002790 if (view && view->surface->resource &&
2791 wl_resource_get_client(view->surface->resource) == client) {
2792 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002793 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002794 weston_pointer_set_focus(pointer, NULL,
2795 wl_fixed_from_int(0),
2796 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002797 }
2798}
2799
2800static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002801popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2802 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002803{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002804 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002805 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002806 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002807
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002808 weston_pointer_move(pointer, x, y);
2809
Neil Roberts96d790e2013-09-19 17:32:00 +01002810 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002811 weston_view_from_global_fixed(pointer->focus,
2812 pointer->x, pointer->y,
2813 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002814 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002815 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002816}
2817
2818static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002819popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002820 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002821{
2822 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002823 struct shell_seat *shseat =
2824 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002825 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002826 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002827 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002828 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002829
Neil Roberts96d790e2013-09-19 17:32:00 +01002830 resource_list = &grab->pointer->focus_resource_list;
2831 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002832 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002833 wl_resource_for_each(resource, resource_list) {
2834 wl_pointer_send_button(resource, serial,
2835 time, button, state);
2836 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002837 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002838 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002839 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002840 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002841 }
2842
Daniel Stone4dbadb12012-05-30 16:31:51 +01002843 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002844 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002845}
2846
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002847static void
2848popup_grab_cancel(struct weston_pointer_grab *grab)
2849{
2850 popup_grab_end(grab->pointer);
2851}
2852
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002853static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002854 popup_grab_focus,
2855 popup_grab_motion,
2856 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002857 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002858};
2859
2860static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002861shell_surface_send_popup_done(struct shell_surface *shsurf)
2862{
2863 if (shell_surface_is_wl_shell_surface(shsurf))
2864 wl_shell_surface_send_popup_done(shsurf->resource);
2865 else if (shell_surface_is_xdg_popup(shsurf))
2866 xdg_popup_send_popup_done(shsurf->resource,
2867 shsurf->popup.serial);
2868}
2869
2870static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002871popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002872{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002873 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002874 struct shell_seat *shseat =
2875 container_of(grab, struct shell_seat, popup_grab.grab);
2876 struct shell_surface *shsurf;
2877 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002878
2879 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002880 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002881 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002882 shseat->popup_grab.grab.interface = NULL;
2883 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002884 /* Send the popup_done event to all the popups open */
2885 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002886 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002887 shsurf->popup.shseat = NULL;
2888 if (prev) {
2889 wl_list_init(&prev->popup.grab_link);
2890 }
2891 prev = shsurf;
2892 }
2893 wl_list_init(&prev->popup.grab_link);
2894 wl_list_init(&shseat->popup_grab.surfaces_list);
2895 }
2896}
2897
2898static void
2899add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2900{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002901 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002902
2903 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002904 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002905 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002906 /* We must make sure here that this popup was opened after
2907 * a mouse press, and not just by moving around with other
2908 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002909 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002910 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002911
Rob Bradforddfe31052013-06-26 19:49:11 +01002912 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002913 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002914 } else {
2915 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002916 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002917}
2918
2919static void
2920remove_popup_grab(struct shell_surface *shsurf)
2921{
2922 struct shell_seat *shseat = shsurf->popup.shseat;
2923
2924 wl_list_remove(&shsurf->popup.grab_link);
2925 wl_list_init(&shsurf->popup.grab_link);
2926 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002927 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002928 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002929 }
2930}
2931
2932static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002933shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002934{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002935 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002936 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002937
Jason Ekstranda7af7042013-10-12 22:38:11 -05002938 shsurf->surface->output = parent_view->output;
2939 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002940
Jason Ekstranda7af7042013-10-12 22:38:11 -05002941 weston_view_set_transform_parent(shsurf->view, parent_view);
2942 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2943 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002944
Kristian Høgsberge3148752013-05-06 23:19:49 -04002945 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002946 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002947 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002948 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002949 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002950 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002951}
2952
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002953static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002954 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002955 shell_surface_move,
2956 shell_surface_resize,
2957 shell_surface_set_toplevel,
2958 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002959 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002960 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002961 shell_surface_set_maximized,
2962 shell_surface_set_title,
2963 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002964};
2965
2966static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002967destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002968{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002969 struct shell_surface *child, *next;
2970
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002971 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2972
Giulio Camuffo5085a752013-03-25 21:42:45 +01002973 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2974 remove_popup_grab(shsurf);
2975 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002976
Alex Wubd3354b2012-04-17 17:20:49 +08002977 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002978 shell_surface_is_top_fullscreen(shsurf))
2979 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002980
Jason Ekstranda7af7042013-10-12 22:38:11 -05002981 if (shsurf->fullscreen.black_view)
2982 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002983
Alex Wubd3354b2012-04-17 17:20:49 +08002984 /* As destroy_resource() use wl_list_for_each_safe(),
2985 * we can always remove the listener.
2986 */
2987 wl_list_remove(&shsurf->surface_destroy_listener.link);
2988 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07002989 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002990
Jason Ekstranda7af7042013-10-12 22:38:11 -05002991 weston_view_destroy(shsurf->view);
2992
Philip Withnall648a4dd2013-11-25 18:01:44 +00002993 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01002994 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
2995 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002996
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002997 wl_list_remove(&shsurf->link);
2998 free(shsurf);
2999}
3000
3001static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003002shell_destroy_shell_surface(struct wl_resource *resource)
3003{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003004 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003005
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003006 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003007}
3008
3009static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003010shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003011{
3012 struct shell_surface *shsurf = container_of(listener,
3013 struct shell_surface,
3014 surface_destroy_listener);
3015
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003016 if (shsurf->resource)
3017 wl_resource_destroy(shsurf->resource);
3018 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003019 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003020}
3021
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003022static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003023fade_out_done(struct weston_view_animation *animation, void *data)
3024{
3025 struct shell_surface *shsurf = data;
3026
3027 weston_surface_destroy(shsurf->surface);
3028}
3029
3030static void
3031handle_resource_destroy(struct wl_listener *listener, void *data)
3032{
3033 struct shell_surface *shsurf =
3034 container_of(listener, struct shell_surface,
3035 resource_destroy_listener);
3036
3037 shsurf->surface->ref_count++;
3038
3039 pixman_region32_fini(&shsurf->surface->pending.input);
3040 pixman_region32_init(&shsurf->surface->pending.input);
3041 pixman_region32_fini(&shsurf->surface->input);
3042 pixman_region32_init(&shsurf->surface->input);
3043 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0, fade_out_done, shsurf);
3044}
3045
3046static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003047shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003048
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003049struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003050get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003051{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003052 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003053 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003054 else
3055 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003056}
3057
Rafael Antognollie2a34552013-12-03 15:35:45 -02003058static struct shell_surface *
3059create_common_surface(void *shell, struct weston_surface *surface,
3060 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003061{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003062 struct shell_surface *shsurf;
3063
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003064 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003065 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003066 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003067 }
3068
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003069 shsurf = calloc(1, sizeof *shsurf);
3070 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003071 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003072 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003073 }
3074
Jason Ekstranda7af7042013-10-12 22:38:11 -05003075 shsurf->view = weston_view_create(surface);
3076 if (!shsurf->view) {
3077 weston_log("no memory to allocate shell surface\n");
3078 free(shsurf);
3079 return NULL;
3080 }
3081
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003082 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003083 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003084
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003085 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3086 wl_resource_add_destroy_listener(surface->resource,
3087 &shsurf->resource_destroy_listener);
3088
Tiago Vignattibc052c92012-04-19 16:18:18 +03003089 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003090 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003091 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003092 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003093 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003094 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003095 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3096 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003097 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003098 wl_list_init(&shsurf->fullscreen.transform.link);
3099
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003100 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003101 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003102 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003103 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003104
3105 /* init link so its safe to always remove it in destroy_shell_surface */
3106 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003107 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003108
Pekka Paalanen460099f2012-01-20 16:48:25 +02003109 /* empty when not in use */
3110 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003111 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003112
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003113 wl_list_init(&shsurf->workspace_transform.link);
3114
Philip Withnall648a4dd2013-11-25 18:01:44 +00003115 wl_list_init(&shsurf->children_link);
3116 wl_list_init(&shsurf->children_list);
3117 shsurf->parent = NULL;
3118
Pekka Paalanen98262232011-12-01 10:42:22 +02003119 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003120
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003121 shsurf->client = client;
3122
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003123 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003124}
3125
Rafael Antognollie2a34552013-12-03 15:35:45 -02003126static struct shell_surface *
3127create_shell_surface(void *shell, struct weston_surface *surface,
3128 const struct weston_shell_client *client)
3129{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003130 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003131}
3132
Jason Ekstranda7af7042013-10-12 22:38:11 -05003133static struct weston_view *
3134get_primary_view(void *shell, struct shell_surface *shsurf)
3135{
3136 return shsurf->view;
3137}
3138
Tiago Vignattibc052c92012-04-19 16:18:18 +03003139static void
3140shell_get_shell_surface(struct wl_client *client,
3141 struct wl_resource *resource,
3142 uint32_t id,
3143 struct wl_resource *surface_resource)
3144{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003145 struct weston_surface *surface =
3146 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003147 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003148 struct shell_surface *shsurf;
3149
3150 if (get_shell_surface(surface)) {
3151 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003152 WL_DISPLAY_ERROR_INVALID_OBJECT,
3153 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003154 return;
3155 }
3156
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003157 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003158 if (!shsurf) {
3159 wl_resource_post_error(surface_resource,
3160 WL_DISPLAY_ERROR_INVALID_OBJECT,
3161 "surface->configure already set");
3162 return;
3163 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003164
Jason Ekstranda85118c2013-06-27 20:17:02 -05003165 shsurf->resource =
3166 wl_resource_create(client,
3167 &wl_shell_surface_interface, 1, id);
3168 wl_resource_set_implementation(shsurf->resource,
3169 &shell_surface_implementation,
3170 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003171}
3172
Rafael Antognollie2a34552013-12-03 15:35:45 -02003173static bool
3174shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3175{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003176 /* A shell surface without a resource is created from xwayland
3177 * and is considered a wl_shell surface for now. */
3178
3179 return shsurf->resource == NULL ||
3180 wl_resource_instance_of(shsurf->resource,
3181 &wl_shell_surface_interface,
3182 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003183}
3184
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003185static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003186 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003187};
3188
Rafael Antognollie2a34552013-12-03 15:35:45 -02003189/****************************
3190 * xdg-shell implementation */
3191
3192static void
3193xdg_surface_destroy(struct wl_client *client,
3194 struct wl_resource *resource)
3195{
3196 wl_resource_destroy(resource);
3197}
3198
3199static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003200xdg_surface_set_transient_for(struct wl_client *client,
3201 struct wl_resource *resource,
3202 struct wl_resource *parent_resource)
3203{
3204 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3205 struct weston_surface *parent;
3206
3207 if (parent_resource)
3208 parent = wl_resource_get_user_data(parent_resource);
3209 else
3210 parent = NULL;
3211
3212 shell_surface_set_parent(shsurf, parent);
3213}
3214
3215static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003216xdg_surface_set_margin(struct wl_client *client,
3217 struct wl_resource *resource,
3218 int32_t left,
3219 int32_t right,
3220 int32_t top,
3221 int32_t bottom)
3222{
3223 /* Do nothing, Weston doesn't try to constrain or place
3224 * surfaces in any special manner... */
3225}
3226
3227static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003228xdg_surface_set_app_id(struct wl_client *client,
3229 struct wl_resource *resource,
3230 const char *app_id)
3231{
3232 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3233
3234 free(shsurf->class);
3235 shsurf->class = strdup(app_id);
3236}
3237
3238static void
3239xdg_surface_set_title(struct wl_client *client,
3240 struct wl_resource *resource, const char *title)
3241{
3242 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3243
3244 set_title(shsurf, title);
3245}
3246
3247static void
3248xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3249 struct wl_resource *seat_resource, uint32_t serial)
3250{
3251 common_surface_move(resource, seat_resource, serial);
3252}
3253
3254static void
3255xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3256 struct wl_resource *seat_resource, uint32_t serial,
3257 uint32_t edges)
3258{
3259 common_surface_resize(resource, seat_resource, serial, edges);
3260}
3261
3262static void
3263xdg_surface_set_output(struct wl_client *client,
3264 struct wl_resource *resource,
3265 struct wl_resource *output_resource)
3266{
3267 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3268 struct weston_output *output;
3269
3270 if (output_resource)
3271 output = wl_resource_get_user_data(output_resource);
3272 else
3273 output = NULL;
3274
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003275 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003276}
3277
3278static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003279xdg_surface_change_state(struct shell_surface *shsurf,
3280 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003281{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003282 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003283 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003284
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003285 switch (state) {
3286 case XDG_SURFACE_STATE_MAXIMIZED:
3287 shsurf->requested_state.maximized = value;
3288 if (value)
3289 set_maximized(shsurf, NULL);
3290 break;
3291 case XDG_SURFACE_STATE_FULLSCREEN:
3292 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003293
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003294 if (value)
3295 set_fullscreen(shsurf,
3296 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3297 0, shsurf->recommended_output);
3298 break;
3299 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003300}
3301
3302static void
3303xdg_surface_request_change_state(struct wl_client *client,
3304 struct wl_resource *resource,
3305 uint32_t state,
3306 uint32_t value,
3307 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003308{
3309 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003310
3311 /* The client can't know what the current state is, so we need
3312 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003313
3314 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3315 return;
3316
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003317 switch (state) {
3318 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003319 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003320 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003321 default:
3322 /* send error? ignore? send change state with value 0? */
3323 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003324 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003325
3326 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003327}
3328
3329static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003330xdg_surface_ack_change_state(struct wl_client *client,
3331 struct wl_resource *resource,
3332 uint32_t state,
3333 uint32_t value,
3334 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003335{
3336 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3337
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003338 if (shsurf->state_requested) {
3339 shsurf->next_state = shsurf->requested_state;
3340 shsurf->state_changed = true;
3341 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003342 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003343}
3344
3345static const struct xdg_surface_interface xdg_surface_implementation = {
3346 xdg_surface_destroy,
3347 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003348 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003349 xdg_surface_set_title,
3350 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003351 xdg_surface_move,
3352 xdg_surface_resize,
3353 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003354 xdg_surface_request_change_state,
3355 xdg_surface_ack_change_state,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003356 NULL /* set_minimized */
3357};
3358
3359static void
3360xdg_send_configure(struct weston_surface *surface,
3361 uint32_t edges, int32_t width, int32_t height)
3362{
3363 struct shell_surface *shsurf = get_shell_surface(surface);
3364
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003365 assert(shsurf);
3366
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003367 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003368}
3369
3370static const struct weston_shell_client xdg_client = {
3371 xdg_send_configure
3372};
3373
3374static void
3375xdg_use_unstable_version(struct wl_client *client,
3376 struct wl_resource *resource,
3377 int32_t version)
3378{
3379 if (version > 1) {
3380 wl_resource_post_error(resource,
3381 1,
3382 "xdg-shell:: version not implemented yet.");
3383 return;
3384 }
3385}
3386
3387static struct shell_surface *
3388create_xdg_surface(void *shell, struct weston_surface *surface,
3389 const struct weston_shell_client *client)
3390{
3391 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003392
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003393 shsurf = create_common_surface(shell, surface, client);
3394 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003395
3396 return shsurf;
3397}
3398
3399static void
3400xdg_get_xdg_surface(struct wl_client *client,
3401 struct wl_resource *resource,
3402 uint32_t id,
3403 struct wl_resource *surface_resource)
3404{
3405 struct weston_surface *surface =
3406 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003407 struct shell_client *sc = wl_resource_get_user_data(resource);
3408 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003409 struct shell_surface *shsurf;
3410
3411 if (get_shell_surface(surface)) {
3412 wl_resource_post_error(surface_resource,
3413 WL_DISPLAY_ERROR_INVALID_OBJECT,
3414 "desktop_shell::get_shell_surface already requested");
3415 return;
3416 }
3417
3418 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3419 if (!shsurf) {
3420 wl_resource_post_error(surface_resource,
3421 WL_DISPLAY_ERROR_INVALID_OBJECT,
3422 "surface->configure already set");
3423 return;
3424 }
3425
3426 shsurf->resource =
3427 wl_resource_create(client,
3428 &xdg_surface_interface, 1, id);
3429 wl_resource_set_implementation(shsurf->resource,
3430 &xdg_surface_implementation,
3431 shsurf, shell_destroy_shell_surface);
3432}
3433
3434static bool
3435shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3436{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003437 return shsurf->resource &&
3438 wl_resource_instance_of(shsurf->resource,
3439 &xdg_surface_interface,
3440 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003441}
3442
3443/* xdg-popup implementation */
3444
3445static void
3446xdg_popup_destroy(struct wl_client *client,
3447 struct wl_resource *resource)
3448{
3449 wl_resource_destroy(resource);
3450}
3451
Rafael Antognollie2a34552013-12-03 15:35:45 -02003452static const struct xdg_popup_interface xdg_popup_implementation = {
3453 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003454};
3455
3456static void
3457xdg_popup_send_configure(struct weston_surface *surface,
3458 uint32_t edges, int32_t width, int32_t height)
3459{
3460}
3461
3462static const struct weston_shell_client xdg_popup_client = {
3463 xdg_popup_send_configure
3464};
3465
3466static struct shell_surface *
3467create_xdg_popup(void *shell, struct weston_surface *surface,
3468 const struct weston_shell_client *client,
3469 struct weston_surface *parent,
3470 struct shell_seat *seat,
3471 uint32_t serial,
3472 int32_t x, int32_t y)
3473{
3474 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003475
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003476 shsurf = create_common_surface(shell, surface, client);
3477 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003478 shsurf->popup.shseat = seat;
3479 shsurf->popup.serial = serial;
3480 shsurf->popup.x = x;
3481 shsurf->popup.y = y;
3482 shell_surface_set_parent(shsurf, parent);
3483
3484 return shsurf;
3485}
3486
3487static void
3488xdg_get_xdg_popup(struct wl_client *client,
3489 struct wl_resource *resource,
3490 uint32_t id,
3491 struct wl_resource *surface_resource,
3492 struct wl_resource *parent_resource,
3493 struct wl_resource *seat_resource,
3494 uint32_t serial,
3495 int32_t x, int32_t y, uint32_t flags)
3496{
3497 struct weston_surface *surface =
3498 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003499 struct shell_client *sc = wl_resource_get_user_data(resource);
3500 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003501 struct shell_surface *shsurf;
3502 struct weston_surface *parent;
3503 struct shell_seat *seat;
3504
3505 if (get_shell_surface(surface)) {
3506 wl_resource_post_error(surface_resource,
3507 WL_DISPLAY_ERROR_INVALID_OBJECT,
3508 "desktop_shell::get_shell_surface already requested");
3509 return;
3510 }
3511
3512 if (!parent_resource) {
3513 wl_resource_post_error(surface_resource,
3514 WL_DISPLAY_ERROR_INVALID_OBJECT,
3515 "xdg_shell::get_xdg_popup requires a parent shell surface");
3516 }
3517
3518 parent = wl_resource_get_user_data(parent_resource);
3519 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3520
3521 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3522 parent, seat, serial, x, y);
3523 if (!shsurf) {
3524 wl_resource_post_error(surface_resource,
3525 WL_DISPLAY_ERROR_INVALID_OBJECT,
3526 "surface->configure already set");
3527 return;
3528 }
3529
3530 shsurf->resource =
3531 wl_resource_create(client,
3532 &xdg_popup_interface, 1, id);
3533 wl_resource_set_implementation(shsurf->resource,
3534 &xdg_popup_implementation,
3535 shsurf, shell_destroy_shell_surface);
3536}
3537
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003538static void
3539xdg_pong(struct wl_client *client,
3540 struct wl_resource *resource, uint32_t serial)
3541{
3542 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003543
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003544 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003545}
3546
Rafael Antognollie2a34552013-12-03 15:35:45 -02003547static bool
3548shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3549{
3550 return wl_resource_instance_of(shsurf->resource,
3551 &xdg_popup_interface,
3552 &xdg_popup_implementation);
3553}
3554
3555static const struct xdg_shell_interface xdg_implementation = {
3556 xdg_use_unstable_version,
3557 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003558 xdg_get_xdg_popup,
3559 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003560};
3561
3562static int
3563xdg_shell_unversioned_dispatch(const void *implementation,
3564 void *_target, uint32_t opcode,
3565 const struct wl_message *message,
3566 union wl_argument *args)
3567{
3568 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003569 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003570
3571 if (opcode != 0) {
3572 wl_resource_post_error(resource,
3573 WL_DISPLAY_ERROR_INVALID_OBJECT,
3574 "must call use_unstable_version first");
3575 return 0;
3576 }
3577
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003578#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003579
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003580 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3581 "shell implementation doesn't match protocol version");
3582
Rafael Antognollie2a34552013-12-03 15:35:45 -02003583 if (args[0].i != XDG_SERVER_VERSION) {
3584 wl_resource_post_error(resource,
3585 WL_DISPLAY_ERROR_INVALID_OBJECT,
3586 "incompatible version, server is %d "
3587 "client wants %d",
3588 XDG_SERVER_VERSION, args[0].i);
3589 return 0;
3590 }
3591
3592 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003593 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003594
3595 return 1;
3596}
3597
3598/* end of xdg-shell implementation */
3599/***********************************/
3600
Kristian Høgsberg07937562011-04-12 17:25:42 -04003601static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003602shell_fade(struct desktop_shell *shell, enum fade_type type);
3603
3604static int
3605screensaver_timeout(void *data)
3606{
3607 struct desktop_shell *shell = data;
3608
3609 shell_fade(shell, FADE_OUT);
3610
3611 return 1;
3612}
3613
3614static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003615handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003616{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003617 struct desktop_shell *shell =
3618 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003619
Pekka Paalanen18027e52011-12-02 16:31:49 +02003620 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003621
3622 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003623 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003624}
3625
3626static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003627launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003628{
3629 if (shell->screensaver.binding)
3630 return;
3631
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003632 if (!shell->screensaver.path) {
3633 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003634 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003635 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003636
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003637 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003638 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003639 return;
3640 }
3641
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003642 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003643 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003644 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003645 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003646}
3647
3648static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003649terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003650{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003651 if (shell->screensaver.process.pid == 0)
3652 return;
3653
3654 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003655}
3656
3657static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003658configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003659{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003660 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003661
Jason Ekstranda7af7042013-10-12 22:38:11 -05003662 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3663 if (v->output == ev->output && v != ev) {
3664 weston_view_unmap(v);
3665 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003666 }
3667 }
3668
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003669 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003670
Jason Ekstranda7af7042013-10-12 22:38:11 -05003671 if (wl_list_empty(&ev->layer_link)) {
3672 wl_list_insert(&layer->view_list, &ev->layer_link);
3673 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003674 }
3675}
3676
3677static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003678background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003679{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003680 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003681 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003682
Jason Ekstranda7af7042013-10-12 22:38:11 -05003683 view = container_of(es->views.next, struct weston_view, surface_link);
3684
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003685 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003686}
3687
3688static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003689desktop_shell_set_background(struct wl_client *client,
3690 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003691 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003692 struct wl_resource *surface_resource)
3693{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003694 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003695 struct weston_surface *surface =
3696 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003697 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003698
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003699 if (surface->configure) {
3700 wl_resource_post_error(surface_resource,
3701 WL_DISPLAY_ERROR_INVALID_OBJECT,
3702 "surface role already assigned");
3703 return;
3704 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003705
Jason Ekstranda7af7042013-10-12 22:38:11 -05003706 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3707 weston_view_destroy(view);
3708 view = weston_view_create(surface);
3709
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003710 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003711 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003712 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003713 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003714 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003715 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003716 surface->output->width,
3717 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003718}
3719
3720static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003721panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003722{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003723 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003724 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003725
Jason Ekstranda7af7042013-10-12 22:38:11 -05003726 view = container_of(es->views.next, struct weston_view, surface_link);
3727
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003728 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003729}
3730
3731static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003732desktop_shell_set_panel(struct wl_client *client,
3733 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003734 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003735 struct wl_resource *surface_resource)
3736{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003737 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003738 struct weston_surface *surface =
3739 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003740 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003741
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003742 if (surface->configure) {
3743 wl_resource_post_error(surface_resource,
3744 WL_DISPLAY_ERROR_INVALID_OBJECT,
3745 "surface role already assigned");
3746 return;
3747 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003748
Jason Ekstranda7af7042013-10-12 22:38:11 -05003749 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3750 weston_view_destroy(view);
3751 view = weston_view_create(surface);
3752
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003753 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003754 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003755 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003756 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003757 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003758 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003759 surface->output->width,
3760 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003761}
3762
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003763static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003764lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003765{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003766 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003767 struct weston_view *view;
3768
3769 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003770
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003771 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003772 return;
3773
Jason Ekstranda7af7042013-10-12 22:38:11 -05003774 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003775
3776 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003777 wl_list_insert(&shell->lock_layer.view_list,
3778 &view->layer_link);
3779 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003780 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003781 }
3782}
3783
3784static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003785handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003786{
Tiago Vignattibe143262012-04-16 17:31:41 +03003787 struct desktop_shell *shell =
3788 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003789
Martin Minarik6d118362012-06-07 18:01:59 +02003790 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003791 shell->lock_surface = NULL;
3792}
3793
3794static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003795desktop_shell_set_lock_surface(struct wl_client *client,
3796 struct wl_resource *resource,
3797 struct wl_resource *surface_resource)
3798{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003799 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003800 struct weston_surface *surface =
3801 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003802
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003803 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003804
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003805 if (!shell->locked)
3806 return;
3807
Pekka Paalanen98262232011-12-01 10:42:22 +02003808 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003809
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003810 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003811 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003812 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003813
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003814 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003815 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003816 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003817}
3818
3819static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003820resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003821{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003822 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003823
Pekka Paalanen77346a62011-11-30 16:26:35 +02003824 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003825
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003826 wl_list_remove(&shell->lock_layer.link);
3827 wl_list_insert(&shell->compositor->cursor_layer.link,
3828 &shell->fullscreen_layer.link);
3829 wl_list_insert(&shell->fullscreen_layer.link,
3830 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003831 if (shell->showing_input_panels) {
3832 wl_list_insert(&shell->panel_layer.link,
3833 &shell->input_panel_layer.link);
3834 wl_list_insert(&shell->input_panel_layer.link,
3835 &ws->layer.link);
3836 } else {
3837 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3838 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003839
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003840 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003841
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003842 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003843 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003844 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003845}
3846
3847static void
3848desktop_shell_unlock(struct wl_client *client,
3849 struct wl_resource *resource)
3850{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003851 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003852
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003853 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003854
3855 if (shell->locked)
3856 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003857}
3858
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003859static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003860desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003861 struct wl_resource *resource,
3862 struct wl_resource *surface_resource)
3863{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003864 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003865
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003866 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003867 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003868}
3869
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003870static void
3871desktop_shell_desktop_ready(struct wl_client *client,
3872 struct wl_resource *resource)
3873{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003874 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003875
3876 shell_fade_startup(shell);
3877}
3878
Kristian Høgsberg75840622011-09-06 13:48:16 -04003879static const struct desktop_shell_interface desktop_shell_implementation = {
3880 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003881 desktop_shell_set_panel,
3882 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003883 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003884 desktop_shell_set_grab_surface,
3885 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003886};
3887
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003888static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003889get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003890{
3891 struct shell_surface *shsurf;
3892
3893 shsurf = get_shell_surface(surface);
3894 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003895 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003896 return shsurf->type;
3897}
3898
Kristian Høgsberg75840622011-09-06 13:48:16 -04003899static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003900move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003901{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003902 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003903 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003904 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003905
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003906 if (seat->pointer->focus == NULL)
3907 return;
3908
3909 focus = seat->pointer->focus->surface;
3910
Pekka Paalanen01388e22013-04-25 13:57:44 +03003911 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003912 if (surface == NULL)
3913 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003914
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003915 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003916 if (shsurf == NULL || shsurf->state.fullscreen ||
3917 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003918 return;
3919
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003920 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003921}
3922
3923static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003924maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3925{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003926 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003927 struct weston_surface *surface;
3928 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003929 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003930
3931 surface = weston_surface_get_main_surface(focus);
3932 if (surface == NULL)
3933 return;
3934
3935 shsurf = get_shell_surface(surface);
3936 if (shsurf == NULL)
3937 return;
3938
3939 if (!shell_surface_is_xdg_surface(shsurf))
3940 return;
3941
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003942 serial = wl_display_next_serial(seat->compositor->wl_display);
3943 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
3944 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003945}
3946
3947static void
3948fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3949{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003950 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003951 struct weston_surface *surface;
3952 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003953 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003954
3955 surface = weston_surface_get_main_surface(focus);
3956 if (surface == NULL)
3957 return;
3958
3959 shsurf = get_shell_surface(surface);
3960 if (shsurf == NULL)
3961 return;
3962
3963 if (!shell_surface_is_xdg_surface(shsurf))
3964 return;
3965
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003966 serial = wl_display_next_serial(seat->compositor->wl_display);
3967 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
3968 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003969}
3970
3971static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003972touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3973{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003974 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003975 struct weston_surface *surface;
3976 struct shell_surface *shsurf;
3977
3978 surface = weston_surface_get_main_surface(focus);
3979 if (surface == NULL)
3980 return;
3981
3982 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003983 if (shsurf == NULL || shsurf->state.fullscreen ||
3984 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003985 return;
3986
3987 surface_touch_move(shsurf, (struct weston_seat *) seat);
3988}
3989
3990static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003991resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003992{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003993 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003994 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003995 uint32_t edges = 0;
3996 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003997 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003998
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003999 if (seat->pointer->focus == NULL)
4000 return;
4001
4002 focus = seat->pointer->focus->surface;
4003
Pekka Paalanen01388e22013-04-25 13:57:44 +03004004 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004005 if (surface == NULL)
4006 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004007
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004008 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004009 if (shsurf == NULL || shsurf->state.fullscreen ||
4010 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004011 return;
4012
Jason Ekstranda7af7042013-10-12 22:38:11 -05004013 weston_view_from_global(shsurf->view,
4014 wl_fixed_to_int(seat->pointer->grab_x),
4015 wl_fixed_to_int(seat->pointer->grab_y),
4016 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004017
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004018 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004019 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004020 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004021 edges |= 0;
4022 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004023 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004024
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004025 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004026 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004027 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004028 edges |= 0;
4029 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004030 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004031
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004032 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004033}
4034
4035static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004036surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004037 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004038{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004039 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004040 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004041 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004042 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004043
Pekka Paalanen01388e22013-04-25 13:57:44 +03004044 /* XXX: broken for windows containing sub-surfaces */
4045 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004046 if (surface == NULL)
4047 return;
4048
4049 shsurf = get_shell_surface(surface);
4050 if (!shsurf)
4051 return;
4052
Jason Ekstranda7af7042013-10-12 22:38:11 -05004053 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004054
Jason Ekstranda7af7042013-10-12 22:38:11 -05004055 if (shsurf->view->alpha > 1.0)
4056 shsurf->view->alpha = 1.0;
4057 if (shsurf->view->alpha < step)
4058 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004059
Jason Ekstranda7af7042013-10-12 22:38:11 -05004060 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004061 weston_surface_damage(surface);
4062}
4063
4064static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004065do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004066 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004067{
Daniel Stone37816df2012-05-16 18:45:18 +01004068 struct weston_seat *ws = (struct weston_seat *) seat;
4069 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004070 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004071 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004072
4073 wl_list_for_each(output, &compositor->output_list, link) {
4074 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004075 wl_fixed_to_double(seat->pointer->x),
4076 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004077 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004078 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004079 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004080 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004081 increment = -output->zoom.increment;
4082 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004083 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004084 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004085 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004086 else
4087 increment = 0;
4088
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004089 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004090
Scott Moreaue6603982012-06-11 13:07:51 -06004091 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004092 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004093 else if (output->zoom.level > output->zoom.max_level)
4094 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004095 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004096 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004097 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004098
Scott Moreaue6603982012-06-11 13:07:51 -06004099 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004100
Jason Ekstranda7af7042013-10-12 22:38:11 -05004101 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004102 }
4103 }
4104}
4105
Scott Moreauccbf29d2012-02-22 14:21:41 -07004106static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004107zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004108 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004109{
4110 do_zoom(seat, time, 0, axis, value);
4111}
4112
4113static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004114zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004115 void *data)
4116{
4117 do_zoom(seat, time, key, 0, 0);
4118}
4119
4120static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004121terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004122 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004123{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004124 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004125
Daniel Stone325fc2d2012-05-30 16:31:58 +01004126 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004127}
4128
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004129static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004130rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4131 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004132{
4133 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004134 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004135 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004136 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004137 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004138
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004139 weston_pointer_move(pointer, x, y);
4140
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004141 if (!shsurf)
4142 return;
4143
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004144 cx = 0.5f * shsurf->surface->width;
4145 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004146
Daniel Stone37816df2012-05-16 18:45:18 +01004147 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4148 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004149 r = sqrtf(dx * dx + dy * dy);
4150
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004151 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004152 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004153
4154 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004155 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004156 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004157
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004158 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004159 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004160
4161 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004162 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004163 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004164 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004165 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004166
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004167 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004168 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004169 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004170 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004171 wl_list_init(&shsurf->rotation.transform.link);
4172 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004173 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004174 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004175
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004176 /* We need to adjust the position of the surface
4177 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004178 cposx = shsurf->view->geometry.x + cx;
4179 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004180 dposx = rotate->center.x - cposx;
4181 dposy = rotate->center.y - cposy;
4182 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004183 weston_view_set_position(shsurf->view,
4184 shsurf->view->geometry.x + dposx,
4185 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004186 }
4187
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004188 /* Repaint implies weston_surface_update_transform(), which
4189 * lazily applies the damage due to rotation update.
4190 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004191 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004192}
4193
4194static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004195rotate_grab_button(struct weston_pointer_grab *grab,
4196 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004197{
4198 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004199 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004200 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004201 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004202 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004203
Daniel Stone4dbadb12012-05-30 16:31:51 +01004204 if (pointer->button_count == 0 &&
4205 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004206 if (shsurf)
4207 weston_matrix_multiply(&shsurf->rotation.rotation,
4208 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004209 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004210 free(rotate);
4211 }
4212}
4213
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004214static void
4215rotate_grab_cancel(struct weston_pointer_grab *grab)
4216{
4217 struct rotate_grab *rotate =
4218 container_of(grab, struct rotate_grab, base.grab);
4219
4220 shell_grab_end(&rotate->base);
4221 free(rotate);
4222}
4223
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004224static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004225 noop_grab_focus,
4226 rotate_grab_motion,
4227 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004228 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004229};
4230
4231static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004232surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004233{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004234 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004235 float dx, dy;
4236 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004237
Pekka Paalanen460099f2012-01-20 16:48:25 +02004238 rotate = malloc(sizeof *rotate);
4239 if (!rotate)
4240 return;
4241
Jason Ekstranda7af7042013-10-12 22:38:11 -05004242 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004243 surface->surface->width * 0.5f,
4244 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004245 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004246
Daniel Stone37816df2012-05-16 18:45:18 +01004247 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4248 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004249 r = sqrtf(dx * dx + dy * dy);
4250 if (r > 20.0f) {
4251 struct weston_matrix inverse;
4252
4253 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004254 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004255 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004256
4257 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004258 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004259 } else {
4260 weston_matrix_init(&surface->rotation.rotation);
4261 weston_matrix_init(&rotate->rotation);
4262 }
4263
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004264 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4265 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004266}
4267
4268static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004269rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004270 void *data)
4271{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004272 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004273 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004274 struct shell_surface *surface;
4275
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004276 if (seat->pointer->focus == NULL)
4277 return;
4278
4279 focus = seat->pointer->focus->surface;
4280
Pekka Paalanen01388e22013-04-25 13:57:44 +03004281 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004282 if (base_surface == NULL)
4283 return;
4284
4285 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004286 if (surface == NULL || surface->state.fullscreen ||
4287 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004288 return;
4289
4290 surface_rotate(surface, seat);
4291}
4292
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004293/* Move all fullscreen layers down to the current workspace in a non-reversible
4294 * manner. This should be used when implementing shell-wide overlays, such as
4295 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004296void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004297lower_fullscreen_layer(struct desktop_shell *shell)
4298{
4299 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004300 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004301
4302 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004303 wl_list_for_each_reverse_safe(view, prev,
4304 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004305 layer_link) {
4306 wl_list_remove(&view->layer_link);
4307 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4308 weston_view_damage_below(view);
4309 weston_surface_damage(view->surface);
4310 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004311}
4312
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004313void
Tiago Vignattibe143262012-04-16 17:31:41 +03004314activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004315 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004316{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004317 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004318 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004319 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004320 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004321 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004322
Pekka Paalanen01388e22013-04-25 13:57:44 +03004323 main_surface = weston_surface_get_main_surface(es);
4324
Daniel Stone37816df2012-05-16 18:45:18 +01004325 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004326
Jonas Ådahl8538b222012-08-29 22:13:03 +02004327 state = ensure_focus_state(shell, seat);
4328 if (state == NULL)
4329 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004330
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004331 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004332 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004333
Rafael Antognolli03b16592013-12-03 15:35:42 -02004334 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004335 assert(shsurf);
4336
Rafael Antognolli03b16592013-12-03 15:35:42 -02004337 if (shsurf->state.fullscreen)
4338 shell_configure_fullscreen(shsurf);
4339 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004340 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004341
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004342 /* Update the surface’s layer. This brings it to the top of the stacking
4343 * order as appropriate. */
4344 shell_surface_update_layer(shsurf);
4345
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004346 if (shell->focus_animation_type != ANIMATION_NONE) {
4347 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004348 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004349 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004350}
4351
Alex Wu21858432012-04-01 20:13:08 +08004352/* no-op func for checking black surface */
4353static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004354black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004355{
4356}
4357
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004358static bool
Alex Wu21858432012-04-01 20:13:08 +08004359is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4360{
4361 if (es->configure == black_surface_configure) {
4362 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004363 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004364 return true;
4365 }
4366 return false;
4367}
4368
Kristian Høgsberg75840622011-09-06 13:48:16 -04004369static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004370activate_binding(struct weston_seat *seat,
4371 struct desktop_shell *shell,
4372 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004373{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004374 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004375
Alex Wu9c35e6b2012-03-05 14:13:13 +08004376 if (!focus)
4377 return;
4378
Pekka Paalanen01388e22013-04-25 13:57:44 +03004379 if (is_black_surface(focus, &main_surface))
4380 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004381
Pekka Paalanen01388e22013-04-25 13:57:44 +03004382 main_surface = weston_surface_get_main_surface(focus);
4383 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004384 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004385
Neil Robertsa28c6932013-10-03 16:43:04 +01004386 activate(shell, focus, seat);
4387}
4388
4389static void
4390click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4391 void *data)
4392{
4393 if (seat->pointer->grab != &seat->pointer->default_grab)
4394 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004395 if (seat->pointer->focus == NULL)
4396 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004397
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004398 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004399}
4400
4401static void
4402touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4403{
4404 if (seat->touch->grab != &seat->touch->default_grab)
4405 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004406 if (seat->touch->focus == NULL)
4407 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004408
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004409 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004410}
4411
4412static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004413lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004414{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004415 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004416
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004417 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004418 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004419 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004420 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004421
4422 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004423
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004424 /* Hide all surfaces by removing the fullscreen, panel and
4425 * toplevel layers. This way nothing else can show or receive
4426 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004427
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004428 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004429 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004430 if (shell->showing_input_panels)
4431 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004432 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004433 wl_list_insert(&shell->compositor->cursor_layer.link,
4434 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004435
Pekka Paalanen77346a62011-11-30 16:26:35 +02004436 launch_screensaver(shell);
4437
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004438 /* TODO: disable bindings that should not work while locked. */
4439
4440 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004441}
4442
4443static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004444unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004445{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004446 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004447 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004448 return;
4449 }
4450
4451 /* If desktop-shell client has gone away, unlock immediately. */
4452 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004453 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004454 return;
4455 }
4456
4457 if (shell->prepare_event_sent)
4458 return;
4459
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004460 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004461 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004462}
4463
4464static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004465shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004466{
4467 struct desktop_shell *shell = data;
4468
4469 shell->fade.animation = NULL;
4470
4471 switch (shell->fade.type) {
4472 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004473 weston_surface_destroy(shell->fade.view->surface);
4474 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004475 break;
4476 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004477 lock(shell);
4478 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004479 default:
4480 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004481 }
4482}
4483
Jason Ekstranda7af7042013-10-12 22:38:11 -05004484static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004485shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004486{
4487 struct weston_compositor *compositor = shell->compositor;
4488 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004489 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004490
4491 surface = weston_surface_create(compositor);
4492 if (!surface)
4493 return NULL;
4494
Jason Ekstranda7af7042013-10-12 22:38:11 -05004495 view = weston_view_create(surface);
4496 if (!view) {
4497 weston_surface_destroy(surface);
4498 return NULL;
4499 }
4500
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004501 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004502 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004503 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004504 wl_list_insert(&compositor->fade_layer.view_list,
4505 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004506 pixman_region32_init(&surface->input);
4507
Jason Ekstranda7af7042013-10-12 22:38:11 -05004508 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004509}
4510
4511static void
4512shell_fade(struct desktop_shell *shell, enum fade_type type)
4513{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004514 float tint;
4515
4516 switch (type) {
4517 case FADE_IN:
4518 tint = 0.0;
4519 break;
4520 case FADE_OUT:
4521 tint = 1.0;
4522 break;
4523 default:
4524 weston_log("shell: invalid fade type\n");
4525 return;
4526 }
4527
4528 shell->fade.type = type;
4529
Jason Ekstranda7af7042013-10-12 22:38:11 -05004530 if (shell->fade.view == NULL) {
4531 shell->fade.view = shell_fade_create_surface(shell);
4532 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004533 return;
4534
Jason Ekstranda7af7042013-10-12 22:38:11 -05004535 shell->fade.view->alpha = 1.0 - tint;
4536 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004537 }
4538
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004539 if (shell->fade.view->output == NULL) {
4540 /* If the black view gets a NULL output, we lost the
4541 * last output and we'll just cancel the fade. This
4542 * happens when you close the last window under the
4543 * X11 or Wayland backends. */
4544 shell->locked = false;
4545 weston_surface_destroy(shell->fade.view->surface);
4546 shell->fade.view = NULL;
4547 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004548 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004549 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004550 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004551 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004552 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004553 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004554 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004555}
4556
4557static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004558do_shell_fade_startup(void *data)
4559{
4560 struct desktop_shell *shell = data;
4561
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004562 if (shell->startup_animation_type == ANIMATION_FADE)
4563 shell_fade(shell, FADE_IN);
4564 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004565 weston_surface_destroy(shell->fade.view->surface);
4566 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004567 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004568}
4569
4570static void
4571shell_fade_startup(struct desktop_shell *shell)
4572{
4573 struct wl_event_loop *loop;
4574
4575 if (!shell->fade.startup_timer)
4576 return;
4577
4578 wl_event_source_remove(shell->fade.startup_timer);
4579 shell->fade.startup_timer = NULL;
4580
4581 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4582 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4583}
4584
4585static int
4586fade_startup_timeout(void *data)
4587{
4588 struct desktop_shell *shell = data;
4589
4590 shell_fade_startup(shell);
4591 return 0;
4592}
4593
4594static void
4595shell_fade_init(struct desktop_shell *shell)
4596{
4597 /* Make compositor output all black, and wait for the desktop-shell
4598 * client to signal it is ready, then fade in. The timer triggers a
4599 * fade-in, in case the desktop-shell client takes too long.
4600 */
4601
4602 struct wl_event_loop *loop;
4603
Jason Ekstranda7af7042013-10-12 22:38:11 -05004604 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004605 weston_log("%s: warning: fade surface already exists\n",
4606 __func__);
4607 return;
4608 }
4609
Jason Ekstranda7af7042013-10-12 22:38:11 -05004610 shell->fade.view = shell_fade_create_surface(shell);
4611 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004612 return;
4613
Jason Ekstranda7af7042013-10-12 22:38:11 -05004614 weston_view_update_transform(shell->fade.view);
4615 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004616
4617 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4618 shell->fade.startup_timer =
4619 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4620 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4621}
4622
4623static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004624idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004625{
4626 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004627 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004628 struct weston_seat *seat;
4629
4630 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4631 if (seat->pointer)
4632 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004633
4634 shell_fade(shell, FADE_OUT);
4635 /* lock() is called from shell_fade_done() */
4636}
4637
4638static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004639wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004640{
4641 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004642 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004643
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004644 unlock(shell);
4645}
4646
4647static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004648center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004649{
Giulio Camuffob8366642013-04-25 13:57:46 +03004650 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004651 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004652
Jason Ekstranda7af7042013-10-12 22:38:11 -05004653 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004654
4655 x = output->x + (output->width - width) / 2 - surf_x / 2;
4656 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004657
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004658 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004659}
4660
4661static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004662weston_view_set_initial_position(struct weston_view *view,
4663 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004664{
4665 struct weston_compositor *compositor = shell->compositor;
4666 int ix = 0, iy = 0;
4667 int range_x, range_y;
4668 int dx, dy, x, y, panel_height;
4669 struct weston_output *output, *target_output = NULL;
4670 struct weston_seat *seat;
4671
4672 /* As a heuristic place the new window on the same output as the
4673 * pointer. Falling back to the output containing 0, 0.
4674 *
4675 * TODO: Do something clever for touch too?
4676 */
4677 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004678 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004679 ix = wl_fixed_to_int(seat->pointer->x);
4680 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004681 break;
4682 }
4683 }
4684
4685 wl_list_for_each(output, &compositor->output_list, link) {
4686 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4687 target_output = output;
4688 break;
4689 }
4690 }
4691
4692 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004693 weston_view_set_position(view, 10 + random() % 400,
4694 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004695 return;
4696 }
4697
4698 /* Valid range within output where the surface will still be onscreen.
4699 * If this is negative it means that the surface is bigger than
4700 * output.
4701 */
4702 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004703 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004704 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004705 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004706
Rob Bradford4cb88c72012-08-13 15:18:44 +01004707 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004708 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004709 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004710 dx = 0;
4711
4712 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004713 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004714 else
4715 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004716
4717 x = target_output->x + dx;
4718 y = target_output->y + dy;
4719
Jason Ekstranda7af7042013-10-12 22:38:11 -05004720 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004721}
4722
4723static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004724map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004725 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004726{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004727 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004728 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004729 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004730 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004731
Pekka Paalanen77346a62011-11-30 16:26:35 +02004732 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004733 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004734 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004735 if (shsurf->state.fullscreen) {
4736 center_on_output(shsurf->view, shsurf->fullscreen_output);
4737 shell_map_fullscreen(shsurf);
4738 } else if (shsurf->state.maximized) {
4739 /* use surface configure to set the geometry */
4740 panel_height = get_output_panel_height(shell, shsurf->output);
4741 surface_subsurfaces_boundingbox(shsurf->surface,
4742 &surf_x, &surf_y, NULL, NULL);
4743 weston_view_set_position(shsurf->view,
4744 shsurf->output->x - surf_x,
4745 shsurf->output->y +
4746 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004747 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004748 weston_view_set_initial_position(shsurf->view, shell);
4749 }
Juan Zhao96879df2012-02-07 08:45:41 +08004750 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004751 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004752 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004753 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004754 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004755 weston_view_set_position(shsurf->view,
4756 shsurf->view->geometry.x + sx,
4757 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004758 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004759 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004760 default:
4761 ;
4762 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004763
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004764 /* Surface stacking order, see also activate(). */
4765 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004766
Jason Ekstranda7af7042013-10-12 22:38:11 -05004767 if (shsurf->type != SHELL_SURFACE_NONE) {
4768 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004769 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004770 shsurf->surface->output = shsurf->output;
4771 shsurf->view->output = shsurf->output;
4772 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004773 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004774
Jason Ekstranda7af7042013-10-12 22:38:11 -05004775 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004776 /* XXX: xwayland's using the same fields for transient type */
4777 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004778 if (shsurf->transient.flags ==
4779 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4780 break;
4781 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004782 if (shsurf->state.relative &&
4783 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4784 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004785 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004786 break;
4787 wl_list_for_each(seat, &compositor->seat_list, link)
4788 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004789 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004790 case SHELL_SURFACE_POPUP:
4791 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004792 default:
4793 break;
4794 }
4795
Rafael Antognolli03b16592013-12-03 15:35:42 -02004796 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4797 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004798 {
4799 switch (shell->win_animation_type) {
4800 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004801 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004802 break;
4803 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004804 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004805 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004806 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004807 default:
4808 break;
4809 }
4810 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004811}
4812
4813static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004814configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004815 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004816{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004817 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004818 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004819 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004820
Pekka Paalanen77346a62011-11-30 16:26:35 +02004821 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004822
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004823 assert(shsurf);
4824
Rafael Antognolli03b16592013-12-03 15:35:42 -02004825 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004826 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004827 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004828 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004829 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4830 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004831 mx = shsurf->output->x - surf_x;
4832 my = shsurf->output->y +
4833 get_output_panel_height(shell,shsurf->output) - surf_y;
4834 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004835 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004836 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004837 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004838
Alex Wu4539b082012-03-01 12:57:46 +08004839 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004840 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004841 wl_list_for_each(view, &surface->views, surface_link)
4842 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004843
Rafael Antognolli03b16592013-12-03 15:35:42 -02004844 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004845 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004846 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004847}
4848
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004849static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004850shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004851{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004852 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004853 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004854 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004855
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004856 assert(shsurf);
4857
4858 shell = shsurf->shell;
4859
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004860 if (!weston_surface_is_mapped(es) &&
4861 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004862 remove_popup_grab(shsurf);
4863 }
4864
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004865 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004866 return;
4867
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004868 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004869 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004870 type_changed = 1;
4871 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004872
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004873 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004874 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004875 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004876 shsurf->last_width != es->width ||
4877 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004878 float from_x, from_y;
4879 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004880
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08004881 if (shsurf->resize_edges) {
4882 sx = 0;
4883 sy = 0;
4884 }
4885
4886 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
4887 sx = shsurf->last_width - es->width;
4888 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
4889 sy = shsurf->last_height - es->height;
4890
4891 shsurf->last_width = es->width;
4892 shsurf->last_height = es->height;
4893
Jason Ekstranda7af7042013-10-12 22:38:11 -05004894 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4895 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004896 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004897 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004898 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004899 }
4900}
4901
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004902static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004903
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004904static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004905desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004906{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004907 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004908 struct desktop_shell *shell =
4909 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004910
4911 shell->child.process.pid = 0;
4912 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004913
4914 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4915 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004916 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004917 shell->child.deathstamp = time;
4918 shell->child.deathcount = 0;
4919 }
4920
4921 shell->child.deathcount++;
4922 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004923 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004924 return;
4925 }
4926
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004927 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004928 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004929 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004930}
4931
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004932static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004933desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4934{
4935 struct desktop_shell *shell;
4936
4937 shell = container_of(listener, struct desktop_shell,
4938 child.client_destroy_listener);
4939
4940 shell->child.client = NULL;
4941}
4942
4943static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004944launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004945{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004946 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004947
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004948 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004949 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004950 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004951 desktop_shell_sigchld);
4952
4953 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004954 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004955
4956 shell->child.client_destroy_listener.notify =
4957 desktop_shell_client_destroy;
4958 wl_client_add_destroy_listener(shell->child.client,
4959 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004960}
4961
4962static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004963handle_shell_client_destroy(struct wl_listener *listener, void *data)
4964{
4965 struct shell_client *sc =
4966 container_of(listener, struct shell_client, destroy_listener);
4967
4968 if (sc->ping_timer)
4969 wl_event_source_remove(sc->ping_timer);
4970 free(sc);
4971}
4972
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004973static struct shell_client *
4974shell_client_create(struct wl_client *client, struct desktop_shell *shell,
4975 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004976{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004977 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004978
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004979 sc = zalloc(sizeof *sc);
4980 if (sc == NULL) {
4981 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004982 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004983 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004984
4985 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004986 if (sc->resource == NULL) {
4987 free(sc);
4988 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004989 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004990 }
4991
4992 sc->client = client;
4993 sc->shell = shell;
4994 sc->destroy_listener.notify = handle_shell_client_destroy;
4995 wl_client_add_destroy_listener(client, &sc->destroy_listener);
4996
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004997 return sc;
4998}
4999
5000static void
5001bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5002{
5003 struct desktop_shell *shell = data;
5004 struct shell_client *sc;
5005
5006 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5007 if (sc)
5008 wl_resource_set_implementation(sc->resource,
5009 &shell_implementation,
5010 shell, NULL);
5011}
5012
5013static void
5014bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5015{
5016 struct desktop_shell *shell = data;
5017 struct shell_client *sc;
5018
5019 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5020 if (sc)
5021 wl_resource_set_dispatcher(sc->resource,
5022 xdg_shell_unversioned_dispatch,
5023 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005024}
5025
5026static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005027unbind_desktop_shell(struct wl_resource *resource)
5028{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005029 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005030
5031 if (shell->locked)
5032 resume_desktop(shell);
5033
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005034 shell->child.desktop_shell = NULL;
5035 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005036}
5037
5038static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005039bind_desktop_shell(struct wl_client *client,
5040 void *data, uint32_t version, uint32_t id)
5041{
Tiago Vignattibe143262012-04-16 17:31:41 +03005042 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005043 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005044
Jason Ekstranda85118c2013-06-27 20:17:02 -05005045 resource = wl_resource_create(client, &desktop_shell_interface,
5046 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005047
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005048 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005049 wl_resource_set_implementation(resource,
5050 &desktop_shell_implementation,
5051 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005052 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005053
5054 if (version < 2)
5055 shell_fade_startup(shell);
5056
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005057 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005058 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005059
5060 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5061 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005062 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005063}
5064
Pekka Paalanen6e168112011-11-24 11:34:05 +02005065static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005066screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005067{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005068 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005069 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005070
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005071 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005072 return;
5073
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005074 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005075 if (!shell->locked)
5076 return;
5077
Jason Ekstranda7af7042013-10-12 22:38:11 -05005078 view = container_of(surface->views.next, struct weston_view, surface_link);
5079 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005080
Jason Ekstranda7af7042013-10-12 22:38:11 -05005081 if (wl_list_empty(&view->layer_link)) {
5082 wl_list_insert(shell->lock_layer.view_list.prev,
5083 &view->layer_link);
5084 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005085 wl_event_source_timer_update(shell->screensaver.timer,
5086 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005087 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005088 }
5089}
5090
5091static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005092screensaver_set_surface(struct wl_client *client,
5093 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005094 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005095 struct wl_resource *output_resource)
5096{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005097 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005098 struct weston_surface *surface =
5099 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005100 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005101 struct weston_view *view, *next;
5102
5103 /* Make sure we only have one view */
5104 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5105 weston_view_destroy(view);
5106 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005107
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005108 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005109 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005110 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005111}
5112
5113static const struct screensaver_interface screensaver_implementation = {
5114 screensaver_set_surface
5115};
5116
5117static void
5118unbind_screensaver(struct wl_resource *resource)
5119{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005120 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005121
Pekka Paalanen77346a62011-11-30 16:26:35 +02005122 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005123}
5124
5125static void
5126bind_screensaver(struct wl_client *client,
5127 void *data, uint32_t version, uint32_t id)
5128{
Tiago Vignattibe143262012-04-16 17:31:41 +03005129 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005130 struct wl_resource *resource;
5131
Jason Ekstranda85118c2013-06-27 20:17:02 -05005132 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005133
Pekka Paalanen77346a62011-11-30 16:26:35 +02005134 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005135 wl_resource_set_implementation(resource,
5136 &screensaver_implementation,
5137 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005138 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005139 return;
5140 }
5141
5142 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5143 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005144 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005145}
5146
Kristian Høgsberg07045392012-02-19 18:52:44 -05005147struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005148 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005149 struct weston_surface *current;
5150 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005151 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005152};
5153
5154static void
5155switcher_next(struct switcher *switcher)
5156{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005157 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005158 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005159 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005160 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005161
Jason Ekstranda7af7042013-10-12 22:38:11 -05005162 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005163 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005164 if (shsurf &&
5165 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5166 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005167 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005168 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005169 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005170 next = view->surface;
5171 prev = view->surface;
5172 view->alpha = 0.25;
5173 weston_view_geometry_dirty(view);
5174 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005175 }
Alex Wu1659daa2012-04-01 20:13:09 +08005176
Jason Ekstranda7af7042013-10-12 22:38:11 -05005177 if (is_black_surface(view->surface, NULL)) {
5178 view->alpha = 0.25;
5179 weston_view_geometry_dirty(view);
5180 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005181 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005182 }
5183
5184 if (next == NULL)
5185 next = first;
5186
Alex Wu07b26062012-03-12 16:06:01 +08005187 if (next == NULL)
5188 return;
5189
Kristian Høgsberg07045392012-02-19 18:52:44 -05005190 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005191 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005192
5193 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005194 wl_list_for_each(view, &next->views, surface_link)
5195 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005196
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005197 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005198 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005199 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005200}
5201
5202static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005203switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005204{
5205 struct switcher *switcher =
5206 container_of(listener, struct switcher, listener);
5207
5208 switcher_next(switcher);
5209}
5210
5211static void
Daniel Stone351eb612012-05-31 15:27:47 -04005212switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005213{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005214 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005215 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005216 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005217
Jason Ekstranda7af7042013-10-12 22:38:11 -05005218 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005219 if (is_focus_view(view))
5220 continue;
5221
Jason Ekstranda7af7042013-10-12 22:38:11 -05005222 view->alpha = 1.0;
5223 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005224 }
5225
Alex Wu07b26062012-03-12 16:06:01 +08005226 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005227 activate(switcher->shell, switcher->current,
5228 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005229 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005230 weston_keyboard_end_grab(keyboard);
5231 if (keyboard->input_method_resource)
5232 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005233 free(switcher);
5234}
5235
5236static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005237switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005238 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005239{
5240 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005241 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005242
Daniel Stonec9785ea2012-05-30 16:31:52 +01005243 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005244 switcher_next(switcher);
5245}
5246
5247static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005248switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005249 uint32_t mods_depressed, uint32_t mods_latched,
5250 uint32_t mods_locked, uint32_t group)
5251{
5252 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005253 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005254
Daniel Stone351eb612012-05-31 15:27:47 -04005255 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5256 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005257}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005258
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005259static void
5260switcher_cancel(struct weston_keyboard_grab *grab)
5261{
5262 struct switcher *switcher = container_of(grab, struct switcher, grab);
5263
5264 switcher_destroy(switcher);
5265}
5266
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005267static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005268 switcher_key,
5269 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005270 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005271};
5272
5273static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005274switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005275 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005276{
Tiago Vignattibe143262012-04-16 17:31:41 +03005277 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005278 struct switcher *switcher;
5279
5280 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005281 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005282 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005283 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005284 wl_list_init(&switcher->listener.link);
5285
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005286 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005287 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005288 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005289 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5290 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005291 switcher_next(switcher);
5292}
5293
Pekka Paalanen3c647232011-12-22 13:43:43 +02005294static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005295backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005296 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005297{
5298 struct weston_compositor *compositor = data;
5299 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005300 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005301
5302 /* TODO: we're limiting to simple use cases, where we assume just
5303 * control on the primary display. We'd have to extend later if we
5304 * ever get support for setting backlights on random desktop LCD
5305 * panels though */
5306 output = get_default_output(compositor);
5307 if (!output)
5308 return;
5309
5310 if (!output->set_backlight)
5311 return;
5312
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005313 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5314 backlight_new = output->backlight_current - 25;
5315 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5316 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005317
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005318 if (backlight_new < 5)
5319 backlight_new = 5;
5320 if (backlight_new > 255)
5321 backlight_new = 255;
5322
5323 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005324 output->set_backlight(output, output->backlight_current);
5325}
5326
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005327struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005328 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005329 struct weston_seat *seat;
5330 uint32_t key[2];
5331 int key_released[2];
5332};
5333
5334static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005335debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005336 uint32_t key, uint32_t state)
5337{
5338 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005339 struct weston_compositor *ec = db->seat->compositor;
5340 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005341 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005342 uint32_t serial;
5343 int send = 0, terminate = 0;
5344 int check_binding = 1;
5345 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005346 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005347
5348 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5349 /* Do not run bindings on key releases */
5350 check_binding = 0;
5351
5352 for (i = 0; i < 2; i++)
5353 if (key == db->key[i])
5354 db->key_released[i] = 1;
5355
5356 if (db->key_released[0] && db->key_released[1]) {
5357 /* All key releases been swalled so end the grab */
5358 terminate = 1;
5359 } else if (key != db->key[0] && key != db->key[1]) {
5360 /* Should not swallow release of other keys */
5361 send = 1;
5362 }
5363 } else if (key == db->key[0] && !db->key_released[0]) {
5364 /* Do not check bindings for the first press of the binding
5365 * key. This allows it to be used as a debug shortcut.
5366 * We still need to swallow this event. */
5367 check_binding = 0;
5368 } else if (db->key[1]) {
5369 /* If we already ran a binding don't process another one since
5370 * we can't keep track of all the binding keys that were
5371 * pressed in order to swallow the release events. */
5372 send = 1;
5373 check_binding = 0;
5374 }
5375
5376 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005377 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5378 key, state)) {
5379 /* We ran a binding so swallow the press and keep the
5380 * grab to swallow the released too. */
5381 send = 0;
5382 terminate = 0;
5383 db->key[1] = key;
5384 } else {
5385 /* Terminate the grab since the key pressed is not a
5386 * debug binding key. */
5387 send = 1;
5388 terminate = 1;
5389 }
5390 }
5391
5392 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005393 serial = wl_display_next_serial(display);
5394 resource_list = &grab->keyboard->focus_resource_list;
5395 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005396 wl_keyboard_send_key(resource, serial, time, key, state);
5397 }
5398 }
5399
5400 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005401 weston_keyboard_end_grab(grab->keyboard);
5402 if (grab->keyboard->input_method_resource)
5403 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005404 free(db);
5405 }
5406}
5407
5408static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005409debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005410 uint32_t mods_depressed, uint32_t mods_latched,
5411 uint32_t mods_locked, uint32_t group)
5412{
5413 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005414 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005415
Neil Roberts96d790e2013-09-19 17:32:00 +01005416 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005417
Neil Roberts96d790e2013-09-19 17:32:00 +01005418 wl_resource_for_each(resource, resource_list) {
5419 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5420 mods_latched, mods_locked, group);
5421 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005422}
5423
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005424static void
5425debug_binding_cancel(struct weston_keyboard_grab *grab)
5426{
5427 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5428
5429 weston_keyboard_end_grab(grab->keyboard);
5430 free(db);
5431}
5432
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005433struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005434 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005435 debug_binding_modifiers,
5436 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005437};
5438
5439static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005440debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005441{
5442 struct debug_binding_grab *grab;
5443
5444 grab = calloc(1, sizeof *grab);
5445 if (!grab)
5446 return;
5447
5448 grab->seat = (struct weston_seat *) seat;
5449 grab->key[0] = key;
5450 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005451 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005452}
5453
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005454static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005455force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005456 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005457{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005458 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005459 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005460 struct desktop_shell *shell = data;
5461 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005462 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005463
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005464 focus_surface = seat->keyboard->focus;
5465 if (!focus_surface)
5466 return;
5467
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005468 wl_signal_emit(&compositor->kill_signal, focus_surface);
5469
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005470 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005471 wl_client_get_credentials(client, &pid, NULL, NULL);
5472
5473 /* Skip clients that we launched ourselves (the credentials of
5474 * the socketpair is ours) */
5475 if (pid == getpid())
5476 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005477
Daniel Stone325fc2d2012-05-30 16:31:58 +01005478 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005479}
5480
5481static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005482workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005483 uint32_t key, void *data)
5484{
5485 struct desktop_shell *shell = data;
5486 unsigned int new_index = shell->workspaces.current;
5487
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005488 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005489 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005490 if (new_index != 0)
5491 new_index--;
5492
5493 change_workspace(shell, new_index);
5494}
5495
5496static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005497workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005498 uint32_t key, void *data)
5499{
5500 struct desktop_shell *shell = data;
5501 unsigned int new_index = shell->workspaces.current;
5502
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005503 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005504 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005505 if (new_index < shell->workspaces.num - 1)
5506 new_index++;
5507
5508 change_workspace(shell, new_index);
5509}
5510
5511static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005512workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005513 uint32_t key, void *data)
5514{
5515 struct desktop_shell *shell = data;
5516 unsigned int new_index;
5517
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005518 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005519 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005520 new_index = key - KEY_F1;
5521 if (new_index >= shell->workspaces.num)
5522 new_index = shell->workspaces.num - 1;
5523
5524 change_workspace(shell, new_index);
5525}
5526
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005527static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005528workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005529 uint32_t key, void *data)
5530{
5531 struct desktop_shell *shell = data;
5532 unsigned int new_index = shell->workspaces.current;
5533
5534 if (shell->locked)
5535 return;
5536
5537 if (new_index != 0)
5538 new_index--;
5539
5540 take_surface_to_workspace_by_seat(shell, seat, new_index);
5541}
5542
5543static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005544workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005545 uint32_t key, void *data)
5546{
5547 struct desktop_shell *shell = data;
5548 unsigned int new_index = shell->workspaces.current;
5549
5550 if (shell->locked)
5551 return;
5552
5553 if (new_index < shell->workspaces.num - 1)
5554 new_index++;
5555
5556 take_surface_to_workspace_by_seat(shell, seat, new_index);
5557}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005558
5559static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005560shell_reposition_view_on_output_destroy(struct weston_view *view)
5561{
5562 struct weston_output *output, *first_output;
5563 struct weston_compositor *ec = view->surface->compositor;
5564 struct shell_surface *shsurf;
5565 float x, y;
5566 int visible;
5567
5568 x = view->geometry.x;
5569 y = view->geometry.y;
5570
5571 /* At this point the destroyed output is not in the list anymore.
5572 * If the view is still visible somewhere, we leave where it is,
5573 * otherwise, move it to the first output. */
5574 visible = 0;
5575 wl_list_for_each(output, &ec->output_list, link) {
5576 if (pixman_region32_contains_point(&output->region,
5577 x, y, NULL)) {
5578 visible = 1;
5579 break;
5580 }
5581 }
5582
5583 if (!visible) {
5584 first_output = container_of(ec->output_list.next,
5585 struct weston_output, link);
5586
5587 x = first_output->x + first_output->width / 4;
5588 y = first_output->y + first_output->height / 4;
5589 }
5590
5591 weston_view_set_position(view, x, y);
5592
5593 shsurf = get_shell_surface(view->surface);
5594
5595 if (shsurf) {
5596 shsurf->saved_position_valid = false;
5597 shsurf->next_state.maximized = false;
5598 shsurf->next_state.fullscreen = false;
5599 shsurf->state_changed = true;
5600 }
5601}
5602
5603static void
5604shell_reposition_views_on_output_destroy(struct shell_output *shell_output)
5605{
5606 struct desktop_shell *shell = shell_output->shell;
5607 struct weston_output *output = shell_output->output;
5608 struct weston_layer *layer;
5609 struct weston_view *view;
5610
5611 /* Move all views in the layers owned by the shell */
5612 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5613 wl_list_for_each(view, &layer->view_list, layer_link) {
5614 if (view->output != output)
5615 continue;
5616
5617 shell_reposition_view_on_output_destroy(view);
5618 }
5619
5620 /* We don't start from the beggining of the layer list, so
5621 * make sure we don't wrap around it. */
5622 if (layer == &shell->background_layer)
5623 break;
5624 }
5625}
5626
5627static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005628handle_output_destroy(struct wl_listener *listener, void *data)
5629{
5630 struct shell_output *output_listener =
5631 container_of(listener, struct shell_output, destroy_listener);
5632
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005633 shell_reposition_views_on_output_destroy(output_listener);
5634
Xiong Zhang6b481422013-10-23 13:58:32 +08005635 wl_list_remove(&output_listener->destroy_listener.link);
5636 wl_list_remove(&output_listener->link);
5637 free(output_listener);
5638}
5639
5640static void
5641create_shell_output(struct desktop_shell *shell,
5642 struct weston_output *output)
5643{
5644 struct shell_output *shell_output;
5645
5646 shell_output = zalloc(sizeof *shell_output);
5647 if (shell_output == NULL)
5648 return;
5649
5650 shell_output->output = output;
5651 shell_output->shell = shell;
5652 shell_output->destroy_listener.notify = handle_output_destroy;
5653 wl_signal_add(&output->destroy_signal,
5654 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005655 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005656}
5657
5658static void
5659handle_output_create(struct wl_listener *listener, void *data)
5660{
5661 struct desktop_shell *shell =
5662 container_of(listener, struct desktop_shell, output_create_listener);
5663 struct weston_output *output = (struct weston_output *)data;
5664
5665 create_shell_output(shell, output);
5666}
5667
5668static void
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005669handle_output_move(struct wl_listener *listener, void *data)
5670{
5671 struct desktop_shell *shell;
5672 struct weston_output *output;
5673 struct weston_layer *layer;
5674 struct weston_view *view;
5675 float x, y;
5676
5677 shell = container_of(listener, struct desktop_shell,
5678 output_move_listener);
5679 output = data;
5680
5681 /* Move all views in the layers owned by the shell */
5682 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5683 wl_list_for_each(view, &layer->view_list, layer_link) {
5684 if (view->output != output)
5685 continue;
5686
5687 x = view->geometry.x + output->move_x;
5688 y = view->geometry.y + output->move_y;
5689 weston_view_set_position(view, x, y);
5690 }
5691
5692 /* We don't start from the beggining of the layer list, so
5693 * make sure we don't wrap around it. */
5694 if (layer == &shell->background_layer)
5695 break;
5696 }
5697}
5698
5699static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005700setup_output_destroy_handler(struct weston_compositor *ec,
5701 struct desktop_shell *shell)
5702{
5703 struct weston_output *output;
5704
5705 wl_list_init(&shell->output_list);
5706 wl_list_for_each(output, &ec->output_list, link)
5707 create_shell_output(shell, output);
5708
5709 shell->output_create_listener.notify = handle_output_create;
5710 wl_signal_add(&ec->output_created_signal,
5711 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005712
5713 shell->output_move_listener.notify = handle_output_move;
5714 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005715}
5716
5717static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005718shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005719{
Tiago Vignattibe143262012-04-16 17:31:41 +03005720 struct desktop_shell *shell =
5721 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005722 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005723 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005724
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005725 /* Force state to unlocked so we don't try to fade */
5726 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005727 if (shell->child.client)
5728 wl_client_destroy(shell->child.client);
5729
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005730 wl_list_remove(&shell->idle_listener.link);
5731 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005732
5733 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005734
Xiong Zhang6b481422013-10-23 13:58:32 +08005735 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5736 wl_list_remove(&shell_output->destroy_listener.link);
5737 wl_list_remove(&shell_output->link);
5738 free(shell_output);
5739 }
5740
5741 wl_list_remove(&shell->output_create_listener.link);
5742
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005743 wl_array_for_each(ws, &shell->workspaces.array)
5744 workspace_destroy(*ws);
5745 wl_array_release(&shell->workspaces.array);
5746
Pekka Paalanen3c647232011-12-22 13:43:43 +02005747 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005748 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005749 free(shell);
5750}
5751
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005752static void
5753shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5754{
5755 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005756 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005757
5758 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005759 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5760 MODIFIER_CTRL | MODIFIER_ALT,
5761 terminate_binding, ec);
5762 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5763 click_to_activate_binding,
5764 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005765 weston_compositor_add_touch_binding(ec, 0,
5766 touch_to_activate_binding,
5767 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005768 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5769 MODIFIER_SUPER | MODIFIER_ALT,
5770 surface_opacity_binding, NULL);
5771 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5772 MODIFIER_SUPER, zoom_axis_binding,
5773 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005774
5775 /* configurable bindings */
5776 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005777 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5778 zoom_key_binding, NULL);
5779 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5780 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005781 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5782 maximize_binding, NULL);
5783 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5784 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005785 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5786 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005787 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005788 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5789 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005790 weston_compositor_add_button_binding(ec, BTN_LEFT,
5791 mod | MODIFIER_SHIFT,
5792 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005793
5794 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5795 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5796 rotate_binding, NULL);
5797
Daniel Stone325fc2d2012-05-30 16:31:58 +01005798 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5799 shell);
5800 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5801 ec);
5802 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5803 backlight_binding, ec);
5804 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5805 ec);
5806 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5807 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005808 weston_compositor_add_key_binding(ec, KEY_K, mod,
5809 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005810 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5811 workspace_up_binding, shell);
5812 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5813 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005814 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5815 workspace_move_surface_up_binding,
5816 shell);
5817 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5818 workspace_move_surface_down_binding,
5819 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005820
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005821 if (shell->exposay_modifier)
5822 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5823 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005824
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005825 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5826 if (shell->workspaces.num > 1) {
5827 num_workspace_bindings = shell->workspaces.num;
5828 if (num_workspace_bindings > 6)
5829 num_workspace_bindings = 6;
5830 for (i = 0; i < num_workspace_bindings; i++)
5831 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5832 workspace_f_binding,
5833 shell);
5834 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005835
5836 /* Debug bindings */
5837 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5838 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005839}
5840
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005841WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005842module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005843 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005844{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005845 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005846 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005847 struct workspace **pws;
5848 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005849 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005850
Peter Huttererf3d62272013-08-08 11:57:05 +10005851 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005852 if (shell == NULL)
5853 return -1;
5854
Kristian Høgsberg75840622011-09-06 13:48:16 -04005855 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005856
5857 shell->destroy_listener.notify = shell_destroy;
5858 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005859 shell->idle_listener.notify = idle_handler;
5860 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5861 shell->wake_listener.notify = wake_handler;
5862 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005863
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005864 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005865 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005866 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005867 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005868 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005869 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005870 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005871 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005872 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005873 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005874
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005875 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5876 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005877 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5878 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005879 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005880
5881 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005882 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005883
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005884 if (input_panel_setup(shell) < 0)
5885 return -1;
5886
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005887 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005888
Daniel Stonedf8133b2013-11-19 11:37:14 +01005889 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5890 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5891
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005892 for (i = 0; i < shell->workspaces.num; i++) {
5893 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5894 if (pws == NULL)
5895 return -1;
5896
5897 *pws = workspace_create();
5898 if (*pws == NULL)
5899 return -1;
5900 }
5901 activate_workspace(shell, 0);
5902
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005903 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005904 wl_list_init(&shell->workspaces.animation.link);
5905 shell->workspaces.animation.frame = animate_workspace_change_frame;
5906
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005907 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005908 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005909 return -1;
5910
Rafael Antognollie2a34552013-12-03 15:35:45 -02005911 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5912 shell, bind_xdg_shell) == NULL)
5913 return -1;
5914
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005915 if (wl_global_create(ec->wl_display,
5916 &desktop_shell_interface, 2,
5917 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005918 return -1;
5919
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005920 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5921 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005922 return -1;
5923
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005924 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5925 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005926 return -1;
5927
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005928 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005929
Xiong Zhang6b481422013-10-23 13:58:32 +08005930 setup_output_destroy_handler(ec, shell);
5931
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005932 loop = wl_display_get_event_loop(ec->wl_display);
5933 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005934
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005935 shell->screensaver.timer =
5936 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5937
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005938 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005939 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005940 create_keyboard_focus_listener(seat);
5941 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005942
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005943 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005944
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005945 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005946
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005947 return 0;
5948}