blob: 7f1c8b1d3f016589b82c4c7f73a4cdd65b0f3c3a [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050059enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020060 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030062 SHELL_SURFACE_POPUP,
63 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020064};
65
Philip Withnall648a4dd2013-11-25 18:01:44 +000066/*
67 * Surface stacking and ordering.
68 *
69 * This is handled using several linked lists of surfaces, organised into
70 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
71 * above all of the surfaces in the layer below. The set of layers is static and
72 * in the following order (top-most first):
73 * • Lock layer (only ever displayed on its own)
74 * • Cursor layer
75 * • Fullscreen layer
76 * • Panel layer
77 * • Input panel layer
78 * • Workspace layers
79 * • Background layer
80 *
81 * The list of layers may be manipulated to remove whole layers of surfaces from
82 * display. For example, when locking the screen, all layers except the lock
83 * layer are removed.
84 *
85 * A surface’s layer is modified on configuring the surface, in
86 * set_surface_type() (which is only called when the surface’s type change is
87 * _committed_). If a surface’s type changes (e.g. when making a window
88 * fullscreen) its layer changes too.
89 *
90 * In order to allow popup and transient surfaces to be correctly stacked above
91 * their parent surfaces, each surface tracks both its parent surface, and a
92 * linked list of its children. When a surface’s layer is updated, so are the
93 * layers of its children. Note that child surfaces are *not* the same as
94 * subsurfaces — child/parent surfaces are purely for maintaining stacking
95 * order.
96 *
97 * The children_link list of siblings of a surface (i.e. those surfaces which
98 * have the same parent) only contains weston_surfaces which have a
99 * shell_surface. Stacking is not implemented for non-shell_surface
100 * weston_surfaces. This means that the following implication does *not* hold:
101 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
102 */
103
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200104struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500105 struct wl_resource *resource;
106 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200107
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500109 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600110 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700112 struct wl_listener resource_destroy_listener;
113
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400114 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000115 struct wl_list children_list; /* child surfaces of this one */
116 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300117 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200118
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800119 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400120 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500121 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200122 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800123 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200124 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800125 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700126 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800127 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100128
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500129 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200130 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500131 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200132 } rotation;
133
134 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100135 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500136 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100137 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400138 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 } popup;
140
Alex Wu4539b082012-03-01 12:57:46 +0800141 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300142 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400143 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300144 } transient;
145
146 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800147 enum wl_shell_surface_fullscreen_method type;
148 struct weston_transform transform; /* matrix from x, y */
149 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500150 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800151 } fullscreen;
152
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200153 struct weston_transform workspace_transform;
154
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500155 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200157 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100158 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400159
160 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200161
162 struct {
163 bool maximized;
164 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200165 bool relative;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500166 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200167 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500168 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500169
170 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200171};
172
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300173struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400174 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300175 struct shell_surface *shsurf;
176 struct wl_listener shsurf_destroy_listener;
177};
178
Rusty Lynch1084da52013-08-15 09:10:08 -0700179struct shell_touch_grab {
180 struct weston_touch_grab grab;
181 struct shell_surface *shsurf;
182 struct wl_listener shsurf_destroy_listener;
183 struct weston_touch *touch;
184};
185
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300186struct weston_move_grab {
187 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100188 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500189};
190
Rusty Lynch1084da52013-08-15 09:10:08 -0700191struct weston_touch_move_grab {
192 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800193 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700194 wl_fixed_t dx, dy;
195};
196
Pekka Paalanen460099f2012-01-20 16:48:25 +0200197struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300198 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500199 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200200 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200201 float x;
202 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 } center;
204};
205
Giulio Camuffo5085a752013-03-25 21:42:45 +0100206struct shell_seat {
207 struct weston_seat *seat;
208 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500209 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100210
211 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400212 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100213 struct wl_list surfaces_list;
214 struct wl_client *client;
215 int32_t initial_up;
216 } popup_grab;
217};
218
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800219struct shell_client {
220 struct wl_resource *resource;
221 struct wl_client *client;
222 struct desktop_shell *shell;
223 struct wl_listener destroy_listener;
224 struct wl_event_source *ping_timer;
225 uint32_t ping_serial;
226 int unresponsive;
227};
228
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100229void
230set_alpha_if_fullscreen(struct shell_surface *shsurf)
231{
232 if (shsurf && shsurf->state.fullscreen)
233 shsurf->fullscreen.black_view->alpha = 0.25;
234}
235
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -0800236static struct shell_client *
237get_shell_client(struct wl_client *client);
238
Alex Wubd3354b2012-04-17 17:20:49 +0800239static struct desktop_shell *
240shell_surface_get_shell(struct shell_surface *shsurf);
241
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500242static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400243surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500244
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300245static void
246shell_fade_startup(struct desktop_shell *shell);
247
Philip Withnallbecb77e2013-11-25 18:01:30 +0000248static struct shell_seat *
249get_shell_seat(struct weston_seat *seat);
250
Philip Withnall648a4dd2013-11-25 18:01:44 +0000251static void
252shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
253
Alex Wubd3354b2012-04-17 17:20:49 +0800254static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200255shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
256
257static bool
258shell_surface_is_xdg_surface(struct shell_surface *shsurf);
259
260static bool
261shell_surface_is_xdg_popup(struct shell_surface *shsurf);
262
263static void
264shell_surface_set_parent(struct shell_surface *shsurf,
265 struct weston_surface *parent);
266
267static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800268shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
269{
270 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800272
273 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100274
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800276 return false;
277
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
279 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800280 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800282}
283
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500284static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400285destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300286{
287 struct shell_grab *grab;
288
289 grab = container_of(listener, struct shell_grab,
290 shsurf_destroy_listener);
291
292 grab->shsurf = NULL;
293}
294
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800295struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500296get_default_view(struct weston_surface *surface)
297{
298 struct shell_surface *shsurf;
299 struct weston_view *view;
300
301 if (!surface || wl_list_empty(&surface->views))
302 return NULL;
303
304 shsurf = get_shell_surface(surface);
305 if (shsurf)
306 return shsurf->view;
307
308 wl_list_for_each(view, &surface->views, surface_link)
309 if (weston_view_is_mapped(view))
310 return view;
311
312 return container_of(surface->views.next, struct weston_view, surface_link);
313}
314
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300315static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400316popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400317
318static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400322 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300324{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300325 struct desktop_shell *shell = shsurf->shell;
326
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400327 popup_grab_end(pointer);
328
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300329 grab->grab.interface = interface;
330 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400331 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500332 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400333 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300334
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700335 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400336 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400337 if (shell->child.desktop_shell) {
338 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
339 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 weston_pointer_set_focus(pointer,
341 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400342 wl_fixed_from_int(0),
343 wl_fixed_from_int(0));
344 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345}
346
347static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300348shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300349{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700350 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400351 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700352 grab->shsurf->grabbed = 0;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800353 grab->shsurf->resize_edges = 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700354 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300355
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700356 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300357}
358
359static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700360shell_touch_grab_start(struct shell_touch_grab *grab,
361 const struct weston_touch_grab_interface *interface,
362 struct shell_surface *shsurf,
363 struct weston_touch *touch)
364{
365 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800366
Rusty Lynch1084da52013-08-15 09:10:08 -0700367 grab->grab.interface = interface;
368 grab->shsurf = shsurf;
369 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
370 wl_signal_add(&shsurf->destroy_signal,
371 &grab->shsurf_destroy_listener);
372
373 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700374 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700375
376 weston_touch_start_grab(touch, &grab->grab);
377 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500378 weston_touch_set_focus(touch->seat,
379 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700380}
381
382static void
383shell_touch_grab_end(struct shell_touch_grab *grab)
384{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700385 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700386 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700387 grab->shsurf->grabbed = 0;
388 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700389
390 weston_touch_end_grab(grab->touch);
391}
392
393static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500394center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800395 struct weston_output *output);
396
Daniel Stone496ca172012-05-30 16:31:42 +0100397static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300398get_modifier(char *modifier)
399{
400 if (!modifier)
401 return MODIFIER_SUPER;
402
403 if (!strcmp("ctrl", modifier))
404 return MODIFIER_CTRL;
405 else if (!strcmp("alt", modifier))
406 return MODIFIER_ALT;
407 else if (!strcmp("super", modifier))
408 return MODIFIER_SUPER;
409 else
410 return MODIFIER_SUPER;
411}
412
Juan Zhaoe10d2792012-04-25 19:09:52 +0800413static enum animation_type
414get_animation_type(char *animation)
415{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800416 if (!animation)
417 return ANIMATION_NONE;
418
Juan Zhaoe10d2792012-04-25 19:09:52 +0800419 if (!strcmp("zoom", animation))
420 return ANIMATION_ZOOM;
421 else if (!strcmp("fade", animation))
422 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100423 else if (!strcmp("dim-layer", animation))
424 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800425 else
426 return ANIMATION_NONE;
427}
428
Alex Wu4539b082012-03-01 12:57:46 +0800429static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400430shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200431{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400432 struct weston_config_section *section;
433 int duration;
434 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200435
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400436 section = weston_config_get_section(shell->compositor->config,
437 "screensaver", NULL, NULL);
438 weston_config_section_get_string(section,
439 "path", &shell->screensaver.path, NULL);
440 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200441 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400442
443 section = weston_config_get_section(shell->compositor->config,
444 "shell", NULL, NULL);
445 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100446 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100447 shell->client = s;
448 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400449 "binding-modifier", &s, "super");
450 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200451 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800452
453 weston_config_section_get_string(section,
454 "exposay-modifier", &s, "none");
455 if (strcmp(s, "none") == 0)
456 shell->exposay_modifier = 0;
457 else
458 shell->exposay_modifier = get_modifier(s);
459 free(s);
460
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400461 weston_config_section_get_string(section, "animation", &s, "none");
462 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200463 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700464 weston_config_section_get_string(section,
465 "startup-animation", &s, "fade");
466 shell->startup_animation_type = get_animation_type(s);
467 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700468 if (shell->startup_animation_type == ANIMATION_ZOOM)
469 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100470 weston_config_section_get_string(section, "focus-animation", &s, "none");
471 shell->focus_animation_type = get_animation_type(s);
472 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400473 weston_config_section_get_uint(section, "num-workspaces",
474 &shell->workspaces.num,
475 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200476}
477
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800478struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100479get_default_output(struct weston_compositor *compositor)
480{
481 return container_of(compositor->output_list.next,
482 struct weston_output, link);
483}
484
485
486/* no-op func for checking focus surface */
487static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600488focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100489{
490}
491
492static struct focus_surface *
493get_focus_surface(struct weston_surface *surface)
494{
495 if (surface->configure == focus_surface_configure)
496 return surface->configure_private;
497 else
498 return NULL;
499}
500
501static bool
502is_focus_surface (struct weston_surface *es)
503{
504 return (es->configure == focus_surface_configure);
505}
506
507static bool
508is_focus_view (struct weston_view *view)
509{
510 return is_focus_surface (view->surface);
511}
512
513static struct focus_surface *
514create_focus_surface(struct weston_compositor *ec,
515 struct weston_output *output)
516{
517 struct focus_surface *fsurf = NULL;
518 struct weston_surface *surface = NULL;
519
520 fsurf = malloc(sizeof *fsurf);
521 if (!fsurf)
522 return NULL;
523
524 fsurf->surface = weston_surface_create(ec);
525 surface = fsurf->surface;
526 if (surface == NULL) {
527 free(fsurf);
528 return NULL;
529 }
530
531 surface->configure = focus_surface_configure;
532 surface->output = output;
533 surface->configure_private = fsurf;
534
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800535 fsurf->view = weston_view_create(surface);
536 if (fsurf->view == NULL) {
537 weston_surface_destroy(surface);
538 free(fsurf);
539 return NULL;
540 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100541 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100542
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600543 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600544 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100545 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
546 pixman_region32_fini(&surface->opaque);
547 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
548 output->width, output->height);
549 pixman_region32_fini(&surface->input);
550 pixman_region32_init(&surface->input);
551
552 wl_list_init(&fsurf->workspace_transform.link);
553
554 return fsurf;
555}
556
557static void
558focus_surface_destroy(struct focus_surface *fsurf)
559{
560 weston_surface_destroy(fsurf->surface);
561 free(fsurf);
562}
563
564static void
565focus_animation_done(struct weston_view_animation *animation, void *data)
566{
567 struct workspace *ws = data;
568
569 ws->focus_animation = NULL;
570}
571
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200572static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200573focus_state_destroy(struct focus_state *state)
574{
575 wl_list_remove(&state->seat_destroy_listener.link);
576 wl_list_remove(&state->surface_destroy_listener.link);
577 free(state);
578}
579
580static void
581focus_state_seat_destroy(struct wl_listener *listener, void *data)
582{
583 struct focus_state *state = container_of(listener,
584 struct focus_state,
585 seat_destroy_listener);
586
587 wl_list_remove(&state->link);
588 focus_state_destroy(state);
589}
590
591static void
592focus_state_surface_destroy(struct wl_listener *listener, void *data)
593{
594 struct focus_state *state = container_of(listener,
595 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400596 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400597 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500598 struct weston_surface *main_surface, *next;
599 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200600
Pekka Paalanen01388e22013-04-25 13:57:44 +0300601 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
602
Kristian Høgsberge3778222012-07-31 17:29:30 -0400603 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500604 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
605 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400606 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100607 if (is_focus_view(view))
608 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400609
Jason Ekstranda7af7042013-10-12 22:38:11 -0500610 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400611 break;
612 }
613
Pekka Paalanen01388e22013-04-25 13:57:44 +0300614 /* if the focus was a sub-surface, activate its main surface */
615 if (main_surface != state->keyboard_focus)
616 next = main_surface;
617
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100618 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400619 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100620 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400621 activate(shell, next, state->seat);
622 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100623 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
624 if (state->ws->focus_animation)
625 weston_view_animation_destroy(state->ws->focus_animation);
626
627 state->ws->focus_animation = weston_fade_run(
628 state->ws->fsurf_front->view,
629 state->ws->fsurf_front->view->alpha, 0.0, 300,
630 focus_animation_done, state->ws);
631 }
632
Kristian Høgsberge3778222012-07-31 17:29:30 -0400633 wl_list_remove(&state->link);
634 focus_state_destroy(state);
635 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200636}
637
638static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400639focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200640{
Jonas Ådahl04769742012-06-13 00:01:24 +0200641 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200642
643 state = malloc(sizeof *state);
644 if (state == NULL)
645 return NULL;
646
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100647 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400648 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200649 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400650 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200651
652 state->seat_destroy_listener.notify = focus_state_seat_destroy;
653 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400654 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200655 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400656 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200657
658 return state;
659}
660
Jonas Ådahl8538b222012-08-29 22:13:03 +0200661static struct focus_state *
662ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
663{
664 struct workspace *ws = get_current_workspace(shell);
665 struct focus_state *state;
666
667 wl_list_for_each(state, &ws->focus_list, link)
668 if (state->seat == seat)
669 break;
670
671 if (&state->link == &ws->focus_list)
672 state = focus_state_create(seat, ws);
673
674 return state;
675}
676
Jonas Ådahl04769742012-06-13 00:01:24 +0200677static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800678focus_state_set_focus(struct focus_state *state,
679 struct weston_surface *surface)
680{
681 if (state->keyboard_focus) {
682 wl_list_remove(&state->surface_destroy_listener.link);
683 wl_list_init(&state->surface_destroy_listener.link);
684 }
685
686 state->keyboard_focus = surface;
687 if (surface)
688 wl_signal_add(&surface->destroy_signal,
689 &state->surface_destroy_listener);
690}
691
692static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400693restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200694{
695 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400696 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200697
698 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800699 if (state->seat->keyboard == NULL)
700 continue;
701
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400702 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200703
Kristian Høgsberge3148752013-05-06 23:19:49 -0400704 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200705 }
706}
707
708static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200709replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
710 struct weston_seat *seat)
711{
712 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200713
714 wl_list_for_each(state, &ws->focus_list, link) {
715 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800716 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200717 return;
718 }
719 }
720}
721
722static void
723drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
724 struct weston_surface *surface)
725{
726 struct focus_state *state;
727
728 wl_list_for_each(state, &ws->focus_list, link)
729 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800730 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200731}
732
733static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100734animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
735 struct weston_view *from, struct weston_view *to)
736{
737 struct weston_output *output;
738 bool focus_surface_created = false;
739
740 /* FIXME: Only support dim animation using two layers */
741 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
742 return;
743
744 output = get_default_output(shell->compositor);
745 if (ws->fsurf_front == NULL && (from || to)) {
746 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800747 if (ws->fsurf_front == NULL)
748 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100749 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800750
751 ws->fsurf_back = create_focus_surface(shell->compositor, output);
752 if (ws->fsurf_back == NULL) {
753 focus_surface_destroy(ws->fsurf_front);
754 return;
755 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100756 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800757
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100758 focus_surface_created = true;
759 } else {
760 wl_list_remove(&ws->fsurf_front->view->layer_link);
761 wl_list_remove(&ws->fsurf_back->view->layer_link);
762 }
763
764 if (ws->focus_animation) {
765 weston_view_animation_destroy(ws->focus_animation);
766 ws->focus_animation = NULL;
767 }
768
769 if (to)
770 wl_list_insert(&to->layer_link,
771 &ws->fsurf_front->view->layer_link);
772 else if (from)
773 wl_list_insert(&ws->layer.view_list,
774 &ws->fsurf_front->view->layer_link);
775
776 if (focus_surface_created) {
777 ws->focus_animation = weston_fade_run(
778 ws->fsurf_front->view,
779 ws->fsurf_front->view->alpha, 0.6, 300,
780 focus_animation_done, ws);
781 } else if (from) {
782 wl_list_insert(&from->layer_link,
783 &ws->fsurf_back->view->layer_link);
784 ws->focus_animation = weston_stable_fade_run(
785 ws->fsurf_front->view, 0.0,
786 ws->fsurf_back->view, 0.6,
787 focus_animation_done, ws);
788 } else if (to) {
789 wl_list_insert(&ws->layer.view_list,
790 &ws->fsurf_back->view->layer_link);
791 ws->focus_animation = weston_stable_fade_run(
792 ws->fsurf_front->view, 0.0,
793 ws->fsurf_back->view, 0.6,
794 focus_animation_done, ws);
795 }
796}
797
798static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200799workspace_destroy(struct workspace *ws)
800{
Jonas Ådahl04769742012-06-13 00:01:24 +0200801 struct focus_state *state, *next;
802
803 wl_list_for_each_safe(state, next, &ws->focus_list, link)
804 focus_state_destroy(state);
805
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100806 if (ws->fsurf_front)
807 focus_surface_destroy(ws->fsurf_front);
808 if (ws->fsurf_back)
809 focus_surface_destroy(ws->fsurf_back);
810
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200811 free(ws);
812}
813
Jonas Ådahl04769742012-06-13 00:01:24 +0200814static void
815seat_destroyed(struct wl_listener *listener, void *data)
816{
817 struct weston_seat *seat = data;
818 struct focus_state *state, *next;
819 struct workspace *ws = container_of(listener,
820 struct workspace,
821 seat_destroyed_listener);
822
823 wl_list_for_each_safe(state, next, &ws->focus_list, link)
824 if (state->seat == seat)
825 wl_list_remove(&state->link);
826}
827
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200828static struct workspace *
829workspace_create(void)
830{
831 struct workspace *ws = malloc(sizeof *ws);
832 if (ws == NULL)
833 return NULL;
834
835 weston_layer_init(&ws->layer, NULL);
836
Jonas Ådahl04769742012-06-13 00:01:24 +0200837 wl_list_init(&ws->focus_list);
838 wl_list_init(&ws->seat_destroyed_listener.link);
839 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100840 ws->fsurf_front = NULL;
841 ws->fsurf_back = NULL;
842 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200843
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200844 return ws;
845}
846
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200847static int
848workspace_is_empty(struct workspace *ws)
849{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500850 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200851}
852
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200853static struct workspace *
854get_workspace(struct desktop_shell *shell, unsigned int index)
855{
856 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200857 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200858 pws += index;
859 return *pws;
860}
861
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800862struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200863get_current_workspace(struct desktop_shell *shell)
864{
865 return get_workspace(shell, shell->workspaces.current);
866}
867
868static void
869activate_workspace(struct desktop_shell *shell, unsigned int index)
870{
871 struct workspace *ws;
872
873 ws = get_workspace(shell, index);
874 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
875
876 shell->workspaces.current = index;
877}
878
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200879static unsigned int
880get_output_height(struct weston_output *output)
881{
882 return abs(output->region.extents.y1 - output->region.extents.y2);
883}
884
885static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100886view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200887{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200888 struct weston_transform *transform;
889
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100890 if (is_focus_view(view)) {
891 struct focus_surface *fsurf = get_focus_surface(view->surface);
892 transform = &fsurf->workspace_transform;
893 } else {
894 struct shell_surface *shsurf = get_shell_surface(view->surface);
895 transform = &shsurf->workspace_transform;
896 }
897
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200898 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500899 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100900 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200901
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100902 weston_matrix_init(&transform->matrix);
903 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200904 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500905 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200906}
907
908static void
909workspace_translate_out(struct workspace *ws, double fraction)
910{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912 unsigned int height;
913 double d;
914
Jason Ekstranda7af7042013-10-12 22:38:11 -0500915 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
916 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200917 d = height * fraction;
918
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100919 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200920 }
921}
922
923static void
924workspace_translate_in(struct workspace *ws, double fraction)
925{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200927 unsigned int height;
928 double d;
929
Jason Ekstranda7af7042013-10-12 22:38:11 -0500930 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
931 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200932
933 if (fraction > 0)
934 d = -(height - height * fraction);
935 else
936 d = height + height * fraction;
937
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100938 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200939 }
940}
941
942static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200943broadcast_current_workspace_state(struct desktop_shell *shell)
944{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700945 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200946
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700947 wl_resource_for_each(resource, &shell->workspaces.client_list)
948 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200949 shell->workspaces.current,
950 shell->workspaces.num);
951}
952
953static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200954reverse_workspace_change_animation(struct desktop_shell *shell,
955 unsigned int index,
956 struct workspace *from,
957 struct workspace *to)
958{
959 shell->workspaces.current = index;
960
961 shell->workspaces.anim_to = to;
962 shell->workspaces.anim_from = from;
963 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
964 shell->workspaces.anim_timestamp = 0;
965
Scott Moreau4272e632012-08-13 09:58:41 -0600966 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967}
968
969static void
970workspace_deactivate_transforms(struct workspace *ws)
971{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100973 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200974
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100976 if (is_focus_view(view)) {
977 struct focus_surface *fsurf = get_focus_surface(view->surface);
978 transform = &fsurf->workspace_transform;
979 } else {
980 struct shell_surface *shsurf = get_shell_surface(view->surface);
981 transform = &shsurf->workspace_transform;
982 }
983
984 if (!wl_list_empty(&transform->link)) {
985 wl_list_remove(&transform->link);
986 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200987 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200989 }
990}
991
992static void
993finish_workspace_change_animation(struct desktop_shell *shell,
994 struct workspace *from,
995 struct workspace *to)
996{
Scott Moreau4272e632012-08-13 09:58:41 -0600997 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998
999 wl_list_remove(&shell->workspaces.animation.link);
1000 workspace_deactivate_transforms(from);
1001 workspace_deactivate_transforms(to);
1002 shell->workspaces.anim_to = NULL;
1003
1004 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1005}
1006
1007static void
1008animate_workspace_change_frame(struct weston_animation *animation,
1009 struct weston_output *output, uint32_t msecs)
1010{
1011 struct desktop_shell *shell =
1012 container_of(animation, struct desktop_shell,
1013 workspaces.animation);
1014 struct workspace *from = shell->workspaces.anim_from;
1015 struct workspace *to = shell->workspaces.anim_to;
1016 uint32_t t;
1017 double x, y;
1018
1019 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1020 finish_workspace_change_animation(shell, from, to);
1021 return;
1022 }
1023
1024 if (shell->workspaces.anim_timestamp == 0) {
1025 if (shell->workspaces.anim_current == 0.0)
1026 shell->workspaces.anim_timestamp = msecs;
1027 else
1028 shell->workspaces.anim_timestamp =
1029 msecs -
1030 /* Invers of movement function 'y' below. */
1031 (asin(1.0 - shell->workspaces.anim_current) *
1032 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1033 M_2_PI);
1034 }
1035
1036 t = msecs - shell->workspaces.anim_timestamp;
1037
1038 /*
1039 * x = [0, π/2]
1040 * y(x) = sin(x)
1041 */
1042 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1043 y = sin(x);
1044
1045 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001046 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001047
1048 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1049 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1050 shell->workspaces.anim_current = y;
1051
Scott Moreau4272e632012-08-13 09:58:41 -06001052 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001053 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001054 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001056}
1057
1058static void
1059animate_workspace_change(struct desktop_shell *shell,
1060 unsigned int index,
1061 struct workspace *from,
1062 struct workspace *to)
1063{
1064 struct weston_output *output;
1065
1066 int dir;
1067
1068 if (index > shell->workspaces.current)
1069 dir = -1;
1070 else
1071 dir = 1;
1072
1073 shell->workspaces.current = index;
1074
1075 shell->workspaces.anim_dir = dir;
1076 shell->workspaces.anim_from = from;
1077 shell->workspaces.anim_to = to;
1078 shell->workspaces.anim_current = 0.0;
1079 shell->workspaces.anim_timestamp = 0;
1080
1081 output = container_of(shell->compositor->output_list.next,
1082 struct weston_output, link);
1083 wl_list_insert(&output->animation_list,
1084 &shell->workspaces.animation.link);
1085
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001086 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001087
1088 workspace_translate_in(to, 0);
1089
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001090 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001091
Scott Moreau4272e632012-08-13 09:58:41 -06001092 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001093}
1094
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001095static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001096update_workspace(struct desktop_shell *shell, unsigned int index,
1097 struct workspace *from, struct workspace *to)
1098{
1099 shell->workspaces.current = index;
1100 wl_list_insert(&from->layer.link, &to->layer.link);
1101 wl_list_remove(&from->layer.link);
1102}
1103
1104static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001105change_workspace(struct desktop_shell *shell, unsigned int index)
1106{
1107 struct workspace *from;
1108 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001109 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001110
1111 if (index == shell->workspaces.current)
1112 return;
1113
1114 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001116 return;
1117
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001118 from = get_current_workspace(shell);
1119 to = get_workspace(shell, index);
1120
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001121 if (shell->workspaces.anim_from == to &&
1122 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001123 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001124 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001125 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126 return;
1127 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001128
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129 if (shell->workspaces.anim_to != NULL)
1130 finish_workspace_change_animation(shell,
1131 shell->workspaces.anim_from,
1132 shell->workspaces.anim_to);
1133
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001134 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001135
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001136 if (shell->focus_animation_type != ANIMATION_NONE) {
1137 wl_list_for_each(state, &from->focus_list, link)
1138 if (state->keyboard_focus)
1139 animate_focus_change(shell, from,
1140 get_default_view(state->keyboard_focus), NULL);
1141
1142 wl_list_for_each(state, &to->focus_list, link)
1143 if (state->keyboard_focus)
1144 animate_focus_change(shell, to,
1145 NULL, get_default_view(state->keyboard_focus));
1146 }
1147
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001148 if (workspace_is_empty(to) && workspace_is_empty(from))
1149 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001150 else
1151 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001152
1153 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001154}
1155
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001156static bool
1157workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1158{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001160 struct wl_list *e;
1161
1162 if (wl_list_empty(list))
1163 return false;
1164
1165 e = list->next;
1166
1167 if (e->next != list)
1168 return false;
1169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001171}
1172
1173static void
Philip Withnall659163d2013-11-25 18:01:36 +00001174move_surface_to_workspace(struct desktop_shell *shell,
1175 struct shell_surface *shsurf,
1176 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001177{
1178 struct workspace *from;
1179 struct workspace *to;
1180 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001181 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001182 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001183
1184 if (workspace == shell->workspaces.current)
1185 return;
1186
Philip Withnall659163d2013-11-25 18:01:36 +00001187 view = get_default_view(shsurf->surface);
1188 if (!view)
1189 return;
1190
1191 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1192
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001193 if (workspace >= shell->workspaces.num)
1194 workspace = shell->workspaces.num - 1;
1195
Jonas Ådahle9d22502012-08-29 22:13:01 +02001196 from = get_current_workspace(shell);
1197 to = get_workspace(shell, workspace);
1198
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 wl_list_remove(&view->layer_link);
1200 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001201
Philip Withnall648a4dd2013-11-25 18:01:44 +00001202 shell_surface_update_child_surface_layers(shsurf);
1203
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001205 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1206 if (!seat->keyboard)
1207 continue;
1208
1209 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001211 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001212 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001213
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001215}
1216
1217static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001218take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001219 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001220 unsigned int index)
1221{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001222 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224 struct shell_surface *shsurf;
1225 struct workspace *from;
1226 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001227 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001228
Pekka Paalanen01388e22013-04-25 13:57:44 +03001229 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 view = get_default_view(surface);
1231 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001232 index == shell->workspaces.current ||
1233 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001234 return;
1235
1236 from = get_current_workspace(shell);
1237 to = get_workspace(shell, index);
1238
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 wl_list_remove(&view->layer_link);
1240 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001241
Philip Withnall659163d2013-11-25 18:01:36 +00001242 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001243 if (shsurf != NULL)
1244 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001245
Jonas Ådahle9d22502012-08-29 22:13:01 +02001246 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001247 drop_focus_state(shell, from, surface);
1248
1249 if (shell->workspaces.anim_from == to &&
1250 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001251 wl_list_remove(&to->layer.link);
1252 wl_list_insert(from->layer.link.prev, &to->layer.link);
1253
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001254 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001255 broadcast_current_workspace_state(shell);
1256
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257 return;
1258 }
1259
1260 if (shell->workspaces.anim_to != NULL)
1261 finish_workspace_change_animation(shell,
1262 shell->workspaces.anim_from,
1263 shell->workspaces.anim_to);
1264
1265 if (workspace_is_empty(from) &&
1266 workspace_has_only(to, surface))
1267 update_workspace(shell, index, from, to);
1268 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001269 if (shsurf != NULL &&
1270 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001271 wl_list_insert(&shell->workspaces.anim_sticky_list,
1272 &shsurf->workspace_transform.link);
1273
1274 animate_workspace_change(shell, index, from, to);
1275 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001276
1277 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001278
1279 state = ensure_focus_state(shell, seat);
1280 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001281 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001282}
1283
1284static void
1285workspace_manager_move_surface(struct wl_client *client,
1286 struct wl_resource *resource,
1287 struct wl_resource *surface_resource,
1288 uint32_t workspace)
1289{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001290 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001291 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001292 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001293 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001294 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001295
Pekka Paalanen01388e22013-04-25 13:57:44 +03001296 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001297 shell_surface = get_shell_surface(main_surface);
1298 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001300
1301 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001302}
1303
1304static const struct workspace_manager_interface workspace_manager_implementation = {
1305 workspace_manager_move_surface,
1306};
1307
1308static void
1309unbind_resource(struct wl_resource *resource)
1310{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001311 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001312}
1313
1314static void
1315bind_workspace_manager(struct wl_client *client,
1316 void *data, uint32_t version, uint32_t id)
1317{
1318 struct desktop_shell *shell = data;
1319 struct wl_resource *resource;
1320
Jason Ekstranda85118c2013-06-27 20:17:02 -05001321 resource = wl_resource_create(client,
1322 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323
1324 if (resource == NULL) {
1325 weston_log("couldn't add workspace manager object");
1326 return;
1327 }
1328
Jason Ekstranda85118c2013-06-27 20:17:02 -05001329 wl_resource_set_implementation(resource,
1330 &workspace_manager_implementation,
1331 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001332 wl_list_insert(&shell->workspaces.client_list,
1333 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001334
1335 workspace_manager_send_state(resource,
1336 shell->workspaces.current,
1337 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001338}
1339
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001340static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001341touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1342 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1343{
1344}
1345
1346static void
1347touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1348{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001349 struct weston_touch_move_grab *move =
1350 (struct weston_touch_move_grab *) container_of(
1351 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001352
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001353 if (touch_id == 0)
1354 move->active = 0;
1355
Jonas Ådahl9484b692013-12-02 22:05:03 +01001356 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001357 shell_touch_grab_end(&move->base);
1358 free(move);
1359 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001360}
1361
1362static void
1363touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1364 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1365{
1366 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1367 struct shell_surface *shsurf = move->base.shsurf;
1368 struct weston_surface *es;
1369 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1370 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1371
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001372 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001373 return;
1374
1375 es = shsurf->surface;
1376
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001377 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001378
1379 weston_compositor_schedule_repaint(es->compositor);
1380}
1381
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001382static void
1383touch_move_grab_cancel(struct weston_touch_grab *grab)
1384{
1385 struct weston_touch_move_grab *move =
1386 (struct weston_touch_move_grab *) container_of(
1387 grab, struct shell_touch_grab, grab);
1388
1389 shell_touch_grab_end(&move->base);
1390 free(move);
1391}
1392
Rusty Lynch1084da52013-08-15 09:10:08 -07001393static const struct weston_touch_grab_interface touch_move_grab_interface = {
1394 touch_move_grab_down,
1395 touch_move_grab_up,
1396 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001397 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001398};
1399
1400static int
1401surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1402{
1403 struct weston_touch_move_grab *move;
1404
1405 if (!shsurf)
1406 return -1;
1407
Rafael Antognolli03b16592013-12-03 15:35:42 -02001408 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001409 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001410 if (shsurf->grabbed)
1411 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001412
1413 move = malloc(sizeof *move);
1414 if (!move)
1415 return -1;
1416
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001417 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001419 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001421 seat->touch->grab_y;
1422
1423 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1424 seat->touch);
1425
1426 return 0;
1427}
1428
1429static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001430noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001431{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001432}
1433
1434static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001435move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1436 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001437{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001438 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001439 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001440 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001441 int dx, dy;
1442
1443 weston_pointer_move(pointer, x, y);
1444 dx = wl_fixed_to_int(pointer->x + move->dx);
1445 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001446
1447 if (!shsurf)
1448 return;
1449
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001450 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001451
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001453}
1454
1455static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001456move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001457 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001458{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001459 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1460 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001461 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001462 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001463
Daniel Stone4dbadb12012-05-30 16:31:51 +01001464 if (pointer->button_count == 0 &&
1465 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001466 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001467 free(grab);
1468 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001469}
1470
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001471static void
1472move_grab_cancel(struct weston_pointer_grab *grab)
1473{
1474 struct shell_grab *shell_grab =
1475 container_of(grab, struct shell_grab, grab);
1476
1477 shell_grab_end(shell_grab);
1478 free(grab);
1479}
1480
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001481static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001482 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001483 move_grab_motion,
1484 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001485 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001486};
1487
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001488static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001489surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001490{
1491 struct weston_move_grab *move;
1492
1493 if (!shsurf)
1494 return -1;
1495
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001496 if (shsurf->grabbed)
1497 return 0;
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001498 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001499 return 0;
1500
1501 move = malloc(sizeof *move);
1502 if (!move)
1503 return -1;
1504
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001506 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001508 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001509
1510 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001511 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001512
1513 return 0;
1514}
1515
1516static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001517common_surface_move(struct wl_resource *resource,
1518 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001519{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001520 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001521 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001522 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001523
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001524 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001525 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001526 seat->pointer->button_count > 0 &&
1527 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001529 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001530 (surface_move(shsurf, seat) < 0))
1531 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001532 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001533 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001534 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001536 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001537 (surface_touch_move(shsurf, seat) < 0))
1538 wl_resource_post_no_memory(resource);
1539 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001540}
1541
Rafael Antognollie2a34552013-12-03 15:35:45 -02001542static void
1543shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1544 struct wl_resource *seat_resource, uint32_t serial)
1545{
1546 common_surface_move(resource, seat_resource, serial);
1547}
1548
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001549struct weston_resize_grab {
1550 struct shell_grab base;
1551 uint32_t edges;
1552 int32_t width, height;
1553};
1554
1555static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001556resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1557 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001558{
1559 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001560 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561 struct shell_surface *shsurf = resize->base.shsurf;
1562 int32_t width, height;
1563 wl_fixed_t from_x, from_y;
1564 wl_fixed_t to_x, to_y;
1565
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001566 weston_pointer_move(pointer, x, y);
1567
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001568 if (!shsurf)
1569 return;
1570
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571 weston_view_from_global_fixed(shsurf->view,
1572 pointer->grab_x, pointer->grab_y,
1573 &from_x, &from_y);
1574 weston_view_from_global_fixed(shsurf->view,
1575 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001576
1577 width = resize->width;
1578 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1579 width += wl_fixed_to_int(from_x - to_x);
1580 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1581 width += wl_fixed_to_int(to_x - from_x);
1582 }
1583
1584 height = resize->height;
1585 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1586 height += wl_fixed_to_int(from_y - to_y);
1587 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1588 height += wl_fixed_to_int(to_y - from_y);
1589 }
1590
1591 shsurf->client->send_configure(shsurf->surface,
1592 resize->edges, width, height);
1593}
1594
1595static void
1596send_configure(struct weston_surface *surface,
1597 uint32_t edges, int32_t width, int32_t height)
1598{
1599 struct shell_surface *shsurf = get_shell_surface(surface);
1600
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001601 assert(shsurf);
1602
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001603 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001604 edges, width, height);
1605}
1606
1607static const struct weston_shell_client shell_client = {
1608 send_configure
1609};
1610
1611static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001612resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001613 uint32_t time, uint32_t button, uint32_t state_w)
1614{
1615 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001616 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617 enum wl_pointer_button_state state = state_w;
1618
1619 if (pointer->button_count == 0 &&
1620 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1621 shell_grab_end(&resize->base);
1622 free(grab);
1623 }
1624}
1625
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001626static void
1627resize_grab_cancel(struct weston_pointer_grab *grab)
1628{
1629 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1630
1631 shell_grab_end(&resize->base);
1632 free(grab);
1633}
1634
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001635static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001636 noop_grab_focus,
1637 resize_grab_motion,
1638 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001639 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001640};
1641
Giulio Camuffob8366642013-04-25 13:57:46 +03001642/*
1643 * Returns the bounding box of a surface and all its sub-surfaces,
1644 * in the surface coordinates system. */
1645static void
1646surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1647 int32_t *y, int32_t *w, int32_t *h) {
1648 pixman_region32_t region;
1649 pixman_box32_t *box;
1650 struct weston_subsurface *subsurface;
1651
1652 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 surface->width,
1654 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001655
1656 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1657 pixman_region32_union_rect(&region, &region,
1658 subsurface->position.x,
1659 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660 subsurface->surface->width,
1661 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001662 }
1663
1664 box = pixman_region32_extents(&region);
1665 if (x)
1666 *x = box->x1;
1667 if (y)
1668 *y = box->y1;
1669 if (w)
1670 *w = box->x2 - box->x1;
1671 if (h)
1672 *h = box->y2 - box->y1;
1673
1674 pixman_region32_fini(&region);
1675}
1676
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677static int
1678surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001679 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001680{
1681 struct weston_resize_grab *resize;
1682
Rafael Antognolli03b16592013-12-03 15:35:42 -02001683 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001684 return 0;
1685
1686 if (edges == 0 || edges > 15 ||
1687 (edges & 3) == 3 || (edges & 12) == 12)
1688 return 0;
1689
1690 resize = malloc(sizeof *resize);
1691 if (!resize)
1692 return -1;
1693
1694 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001695 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1696 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001697
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001698 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001700 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701
1702 return 0;
1703}
1704
1705static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001706common_surface_resize(struct wl_resource *resource,
1707 struct wl_resource *seat_resource, uint32_t serial,
1708 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001710 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001711 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001712 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713
Rafael Antognolli03b16592013-12-03 15:35:42 -02001714 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715 return;
1716
Kristian Høgsberge3148752013-05-06 23:19:49 -04001717 if (seat->pointer->button_count == 0 ||
1718 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001719 seat->pointer->focus == NULL)
1720 return;
1721
1722 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1723 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001724 return;
1725
Kristian Høgsberge3148752013-05-06 23:19:49 -04001726 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727 wl_resource_post_no_memory(resource);
1728}
1729
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001730static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001731shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1732 struct wl_resource *seat_resource, uint32_t serial,
1733 uint32_t edges)
1734{
1735 common_surface_resize(resource, seat_resource, serial, edges);
1736}
1737
1738static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001739busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001740{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001741 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001742 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001744 wl_fixed_t sx, sy;
1745
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 view = weston_compositor_pick_view(pointer->seat->compositor,
1747 pointer->x, pointer->y,
1748 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001749
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001750 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1751 shell_grab_end(grab);
1752 free(grab);
1753 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001754}
1755
1756static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001757busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1758 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001759{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001760 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001761}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001762
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001763static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001764busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001765 uint32_t time, uint32_t button, uint32_t state)
1766{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001767 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001768 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001769 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001770
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001771 if (shsurf && button == BTN_LEFT && state) {
1772 activate(shsurf->shell, shsurf->surface, seat);
1773 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001774 } else if (shsurf && button == BTN_RIGHT && state) {
1775 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001776 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001777 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001778}
1779
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001780static void
1781busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1782{
1783 struct shell_grab *grab = (struct shell_grab *) base;
1784
1785 shell_grab_end(grab);
1786 free(grab);
1787}
1788
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001789static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001790 busy_cursor_grab_focus,
1791 busy_cursor_grab_motion,
1792 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001793 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794};
1795
1796static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001797set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001798{
1799 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001800
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001801 if (pointer->grab->interface == &busy_cursor_grab_interface)
1802 return;
1803
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804 grab = malloc(sizeof *grab);
1805 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001806 return;
1807
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001808 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1809 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001810}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001811
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001812static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001813end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001814{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001815 struct shell_grab *grab;
1816 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001817
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001818 wl_list_for_each(seat, &compositor->seat_list, link) {
1819 if (seat->pointer == NULL)
1820 continue;
1821
1822 grab = (struct shell_grab *) seat->pointer->grab;
1823 if (grab->grab.interface == &busy_cursor_grab_interface &&
1824 wl_resource_get_client(grab->shsurf->resource) == client) {
1825 shell_grab_end(grab);
1826 free(grab);
1827 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001828 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001829}
1830
Scott Moreau9521d5e2012-04-19 13:06:17 -06001831static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001832handle_shell_client_destroy(struct wl_listener *listener, void *data);
1833
1834static int
1835xdg_ping_timeout_handler(void *data)
1836{
1837 struct shell_client *sc = data;
1838 struct weston_seat *seat;
1839 struct shell_surface *shsurf;
1840
1841 /* Client is not responding */
1842 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001843 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1844 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1845 continue;
1846 if (seat->pointer->focus->surface->resource == NULL)
1847 continue;
1848
1849 shsurf = get_shell_surface(seat->pointer->focus->surface);
1850 if (shsurf &&
1851 wl_resource_get_client(shsurf->resource) == sc->client)
1852 set_busy_cursor(shsurf, seat->pointer);
1853 }
1854
1855 return 1;
1856}
1857
1858static void
1859handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1860{
1861 struct weston_compositor *compositor = shsurf->shell->compositor;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001862 struct wl_client *client = wl_resource_get_client(shsurf->resource);
1863 struct shell_client *sc;
1864 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001865 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001866
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001867 sc = get_shell_client(client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001868 if (sc->unresponsive) {
1869 xdg_ping_timeout_handler(sc);
1870 return;
1871 }
1872
1873 sc->ping_serial = serial;
1874 loop = wl_display_get_event_loop(compositor->wl_display);
1875 if (sc->ping_timer == NULL)
1876 sc->ping_timer =
1877 wl_event_loop_add_timer(loop,
1878 xdg_ping_timeout_handler, sc);
1879 if (sc->ping_timer == NULL)
1880 return;
1881
1882 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1883
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001884 if (shell_surface_is_xdg_surface(shsurf) ||
1885 shell_surface_is_xdg_popup(shsurf))
1886 xdg_shell_send_ping(sc->resource, serial);
1887 else if (shell_surface_is_wl_shell_surface(shsurf))
1888 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001889}
1890
Scott Moreauff1db4a2012-04-17 19:06:18 -06001891static void
1892ping_handler(struct weston_surface *surface, uint32_t serial)
1893{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001894 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001895
1896 if (!shsurf)
1897 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001898 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001899 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001900 if (shsurf->surface == shsurf->shell->grab_surface)
1901 return;
1902
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001903 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001904}
1905
1906static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001907handle_pointer_focus(struct wl_listener *listener, void *data)
1908{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001909 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001910 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001911 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001912 uint32_t serial;
1913
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001915 return;
1916
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001918 serial = wl_display_next_serial(compositor->wl_display);
1919 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001920}
1921
1922static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001923create_pointer_focus_listener(struct weston_seat *seat)
1924{
1925 struct wl_listener *listener;
1926
Kristian Høgsberge3148752013-05-06 23:19:49 -04001927 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001928 return;
1929
1930 listener = malloc(sizeof *listener);
1931 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001932 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001933}
1934
1935static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001936shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1937{
1938 if (--shsurf->focus_count == 0)
1939 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001940 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001941}
1942
1943static void
1944shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1945{
1946 if (shsurf->focus_count++ == 0)
1947 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001948 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001949}
1950
1951static void
1952handle_keyboard_focus(struct wl_listener *listener, void *data)
1953{
1954 struct weston_keyboard *keyboard = data;
1955 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1956
1957 if (seat->focused_surface) {
1958 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1959 if (shsurf)
1960 shell_surface_lose_keyboard_focus(shsurf);
1961 }
1962
1963 seat->focused_surface = keyboard->focus;
1964
1965 if (seat->focused_surface) {
1966 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1967 if (shsurf)
1968 shell_surface_gain_keyboard_focus(shsurf);
1969 }
1970}
1971
1972static void
1973create_keyboard_focus_listener(struct weston_seat *seat)
1974{
1975 struct wl_listener *listener;
1976
1977 if (!seat->keyboard)
1978 return;
1979
1980 listener = malloc(sizeof *listener);
1981 listener->notify = handle_keyboard_focus;
1982 wl_signal_add(&seat->keyboard->focus_signal, listener);
1983}
1984
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001985static struct shell_client *
1986get_shell_client(struct wl_client *client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02001987{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001988 struct wl_listener *listener;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001989
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001990 listener = wl_client_get_destroy_listener(client,
1991 handle_shell_client_destroy);
1992 if (listener == NULL)
1993 return NULL;
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001994
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001995 return container_of(listener, struct shell_client, destroy_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001996}
1997
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001998static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001999shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002000{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002001 if (sc->ping_serial != serial)
2002 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002003
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002004 sc->unresponsive = 0;
2005 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002006
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002007 if (sc->ping_timer) {
2008 wl_event_source_remove(sc->ping_timer);
2009 sc->ping_timer = NULL;
2010 }
2011
2012}
2013
2014static void
2015shell_surface_pong(struct wl_client *client,
2016 struct wl_resource *resource, uint32_t serial)
2017{
2018 struct shell_client *sc;
2019
2020 sc = get_shell_client(client);
2021 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002022}
2023
2024static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002025set_title(struct shell_surface *shsurf, const char *title)
2026{
2027 free(shsurf->title);
2028 shsurf->title = strdup(title);
2029}
2030
2031static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002032shell_surface_set_title(struct wl_client *client,
2033 struct wl_resource *resource, const char *title)
2034{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002035 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002036
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002037 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002038}
2039
2040static void
2041shell_surface_set_class(struct wl_client *client,
2042 struct wl_resource *resource, const char *class)
2043{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002044 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002045
2046 free(shsurf->class);
2047 shsurf->class = strdup(class);
2048}
2049
Alex Wu4539b082012-03-01 12:57:46 +08002050static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002051restore_output_mode(struct weston_output *output)
2052{
Hardening57388e42013-09-18 23:56:36 +02002053 if (output->current_mode != output->original_mode ||
2054 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002055 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002056 output->original_mode,
2057 output->original_scale,
2058 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002059}
2060
2061static void
2062restore_all_output_modes(struct weston_compositor *compositor)
2063{
2064 struct weston_output *output;
2065
2066 wl_list_for_each(output, &compositor->output_list, link)
2067 restore_output_mode(output);
2068}
2069
Philip Withnallbecb77e2013-11-25 18:01:30 +00002070static int
2071get_output_panel_height(struct desktop_shell *shell,
2072 struct weston_output *output)
2073{
2074 struct weston_view *view;
2075 int panel_height = 0;
2076
2077 if (!output)
2078 return 0;
2079
2080 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2081 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002082 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002083 break;
2084 }
2085 }
2086
2087 return panel_height;
2088}
2089
Philip Withnall07926d92013-11-25 18:01:40 +00002090/* The surface will be inserted into the list immediately after the link
2091 * returned by this function (i.e. will be stacked immediately above the
2092 * returned link). */
2093static struct wl_list *
2094shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2095{
2096 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002097 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002098
2099 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002100 case SHELL_SURFACE_POPUP:
2101 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002102 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002103 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002104 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002105 /* Move the surface to its parent layer so
2106 * that surfaces which are transient for
2107 * fullscreen surfaces don't get hidden by the
2108 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002109
2110 /* TODO: Handle a parent with multiple views */
2111 parent = get_default_view(shsurf->parent);
2112 if (parent)
2113 return parent->layer_link.prev;
2114 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002115 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002116
2117 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002118 return &shsurf->shell->fullscreen_layer.view_list;
2119
Philip Withnall07926d92013-11-25 18:01:40 +00002120 case SHELL_SURFACE_NONE:
2121 default:
2122 /* Go to the fallback, below. */
2123 break;
2124 }
2125
2126 /* Move the surface to a normal workspace layer so that surfaces
2127 * which were previously fullscreen or transient are no longer
2128 * rendered on top. */
2129 ws = get_current_workspace(shsurf->shell);
2130 return &ws->layer.view_list;
2131}
2132
Philip Withnall648a4dd2013-11-25 18:01:44 +00002133static void
2134shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2135{
2136 struct shell_surface *child;
2137
2138 /* Move the child layers to the same workspace as shsurf. They will be
2139 * stacked above shsurf. */
2140 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2141 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2142 weston_view_geometry_dirty(child->view);
2143 wl_list_remove(&child->view->layer_link);
2144 wl_list_insert(shsurf->view->layer_link.prev,
2145 &child->view->layer_link);
2146 weston_view_geometry_dirty(child->view);
2147 weston_surface_damage(child->surface);
2148
2149 /* Recurse. We don’t expect this to recurse very far (if
2150 * at all) because that would imply we have transient
2151 * (or popup) children of transient surfaces, which
2152 * would be unusual. */
2153 shell_surface_update_child_surface_layers(child);
2154 }
2155 }
2156}
2157
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002158/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002159 * geometry to ensure the changes are redrawn.
2160 *
2161 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2162 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002163static void
2164shell_surface_update_layer(struct shell_surface *shsurf)
2165{
2166 struct wl_list *new_layer_link;
2167
2168 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2169
2170 if (new_layer_link == &shsurf->view->layer_link)
2171 return;
2172
2173 weston_view_geometry_dirty(shsurf->view);
2174 wl_list_remove(&shsurf->view->layer_link);
2175 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2176 weston_view_geometry_dirty(shsurf->view);
2177 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002178
2179 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002180}
2181
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002182static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002183shell_surface_set_parent(struct shell_surface *shsurf,
2184 struct weston_surface *parent)
2185{
2186 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002187
2188 wl_list_remove(&shsurf->children_link);
2189 wl_list_init(&shsurf->children_link);
2190
2191 /* Insert into the parent surface’s child list. */
2192 if (parent != NULL) {
2193 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2194 if (parent_shsurf != NULL)
2195 wl_list_insert(&parent_shsurf->children_list,
2196 &shsurf->children_link);
2197 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002198}
2199
2200static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002201shell_surface_set_output(struct shell_surface *shsurf,
2202 struct weston_output *output)
2203{
2204 struct weston_surface *es = shsurf->surface;
2205
2206 /* get the default output, if the client set it as NULL
2207 check whether the ouput is available */
2208 if (output)
2209 shsurf->output = output;
2210 else if (es->output)
2211 shsurf->output = es->output;
2212 else
2213 shsurf->output = get_default_output(es->compositor);
2214}
2215
2216static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002217surface_clear_next_states(struct shell_surface *shsurf)
2218{
2219 shsurf->next_state.maximized = false;
2220 shsurf->next_state.fullscreen = false;
2221
2222 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2223 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2224 shsurf->state_changed = true;
2225}
2226
2227static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002228set_toplevel(struct shell_surface *shsurf)
2229{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002230 shell_surface_set_parent(shsurf, NULL);
2231 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002232 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002233
2234 /* The layer_link is updated in set_surface_type(),
2235 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002236}
2237
2238static void
2239shell_surface_set_toplevel(struct wl_client *client,
2240 struct wl_resource *resource)
2241{
2242 struct shell_surface *surface = wl_resource_get_user_data(resource);
2243
2244 set_toplevel(surface);
2245}
2246
2247static void
2248set_transient(struct shell_surface *shsurf,
2249 struct weston_surface *parent, int x, int y, uint32_t flags)
2250{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002251 assert(parent != NULL);
2252
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002253 shell_surface_set_parent(shsurf, parent);
2254
2255 surface_clear_next_states(shsurf);
2256
Philip Withnallbecb77e2013-11-25 18:01:30 +00002257 shsurf->transient.x = x;
2258 shsurf->transient.y = y;
2259 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002260
Rafael Antognollied207b42013-12-03 15:35:43 -02002261 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002262 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002263 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002264
2265 /* The layer_link is updated in set_surface_type(),
2266 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002267}
2268
2269static void
2270shell_surface_set_transient(struct wl_client *client,
2271 struct wl_resource *resource,
2272 struct wl_resource *parent_resource,
2273 int x, int y, uint32_t flags)
2274{
2275 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2276 struct weston_surface *parent =
2277 wl_resource_get_user_data(parent_resource);
2278
2279 set_transient(shsurf, parent, x, y, flags);
2280}
2281
2282static void
2283set_fullscreen(struct shell_surface *shsurf,
2284 uint32_t method,
2285 uint32_t framerate,
2286 struct weston_output *output)
2287{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002288 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002289
2290 shsurf->fullscreen_output = shsurf->output;
2291 shsurf->fullscreen.type = method;
2292 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002293
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002294 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002295
2296 shsurf->client->send_configure(shsurf->surface, 0,
2297 shsurf->output->width,
2298 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002299
2300 /* The layer_link is updated in set_surface_type(),
2301 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002302}
2303
2304static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002305weston_view_set_initial_position(struct weston_view *view,
2306 struct desktop_shell *shell);
2307
2308static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002309unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002310{
Philip Withnallf85fe842013-11-25 18:01:37 +00002311 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002312 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2313 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002314 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002315 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002316 shsurf->fullscreen_output = NULL;
2317
Alex Wu4539b082012-03-01 12:57:46 +08002318 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2319 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002320
Alex Wu4539b082012-03-01 12:57:46 +08002321 wl_list_remove(&shsurf->fullscreen.transform.link);
2322 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002323
Jason Ekstranda7af7042013-10-12 22:38:11 -05002324 if (shsurf->fullscreen.black_view)
2325 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2326 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002327
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002328 if (shsurf->saved_position_valid)
2329 weston_view_set_position(shsurf->view,
2330 shsurf->saved_x, shsurf->saved_y);
2331 else
2332 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2333
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002334 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002335 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002336 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002337 shsurf->saved_rotation_valid = false;
2338 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002339
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002340 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002341}
2342
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002343static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002344shell_surface_set_fullscreen(struct wl_client *client,
2345 struct wl_resource *resource,
2346 uint32_t method,
2347 uint32_t framerate,
2348 struct wl_resource *output_resource)
2349{
2350 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2351 struct weston_output *output;
2352
2353 if (output_resource)
2354 output = wl_resource_get_user_data(output_resource);
2355 else
2356 output = NULL;
2357
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002358 shell_surface_set_parent(shsurf, NULL);
2359
Rafael Antognolli03b16592013-12-03 15:35:42 -02002360 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002361 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002362
2363 shsurf->next_state.fullscreen = true;
2364 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002365}
2366
2367static void
2368set_popup(struct shell_surface *shsurf,
2369 struct weston_surface *parent,
2370 struct weston_seat *seat,
2371 uint32_t serial,
2372 int32_t x,
2373 int32_t y)
2374{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002375 assert(parent != NULL);
2376
Philip Withnallbecb77e2013-11-25 18:01:30 +00002377 shsurf->popup.shseat = get_shell_seat(seat);
2378 shsurf->popup.serial = serial;
2379 shsurf->popup.x = x;
2380 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002381
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002382 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002383}
2384
2385static void
2386shell_surface_set_popup(struct wl_client *client,
2387 struct wl_resource *resource,
2388 struct wl_resource *seat_resource,
2389 uint32_t serial,
2390 struct wl_resource *parent_resource,
2391 int32_t x, int32_t y, uint32_t flags)
2392{
2393 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002394 struct weston_surface *parent =
2395 wl_resource_get_user_data(parent_resource);
2396
2397 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002398
Rafael Antognolli03b16592013-12-03 15:35:42 -02002399 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002400 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002401 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002402 wl_resource_get_user_data(seat_resource),
2403 serial, x, y);
2404}
2405
2406static void
2407set_maximized(struct shell_surface *shsurf,
2408 struct weston_output *output)
2409{
2410 struct desktop_shell *shell;
2411 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002412
Philip Withnall352e7ed2013-11-25 18:01:35 +00002413 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002414
2415 shell = shell_surface_get_shell(shsurf);
2416 panel_height = get_output_panel_height(shell, shsurf->output);
2417 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2418
2419 shsurf->client->send_configure(shsurf->surface, edges,
2420 shsurf->output->width,
2421 shsurf->output->height - panel_height);
2422
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002423 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002424}
2425
2426static void
2427unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002428{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002429 /* undo all maximized things here */
2430 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002431
2432 if (shsurf->saved_position_valid)
2433 weston_view_set_position(shsurf->view,
2434 shsurf->saved_x, shsurf->saved_y);
2435 else
2436 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002437
2438 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002439 wl_list_insert(&shsurf->view->geometry.transformation_list,
2440 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002441 shsurf->saved_rotation_valid = false;
2442 }
2443
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002444 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002445}
2446
Philip Withnallbecb77e2013-11-25 18:01:30 +00002447static void
2448shell_surface_set_maximized(struct wl_client *client,
2449 struct wl_resource *resource,
2450 struct wl_resource *output_resource)
2451{
2452 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2453 struct weston_output *output;
2454
2455 if (output_resource)
2456 output = wl_resource_get_user_data(output_resource);
2457 else
2458 output = NULL;
2459
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002460 shell_surface_set_parent(shsurf, NULL);
2461
Rafael Antognolli03b16592013-12-03 15:35:42 -02002462 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002463 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002464
2465 shsurf->next_state.maximized = true;
2466 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002467}
2468
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002469/* This is only ever called from set_surface_type(), so there’s no need to
2470 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002471static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002472reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002473{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002474 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002475 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002476 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002477 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002478
Pekka Paalanen98262232011-12-01 10:42:22 +02002479 return 0;
2480}
2481
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002482static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002483set_full_output(struct shell_surface *shsurf)
2484{
2485 shsurf->saved_x = shsurf->view->geometry.x;
2486 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002487 shsurf->saved_width = shsurf->surface->width;
2488 shsurf->saved_height = shsurf->surface->height;
2489 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002490 shsurf->saved_position_valid = true;
2491
2492 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2493 wl_list_remove(&shsurf->rotation.transform.link);
2494 wl_list_init(&shsurf->rotation.transform.link);
2495 weston_view_geometry_dirty(shsurf->view);
2496 shsurf->saved_rotation_valid = true;
2497 }
2498}
2499
2500static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002501set_surface_type(struct shell_surface *shsurf)
2502{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002503 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002504 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002505
Philip Withnallbecb77e2013-11-25 18:01:30 +00002506 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002507
Rafael Antognolli03b16592013-12-03 15:35:42 -02002508 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002509 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002510
2511 switch (shsurf->type) {
2512 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002513 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002514 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002515 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002516 weston_view_set_position(shsurf->view,
2517 pev->geometry.x + shsurf->transient.x,
2518 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002519 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002520 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002521
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002522 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002523 weston_view_set_position(shsurf->view, shsurf->transient.x,
2524 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002525 break;
2526
Philip Withnall0f640e22013-11-25 18:01:31 +00002527 case SHELL_SURFACE_POPUP:
2528 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002529 default:
2530 break;
2531 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002532
2533 /* Update the surface’s layer. */
2534 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002535}
2536
Tiago Vignattibe143262012-04-16 17:31:41 +03002537static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002538shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002539{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002540 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002541}
2542
Alex Wu21858432012-04-01 20:13:08 +08002543static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002544black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002545
Jason Ekstranda7af7042013-10-12 22:38:11 -05002546static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002547create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002548 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002549 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002550{
2551 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002552 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002553
2554 surface = weston_surface_create(ec);
2555 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002556 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002557 return NULL;
2558 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002559 view = weston_view_create(surface);
2560 if (surface == NULL) {
2561 weston_log("no memory\n");
2562 weston_surface_destroy(surface);
2563 return NULL;
2564 }
Alex Wu4539b082012-03-01 12:57:46 +08002565
Alex Wu21858432012-04-01 20:13:08 +08002566 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002567 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002568 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002569 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002570 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002571 pixman_region32_fini(&surface->input);
2572 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002573
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002574 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002575 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002576
2577 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002578}
2579
Philip Withnalled8f1a92013-11-25 18:01:43 +00002580static void
2581shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2582{
2583 struct weston_output *output = shsurf->fullscreen_output;
2584
Rafael Antognolli03b16592013-12-03 15:35:42 -02002585 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002586
2587 if (!shsurf->fullscreen.black_view)
2588 shsurf->fullscreen.black_view =
2589 create_black_surface(shsurf->surface->compositor,
2590 shsurf->surface,
2591 output->x, output->y,
2592 output->width,
2593 output->height);
2594
2595 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2596 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2597 wl_list_insert(&shsurf->view->layer_link,
2598 &shsurf->fullscreen.black_view->layer_link);
2599 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2600 weston_surface_damage(shsurf->surface);
2601}
2602
Alex Wu4539b082012-03-01 12:57:46 +08002603/* Create black surface and append it to the associated fullscreen surface.
2604 * Handle size dismatch and positioning according to the method. */
2605static void
2606shell_configure_fullscreen(struct shell_surface *shsurf)
2607{
2608 struct weston_output *output = shsurf->fullscreen_output;
2609 struct weston_surface *surface = shsurf->surface;
2610 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002611 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002612 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002613
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002614 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2615 restore_output_mode(output);
2616
Philip Withnalled8f1a92013-11-25 18:01:43 +00002617 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002618
Jason Ekstranda7af7042013-10-12 22:38:11 -05002619 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002620 &surf_width, &surf_height);
2621
Alex Wu4539b082012-03-01 12:57:46 +08002622 switch (shsurf->fullscreen.type) {
2623 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002624 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002625 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002626 break;
2627 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002628 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002629 if (output->width == surf_width &&
2630 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002631 weston_view_set_position(shsurf->view,
2632 output->x - surf_x,
2633 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002634 break;
2635 }
2636
Alex Wu4539b082012-03-01 12:57:46 +08002637 matrix = &shsurf->fullscreen.transform.matrix;
2638 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002639
Scott Moreau1bad5db2012-08-18 01:04:05 -06002640 output_aspect = (float) output->width /
2641 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002642 /* XXX: Use surf_width and surf_height here? */
2643 surface_aspect = (float) surface->width /
2644 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002645 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002646 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002647 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002648 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002649 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002650 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002651
Alex Wu4539b082012-03-01 12:57:46 +08002652 weston_matrix_scale(matrix, scale, scale, 1);
2653 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002654 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002655 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002656 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2657 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002658 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002659
Alex Wu4539b082012-03-01 12:57:46 +08002660 break;
2661 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002662 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002663 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002664 surf_width * surface->buffer_viewport.scale,
2665 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002666 shsurf->fullscreen.framerate};
2667
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002668 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002669 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002670 weston_view_set_position(shsurf->view,
2671 output->x - surf_x,
2672 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002673 shsurf->fullscreen.black_view->surface->width = output->width;
2674 shsurf->fullscreen.black_view->surface->height = output->height;
2675 weston_view_set_position(shsurf->fullscreen.black_view,
2676 output->x - surf_x,
2677 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002678 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002679 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002680 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002681 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002682 }
Alex Wubd3354b2012-04-17 17:20:49 +08002683 }
Alex Wu4539b082012-03-01 12:57:46 +08002684 break;
2685 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002686 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002687 break;
2688 default:
2689 break;
2690 }
2691}
2692
Alex Wu4539b082012-03-01 12:57:46 +08002693static void
2694shell_map_fullscreen(struct shell_surface *shsurf)
2695{
Alex Wubd3354b2012-04-17 17:20:49 +08002696 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002697}
2698
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002699static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002700set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2701{
2702 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002703 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002704 shsurf->transient.x = x;
2705 shsurf->transient.y = y;
2706 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002707
2708 shell_surface_set_parent(shsurf, NULL);
2709
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002710 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002711 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002712}
2713
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002714static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002715
2716static void
2717destroy_shell_seat(struct wl_listener *listener, void *data)
2718{
2719 struct shell_seat *shseat =
2720 container_of(listener,
2721 struct shell_seat, seat_destroy_listener);
2722 struct shell_surface *shsurf, *prev = NULL;
2723
2724 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002725 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002726 shseat->popup_grab.client = NULL;
2727
2728 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2729 shsurf->popup.shseat = NULL;
2730 if (prev) {
2731 wl_list_init(&prev->popup.grab_link);
2732 }
2733 prev = shsurf;
2734 }
2735 wl_list_init(&prev->popup.grab_link);
2736 }
2737
2738 wl_list_remove(&shseat->seat_destroy_listener.link);
2739 free(shseat);
2740}
2741
2742static struct shell_seat *
2743create_shell_seat(struct weston_seat *seat)
2744{
2745 struct shell_seat *shseat;
2746
2747 shseat = calloc(1, sizeof *shseat);
2748 if (!shseat) {
2749 weston_log("no memory to allocate shell seat\n");
2750 return NULL;
2751 }
2752
2753 shseat->seat = seat;
2754 wl_list_init(&shseat->popup_grab.surfaces_list);
2755
2756 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2757 wl_signal_add(&seat->destroy_signal,
2758 &shseat->seat_destroy_listener);
2759
2760 return shseat;
2761}
2762
2763static struct shell_seat *
2764get_shell_seat(struct weston_seat *seat)
2765{
2766 struct wl_listener *listener;
2767
2768 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2769 if (listener == NULL)
2770 return create_shell_seat(seat);
2771
2772 return container_of(listener,
2773 struct shell_seat, seat_destroy_listener);
2774}
2775
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002776static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002777popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002778{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002779 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002780 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002781 struct shell_seat *shseat =
2782 container_of(grab, struct shell_seat, popup_grab.grab);
2783 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002784 wl_fixed_t sx, sy;
2785
Jason Ekstranda7af7042013-10-12 22:38:11 -05002786 view = weston_compositor_pick_view(pointer->seat->compositor,
2787 pointer->x, pointer->y,
2788 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002789
Jason Ekstranda7af7042013-10-12 22:38:11 -05002790 if (view && view->surface->resource &&
2791 wl_resource_get_client(view->surface->resource) == client) {
2792 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002793 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002794 weston_pointer_set_focus(pointer, NULL,
2795 wl_fixed_from_int(0),
2796 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002797 }
2798}
2799
2800static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002801popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2802 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002803{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002804 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002805 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002806 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002807
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002808 weston_pointer_move(pointer, x, y);
2809
Neil Roberts96d790e2013-09-19 17:32:00 +01002810 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002811 weston_view_from_global_fixed(pointer->focus,
2812 pointer->x, pointer->y,
2813 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002814 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002815 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002816}
2817
2818static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002819popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002820 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002821{
2822 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002823 struct shell_seat *shseat =
2824 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002825 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002826 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002827 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002828 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002829
Neil Roberts96d790e2013-09-19 17:32:00 +01002830 resource_list = &grab->pointer->focus_resource_list;
2831 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002832 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002833 wl_resource_for_each(resource, resource_list) {
2834 wl_pointer_send_button(resource, serial,
2835 time, button, state);
2836 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002837 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002838 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002839 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002840 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002841 }
2842
Daniel Stone4dbadb12012-05-30 16:31:51 +01002843 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002844 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002845}
2846
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002847static void
2848popup_grab_cancel(struct weston_pointer_grab *grab)
2849{
2850 popup_grab_end(grab->pointer);
2851}
2852
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002853static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002854 popup_grab_focus,
2855 popup_grab_motion,
2856 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002857 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002858};
2859
2860static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002861shell_surface_send_popup_done(struct shell_surface *shsurf)
2862{
2863 if (shell_surface_is_wl_shell_surface(shsurf))
2864 wl_shell_surface_send_popup_done(shsurf->resource);
2865 else if (shell_surface_is_xdg_popup(shsurf))
2866 xdg_popup_send_popup_done(shsurf->resource,
2867 shsurf->popup.serial);
2868}
2869
2870static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002871popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002872{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002873 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002874 struct shell_seat *shseat =
2875 container_of(grab, struct shell_seat, popup_grab.grab);
2876 struct shell_surface *shsurf;
2877 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002878
2879 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002880 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002881 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002882 shseat->popup_grab.grab.interface = NULL;
2883 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002884 /* Send the popup_done event to all the popups open */
2885 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002886 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002887 shsurf->popup.shseat = NULL;
2888 if (prev) {
2889 wl_list_init(&prev->popup.grab_link);
2890 }
2891 prev = shsurf;
2892 }
2893 wl_list_init(&prev->popup.grab_link);
2894 wl_list_init(&shseat->popup_grab.surfaces_list);
2895 }
2896}
2897
2898static void
2899add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2900{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002901 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002902
2903 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002904 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002905 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002906 /* We must make sure here that this popup was opened after
2907 * a mouse press, and not just by moving around with other
2908 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002909 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002910 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002911
Rob Bradforddfe31052013-06-26 19:49:11 +01002912 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002913 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002914 } else {
2915 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002916 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002917}
2918
2919static void
2920remove_popup_grab(struct shell_surface *shsurf)
2921{
2922 struct shell_seat *shseat = shsurf->popup.shseat;
2923
2924 wl_list_remove(&shsurf->popup.grab_link);
2925 wl_list_init(&shsurf->popup.grab_link);
2926 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002927 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002928 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002929 }
2930}
2931
2932static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002933shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002934{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002935 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002936 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002937
Jason Ekstranda7af7042013-10-12 22:38:11 -05002938 shsurf->surface->output = parent_view->output;
2939 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002940
Jason Ekstranda7af7042013-10-12 22:38:11 -05002941 weston_view_set_transform_parent(shsurf->view, parent_view);
2942 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2943 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002944
Kristian Høgsberge3148752013-05-06 23:19:49 -04002945 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002946 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002947 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002948 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002949 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002950 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002951}
2952
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002953static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002954 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002955 shell_surface_move,
2956 shell_surface_resize,
2957 shell_surface_set_toplevel,
2958 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002959 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002960 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002961 shell_surface_set_maximized,
2962 shell_surface_set_title,
2963 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002964};
2965
2966static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002967destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002968{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002969 struct shell_surface *child, *next;
2970
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002971 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2972
Giulio Camuffo5085a752013-03-25 21:42:45 +01002973 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2974 remove_popup_grab(shsurf);
2975 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002976
Alex Wubd3354b2012-04-17 17:20:49 +08002977 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002978 shell_surface_is_top_fullscreen(shsurf))
2979 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002980
Jason Ekstranda7af7042013-10-12 22:38:11 -05002981 if (shsurf->fullscreen.black_view)
2982 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002983
Alex Wubd3354b2012-04-17 17:20:49 +08002984 /* As destroy_resource() use wl_list_for_each_safe(),
2985 * we can always remove the listener.
2986 */
2987 wl_list_remove(&shsurf->surface_destroy_listener.link);
2988 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07002989 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002990
Jason Ekstranda7af7042013-10-12 22:38:11 -05002991 weston_view_destroy(shsurf->view);
2992
Philip Withnall648a4dd2013-11-25 18:01:44 +00002993 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01002994 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
2995 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002996
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002997 wl_list_remove(&shsurf->link);
2998 free(shsurf);
2999}
3000
3001static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003002shell_destroy_shell_surface(struct wl_resource *resource)
3003{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003004 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003005
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003006 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003007}
3008
3009static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003010shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003011{
3012 struct shell_surface *shsurf = container_of(listener,
3013 struct shell_surface,
3014 surface_destroy_listener);
3015
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003016 if (shsurf->resource)
3017 wl_resource_destroy(shsurf->resource);
3018 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003019 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003020}
3021
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003022static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003023fade_out_done(struct weston_view_animation *animation, void *data)
3024{
3025 struct shell_surface *shsurf = data;
3026
3027 weston_surface_destroy(shsurf->surface);
3028}
3029
3030static void
3031handle_resource_destroy(struct wl_listener *listener, void *data)
3032{
3033 struct shell_surface *shsurf =
3034 container_of(listener, struct shell_surface,
3035 resource_destroy_listener);
3036
3037 shsurf->surface->ref_count++;
3038
3039 pixman_region32_fini(&shsurf->surface->pending.input);
3040 pixman_region32_init(&shsurf->surface->pending.input);
3041 pixman_region32_fini(&shsurf->surface->input);
3042 pixman_region32_init(&shsurf->surface->input);
Kristian Høgsbergb033c5e2014-03-20 14:49:07 -07003043 if (weston_surface_is_mapped(shsurf->surface))
3044 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3045 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003046}
3047
3048static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003049shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003050
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003051struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003052get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003053{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003054 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003055 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003056 else
3057 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003058}
3059
Rafael Antognollie2a34552013-12-03 15:35:45 -02003060static struct shell_surface *
3061create_common_surface(void *shell, struct weston_surface *surface,
3062 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003063{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003064 struct shell_surface *shsurf;
3065
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003066 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003067 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003068 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003069 }
3070
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003071 shsurf = calloc(1, sizeof *shsurf);
3072 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003073 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003074 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003075 }
3076
Jason Ekstranda7af7042013-10-12 22:38:11 -05003077 shsurf->view = weston_view_create(surface);
3078 if (!shsurf->view) {
3079 weston_log("no memory to allocate shell surface\n");
3080 free(shsurf);
3081 return NULL;
3082 }
3083
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003084 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003085 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003086
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003087 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3088 wl_resource_add_destroy_listener(surface->resource,
3089 &shsurf->resource_destroy_listener);
3090
Tiago Vignattibc052c92012-04-19 16:18:18 +03003091 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003092 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003093 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003094 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003095 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003096 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003097 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3098 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003099 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003100 wl_list_init(&shsurf->fullscreen.transform.link);
3101
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003102 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003103 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003104 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003105 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003106
3107 /* init link so its safe to always remove it in destroy_shell_surface */
3108 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003109 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003110
Pekka Paalanen460099f2012-01-20 16:48:25 +02003111 /* empty when not in use */
3112 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003113 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003114
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003115 wl_list_init(&shsurf->workspace_transform.link);
3116
Philip Withnall648a4dd2013-11-25 18:01:44 +00003117 wl_list_init(&shsurf->children_link);
3118 wl_list_init(&shsurf->children_list);
3119 shsurf->parent = NULL;
3120
Pekka Paalanen98262232011-12-01 10:42:22 +02003121 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003122
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003123 shsurf->client = client;
3124
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003125 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003126}
3127
Rafael Antognollie2a34552013-12-03 15:35:45 -02003128static struct shell_surface *
3129create_shell_surface(void *shell, struct weston_surface *surface,
3130 const struct weston_shell_client *client)
3131{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003132 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003133}
3134
Jason Ekstranda7af7042013-10-12 22:38:11 -05003135static struct weston_view *
3136get_primary_view(void *shell, struct shell_surface *shsurf)
3137{
3138 return shsurf->view;
3139}
3140
Tiago Vignattibc052c92012-04-19 16:18:18 +03003141static void
3142shell_get_shell_surface(struct wl_client *client,
3143 struct wl_resource *resource,
3144 uint32_t id,
3145 struct wl_resource *surface_resource)
3146{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003147 struct weston_surface *surface =
3148 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003149 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003150 struct shell_surface *shsurf;
3151
3152 if (get_shell_surface(surface)) {
3153 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003154 WL_DISPLAY_ERROR_INVALID_OBJECT,
3155 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003156 return;
3157 }
3158
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003159 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003160 if (!shsurf) {
3161 wl_resource_post_error(surface_resource,
3162 WL_DISPLAY_ERROR_INVALID_OBJECT,
3163 "surface->configure already set");
3164 return;
3165 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003166
Jason Ekstranda85118c2013-06-27 20:17:02 -05003167 shsurf->resource =
3168 wl_resource_create(client,
3169 &wl_shell_surface_interface, 1, id);
3170 wl_resource_set_implementation(shsurf->resource,
3171 &shell_surface_implementation,
3172 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003173}
3174
Rafael Antognollie2a34552013-12-03 15:35:45 -02003175static bool
3176shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3177{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003178 /* A shell surface without a resource is created from xwayland
3179 * and is considered a wl_shell surface for now. */
3180
3181 return shsurf->resource == NULL ||
3182 wl_resource_instance_of(shsurf->resource,
3183 &wl_shell_surface_interface,
3184 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003185}
3186
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003187static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003188 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003189};
3190
Rafael Antognollie2a34552013-12-03 15:35:45 -02003191/****************************
3192 * xdg-shell implementation */
3193
3194static void
3195xdg_surface_destroy(struct wl_client *client,
3196 struct wl_resource *resource)
3197{
3198 wl_resource_destroy(resource);
3199}
3200
3201static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003202xdg_surface_set_transient_for(struct wl_client *client,
3203 struct wl_resource *resource,
3204 struct wl_resource *parent_resource)
3205{
3206 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3207 struct weston_surface *parent;
3208
3209 if (parent_resource)
3210 parent = wl_resource_get_user_data(parent_resource);
3211 else
3212 parent = NULL;
3213
3214 shell_surface_set_parent(shsurf, parent);
3215}
3216
3217static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003218xdg_surface_set_margin(struct wl_client *client,
3219 struct wl_resource *resource,
3220 int32_t left,
3221 int32_t right,
3222 int32_t top,
3223 int32_t bottom)
3224{
3225 /* Do nothing, Weston doesn't try to constrain or place
3226 * surfaces in any special manner... */
3227}
3228
3229static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003230xdg_surface_set_app_id(struct wl_client *client,
3231 struct wl_resource *resource,
3232 const char *app_id)
3233{
3234 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3235
3236 free(shsurf->class);
3237 shsurf->class = strdup(app_id);
3238}
3239
3240static void
3241xdg_surface_set_title(struct wl_client *client,
3242 struct wl_resource *resource, const char *title)
3243{
3244 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3245
3246 set_title(shsurf, title);
3247}
3248
3249static void
3250xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3251 struct wl_resource *seat_resource, uint32_t serial)
3252{
3253 common_surface_move(resource, seat_resource, serial);
3254}
3255
3256static void
3257xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3258 struct wl_resource *seat_resource, uint32_t serial,
3259 uint32_t edges)
3260{
3261 common_surface_resize(resource, seat_resource, serial, edges);
3262}
3263
3264static void
3265xdg_surface_set_output(struct wl_client *client,
3266 struct wl_resource *resource,
3267 struct wl_resource *output_resource)
3268{
3269 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3270 struct weston_output *output;
3271
3272 if (output_resource)
3273 output = wl_resource_get_user_data(output_resource);
3274 else
3275 output = NULL;
3276
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003277 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003278}
3279
3280static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003281xdg_surface_change_state(struct shell_surface *shsurf,
3282 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003283{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003284 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003285 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003286
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003287 switch (state) {
3288 case XDG_SURFACE_STATE_MAXIMIZED:
3289 shsurf->requested_state.maximized = value;
3290 if (value)
3291 set_maximized(shsurf, NULL);
3292 break;
3293 case XDG_SURFACE_STATE_FULLSCREEN:
3294 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003295
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003296 if (value)
3297 set_fullscreen(shsurf,
3298 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3299 0, shsurf->recommended_output);
3300 break;
3301 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003302}
3303
3304static void
3305xdg_surface_request_change_state(struct wl_client *client,
3306 struct wl_resource *resource,
3307 uint32_t state,
3308 uint32_t value,
3309 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003310{
3311 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003312
3313 /* The client can't know what the current state is, so we need
3314 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003315
3316 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3317 return;
3318
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003319 switch (state) {
3320 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003321 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003322 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003323 default:
3324 /* send error? ignore? send change state with value 0? */
3325 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003326 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003327
3328 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003329}
3330
3331static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003332xdg_surface_ack_change_state(struct wl_client *client,
3333 struct wl_resource *resource,
3334 uint32_t state,
3335 uint32_t value,
3336 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003337{
3338 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3339
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003340 if (shsurf->state_requested) {
3341 shsurf->next_state = shsurf->requested_state;
3342 shsurf->state_changed = true;
3343 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003344 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003345}
3346
3347static const struct xdg_surface_interface xdg_surface_implementation = {
3348 xdg_surface_destroy,
3349 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003350 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003351 xdg_surface_set_title,
3352 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003353 xdg_surface_move,
3354 xdg_surface_resize,
3355 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003356 xdg_surface_request_change_state,
3357 xdg_surface_ack_change_state,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003358 NULL /* set_minimized */
3359};
3360
3361static void
3362xdg_send_configure(struct weston_surface *surface,
3363 uint32_t edges, int32_t width, int32_t height)
3364{
3365 struct shell_surface *shsurf = get_shell_surface(surface);
3366
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003367 assert(shsurf);
3368
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003369 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003370}
3371
3372static const struct weston_shell_client xdg_client = {
3373 xdg_send_configure
3374};
3375
3376static void
3377xdg_use_unstable_version(struct wl_client *client,
3378 struct wl_resource *resource,
3379 int32_t version)
3380{
3381 if (version > 1) {
3382 wl_resource_post_error(resource,
3383 1,
3384 "xdg-shell:: version not implemented yet.");
3385 return;
3386 }
3387}
3388
3389static struct shell_surface *
3390create_xdg_surface(void *shell, struct weston_surface *surface,
3391 const struct weston_shell_client *client)
3392{
3393 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003394
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003395 shsurf = create_common_surface(shell, surface, client);
3396 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003397
3398 return shsurf;
3399}
3400
3401static void
3402xdg_get_xdg_surface(struct wl_client *client,
3403 struct wl_resource *resource,
3404 uint32_t id,
3405 struct wl_resource *surface_resource)
3406{
3407 struct weston_surface *surface =
3408 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003409 struct shell_client *sc = wl_resource_get_user_data(resource);
3410 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003411 struct shell_surface *shsurf;
3412
3413 if (get_shell_surface(surface)) {
3414 wl_resource_post_error(surface_resource,
3415 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003416 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003417 return;
3418 }
3419
3420 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3421 if (!shsurf) {
3422 wl_resource_post_error(surface_resource,
3423 WL_DISPLAY_ERROR_INVALID_OBJECT,
3424 "surface->configure already set");
3425 return;
3426 }
3427
3428 shsurf->resource =
3429 wl_resource_create(client,
3430 &xdg_surface_interface, 1, id);
3431 wl_resource_set_implementation(shsurf->resource,
3432 &xdg_surface_implementation,
3433 shsurf, shell_destroy_shell_surface);
3434}
3435
3436static bool
3437shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3438{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003439 return shsurf->resource &&
3440 wl_resource_instance_of(shsurf->resource,
3441 &xdg_surface_interface,
3442 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003443}
3444
3445/* xdg-popup implementation */
3446
3447static void
3448xdg_popup_destroy(struct wl_client *client,
3449 struct wl_resource *resource)
3450{
3451 wl_resource_destroy(resource);
3452}
3453
Rafael Antognollie2a34552013-12-03 15:35:45 -02003454static const struct xdg_popup_interface xdg_popup_implementation = {
3455 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003456};
3457
3458static void
3459xdg_popup_send_configure(struct weston_surface *surface,
3460 uint32_t edges, int32_t width, int32_t height)
3461{
3462}
3463
3464static const struct weston_shell_client xdg_popup_client = {
3465 xdg_popup_send_configure
3466};
3467
3468static struct shell_surface *
3469create_xdg_popup(void *shell, struct weston_surface *surface,
3470 const struct weston_shell_client *client,
3471 struct weston_surface *parent,
3472 struct shell_seat *seat,
3473 uint32_t serial,
3474 int32_t x, int32_t y)
3475{
3476 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003477
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003478 shsurf = create_common_surface(shell, surface, client);
3479 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003480 shsurf->popup.shseat = seat;
3481 shsurf->popup.serial = serial;
3482 shsurf->popup.x = x;
3483 shsurf->popup.y = y;
3484 shell_surface_set_parent(shsurf, parent);
3485
3486 return shsurf;
3487}
3488
3489static void
3490xdg_get_xdg_popup(struct wl_client *client,
3491 struct wl_resource *resource,
3492 uint32_t id,
3493 struct wl_resource *surface_resource,
3494 struct wl_resource *parent_resource,
3495 struct wl_resource *seat_resource,
3496 uint32_t serial,
3497 int32_t x, int32_t y, uint32_t flags)
3498{
3499 struct weston_surface *surface =
3500 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003501 struct shell_client *sc = wl_resource_get_user_data(resource);
3502 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003503 struct shell_surface *shsurf;
3504 struct weston_surface *parent;
3505 struct shell_seat *seat;
3506
3507 if (get_shell_surface(surface)) {
3508 wl_resource_post_error(surface_resource,
3509 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003510 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003511 return;
3512 }
3513
3514 if (!parent_resource) {
3515 wl_resource_post_error(surface_resource,
3516 WL_DISPLAY_ERROR_INVALID_OBJECT,
3517 "xdg_shell::get_xdg_popup requires a parent shell surface");
3518 }
3519
3520 parent = wl_resource_get_user_data(parent_resource);
3521 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3522
3523 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3524 parent, seat, serial, x, y);
3525 if (!shsurf) {
3526 wl_resource_post_error(surface_resource,
3527 WL_DISPLAY_ERROR_INVALID_OBJECT,
3528 "surface->configure already set");
3529 return;
3530 }
3531
3532 shsurf->resource =
3533 wl_resource_create(client,
3534 &xdg_popup_interface, 1, id);
3535 wl_resource_set_implementation(shsurf->resource,
3536 &xdg_popup_implementation,
3537 shsurf, shell_destroy_shell_surface);
3538}
3539
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003540static void
3541xdg_pong(struct wl_client *client,
3542 struct wl_resource *resource, uint32_t serial)
3543{
3544 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003545
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003546 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003547}
3548
Rafael Antognollie2a34552013-12-03 15:35:45 -02003549static bool
3550shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3551{
3552 return wl_resource_instance_of(shsurf->resource,
3553 &xdg_popup_interface,
3554 &xdg_popup_implementation);
3555}
3556
3557static const struct xdg_shell_interface xdg_implementation = {
3558 xdg_use_unstable_version,
3559 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003560 xdg_get_xdg_popup,
3561 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003562};
3563
3564static int
3565xdg_shell_unversioned_dispatch(const void *implementation,
3566 void *_target, uint32_t opcode,
3567 const struct wl_message *message,
3568 union wl_argument *args)
3569{
3570 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003571 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003572
3573 if (opcode != 0) {
3574 wl_resource_post_error(resource,
3575 WL_DISPLAY_ERROR_INVALID_OBJECT,
3576 "must call use_unstable_version first");
3577 return 0;
3578 }
3579
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003580#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003581
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003582 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3583 "shell implementation doesn't match protocol version");
3584
Rafael Antognollie2a34552013-12-03 15:35:45 -02003585 if (args[0].i != XDG_SERVER_VERSION) {
3586 wl_resource_post_error(resource,
3587 WL_DISPLAY_ERROR_INVALID_OBJECT,
3588 "incompatible version, server is %d "
3589 "client wants %d",
3590 XDG_SERVER_VERSION, args[0].i);
3591 return 0;
3592 }
3593
3594 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003595 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003596
3597 return 1;
3598}
3599
3600/* end of xdg-shell implementation */
3601/***********************************/
3602
Kristian Høgsberg07937562011-04-12 17:25:42 -04003603static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003604shell_fade(struct desktop_shell *shell, enum fade_type type);
3605
3606static int
3607screensaver_timeout(void *data)
3608{
3609 struct desktop_shell *shell = data;
3610
3611 shell_fade(shell, FADE_OUT);
3612
3613 return 1;
3614}
3615
3616static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003617handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003618{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003619 struct desktop_shell *shell =
3620 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003621
Pekka Paalanen18027e52011-12-02 16:31:49 +02003622 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003623
3624 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003625 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003626}
3627
3628static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003629launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003630{
3631 if (shell->screensaver.binding)
3632 return;
3633
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003634 if (!shell->screensaver.path) {
3635 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003636 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003637 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003638
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003639 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003640 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003641 return;
3642 }
3643
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003644 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003645 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003646 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003647 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003648}
3649
3650static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003651terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003652{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003653 if (shell->screensaver.process.pid == 0)
3654 return;
3655
3656 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003657}
3658
3659static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003660configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003661{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003662 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003663
Jason Ekstranda7af7042013-10-12 22:38:11 -05003664 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3665 if (v->output == ev->output && v != ev) {
3666 weston_view_unmap(v);
3667 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003668 }
3669 }
3670
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003671 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003672
Jason Ekstranda7af7042013-10-12 22:38:11 -05003673 if (wl_list_empty(&ev->layer_link)) {
3674 wl_list_insert(&layer->view_list, &ev->layer_link);
3675 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003676 }
3677}
3678
3679static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003680background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003681{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003682 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003683 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003684
Jason Ekstranda7af7042013-10-12 22:38:11 -05003685 view = container_of(es->views.next, struct weston_view, surface_link);
3686
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003687 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003688}
3689
3690static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003691desktop_shell_set_background(struct wl_client *client,
3692 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003693 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003694 struct wl_resource *surface_resource)
3695{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003696 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003697 struct weston_surface *surface =
3698 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003699 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003700
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003701 if (surface->configure) {
3702 wl_resource_post_error(surface_resource,
3703 WL_DISPLAY_ERROR_INVALID_OBJECT,
3704 "surface role already assigned");
3705 return;
3706 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003707
Jason Ekstranda7af7042013-10-12 22:38:11 -05003708 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3709 weston_view_destroy(view);
3710 view = weston_view_create(surface);
3711
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003712 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003713 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003714 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003715 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003716 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003717 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003718 surface->output->width,
3719 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003720}
3721
3722static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003723panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003724{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003725 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003726 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003727
Jason Ekstranda7af7042013-10-12 22:38:11 -05003728 view = container_of(es->views.next, struct weston_view, surface_link);
3729
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003730 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003731}
3732
3733static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003734desktop_shell_set_panel(struct wl_client *client,
3735 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003736 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003737 struct wl_resource *surface_resource)
3738{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003739 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003740 struct weston_surface *surface =
3741 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003742 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003743
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003744 if (surface->configure) {
3745 wl_resource_post_error(surface_resource,
3746 WL_DISPLAY_ERROR_INVALID_OBJECT,
3747 "surface role already assigned");
3748 return;
3749 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003750
Jason Ekstranda7af7042013-10-12 22:38:11 -05003751 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3752 weston_view_destroy(view);
3753 view = weston_view_create(surface);
3754
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003755 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003756 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003757 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003758 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003759 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003760 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003761 surface->output->width,
3762 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003763}
3764
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003765static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003766lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003767{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003768 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003769 struct weston_view *view;
3770
3771 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003772
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003773 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003774 return;
3775
Jason Ekstranda7af7042013-10-12 22:38:11 -05003776 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003777
3778 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003779 wl_list_insert(&shell->lock_layer.view_list,
3780 &view->layer_link);
3781 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003782 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003783 }
3784}
3785
3786static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003787handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003788{
Tiago Vignattibe143262012-04-16 17:31:41 +03003789 struct desktop_shell *shell =
3790 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003791
Martin Minarik6d118362012-06-07 18:01:59 +02003792 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003793 shell->lock_surface = NULL;
3794}
3795
3796static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003797desktop_shell_set_lock_surface(struct wl_client *client,
3798 struct wl_resource *resource,
3799 struct wl_resource *surface_resource)
3800{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003801 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003802 struct weston_surface *surface =
3803 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003804
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003805 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003806
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003807 if (!shell->locked)
3808 return;
3809
Pekka Paalanen98262232011-12-01 10:42:22 +02003810 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003811
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003812 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003813 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003814 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003815
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003816 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003817 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003818 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003819}
3820
3821static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003822resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003823{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003824 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003825
Pekka Paalanen77346a62011-11-30 16:26:35 +02003826 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003827
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003828 wl_list_remove(&shell->lock_layer.link);
3829 wl_list_insert(&shell->compositor->cursor_layer.link,
3830 &shell->fullscreen_layer.link);
3831 wl_list_insert(&shell->fullscreen_layer.link,
3832 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003833 if (shell->showing_input_panels) {
3834 wl_list_insert(&shell->panel_layer.link,
3835 &shell->input_panel_layer.link);
3836 wl_list_insert(&shell->input_panel_layer.link,
3837 &ws->layer.link);
3838 } else {
3839 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3840 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003841
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003842 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003843
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003844 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003845 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003846 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003847}
3848
3849static void
3850desktop_shell_unlock(struct wl_client *client,
3851 struct wl_resource *resource)
3852{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003853 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003854
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003855 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003856
3857 if (shell->locked)
3858 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003859}
3860
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003861static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003862desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003863 struct wl_resource *resource,
3864 struct wl_resource *surface_resource)
3865{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003866 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003867
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003868 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003869 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003870}
3871
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003872static void
3873desktop_shell_desktop_ready(struct wl_client *client,
3874 struct wl_resource *resource)
3875{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003876 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003877
3878 shell_fade_startup(shell);
3879}
3880
Kristian Høgsberg75840622011-09-06 13:48:16 -04003881static const struct desktop_shell_interface desktop_shell_implementation = {
3882 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003883 desktop_shell_set_panel,
3884 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003885 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003886 desktop_shell_set_grab_surface,
3887 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003888};
3889
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003890static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003891get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003892{
3893 struct shell_surface *shsurf;
3894
3895 shsurf = get_shell_surface(surface);
3896 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003897 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003898 return shsurf->type;
3899}
3900
Kristian Høgsberg75840622011-09-06 13:48:16 -04003901static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003902move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003903{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003904 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003905 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003906 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003907
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003908 if (seat->pointer->focus == NULL)
3909 return;
3910
3911 focus = seat->pointer->focus->surface;
3912
Pekka Paalanen01388e22013-04-25 13:57:44 +03003913 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003914 if (surface == NULL)
3915 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003916
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003917 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003918 if (shsurf == NULL || shsurf->state.fullscreen ||
3919 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003920 return;
3921
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003922 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003923}
3924
3925static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003926maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3927{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003928 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003929 struct weston_surface *surface;
3930 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003931 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003932
3933 surface = weston_surface_get_main_surface(focus);
3934 if (surface == NULL)
3935 return;
3936
3937 shsurf = get_shell_surface(surface);
3938 if (shsurf == NULL)
3939 return;
3940
3941 if (!shell_surface_is_xdg_surface(shsurf))
3942 return;
3943
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003944 serial = wl_display_next_serial(seat->compositor->wl_display);
3945 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
3946 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003947}
3948
3949static void
3950fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3951{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003952 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003953 struct weston_surface *surface;
3954 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003955 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003956
3957 surface = weston_surface_get_main_surface(focus);
3958 if (surface == NULL)
3959 return;
3960
3961 shsurf = get_shell_surface(surface);
3962 if (shsurf == NULL)
3963 return;
3964
3965 if (!shell_surface_is_xdg_surface(shsurf))
3966 return;
3967
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003968 serial = wl_display_next_serial(seat->compositor->wl_display);
3969 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
3970 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003971}
3972
3973static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003974touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3975{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003976 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003977 struct weston_surface *surface;
3978 struct shell_surface *shsurf;
3979
3980 surface = weston_surface_get_main_surface(focus);
3981 if (surface == NULL)
3982 return;
3983
3984 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003985 if (shsurf == NULL || shsurf->state.fullscreen ||
3986 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003987 return;
3988
3989 surface_touch_move(shsurf, (struct weston_seat *) seat);
3990}
3991
3992static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003993resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003994{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003995 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003996 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003997 uint32_t edges = 0;
3998 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003999 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004000
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004001 if (seat->pointer->focus == NULL)
4002 return;
4003
4004 focus = seat->pointer->focus->surface;
4005
Pekka Paalanen01388e22013-04-25 13:57:44 +03004006 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004007 if (surface == NULL)
4008 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004009
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004010 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004011 if (shsurf == NULL || shsurf->state.fullscreen ||
4012 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004013 return;
4014
Jason Ekstranda7af7042013-10-12 22:38:11 -05004015 weston_view_from_global(shsurf->view,
4016 wl_fixed_to_int(seat->pointer->grab_x),
4017 wl_fixed_to_int(seat->pointer->grab_y),
4018 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004019
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004020 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004021 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004022 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004023 edges |= 0;
4024 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004025 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004026
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004027 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004028 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004029 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004030 edges |= 0;
4031 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004032 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004033
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004034 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004035}
4036
4037static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004038surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004039 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004040{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004041 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004042 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004043 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004044 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004045
Pekka Paalanen01388e22013-04-25 13:57:44 +03004046 /* XXX: broken for windows containing sub-surfaces */
4047 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004048 if (surface == NULL)
4049 return;
4050
4051 shsurf = get_shell_surface(surface);
4052 if (!shsurf)
4053 return;
4054
Jason Ekstranda7af7042013-10-12 22:38:11 -05004055 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004056
Jason Ekstranda7af7042013-10-12 22:38:11 -05004057 if (shsurf->view->alpha > 1.0)
4058 shsurf->view->alpha = 1.0;
4059 if (shsurf->view->alpha < step)
4060 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004061
Jason Ekstranda7af7042013-10-12 22:38:11 -05004062 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004063 weston_surface_damage(surface);
4064}
4065
4066static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004067do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004068 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004069{
Daniel Stone37816df2012-05-16 18:45:18 +01004070 struct weston_seat *ws = (struct weston_seat *) seat;
4071 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004072 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004073 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004074
4075 wl_list_for_each(output, &compositor->output_list, link) {
4076 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004077 wl_fixed_to_double(seat->pointer->x),
4078 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004079 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004080 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004081 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004082 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004083 increment = -output->zoom.increment;
4084 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004085 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004086 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004087 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004088 else
4089 increment = 0;
4090
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004091 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004092
Scott Moreaue6603982012-06-11 13:07:51 -06004093 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004094 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004095 else if (output->zoom.level > output->zoom.max_level)
4096 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004097 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004098 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004099 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004100
Scott Moreaue6603982012-06-11 13:07:51 -06004101 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004102
Jason Ekstranda7af7042013-10-12 22:38:11 -05004103 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004104 }
4105 }
4106}
4107
Scott Moreauccbf29d2012-02-22 14:21:41 -07004108static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004109zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004110 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004111{
4112 do_zoom(seat, time, 0, axis, value);
4113}
4114
4115static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004116zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004117 void *data)
4118{
4119 do_zoom(seat, time, key, 0, 0);
4120}
4121
4122static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004123terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004124 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004125{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004126 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004127
Daniel Stone325fc2d2012-05-30 16:31:58 +01004128 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004129}
4130
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004131static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004132rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4133 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004134{
4135 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004136 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004137 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004138 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004139 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004140
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004141 weston_pointer_move(pointer, x, y);
4142
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004143 if (!shsurf)
4144 return;
4145
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004146 cx = 0.5f * shsurf->surface->width;
4147 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004148
Daniel Stone37816df2012-05-16 18:45:18 +01004149 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4150 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004151 r = sqrtf(dx * dx + dy * dy);
4152
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004153 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004154 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004155
4156 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004157 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004158 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004159
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004160 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004161 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004162
4163 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004164 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004165 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004166 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004167 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004168
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004169 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004170 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004171 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004172 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004173 wl_list_init(&shsurf->rotation.transform.link);
4174 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004175 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004176 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004177
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004178 /* We need to adjust the position of the surface
4179 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004180 cposx = shsurf->view->geometry.x + cx;
4181 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004182 dposx = rotate->center.x - cposx;
4183 dposy = rotate->center.y - cposy;
4184 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004185 weston_view_set_position(shsurf->view,
4186 shsurf->view->geometry.x + dposx,
4187 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004188 }
4189
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004190 /* Repaint implies weston_surface_update_transform(), which
4191 * lazily applies the damage due to rotation update.
4192 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004193 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004194}
4195
4196static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004197rotate_grab_button(struct weston_pointer_grab *grab,
4198 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004199{
4200 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004201 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004202 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004203 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004204 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004205
Daniel Stone4dbadb12012-05-30 16:31:51 +01004206 if (pointer->button_count == 0 &&
4207 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004208 if (shsurf)
4209 weston_matrix_multiply(&shsurf->rotation.rotation,
4210 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004211 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004212 free(rotate);
4213 }
4214}
4215
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004216static void
4217rotate_grab_cancel(struct weston_pointer_grab *grab)
4218{
4219 struct rotate_grab *rotate =
4220 container_of(grab, struct rotate_grab, base.grab);
4221
4222 shell_grab_end(&rotate->base);
4223 free(rotate);
4224}
4225
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004226static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004227 noop_grab_focus,
4228 rotate_grab_motion,
4229 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004230 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004231};
4232
4233static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004234surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004235{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004236 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004237 float dx, dy;
4238 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004239
Pekka Paalanen460099f2012-01-20 16:48:25 +02004240 rotate = malloc(sizeof *rotate);
4241 if (!rotate)
4242 return;
4243
Jason Ekstranda7af7042013-10-12 22:38:11 -05004244 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004245 surface->surface->width * 0.5f,
4246 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004247 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004248
Daniel Stone37816df2012-05-16 18:45:18 +01004249 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4250 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004251 r = sqrtf(dx * dx + dy * dy);
4252 if (r > 20.0f) {
4253 struct weston_matrix inverse;
4254
4255 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004256 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004257 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004258
4259 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004260 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004261 } else {
4262 weston_matrix_init(&surface->rotation.rotation);
4263 weston_matrix_init(&rotate->rotation);
4264 }
4265
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004266 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4267 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004268}
4269
4270static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004271rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004272 void *data)
4273{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004274 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004275 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004276 struct shell_surface *surface;
4277
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004278 if (seat->pointer->focus == NULL)
4279 return;
4280
4281 focus = seat->pointer->focus->surface;
4282
Pekka Paalanen01388e22013-04-25 13:57:44 +03004283 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004284 if (base_surface == NULL)
4285 return;
4286
4287 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004288 if (surface == NULL || surface->state.fullscreen ||
4289 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004290 return;
4291
4292 surface_rotate(surface, seat);
4293}
4294
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004295/* Move all fullscreen layers down to the current workspace in a non-reversible
4296 * manner. This should be used when implementing shell-wide overlays, such as
4297 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004298void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004299lower_fullscreen_layer(struct desktop_shell *shell)
4300{
4301 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004302 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004303
4304 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004305 wl_list_for_each_reverse_safe(view, prev,
4306 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004307 layer_link) {
4308 wl_list_remove(&view->layer_link);
4309 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4310 weston_view_damage_below(view);
4311 weston_surface_damage(view->surface);
4312 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004313}
4314
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004315void
Tiago Vignattibe143262012-04-16 17:31:41 +03004316activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004317 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004318{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004319 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004320 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004321 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004322 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004323 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004324
Pekka Paalanen01388e22013-04-25 13:57:44 +03004325 main_surface = weston_surface_get_main_surface(es);
4326
Daniel Stone37816df2012-05-16 18:45:18 +01004327 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004328
Jonas Ådahl8538b222012-08-29 22:13:03 +02004329 state = ensure_focus_state(shell, seat);
4330 if (state == NULL)
4331 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004332
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004333 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004334 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004335
Rafael Antognolli03b16592013-12-03 15:35:42 -02004336 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004337 assert(shsurf);
4338
Rafael Antognolli03b16592013-12-03 15:35:42 -02004339 if (shsurf->state.fullscreen)
4340 shell_configure_fullscreen(shsurf);
4341 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004342 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004343
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004344 /* Update the surface’s layer. This brings it to the top of the stacking
4345 * order as appropriate. */
4346 shell_surface_update_layer(shsurf);
4347
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004348 if (shell->focus_animation_type != ANIMATION_NONE) {
4349 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004350 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004351 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004352}
4353
Alex Wu21858432012-04-01 20:13:08 +08004354/* no-op func for checking black surface */
4355static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004356black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004357{
4358}
4359
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004360static bool
Alex Wu21858432012-04-01 20:13:08 +08004361is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4362{
4363 if (es->configure == black_surface_configure) {
4364 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004365 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004366 return true;
4367 }
4368 return false;
4369}
4370
Kristian Høgsberg75840622011-09-06 13:48:16 -04004371static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004372activate_binding(struct weston_seat *seat,
4373 struct desktop_shell *shell,
4374 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004375{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004376 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004377
Alex Wu9c35e6b2012-03-05 14:13:13 +08004378 if (!focus)
4379 return;
4380
Pekka Paalanen01388e22013-04-25 13:57:44 +03004381 if (is_black_surface(focus, &main_surface))
4382 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004383
Pekka Paalanen01388e22013-04-25 13:57:44 +03004384 main_surface = weston_surface_get_main_surface(focus);
4385 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004386 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004387
Neil Robertsa28c6932013-10-03 16:43:04 +01004388 activate(shell, focus, seat);
4389}
4390
4391static void
4392click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4393 void *data)
4394{
4395 if (seat->pointer->grab != &seat->pointer->default_grab)
4396 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004397 if (seat->pointer->focus == NULL)
4398 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004399
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004400 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004401}
4402
4403static void
4404touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4405{
4406 if (seat->touch->grab != &seat->touch->default_grab)
4407 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004408 if (seat->touch->focus == NULL)
4409 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004410
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004411 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004412}
4413
4414static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004415lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004416{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004417 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004418
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004419 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004420 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004421 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004422 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004423
4424 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004425
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004426 /* Hide all surfaces by removing the fullscreen, panel and
4427 * toplevel layers. This way nothing else can show or receive
4428 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004429
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004430 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004431 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004432 if (shell->showing_input_panels)
4433 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004434 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004435 wl_list_insert(&shell->compositor->cursor_layer.link,
4436 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004437
Pekka Paalanen77346a62011-11-30 16:26:35 +02004438 launch_screensaver(shell);
4439
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004440 /* TODO: disable bindings that should not work while locked. */
4441
4442 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004443}
4444
4445static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004446unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004447{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004448 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004449 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004450 return;
4451 }
4452
4453 /* If desktop-shell client has gone away, unlock immediately. */
4454 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004455 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004456 return;
4457 }
4458
4459 if (shell->prepare_event_sent)
4460 return;
4461
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004462 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004463 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004464}
4465
4466static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004467shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004468{
4469 struct desktop_shell *shell = data;
4470
4471 shell->fade.animation = NULL;
4472
4473 switch (shell->fade.type) {
4474 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004475 weston_surface_destroy(shell->fade.view->surface);
4476 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004477 break;
4478 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004479 lock(shell);
4480 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004481 default:
4482 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004483 }
4484}
4485
Jason Ekstranda7af7042013-10-12 22:38:11 -05004486static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004487shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004488{
4489 struct weston_compositor *compositor = shell->compositor;
4490 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004491 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004492
4493 surface = weston_surface_create(compositor);
4494 if (!surface)
4495 return NULL;
4496
Jason Ekstranda7af7042013-10-12 22:38:11 -05004497 view = weston_view_create(surface);
4498 if (!view) {
4499 weston_surface_destroy(surface);
4500 return NULL;
4501 }
4502
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004503 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004504 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004505 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004506 wl_list_insert(&compositor->fade_layer.view_list,
4507 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004508 pixman_region32_init(&surface->input);
4509
Jason Ekstranda7af7042013-10-12 22:38:11 -05004510 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004511}
4512
4513static void
4514shell_fade(struct desktop_shell *shell, enum fade_type type)
4515{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004516 float tint;
4517
4518 switch (type) {
4519 case FADE_IN:
4520 tint = 0.0;
4521 break;
4522 case FADE_OUT:
4523 tint = 1.0;
4524 break;
4525 default:
4526 weston_log("shell: invalid fade type\n");
4527 return;
4528 }
4529
4530 shell->fade.type = type;
4531
Jason Ekstranda7af7042013-10-12 22:38:11 -05004532 if (shell->fade.view == NULL) {
4533 shell->fade.view = shell_fade_create_surface(shell);
4534 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004535 return;
4536
Jason Ekstranda7af7042013-10-12 22:38:11 -05004537 shell->fade.view->alpha = 1.0 - tint;
4538 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004539 }
4540
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004541 if (shell->fade.view->output == NULL) {
4542 /* If the black view gets a NULL output, we lost the
4543 * last output and we'll just cancel the fade. This
4544 * happens when you close the last window under the
4545 * X11 or Wayland backends. */
4546 shell->locked = false;
4547 weston_surface_destroy(shell->fade.view->surface);
4548 shell->fade.view = NULL;
4549 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004550 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004551 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004552 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004553 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004554 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004555 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004556 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004557}
4558
4559static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004560do_shell_fade_startup(void *data)
4561{
4562 struct desktop_shell *shell = data;
4563
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004564 if (shell->startup_animation_type == ANIMATION_FADE)
4565 shell_fade(shell, FADE_IN);
4566 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004567 weston_surface_destroy(shell->fade.view->surface);
4568 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004569 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004570}
4571
4572static void
4573shell_fade_startup(struct desktop_shell *shell)
4574{
4575 struct wl_event_loop *loop;
4576
4577 if (!shell->fade.startup_timer)
4578 return;
4579
4580 wl_event_source_remove(shell->fade.startup_timer);
4581 shell->fade.startup_timer = NULL;
4582
4583 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4584 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4585}
4586
4587static int
4588fade_startup_timeout(void *data)
4589{
4590 struct desktop_shell *shell = data;
4591
4592 shell_fade_startup(shell);
4593 return 0;
4594}
4595
4596static void
4597shell_fade_init(struct desktop_shell *shell)
4598{
4599 /* Make compositor output all black, and wait for the desktop-shell
4600 * client to signal it is ready, then fade in. The timer triggers a
4601 * fade-in, in case the desktop-shell client takes too long.
4602 */
4603
4604 struct wl_event_loop *loop;
4605
Jason Ekstranda7af7042013-10-12 22:38:11 -05004606 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004607 weston_log("%s: warning: fade surface already exists\n",
4608 __func__);
4609 return;
4610 }
4611
Jason Ekstranda7af7042013-10-12 22:38:11 -05004612 shell->fade.view = shell_fade_create_surface(shell);
4613 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004614 return;
4615
Jason Ekstranda7af7042013-10-12 22:38:11 -05004616 weston_view_update_transform(shell->fade.view);
4617 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004618
4619 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4620 shell->fade.startup_timer =
4621 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4622 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4623}
4624
4625static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004626idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004627{
4628 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004629 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004630 struct weston_seat *seat;
4631
4632 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4633 if (seat->pointer)
4634 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004635
4636 shell_fade(shell, FADE_OUT);
4637 /* lock() is called from shell_fade_done() */
4638}
4639
4640static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004641wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004642{
4643 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004644 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004645
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004646 unlock(shell);
4647}
4648
4649static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004650center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004651{
Giulio Camuffob8366642013-04-25 13:57:46 +03004652 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004653 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004654
Jason Ekstranda7af7042013-10-12 22:38:11 -05004655 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004656
4657 x = output->x + (output->width - width) / 2 - surf_x / 2;
4658 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004659
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004660 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004661}
4662
4663static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004664weston_view_set_initial_position(struct weston_view *view,
4665 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004666{
4667 struct weston_compositor *compositor = shell->compositor;
4668 int ix = 0, iy = 0;
4669 int range_x, range_y;
4670 int dx, dy, x, y, panel_height;
4671 struct weston_output *output, *target_output = NULL;
4672 struct weston_seat *seat;
4673
4674 /* As a heuristic place the new window on the same output as the
4675 * pointer. Falling back to the output containing 0, 0.
4676 *
4677 * TODO: Do something clever for touch too?
4678 */
4679 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004680 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004681 ix = wl_fixed_to_int(seat->pointer->x);
4682 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004683 break;
4684 }
4685 }
4686
4687 wl_list_for_each(output, &compositor->output_list, link) {
4688 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4689 target_output = output;
4690 break;
4691 }
4692 }
4693
4694 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004695 weston_view_set_position(view, 10 + random() % 400,
4696 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004697 return;
4698 }
4699
4700 /* Valid range within output where the surface will still be onscreen.
4701 * If this is negative it means that the surface is bigger than
4702 * output.
4703 */
4704 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004705 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004706 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004707 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004708
Rob Bradford4cb88c72012-08-13 15:18:44 +01004709 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004710 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004711 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004712 dx = 0;
4713
4714 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004715 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004716 else
4717 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004718
4719 x = target_output->x + dx;
4720 y = target_output->y + dy;
4721
Jason Ekstranda7af7042013-10-12 22:38:11 -05004722 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004723}
4724
4725static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004726map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004727 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004728{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004729 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004730 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004731 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004732 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004733
Pekka Paalanen77346a62011-11-30 16:26:35 +02004734 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004735 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004736 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004737 if (shsurf->state.fullscreen) {
4738 center_on_output(shsurf->view, shsurf->fullscreen_output);
4739 shell_map_fullscreen(shsurf);
4740 } else if (shsurf->state.maximized) {
4741 /* use surface configure to set the geometry */
4742 panel_height = get_output_panel_height(shell, shsurf->output);
4743 surface_subsurfaces_boundingbox(shsurf->surface,
4744 &surf_x, &surf_y, NULL, NULL);
4745 weston_view_set_position(shsurf->view,
4746 shsurf->output->x - surf_x,
4747 shsurf->output->y +
4748 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004749 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004750 weston_view_set_initial_position(shsurf->view, shell);
4751 }
Juan Zhao96879df2012-02-07 08:45:41 +08004752 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004753 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004754 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004755 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004756 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004757 weston_view_set_position(shsurf->view,
4758 shsurf->view->geometry.x + sx,
4759 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004760 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004761 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004762 default:
4763 ;
4764 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004765
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004766 /* Surface stacking order, see also activate(). */
4767 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004768
Jason Ekstranda7af7042013-10-12 22:38:11 -05004769 if (shsurf->type != SHELL_SURFACE_NONE) {
4770 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004771 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004772 shsurf->surface->output = shsurf->output;
4773 shsurf->view->output = shsurf->output;
4774 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004775 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004776
Jason Ekstranda7af7042013-10-12 22:38:11 -05004777 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004778 /* XXX: xwayland's using the same fields for transient type */
4779 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004780 if (shsurf->transient.flags ==
4781 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4782 break;
4783 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004784 if (shsurf->state.relative &&
4785 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4786 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004787 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004788 break;
4789 wl_list_for_each(seat, &compositor->seat_list, link)
4790 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004791 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004792 case SHELL_SURFACE_POPUP:
4793 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004794 default:
4795 break;
4796 }
4797
Rafael Antognolli03b16592013-12-03 15:35:42 -02004798 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4799 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004800 {
4801 switch (shell->win_animation_type) {
4802 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004803 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004804 break;
4805 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004806 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004807 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004808 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004809 default:
4810 break;
4811 }
4812 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004813}
4814
4815static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004816configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004817 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004818{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004819 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004820 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004821 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004822
Pekka Paalanen77346a62011-11-30 16:26:35 +02004823 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004824
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004825 assert(shsurf);
4826
Rafael Antognolli03b16592013-12-03 15:35:42 -02004827 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004828 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004829 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004830 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004831 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4832 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004833 mx = shsurf->output->x - surf_x;
4834 my = shsurf->output->y +
4835 get_output_panel_height(shell,shsurf->output) - surf_y;
4836 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004837 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004838 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004839 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004840
Alex Wu4539b082012-03-01 12:57:46 +08004841 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004842 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004843 wl_list_for_each(view, &surface->views, surface_link)
4844 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004845
Rafael Antognolli03b16592013-12-03 15:35:42 -02004846 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004847 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004848 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004849}
4850
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004851static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004852shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004853{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004854 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004855 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004856 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004857
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004858 assert(shsurf);
4859
4860 shell = shsurf->shell;
4861
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004862 if (!weston_surface_is_mapped(es) &&
4863 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004864 remove_popup_grab(shsurf);
4865 }
4866
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004867 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004868 return;
4869
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004870 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004871 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004872 type_changed = 1;
4873 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004874
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004875 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004876 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004877 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004878 shsurf->last_width != es->width ||
4879 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004880 float from_x, from_y;
4881 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004882
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08004883 if (shsurf->resize_edges) {
4884 sx = 0;
4885 sy = 0;
4886 }
4887
4888 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
4889 sx = shsurf->last_width - es->width;
4890 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
4891 sy = shsurf->last_height - es->height;
4892
4893 shsurf->last_width = es->width;
4894 shsurf->last_height = es->height;
4895
Jason Ekstranda7af7042013-10-12 22:38:11 -05004896 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4897 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004898 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004899 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004900 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004901 }
4902}
4903
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004904static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004905
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004906static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004907desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004908{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004909 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004910 struct desktop_shell *shell =
4911 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004912
4913 shell->child.process.pid = 0;
4914 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004915
4916 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4917 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004918 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004919 shell->child.deathstamp = time;
4920 shell->child.deathcount = 0;
4921 }
4922
4923 shell->child.deathcount++;
4924 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004925 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004926 return;
4927 }
4928
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004929 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004930 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004931 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004932}
4933
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004934static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004935desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4936{
4937 struct desktop_shell *shell;
4938
4939 shell = container_of(listener, struct desktop_shell,
4940 child.client_destroy_listener);
4941
4942 shell->child.client = NULL;
4943}
4944
4945static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004946launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004947{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004948 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004949
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004950 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004951 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004952 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004953 desktop_shell_sigchld);
4954
4955 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004956 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004957
4958 shell->child.client_destroy_listener.notify =
4959 desktop_shell_client_destroy;
4960 wl_client_add_destroy_listener(shell->child.client,
4961 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004962}
4963
4964static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004965handle_shell_client_destroy(struct wl_listener *listener, void *data)
4966{
4967 struct shell_client *sc =
4968 container_of(listener, struct shell_client, destroy_listener);
4969
4970 if (sc->ping_timer)
4971 wl_event_source_remove(sc->ping_timer);
4972 free(sc);
4973}
4974
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004975static struct shell_client *
4976shell_client_create(struct wl_client *client, struct desktop_shell *shell,
4977 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004978{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004979 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004980
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004981 sc = zalloc(sizeof *sc);
4982 if (sc == NULL) {
4983 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004984 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004985 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004986
4987 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004988 if (sc->resource == NULL) {
4989 free(sc);
4990 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004991 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004992 }
4993
4994 sc->client = client;
4995 sc->shell = shell;
4996 sc->destroy_listener.notify = handle_shell_client_destroy;
4997 wl_client_add_destroy_listener(client, &sc->destroy_listener);
4998
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004999 return sc;
5000}
5001
5002static void
5003bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5004{
5005 struct desktop_shell *shell = data;
5006 struct shell_client *sc;
5007
5008 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5009 if (sc)
5010 wl_resource_set_implementation(sc->resource,
5011 &shell_implementation,
5012 shell, NULL);
5013}
5014
5015static void
5016bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5017{
5018 struct desktop_shell *shell = data;
5019 struct shell_client *sc;
5020
5021 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5022 if (sc)
5023 wl_resource_set_dispatcher(sc->resource,
5024 xdg_shell_unversioned_dispatch,
5025 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005026}
5027
5028static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005029unbind_desktop_shell(struct wl_resource *resource)
5030{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005031 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005032
5033 if (shell->locked)
5034 resume_desktop(shell);
5035
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005036 shell->child.desktop_shell = NULL;
5037 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005038}
5039
5040static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005041bind_desktop_shell(struct wl_client *client,
5042 void *data, uint32_t version, uint32_t id)
5043{
Tiago Vignattibe143262012-04-16 17:31:41 +03005044 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005045 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005046
Jason Ekstranda85118c2013-06-27 20:17:02 -05005047 resource = wl_resource_create(client, &desktop_shell_interface,
5048 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005049
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005050 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005051 wl_resource_set_implementation(resource,
5052 &desktop_shell_implementation,
5053 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005054 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005055
5056 if (version < 2)
5057 shell_fade_startup(shell);
5058
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005059 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005060 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005061
5062 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5063 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005064 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005065}
5066
Pekka Paalanen6e168112011-11-24 11:34:05 +02005067static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005068screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005069{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005070 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005071 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005072
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005073 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005074 return;
5075
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005076 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005077 if (!shell->locked)
5078 return;
5079
Jason Ekstranda7af7042013-10-12 22:38:11 -05005080 view = container_of(surface->views.next, struct weston_view, surface_link);
5081 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005082
Jason Ekstranda7af7042013-10-12 22:38:11 -05005083 if (wl_list_empty(&view->layer_link)) {
5084 wl_list_insert(shell->lock_layer.view_list.prev,
5085 &view->layer_link);
5086 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005087 wl_event_source_timer_update(shell->screensaver.timer,
5088 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005089 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005090 }
5091}
5092
5093static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005094screensaver_set_surface(struct wl_client *client,
5095 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005096 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005097 struct wl_resource *output_resource)
5098{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005099 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005100 struct weston_surface *surface =
5101 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005102 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005103 struct weston_view *view, *next;
5104
5105 /* Make sure we only have one view */
5106 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5107 weston_view_destroy(view);
5108 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005109
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005110 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005111 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005112 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005113}
5114
5115static const struct screensaver_interface screensaver_implementation = {
5116 screensaver_set_surface
5117};
5118
5119static void
5120unbind_screensaver(struct wl_resource *resource)
5121{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005122 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005123
Pekka Paalanen77346a62011-11-30 16:26:35 +02005124 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005125}
5126
5127static void
5128bind_screensaver(struct wl_client *client,
5129 void *data, uint32_t version, uint32_t id)
5130{
Tiago Vignattibe143262012-04-16 17:31:41 +03005131 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005132 struct wl_resource *resource;
5133
Jason Ekstranda85118c2013-06-27 20:17:02 -05005134 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005135
Pekka Paalanen77346a62011-11-30 16:26:35 +02005136 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005137 wl_resource_set_implementation(resource,
5138 &screensaver_implementation,
5139 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005140 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005141 return;
5142 }
5143
5144 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5145 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005146 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005147}
5148
Kristian Høgsberg07045392012-02-19 18:52:44 -05005149struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005150 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005151 struct weston_surface *current;
5152 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005153 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005154};
5155
5156static void
5157switcher_next(struct switcher *switcher)
5158{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005159 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005160 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005161 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005162 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005163
Jason Ekstranda7af7042013-10-12 22:38:11 -05005164 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005165 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005166 if (shsurf &&
5167 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5168 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005169 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005170 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005171 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005172 next = view->surface;
5173 prev = view->surface;
5174 view->alpha = 0.25;
5175 weston_view_geometry_dirty(view);
5176 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005177 }
Alex Wu1659daa2012-04-01 20:13:09 +08005178
Jason Ekstranda7af7042013-10-12 22:38:11 -05005179 if (is_black_surface(view->surface, NULL)) {
5180 view->alpha = 0.25;
5181 weston_view_geometry_dirty(view);
5182 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005183 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005184 }
5185
5186 if (next == NULL)
5187 next = first;
5188
Alex Wu07b26062012-03-12 16:06:01 +08005189 if (next == NULL)
5190 return;
5191
Kristian Høgsberg07045392012-02-19 18:52:44 -05005192 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005193 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005194
5195 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005196 wl_list_for_each(view, &next->views, surface_link)
5197 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005198
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005199 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005200 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005201 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005202}
5203
5204static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005205switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005206{
5207 struct switcher *switcher =
5208 container_of(listener, struct switcher, listener);
5209
5210 switcher_next(switcher);
5211}
5212
5213static void
Daniel Stone351eb612012-05-31 15:27:47 -04005214switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005215{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005216 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005217 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005218 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005219
Jason Ekstranda7af7042013-10-12 22:38:11 -05005220 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005221 if (is_focus_view(view))
5222 continue;
5223
Jason Ekstranda7af7042013-10-12 22:38:11 -05005224 view->alpha = 1.0;
5225 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005226 }
5227
Alex Wu07b26062012-03-12 16:06:01 +08005228 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005229 activate(switcher->shell, switcher->current,
5230 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005231 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005232 weston_keyboard_end_grab(keyboard);
5233 if (keyboard->input_method_resource)
5234 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005235 free(switcher);
5236}
5237
5238static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005239switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005240 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005241{
5242 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005243 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005244
Daniel Stonec9785ea2012-05-30 16:31:52 +01005245 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005246 switcher_next(switcher);
5247}
5248
5249static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005250switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005251 uint32_t mods_depressed, uint32_t mods_latched,
5252 uint32_t mods_locked, uint32_t group)
5253{
5254 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005255 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005256
Daniel Stone351eb612012-05-31 15:27:47 -04005257 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5258 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005259}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005260
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005261static void
5262switcher_cancel(struct weston_keyboard_grab *grab)
5263{
5264 struct switcher *switcher = container_of(grab, struct switcher, grab);
5265
5266 switcher_destroy(switcher);
5267}
5268
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005269static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005270 switcher_key,
5271 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005272 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005273};
5274
5275static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005276switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005277 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005278{
Tiago Vignattibe143262012-04-16 17:31:41 +03005279 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005280 struct switcher *switcher;
5281
5282 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005283 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005284 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005285 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005286 wl_list_init(&switcher->listener.link);
5287
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005288 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005289 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005290 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005291 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5292 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005293 switcher_next(switcher);
5294}
5295
Pekka Paalanen3c647232011-12-22 13:43:43 +02005296static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005297backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005298 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005299{
5300 struct weston_compositor *compositor = data;
5301 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005302 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005303
5304 /* TODO: we're limiting to simple use cases, where we assume just
5305 * control on the primary display. We'd have to extend later if we
5306 * ever get support for setting backlights on random desktop LCD
5307 * panels though */
5308 output = get_default_output(compositor);
5309 if (!output)
5310 return;
5311
5312 if (!output->set_backlight)
5313 return;
5314
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005315 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5316 backlight_new = output->backlight_current - 25;
5317 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5318 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005319
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005320 if (backlight_new < 5)
5321 backlight_new = 5;
5322 if (backlight_new > 255)
5323 backlight_new = 255;
5324
5325 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005326 output->set_backlight(output, output->backlight_current);
5327}
5328
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005329struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005330 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005331 struct weston_seat *seat;
5332 uint32_t key[2];
5333 int key_released[2];
5334};
5335
5336static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005337debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005338 uint32_t key, uint32_t state)
5339{
5340 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005341 struct weston_compositor *ec = db->seat->compositor;
5342 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005343 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005344 uint32_t serial;
5345 int send = 0, terminate = 0;
5346 int check_binding = 1;
5347 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005348 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005349
5350 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5351 /* Do not run bindings on key releases */
5352 check_binding = 0;
5353
5354 for (i = 0; i < 2; i++)
5355 if (key == db->key[i])
5356 db->key_released[i] = 1;
5357
5358 if (db->key_released[0] && db->key_released[1]) {
5359 /* All key releases been swalled so end the grab */
5360 terminate = 1;
5361 } else if (key != db->key[0] && key != db->key[1]) {
5362 /* Should not swallow release of other keys */
5363 send = 1;
5364 }
5365 } else if (key == db->key[0] && !db->key_released[0]) {
5366 /* Do not check bindings for the first press of the binding
5367 * key. This allows it to be used as a debug shortcut.
5368 * We still need to swallow this event. */
5369 check_binding = 0;
5370 } else if (db->key[1]) {
5371 /* If we already ran a binding don't process another one since
5372 * we can't keep track of all the binding keys that were
5373 * pressed in order to swallow the release events. */
5374 send = 1;
5375 check_binding = 0;
5376 }
5377
5378 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005379 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5380 key, state)) {
5381 /* We ran a binding so swallow the press and keep the
5382 * grab to swallow the released too. */
5383 send = 0;
5384 terminate = 0;
5385 db->key[1] = key;
5386 } else {
5387 /* Terminate the grab since the key pressed is not a
5388 * debug binding key. */
5389 send = 1;
5390 terminate = 1;
5391 }
5392 }
5393
5394 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005395 serial = wl_display_next_serial(display);
5396 resource_list = &grab->keyboard->focus_resource_list;
5397 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005398 wl_keyboard_send_key(resource, serial, time, key, state);
5399 }
5400 }
5401
5402 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005403 weston_keyboard_end_grab(grab->keyboard);
5404 if (grab->keyboard->input_method_resource)
5405 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005406 free(db);
5407 }
5408}
5409
5410static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005411debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005412 uint32_t mods_depressed, uint32_t mods_latched,
5413 uint32_t mods_locked, uint32_t group)
5414{
5415 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005416 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005417
Neil Roberts96d790e2013-09-19 17:32:00 +01005418 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005419
Neil Roberts96d790e2013-09-19 17:32:00 +01005420 wl_resource_for_each(resource, resource_list) {
5421 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5422 mods_latched, mods_locked, group);
5423 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005424}
5425
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005426static void
5427debug_binding_cancel(struct weston_keyboard_grab *grab)
5428{
5429 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5430
5431 weston_keyboard_end_grab(grab->keyboard);
5432 free(db);
5433}
5434
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005435struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005436 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005437 debug_binding_modifiers,
5438 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005439};
5440
5441static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005442debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005443{
5444 struct debug_binding_grab *grab;
5445
5446 grab = calloc(1, sizeof *grab);
5447 if (!grab)
5448 return;
5449
5450 grab->seat = (struct weston_seat *) seat;
5451 grab->key[0] = key;
5452 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005453 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005454}
5455
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005456static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005457force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005458 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005459{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005460 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005461 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005462 struct desktop_shell *shell = data;
5463 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005464 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005465
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005466 focus_surface = seat->keyboard->focus;
5467 if (!focus_surface)
5468 return;
5469
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005470 wl_signal_emit(&compositor->kill_signal, focus_surface);
5471
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005472 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005473 wl_client_get_credentials(client, &pid, NULL, NULL);
5474
5475 /* Skip clients that we launched ourselves (the credentials of
5476 * the socketpair is ours) */
5477 if (pid == getpid())
5478 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005479
Daniel Stone325fc2d2012-05-30 16:31:58 +01005480 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005481}
5482
5483static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005484workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005485 uint32_t key, void *data)
5486{
5487 struct desktop_shell *shell = data;
5488 unsigned int new_index = shell->workspaces.current;
5489
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005490 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005491 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005492 if (new_index != 0)
5493 new_index--;
5494
5495 change_workspace(shell, new_index);
5496}
5497
5498static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005499workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005500 uint32_t key, void *data)
5501{
5502 struct desktop_shell *shell = data;
5503 unsigned int new_index = shell->workspaces.current;
5504
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005505 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005506 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005507 if (new_index < shell->workspaces.num - 1)
5508 new_index++;
5509
5510 change_workspace(shell, new_index);
5511}
5512
5513static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005514workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005515 uint32_t key, void *data)
5516{
5517 struct desktop_shell *shell = data;
5518 unsigned int new_index;
5519
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005520 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005521 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005522 new_index = key - KEY_F1;
5523 if (new_index >= shell->workspaces.num)
5524 new_index = shell->workspaces.num - 1;
5525
5526 change_workspace(shell, new_index);
5527}
5528
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005529static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005530workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005531 uint32_t key, void *data)
5532{
5533 struct desktop_shell *shell = data;
5534 unsigned int new_index = shell->workspaces.current;
5535
5536 if (shell->locked)
5537 return;
5538
5539 if (new_index != 0)
5540 new_index--;
5541
5542 take_surface_to_workspace_by_seat(shell, seat, new_index);
5543}
5544
5545static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005546workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005547 uint32_t key, void *data)
5548{
5549 struct desktop_shell *shell = data;
5550 unsigned int new_index = shell->workspaces.current;
5551
5552 if (shell->locked)
5553 return;
5554
5555 if (new_index < shell->workspaces.num - 1)
5556 new_index++;
5557
5558 take_surface_to_workspace_by_seat(shell, seat, new_index);
5559}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005560
5561static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005562shell_reposition_view_on_output_destroy(struct weston_view *view)
5563{
5564 struct weston_output *output, *first_output;
5565 struct weston_compositor *ec = view->surface->compositor;
5566 struct shell_surface *shsurf;
5567 float x, y;
5568 int visible;
5569
5570 x = view->geometry.x;
5571 y = view->geometry.y;
5572
5573 /* At this point the destroyed output is not in the list anymore.
5574 * If the view is still visible somewhere, we leave where it is,
5575 * otherwise, move it to the first output. */
5576 visible = 0;
5577 wl_list_for_each(output, &ec->output_list, link) {
5578 if (pixman_region32_contains_point(&output->region,
5579 x, y, NULL)) {
5580 visible = 1;
5581 break;
5582 }
5583 }
5584
5585 if (!visible) {
5586 first_output = container_of(ec->output_list.next,
5587 struct weston_output, link);
5588
5589 x = first_output->x + first_output->width / 4;
5590 y = first_output->y + first_output->height / 4;
5591 }
5592
5593 weston_view_set_position(view, x, y);
5594
5595 shsurf = get_shell_surface(view->surface);
5596
5597 if (shsurf) {
5598 shsurf->saved_position_valid = false;
5599 shsurf->next_state.maximized = false;
5600 shsurf->next_state.fullscreen = false;
5601 shsurf->state_changed = true;
5602 }
5603}
5604
5605static void
5606shell_reposition_views_on_output_destroy(struct shell_output *shell_output)
5607{
5608 struct desktop_shell *shell = shell_output->shell;
5609 struct weston_output *output = shell_output->output;
5610 struct weston_layer *layer;
5611 struct weston_view *view;
5612
5613 /* Move all views in the layers owned by the shell */
5614 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5615 wl_list_for_each(view, &layer->view_list, layer_link) {
5616 if (view->output != output)
5617 continue;
5618
5619 shell_reposition_view_on_output_destroy(view);
5620 }
5621
5622 /* We don't start from the beggining of the layer list, so
5623 * make sure we don't wrap around it. */
5624 if (layer == &shell->background_layer)
5625 break;
5626 }
5627}
5628
5629static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005630handle_output_destroy(struct wl_listener *listener, void *data)
5631{
5632 struct shell_output *output_listener =
5633 container_of(listener, struct shell_output, destroy_listener);
5634
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005635 shell_reposition_views_on_output_destroy(output_listener);
5636
Xiong Zhang6b481422013-10-23 13:58:32 +08005637 wl_list_remove(&output_listener->destroy_listener.link);
5638 wl_list_remove(&output_listener->link);
5639 free(output_listener);
5640}
5641
5642static void
5643create_shell_output(struct desktop_shell *shell,
5644 struct weston_output *output)
5645{
5646 struct shell_output *shell_output;
5647
5648 shell_output = zalloc(sizeof *shell_output);
5649 if (shell_output == NULL)
5650 return;
5651
5652 shell_output->output = output;
5653 shell_output->shell = shell;
5654 shell_output->destroy_listener.notify = handle_output_destroy;
5655 wl_signal_add(&output->destroy_signal,
5656 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005657 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005658}
5659
5660static void
5661handle_output_create(struct wl_listener *listener, void *data)
5662{
5663 struct desktop_shell *shell =
5664 container_of(listener, struct desktop_shell, output_create_listener);
5665 struct weston_output *output = (struct weston_output *)data;
5666
5667 create_shell_output(shell, output);
5668}
5669
5670static void
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005671handle_output_move(struct wl_listener *listener, void *data)
5672{
5673 struct desktop_shell *shell;
5674 struct weston_output *output;
5675 struct weston_layer *layer;
5676 struct weston_view *view;
5677 float x, y;
5678
5679 shell = container_of(listener, struct desktop_shell,
5680 output_move_listener);
5681 output = data;
5682
5683 /* Move all views in the layers owned by the shell */
5684 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5685 wl_list_for_each(view, &layer->view_list, layer_link) {
5686 if (view->output != output)
5687 continue;
5688
5689 x = view->geometry.x + output->move_x;
5690 y = view->geometry.y + output->move_y;
5691 weston_view_set_position(view, x, y);
5692 }
5693
5694 /* We don't start from the beggining of the layer list, so
5695 * make sure we don't wrap around it. */
5696 if (layer == &shell->background_layer)
5697 break;
5698 }
5699}
5700
5701static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005702setup_output_destroy_handler(struct weston_compositor *ec,
5703 struct desktop_shell *shell)
5704{
5705 struct weston_output *output;
5706
5707 wl_list_init(&shell->output_list);
5708 wl_list_for_each(output, &ec->output_list, link)
5709 create_shell_output(shell, output);
5710
5711 shell->output_create_listener.notify = handle_output_create;
5712 wl_signal_add(&ec->output_created_signal,
5713 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005714
5715 shell->output_move_listener.notify = handle_output_move;
5716 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005717}
5718
5719static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005720shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005721{
Tiago Vignattibe143262012-04-16 17:31:41 +03005722 struct desktop_shell *shell =
5723 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005724 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005725 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005726
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005727 /* Force state to unlocked so we don't try to fade */
5728 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005729 if (shell->child.client)
5730 wl_client_destroy(shell->child.client);
5731
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005732 wl_list_remove(&shell->idle_listener.link);
5733 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005734
5735 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005736
Xiong Zhang6b481422013-10-23 13:58:32 +08005737 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5738 wl_list_remove(&shell_output->destroy_listener.link);
5739 wl_list_remove(&shell_output->link);
5740 free(shell_output);
5741 }
5742
5743 wl_list_remove(&shell->output_create_listener.link);
5744
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005745 wl_array_for_each(ws, &shell->workspaces.array)
5746 workspace_destroy(*ws);
5747 wl_array_release(&shell->workspaces.array);
5748
Pekka Paalanen3c647232011-12-22 13:43:43 +02005749 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005750 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005751 free(shell);
5752}
5753
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005754static void
5755shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5756{
5757 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005758 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005759
5760 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005761 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5762 MODIFIER_CTRL | MODIFIER_ALT,
5763 terminate_binding, ec);
5764 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5765 click_to_activate_binding,
5766 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005767 weston_compositor_add_touch_binding(ec, 0,
5768 touch_to_activate_binding,
5769 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005770 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5771 MODIFIER_SUPER | MODIFIER_ALT,
5772 surface_opacity_binding, NULL);
5773 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5774 MODIFIER_SUPER, zoom_axis_binding,
5775 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005776
5777 /* configurable bindings */
5778 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005779 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5780 zoom_key_binding, NULL);
5781 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5782 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005783 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5784 maximize_binding, NULL);
5785 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5786 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005787 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5788 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005789 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005790 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5791 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005792 weston_compositor_add_button_binding(ec, BTN_LEFT,
5793 mod | MODIFIER_SHIFT,
5794 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005795
5796 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5797 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5798 rotate_binding, NULL);
5799
Daniel Stone325fc2d2012-05-30 16:31:58 +01005800 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5801 shell);
5802 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5803 ec);
5804 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5805 backlight_binding, ec);
5806 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5807 ec);
5808 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5809 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005810 weston_compositor_add_key_binding(ec, KEY_K, mod,
5811 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005812 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5813 workspace_up_binding, shell);
5814 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5815 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005816 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5817 workspace_move_surface_up_binding,
5818 shell);
5819 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5820 workspace_move_surface_down_binding,
5821 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005822
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005823 if (shell->exposay_modifier)
5824 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5825 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005826
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005827 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5828 if (shell->workspaces.num > 1) {
5829 num_workspace_bindings = shell->workspaces.num;
5830 if (num_workspace_bindings > 6)
5831 num_workspace_bindings = 6;
5832 for (i = 0; i < num_workspace_bindings; i++)
5833 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5834 workspace_f_binding,
5835 shell);
5836 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005837
5838 /* Debug bindings */
5839 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5840 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005841}
5842
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005843WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005844module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005845 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005846{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005847 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005848 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005849 struct workspace **pws;
5850 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005851 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005852
Peter Huttererf3d62272013-08-08 11:57:05 +10005853 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005854 if (shell == NULL)
5855 return -1;
5856
Kristian Høgsberg75840622011-09-06 13:48:16 -04005857 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005858
5859 shell->destroy_listener.notify = shell_destroy;
5860 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005861 shell->idle_listener.notify = idle_handler;
5862 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5863 shell->wake_listener.notify = wake_handler;
5864 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005865
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005866 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005867 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005868 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005869 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005870 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005871 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005872 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005873 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005874 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005875 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005876
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005877 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5878 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005879 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5880 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005881 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005882
5883 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005884 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005885
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005886 if (input_panel_setup(shell) < 0)
5887 return -1;
5888
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005889 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005890
Daniel Stonedf8133b2013-11-19 11:37:14 +01005891 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5892 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5893
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005894 for (i = 0; i < shell->workspaces.num; i++) {
5895 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5896 if (pws == NULL)
5897 return -1;
5898
5899 *pws = workspace_create();
5900 if (*pws == NULL)
5901 return -1;
5902 }
5903 activate_workspace(shell, 0);
5904
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005905 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005906 wl_list_init(&shell->workspaces.animation.link);
5907 shell->workspaces.animation.frame = animate_workspace_change_frame;
5908
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005909 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005910 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005911 return -1;
5912
Rafael Antognollie2a34552013-12-03 15:35:45 -02005913 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5914 shell, bind_xdg_shell) == NULL)
5915 return -1;
5916
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005917 if (wl_global_create(ec->wl_display,
5918 &desktop_shell_interface, 2,
5919 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005920 return -1;
5921
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005922 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5923 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005924 return -1;
5925
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005926 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5927 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005928 return -1;
5929
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005930 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005931
Xiong Zhang6b481422013-10-23 13:58:32 +08005932 setup_output_destroy_handler(ec, shell);
5933
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005934 loop = wl_display_get_event_loop(ec->wl_display);
5935 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005936
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005937 shell->screensaver.timer =
5938 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5939
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005940 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005941 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005942 create_keyboard_focus_listener(seat);
5943 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005944
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005945 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005946
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005947 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005948
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005949 return 0;
5950}