blob: 97e8b1ea4a71853a249354e06f6b76e3ff10f2c2 [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
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002448set_minimized(struct weston_surface *surface, uint32_t is_true)
2449{
2450 struct shell_surface *shsurf;
2451 struct workspace *current_ws;
2452 struct weston_seat *seat;
2453 struct weston_surface *focus;
2454 struct weston_view *view;
2455
2456 view = get_default_view(surface);
2457 if (!view)
2458 return;
2459
2460 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2461
2462 shsurf = get_shell_surface(surface);
2463 current_ws = get_current_workspace(shsurf->shell);
2464
2465 wl_list_remove(&view->layer_link);
2466 /* hide or show, depending on the state */
2467 if (is_true) {
2468 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2469
2470 drop_focus_state(shsurf->shell, current_ws, view->surface);
2471 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2472 if (!seat->keyboard)
2473 continue;
2474 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2475 if (focus == view->surface)
2476 weston_keyboard_set_focus(seat->keyboard, NULL);
2477 }
2478 }
2479 else {
2480 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2481
2482 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2483 if (!seat->keyboard)
2484 continue;
2485 activate(shsurf->shell, view->surface, seat);
2486 }
2487 }
2488
2489 shell_surface_update_child_surface_layers(shsurf);
2490
2491 weston_view_damage_below(view);
2492}
2493
2494static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002495shell_surface_set_maximized(struct wl_client *client,
2496 struct wl_resource *resource,
2497 struct wl_resource *output_resource)
2498{
2499 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2500 struct weston_output *output;
2501
2502 if (output_resource)
2503 output = wl_resource_get_user_data(output_resource);
2504 else
2505 output = NULL;
2506
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002507 shell_surface_set_parent(shsurf, NULL);
2508
Rafael Antognolli03b16592013-12-03 15:35:42 -02002509 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002510 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002511
2512 shsurf->next_state.maximized = true;
2513 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002514}
2515
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002516/* This is only ever called from set_surface_type(), so there’s no need to
2517 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002518static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002519reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002520{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002521 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002522 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002523 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002524 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002525
Pekka Paalanen98262232011-12-01 10:42:22 +02002526 return 0;
2527}
2528
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002529static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002530set_full_output(struct shell_surface *shsurf)
2531{
2532 shsurf->saved_x = shsurf->view->geometry.x;
2533 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002534 shsurf->saved_width = shsurf->surface->width;
2535 shsurf->saved_height = shsurf->surface->height;
2536 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002537 shsurf->saved_position_valid = true;
2538
2539 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2540 wl_list_remove(&shsurf->rotation.transform.link);
2541 wl_list_init(&shsurf->rotation.transform.link);
2542 weston_view_geometry_dirty(shsurf->view);
2543 shsurf->saved_rotation_valid = true;
2544 }
2545}
2546
2547static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002548set_surface_type(struct shell_surface *shsurf)
2549{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002550 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002551 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002552
Philip Withnallbecb77e2013-11-25 18:01:30 +00002553 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002554
Rafael Antognolli03b16592013-12-03 15:35:42 -02002555 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002556 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002557
2558 switch (shsurf->type) {
2559 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002560 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002561 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002562 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002563 weston_view_set_position(shsurf->view,
2564 pev->geometry.x + shsurf->transient.x,
2565 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002566 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002567 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002568
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002569 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002570 weston_view_set_position(shsurf->view, shsurf->transient.x,
2571 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002572 break;
2573
Philip Withnall0f640e22013-11-25 18:01:31 +00002574 case SHELL_SURFACE_POPUP:
2575 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002576 default:
2577 break;
2578 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002579
2580 /* Update the surface’s layer. */
2581 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002582}
2583
Tiago Vignattibe143262012-04-16 17:31:41 +03002584static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002585shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002586{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002587 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002588}
2589
Alex Wu21858432012-04-01 20:13:08 +08002590static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002591black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002592
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002594create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002595 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002596 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002597{
2598 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002599 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002600
2601 surface = weston_surface_create(ec);
2602 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002603 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002604 return NULL;
2605 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002606 view = weston_view_create(surface);
2607 if (surface == NULL) {
2608 weston_log("no memory\n");
2609 weston_surface_destroy(surface);
2610 return NULL;
2611 }
Alex Wu4539b082012-03-01 12:57:46 +08002612
Alex Wu21858432012-04-01 20:13:08 +08002613 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002614 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002615 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002616 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002617 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002618 pixman_region32_fini(&surface->input);
2619 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002620
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002621 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002622 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002623
2624 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002625}
2626
Philip Withnalled8f1a92013-11-25 18:01:43 +00002627static void
2628shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2629{
2630 struct weston_output *output = shsurf->fullscreen_output;
2631
Rafael Antognolli03b16592013-12-03 15:35:42 -02002632 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002633
2634 if (!shsurf->fullscreen.black_view)
2635 shsurf->fullscreen.black_view =
2636 create_black_surface(shsurf->surface->compositor,
2637 shsurf->surface,
2638 output->x, output->y,
2639 output->width,
2640 output->height);
2641
2642 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2643 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2644 wl_list_insert(&shsurf->view->layer_link,
2645 &shsurf->fullscreen.black_view->layer_link);
2646 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2647 weston_surface_damage(shsurf->surface);
2648}
2649
Alex Wu4539b082012-03-01 12:57:46 +08002650/* Create black surface and append it to the associated fullscreen surface.
2651 * Handle size dismatch and positioning according to the method. */
2652static void
2653shell_configure_fullscreen(struct shell_surface *shsurf)
2654{
2655 struct weston_output *output = shsurf->fullscreen_output;
2656 struct weston_surface *surface = shsurf->surface;
2657 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002658 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002659 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002660
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002661 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2662 restore_output_mode(output);
2663
Philip Withnalled8f1a92013-11-25 18:01:43 +00002664 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002665
Jason Ekstranda7af7042013-10-12 22:38:11 -05002666 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002667 &surf_width, &surf_height);
2668
Alex Wu4539b082012-03-01 12:57:46 +08002669 switch (shsurf->fullscreen.type) {
2670 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002671 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002673 break;
2674 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002675 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002676 if (output->width == surf_width &&
2677 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002678 weston_view_set_position(shsurf->view,
2679 output->x - surf_x,
2680 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002681 break;
2682 }
2683
Alex Wu4539b082012-03-01 12:57:46 +08002684 matrix = &shsurf->fullscreen.transform.matrix;
2685 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002686
Scott Moreau1bad5db2012-08-18 01:04:05 -06002687 output_aspect = (float) output->width /
2688 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002689 /* XXX: Use surf_width and surf_height here? */
2690 surface_aspect = (float) surface->width /
2691 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002692 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002693 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002694 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002695 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002696 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002697 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002698
Alex Wu4539b082012-03-01 12:57:46 +08002699 weston_matrix_scale(matrix, scale, scale, 1);
2700 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002701 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002702 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002703 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2704 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002705 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002706
Alex Wu4539b082012-03-01 12:57:46 +08002707 break;
2708 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002709 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002710 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002711 surf_width * surface->buffer_viewport.buffer.scale,
2712 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002713 shsurf->fullscreen.framerate};
2714
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002715 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002716 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002717 weston_view_set_position(shsurf->view,
2718 output->x - surf_x,
2719 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002720 shsurf->fullscreen.black_view->surface->width = output->width;
2721 shsurf->fullscreen.black_view->surface->height = output->height;
2722 weston_view_set_position(shsurf->fullscreen.black_view,
2723 output->x - surf_x,
2724 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002725 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002726 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002727 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002728 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002729 }
Alex Wubd3354b2012-04-17 17:20:49 +08002730 }
Alex Wu4539b082012-03-01 12:57:46 +08002731 break;
2732 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002733 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002734 break;
2735 default:
2736 break;
2737 }
2738}
2739
Alex Wu4539b082012-03-01 12:57:46 +08002740static void
2741shell_map_fullscreen(struct shell_surface *shsurf)
2742{
Alex Wubd3354b2012-04-17 17:20:49 +08002743 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002744}
2745
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002746static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002747set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2748{
2749 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002750 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002751 shsurf->transient.x = x;
2752 shsurf->transient.y = y;
2753 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002754
2755 shell_surface_set_parent(shsurf, NULL);
2756
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002757 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002758 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002759}
2760
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002761static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002762
2763static void
2764destroy_shell_seat(struct wl_listener *listener, void *data)
2765{
2766 struct shell_seat *shseat =
2767 container_of(listener,
2768 struct shell_seat, seat_destroy_listener);
2769 struct shell_surface *shsurf, *prev = NULL;
2770
2771 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002772 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002773 shseat->popup_grab.client = NULL;
2774
2775 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2776 shsurf->popup.shseat = NULL;
2777 if (prev) {
2778 wl_list_init(&prev->popup.grab_link);
2779 }
2780 prev = shsurf;
2781 }
2782 wl_list_init(&prev->popup.grab_link);
2783 }
2784
2785 wl_list_remove(&shseat->seat_destroy_listener.link);
2786 free(shseat);
2787}
2788
2789static struct shell_seat *
2790create_shell_seat(struct weston_seat *seat)
2791{
2792 struct shell_seat *shseat;
2793
2794 shseat = calloc(1, sizeof *shseat);
2795 if (!shseat) {
2796 weston_log("no memory to allocate shell seat\n");
2797 return NULL;
2798 }
2799
2800 shseat->seat = seat;
2801 wl_list_init(&shseat->popup_grab.surfaces_list);
2802
2803 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2804 wl_signal_add(&seat->destroy_signal,
2805 &shseat->seat_destroy_listener);
2806
2807 return shseat;
2808}
2809
2810static struct shell_seat *
2811get_shell_seat(struct weston_seat *seat)
2812{
2813 struct wl_listener *listener;
2814
2815 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2816 if (listener == NULL)
2817 return create_shell_seat(seat);
2818
2819 return container_of(listener,
2820 struct shell_seat, seat_destroy_listener);
2821}
2822
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002823static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002824popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002825{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002826 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002828 struct shell_seat *shseat =
2829 container_of(grab, struct shell_seat, popup_grab.grab);
2830 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002831 wl_fixed_t sx, sy;
2832
Jason Ekstranda7af7042013-10-12 22:38:11 -05002833 view = weston_compositor_pick_view(pointer->seat->compositor,
2834 pointer->x, pointer->y,
2835 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002836
Jason Ekstranda7af7042013-10-12 22:38:11 -05002837 if (view && view->surface->resource &&
2838 wl_resource_get_client(view->surface->resource) == client) {
2839 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002840 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002841 weston_pointer_set_focus(pointer, NULL,
2842 wl_fixed_from_int(0),
2843 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002844 }
2845}
2846
2847static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002848popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2849 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002850{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002851 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002852 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002853 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002854
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002855 weston_pointer_move(pointer, x, y);
2856
Neil Roberts96d790e2013-09-19 17:32:00 +01002857 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002858 weston_view_from_global_fixed(pointer->focus,
2859 pointer->x, pointer->y,
2860 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002861 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002862 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002863}
2864
2865static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002866popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002867 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002868{
2869 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002870 struct shell_seat *shseat =
2871 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002872 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002873 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002874 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002875 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002876
Neil Roberts96d790e2013-09-19 17:32:00 +01002877 resource_list = &grab->pointer->focus_resource_list;
2878 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002879 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002880 wl_resource_for_each(resource, resource_list) {
2881 wl_pointer_send_button(resource, serial,
2882 time, button, state);
2883 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002884 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002885 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002886 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002887 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002888 }
2889
Daniel Stone4dbadb12012-05-30 16:31:51 +01002890 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002891 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002892}
2893
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002894static void
2895popup_grab_cancel(struct weston_pointer_grab *grab)
2896{
2897 popup_grab_end(grab->pointer);
2898}
2899
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002900static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002901 popup_grab_focus,
2902 popup_grab_motion,
2903 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002904 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002905};
2906
2907static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002908shell_surface_send_popup_done(struct shell_surface *shsurf)
2909{
2910 if (shell_surface_is_wl_shell_surface(shsurf))
2911 wl_shell_surface_send_popup_done(shsurf->resource);
2912 else if (shell_surface_is_xdg_popup(shsurf))
2913 xdg_popup_send_popup_done(shsurf->resource,
2914 shsurf->popup.serial);
2915}
2916
2917static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002918popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002919{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002920 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002921 struct shell_seat *shseat =
2922 container_of(grab, struct shell_seat, popup_grab.grab);
2923 struct shell_surface *shsurf;
2924 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002925
2926 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002927 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002928 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002929 shseat->popup_grab.grab.interface = NULL;
2930 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002931 /* Send the popup_done event to all the popups open */
2932 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002933 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002934 shsurf->popup.shseat = NULL;
2935 if (prev) {
2936 wl_list_init(&prev->popup.grab_link);
2937 }
2938 prev = shsurf;
2939 }
2940 wl_list_init(&prev->popup.grab_link);
2941 wl_list_init(&shseat->popup_grab.surfaces_list);
2942 }
2943}
2944
2945static void
2946add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2947{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002948 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002949
2950 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002951 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002952 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002953 /* We must make sure here that this popup was opened after
2954 * a mouse press, and not just by moving around with other
2955 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002956 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002957 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002958
Rob Bradforddfe31052013-06-26 19:49:11 +01002959 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002960 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002961 } else {
2962 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002963 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002964}
2965
2966static void
2967remove_popup_grab(struct shell_surface *shsurf)
2968{
2969 struct shell_seat *shseat = shsurf->popup.shseat;
2970
2971 wl_list_remove(&shsurf->popup.grab_link);
2972 wl_list_init(&shsurf->popup.grab_link);
2973 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002974 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002975 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002976 }
2977}
2978
2979static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002980shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002981{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002982 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002983 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002984
Jason Ekstranda7af7042013-10-12 22:38:11 -05002985 shsurf->surface->output = parent_view->output;
2986 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002987
Jason Ekstranda7af7042013-10-12 22:38:11 -05002988 weston_view_set_transform_parent(shsurf->view, parent_view);
2989 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2990 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002991
Kristian Høgsberge3148752013-05-06 23:19:49 -04002992 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002993 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002994 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002995 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002996 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002997 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002998}
2999
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003000static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003001 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003002 shell_surface_move,
3003 shell_surface_resize,
3004 shell_surface_set_toplevel,
3005 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003006 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003007 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003008 shell_surface_set_maximized,
3009 shell_surface_set_title,
3010 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003011};
3012
3013static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003014destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003015{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003016 struct shell_surface *child, *next;
3017
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003018 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3019
Giulio Camuffo5085a752013-03-25 21:42:45 +01003020 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3021 remove_popup_grab(shsurf);
3022 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003023
Alex Wubd3354b2012-04-17 17:20:49 +08003024 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003025 shell_surface_is_top_fullscreen(shsurf))
3026 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003027
Jason Ekstranda7af7042013-10-12 22:38:11 -05003028 if (shsurf->fullscreen.black_view)
3029 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003030
Alex Wubd3354b2012-04-17 17:20:49 +08003031 /* As destroy_resource() use wl_list_for_each_safe(),
3032 * we can always remove the listener.
3033 */
3034 wl_list_remove(&shsurf->surface_destroy_listener.link);
3035 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003036 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003037
Jason Ekstranda7af7042013-10-12 22:38:11 -05003038 weston_view_destroy(shsurf->view);
3039
Philip Withnall648a4dd2013-11-25 18:01:44 +00003040 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003041 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3042 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003043
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003044 wl_list_remove(&shsurf->link);
3045 free(shsurf);
3046}
3047
3048static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003049shell_destroy_shell_surface(struct wl_resource *resource)
3050{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003051 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003052
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003053 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003054}
3055
3056static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003057shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003058{
3059 struct shell_surface *shsurf = container_of(listener,
3060 struct shell_surface,
3061 surface_destroy_listener);
3062
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003063 if (shsurf->resource)
3064 wl_resource_destroy(shsurf->resource);
3065 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003066 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003067}
3068
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003069static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003070fade_out_done(struct weston_view_animation *animation, void *data)
3071{
3072 struct shell_surface *shsurf = data;
3073
3074 weston_surface_destroy(shsurf->surface);
3075}
3076
3077static void
3078handle_resource_destroy(struct wl_listener *listener, void *data)
3079{
3080 struct shell_surface *shsurf =
3081 container_of(listener, struct shell_surface,
3082 resource_destroy_listener);
3083
3084 shsurf->surface->ref_count++;
3085
3086 pixman_region32_fini(&shsurf->surface->pending.input);
3087 pixman_region32_init(&shsurf->surface->pending.input);
3088 pixman_region32_fini(&shsurf->surface->input);
3089 pixman_region32_init(&shsurf->surface->input);
Kristian Høgsbergb033c5e2014-03-20 14:49:07 -07003090 if (weston_surface_is_mapped(shsurf->surface))
3091 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3092 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003093}
3094
3095static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003096shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003097
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003098struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003099get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003100{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003101 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003102 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003103 else
3104 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003105}
3106
Rafael Antognollie2a34552013-12-03 15:35:45 -02003107static struct shell_surface *
3108create_common_surface(void *shell, struct weston_surface *surface,
3109 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003110{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003111 struct shell_surface *shsurf;
3112
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003113 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003114 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003115 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003116 }
3117
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003118 shsurf = calloc(1, sizeof *shsurf);
3119 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003120 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003121 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003122 }
3123
Jason Ekstranda7af7042013-10-12 22:38:11 -05003124 shsurf->view = weston_view_create(surface);
3125 if (!shsurf->view) {
3126 weston_log("no memory to allocate shell surface\n");
3127 free(shsurf);
3128 return NULL;
3129 }
3130
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003131 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003132 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003133
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003134 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3135 wl_resource_add_destroy_listener(surface->resource,
3136 &shsurf->resource_destroy_listener);
3137
Tiago Vignattibc052c92012-04-19 16:18:18 +03003138 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003139 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003140 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003141 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003142 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003143 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003144 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3145 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003146 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003147 wl_list_init(&shsurf->fullscreen.transform.link);
3148
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003149 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003150 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003151 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003152 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003153
3154 /* init link so its safe to always remove it in destroy_shell_surface */
3155 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003156 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003157
Pekka Paalanen460099f2012-01-20 16:48:25 +02003158 /* empty when not in use */
3159 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003160 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003161
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003162 wl_list_init(&shsurf->workspace_transform.link);
3163
Philip Withnall648a4dd2013-11-25 18:01:44 +00003164 wl_list_init(&shsurf->children_link);
3165 wl_list_init(&shsurf->children_list);
3166 shsurf->parent = NULL;
3167
Pekka Paalanen98262232011-12-01 10:42:22 +02003168 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003169
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003170 shsurf->client = client;
3171
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003172 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003173}
3174
Rafael Antognollie2a34552013-12-03 15:35:45 -02003175static struct shell_surface *
3176create_shell_surface(void *shell, struct weston_surface *surface,
3177 const struct weston_shell_client *client)
3178{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003179 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003180}
3181
Jason Ekstranda7af7042013-10-12 22:38:11 -05003182static struct weston_view *
3183get_primary_view(void *shell, struct shell_surface *shsurf)
3184{
3185 return shsurf->view;
3186}
3187
Tiago Vignattibc052c92012-04-19 16:18:18 +03003188static void
3189shell_get_shell_surface(struct wl_client *client,
3190 struct wl_resource *resource,
3191 uint32_t id,
3192 struct wl_resource *surface_resource)
3193{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003194 struct weston_surface *surface =
3195 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003196 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003197 struct shell_surface *shsurf;
3198
3199 if (get_shell_surface(surface)) {
3200 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003201 WL_DISPLAY_ERROR_INVALID_OBJECT,
3202 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003203 return;
3204 }
3205
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003206 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003207 if (!shsurf) {
3208 wl_resource_post_error(surface_resource,
3209 WL_DISPLAY_ERROR_INVALID_OBJECT,
3210 "surface->configure already set");
3211 return;
3212 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003213
Jason Ekstranda85118c2013-06-27 20:17:02 -05003214 shsurf->resource =
3215 wl_resource_create(client,
3216 &wl_shell_surface_interface, 1, id);
3217 wl_resource_set_implementation(shsurf->resource,
3218 &shell_surface_implementation,
3219 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003220}
3221
Rafael Antognollie2a34552013-12-03 15:35:45 -02003222static bool
3223shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3224{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003225 /* A shell surface without a resource is created from xwayland
3226 * and is considered a wl_shell surface for now. */
3227
3228 return shsurf->resource == NULL ||
3229 wl_resource_instance_of(shsurf->resource,
3230 &wl_shell_surface_interface,
3231 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003232}
3233
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003234static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003235 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003236};
3237
Rafael Antognollie2a34552013-12-03 15:35:45 -02003238/****************************
3239 * xdg-shell implementation */
3240
3241static void
3242xdg_surface_destroy(struct wl_client *client,
3243 struct wl_resource *resource)
3244{
3245 wl_resource_destroy(resource);
3246}
3247
3248static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003249xdg_surface_set_transient_for(struct wl_client *client,
3250 struct wl_resource *resource,
3251 struct wl_resource *parent_resource)
3252{
3253 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3254 struct weston_surface *parent;
3255
3256 if (parent_resource)
3257 parent = wl_resource_get_user_data(parent_resource);
3258 else
3259 parent = NULL;
3260
3261 shell_surface_set_parent(shsurf, parent);
3262}
3263
3264static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003265xdg_surface_set_margin(struct wl_client *client,
3266 struct wl_resource *resource,
3267 int32_t left,
3268 int32_t right,
3269 int32_t top,
3270 int32_t bottom)
3271{
3272 /* Do nothing, Weston doesn't try to constrain or place
3273 * surfaces in any special manner... */
3274}
3275
3276static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003277xdg_surface_set_app_id(struct wl_client *client,
3278 struct wl_resource *resource,
3279 const char *app_id)
3280{
3281 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3282
3283 free(shsurf->class);
3284 shsurf->class = strdup(app_id);
3285}
3286
3287static void
3288xdg_surface_set_title(struct wl_client *client,
3289 struct wl_resource *resource, const char *title)
3290{
3291 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3292
3293 set_title(shsurf, title);
3294}
3295
3296static void
3297xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3298 struct wl_resource *seat_resource, uint32_t serial)
3299{
3300 common_surface_move(resource, seat_resource, serial);
3301}
3302
3303static void
3304xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3305 struct wl_resource *seat_resource, uint32_t serial,
3306 uint32_t edges)
3307{
3308 common_surface_resize(resource, seat_resource, serial, edges);
3309}
3310
3311static void
3312xdg_surface_set_output(struct wl_client *client,
3313 struct wl_resource *resource,
3314 struct wl_resource *output_resource)
3315{
3316 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3317 struct weston_output *output;
3318
3319 if (output_resource)
3320 output = wl_resource_get_user_data(output_resource);
3321 else
3322 output = NULL;
3323
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003324 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003325}
3326
3327static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003328xdg_surface_change_state(struct shell_surface *shsurf,
3329 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003330{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003331 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003332 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003333
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003334 switch (state) {
3335 case XDG_SURFACE_STATE_MAXIMIZED:
3336 shsurf->requested_state.maximized = value;
3337 if (value)
3338 set_maximized(shsurf, NULL);
3339 break;
3340 case XDG_SURFACE_STATE_FULLSCREEN:
3341 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003342
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003343 if (value)
3344 set_fullscreen(shsurf,
3345 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3346 0, shsurf->recommended_output);
3347 break;
3348 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003349}
3350
3351static void
3352xdg_surface_request_change_state(struct wl_client *client,
3353 struct wl_resource *resource,
3354 uint32_t state,
3355 uint32_t value,
3356 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003357{
3358 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003359
3360 /* The client can't know what the current state is, so we need
3361 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003362
3363 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3364 return;
3365
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003366 switch (state) {
3367 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003368 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003369 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003370 default:
3371 /* send error? ignore? send change state with value 0? */
3372 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003373 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003374
3375 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003376}
3377
3378static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003379xdg_surface_ack_change_state(struct wl_client *client,
3380 struct wl_resource *resource,
3381 uint32_t state,
3382 uint32_t value,
3383 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003384{
3385 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3386
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003387 if (shsurf->state_requested) {
3388 shsurf->next_state = shsurf->requested_state;
3389 shsurf->state_changed = true;
3390 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003391 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003392}
3393
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003394static void
3395xdg_surface_set_minimized(struct wl_client *client,
3396 struct wl_resource *resource)
3397{
3398 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3399
3400 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3401 return;
3402
3403 /* apply compositor's own minimization logic (hide) */
3404 set_minimized(shsurf->surface, 1);
3405}
3406
Rafael Antognollie2a34552013-12-03 15:35:45 -02003407static const struct xdg_surface_interface xdg_surface_implementation = {
3408 xdg_surface_destroy,
3409 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003410 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003411 xdg_surface_set_title,
3412 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003413 xdg_surface_move,
3414 xdg_surface_resize,
3415 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003416 xdg_surface_request_change_state,
3417 xdg_surface_ack_change_state,
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003418 xdg_surface_set_minimized
Rafael Antognollie2a34552013-12-03 15:35:45 -02003419};
3420
3421static void
3422xdg_send_configure(struct weston_surface *surface,
3423 uint32_t edges, int32_t width, int32_t height)
3424{
3425 struct shell_surface *shsurf = get_shell_surface(surface);
3426
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003427 assert(shsurf);
3428
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003429 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003430}
3431
3432static const struct weston_shell_client xdg_client = {
3433 xdg_send_configure
3434};
3435
3436static void
3437xdg_use_unstable_version(struct wl_client *client,
3438 struct wl_resource *resource,
3439 int32_t version)
3440{
3441 if (version > 1) {
3442 wl_resource_post_error(resource,
3443 1,
3444 "xdg-shell:: version not implemented yet.");
3445 return;
3446 }
3447}
3448
3449static struct shell_surface *
3450create_xdg_surface(void *shell, struct weston_surface *surface,
3451 const struct weston_shell_client *client)
3452{
3453 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003454
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003455 shsurf = create_common_surface(shell, surface, client);
3456 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003457
3458 return shsurf;
3459}
3460
3461static void
3462xdg_get_xdg_surface(struct wl_client *client,
3463 struct wl_resource *resource,
3464 uint32_t id,
3465 struct wl_resource *surface_resource)
3466{
3467 struct weston_surface *surface =
3468 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003469 struct shell_client *sc = wl_resource_get_user_data(resource);
3470 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003471 struct shell_surface *shsurf;
3472
3473 if (get_shell_surface(surface)) {
3474 wl_resource_post_error(surface_resource,
3475 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003476 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003477 return;
3478 }
3479
3480 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3481 if (!shsurf) {
3482 wl_resource_post_error(surface_resource,
3483 WL_DISPLAY_ERROR_INVALID_OBJECT,
3484 "surface->configure already set");
3485 return;
3486 }
3487
3488 shsurf->resource =
3489 wl_resource_create(client,
3490 &xdg_surface_interface, 1, id);
3491 wl_resource_set_implementation(shsurf->resource,
3492 &xdg_surface_implementation,
3493 shsurf, shell_destroy_shell_surface);
3494}
3495
3496static bool
3497shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3498{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003499 return shsurf->resource &&
3500 wl_resource_instance_of(shsurf->resource,
3501 &xdg_surface_interface,
3502 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003503}
3504
3505/* xdg-popup implementation */
3506
3507static void
3508xdg_popup_destroy(struct wl_client *client,
3509 struct wl_resource *resource)
3510{
3511 wl_resource_destroy(resource);
3512}
3513
Rafael Antognollie2a34552013-12-03 15:35:45 -02003514static const struct xdg_popup_interface xdg_popup_implementation = {
3515 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003516};
3517
3518static void
3519xdg_popup_send_configure(struct weston_surface *surface,
3520 uint32_t edges, int32_t width, int32_t height)
3521{
3522}
3523
3524static const struct weston_shell_client xdg_popup_client = {
3525 xdg_popup_send_configure
3526};
3527
3528static struct shell_surface *
3529create_xdg_popup(void *shell, struct weston_surface *surface,
3530 const struct weston_shell_client *client,
3531 struct weston_surface *parent,
3532 struct shell_seat *seat,
3533 uint32_t serial,
3534 int32_t x, int32_t y)
3535{
3536 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003537
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003538 shsurf = create_common_surface(shell, surface, client);
3539 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003540 shsurf->popup.shseat = seat;
3541 shsurf->popup.serial = serial;
3542 shsurf->popup.x = x;
3543 shsurf->popup.y = y;
3544 shell_surface_set_parent(shsurf, parent);
3545
3546 return shsurf;
3547}
3548
3549static void
3550xdg_get_xdg_popup(struct wl_client *client,
3551 struct wl_resource *resource,
3552 uint32_t id,
3553 struct wl_resource *surface_resource,
3554 struct wl_resource *parent_resource,
3555 struct wl_resource *seat_resource,
3556 uint32_t serial,
3557 int32_t x, int32_t y, uint32_t flags)
3558{
3559 struct weston_surface *surface =
3560 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003561 struct shell_client *sc = wl_resource_get_user_data(resource);
3562 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003563 struct shell_surface *shsurf;
3564 struct weston_surface *parent;
3565 struct shell_seat *seat;
3566
3567 if (get_shell_surface(surface)) {
3568 wl_resource_post_error(surface_resource,
3569 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003570 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003571 return;
3572 }
3573
3574 if (!parent_resource) {
3575 wl_resource_post_error(surface_resource,
3576 WL_DISPLAY_ERROR_INVALID_OBJECT,
3577 "xdg_shell::get_xdg_popup requires a parent shell surface");
3578 }
3579
3580 parent = wl_resource_get_user_data(parent_resource);
3581 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3582
3583 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3584 parent, seat, serial, x, y);
3585 if (!shsurf) {
3586 wl_resource_post_error(surface_resource,
3587 WL_DISPLAY_ERROR_INVALID_OBJECT,
3588 "surface->configure already set");
3589 return;
3590 }
3591
3592 shsurf->resource =
3593 wl_resource_create(client,
3594 &xdg_popup_interface, 1, id);
3595 wl_resource_set_implementation(shsurf->resource,
3596 &xdg_popup_implementation,
3597 shsurf, shell_destroy_shell_surface);
3598}
3599
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003600static void
3601xdg_pong(struct wl_client *client,
3602 struct wl_resource *resource, uint32_t serial)
3603{
3604 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003605
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003606 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003607}
3608
Rafael Antognollie2a34552013-12-03 15:35:45 -02003609static bool
3610shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3611{
3612 return wl_resource_instance_of(shsurf->resource,
3613 &xdg_popup_interface,
3614 &xdg_popup_implementation);
3615}
3616
3617static const struct xdg_shell_interface xdg_implementation = {
3618 xdg_use_unstable_version,
3619 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003620 xdg_get_xdg_popup,
3621 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003622};
3623
3624static int
3625xdg_shell_unversioned_dispatch(const void *implementation,
3626 void *_target, uint32_t opcode,
3627 const struct wl_message *message,
3628 union wl_argument *args)
3629{
3630 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003631 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003632
3633 if (opcode != 0) {
3634 wl_resource_post_error(resource,
3635 WL_DISPLAY_ERROR_INVALID_OBJECT,
3636 "must call use_unstable_version first");
3637 return 0;
3638 }
3639
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003640#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003641
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003642 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3643 "shell implementation doesn't match protocol version");
3644
Rafael Antognollie2a34552013-12-03 15:35:45 -02003645 if (args[0].i != XDG_SERVER_VERSION) {
3646 wl_resource_post_error(resource,
3647 WL_DISPLAY_ERROR_INVALID_OBJECT,
3648 "incompatible version, server is %d "
3649 "client wants %d",
3650 XDG_SERVER_VERSION, args[0].i);
3651 return 0;
3652 }
3653
3654 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003655 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003656
3657 return 1;
3658}
3659
3660/* end of xdg-shell implementation */
3661/***********************************/
3662
Kristian Høgsberg07937562011-04-12 17:25:42 -04003663static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003664shell_fade(struct desktop_shell *shell, enum fade_type type);
3665
3666static int
3667screensaver_timeout(void *data)
3668{
3669 struct desktop_shell *shell = data;
3670
3671 shell_fade(shell, FADE_OUT);
3672
3673 return 1;
3674}
3675
3676static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003677handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003678{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003679 struct desktop_shell *shell =
3680 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003681
Pekka Paalanen18027e52011-12-02 16:31:49 +02003682 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003683
3684 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003685 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003686}
3687
3688static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003689launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003690{
3691 if (shell->screensaver.binding)
3692 return;
3693
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003694 if (!shell->screensaver.path) {
3695 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003696 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003697 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003698
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003699 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003700 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003701 return;
3702 }
3703
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003704 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003705 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003706 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003707 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003708}
3709
3710static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003711terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003712{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003713 if (shell->screensaver.process.pid == 0)
3714 return;
3715
3716 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003717}
3718
3719static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003720configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003721{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003722 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003723
Jason Ekstranda7af7042013-10-12 22:38:11 -05003724 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3725 if (v->output == ev->output && v != ev) {
3726 weston_view_unmap(v);
3727 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003728 }
3729 }
3730
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003731 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003732
Jason Ekstranda7af7042013-10-12 22:38:11 -05003733 if (wl_list_empty(&ev->layer_link)) {
3734 wl_list_insert(&layer->view_list, &ev->layer_link);
3735 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003736 }
3737}
3738
3739static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003740background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003741{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003742 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003743 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003744
Jason Ekstranda7af7042013-10-12 22:38:11 -05003745 view = container_of(es->views.next, struct weston_view, surface_link);
3746
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003747 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003748}
3749
3750static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003751desktop_shell_set_background(struct wl_client *client,
3752 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003753 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003754 struct wl_resource *surface_resource)
3755{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003756 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003757 struct weston_surface *surface =
3758 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003759 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003760
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003761 if (surface->configure) {
3762 wl_resource_post_error(surface_resource,
3763 WL_DISPLAY_ERROR_INVALID_OBJECT,
3764 "surface role already assigned");
3765 return;
3766 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003767
Jason Ekstranda7af7042013-10-12 22:38:11 -05003768 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3769 weston_view_destroy(view);
3770 view = weston_view_create(surface);
3771
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003772 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003773 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003774 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003775 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003776 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003777 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003778 surface->output->width,
3779 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003780}
3781
3782static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003783panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003784{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003785 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003786 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003787
Jason Ekstranda7af7042013-10-12 22:38:11 -05003788 view = container_of(es->views.next, struct weston_view, surface_link);
3789
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003790 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003791}
3792
3793static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003794desktop_shell_set_panel(struct wl_client *client,
3795 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003796 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003797 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);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003802 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003803
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003804 if (surface->configure) {
3805 wl_resource_post_error(surface_resource,
3806 WL_DISPLAY_ERROR_INVALID_OBJECT,
3807 "surface role already assigned");
3808 return;
3809 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003810
Jason Ekstranda7af7042013-10-12 22:38:11 -05003811 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3812 weston_view_destroy(view);
3813 view = weston_view_create(surface);
3814
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003815 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003816 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003817 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003818 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003819 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003820 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003821 surface->output->width,
3822 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003823}
3824
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003825static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003826lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003827{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003828 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003829 struct weston_view *view;
3830
3831 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003832
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003833 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003834 return;
3835
Jason Ekstranda7af7042013-10-12 22:38:11 -05003836 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003837
3838 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003839 wl_list_insert(&shell->lock_layer.view_list,
3840 &view->layer_link);
3841 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003842 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003843 }
3844}
3845
3846static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003847handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003848{
Tiago Vignattibe143262012-04-16 17:31:41 +03003849 struct desktop_shell *shell =
3850 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003851
Martin Minarik6d118362012-06-07 18:01:59 +02003852 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003853 shell->lock_surface = NULL;
3854}
3855
3856static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003857desktop_shell_set_lock_surface(struct wl_client *client,
3858 struct wl_resource *resource,
3859 struct wl_resource *surface_resource)
3860{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003861 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003862 struct weston_surface *surface =
3863 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003864
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003865 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003866
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003867 if (!shell->locked)
3868 return;
3869
Pekka Paalanen98262232011-12-01 10:42:22 +02003870 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003871
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003872 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003873 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003874 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003875
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003876 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003877 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003878 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003879}
3880
3881static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003882resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003883{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003884 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003885
Pekka Paalanen77346a62011-11-30 16:26:35 +02003886 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003887
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003888 wl_list_remove(&shell->lock_layer.link);
3889 wl_list_insert(&shell->compositor->cursor_layer.link,
3890 &shell->fullscreen_layer.link);
3891 wl_list_insert(&shell->fullscreen_layer.link,
3892 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003893 if (shell->showing_input_panels) {
3894 wl_list_insert(&shell->panel_layer.link,
3895 &shell->input_panel_layer.link);
3896 wl_list_insert(&shell->input_panel_layer.link,
3897 &ws->layer.link);
3898 } else {
3899 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3900 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003901
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003902 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003903
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003904 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003905 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003906 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003907}
3908
3909static void
3910desktop_shell_unlock(struct wl_client *client,
3911 struct wl_resource *resource)
3912{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003913 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003914
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003915 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003916
3917 if (shell->locked)
3918 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003919}
3920
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003921static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003922desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003923 struct wl_resource *resource,
3924 struct wl_resource *surface_resource)
3925{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003926 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003927
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003928 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003929 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003930}
3931
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003932static void
3933desktop_shell_desktop_ready(struct wl_client *client,
3934 struct wl_resource *resource)
3935{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003936 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003937
3938 shell_fade_startup(shell);
3939}
3940
Kristian Høgsberg75840622011-09-06 13:48:16 -04003941static const struct desktop_shell_interface desktop_shell_implementation = {
3942 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003943 desktop_shell_set_panel,
3944 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003945 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003946 desktop_shell_set_grab_surface,
3947 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003948};
3949
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003950static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003951get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003952{
3953 struct shell_surface *shsurf;
3954
3955 shsurf = get_shell_surface(surface);
3956 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003957 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003958 return shsurf->type;
3959}
3960
Kristian Høgsberg75840622011-09-06 13:48:16 -04003961static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003962move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003963{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003964 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003965 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003966 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003967
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003968 if (seat->pointer->focus == NULL)
3969 return;
3970
3971 focus = seat->pointer->focus->surface;
3972
Pekka Paalanen01388e22013-04-25 13:57:44 +03003973 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003974 if (surface == NULL)
3975 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003976
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003977 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003978 if (shsurf == NULL || shsurf->state.fullscreen ||
3979 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003980 return;
3981
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003982 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003983}
3984
3985static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003986maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3987{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003988 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003989 struct weston_surface *surface;
3990 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003991 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003992
3993 surface = weston_surface_get_main_surface(focus);
3994 if (surface == NULL)
3995 return;
3996
3997 shsurf = get_shell_surface(surface);
3998 if (shsurf == NULL)
3999 return;
4000
4001 if (!shell_surface_is_xdg_surface(shsurf))
4002 return;
4003
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004004 serial = wl_display_next_serial(seat->compositor->wl_display);
4005 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4006 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004007}
4008
4009static void
4010fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4011{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004012 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004013 struct weston_surface *surface;
4014 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004015 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004016
4017 surface = weston_surface_get_main_surface(focus);
4018 if (surface == NULL)
4019 return;
4020
4021 shsurf = get_shell_surface(surface);
4022 if (shsurf == NULL)
4023 return;
4024
4025 if (!shell_surface_is_xdg_surface(shsurf))
4026 return;
4027
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004028 serial = wl_display_next_serial(seat->compositor->wl_display);
4029 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4030 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004031}
4032
4033static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004034touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4035{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004036 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004037 struct weston_surface *surface;
4038 struct shell_surface *shsurf;
4039
4040 surface = weston_surface_get_main_surface(focus);
4041 if (surface == NULL)
4042 return;
4043
4044 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004045 if (shsurf == NULL || shsurf->state.fullscreen ||
4046 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004047 return;
4048
4049 surface_touch_move(shsurf, (struct weston_seat *) seat);
4050}
4051
4052static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004053resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004054{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004055 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004056 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004057 uint32_t edges = 0;
4058 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004059 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004060
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004061 if (seat->pointer->focus == NULL)
4062 return;
4063
4064 focus = seat->pointer->focus->surface;
4065
Pekka Paalanen01388e22013-04-25 13:57:44 +03004066 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004067 if (surface == NULL)
4068 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004069
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004070 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004071 if (shsurf == NULL || shsurf->state.fullscreen ||
4072 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004073 return;
4074
Jason Ekstranda7af7042013-10-12 22:38:11 -05004075 weston_view_from_global(shsurf->view,
4076 wl_fixed_to_int(seat->pointer->grab_x),
4077 wl_fixed_to_int(seat->pointer->grab_y),
4078 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004079
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004080 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004081 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004082 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004083 edges |= 0;
4084 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004085 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004086
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004087 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004088 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004089 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004090 edges |= 0;
4091 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004092 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004093
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004094 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004095}
4096
4097static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004098surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004099 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004100{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004101 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004102 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004103 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004104 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004105
Pekka Paalanen01388e22013-04-25 13:57:44 +03004106 /* XXX: broken for windows containing sub-surfaces */
4107 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004108 if (surface == NULL)
4109 return;
4110
4111 shsurf = get_shell_surface(surface);
4112 if (!shsurf)
4113 return;
4114
Jason Ekstranda7af7042013-10-12 22:38:11 -05004115 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004116
Jason Ekstranda7af7042013-10-12 22:38:11 -05004117 if (shsurf->view->alpha > 1.0)
4118 shsurf->view->alpha = 1.0;
4119 if (shsurf->view->alpha < step)
4120 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004121
Jason Ekstranda7af7042013-10-12 22:38:11 -05004122 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004123 weston_surface_damage(surface);
4124}
4125
4126static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004127do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004128 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004129{
Daniel Stone37816df2012-05-16 18:45:18 +01004130 struct weston_seat *ws = (struct weston_seat *) seat;
4131 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004132 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004133 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004134
4135 wl_list_for_each(output, &compositor->output_list, link) {
4136 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004137 wl_fixed_to_double(seat->pointer->x),
4138 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004139 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004140 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004141 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004142 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004143 increment = -output->zoom.increment;
4144 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004145 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004146 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004147 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004148 else
4149 increment = 0;
4150
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004151 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004152
Scott Moreaue6603982012-06-11 13:07:51 -06004153 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004154 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004155 else if (output->zoom.level > output->zoom.max_level)
4156 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004157 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004158 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004159 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004160
Scott Moreaue6603982012-06-11 13:07:51 -06004161 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004162
Jason Ekstranda7af7042013-10-12 22:38:11 -05004163 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004164 }
4165 }
4166}
4167
Scott Moreauccbf29d2012-02-22 14:21:41 -07004168static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004169zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004170 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004171{
4172 do_zoom(seat, time, 0, axis, value);
4173}
4174
4175static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004176zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004177 void *data)
4178{
4179 do_zoom(seat, time, key, 0, 0);
4180}
4181
4182static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004183terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004184 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004185{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004186 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004187
Daniel Stone325fc2d2012-05-30 16:31:58 +01004188 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004189}
4190
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004191static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004192rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4193 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004194{
4195 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004196 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004197 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004198 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004199 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004200
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004201 weston_pointer_move(pointer, x, y);
4202
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004203 if (!shsurf)
4204 return;
4205
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004206 cx = 0.5f * shsurf->surface->width;
4207 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004208
Daniel Stone37816df2012-05-16 18:45:18 +01004209 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4210 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004211 r = sqrtf(dx * dx + dy * dy);
4212
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004213 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004214 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004215
4216 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004217 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004218 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004219
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004220 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004221 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004222
4223 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004224 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004225 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004226 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004227 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004228
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004229 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004230 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004231 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004232 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004233 wl_list_init(&shsurf->rotation.transform.link);
4234 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004235 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004236 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004237
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004238 /* We need to adjust the position of the surface
4239 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004240 cposx = shsurf->view->geometry.x + cx;
4241 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004242 dposx = rotate->center.x - cposx;
4243 dposy = rotate->center.y - cposy;
4244 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004245 weston_view_set_position(shsurf->view,
4246 shsurf->view->geometry.x + dposx,
4247 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004248 }
4249
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004250 /* Repaint implies weston_surface_update_transform(), which
4251 * lazily applies the damage due to rotation update.
4252 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004253 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004254}
4255
4256static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004257rotate_grab_button(struct weston_pointer_grab *grab,
4258 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004259{
4260 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004261 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004262 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004263 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004264 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004265
Daniel Stone4dbadb12012-05-30 16:31:51 +01004266 if (pointer->button_count == 0 &&
4267 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004268 if (shsurf)
4269 weston_matrix_multiply(&shsurf->rotation.rotation,
4270 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004271 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004272 free(rotate);
4273 }
4274}
4275
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004276static void
4277rotate_grab_cancel(struct weston_pointer_grab *grab)
4278{
4279 struct rotate_grab *rotate =
4280 container_of(grab, struct rotate_grab, base.grab);
4281
4282 shell_grab_end(&rotate->base);
4283 free(rotate);
4284}
4285
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004286static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004287 noop_grab_focus,
4288 rotate_grab_motion,
4289 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004290 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004291};
4292
4293static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004294surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004295{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004296 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004297 float dx, dy;
4298 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004299
Pekka Paalanen460099f2012-01-20 16:48:25 +02004300 rotate = malloc(sizeof *rotate);
4301 if (!rotate)
4302 return;
4303
Jason Ekstranda7af7042013-10-12 22:38:11 -05004304 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004305 surface->surface->width * 0.5f,
4306 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004307 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004308
Daniel Stone37816df2012-05-16 18:45:18 +01004309 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4310 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004311 r = sqrtf(dx * dx + dy * dy);
4312 if (r > 20.0f) {
4313 struct weston_matrix inverse;
4314
4315 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004316 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004317 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004318
4319 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004320 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004321 } else {
4322 weston_matrix_init(&surface->rotation.rotation);
4323 weston_matrix_init(&rotate->rotation);
4324 }
4325
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004326 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4327 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004328}
4329
4330static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004331rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004332 void *data)
4333{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004334 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004335 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004336 struct shell_surface *surface;
4337
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004338 if (seat->pointer->focus == NULL)
4339 return;
4340
4341 focus = seat->pointer->focus->surface;
4342
Pekka Paalanen01388e22013-04-25 13:57:44 +03004343 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004344 if (base_surface == NULL)
4345 return;
4346
4347 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004348 if (surface == NULL || surface->state.fullscreen ||
4349 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004350 return;
4351
4352 surface_rotate(surface, seat);
4353}
4354
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004355/* Move all fullscreen layers down to the current workspace in a non-reversible
4356 * manner. This should be used when implementing shell-wide overlays, such as
4357 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004358void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004359lower_fullscreen_layer(struct desktop_shell *shell)
4360{
4361 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004362 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004363
4364 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004365 wl_list_for_each_reverse_safe(view, prev,
4366 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004367 layer_link) {
4368 wl_list_remove(&view->layer_link);
4369 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4370 weston_view_damage_below(view);
4371 weston_surface_damage(view->surface);
4372 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004373}
4374
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004375void
Tiago Vignattibe143262012-04-16 17:31:41 +03004376activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004377 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004378{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004379 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004380 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004381 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004382 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004383 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004384
Pekka Paalanen01388e22013-04-25 13:57:44 +03004385 main_surface = weston_surface_get_main_surface(es);
4386
Daniel Stone37816df2012-05-16 18:45:18 +01004387 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004388
Jonas Ådahl8538b222012-08-29 22:13:03 +02004389 state = ensure_focus_state(shell, seat);
4390 if (state == NULL)
4391 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004392
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004393 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004394 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004395
Rafael Antognolli03b16592013-12-03 15:35:42 -02004396 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004397 assert(shsurf);
4398
Rafael Antognolli03b16592013-12-03 15:35:42 -02004399 if (shsurf->state.fullscreen)
4400 shell_configure_fullscreen(shsurf);
4401 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004402 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004403
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004404 /* Update the surface’s layer. This brings it to the top of the stacking
4405 * order as appropriate. */
4406 shell_surface_update_layer(shsurf);
4407
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004408 if (shell->focus_animation_type != ANIMATION_NONE) {
4409 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004410 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004411 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004412}
4413
Alex Wu21858432012-04-01 20:13:08 +08004414/* no-op func for checking black surface */
4415static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004416black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004417{
4418}
4419
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004420static bool
Alex Wu21858432012-04-01 20:13:08 +08004421is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4422{
4423 if (es->configure == black_surface_configure) {
4424 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004425 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004426 return true;
4427 }
4428 return false;
4429}
4430
Kristian Høgsberg75840622011-09-06 13:48:16 -04004431static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004432activate_binding(struct weston_seat *seat,
4433 struct desktop_shell *shell,
4434 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004435{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004436 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004437
Alex Wu9c35e6b2012-03-05 14:13:13 +08004438 if (!focus)
4439 return;
4440
Pekka Paalanen01388e22013-04-25 13:57:44 +03004441 if (is_black_surface(focus, &main_surface))
4442 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004443
Pekka Paalanen01388e22013-04-25 13:57:44 +03004444 main_surface = weston_surface_get_main_surface(focus);
4445 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004446 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004447
Neil Robertsa28c6932013-10-03 16:43:04 +01004448 activate(shell, focus, seat);
4449}
4450
4451static void
4452click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4453 void *data)
4454{
4455 if (seat->pointer->grab != &seat->pointer->default_grab)
4456 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004457 if (seat->pointer->focus == NULL)
4458 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004459
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004460 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004461}
4462
4463static void
4464touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4465{
4466 if (seat->touch->grab != &seat->touch->default_grab)
4467 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004468 if (seat->touch->focus == NULL)
4469 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004470
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004471 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004472}
4473
4474static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004475lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004476{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004477 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004478
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004479 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004480 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004481 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004482 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004483
4484 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004485
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004486 /* Hide all surfaces by removing the fullscreen, panel and
4487 * toplevel layers. This way nothing else can show or receive
4488 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004489
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004490 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004491 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004492 if (shell->showing_input_panels)
4493 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004494 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004495 wl_list_insert(&shell->compositor->cursor_layer.link,
4496 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004497
Pekka Paalanen77346a62011-11-30 16:26:35 +02004498 launch_screensaver(shell);
4499
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004500 /* TODO: disable bindings that should not work while locked. */
4501
4502 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004503}
4504
4505static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004506unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004507{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004508 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004509 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004510 return;
4511 }
4512
4513 /* If desktop-shell client has gone away, unlock immediately. */
4514 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004515 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004516 return;
4517 }
4518
4519 if (shell->prepare_event_sent)
4520 return;
4521
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004522 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004523 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004524}
4525
4526static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004527shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004528{
4529 struct desktop_shell *shell = data;
4530
4531 shell->fade.animation = NULL;
4532
4533 switch (shell->fade.type) {
4534 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004535 weston_surface_destroy(shell->fade.view->surface);
4536 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004537 break;
4538 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004539 lock(shell);
4540 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004541 default:
4542 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004543 }
4544}
4545
Jason Ekstranda7af7042013-10-12 22:38:11 -05004546static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004547shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004548{
4549 struct weston_compositor *compositor = shell->compositor;
4550 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004551 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004552
4553 surface = weston_surface_create(compositor);
4554 if (!surface)
4555 return NULL;
4556
Jason Ekstranda7af7042013-10-12 22:38:11 -05004557 view = weston_view_create(surface);
4558 if (!view) {
4559 weston_surface_destroy(surface);
4560 return NULL;
4561 }
4562
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004563 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004564 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004565 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004566 wl_list_insert(&compositor->fade_layer.view_list,
4567 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004568 pixman_region32_init(&surface->input);
4569
Jason Ekstranda7af7042013-10-12 22:38:11 -05004570 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004571}
4572
4573static void
4574shell_fade(struct desktop_shell *shell, enum fade_type type)
4575{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004576 float tint;
4577
4578 switch (type) {
4579 case FADE_IN:
4580 tint = 0.0;
4581 break;
4582 case FADE_OUT:
4583 tint = 1.0;
4584 break;
4585 default:
4586 weston_log("shell: invalid fade type\n");
4587 return;
4588 }
4589
4590 shell->fade.type = type;
4591
Jason Ekstranda7af7042013-10-12 22:38:11 -05004592 if (shell->fade.view == NULL) {
4593 shell->fade.view = shell_fade_create_surface(shell);
4594 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004595 return;
4596
Jason Ekstranda7af7042013-10-12 22:38:11 -05004597 shell->fade.view->alpha = 1.0 - tint;
4598 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004599 }
4600
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004601 if (shell->fade.view->output == NULL) {
4602 /* If the black view gets a NULL output, we lost the
4603 * last output and we'll just cancel the fade. This
4604 * happens when you close the last window under the
4605 * X11 or Wayland backends. */
4606 shell->locked = false;
4607 weston_surface_destroy(shell->fade.view->surface);
4608 shell->fade.view = NULL;
4609 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004610 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004611 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004612 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004613 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004614 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004615 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004616 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004617}
4618
4619static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004620do_shell_fade_startup(void *data)
4621{
4622 struct desktop_shell *shell = data;
4623
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004624 if (shell->startup_animation_type == ANIMATION_FADE)
4625 shell_fade(shell, FADE_IN);
4626 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004627 weston_surface_destroy(shell->fade.view->surface);
4628 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004629 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004630}
4631
4632static void
4633shell_fade_startup(struct desktop_shell *shell)
4634{
4635 struct wl_event_loop *loop;
4636
4637 if (!shell->fade.startup_timer)
4638 return;
4639
4640 wl_event_source_remove(shell->fade.startup_timer);
4641 shell->fade.startup_timer = NULL;
4642
4643 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4644 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4645}
4646
4647static int
4648fade_startup_timeout(void *data)
4649{
4650 struct desktop_shell *shell = data;
4651
4652 shell_fade_startup(shell);
4653 return 0;
4654}
4655
4656static void
4657shell_fade_init(struct desktop_shell *shell)
4658{
4659 /* Make compositor output all black, and wait for the desktop-shell
4660 * client to signal it is ready, then fade in. The timer triggers a
4661 * fade-in, in case the desktop-shell client takes too long.
4662 */
4663
4664 struct wl_event_loop *loop;
4665
Jason Ekstranda7af7042013-10-12 22:38:11 -05004666 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004667 weston_log("%s: warning: fade surface already exists\n",
4668 __func__);
4669 return;
4670 }
4671
Jason Ekstranda7af7042013-10-12 22:38:11 -05004672 shell->fade.view = shell_fade_create_surface(shell);
4673 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004674 return;
4675
Jason Ekstranda7af7042013-10-12 22:38:11 -05004676 weston_view_update_transform(shell->fade.view);
4677 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004678
4679 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4680 shell->fade.startup_timer =
4681 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4682 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4683}
4684
4685static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004686idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004687{
4688 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004689 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004690 struct weston_seat *seat;
4691
4692 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4693 if (seat->pointer)
4694 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004695
4696 shell_fade(shell, FADE_OUT);
4697 /* lock() is called from shell_fade_done() */
4698}
4699
4700static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004701wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004702{
4703 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004704 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004705
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004706 unlock(shell);
4707}
4708
4709static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004710center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004711{
Giulio Camuffob8366642013-04-25 13:57:46 +03004712 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004713 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004714
Jason Ekstranda7af7042013-10-12 22:38:11 -05004715 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004716
4717 x = output->x + (output->width - width) / 2 - surf_x / 2;
4718 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004719
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004720 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004721}
4722
4723static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004724weston_view_set_initial_position(struct weston_view *view,
4725 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004726{
4727 struct weston_compositor *compositor = shell->compositor;
4728 int ix = 0, iy = 0;
4729 int range_x, range_y;
4730 int dx, dy, x, y, panel_height;
4731 struct weston_output *output, *target_output = NULL;
4732 struct weston_seat *seat;
4733
4734 /* As a heuristic place the new window on the same output as the
4735 * pointer. Falling back to the output containing 0, 0.
4736 *
4737 * TODO: Do something clever for touch too?
4738 */
4739 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004740 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004741 ix = wl_fixed_to_int(seat->pointer->x);
4742 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004743 break;
4744 }
4745 }
4746
4747 wl_list_for_each(output, &compositor->output_list, link) {
4748 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4749 target_output = output;
4750 break;
4751 }
4752 }
4753
4754 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004755 weston_view_set_position(view, 10 + random() % 400,
4756 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004757 return;
4758 }
4759
4760 /* Valid range within output where the surface will still be onscreen.
4761 * If this is negative it means that the surface is bigger than
4762 * output.
4763 */
4764 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004765 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004766 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004767 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004768
Rob Bradford4cb88c72012-08-13 15:18:44 +01004769 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004770 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004771 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004772 dx = 0;
4773
4774 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004775 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004776 else
4777 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004778
4779 x = target_output->x + dx;
4780 y = target_output->y + dy;
4781
Jason Ekstranda7af7042013-10-12 22:38:11 -05004782 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004783}
4784
4785static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004786map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004787 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004788{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004789 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004790 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004791 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004792 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004793
Pekka Paalanen77346a62011-11-30 16:26:35 +02004794 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004795 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004796 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004797 if (shsurf->state.fullscreen) {
4798 center_on_output(shsurf->view, shsurf->fullscreen_output);
4799 shell_map_fullscreen(shsurf);
4800 } else if (shsurf->state.maximized) {
4801 /* use surface configure to set the geometry */
4802 panel_height = get_output_panel_height(shell, shsurf->output);
4803 surface_subsurfaces_boundingbox(shsurf->surface,
4804 &surf_x, &surf_y, NULL, NULL);
4805 weston_view_set_position(shsurf->view,
4806 shsurf->output->x - surf_x,
4807 shsurf->output->y +
4808 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004809 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004810 weston_view_set_initial_position(shsurf->view, shell);
4811 }
Juan Zhao96879df2012-02-07 08:45:41 +08004812 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004813 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004814 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004815 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004816 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004817 weston_view_set_position(shsurf->view,
4818 shsurf->view->geometry.x + sx,
4819 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004820 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004821 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004822 default:
4823 ;
4824 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004825
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004826 /* Surface stacking order, see also activate(). */
4827 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004828
Jason Ekstranda7af7042013-10-12 22:38:11 -05004829 if (shsurf->type != SHELL_SURFACE_NONE) {
4830 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004831 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004832 shsurf->surface->output = shsurf->output;
4833 shsurf->view->output = shsurf->output;
4834 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004835 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004836
Jason Ekstranda7af7042013-10-12 22:38:11 -05004837 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004838 /* XXX: xwayland's using the same fields for transient type */
4839 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004840 if (shsurf->transient.flags ==
4841 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4842 break;
4843 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004844 if (shsurf->state.relative &&
4845 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4846 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004847 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004848 break;
4849 wl_list_for_each(seat, &compositor->seat_list, link)
4850 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004851 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004852 case SHELL_SURFACE_POPUP:
4853 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004854 default:
4855 break;
4856 }
4857
Rafael Antognolli03b16592013-12-03 15:35:42 -02004858 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4859 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004860 {
4861 switch (shell->win_animation_type) {
4862 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004863 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004864 break;
4865 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004866 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004867 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004868 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004869 default:
4870 break;
4871 }
4872 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004873}
4874
4875static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004876configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004877 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004878{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004879 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004880 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004881 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004882
Pekka Paalanen77346a62011-11-30 16:26:35 +02004883 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004884
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004885 assert(shsurf);
4886
Rafael Antognolli03b16592013-12-03 15:35:42 -02004887 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004888 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004889 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004890 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004891 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4892 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004893 mx = shsurf->output->x - surf_x;
4894 my = shsurf->output->y +
4895 get_output_panel_height(shell,shsurf->output) - surf_y;
4896 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004897 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004898 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004899 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004900
Alex Wu4539b082012-03-01 12:57:46 +08004901 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004902 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004903 wl_list_for_each(view, &surface->views, surface_link)
4904 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004905
Rafael Antognolli03b16592013-12-03 15:35:42 -02004906 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004907 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004908 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004909}
4910
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004911static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004912shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004913{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004914 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004915 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004916 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004917
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004918 assert(shsurf);
4919
4920 shell = shsurf->shell;
4921
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004922 if (!weston_surface_is_mapped(es) &&
4923 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004924 remove_popup_grab(shsurf);
4925 }
4926
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004927 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004928 return;
4929
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004930 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004931 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004932 type_changed = 1;
4933 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004934
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004935 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004936 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004937 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004938 shsurf->last_width != es->width ||
4939 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004940 float from_x, from_y;
4941 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004942
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08004943 if (shsurf->resize_edges) {
4944 sx = 0;
4945 sy = 0;
4946 }
4947
4948 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
4949 sx = shsurf->last_width - es->width;
4950 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
4951 sy = shsurf->last_height - es->height;
4952
4953 shsurf->last_width = es->width;
4954 shsurf->last_height = es->height;
4955
Jason Ekstranda7af7042013-10-12 22:38:11 -05004956 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4957 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004958 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004959 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004960 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004961 }
4962}
4963
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004964static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004965
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004966static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004967desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004968{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004969 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004970 struct desktop_shell *shell =
4971 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004972
4973 shell->child.process.pid = 0;
4974 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004975
4976 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4977 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004978 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004979 shell->child.deathstamp = time;
4980 shell->child.deathcount = 0;
4981 }
4982
4983 shell->child.deathcount++;
4984 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004985 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004986 return;
4987 }
4988
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004989 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004990 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004991 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004992}
4993
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004994static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004995desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4996{
4997 struct desktop_shell *shell;
4998
4999 shell = container_of(listener, struct desktop_shell,
5000 child.client_destroy_listener);
5001
5002 shell->child.client = NULL;
5003}
5004
5005static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005006launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005007{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005008 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005009
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005010 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005011 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005012 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005013 desktop_shell_sigchld);
5014
5015 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005016 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005017
5018 shell->child.client_destroy_listener.notify =
5019 desktop_shell_client_destroy;
5020 wl_client_add_destroy_listener(shell->child.client,
5021 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005022}
5023
5024static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005025handle_shell_client_destroy(struct wl_listener *listener, void *data)
5026{
5027 struct shell_client *sc =
5028 container_of(listener, struct shell_client, destroy_listener);
5029
5030 if (sc->ping_timer)
5031 wl_event_source_remove(sc->ping_timer);
5032 free(sc);
5033}
5034
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005035static struct shell_client *
5036shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5037 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005038{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005039 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005040
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005041 sc = zalloc(sizeof *sc);
5042 if (sc == NULL) {
5043 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005044 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005045 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005046
5047 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005048 if (sc->resource == NULL) {
5049 free(sc);
5050 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005051 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005052 }
5053
5054 sc->client = client;
5055 sc->shell = shell;
5056 sc->destroy_listener.notify = handle_shell_client_destroy;
5057 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5058
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005059 return sc;
5060}
5061
5062static void
5063bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5064{
5065 struct desktop_shell *shell = data;
5066 struct shell_client *sc;
5067
5068 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5069 if (sc)
5070 wl_resource_set_implementation(sc->resource,
5071 &shell_implementation,
5072 shell, NULL);
5073}
5074
5075static void
5076bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5077{
5078 struct desktop_shell *shell = data;
5079 struct shell_client *sc;
5080
5081 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5082 if (sc)
5083 wl_resource_set_dispatcher(sc->resource,
5084 xdg_shell_unversioned_dispatch,
5085 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005086}
5087
5088static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005089unbind_desktop_shell(struct wl_resource *resource)
5090{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005091 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005092
5093 if (shell->locked)
5094 resume_desktop(shell);
5095
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005096 shell->child.desktop_shell = NULL;
5097 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005098}
5099
5100static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005101bind_desktop_shell(struct wl_client *client,
5102 void *data, uint32_t version, uint32_t id)
5103{
Tiago Vignattibe143262012-04-16 17:31:41 +03005104 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005105 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005106
Jason Ekstranda85118c2013-06-27 20:17:02 -05005107 resource = wl_resource_create(client, &desktop_shell_interface,
5108 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005109
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005110 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005111 wl_resource_set_implementation(resource,
5112 &desktop_shell_implementation,
5113 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005114 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005115
5116 if (version < 2)
5117 shell_fade_startup(shell);
5118
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005119 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005120 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005121
5122 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5123 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005124 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005125}
5126
Pekka Paalanen6e168112011-11-24 11:34:05 +02005127static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005128screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005129{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005130 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005131 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005132
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005133 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005134 return;
5135
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005136 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005137 if (!shell->locked)
5138 return;
5139
Jason Ekstranda7af7042013-10-12 22:38:11 -05005140 view = container_of(surface->views.next, struct weston_view, surface_link);
5141 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005142
Jason Ekstranda7af7042013-10-12 22:38:11 -05005143 if (wl_list_empty(&view->layer_link)) {
5144 wl_list_insert(shell->lock_layer.view_list.prev,
5145 &view->layer_link);
5146 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005147 wl_event_source_timer_update(shell->screensaver.timer,
5148 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005149 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005150 }
5151}
5152
5153static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005154screensaver_set_surface(struct wl_client *client,
5155 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005156 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005157 struct wl_resource *output_resource)
5158{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005159 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005160 struct weston_surface *surface =
5161 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005162 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005163 struct weston_view *view, *next;
5164
5165 /* Make sure we only have one view */
5166 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5167 weston_view_destroy(view);
5168 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005169
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005170 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005171 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005172 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005173}
5174
5175static const struct screensaver_interface screensaver_implementation = {
5176 screensaver_set_surface
5177};
5178
5179static void
5180unbind_screensaver(struct wl_resource *resource)
5181{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005182 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005183
Pekka Paalanen77346a62011-11-30 16:26:35 +02005184 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005185}
5186
5187static void
5188bind_screensaver(struct wl_client *client,
5189 void *data, uint32_t version, uint32_t id)
5190{
Tiago Vignattibe143262012-04-16 17:31:41 +03005191 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005192 struct wl_resource *resource;
5193
Jason Ekstranda85118c2013-06-27 20:17:02 -05005194 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005195
Pekka Paalanen77346a62011-11-30 16:26:35 +02005196 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005197 wl_resource_set_implementation(resource,
5198 &screensaver_implementation,
5199 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005200 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005201 return;
5202 }
5203
5204 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5205 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005206 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005207}
5208
Kristian Høgsberg07045392012-02-19 18:52:44 -05005209struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005210 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005211 struct weston_surface *current;
5212 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005213 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005214 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005215};
5216
5217static void
5218switcher_next(struct switcher *switcher)
5219{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005220 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005221 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005222 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005223 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005224
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005225 /* temporary re-display minimized surfaces */
5226 struct weston_view *tmp;
5227 struct weston_view **minimized;
5228 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5229 wl_list_remove(&view->layer_link);
5230 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5231 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5232 *minimized = view;
5233 }
5234
Jason Ekstranda7af7042013-10-12 22:38:11 -05005235 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005236 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005237 if (shsurf &&
5238 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5239 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005240 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005241 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005242 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005243 next = view->surface;
5244 prev = view->surface;
5245 view->alpha = 0.25;
5246 weston_view_geometry_dirty(view);
5247 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005248 }
Alex Wu1659daa2012-04-01 20:13:09 +08005249
Jason Ekstranda7af7042013-10-12 22:38:11 -05005250 if (is_black_surface(view->surface, NULL)) {
5251 view->alpha = 0.25;
5252 weston_view_geometry_dirty(view);
5253 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005254 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005255 }
5256
5257 if (next == NULL)
5258 next = first;
5259
Alex Wu07b26062012-03-12 16:06:01 +08005260 if (next == NULL)
5261 return;
5262
Kristian Høgsberg07045392012-02-19 18:52:44 -05005263 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005264 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005265
5266 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005267 wl_list_for_each(view, &next->views, surface_link)
5268 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005269
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005270 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005271 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005272 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005273}
5274
5275static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005276switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005277{
5278 struct switcher *switcher =
5279 container_of(listener, struct switcher, listener);
5280
5281 switcher_next(switcher);
5282}
5283
5284static void
Daniel Stone351eb612012-05-31 15:27:47 -04005285switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005286{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005287 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005288 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005289 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005290
Jason Ekstranda7af7042013-10-12 22:38:11 -05005291 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005292 if (is_focus_view(view))
5293 continue;
5294
Jason Ekstranda7af7042013-10-12 22:38:11 -05005295 view->alpha = 1.0;
5296 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005297 }
5298
Alex Wu07b26062012-03-12 16:06:01 +08005299 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005300 activate(switcher->shell, switcher->current,
5301 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005302 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005303 weston_keyboard_end_grab(keyboard);
5304 if (keyboard->input_method_resource)
5305 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005306
5307 /* re-hide surfaces that were temporary shown during the switch */
5308 struct weston_view **minimized;
5309 wl_array_for_each(minimized, &switcher->minimized_array) {
5310 /* with the exception of the current selected */
5311 if ((*minimized)->surface != switcher->current) {
5312 wl_list_remove(&(*minimized)->layer_link);
5313 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5314 weston_view_damage_below(*minimized);
5315 }
5316 }
5317 wl_array_release(&switcher->minimized_array);
5318
Kristian Høgsberg07045392012-02-19 18:52:44 -05005319 free(switcher);
5320}
5321
5322static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005323switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005324 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005325{
5326 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005327 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005328
Daniel Stonec9785ea2012-05-30 16:31:52 +01005329 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005330 switcher_next(switcher);
5331}
5332
5333static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005334switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005335 uint32_t mods_depressed, uint32_t mods_latched,
5336 uint32_t mods_locked, uint32_t group)
5337{
5338 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005339 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005340
Daniel Stone351eb612012-05-31 15:27:47 -04005341 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5342 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005343}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005344
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005345static void
5346switcher_cancel(struct weston_keyboard_grab *grab)
5347{
5348 struct switcher *switcher = container_of(grab, struct switcher, grab);
5349
5350 switcher_destroy(switcher);
5351}
5352
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005353static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005354 switcher_key,
5355 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005356 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005357};
5358
5359static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005360switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005361 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005362{
Tiago Vignattibe143262012-04-16 17:31:41 +03005363 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005364 struct switcher *switcher;
5365
5366 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005367 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005368 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005369 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005370 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005371 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005372
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005373 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005374 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005375 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005376 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5377 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005378 switcher_next(switcher);
5379}
5380
Pekka Paalanen3c647232011-12-22 13:43:43 +02005381static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005382backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005383 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005384{
5385 struct weston_compositor *compositor = data;
5386 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005387 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005388
5389 /* TODO: we're limiting to simple use cases, where we assume just
5390 * control on the primary display. We'd have to extend later if we
5391 * ever get support for setting backlights on random desktop LCD
5392 * panels though */
5393 output = get_default_output(compositor);
5394 if (!output)
5395 return;
5396
5397 if (!output->set_backlight)
5398 return;
5399
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005400 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5401 backlight_new = output->backlight_current - 25;
5402 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5403 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005404
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005405 if (backlight_new < 5)
5406 backlight_new = 5;
5407 if (backlight_new > 255)
5408 backlight_new = 255;
5409
5410 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005411 output->set_backlight(output, output->backlight_current);
5412}
5413
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005414struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005415 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005416 struct weston_seat *seat;
5417 uint32_t key[2];
5418 int key_released[2];
5419};
5420
5421static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005422debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005423 uint32_t key, uint32_t state)
5424{
5425 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005426 struct weston_compositor *ec = db->seat->compositor;
5427 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005428 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005429 uint32_t serial;
5430 int send = 0, terminate = 0;
5431 int check_binding = 1;
5432 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005433 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005434
5435 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5436 /* Do not run bindings on key releases */
5437 check_binding = 0;
5438
5439 for (i = 0; i < 2; i++)
5440 if (key == db->key[i])
5441 db->key_released[i] = 1;
5442
5443 if (db->key_released[0] && db->key_released[1]) {
5444 /* All key releases been swalled so end the grab */
5445 terminate = 1;
5446 } else if (key != db->key[0] && key != db->key[1]) {
5447 /* Should not swallow release of other keys */
5448 send = 1;
5449 }
5450 } else if (key == db->key[0] && !db->key_released[0]) {
5451 /* Do not check bindings for the first press of the binding
5452 * key. This allows it to be used as a debug shortcut.
5453 * We still need to swallow this event. */
5454 check_binding = 0;
5455 } else if (db->key[1]) {
5456 /* If we already ran a binding don't process another one since
5457 * we can't keep track of all the binding keys that were
5458 * pressed in order to swallow the release events. */
5459 send = 1;
5460 check_binding = 0;
5461 }
5462
5463 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005464 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5465 key, state)) {
5466 /* We ran a binding so swallow the press and keep the
5467 * grab to swallow the released too. */
5468 send = 0;
5469 terminate = 0;
5470 db->key[1] = key;
5471 } else {
5472 /* Terminate the grab since the key pressed is not a
5473 * debug binding key. */
5474 send = 1;
5475 terminate = 1;
5476 }
5477 }
5478
5479 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005480 serial = wl_display_next_serial(display);
5481 resource_list = &grab->keyboard->focus_resource_list;
5482 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005483 wl_keyboard_send_key(resource, serial, time, key, state);
5484 }
5485 }
5486
5487 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005488 weston_keyboard_end_grab(grab->keyboard);
5489 if (grab->keyboard->input_method_resource)
5490 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005491 free(db);
5492 }
5493}
5494
5495static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005496debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005497 uint32_t mods_depressed, uint32_t mods_latched,
5498 uint32_t mods_locked, uint32_t group)
5499{
5500 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005501 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005502
Neil Roberts96d790e2013-09-19 17:32:00 +01005503 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005504
Neil Roberts96d790e2013-09-19 17:32:00 +01005505 wl_resource_for_each(resource, resource_list) {
5506 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5507 mods_latched, mods_locked, group);
5508 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005509}
5510
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005511static void
5512debug_binding_cancel(struct weston_keyboard_grab *grab)
5513{
5514 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5515
5516 weston_keyboard_end_grab(grab->keyboard);
5517 free(db);
5518}
5519
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005520struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005521 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005522 debug_binding_modifiers,
5523 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005524};
5525
5526static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005527debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005528{
5529 struct debug_binding_grab *grab;
5530
5531 grab = calloc(1, sizeof *grab);
5532 if (!grab)
5533 return;
5534
5535 grab->seat = (struct weston_seat *) seat;
5536 grab->key[0] = key;
5537 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005538 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005539}
5540
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005541static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005542force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005543 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005544{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005545 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005546 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005547 struct desktop_shell *shell = data;
5548 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005549 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005550
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005551 focus_surface = seat->keyboard->focus;
5552 if (!focus_surface)
5553 return;
5554
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005555 wl_signal_emit(&compositor->kill_signal, focus_surface);
5556
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005557 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005558 wl_client_get_credentials(client, &pid, NULL, NULL);
5559
5560 /* Skip clients that we launched ourselves (the credentials of
5561 * the socketpair is ours) */
5562 if (pid == getpid())
5563 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005564
Daniel Stone325fc2d2012-05-30 16:31:58 +01005565 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005566}
5567
5568static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005569workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005570 uint32_t key, void *data)
5571{
5572 struct desktop_shell *shell = data;
5573 unsigned int new_index = shell->workspaces.current;
5574
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005575 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005576 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005577 if (new_index != 0)
5578 new_index--;
5579
5580 change_workspace(shell, new_index);
5581}
5582
5583static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005584workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005585 uint32_t key, void *data)
5586{
5587 struct desktop_shell *shell = data;
5588 unsigned int new_index = shell->workspaces.current;
5589
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005590 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005591 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005592 if (new_index < shell->workspaces.num - 1)
5593 new_index++;
5594
5595 change_workspace(shell, new_index);
5596}
5597
5598static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005599workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005600 uint32_t key, void *data)
5601{
5602 struct desktop_shell *shell = data;
5603 unsigned int new_index;
5604
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005605 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005606 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005607 new_index = key - KEY_F1;
5608 if (new_index >= shell->workspaces.num)
5609 new_index = shell->workspaces.num - 1;
5610
5611 change_workspace(shell, new_index);
5612}
5613
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005614static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005615workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005616 uint32_t key, void *data)
5617{
5618 struct desktop_shell *shell = data;
5619 unsigned int new_index = shell->workspaces.current;
5620
5621 if (shell->locked)
5622 return;
5623
5624 if (new_index != 0)
5625 new_index--;
5626
5627 take_surface_to_workspace_by_seat(shell, seat, new_index);
5628}
5629
5630static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005631workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005632 uint32_t key, void *data)
5633{
5634 struct desktop_shell *shell = data;
5635 unsigned int new_index = shell->workspaces.current;
5636
5637 if (shell->locked)
5638 return;
5639
5640 if (new_index < shell->workspaces.num - 1)
5641 new_index++;
5642
5643 take_surface_to_workspace_by_seat(shell, seat, new_index);
5644}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005645
5646static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005647shell_reposition_view_on_output_destroy(struct weston_view *view)
5648{
5649 struct weston_output *output, *first_output;
5650 struct weston_compositor *ec = view->surface->compositor;
5651 struct shell_surface *shsurf;
5652 float x, y;
5653 int visible;
5654
5655 x = view->geometry.x;
5656 y = view->geometry.y;
5657
5658 /* At this point the destroyed output is not in the list anymore.
5659 * If the view is still visible somewhere, we leave where it is,
5660 * otherwise, move it to the first output. */
5661 visible = 0;
5662 wl_list_for_each(output, &ec->output_list, link) {
5663 if (pixman_region32_contains_point(&output->region,
5664 x, y, NULL)) {
5665 visible = 1;
5666 break;
5667 }
5668 }
5669
5670 if (!visible) {
5671 first_output = container_of(ec->output_list.next,
5672 struct weston_output, link);
5673
5674 x = first_output->x + first_output->width / 4;
5675 y = first_output->y + first_output->height / 4;
5676 }
5677
5678 weston_view_set_position(view, x, y);
5679
5680 shsurf = get_shell_surface(view->surface);
5681
5682 if (shsurf) {
5683 shsurf->saved_position_valid = false;
5684 shsurf->next_state.maximized = false;
5685 shsurf->next_state.fullscreen = false;
5686 shsurf->state_changed = true;
5687 }
5688}
5689
5690static void
5691shell_reposition_views_on_output_destroy(struct shell_output *shell_output)
5692{
5693 struct desktop_shell *shell = shell_output->shell;
5694 struct weston_output *output = shell_output->output;
5695 struct weston_layer *layer;
5696 struct weston_view *view;
5697
5698 /* Move all views in the layers owned by the shell */
5699 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5700 wl_list_for_each(view, &layer->view_list, layer_link) {
5701 if (view->output != output)
5702 continue;
5703
5704 shell_reposition_view_on_output_destroy(view);
5705 }
5706
5707 /* We don't start from the beggining of the layer list, so
5708 * make sure we don't wrap around it. */
5709 if (layer == &shell->background_layer)
5710 break;
5711 }
5712}
5713
5714static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005715handle_output_destroy(struct wl_listener *listener, void *data)
5716{
5717 struct shell_output *output_listener =
5718 container_of(listener, struct shell_output, destroy_listener);
5719
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005720 shell_reposition_views_on_output_destroy(output_listener);
5721
Xiong Zhang6b481422013-10-23 13:58:32 +08005722 wl_list_remove(&output_listener->destroy_listener.link);
5723 wl_list_remove(&output_listener->link);
5724 free(output_listener);
5725}
5726
5727static void
5728create_shell_output(struct desktop_shell *shell,
5729 struct weston_output *output)
5730{
5731 struct shell_output *shell_output;
5732
5733 shell_output = zalloc(sizeof *shell_output);
5734 if (shell_output == NULL)
5735 return;
5736
5737 shell_output->output = output;
5738 shell_output->shell = shell;
5739 shell_output->destroy_listener.notify = handle_output_destroy;
5740 wl_signal_add(&output->destroy_signal,
5741 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005742 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005743}
5744
5745static void
5746handle_output_create(struct wl_listener *listener, void *data)
5747{
5748 struct desktop_shell *shell =
5749 container_of(listener, struct desktop_shell, output_create_listener);
5750 struct weston_output *output = (struct weston_output *)data;
5751
5752 create_shell_output(shell, output);
5753}
5754
5755static void
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005756handle_output_move(struct wl_listener *listener, void *data)
5757{
5758 struct desktop_shell *shell;
5759 struct weston_output *output;
5760 struct weston_layer *layer;
5761 struct weston_view *view;
5762 float x, y;
5763
5764 shell = container_of(listener, struct desktop_shell,
5765 output_move_listener);
5766 output = data;
5767
5768 /* Move all views in the layers owned by the shell */
5769 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5770 wl_list_for_each(view, &layer->view_list, layer_link) {
5771 if (view->output != output)
5772 continue;
5773
5774 x = view->geometry.x + output->move_x;
5775 y = view->geometry.y + output->move_y;
5776 weston_view_set_position(view, x, y);
5777 }
5778
5779 /* We don't start from the beggining of the layer list, so
5780 * make sure we don't wrap around it. */
5781 if (layer == &shell->background_layer)
5782 break;
5783 }
5784}
5785
5786static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005787setup_output_destroy_handler(struct weston_compositor *ec,
5788 struct desktop_shell *shell)
5789{
5790 struct weston_output *output;
5791
5792 wl_list_init(&shell->output_list);
5793 wl_list_for_each(output, &ec->output_list, link)
5794 create_shell_output(shell, output);
5795
5796 shell->output_create_listener.notify = handle_output_create;
5797 wl_signal_add(&ec->output_created_signal,
5798 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005799
5800 shell->output_move_listener.notify = handle_output_move;
5801 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005802}
5803
5804static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005805shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005806{
Tiago Vignattibe143262012-04-16 17:31:41 +03005807 struct desktop_shell *shell =
5808 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005809 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005810 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005811
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005812 /* Force state to unlocked so we don't try to fade */
5813 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005814 if (shell->child.client)
5815 wl_client_destroy(shell->child.client);
5816
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005817 wl_list_remove(&shell->idle_listener.link);
5818 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005819
5820 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005821
Xiong Zhang6b481422013-10-23 13:58:32 +08005822 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5823 wl_list_remove(&shell_output->destroy_listener.link);
5824 wl_list_remove(&shell_output->link);
5825 free(shell_output);
5826 }
5827
5828 wl_list_remove(&shell->output_create_listener.link);
5829
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005830 wl_array_for_each(ws, &shell->workspaces.array)
5831 workspace_destroy(*ws);
5832 wl_array_release(&shell->workspaces.array);
5833
Pekka Paalanen3c647232011-12-22 13:43:43 +02005834 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005835 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005836 free(shell);
5837}
5838
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005839static void
5840shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5841{
5842 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005843 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005844
5845 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005846 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5847 MODIFIER_CTRL | MODIFIER_ALT,
5848 terminate_binding, ec);
5849 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5850 click_to_activate_binding,
5851 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005852 weston_compositor_add_touch_binding(ec, 0,
5853 touch_to_activate_binding,
5854 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005855 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5856 MODIFIER_SUPER | MODIFIER_ALT,
5857 surface_opacity_binding, NULL);
5858 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5859 MODIFIER_SUPER, zoom_axis_binding,
5860 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005861
5862 /* configurable bindings */
5863 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005864 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5865 zoom_key_binding, NULL);
5866 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5867 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005868 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5869 maximize_binding, NULL);
5870 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5871 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005872 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5873 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005874 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005875 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5876 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005877 weston_compositor_add_button_binding(ec, BTN_LEFT,
5878 mod | MODIFIER_SHIFT,
5879 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005880
5881 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5882 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5883 rotate_binding, NULL);
5884
Daniel Stone325fc2d2012-05-30 16:31:58 +01005885 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5886 shell);
5887 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5888 ec);
5889 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5890 backlight_binding, ec);
5891 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5892 ec);
5893 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5894 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005895 weston_compositor_add_key_binding(ec, KEY_K, mod,
5896 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005897 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5898 workspace_up_binding, shell);
5899 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5900 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005901 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5902 workspace_move_surface_up_binding,
5903 shell);
5904 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5905 workspace_move_surface_down_binding,
5906 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005907
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005908 if (shell->exposay_modifier)
5909 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5910 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005911
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005912 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5913 if (shell->workspaces.num > 1) {
5914 num_workspace_bindings = shell->workspaces.num;
5915 if (num_workspace_bindings > 6)
5916 num_workspace_bindings = 6;
5917 for (i = 0; i < num_workspace_bindings; i++)
5918 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5919 workspace_f_binding,
5920 shell);
5921 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005922
5923 /* Debug bindings */
5924 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5925 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005926}
5927
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005928WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005929module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005930 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005931{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005932 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005933 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005934 struct workspace **pws;
5935 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005936 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005937
Peter Huttererf3d62272013-08-08 11:57:05 +10005938 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005939 if (shell == NULL)
5940 return -1;
5941
Kristian Høgsberg75840622011-09-06 13:48:16 -04005942 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005943
5944 shell->destroy_listener.notify = shell_destroy;
5945 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005946 shell->idle_listener.notify = idle_handler;
5947 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5948 shell->wake_listener.notify = wake_handler;
5949 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005950
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005951 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005952 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005953 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005954 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005955 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005956 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005957 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005958 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005959 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005960 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005961
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005962 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5963 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005964 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5965 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005966 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005967
5968 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005969 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005970
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005971 if (input_panel_setup(shell) < 0)
5972 return -1;
5973
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005974 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005975
Daniel Stonedf8133b2013-11-19 11:37:14 +01005976 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5977 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5978
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005979 for (i = 0; i < shell->workspaces.num; i++) {
5980 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5981 if (pws == NULL)
5982 return -1;
5983
5984 *pws = workspace_create();
5985 if (*pws == NULL)
5986 return -1;
5987 }
5988 activate_workspace(shell, 0);
5989
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005990 weston_layer_init(&shell->minimized_layer, NULL);
5991
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005992 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005993 wl_list_init(&shell->workspaces.animation.link);
5994 shell->workspaces.animation.frame = animate_workspace_change_frame;
5995
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005996 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005997 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005998 return -1;
5999
Rafael Antognollie2a34552013-12-03 15:35:45 -02006000 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6001 shell, bind_xdg_shell) == NULL)
6002 return -1;
6003
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006004 if (wl_global_create(ec->wl_display,
6005 &desktop_shell_interface, 2,
6006 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006007 return -1;
6008
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006009 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6010 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006011 return -1;
6012
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006013 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6014 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006015 return -1;
6016
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006017 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006018
Xiong Zhang6b481422013-10-23 13:58:32 +08006019 setup_output_destroy_handler(ec, shell);
6020
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006021 loop = wl_display_get_event_loop(ec->wl_display);
6022 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006023
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006024 shell->screensaver.timer =
6025 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6026
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006027 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006028 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006029 create_keyboard_focus_listener(seat);
6030 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006031
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006032 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006033
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006034 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006035
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006036 return 0;
6037}