blob: e3abaadc794bd622e41912b20607392f302ccb56 [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
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050066struct shell_client;
67
Philip Withnall648a4dd2013-11-25 18:01:44 +000068/*
69 * Surface stacking and ordering.
70 *
71 * This is handled using several linked lists of surfaces, organised into
72 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
73 * above all of the surfaces in the layer below. The set of layers is static and
74 * in the following order (top-most first):
75 * • Lock layer (only ever displayed on its own)
76 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010077 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000078 * • Fullscreen layer
79 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Workspace layers
81 * • Background layer
82 *
83 * The list of layers may be manipulated to remove whole layers of surfaces from
84 * display. For example, when locking the screen, all layers except the lock
85 * layer are removed.
86 *
87 * A surface’s layer is modified on configuring the surface, in
88 * set_surface_type() (which is only called when the surface’s type change is
89 * _committed_). If a surface’s type changes (e.g. when making a window
90 * fullscreen) its layer changes too.
91 *
92 * In order to allow popup and transient surfaces to be correctly stacked above
93 * their parent surfaces, each surface tracks both its parent surface, and a
94 * linked list of its children. When a surface’s layer is updated, so are the
95 * layers of its children. Note that child surfaces are *not* the same as
96 * subsurfaces — child/parent surfaces are purely for maintaining stacking
97 * order.
98 *
99 * The children_link list of siblings of a surface (i.e. those surfaces which
100 * have the same parent) only contains weston_surfaces which have a
101 * shell_surface. Stacking is not implemented for non-shell_surface
102 * weston_surfaces. This means that the following implication does *not* hold:
103 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
104 */
105
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200106struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500107 struct wl_resource *resource;
108 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500109 struct shell_client *owner;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200110
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500111 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500112 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600113 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700115 struct wl_listener resource_destroy_listener;
116
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400117 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000118 struct wl_list children_list; /* child surfaces of this one */
119 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300120 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200121
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800122 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400123 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500124 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200125 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800126 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200127 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800128 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700129 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800130 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100131
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200133 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500134 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200135 } rotation;
136
137 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100138 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100140 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400141 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 } popup;
143
Alex Wu4539b082012-03-01 12:57:46 +0800144 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300145 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400146 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 } transient;
148
149 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800150 enum wl_shell_surface_fullscreen_method type;
151 struct weston_transform transform; /* matrix from x, y */
152 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800154 } fullscreen;
155
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200156 struct weston_transform workspace_transform;
157
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500158 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400161
162 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200163
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700164 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200165 bool maximized;
166 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200167 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100168 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500169 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200170 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500171 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500172
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700173 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400174 int32_t x, y, width, height;
175 } geometry, next_geometry;
176 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700177
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500178 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179};
180
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300181struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400182 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300183 struct shell_surface *shsurf;
184 struct wl_listener shsurf_destroy_listener;
185};
186
Rusty Lynch1084da52013-08-15 09:10:08 -0700187struct shell_touch_grab {
188 struct weston_touch_grab grab;
189 struct shell_surface *shsurf;
190 struct wl_listener shsurf_destroy_listener;
191 struct weston_touch *touch;
192};
193
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300194struct weston_move_grab {
195 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100196 wl_fixed_t dx, dy;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700197 int client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500198};
199
Rusty Lynch1084da52013-08-15 09:10:08 -0700200struct weston_touch_move_grab {
201 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800202 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700203 wl_fixed_t dx, dy;
204};
205
Pekka Paalanen460099f2012-01-20 16:48:25 +0200206struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300207 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500208 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200209 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200210 float x;
211 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200212 } center;
213};
214
Giulio Camuffo5085a752013-03-25 21:42:45 +0100215struct shell_seat {
216 struct weston_seat *seat;
217 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500218 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100219
Jason Ekstrand024177c2014-04-21 19:42:58 -0500220 struct wl_listener caps_changed_listener;
221 struct wl_listener pointer_focus_listener;
222 struct wl_listener keyboard_focus_listener;
223
Giulio Camuffo5085a752013-03-25 21:42:45 +0100224 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400225 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100226 struct wl_list surfaces_list;
227 struct wl_client *client;
228 int32_t initial_up;
229 } popup_grab;
230};
231
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800232struct shell_client {
233 struct wl_resource *resource;
234 struct wl_client *client;
235 struct desktop_shell *shell;
236 struct wl_listener destroy_listener;
237 struct wl_event_source *ping_timer;
238 uint32_t ping_serial;
239 int unresponsive;
240};
241
Alex Wubd3354b2012-04-17 17:20:49 +0800242static struct desktop_shell *
243shell_surface_get_shell(struct shell_surface *shsurf);
244
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500245static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400246surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500247
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300248static void
249shell_fade_startup(struct desktop_shell *shell);
250
Philip Withnallbecb77e2013-11-25 18:01:30 +0000251static struct shell_seat *
252get_shell_seat(struct weston_seat *seat);
253
Jonny Lambd73c6942014-08-20 15:53:20 +0200254static void
255get_output_panel_size(struct desktop_shell *shell,
256 struct weston_output *output,
257 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700258
Philip Withnall648a4dd2013-11-25 18:01:44 +0000259static void
260shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
261
Alex Wubd3354b2012-04-17 17:20:49 +0800262static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200263shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
264
265static bool
266shell_surface_is_xdg_surface(struct shell_surface *shsurf);
267
268static bool
269shell_surface_is_xdg_popup(struct shell_surface *shsurf);
270
271static void
272shell_surface_set_parent(struct shell_surface *shsurf,
273 struct weston_surface *parent);
274
275static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800276shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
277{
278 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800280
281 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100282
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300283 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800284 return false;
285
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300286 top_fs_ev = container_of(shell->fullscreen_layer.view_list.link.next,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500287 struct weston_view,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300288 layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800290}
291
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500292static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400293destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300294{
295 struct shell_grab *grab;
296
297 grab = container_of(listener, struct shell_grab,
298 shsurf_destroy_listener);
299
300 grab->shsurf = NULL;
301}
302
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800303struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304get_default_view(struct weston_surface *surface)
305{
306 struct shell_surface *shsurf;
307 struct weston_view *view;
308
309 if (!surface || wl_list_empty(&surface->views))
310 return NULL;
311
312 shsurf = get_shell_surface(surface);
313 if (shsurf)
314 return shsurf->view;
315
316 wl_list_for_each(view, &surface->views, surface_link)
317 if (weston_view_is_mapped(view))
318 return view;
319
320 return container_of(surface->views.next, struct weston_view, surface_link);
321}
322
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300323static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400324popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400325
326static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300327shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400328 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300329 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400330 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300331 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300333 struct desktop_shell *shell = shsurf->shell;
334
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400335 popup_grab_end(pointer);
336
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300337 grab->grab.interface = interface;
338 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400339 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500340 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400341 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300342
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700343 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400344 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400345 if (shell->child.desktop_shell) {
346 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
347 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500348 weston_pointer_set_focus(pointer,
349 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400350 wl_fixed_from_int(0),
351 wl_fixed_from_int(0));
352 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300353}
354
Jonny Lambd73c6942014-08-20 15:53:20 +0200355static void
356get_output_panel_size(struct desktop_shell *shell,
357 struct weston_output *output,
358 int *width,
359 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700360{
361 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200362
363 *width = 0;
364 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700365
366 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200367 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700368
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300369 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200370 float x, y;
371
372 if (view->surface->output != output)
373 continue;
374
375 switch (shell->panel_position) {
376 case DESKTOP_SHELL_PANEL_POSITION_TOP:
377 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
378
379 weston_view_to_global_float(view,
380 view->surface->width, 0,
381 &x, &y);
382
383 *width = (int) x;
384 *height = view->surface->height + (int) y;
385 return;
386
387 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
388 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
389 weston_view_to_global_float(view,
390 0, view->surface->height,
391 &x, &y);
392
393 *width = view->surface->width + (int) x;
394 *height = (int) y;
395 return;
396
397 default:
398 /* we've already set width and height to
399 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700400 break;
401 }
402 }
403
Jonny Lambd73c6942014-08-20 15:53:20 +0200404 /* the correct view wasn't found */
405}
406
407static void
408get_output_work_area(struct desktop_shell *shell,
409 struct weston_output *output,
410 pixman_rectangle32_t *area)
411{
412 int32_t panel_width = 0, panel_height = 0;
413
414 area->x = 0;
415 area->y = 0;
416
417 get_output_panel_size(shell, output, &panel_width, &panel_height);
418
419 switch (shell->panel_position) {
420 case DESKTOP_SHELL_PANEL_POSITION_TOP:
421 default:
422 area->y = panel_height;
423 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
424 area->width = output->width;
425 area->height = output->height - panel_height;
426 break;
427 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
428 area->x = panel_width;
429 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
430 area->width = output->width - panel_width;
431 area->height = output->height;
432 break;
433 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700434}
435
436static void
437send_configure_for_surface(struct shell_surface *shsurf)
438{
439 int32_t width, height;
440 struct surface_state *state;
441
442 if (shsurf->state_requested)
443 state = &shsurf->requested_state;
444 else if (shsurf->state_changed)
445 state = &shsurf->next_state;
446 else
447 state = &shsurf->state;
448
449 if (state->fullscreen) {
450 width = shsurf->output->width;
451 height = shsurf->output->height;
452 } else if (state->maximized) {
453 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200454 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700455
456 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200457 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700458
Jonny Lambd73c6942014-08-20 15:53:20 +0200459 width = area.width;
460 height = area.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700461 } else {
462 width = 0;
463 height = 0;
464 }
465
466 shsurf->client->send_configure(shsurf->surface, width, height);
467}
468
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300469static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400470shell_surface_state_changed(struct shell_surface *shsurf)
471{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700472 if (shell_surface_is_xdg_surface(shsurf))
473 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400474}
475
476static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300477shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300478{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700479 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400480 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700481 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400482
483 if (grab->shsurf->resize_edges) {
484 grab->shsurf->resize_edges = 0;
485 shell_surface_state_changed(grab->shsurf);
486 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700487 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300488
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700489 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300490}
491
492static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700493shell_touch_grab_start(struct shell_touch_grab *grab,
494 const struct weston_touch_grab_interface *interface,
495 struct shell_surface *shsurf,
496 struct weston_touch *touch)
497{
498 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800499
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700500 if (touch->seat->pointer)
501 popup_grab_end(touch->seat->pointer);
502
Rusty Lynch1084da52013-08-15 09:10:08 -0700503 grab->grab.interface = interface;
504 grab->shsurf = shsurf;
505 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
506 wl_signal_add(&shsurf->destroy_signal,
507 &grab->shsurf_destroy_listener);
508
509 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700510 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700511
512 weston_touch_start_grab(touch, &grab->grab);
513 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500514 weston_touch_set_focus(touch->seat,
515 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700516}
517
518static void
519shell_touch_grab_end(struct shell_touch_grab *grab)
520{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700521 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700522 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700523 grab->shsurf->grabbed = 0;
524 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700525
526 weston_touch_end_grab(grab->touch);
527}
528
529static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500530center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800531 struct weston_output *output);
532
Daniel Stone496ca172012-05-30 16:31:42 +0100533static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300534get_modifier(char *modifier)
535{
536 if (!modifier)
537 return MODIFIER_SUPER;
538
539 if (!strcmp("ctrl", modifier))
540 return MODIFIER_CTRL;
541 else if (!strcmp("alt", modifier))
542 return MODIFIER_ALT;
543 else if (!strcmp("super", modifier))
544 return MODIFIER_SUPER;
545 else
546 return MODIFIER_SUPER;
547}
548
Juan Zhaoe10d2792012-04-25 19:09:52 +0800549static enum animation_type
550get_animation_type(char *animation)
551{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800552 if (!animation)
553 return ANIMATION_NONE;
554
Juan Zhaoe10d2792012-04-25 19:09:52 +0800555 if (!strcmp("zoom", animation))
556 return ANIMATION_ZOOM;
557 else if (!strcmp("fade", animation))
558 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100559 else if (!strcmp("dim-layer", animation))
560 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800561 else
562 return ANIMATION_NONE;
563}
564
Alex Wu4539b082012-03-01 12:57:46 +0800565static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400566shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200567{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400568 struct weston_config_section *section;
569 int duration;
570 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200571
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400572 section = weston_config_get_section(shell->compositor->config,
573 "screensaver", NULL, NULL);
574 weston_config_section_get_string(section,
575 "path", &shell->screensaver.path, NULL);
576 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200577 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400578
579 section = weston_config_get_section(shell->compositor->config,
580 "shell", NULL, NULL);
581 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100582 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100583 shell->client = s;
584 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400585 "binding-modifier", &s, "super");
586 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200587 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800588
589 weston_config_section_get_string(section,
590 "exposay-modifier", &s, "none");
591 if (strcmp(s, "none") == 0)
592 shell->exposay_modifier = 0;
593 else
594 shell->exposay_modifier = get_modifier(s);
595 free(s);
596
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400597 weston_config_section_get_string(section, "animation", &s, "none");
598 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200599 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200600 weston_config_section_get_string(section, "close-animation", &s, "fade");
601 shell->win_close_animation_type = get_animation_type(s);
602 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700603 weston_config_section_get_string(section,
604 "startup-animation", &s, "fade");
605 shell->startup_animation_type = get_animation_type(s);
606 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700607 if (shell->startup_animation_type == ANIMATION_ZOOM)
608 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100609 weston_config_section_get_string(section, "focus-animation", &s, "none");
610 shell->focus_animation_type = get_animation_type(s);
611 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400612 weston_config_section_get_uint(section, "num-workspaces",
613 &shell->workspaces.num,
614 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200615}
616
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800617struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100618get_default_output(struct weston_compositor *compositor)
619{
620 return container_of(compositor->output_list.next,
621 struct weston_output, link);
622}
623
624
625/* no-op func for checking focus surface */
626static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600627focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100628{
629}
630
631static struct focus_surface *
632get_focus_surface(struct weston_surface *surface)
633{
634 if (surface->configure == focus_surface_configure)
635 return surface->configure_private;
636 else
637 return NULL;
638}
639
640static bool
641is_focus_surface (struct weston_surface *es)
642{
643 return (es->configure == focus_surface_configure);
644}
645
646static bool
647is_focus_view (struct weston_view *view)
648{
649 return is_focus_surface (view->surface);
650}
651
652static struct focus_surface *
653create_focus_surface(struct weston_compositor *ec,
654 struct weston_output *output)
655{
656 struct focus_surface *fsurf = NULL;
657 struct weston_surface *surface = NULL;
658
659 fsurf = malloc(sizeof *fsurf);
660 if (!fsurf)
661 return NULL;
662
663 fsurf->surface = weston_surface_create(ec);
664 surface = fsurf->surface;
665 if (surface == NULL) {
666 free(fsurf);
667 return NULL;
668 }
669
670 surface->configure = focus_surface_configure;
671 surface->output = output;
672 surface->configure_private = fsurf;
673
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800674 fsurf->view = weston_view_create(surface);
675 if (fsurf->view == NULL) {
676 weston_surface_destroy(surface);
677 free(fsurf);
678 return NULL;
679 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100680 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100681
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600682 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600683 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100684 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
685 pixman_region32_fini(&surface->opaque);
686 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
687 output->width, output->height);
688 pixman_region32_fini(&surface->input);
689 pixman_region32_init(&surface->input);
690
691 wl_list_init(&fsurf->workspace_transform.link);
692
693 return fsurf;
694}
695
696static void
697focus_surface_destroy(struct focus_surface *fsurf)
698{
699 weston_surface_destroy(fsurf->surface);
700 free(fsurf);
701}
702
703static void
704focus_animation_done(struct weston_view_animation *animation, void *data)
705{
706 struct workspace *ws = data;
707
708 ws->focus_animation = NULL;
709}
710
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200711static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200712focus_state_destroy(struct focus_state *state)
713{
714 wl_list_remove(&state->seat_destroy_listener.link);
715 wl_list_remove(&state->surface_destroy_listener.link);
716 free(state);
717}
718
719static void
720focus_state_seat_destroy(struct wl_listener *listener, void *data)
721{
722 struct focus_state *state = container_of(listener,
723 struct focus_state,
724 seat_destroy_listener);
725
726 wl_list_remove(&state->link);
727 focus_state_destroy(state);
728}
729
730static void
731focus_state_surface_destroy(struct wl_listener *listener, void *data)
732{
733 struct focus_state *state = container_of(listener,
734 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400735 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400736 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500737 struct weston_surface *main_surface, *next;
738 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200739
Pekka Paalanen01388e22013-04-25 13:57:44 +0300740 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
741
Kristian Høgsberge3778222012-07-31 17:29:30 -0400742 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300743 wl_list_for_each(view,
744 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500745 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400746 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100747 if (is_focus_view(view))
748 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400749
Jason Ekstranda7af7042013-10-12 22:38:11 -0500750 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400751 break;
752 }
753
Pekka Paalanen01388e22013-04-25 13:57:44 +0300754 /* if the focus was a sub-surface, activate its main surface */
755 if (main_surface != state->keyboard_focus)
756 next = main_surface;
757
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100758 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400759 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100760 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100761 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400762 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100763 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
764 if (state->ws->focus_animation)
765 weston_view_animation_destroy(state->ws->focus_animation);
766
767 state->ws->focus_animation = weston_fade_run(
768 state->ws->fsurf_front->view,
769 state->ws->fsurf_front->view->alpha, 0.0, 300,
770 focus_animation_done, state->ws);
771 }
772
Kristian Høgsberge3778222012-07-31 17:29:30 -0400773 wl_list_remove(&state->link);
774 focus_state_destroy(state);
775 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200776}
777
778static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400779focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200780{
Jonas Ådahl04769742012-06-13 00:01:24 +0200781 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200782
783 state = malloc(sizeof *state);
784 if (state == NULL)
785 return NULL;
786
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100787 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400788 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200789 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400790 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200791
792 state->seat_destroy_listener.notify = focus_state_seat_destroy;
793 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400794 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200795 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400796 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200797
798 return state;
799}
800
Jonas Ådahl8538b222012-08-29 22:13:03 +0200801static struct focus_state *
802ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
803{
804 struct workspace *ws = get_current_workspace(shell);
805 struct focus_state *state;
806
807 wl_list_for_each(state, &ws->focus_list, link)
808 if (state->seat == seat)
809 break;
810
811 if (&state->link == &ws->focus_list)
812 state = focus_state_create(seat, ws);
813
814 return state;
815}
816
Jonas Ådahl04769742012-06-13 00:01:24 +0200817static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800818focus_state_set_focus(struct focus_state *state,
819 struct weston_surface *surface)
820{
821 if (state->keyboard_focus) {
822 wl_list_remove(&state->surface_destroy_listener.link);
823 wl_list_init(&state->surface_destroy_listener.link);
824 }
825
826 state->keyboard_focus = surface;
827 if (surface)
828 wl_signal_add(&surface->destroy_signal,
829 &state->surface_destroy_listener);
830}
831
832static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400833restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200834{
835 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400836 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100837 struct wl_list pending_seat_list;
838 struct weston_seat *seat, *next_seat;
839
840 /* Temporarily steal the list of seats so that we can keep
841 * track of the seats we've already processed */
842 wl_list_init(&pending_seat_list);
843 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
844 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200845
846 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100847 wl_list_remove(&state->seat->link);
848 wl_list_insert(&shell->compositor->seat_list,
849 &state->seat->link);
850
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800851 if (state->seat->keyboard == NULL)
852 continue;
853
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400854 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200855
Kristian Høgsberge3148752013-05-06 23:19:49 -0400856 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200857 }
Neil Roberts4237f502014-04-09 16:33:31 +0100858
859 /* For any remaining seats that we don't have a focus state
860 * for we'll reset the keyboard focus to NULL */
861 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
862 wl_list_insert(&shell->compositor->seat_list, &seat->link);
863
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300864 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100865 continue;
866
867 weston_keyboard_set_focus(seat->keyboard, NULL);
868 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200869}
870
871static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200872replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
873 struct weston_seat *seat)
874{
875 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200876
877 wl_list_for_each(state, &ws->focus_list, link) {
878 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800879 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200880 return;
881 }
882 }
883}
884
885static void
886drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
887 struct weston_surface *surface)
888{
889 struct focus_state *state;
890
891 wl_list_for_each(state, &ws->focus_list, link)
892 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800893 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200894}
895
896static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100897animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
898 struct weston_view *from, struct weston_view *to)
899{
900 struct weston_output *output;
901 bool focus_surface_created = false;
902
903 /* FIXME: Only support dim animation using two layers */
904 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
905 return;
906
907 output = get_default_output(shell->compositor);
908 if (ws->fsurf_front == NULL && (from || to)) {
909 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800910 if (ws->fsurf_front == NULL)
911 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100912 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800913
914 ws->fsurf_back = create_focus_surface(shell->compositor, output);
915 if (ws->fsurf_back == NULL) {
916 focus_surface_destroy(ws->fsurf_front);
917 return;
918 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100919 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800920
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100921 focus_surface_created = true;
922 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300923 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
924 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100925 }
926
927 if (ws->focus_animation) {
928 weston_view_animation_destroy(ws->focus_animation);
929 ws->focus_animation = NULL;
930 }
931
932 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300933 weston_layer_entry_insert(&to->layer_link,
934 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100935 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300936 weston_layer_entry_insert(&ws->layer.view_list,
937 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100938
939 if (focus_surface_created) {
940 ws->focus_animation = weston_fade_run(
941 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200942 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100943 focus_animation_done, ws);
944 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300945 weston_layer_entry_insert(&from->layer_link,
946 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100947 ws->focus_animation = weston_stable_fade_run(
948 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200949 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100950 focus_animation_done, ws);
951 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300952 weston_layer_entry_insert(&ws->layer.view_list,
953 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100954 ws->focus_animation = weston_stable_fade_run(
955 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200956 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100957 focus_animation_done, ws);
958 }
959}
960
961static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200962workspace_destroy(struct workspace *ws)
963{
Jonas Ådahl04769742012-06-13 00:01:24 +0200964 struct focus_state *state, *next;
965
966 wl_list_for_each_safe(state, next, &ws->focus_list, link)
967 focus_state_destroy(state);
968
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100969 if (ws->fsurf_front)
970 focus_surface_destroy(ws->fsurf_front);
971 if (ws->fsurf_back)
972 focus_surface_destroy(ws->fsurf_back);
973
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200974 free(ws);
975}
976
Jonas Ådahl04769742012-06-13 00:01:24 +0200977static void
978seat_destroyed(struct wl_listener *listener, void *data)
979{
980 struct weston_seat *seat = data;
981 struct focus_state *state, *next;
982 struct workspace *ws = container_of(listener,
983 struct workspace,
984 seat_destroyed_listener);
985
986 wl_list_for_each_safe(state, next, &ws->focus_list, link)
987 if (state->seat == seat)
988 wl_list_remove(&state->link);
989}
990
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200991static struct workspace *
992workspace_create(void)
993{
994 struct workspace *ws = malloc(sizeof *ws);
995 if (ws == NULL)
996 return NULL;
997
998 weston_layer_init(&ws->layer, NULL);
999
Jonas Ådahl04769742012-06-13 00:01:24 +02001000 wl_list_init(&ws->focus_list);
1001 wl_list_init(&ws->seat_destroyed_listener.link);
1002 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001003 ws->fsurf_front = NULL;
1004 ws->fsurf_back = NULL;
1005 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001006
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001007 return ws;
1008}
1009
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001010static int
1011workspace_is_empty(struct workspace *ws)
1012{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001013 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001014}
1015
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001016static struct workspace *
1017get_workspace(struct desktop_shell *shell, unsigned int index)
1018{
1019 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001020 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001021 pws += index;
1022 return *pws;
1023}
1024
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001025struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001026get_current_workspace(struct desktop_shell *shell)
1027{
1028 return get_workspace(shell, shell->workspaces.current);
1029}
1030
1031static void
1032activate_workspace(struct desktop_shell *shell, unsigned int index)
1033{
1034 struct workspace *ws;
1035
1036 ws = get_workspace(shell, index);
1037 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1038
1039 shell->workspaces.current = index;
1040}
1041
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001042static unsigned int
1043get_output_height(struct weston_output *output)
1044{
1045 return abs(output->region.extents.y1 - output->region.extents.y2);
1046}
1047
1048static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001049view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001050{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001051 struct weston_transform *transform;
1052
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001053 if (is_focus_view(view)) {
1054 struct focus_surface *fsurf = get_focus_surface(view->surface);
1055 transform = &fsurf->workspace_transform;
1056 } else {
1057 struct shell_surface *shsurf = get_shell_surface(view->surface);
1058 transform = &shsurf->workspace_transform;
1059 }
1060
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001061 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001063 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001064
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001065 weston_matrix_init(&transform->matrix);
1066 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001067 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001069}
1070
1071static void
1072workspace_translate_out(struct workspace *ws, double fraction)
1073{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001074 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001075 unsigned int height;
1076 double d;
1077
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001078 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080 d = height * fraction;
1081
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001082 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001083 }
1084}
1085
1086static void
1087workspace_translate_in(struct workspace *ws, double fraction)
1088{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001090 unsigned int height;
1091 double d;
1092
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001093 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001094 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001095
1096 if (fraction > 0)
1097 d = -(height - height * fraction);
1098 else
1099 d = height + height * fraction;
1100
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001101 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001102 }
1103}
1104
1105static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001106broadcast_current_workspace_state(struct desktop_shell *shell)
1107{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001108 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001109
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001110 wl_resource_for_each(resource, &shell->workspaces.client_list)
1111 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001112 shell->workspaces.current,
1113 shell->workspaces.num);
1114}
1115
1116static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117reverse_workspace_change_animation(struct desktop_shell *shell,
1118 unsigned int index,
1119 struct workspace *from,
1120 struct workspace *to)
1121{
1122 shell->workspaces.current = index;
1123
1124 shell->workspaces.anim_to = to;
1125 shell->workspaces.anim_from = from;
1126 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1127 shell->workspaces.anim_timestamp = 0;
1128
Scott Moreau4272e632012-08-13 09:58:41 -06001129 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130}
1131
1132static void
1133workspace_deactivate_transforms(struct workspace *ws)
1134{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001136 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001137
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001138 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001139 if (is_focus_view(view)) {
1140 struct focus_surface *fsurf = get_focus_surface(view->surface);
1141 transform = &fsurf->workspace_transform;
1142 } else {
1143 struct shell_surface *shsurf = get_shell_surface(view->surface);
1144 transform = &shsurf->workspace_transform;
1145 }
1146
1147 if (!wl_list_empty(&transform->link)) {
1148 wl_list_remove(&transform->link);
1149 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001150 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152 }
1153}
1154
1155static void
1156finish_workspace_change_animation(struct desktop_shell *shell,
1157 struct workspace *from,
1158 struct workspace *to)
1159{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001160 struct weston_view *view;
1161
Scott Moreau4272e632012-08-13 09:58:41 -06001162 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001164 /* Views that extend past the bottom of the output are still
1165 * visible after the workspace animation ends but before its layer
1166 * is hidden. In that case, we need to damage below those views so
1167 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001168 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001169 weston_view_damage_below(view);
1170
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001171 wl_list_remove(&shell->workspaces.animation.link);
1172 workspace_deactivate_transforms(from);
1173 workspace_deactivate_transforms(to);
1174 shell->workspaces.anim_to = NULL;
1175
1176 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1177}
1178
1179static void
1180animate_workspace_change_frame(struct weston_animation *animation,
1181 struct weston_output *output, uint32_t msecs)
1182{
1183 struct desktop_shell *shell =
1184 container_of(animation, struct desktop_shell,
1185 workspaces.animation);
1186 struct workspace *from = shell->workspaces.anim_from;
1187 struct workspace *to = shell->workspaces.anim_to;
1188 uint32_t t;
1189 double x, y;
1190
1191 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1192 finish_workspace_change_animation(shell, from, to);
1193 return;
1194 }
1195
1196 if (shell->workspaces.anim_timestamp == 0) {
1197 if (shell->workspaces.anim_current == 0.0)
1198 shell->workspaces.anim_timestamp = msecs;
1199 else
1200 shell->workspaces.anim_timestamp =
1201 msecs -
1202 /* Invers of movement function 'y' below. */
1203 (asin(1.0 - shell->workspaces.anim_current) *
1204 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1205 M_2_PI);
1206 }
1207
1208 t = msecs - shell->workspaces.anim_timestamp;
1209
1210 /*
1211 * x = [0, π/2]
1212 * y(x) = sin(x)
1213 */
1214 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1215 y = sin(x);
1216
1217 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001218 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001219
1220 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1221 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1222 shell->workspaces.anim_current = y;
1223
Scott Moreau4272e632012-08-13 09:58:41 -06001224 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001225 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001226 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001227 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001228}
1229
1230static void
1231animate_workspace_change(struct desktop_shell *shell,
1232 unsigned int index,
1233 struct workspace *from,
1234 struct workspace *to)
1235{
1236 struct weston_output *output;
1237
1238 int dir;
1239
1240 if (index > shell->workspaces.current)
1241 dir = -1;
1242 else
1243 dir = 1;
1244
1245 shell->workspaces.current = index;
1246
1247 shell->workspaces.anim_dir = dir;
1248 shell->workspaces.anim_from = from;
1249 shell->workspaces.anim_to = to;
1250 shell->workspaces.anim_current = 0.0;
1251 shell->workspaces.anim_timestamp = 0;
1252
1253 output = container_of(shell->compositor->output_list.next,
1254 struct weston_output, link);
1255 wl_list_insert(&output->animation_list,
1256 &shell->workspaces.animation.link);
1257
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001258 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001259
1260 workspace_translate_in(to, 0);
1261
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001262 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001263
Scott Moreau4272e632012-08-13 09:58:41 -06001264 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001265}
1266
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001267static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001268update_workspace(struct desktop_shell *shell, unsigned int index,
1269 struct workspace *from, struct workspace *to)
1270{
1271 shell->workspaces.current = index;
1272 wl_list_insert(&from->layer.link, &to->layer.link);
1273 wl_list_remove(&from->layer.link);
1274}
1275
1276static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001277change_workspace(struct desktop_shell *shell, unsigned int index)
1278{
1279 struct workspace *from;
1280 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001281 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001282
1283 if (index == shell->workspaces.current)
1284 return;
1285
1286 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001287 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001288 return;
1289
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001290 from = get_current_workspace(shell);
1291 to = get_workspace(shell, index);
1292
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001293 if (shell->workspaces.anim_from == to &&
1294 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001296 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001297 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001298 return;
1299 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001300
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001301 if (shell->workspaces.anim_to != NULL)
1302 finish_workspace_change_animation(shell,
1303 shell->workspaces.anim_from,
1304 shell->workspaces.anim_to);
1305
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001306 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001307
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001308 if (shell->focus_animation_type != ANIMATION_NONE) {
1309 wl_list_for_each(state, &from->focus_list, link)
1310 if (state->keyboard_focus)
1311 animate_focus_change(shell, from,
1312 get_default_view(state->keyboard_focus), NULL);
1313
1314 wl_list_for_each(state, &to->focus_list, link)
1315 if (state->keyboard_focus)
1316 animate_focus_change(shell, to,
1317 NULL, get_default_view(state->keyboard_focus));
1318 }
1319
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001320 if (workspace_is_empty(to) && workspace_is_empty(from))
1321 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001322 else
1323 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001324
1325 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001326}
1327
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001328static bool
1329workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1330{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001331 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001332 struct wl_list *e;
1333
1334 if (wl_list_empty(list))
1335 return false;
1336
1337 e = list->next;
1338
1339 if (e->next != list)
1340 return false;
1341
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001342 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001343}
1344
1345static void
Philip Withnall659163d2013-11-25 18:01:36 +00001346move_surface_to_workspace(struct desktop_shell *shell,
1347 struct shell_surface *shsurf,
1348 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001349{
1350 struct workspace *from;
1351 struct workspace *to;
1352 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001353 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001354 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001355
1356 if (workspace == shell->workspaces.current)
1357 return;
1358
Philip Withnall659163d2013-11-25 18:01:36 +00001359 view = get_default_view(shsurf->surface);
1360 if (!view)
1361 return;
1362
1363 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1364
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001365 if (workspace >= shell->workspaces.num)
1366 workspace = shell->workspaces.num - 1;
1367
Jonas Ådahle9d22502012-08-29 22:13:01 +02001368 from = get_current_workspace(shell);
1369 to = get_workspace(shell, workspace);
1370
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001371 weston_layer_entry_remove(&view->layer_link);
1372 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001373
Philip Withnall648a4dd2013-11-25 18:01:44 +00001374 shell_surface_update_child_surface_layers(shsurf);
1375
Jason Ekstranda7af7042013-10-12 22:38:11 -05001376 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001377 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1378 if (!seat->keyboard)
1379 continue;
1380
1381 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001383 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001384 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001385
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001387}
1388
1389static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001390take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001391 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001392 unsigned int index)
1393{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001394 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001396 struct shell_surface *shsurf;
1397 struct workspace *from;
1398 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001399 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001400
Pekka Paalanen01388e22013-04-25 13:57:44 +03001401 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402 view = get_default_view(surface);
1403 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001404 index == shell->workspaces.current ||
1405 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001406 return;
1407
1408 from = get_current_workspace(shell);
1409 to = get_workspace(shell, index);
1410
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001411 weston_layer_entry_remove(&view->layer_link);
1412 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001413
Philip Withnall659163d2013-11-25 18:01:36 +00001414 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001415 if (shsurf != NULL)
1416 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001417
Jonas Ådahle9d22502012-08-29 22:13:01 +02001418 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001419 drop_focus_state(shell, from, surface);
1420
1421 if (shell->workspaces.anim_from == to &&
1422 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001423 wl_list_remove(&to->layer.link);
1424 wl_list_insert(from->layer.link.prev, &to->layer.link);
1425
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001426 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001427 broadcast_current_workspace_state(shell);
1428
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001429 return;
1430 }
1431
1432 if (shell->workspaces.anim_to != NULL)
1433 finish_workspace_change_animation(shell,
1434 shell->workspaces.anim_from,
1435 shell->workspaces.anim_to);
1436
1437 if (workspace_is_empty(from) &&
1438 workspace_has_only(to, surface))
1439 update_workspace(shell, index, from, to);
1440 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001441 if (shsurf != NULL &&
1442 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001443 wl_list_insert(&shell->workspaces.anim_sticky_list,
1444 &shsurf->workspace_transform.link);
1445
1446 animate_workspace_change(shell, index, from, to);
1447 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001448
1449 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001450
1451 state = ensure_focus_state(shell, seat);
1452 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001453 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001454}
1455
1456static void
1457workspace_manager_move_surface(struct wl_client *client,
1458 struct wl_resource *resource,
1459 struct wl_resource *surface_resource,
1460 uint32_t workspace)
1461{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001462 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001463 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001464 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001465 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001466 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001467
Pekka Paalanen01388e22013-04-25 13:57:44 +03001468 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001469 shell_surface = get_shell_surface(main_surface);
1470 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001472
1473 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001474}
1475
1476static const struct workspace_manager_interface workspace_manager_implementation = {
1477 workspace_manager_move_surface,
1478};
1479
1480static void
1481unbind_resource(struct wl_resource *resource)
1482{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001483 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001484}
1485
1486static void
1487bind_workspace_manager(struct wl_client *client,
1488 void *data, uint32_t version, uint32_t id)
1489{
1490 struct desktop_shell *shell = data;
1491 struct wl_resource *resource;
1492
Jason Ekstranda85118c2013-06-27 20:17:02 -05001493 resource = wl_resource_create(client,
1494 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001495
1496 if (resource == NULL) {
1497 weston_log("couldn't add workspace manager object");
1498 return;
1499 }
1500
Jason Ekstranda85118c2013-06-27 20:17:02 -05001501 wl_resource_set_implementation(resource,
1502 &workspace_manager_implementation,
1503 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001504 wl_list_insert(&shell->workspaces.client_list,
1505 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001506
1507 workspace_manager_send_state(resource,
1508 shell->workspaces.current,
1509 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001510}
1511
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001512static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001513touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1514 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1515{
1516}
1517
1518static void
1519touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1520{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001521 struct weston_touch_move_grab *move =
1522 (struct weston_touch_move_grab *) container_of(
1523 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001524
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001525 if (touch_id == 0)
1526 move->active = 0;
1527
Jonas Ådahl9484b692013-12-02 22:05:03 +01001528 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001529 shell_touch_grab_end(&move->base);
1530 free(move);
1531 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001532}
1533
1534static void
1535touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1536 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1537{
1538 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1539 struct shell_surface *shsurf = move->base.shsurf;
1540 struct weston_surface *es;
1541 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1542 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1543
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001544 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001545 return;
1546
1547 es = shsurf->surface;
1548
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001549 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001550
1551 weston_compositor_schedule_repaint(es->compositor);
1552}
1553
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001554static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001555touch_move_grab_frame(struct weston_touch_grab *grab)
1556{
1557}
1558
1559static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001560touch_move_grab_cancel(struct weston_touch_grab *grab)
1561{
1562 struct weston_touch_move_grab *move =
1563 (struct weston_touch_move_grab *) container_of(
1564 grab, struct shell_touch_grab, grab);
1565
1566 shell_touch_grab_end(&move->base);
1567 free(move);
1568}
1569
Rusty Lynch1084da52013-08-15 09:10:08 -07001570static const struct weston_touch_grab_interface touch_move_grab_interface = {
1571 touch_move_grab_down,
1572 touch_move_grab_up,
1573 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001574 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001575 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001576};
1577
1578static int
1579surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1580{
1581 struct weston_touch_move_grab *move;
1582
1583 if (!shsurf)
1584 return -1;
1585
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001586 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001587 return 0;
1588
1589 move = malloc(sizeof *move);
1590 if (!move)
1591 return -1;
1592
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001593 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001594 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001595 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001597 seat->touch->grab_y;
1598
1599 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1600 seat->touch);
1601
1602 return 0;
1603}
1604
1605static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001606noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001607{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001608}
1609
1610static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001611constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1612{
1613 struct shell_surface *shsurf = move->base.shsurf;
1614 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001615 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001616 const int safety = 50;
1617
1618 x = wl_fixed_to_int(pointer->x + move->dx);
1619 y = wl_fixed_to_int(pointer->y + move->dy);
1620
Jonny Lambd73c6942014-08-20 15:53:20 +02001621 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1622 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1623 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001624
Jonny Lambd73c6942014-08-20 15:53:20 +02001625 bottom = y + shsurf->geometry.height;
1626 if (bottom - panel_height < safety)
1627 y = panel_height + safety -
1628 shsurf->geometry.height;
1629
1630 if (move->client_initiated &&
1631 y + shsurf->geometry.y < panel_height)
1632 y = panel_height - shsurf->geometry.y;
1633 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001634
1635 *cx = x;
1636 *cy = y;
1637}
1638
1639static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001640move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1641 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001642{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001643 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001644 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001645 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001646 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001647
1648 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001649 if (!shsurf)
1650 return;
1651
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001652 constrain_position(move, &cx, &cy);
1653
1654 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001655
Jason Ekstranda7af7042013-10-12 22:38:11 -05001656 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001657}
1658
1659static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001660move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001661 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001662{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001663 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1664 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001665 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001666 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001667
Daniel Stone4dbadb12012-05-30 16:31:51 +01001668 if (pointer->button_count == 0 &&
1669 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001670 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001671 free(grab);
1672 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001673}
1674
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001675static void
1676move_grab_cancel(struct weston_pointer_grab *grab)
1677{
1678 struct shell_grab *shell_grab =
1679 container_of(grab, struct shell_grab, grab);
1680
1681 shell_grab_end(shell_grab);
1682 free(grab);
1683}
1684
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001685static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001686 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001687 move_grab_motion,
1688 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001689 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001690};
1691
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001692static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001693surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1694 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001695{
1696 struct weston_move_grab *move;
1697
1698 if (!shsurf)
1699 return -1;
1700
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001701 if (shsurf->grabbed ||
1702 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001703 return 0;
1704
1705 move = malloc(sizeof *move);
1706 if (!move)
1707 return -1;
1708
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001710 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001711 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001712 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001713 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001714
1715 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001716 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001717
1718 return 0;
1719}
1720
1721static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001722common_surface_move(struct wl_resource *resource,
1723 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001724{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001725 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001726 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001727 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001728
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001729 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001730 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001731 seat->pointer->button_count > 0 &&
1732 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001733 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001734 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001735 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001736 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001737 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001738 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001739 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001740 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001741 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001742 (surface_touch_move(shsurf, seat) < 0))
1743 wl_resource_post_no_memory(resource);
1744 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001745}
1746
Rafael Antognollie2a34552013-12-03 15:35:45 -02001747static void
1748shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1749 struct wl_resource *seat_resource, uint32_t serial)
1750{
1751 common_surface_move(resource, seat_resource, serial);
1752}
1753
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001754struct weston_resize_grab {
1755 struct shell_grab base;
1756 uint32_t edges;
1757 int32_t width, height;
1758};
1759
1760static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001761resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1762 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001763{
1764 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001765 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001766 struct shell_surface *shsurf = resize->base.shsurf;
1767 int32_t width, height;
1768 wl_fixed_t from_x, from_y;
1769 wl_fixed_t to_x, to_y;
1770
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001771 weston_pointer_move(pointer, x, y);
1772
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001773 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774 return;
1775
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776 weston_view_from_global_fixed(shsurf->view,
1777 pointer->grab_x, pointer->grab_y,
1778 &from_x, &from_y);
1779 weston_view_from_global_fixed(shsurf->view,
1780 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001781
1782 width = resize->width;
1783 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1784 width += wl_fixed_to_int(from_x - to_x);
1785 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1786 width += wl_fixed_to_int(to_x - from_x);
1787 }
1788
1789 height = resize->height;
1790 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1791 height += wl_fixed_to_int(from_y - to_y);
1792 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1793 height += wl_fixed_to_int(to_y - from_y);
1794 }
1795
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001796 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001797}
1798
1799static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001800send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001801{
1802 struct shell_surface *shsurf = get_shell_surface(surface);
1803
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001804 assert(shsurf);
1805
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001806 if (shsurf->resource)
1807 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001808 shsurf->resize_edges,
1809 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001810}
1811
1812static const struct weston_shell_client shell_client = {
1813 send_configure
1814};
1815
1816static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001817resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001818 uint32_t time, uint32_t button, uint32_t state_w)
1819{
1820 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001821 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001822 enum wl_pointer_button_state state = state_w;
1823
1824 if (pointer->button_count == 0 &&
1825 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1826 shell_grab_end(&resize->base);
1827 free(grab);
1828 }
1829}
1830
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001831static void
1832resize_grab_cancel(struct weston_pointer_grab *grab)
1833{
1834 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1835
1836 shell_grab_end(&resize->base);
1837 free(grab);
1838}
1839
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001840static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001841 noop_grab_focus,
1842 resize_grab_motion,
1843 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001844 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001845};
1846
Giulio Camuffob8366642013-04-25 13:57:46 +03001847/*
1848 * Returns the bounding box of a surface and all its sub-surfaces,
1849 * in the surface coordinates system. */
1850static void
1851surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1852 int32_t *y, int32_t *w, int32_t *h) {
1853 pixman_region32_t region;
1854 pixman_box32_t *box;
1855 struct weston_subsurface *subsurface;
1856
1857 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858 surface->width,
1859 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001860
1861 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1862 pixman_region32_union_rect(&region, &region,
1863 subsurface->position.x,
1864 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001865 subsurface->surface->width,
1866 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001867 }
1868
1869 box = pixman_region32_extents(&region);
1870 if (x)
1871 *x = box->x1;
1872 if (y)
1873 *y = box->y1;
1874 if (w)
1875 *w = box->x2 - box->x1;
1876 if (h)
1877 *h = box->y2 - box->y1;
1878
1879 pixman_region32_fini(&region);
1880}
1881
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001882static int
1883surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001884 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001885{
1886 struct weston_resize_grab *resize;
1887
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001888 if (shsurf->grabbed ||
1889 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001890 return 0;
1891
1892 if (edges == 0 || edges > 15 ||
1893 (edges & 3) == 3 || (edges & 12) == 12)
1894 return 0;
1895
1896 resize = malloc(sizeof *resize);
1897 if (!resize)
1898 return -1;
1899
1900 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001901
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001902 resize->width = shsurf->geometry.width;
1903 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001904
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001905 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001906 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001907 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001908 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001909
1910 return 0;
1911}
1912
1913static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001914common_surface_resize(struct wl_resource *resource,
1915 struct wl_resource *seat_resource, uint32_t serial,
1916 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001917{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001918 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001919 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001920 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001921
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001922 if (seat->pointer == NULL ||
1923 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001924 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001925 seat->pointer->focus == NULL)
1926 return;
1927
1928 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1929 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001930 return;
1931
Kristian Høgsberge3148752013-05-06 23:19:49 -04001932 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001933 wl_resource_post_no_memory(resource);
1934}
1935
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001936static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001937shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1938 struct wl_resource *seat_resource, uint32_t serial,
1939 uint32_t edges)
1940{
1941 common_surface_resize(resource, seat_resource, serial, edges);
1942}
1943
1944static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001945busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001946{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001947 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001948 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001949 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001950 wl_fixed_t sx, sy;
1951
Jason Ekstranda7af7042013-10-12 22:38:11 -05001952 view = weston_compositor_pick_view(pointer->seat->compositor,
1953 pointer->x, pointer->y,
1954 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001955
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001956 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1957 shell_grab_end(grab);
1958 free(grab);
1959 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001960}
1961
1962static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001963busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1964 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001965{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001966 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001967}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001968
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001969static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001970busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001971 uint32_t time, uint32_t button, uint32_t state)
1972{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001973 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001974 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001975 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001976
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001977 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001978 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001979 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001980 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001981 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001982 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001983 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001984}
1985
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001986static void
1987busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1988{
1989 struct shell_grab *grab = (struct shell_grab *) base;
1990
1991 shell_grab_end(grab);
1992 free(grab);
1993}
1994
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001995static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001996 busy_cursor_grab_focus,
1997 busy_cursor_grab_motion,
1998 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001999 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002000};
2001
2002static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002003set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002004{
2005 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002006
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002007 if (pointer->grab->interface == &busy_cursor_grab_interface)
2008 return;
2009
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002010 grab = malloc(sizeof *grab);
2011 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002012 return;
2013
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002014 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2015 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002016 /* Mark the shsurf as ungrabbed so that button binding is able
2017 * to move it. */
2018 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002019}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002020
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002021static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002022end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002023{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002024 struct shell_grab *grab;
2025 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002026
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002027 wl_list_for_each(seat, &compositor->seat_list, link) {
2028 if (seat->pointer == NULL)
2029 continue;
2030
2031 grab = (struct shell_grab *) seat->pointer->grab;
2032 if (grab->grab.interface == &busy_cursor_grab_interface &&
2033 wl_resource_get_client(grab->shsurf->resource) == client) {
2034 shell_grab_end(grab);
2035 free(grab);
2036 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002037 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002038}
2039
Scott Moreau9521d5e2012-04-19 13:06:17 -06002040static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002041handle_shell_client_destroy(struct wl_listener *listener, void *data);
2042
2043static int
2044xdg_ping_timeout_handler(void *data)
2045{
2046 struct shell_client *sc = data;
2047 struct weston_seat *seat;
2048 struct shell_surface *shsurf;
2049
2050 /* Client is not responding */
2051 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002052 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2053 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2054 continue;
2055 if (seat->pointer->focus->surface->resource == NULL)
2056 continue;
2057
2058 shsurf = get_shell_surface(seat->pointer->focus->surface);
2059 if (shsurf &&
2060 wl_resource_get_client(shsurf->resource) == sc->client)
2061 set_busy_cursor(shsurf, seat->pointer);
2062 }
2063
2064 return 1;
2065}
2066
2067static void
2068handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2069{
2070 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002071 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002072 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002073 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002074
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002075 if (sc->unresponsive) {
2076 xdg_ping_timeout_handler(sc);
2077 return;
2078 }
2079
2080 sc->ping_serial = serial;
2081 loop = wl_display_get_event_loop(compositor->wl_display);
2082 if (sc->ping_timer == NULL)
2083 sc->ping_timer =
2084 wl_event_loop_add_timer(loop,
2085 xdg_ping_timeout_handler, sc);
2086 if (sc->ping_timer == NULL)
2087 return;
2088
2089 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2090
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002091 if (shell_surface_is_xdg_surface(shsurf) ||
2092 shell_surface_is_xdg_popup(shsurf))
2093 xdg_shell_send_ping(sc->resource, serial);
2094 else if (shell_surface_is_wl_shell_surface(shsurf))
2095 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002096}
2097
Scott Moreauff1db4a2012-04-17 19:06:18 -06002098static void
2099ping_handler(struct weston_surface *surface, uint32_t serial)
2100{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002101 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002102
2103 if (!shsurf)
2104 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002105 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002106 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002107 if (shsurf->surface == shsurf->shell->grab_surface)
2108 return;
2109
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002110 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002111}
2112
2113static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002114handle_pointer_focus(struct wl_listener *listener, void *data)
2115{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002116 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002117 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002118 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002119 uint32_t serial;
2120
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002122 return;
2123
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002125 serial = wl_display_next_serial(compositor->wl_display);
2126 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002127}
2128
2129static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002130shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2131{
2132 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002133 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002134}
2135
2136static void
2137shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2138{
2139 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002140 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002141}
2142
2143static void
2144handle_keyboard_focus(struct wl_listener *listener, void *data)
2145{
2146 struct weston_keyboard *keyboard = data;
2147 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2148
2149 if (seat->focused_surface) {
2150 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2151 if (shsurf)
2152 shell_surface_lose_keyboard_focus(shsurf);
2153 }
2154
2155 seat->focused_surface = keyboard->focus;
2156
2157 if (seat->focused_surface) {
2158 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2159 if (shsurf)
2160 shell_surface_gain_keyboard_focus(shsurf);
2161 }
2162}
2163
2164static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002165shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002166{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002167 if (sc->ping_serial != serial)
2168 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002169
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002170 sc->unresponsive = 0;
2171 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002172
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002173 if (sc->ping_timer) {
2174 wl_event_source_remove(sc->ping_timer);
2175 sc->ping_timer = NULL;
2176 }
2177
2178}
2179
2180static void
2181shell_surface_pong(struct wl_client *client,
2182 struct wl_resource *resource, uint32_t serial)
2183{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002184 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2185 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002186
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002187 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002188}
2189
2190static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002191set_title(struct shell_surface *shsurf, const char *title)
2192{
2193 free(shsurf->title);
2194 shsurf->title = strdup(title);
2195}
2196
2197static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002198set_window_geometry(struct shell_surface *shsurf,
2199 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002200{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002201 shsurf->next_geometry.x = x;
2202 shsurf->next_geometry.y = y;
2203 shsurf->next_geometry.width = width;
2204 shsurf->next_geometry.height = height;
2205 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002206}
2207
2208static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002209shell_surface_set_title(struct wl_client *client,
2210 struct wl_resource *resource, const char *title)
2211{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002212 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002213
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002214 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002215}
2216
2217static void
2218shell_surface_set_class(struct wl_client *client,
2219 struct wl_resource *resource, const char *class)
2220{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002221 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002222
2223 free(shsurf->class);
2224 shsurf->class = strdup(class);
2225}
2226
Alex Wu4539b082012-03-01 12:57:46 +08002227static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002228restore_output_mode(struct weston_output *output)
2229{
Hardening57388e42013-09-18 23:56:36 +02002230 if (output->current_mode != output->original_mode ||
2231 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002232 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002233 output->original_mode,
2234 output->original_scale,
2235 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002236}
2237
2238static void
2239restore_all_output_modes(struct weston_compositor *compositor)
2240{
2241 struct weston_output *output;
2242
2243 wl_list_for_each(output, &compositor->output_list, link)
2244 restore_output_mode(output);
2245}
2246
Philip Withnall07926d92013-11-25 18:01:40 +00002247/* The surface will be inserted into the list immediately after the link
2248 * returned by this function (i.e. will be stacked immediately above the
2249 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002250static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002251shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2252{
2253 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002254 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002255
2256 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002257 case SHELL_SURFACE_XWAYLAND:
2258 return &shsurf->shell->fullscreen_layer.view_list;
2259
2260 case SHELL_SURFACE_NONE:
2261 return NULL;
2262
Kristian Høgsbergead52422014-01-01 13:51:52 -08002263 case SHELL_SURFACE_POPUP:
2264 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002265 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002266 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002267 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002268 /* Move the surface to its parent layer so
2269 * that surfaces which are transient for
2270 * fullscreen surfaces don't get hidden by the
2271 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002272
2273 /* TODO: Handle a parent with multiple views */
2274 parent = get_default_view(shsurf->parent);
2275 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002276 return container_of(parent->layer_link.link.prev,
2277 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002278 }
Philip Withnall07926d92013-11-25 18:01:40 +00002279
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002280 /* Move the surface to a normal workspace layer so that surfaces
2281 * which were previously fullscreen or transient are no longer
2282 * rendered on top. */
2283 ws = get_current_workspace(shsurf->shell);
2284 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002285 }
2286
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002287 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002288}
2289
Philip Withnall648a4dd2013-11-25 18:01:44 +00002290static void
2291shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2292{
2293 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002294 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002295
2296 /* Move the child layers to the same workspace as shsurf. They will be
2297 * stacked above shsurf. */
2298 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002299 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002300 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002301 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002302 prev = container_of(shsurf->view->layer_link.link.prev,
2303 struct weston_layer_entry, link);
2304 weston_layer_entry_remove(&child->view->layer_link);
2305 weston_layer_entry_insert(prev,
2306 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002307 weston_view_geometry_dirty(child->view);
2308 weston_surface_damage(child->surface);
2309
2310 /* Recurse. We don’t expect this to recurse very far (if
2311 * at all) because that would imply we have transient
2312 * (or popup) children of transient surfaces, which
2313 * would be unusual. */
2314 shell_surface_update_child_surface_layers(child);
2315 }
2316 }
2317}
2318
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002319/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002320 * geometry to ensure the changes are redrawn.
2321 *
2322 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2323 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002324static void
2325shell_surface_update_layer(struct shell_surface *shsurf)
2326{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002327 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002328
2329 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2330
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002331 if (new_layer_link == NULL)
2332 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002333 if (new_layer_link == &shsurf->view->layer_link)
2334 return;
2335
2336 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002337 weston_layer_entry_remove(&shsurf->view->layer_link);
2338 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002339 weston_view_geometry_dirty(shsurf->view);
2340 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002341
2342 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002343}
2344
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002345static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002346shell_surface_set_parent(struct shell_surface *shsurf,
2347 struct weston_surface *parent)
2348{
2349 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002350
2351 wl_list_remove(&shsurf->children_link);
2352 wl_list_init(&shsurf->children_link);
2353
2354 /* Insert into the parent surface’s child list. */
2355 if (parent != NULL) {
2356 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2357 if (parent_shsurf != NULL)
2358 wl_list_insert(&parent_shsurf->children_list,
2359 &shsurf->children_link);
2360 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002361}
2362
2363static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002364shell_surface_set_output(struct shell_surface *shsurf,
2365 struct weston_output *output)
2366{
2367 struct weston_surface *es = shsurf->surface;
2368
2369 /* get the default output, if the client set it as NULL
2370 check whether the ouput is available */
2371 if (output)
2372 shsurf->output = output;
2373 else if (es->output)
2374 shsurf->output = es->output;
2375 else
2376 shsurf->output = get_default_output(es->compositor);
2377}
2378
2379static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002380surface_clear_next_states(struct shell_surface *shsurf)
2381{
2382 shsurf->next_state.maximized = false;
2383 shsurf->next_state.fullscreen = false;
2384
2385 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2386 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2387 shsurf->state_changed = true;
2388}
2389
2390static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002391set_toplevel(struct shell_surface *shsurf)
2392{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002393 shell_surface_set_parent(shsurf, NULL);
2394 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002395 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002396
2397 /* The layer_link is updated in set_surface_type(),
2398 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002399}
2400
2401static void
2402shell_surface_set_toplevel(struct wl_client *client,
2403 struct wl_resource *resource)
2404{
2405 struct shell_surface *surface = wl_resource_get_user_data(resource);
2406
2407 set_toplevel(surface);
2408}
2409
2410static void
2411set_transient(struct shell_surface *shsurf,
2412 struct weston_surface *parent, int x, int y, uint32_t flags)
2413{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002414 assert(parent != NULL);
2415
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002416 shell_surface_set_parent(shsurf, parent);
2417
2418 surface_clear_next_states(shsurf);
2419
Philip Withnallbecb77e2013-11-25 18:01:30 +00002420 shsurf->transient.x = x;
2421 shsurf->transient.y = y;
2422 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002423
Rafael Antognollied207b42013-12-03 15:35:43 -02002424 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002425 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002426 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002427
2428 /* The layer_link is updated in set_surface_type(),
2429 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002430}
2431
2432static void
2433shell_surface_set_transient(struct wl_client *client,
2434 struct wl_resource *resource,
2435 struct wl_resource *parent_resource,
2436 int x, int y, uint32_t flags)
2437{
2438 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2439 struct weston_surface *parent =
2440 wl_resource_get_user_data(parent_resource);
2441
2442 set_transient(shsurf, parent, x, y, flags);
2443}
2444
2445static void
2446set_fullscreen(struct shell_surface *shsurf,
2447 uint32_t method,
2448 uint32_t framerate,
2449 struct weston_output *output)
2450{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002451 shell_surface_set_output(shsurf, output);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002452 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002453
2454 shsurf->fullscreen_output = shsurf->output;
2455 shsurf->fullscreen.type = method;
2456 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002457
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002458 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002459}
2460
2461static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002462weston_view_set_initial_position(struct weston_view *view,
2463 struct desktop_shell *shell);
2464
2465static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002466unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002467{
Philip Withnallf85fe842013-11-25 18:01:37 +00002468 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002469 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2470 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002471 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002472 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002473
Alex Wu4539b082012-03-01 12:57:46 +08002474 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2475 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002476
Alex Wu4539b082012-03-01 12:57:46 +08002477 wl_list_remove(&shsurf->fullscreen.transform.link);
2478 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002479
Jason Ekstranda7af7042013-10-12 22:38:11 -05002480 if (shsurf->fullscreen.black_view)
2481 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2482 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002483
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002484 if (shsurf->saved_position_valid)
2485 weston_view_set_position(shsurf->view,
2486 shsurf->saved_x, shsurf->saved_y);
2487 else
2488 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2489
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002490 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002491 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002492 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002493 shsurf->saved_rotation_valid = false;
2494 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002495
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002496 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002497}
2498
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002499static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002500shell_surface_set_fullscreen(struct wl_client *client,
2501 struct wl_resource *resource,
2502 uint32_t method,
2503 uint32_t framerate,
2504 struct wl_resource *output_resource)
2505{
2506 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2507 struct weston_output *output;
2508
2509 if (output_resource)
2510 output = wl_resource_get_user_data(output_resource);
2511 else
2512 output = NULL;
2513
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002514 shell_surface_set_parent(shsurf, NULL);
2515
Rafael Antognolli03b16592013-12-03 15:35:42 -02002516 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002517 shsurf->next_state.fullscreen = true;
2518 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002519 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002520}
2521
2522static void
2523set_popup(struct shell_surface *shsurf,
2524 struct weston_surface *parent,
2525 struct weston_seat *seat,
2526 uint32_t serial,
2527 int32_t x,
2528 int32_t y)
2529{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002530 assert(parent != NULL);
2531
Philip Withnallbecb77e2013-11-25 18:01:30 +00002532 shsurf->popup.shseat = get_shell_seat(seat);
2533 shsurf->popup.serial = serial;
2534 shsurf->popup.x = x;
2535 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002536
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002537 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002538}
2539
2540static void
2541shell_surface_set_popup(struct wl_client *client,
2542 struct wl_resource *resource,
2543 struct wl_resource *seat_resource,
2544 uint32_t serial,
2545 struct wl_resource *parent_resource,
2546 int32_t x, int32_t y, uint32_t flags)
2547{
2548 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002549 struct weston_surface *parent =
2550 wl_resource_get_user_data(parent_resource);
2551
2552 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002553
Rafael Antognolli03b16592013-12-03 15:35:42 -02002554 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002555 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002556 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002557 wl_resource_get_user_data(seat_resource),
2558 serial, x, y);
2559}
2560
2561static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002562unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002563{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002564 /* undo all maximized things here */
2565 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002566
2567 if (shsurf->saved_position_valid)
2568 weston_view_set_position(shsurf->view,
2569 shsurf->saved_x, shsurf->saved_y);
2570 else
2571 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002572
2573 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002574 wl_list_insert(&shsurf->view->geometry.transformation_list,
2575 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002576 shsurf->saved_rotation_valid = false;
2577 }
2578
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002579 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002580}
2581
Philip Withnallbecb77e2013-11-25 18:01:30 +00002582static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002583set_minimized(struct weston_surface *surface, uint32_t is_true)
2584{
2585 struct shell_surface *shsurf;
2586 struct workspace *current_ws;
2587 struct weston_seat *seat;
2588 struct weston_surface *focus;
2589 struct weston_view *view;
2590
2591 view = get_default_view(surface);
2592 if (!view)
2593 return;
2594
2595 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2596
2597 shsurf = get_shell_surface(surface);
2598 current_ws = get_current_workspace(shsurf->shell);
2599
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002600 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002601 /* hide or show, depending on the state */
2602 if (is_true) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002603 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002604
2605 drop_focus_state(shsurf->shell, current_ws, view->surface);
2606 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2607 if (!seat->keyboard)
2608 continue;
2609 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2610 if (focus == view->surface)
2611 weston_keyboard_set_focus(seat->keyboard, NULL);
2612 }
2613 }
2614 else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002615 weston_layer_entry_insert(&current_ws->layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002616
2617 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2618 if (!seat->keyboard)
2619 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002620 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002621 }
2622 }
2623
2624 shell_surface_update_child_surface_layers(shsurf);
2625
2626 weston_view_damage_below(view);
2627}
2628
2629static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002630shell_surface_set_maximized(struct wl_client *client,
2631 struct wl_resource *resource,
2632 struct wl_resource *output_resource)
2633{
2634 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2635 struct weston_output *output;
2636
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002637 surface_clear_next_states(shsurf);
2638 shsurf->next_state.maximized = true;
2639 shsurf->state_changed = true;
2640
2641 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2642 shell_surface_set_parent(shsurf, NULL);
2643
Philip Withnallbecb77e2013-11-25 18:01:30 +00002644 if (output_resource)
2645 output = wl_resource_get_user_data(output_resource);
2646 else
2647 output = NULL;
2648
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002649 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002650
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002651 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002652}
2653
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002654/* This is only ever called from set_surface_type(), so there’s no need to
2655 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002656static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002657reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002658{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002659 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002660 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002661 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002662 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002663
Pekka Paalanen98262232011-12-01 10:42:22 +02002664 return 0;
2665}
2666
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002667static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002668set_full_output(struct shell_surface *shsurf)
2669{
2670 shsurf->saved_x = shsurf->view->geometry.x;
2671 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002672 shsurf->saved_width = shsurf->surface->width;
2673 shsurf->saved_height = shsurf->surface->height;
2674 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002675 shsurf->saved_position_valid = true;
2676
2677 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2678 wl_list_remove(&shsurf->rotation.transform.link);
2679 wl_list_init(&shsurf->rotation.transform.link);
2680 weston_view_geometry_dirty(shsurf->view);
2681 shsurf->saved_rotation_valid = true;
2682 }
2683}
2684
2685static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002686set_surface_type(struct shell_surface *shsurf)
2687{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002688 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002689 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002690
Philip Withnallbecb77e2013-11-25 18:01:30 +00002691 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002692
Rafael Antognolli03b16592013-12-03 15:35:42 -02002693 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002694 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002695
2696 switch (shsurf->type) {
2697 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002698 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002699 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002700 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002701 weston_view_set_position(shsurf->view,
2702 pev->geometry.x + shsurf->transient.x,
2703 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002704 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002705 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002706
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002707 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002708 weston_view_set_position(shsurf->view, shsurf->transient.x,
2709 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002710 break;
2711
Philip Withnall0f640e22013-11-25 18:01:31 +00002712 case SHELL_SURFACE_POPUP:
2713 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002714 default:
2715 break;
2716 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002717
2718 /* Update the surface’s layer. */
2719 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002720}
2721
Tiago Vignattibe143262012-04-16 17:31:41 +03002722static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002723shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002724{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002725 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002726}
2727
Alex Wu21858432012-04-01 20:13:08 +08002728static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002729black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002730
Jason Ekstranda7af7042013-10-12 22:38:11 -05002731static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002732create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002733 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002734 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002735{
2736 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002737 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002738
2739 surface = weston_surface_create(ec);
2740 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002741 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002742 return NULL;
2743 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002744 view = weston_view_create(surface);
2745 if (surface == NULL) {
2746 weston_log("no memory\n");
2747 weston_surface_destroy(surface);
2748 return NULL;
2749 }
Alex Wu4539b082012-03-01 12:57:46 +08002750
Alex Wu21858432012-04-01 20:13:08 +08002751 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002752 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002753 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002754 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002755 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002756 pixman_region32_fini(&surface->input);
2757 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002758
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002759 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002760 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002761
2762 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002763}
2764
Philip Withnalled8f1a92013-11-25 18:01:43 +00002765static void
2766shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2767{
2768 struct weston_output *output = shsurf->fullscreen_output;
2769
Rafael Antognolli03b16592013-12-03 15:35:42 -02002770 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002771
2772 if (!shsurf->fullscreen.black_view)
2773 shsurf->fullscreen.black_view =
2774 create_black_surface(shsurf->surface->compositor,
2775 shsurf->surface,
2776 output->x, output->y,
2777 output->width,
2778 output->height);
2779
2780 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002781 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2782 weston_layer_entry_insert(&shsurf->view->layer_link,
2783 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002784 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2785 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002786
2787 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002788}
2789
Alex Wu4539b082012-03-01 12:57:46 +08002790/* Create black surface and append it to the associated fullscreen surface.
2791 * Handle size dismatch and positioning according to the method. */
2792static void
2793shell_configure_fullscreen(struct shell_surface *shsurf)
2794{
2795 struct weston_output *output = shsurf->fullscreen_output;
2796 struct weston_surface *surface = shsurf->surface;
2797 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002798 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002799 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002800
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002801 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2802 restore_output_mode(output);
2803
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002804 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002805 weston_layer_entry_remove(&shsurf->view->layer_link);
2806 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002807
Philip Withnalled8f1a92013-11-25 18:01:43 +00002808 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002809
Jason Ekstranda7af7042013-10-12 22:38:11 -05002810 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002811 &surf_width, &surf_height);
2812
Alex Wu4539b082012-03-01 12:57:46 +08002813 switch (shsurf->fullscreen.type) {
2814 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002815 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002816 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002817 break;
2818 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002819 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002820 if (output->width == surf_width &&
2821 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002822 weston_view_set_position(shsurf->view,
2823 output->x - surf_x,
2824 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002825 break;
2826 }
2827
Alex Wu4539b082012-03-01 12:57:46 +08002828 matrix = &shsurf->fullscreen.transform.matrix;
2829 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002830
Scott Moreau1bad5db2012-08-18 01:04:05 -06002831 output_aspect = (float) output->width /
2832 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002833 /* XXX: Use surf_width and surf_height here? */
2834 surface_aspect = (float) surface->width /
2835 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002836 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002837 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002838 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002839 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002840 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002841 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002842
Alex Wu4539b082012-03-01 12:57:46 +08002843 weston_matrix_scale(matrix, scale, scale, 1);
2844 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002845 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002846 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002847 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2848 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002849 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002850
Alex Wu4539b082012-03-01 12:57:46 +08002851 break;
2852 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002853 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002854 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002855 surf_width * surface->buffer_viewport.buffer.scale,
2856 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002857 shsurf->fullscreen.framerate};
2858
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002859 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002860 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002861 weston_view_set_position(shsurf->view,
2862 output->x - surf_x,
2863 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002864 shsurf->fullscreen.black_view->surface->width = output->width;
2865 shsurf->fullscreen.black_view->surface->height = output->height;
2866 weston_view_set_position(shsurf->fullscreen.black_view,
2867 output->x - surf_x,
2868 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002869 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002870 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002871 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002872 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002873 }
Alex Wubd3354b2012-04-17 17:20:49 +08002874 }
Alex Wu4539b082012-03-01 12:57:46 +08002875 break;
2876 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002877 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002878 break;
2879 default:
2880 break;
2881 }
2882}
2883
Alex Wu4539b082012-03-01 12:57:46 +08002884static void
2885shell_map_fullscreen(struct shell_surface *shsurf)
2886{
Alex Wubd3354b2012-04-17 17:20:49 +08002887 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002888}
2889
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002890static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002891set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2892{
2893 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002894 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002895 shsurf->transient.x = x;
2896 shsurf->transient.y = y;
2897 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002898
2899 shell_surface_set_parent(shsurf, NULL);
2900
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002901 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002902 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002903}
2904
Kristian Høgsberg47792412014-05-04 13:47:06 -07002905static void
2906shell_interface_set_fullscreen(struct shell_surface *shsurf,
2907 uint32_t method,
2908 uint32_t framerate,
2909 struct weston_output *output)
2910{
2911 surface_clear_next_states(shsurf);
2912 set_fullscreen(shsurf, method, framerate, output);
2913
2914 shsurf->next_state.fullscreen = true;
2915 shsurf->state_changed = true;
2916}
2917
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002918static int
2919shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2920{
2921 return surface_move(shsurf, ws, 1);
2922}
2923
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002924static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002925
2926static void
2927destroy_shell_seat(struct wl_listener *listener, void *data)
2928{
2929 struct shell_seat *shseat =
2930 container_of(listener,
2931 struct shell_seat, seat_destroy_listener);
2932 struct shell_surface *shsurf, *prev = NULL;
2933
2934 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002935 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002936 shseat->popup_grab.client = NULL;
2937
2938 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2939 shsurf->popup.shseat = NULL;
2940 if (prev) {
2941 wl_list_init(&prev->popup.grab_link);
2942 }
2943 prev = shsurf;
2944 }
2945 wl_list_init(&prev->popup.grab_link);
2946 }
2947
2948 wl_list_remove(&shseat->seat_destroy_listener.link);
2949 free(shseat);
2950}
2951
Jason Ekstrand024177c2014-04-21 19:42:58 -05002952static void
2953shell_seat_caps_changed(struct wl_listener *listener, void *data)
2954{
2955 struct shell_seat *seat;
2956
2957 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2958
2959 if (seat->seat->keyboard &&
2960 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2961 wl_signal_add(&seat->seat->keyboard->focus_signal,
2962 &seat->keyboard_focus_listener);
2963 } else if (!seat->seat->keyboard) {
2964 wl_list_init(&seat->keyboard_focus_listener.link);
2965 }
2966
2967 if (seat->seat->pointer &&
2968 wl_list_empty(&seat->pointer_focus_listener.link)) {
2969 wl_signal_add(&seat->seat->pointer->focus_signal,
2970 &seat->pointer_focus_listener);
2971 } else if (!seat->seat->pointer) {
2972 wl_list_init(&seat->pointer_focus_listener.link);
2973 }
2974}
2975
Giulio Camuffo5085a752013-03-25 21:42:45 +01002976static struct shell_seat *
2977create_shell_seat(struct weston_seat *seat)
2978{
2979 struct shell_seat *shseat;
2980
2981 shseat = calloc(1, sizeof *shseat);
2982 if (!shseat) {
2983 weston_log("no memory to allocate shell seat\n");
2984 return NULL;
2985 }
2986
2987 shseat->seat = seat;
2988 wl_list_init(&shseat->popup_grab.surfaces_list);
2989
2990 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2991 wl_signal_add(&seat->destroy_signal,
2992 &shseat->seat_destroy_listener);
2993
Jason Ekstrand024177c2014-04-21 19:42:58 -05002994 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2995 wl_list_init(&shseat->keyboard_focus_listener.link);
2996
2997 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2998 wl_list_init(&shseat->pointer_focus_listener.link);
2999
3000 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3001 wl_signal_add(&seat->updated_caps_signal,
3002 &shseat->caps_changed_listener);
3003 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3004
Giulio Camuffo5085a752013-03-25 21:42:45 +01003005 return shseat;
3006}
3007
3008static struct shell_seat *
3009get_shell_seat(struct weston_seat *seat)
3010{
3011 struct wl_listener *listener;
3012
3013 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003014 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003015
3016 return container_of(listener,
3017 struct shell_seat, seat_destroy_listener);
3018}
3019
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003020static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003021popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003022{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003023 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003024 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003025 struct shell_seat *shseat =
3026 container_of(grab, struct shell_seat, popup_grab.grab);
3027 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003028 wl_fixed_t sx, sy;
3029
Jason Ekstranda7af7042013-10-12 22:38:11 -05003030 view = weston_compositor_pick_view(pointer->seat->compositor,
3031 pointer->x, pointer->y,
3032 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003033
Jason Ekstranda7af7042013-10-12 22:38:11 -05003034 if (view && view->surface->resource &&
3035 wl_resource_get_client(view->surface->resource) == client) {
3036 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003037 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003038 weston_pointer_set_focus(pointer, NULL,
3039 wl_fixed_from_int(0),
3040 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003041 }
3042}
3043
3044static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003045popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3046 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003047{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003048 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003049 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003050 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003051
Jonas Ådahl61917c82014-08-11 22:44:02 +02003052 if (pointer->focus) {
3053 weston_view_from_global_fixed(pointer->focus, x, y,
3054 &pointer->sx, &pointer->sy);
3055 }
3056
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003057 weston_pointer_move(pointer, x, y);
3058
Neil Roberts96d790e2013-09-19 17:32:00 +01003059 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003060 weston_view_from_global_fixed(pointer->focus,
3061 pointer->x, pointer->y,
3062 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003063 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003064 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003065}
3066
3067static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003068popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003069 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003070{
3071 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003072 struct shell_seat *shseat =
3073 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003074 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003075 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003076 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003077 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003078
Neil Roberts96d790e2013-09-19 17:32:00 +01003079 resource_list = &grab->pointer->focus_resource_list;
3080 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003081 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003082 wl_resource_for_each(resource, resource_list) {
3083 wl_pointer_send_button(resource, serial,
3084 time, button, state);
3085 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003086 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003087 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003088 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003089 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003090 }
3091
Daniel Stone4dbadb12012-05-30 16:31:51 +01003092 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003093 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003094}
3095
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003096static void
3097popup_grab_cancel(struct weston_pointer_grab *grab)
3098{
3099 popup_grab_end(grab->pointer);
3100}
3101
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003102static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003103 popup_grab_focus,
3104 popup_grab_motion,
3105 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003106 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003107};
3108
3109static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003110shell_surface_send_popup_done(struct shell_surface *shsurf)
3111{
3112 if (shell_surface_is_wl_shell_surface(shsurf))
3113 wl_shell_surface_send_popup_done(shsurf->resource);
3114 else if (shell_surface_is_xdg_popup(shsurf))
3115 xdg_popup_send_popup_done(shsurf->resource,
3116 shsurf->popup.serial);
3117}
3118
3119static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003120popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003121{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003122 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003123 struct shell_seat *shseat =
3124 container_of(grab, struct shell_seat, popup_grab.grab);
3125 struct shell_surface *shsurf;
3126 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003127
3128 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003129 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003130 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003131 shseat->popup_grab.grab.interface = NULL;
3132 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003133 /* Send the popup_done event to all the popups open */
3134 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003135 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003136 shsurf->popup.shseat = NULL;
3137 if (prev) {
3138 wl_list_init(&prev->popup.grab_link);
3139 }
3140 prev = shsurf;
3141 }
3142 wl_list_init(&prev->popup.grab_link);
3143 wl_list_init(&shseat->popup_grab.surfaces_list);
3144 }
3145}
3146
3147static void
3148add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3149{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003150 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003151
3152 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003153 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003154 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003155 /* We must make sure here that this popup was opened after
3156 * a mouse press, and not just by moving around with other
3157 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003158 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003159 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003160
Rob Bradforddfe31052013-06-26 19:49:11 +01003161 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003162 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003163 } else {
3164 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003165 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003166}
3167
3168static void
3169remove_popup_grab(struct shell_surface *shsurf)
3170{
3171 struct shell_seat *shseat = shsurf->popup.shseat;
3172
3173 wl_list_remove(&shsurf->popup.grab_link);
3174 wl_list_init(&shsurf->popup.grab_link);
3175 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003176 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003177 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003178 }
3179}
3180
3181static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003182shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003183{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003184 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003185 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003186
Jason Ekstranda7af7042013-10-12 22:38:11 -05003187 shsurf->surface->output = parent_view->output;
3188 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003189
Jason Ekstranda7af7042013-10-12 22:38:11 -05003190 weston_view_set_transform_parent(shsurf->view, parent_view);
3191 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3192 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003193
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003194 if (shseat->seat->pointer &&
3195 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003196 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003197 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003198 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003199 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003200 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003201}
3202
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003203static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003204 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003205 shell_surface_move,
3206 shell_surface_resize,
3207 shell_surface_set_toplevel,
3208 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003209 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003210 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003211 shell_surface_set_maximized,
3212 shell_surface_set_title,
3213 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003214};
3215
3216static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003217destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003218{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003219 struct shell_surface *child, *next;
3220
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003221 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3222
Giulio Camuffo5085a752013-03-25 21:42:45 +01003223 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3224 remove_popup_grab(shsurf);
3225 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003226
Alex Wubd3354b2012-04-17 17:20:49 +08003227 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003228 shell_surface_is_top_fullscreen(shsurf))
3229 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003230
Jason Ekstranda7af7042013-10-12 22:38:11 -05003231 if (shsurf->fullscreen.black_view)
3232 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003233
Alex Wubd3354b2012-04-17 17:20:49 +08003234 /* As destroy_resource() use wl_list_for_each_safe(),
3235 * we can always remove the listener.
3236 */
3237 wl_list_remove(&shsurf->surface_destroy_listener.link);
3238 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003239 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003240
Jason Ekstranda7af7042013-10-12 22:38:11 -05003241 weston_view_destroy(shsurf->view);
3242
Philip Withnall648a4dd2013-11-25 18:01:44 +00003243 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003244 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3245 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003246
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003247 wl_list_remove(&shsurf->link);
3248 free(shsurf);
3249}
3250
3251static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003252shell_destroy_shell_surface(struct wl_resource *resource)
3253{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003254 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003255
Bryan Caina46b9462014-04-04 17:41:24 -05003256 if (!wl_list_empty(&shsurf->popup.grab_link))
3257 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003258 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003259}
3260
3261static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003262shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003263{
3264 struct shell_surface *shsurf = container_of(listener,
3265 struct shell_surface,
3266 surface_destroy_listener);
3267
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003268 if (shsurf->resource)
3269 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003270
3271 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003272}
3273
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003274static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003275fade_out_done(struct weston_view_animation *animation, void *data)
3276{
3277 struct shell_surface *shsurf = data;
3278
3279 weston_surface_destroy(shsurf->surface);
3280}
3281
3282static void
3283handle_resource_destroy(struct wl_listener *listener, void *data)
3284{
3285 struct shell_surface *shsurf =
3286 container_of(listener, struct shell_surface,
3287 resource_destroy_listener);
3288
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003289 if (!weston_surface_is_mapped(shsurf->surface))
3290 return;
3291
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003292 shsurf->surface->ref_count++;
3293
3294 pixman_region32_fini(&shsurf->surface->pending.input);
3295 pixman_region32_init(&shsurf->surface->pending.input);
3296 pixman_region32_fini(&shsurf->surface->input);
3297 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003298 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3299 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3300 fade_out_done, shsurf);
3301 } else {
3302 weston_surface_destroy(shsurf->surface);
3303 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003304}
3305
3306static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003307shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003308
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003309struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003310get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003311{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003312 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003313 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003314 else
3315 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003316}
3317
Rafael Antognollie2a34552013-12-03 15:35:45 -02003318static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003319create_common_surface(struct shell_client *owner, void *shell,
3320 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003321 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003322{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003323 struct shell_surface *shsurf;
3324
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003325 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003326 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003327 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003328 }
3329
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003330 shsurf = calloc(1, sizeof *shsurf);
3331 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003332 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003333 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003334 }
3335
Jason Ekstranda7af7042013-10-12 22:38:11 -05003336 shsurf->view = weston_view_create(surface);
3337 if (!shsurf->view) {
3338 weston_log("no memory to allocate shell surface\n");
3339 free(shsurf);
3340 return NULL;
3341 }
3342
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003343 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003344 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003345
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003346 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3347 wl_resource_add_destroy_listener(surface->resource,
3348 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003349 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003350
Tiago Vignattibc052c92012-04-19 16:18:18 +03003351 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003352 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003353 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003354 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003355 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003356 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003357 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3358 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003359 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003360 wl_list_init(&shsurf->fullscreen.transform.link);
3361
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003362 shsurf->output = get_default_output(shsurf->shell->compositor);
3363
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003364 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003365 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003366 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003367 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003368
3369 /* init link so its safe to always remove it in destroy_shell_surface */
3370 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003371 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003372
Pekka Paalanen460099f2012-01-20 16:48:25 +02003373 /* empty when not in use */
3374 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003375 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003376
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003377 wl_list_init(&shsurf->workspace_transform.link);
3378
Philip Withnall648a4dd2013-11-25 18:01:44 +00003379 wl_list_init(&shsurf->children_link);
3380 wl_list_init(&shsurf->children_list);
3381 shsurf->parent = NULL;
3382
Pekka Paalanen98262232011-12-01 10:42:22 +02003383 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003384
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003385 shsurf->client = client;
3386
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003387 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003388}
3389
Rafael Antognollie2a34552013-12-03 15:35:45 -02003390static struct shell_surface *
3391create_shell_surface(void *shell, struct weston_surface *surface,
3392 const struct weston_shell_client *client)
3393{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003394 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003395}
3396
Jason Ekstranda7af7042013-10-12 22:38:11 -05003397static struct weston_view *
3398get_primary_view(void *shell, struct shell_surface *shsurf)
3399{
3400 return shsurf->view;
3401}
3402
Tiago Vignattibc052c92012-04-19 16:18:18 +03003403static void
3404shell_get_shell_surface(struct wl_client *client,
3405 struct wl_resource *resource,
3406 uint32_t id,
3407 struct wl_resource *surface_resource)
3408{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003409 struct weston_surface *surface =
3410 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003411 struct shell_client *sc = wl_resource_get_user_data(resource);
3412 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003413 struct shell_surface *shsurf;
3414
3415 if (get_shell_surface(surface)) {
3416 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003417 WL_DISPLAY_ERROR_INVALID_OBJECT,
3418 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003419 return;
3420 }
3421
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003422 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003423 if (!shsurf) {
3424 wl_resource_post_error(surface_resource,
3425 WL_DISPLAY_ERROR_INVALID_OBJECT,
3426 "surface->configure already set");
3427 return;
3428 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003429
Jason Ekstranda85118c2013-06-27 20:17:02 -05003430 shsurf->resource =
3431 wl_resource_create(client,
3432 &wl_shell_surface_interface, 1, id);
3433 wl_resource_set_implementation(shsurf->resource,
3434 &shell_surface_implementation,
3435 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003436}
3437
Rafael Antognollie2a34552013-12-03 15:35:45 -02003438static bool
3439shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3440{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003441 /* A shell surface without a resource is created from xwayland
3442 * and is considered a wl_shell surface for now. */
3443
3444 return shsurf->resource == NULL ||
3445 wl_resource_instance_of(shsurf->resource,
3446 &wl_shell_surface_interface,
3447 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003448}
3449
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003450static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003451 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003452};
3453
Rafael Antognollie2a34552013-12-03 15:35:45 -02003454/****************************
3455 * xdg-shell implementation */
3456
3457static void
3458xdg_surface_destroy(struct wl_client *client,
3459 struct wl_resource *resource)
3460{
3461 wl_resource_destroy(resource);
3462}
3463
3464static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003465xdg_surface_set_parent(struct wl_client *client,
3466 struct wl_resource *resource,
3467 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003468{
3469 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3470 struct weston_surface *parent;
3471
3472 if (parent_resource)
3473 parent = wl_resource_get_user_data(parent_resource);
3474 else
3475 parent = NULL;
3476
3477 shell_surface_set_parent(shsurf, parent);
3478}
3479
3480static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003481xdg_surface_set_app_id(struct wl_client *client,
3482 struct wl_resource *resource,
3483 const char *app_id)
3484{
3485 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3486
3487 free(shsurf->class);
3488 shsurf->class = strdup(app_id);
3489}
3490
3491static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003492xdg_surface_show_window_menu(struct wl_client *client,
3493 struct wl_resource *surface_resource,
3494 struct wl_resource *seat_resource,
3495 uint32_t serial,
3496 int32_t x,
3497 int32_t y)
3498{
3499 /* TODO */
3500}
3501
3502static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003503xdg_surface_set_title(struct wl_client *client,
3504 struct wl_resource *resource, const char *title)
3505{
3506 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3507
3508 set_title(shsurf, title);
3509}
3510
3511static void
3512xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3513 struct wl_resource *seat_resource, uint32_t serial)
3514{
3515 common_surface_move(resource, seat_resource, serial);
3516}
3517
3518static void
3519xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3520 struct wl_resource *seat_resource, uint32_t serial,
3521 uint32_t edges)
3522{
3523 common_surface_resize(resource, seat_resource, serial, edges);
3524}
3525
3526static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003527xdg_surface_ack_configure(struct wl_client *client,
3528 struct wl_resource *resource,
3529 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003530{
3531 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3532
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003533 if (shsurf->state_requested) {
3534 shsurf->next_state = shsurf->requested_state;
3535 shsurf->state_changed = true;
3536 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003537 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003538}
3539
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003540static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003541xdg_surface_set_window_geometry(struct wl_client *client,
3542 struct wl_resource *resource,
3543 int32_t x,
3544 int32_t y,
3545 int32_t width,
3546 int32_t height)
3547{
3548 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3549
3550 set_window_geometry(shsurf, x, y, width, height);
3551}
3552
3553static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003554xdg_surface_set_maximized(struct wl_client *client,
3555 struct wl_resource *resource)
3556{
3557 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3558
3559 shsurf->state_requested = true;
3560 shsurf->requested_state.maximized = true;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003561 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003562}
3563
3564static void
3565xdg_surface_unset_maximized(struct wl_client *client,
3566 struct wl_resource *resource)
3567{
3568 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3569
3570 shsurf->state_requested = true;
3571 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003572 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003573}
3574
3575static void
3576xdg_surface_set_fullscreen(struct wl_client *client,
3577 struct wl_resource *resource,
3578 struct wl_resource *output_resource)
3579{
3580 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3581 struct weston_output *output;
3582
3583 shsurf->state_requested = true;
3584 shsurf->requested_state.fullscreen = true;
3585
3586 if (output_resource)
3587 output = wl_resource_get_user_data(output_resource);
3588 else
3589 output = NULL;
3590
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003591 shell_surface_set_output(shsurf, output);
3592 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003593
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003594 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003595}
3596
3597static void
3598xdg_surface_unset_fullscreen(struct wl_client *client,
3599 struct wl_resource *resource)
3600{
3601 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3602
3603 shsurf->state_requested = true;
3604 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003605 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003606}
3607
3608static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003609xdg_surface_set_minimized(struct wl_client *client,
3610 struct wl_resource *resource)
3611{
3612 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3613
3614 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3615 return;
3616
3617 /* apply compositor's own minimization logic (hide) */
3618 set_minimized(shsurf->surface, 1);
3619}
3620
Rafael Antognollie2a34552013-12-03 15:35:45 -02003621static const struct xdg_surface_interface xdg_surface_implementation = {
3622 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003623 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003624 xdg_surface_set_title,
3625 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003626 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003627 xdg_surface_move,
3628 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003629 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003630 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003631 xdg_surface_set_maximized,
3632 xdg_surface_unset_maximized,
3633 xdg_surface_set_fullscreen,
3634 xdg_surface_unset_fullscreen,
3635 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003636};
3637
3638static void
3639xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003640 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003641{
3642 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003643 uint32_t *s;
3644 struct wl_array states;
3645 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003646
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003647 assert(shsurf);
3648
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003649 if (!shsurf->resource)
3650 return;
3651
3652 wl_array_init(&states);
3653 if (shsurf->requested_state.fullscreen) {
3654 s = wl_array_add(&states, sizeof *s);
3655 *s = XDG_SURFACE_STATE_FULLSCREEN;
3656 } else if (shsurf->requested_state.maximized) {
3657 s = wl_array_add(&states, sizeof *s);
3658 *s = XDG_SURFACE_STATE_MAXIMIZED;
3659 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003660 if (shsurf->resize_edges != 0) {
3661 s = wl_array_add(&states, sizeof *s);
3662 *s = XDG_SURFACE_STATE_RESIZING;
3663 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003664 if (shsurf->focus_count > 0) {
3665 s = wl_array_add(&states, sizeof *s);
3666 *s = XDG_SURFACE_STATE_ACTIVATED;
3667 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003668
3669 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3670 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3671
3672 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003673}
3674
3675static const struct weston_shell_client xdg_client = {
3676 xdg_send_configure
3677};
3678
3679static void
3680xdg_use_unstable_version(struct wl_client *client,
3681 struct wl_resource *resource,
3682 int32_t version)
3683{
3684 if (version > 1) {
3685 wl_resource_post_error(resource,
3686 1,
3687 "xdg-shell:: version not implemented yet.");
3688 return;
3689 }
3690}
3691
3692static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003693create_xdg_surface(struct shell_client *owner, void *shell,
3694 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003695 const struct weston_shell_client *client)
3696{
3697 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003698
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003699 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003700 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003701
3702 return shsurf;
3703}
3704
3705static void
3706xdg_get_xdg_surface(struct wl_client *client,
3707 struct wl_resource *resource,
3708 uint32_t id,
3709 struct wl_resource *surface_resource)
3710{
3711 struct weston_surface *surface =
3712 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003713 struct shell_client *sc = wl_resource_get_user_data(resource);
3714 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003715 struct shell_surface *shsurf;
3716
3717 if (get_shell_surface(surface)) {
3718 wl_resource_post_error(surface_resource,
3719 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003720 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003721 return;
3722 }
3723
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003724 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003725 if (!shsurf) {
3726 wl_resource_post_error(surface_resource,
3727 WL_DISPLAY_ERROR_INVALID_OBJECT,
3728 "surface->configure already set");
3729 return;
3730 }
3731
3732 shsurf->resource =
3733 wl_resource_create(client,
3734 &xdg_surface_interface, 1, id);
3735 wl_resource_set_implementation(shsurf->resource,
3736 &xdg_surface_implementation,
3737 shsurf, shell_destroy_shell_surface);
3738}
3739
3740static bool
3741shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3742{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003743 return shsurf->resource &&
3744 wl_resource_instance_of(shsurf->resource,
3745 &xdg_surface_interface,
3746 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003747}
3748
3749/* xdg-popup implementation */
3750
3751static void
3752xdg_popup_destroy(struct wl_client *client,
3753 struct wl_resource *resource)
3754{
3755 wl_resource_destroy(resource);
3756}
3757
Rafael Antognollie2a34552013-12-03 15:35:45 -02003758static const struct xdg_popup_interface xdg_popup_implementation = {
3759 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003760};
3761
3762static void
3763xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003764 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003765{
3766}
3767
3768static const struct weston_shell_client xdg_popup_client = {
3769 xdg_popup_send_configure
3770};
3771
3772static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003773create_xdg_popup(struct shell_client *owner, void *shell,
3774 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003775 const struct weston_shell_client *client,
3776 struct weston_surface *parent,
3777 struct shell_seat *seat,
3778 uint32_t serial,
3779 int32_t x, int32_t y)
3780{
3781 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003782
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003783 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003784 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003785 shsurf->popup.shseat = seat;
3786 shsurf->popup.serial = serial;
3787 shsurf->popup.x = x;
3788 shsurf->popup.y = y;
3789 shell_surface_set_parent(shsurf, parent);
3790
3791 return shsurf;
3792}
3793
3794static void
3795xdg_get_xdg_popup(struct wl_client *client,
3796 struct wl_resource *resource,
3797 uint32_t id,
3798 struct wl_resource *surface_resource,
3799 struct wl_resource *parent_resource,
3800 struct wl_resource *seat_resource,
3801 uint32_t serial,
3802 int32_t x, int32_t y, uint32_t flags)
3803{
3804 struct weston_surface *surface =
3805 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003806 struct shell_client *sc = wl_resource_get_user_data(resource);
3807 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003808 struct shell_surface *shsurf;
3809 struct weston_surface *parent;
3810 struct shell_seat *seat;
3811
3812 if (get_shell_surface(surface)) {
3813 wl_resource_post_error(surface_resource,
3814 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003815 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003816 return;
3817 }
3818
3819 if (!parent_resource) {
3820 wl_resource_post_error(surface_resource,
3821 WL_DISPLAY_ERROR_INVALID_OBJECT,
3822 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04003823 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003824 }
3825
3826 parent = wl_resource_get_user_data(parent_resource);
3827 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3828
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003829 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003830 parent, seat, serial, x, y);
3831 if (!shsurf) {
3832 wl_resource_post_error(surface_resource,
3833 WL_DISPLAY_ERROR_INVALID_OBJECT,
3834 "surface->configure already set");
3835 return;
3836 }
3837
3838 shsurf->resource =
3839 wl_resource_create(client,
3840 &xdg_popup_interface, 1, id);
3841 wl_resource_set_implementation(shsurf->resource,
3842 &xdg_popup_implementation,
3843 shsurf, shell_destroy_shell_surface);
3844}
3845
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003846static void
3847xdg_pong(struct wl_client *client,
3848 struct wl_resource *resource, uint32_t serial)
3849{
3850 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003851
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003852 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003853}
3854
Rafael Antognollie2a34552013-12-03 15:35:45 -02003855static bool
3856shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3857{
3858 return wl_resource_instance_of(shsurf->resource,
3859 &xdg_popup_interface,
3860 &xdg_popup_implementation);
3861}
3862
3863static const struct xdg_shell_interface xdg_implementation = {
3864 xdg_use_unstable_version,
3865 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003866 xdg_get_xdg_popup,
3867 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003868};
3869
3870static int
3871xdg_shell_unversioned_dispatch(const void *implementation,
3872 void *_target, uint32_t opcode,
3873 const struct wl_message *message,
3874 union wl_argument *args)
3875{
3876 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003877 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003878
3879 if (opcode != 0) {
3880 wl_resource_post_error(resource,
3881 WL_DISPLAY_ERROR_INVALID_OBJECT,
3882 "must call use_unstable_version first");
3883 return 0;
3884 }
3885
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003886#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003887
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003888 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3889 "shell implementation doesn't match protocol version");
3890
Rafael Antognollie2a34552013-12-03 15:35:45 -02003891 if (args[0].i != XDG_SERVER_VERSION) {
3892 wl_resource_post_error(resource,
3893 WL_DISPLAY_ERROR_INVALID_OBJECT,
3894 "incompatible version, server is %d "
3895 "client wants %d",
3896 XDG_SERVER_VERSION, args[0].i);
3897 return 0;
3898 }
3899
3900 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003901 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003902
3903 return 1;
3904}
3905
3906/* end of xdg-shell implementation */
3907/***********************************/
3908
Kristian Høgsberg07937562011-04-12 17:25:42 -04003909static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003910shell_fade(struct desktop_shell *shell, enum fade_type type);
3911
3912static int
3913screensaver_timeout(void *data)
3914{
3915 struct desktop_shell *shell = data;
3916
3917 shell_fade(shell, FADE_OUT);
3918
3919 return 1;
3920}
3921
3922static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003923handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003924{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003925 struct desktop_shell *shell =
3926 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003927
Pekka Paalanen18027e52011-12-02 16:31:49 +02003928 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003929
3930 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003931 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003932}
3933
3934static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003935launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003936{
3937 if (shell->screensaver.binding)
3938 return;
3939
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003940 if (!shell->screensaver.path) {
3941 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003942 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003943 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003944
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003945 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003946 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003947 return;
3948 }
3949
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003950 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003951 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003952 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003953 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003954}
3955
3956static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003957terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003958{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003959 if (shell->screensaver.process.pid == 0)
3960 return;
3961
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003962 /* Disarm the screensaver timer, otherwise it may fire when the
3963 * compositor is not in the idle state. In that case, the screen will
3964 * be locked, but the wake_signal won't fire on user input, making the
3965 * system unresponsive. */
3966 wl_event_source_timer_update(shell->screensaver.timer, 0);
3967
Pekka Paalanen18027e52011-12-02 16:31:49 +02003968 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003969}
3970
3971static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003972configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003973{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003974 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003975
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003976 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003977 if (v->output == ev->output && v != ev) {
3978 weston_view_unmap(v);
3979 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003980 }
3981 }
3982
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003983 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003984
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003985 if (wl_list_empty(&ev->layer_link.link)) {
3986 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003987 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003988 }
3989}
3990
3991static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003992background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003993{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003994 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003995 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003996
Jason Ekstranda7af7042013-10-12 22:38:11 -05003997 view = container_of(es->views.next, struct weston_view, surface_link);
3998
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003999 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004000}
4001
4002static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004003desktop_shell_set_background(struct wl_client *client,
4004 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004005 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004006 struct wl_resource *surface_resource)
4007{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004008 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004009 struct weston_surface *surface =
4010 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004011 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004012
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004013 if (surface->configure) {
4014 wl_resource_post_error(surface_resource,
4015 WL_DISPLAY_ERROR_INVALID_OBJECT,
4016 "surface role already assigned");
4017 return;
4018 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004019
Jason Ekstranda7af7042013-10-12 22:38:11 -05004020 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4021 weston_view_destroy(view);
4022 view = weston_view_create(surface);
4023
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004024 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004025 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004026 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004027 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004028 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004029 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004030 surface->output->width,
4031 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004032}
4033
4034static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004035panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004036{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004037 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004038 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004039
Jason Ekstranda7af7042013-10-12 22:38:11 -05004040 view = container_of(es->views.next, struct weston_view, surface_link);
4041
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004042 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004043}
4044
4045static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004046desktop_shell_set_panel(struct wl_client *client,
4047 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004048 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004049 struct wl_resource *surface_resource)
4050{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004051 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004052 struct weston_surface *surface =
4053 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004054 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004055
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004056 if (surface->configure) {
4057 wl_resource_post_error(surface_resource,
4058 WL_DISPLAY_ERROR_INVALID_OBJECT,
4059 "surface role already assigned");
4060 return;
4061 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004062
Jason Ekstranda7af7042013-10-12 22:38:11 -05004063 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4064 weston_view_destroy(view);
4065 view = weston_view_create(surface);
4066
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004067 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004068 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004069 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004070 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004071 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004072 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004073 surface->output->width,
4074 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004075}
4076
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004077static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004078lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004079{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004080 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004081 struct weston_view *view;
4082
4083 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004084
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004085 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004086 return;
4087
Jason Ekstranda7af7042013-10-12 22:38:11 -05004088 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004089
4090 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004091 weston_layer_entry_insert(&shell->lock_layer.view_list,
4092 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004093 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004094 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004095 }
4096}
4097
4098static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004099handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004100{
Tiago Vignattibe143262012-04-16 17:31:41 +03004101 struct desktop_shell *shell =
4102 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004103
Martin Minarik6d118362012-06-07 18:01:59 +02004104 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004105 shell->lock_surface = NULL;
4106}
4107
4108static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004109desktop_shell_set_lock_surface(struct wl_client *client,
4110 struct wl_resource *resource,
4111 struct wl_resource *surface_resource)
4112{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004113 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004114 struct weston_surface *surface =
4115 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004116
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004117 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004118
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004119 if (!shell->locked)
4120 return;
4121
Pekka Paalanen98262232011-12-01 10:42:22 +02004122 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004123
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004124 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004125 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004126 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004127
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004128 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004129 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004130 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004131}
4132
4133static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004134resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004135{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004136 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004137
Pekka Paalanen77346a62011-11-30 16:26:35 +02004138 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004139
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004140 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004141 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004142 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004143 &shell->input_panel_layer.link);
4144 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004145 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004146 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004147 wl_list_insert(&shell->compositor->cursor_layer.link,
4148 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004149 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004150 wl_list_insert(&shell->fullscreen_layer.link,
4151 &shell->panel_layer.link);
4152 wl_list_insert(&shell->panel_layer.link,
4153 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004154
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004155 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004156
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004157 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004158 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004159 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004160}
4161
4162static void
4163desktop_shell_unlock(struct wl_client *client,
4164 struct wl_resource *resource)
4165{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004166 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004167
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004168 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004169
4170 if (shell->locked)
4171 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004172}
4173
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004174static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004175desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004176 struct wl_resource *resource,
4177 struct wl_resource *surface_resource)
4178{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004179 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004180
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004181 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004182 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004183}
4184
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004185static void
4186desktop_shell_desktop_ready(struct wl_client *client,
4187 struct wl_resource *resource)
4188{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004189 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004190
4191 shell_fade_startup(shell);
4192}
4193
Jonny Lamb765760d2014-08-20 15:53:19 +02004194static void
4195desktop_shell_set_panel_position(struct wl_client *client,
4196 struct wl_resource *resource,
4197 uint32_t position)
4198{
4199 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4200
4201 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4202 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4203 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4204 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4205 wl_resource_post_error(resource,
4206 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4207 "bad position argument");
4208 return;
4209 }
4210
4211 shell->panel_position = position;
4212}
4213
Kristian Høgsberg75840622011-09-06 13:48:16 -04004214static const struct desktop_shell_interface desktop_shell_implementation = {
4215 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004216 desktop_shell_set_panel,
4217 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004218 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004219 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004220 desktop_shell_desktop_ready,
4221 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004222};
4223
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004224static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004225get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004226{
4227 struct shell_surface *shsurf;
4228
4229 shsurf = get_shell_surface(surface);
4230 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004231 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004232 return shsurf->type;
4233}
4234
Kristian Høgsberg75840622011-09-06 13:48:16 -04004235static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004236move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004237{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004238 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004239 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004240 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004241
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004242 if (seat->pointer->focus == NULL)
4243 return;
4244
4245 focus = seat->pointer->focus->surface;
4246
Pekka Paalanen01388e22013-04-25 13:57:44 +03004247 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004248 if (surface == NULL)
4249 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004250
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004251 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004252 if (shsurf == NULL || shsurf->state.fullscreen ||
4253 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004254 return;
4255
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004256 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004257}
4258
4259static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004260maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4261{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004262 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004263 struct weston_surface *surface;
4264 struct shell_surface *shsurf;
4265
4266 surface = weston_surface_get_main_surface(focus);
4267 if (surface == NULL)
4268 return;
4269
4270 shsurf = get_shell_surface(surface);
4271 if (shsurf == NULL)
4272 return;
4273
4274 if (!shell_surface_is_xdg_surface(shsurf))
4275 return;
4276
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004277 shsurf->state_requested = true;
4278 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004279 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004280}
4281
4282static void
4283fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4284{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004285 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004286 struct weston_surface *surface;
4287 struct shell_surface *shsurf;
4288
4289 surface = weston_surface_get_main_surface(focus);
4290 if (surface == NULL)
4291 return;
4292
4293 shsurf = get_shell_surface(surface);
4294 if (shsurf == NULL)
4295 return;
4296
4297 if (!shell_surface_is_xdg_surface(shsurf))
4298 return;
4299
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004300 shsurf->state_requested = true;
4301 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004302 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004303 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004304}
4305
4306static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004307touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4308{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004309 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004310 struct weston_surface *surface;
4311 struct shell_surface *shsurf;
4312
4313 surface = weston_surface_get_main_surface(focus);
4314 if (surface == NULL)
4315 return;
4316
4317 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004318 if (shsurf == NULL || shsurf->state.fullscreen ||
4319 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004320 return;
4321
4322 surface_touch_move(shsurf, (struct weston_seat *) seat);
4323}
4324
4325static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004326resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004327{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004328 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004329 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004330 uint32_t edges = 0;
4331 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004332 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004333
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004334 if (seat->pointer->focus == NULL)
4335 return;
4336
4337 focus = seat->pointer->focus->surface;
4338
Pekka Paalanen01388e22013-04-25 13:57:44 +03004339 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004340 if (surface == NULL)
4341 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004342
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004343 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004344 if (shsurf == NULL || shsurf->state.fullscreen ||
4345 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004346 return;
4347
Jason Ekstranda7af7042013-10-12 22:38:11 -05004348 weston_view_from_global(shsurf->view,
4349 wl_fixed_to_int(seat->pointer->grab_x),
4350 wl_fixed_to_int(seat->pointer->grab_y),
4351 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004352
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004353 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004354 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004355 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004356 edges |= 0;
4357 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004358 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004359
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004360 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004361 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004362 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004363 edges |= 0;
4364 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004365 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004366
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004367 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004368}
4369
4370static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004371surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004372 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004373{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004374 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004375 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004376 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004377 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004378
Pekka Paalanen01388e22013-04-25 13:57:44 +03004379 /* XXX: broken for windows containing sub-surfaces */
4380 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004381 if (surface == NULL)
4382 return;
4383
4384 shsurf = get_shell_surface(surface);
4385 if (!shsurf)
4386 return;
4387
Jason Ekstranda7af7042013-10-12 22:38:11 -05004388 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004389
Jason Ekstranda7af7042013-10-12 22:38:11 -05004390 if (shsurf->view->alpha > 1.0)
4391 shsurf->view->alpha = 1.0;
4392 if (shsurf->view->alpha < step)
4393 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004394
Jason Ekstranda7af7042013-10-12 22:38:11 -05004395 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004396 weston_surface_damage(surface);
4397}
4398
4399static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004400do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004401 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004402{
Daniel Stone37816df2012-05-16 18:45:18 +01004403 struct weston_seat *ws = (struct weston_seat *) seat;
4404 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004405 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004406 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004407
4408 wl_list_for_each(output, &compositor->output_list, link) {
4409 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004410 wl_fixed_to_double(seat->pointer->x),
4411 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004412 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004413 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004414 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004415 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004416 increment = -output->zoom.increment;
4417 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004418 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004419 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004420 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004421 else
4422 increment = 0;
4423
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004424 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004425
Scott Moreaue6603982012-06-11 13:07:51 -06004426 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004427 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004428 else if (output->zoom.level > output->zoom.max_level)
4429 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004430 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004431 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004432 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004433
Scott Moreaue6603982012-06-11 13:07:51 -06004434 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004435
Jason Ekstranda7af7042013-10-12 22:38:11 -05004436 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004437 }
4438 }
4439}
4440
Scott Moreauccbf29d2012-02-22 14:21:41 -07004441static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004442zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004443 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004444{
4445 do_zoom(seat, time, 0, axis, value);
4446}
4447
4448static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004449zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004450 void *data)
4451{
4452 do_zoom(seat, time, key, 0, 0);
4453}
4454
4455static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004456terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004457 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004458{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004459 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004460
Daniel Stone325fc2d2012-05-30 16:31:58 +01004461 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004462}
4463
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004464static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004465rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4466 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004467{
4468 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004469 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004470 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004471 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004472 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004473
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004474 weston_pointer_move(pointer, x, y);
4475
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004476 if (!shsurf)
4477 return;
4478
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004479 cx = 0.5f * shsurf->surface->width;
4480 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004481
Daniel Stone37816df2012-05-16 18:45:18 +01004482 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4483 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004484 r = sqrtf(dx * dx + dy * dy);
4485
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004486 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004487 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004488
4489 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004490 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004491 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004492
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004493 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004494 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004495
4496 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004497 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004498 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004499 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004500 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004501
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004502 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004503 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004504 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004505 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004506 wl_list_init(&shsurf->rotation.transform.link);
4507 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004508 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004509 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004510
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004511 /* We need to adjust the position of the surface
4512 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004513 cposx = shsurf->view->geometry.x + cx;
4514 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004515 dposx = rotate->center.x - cposx;
4516 dposy = rotate->center.y - cposy;
4517 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004518 weston_view_set_position(shsurf->view,
4519 shsurf->view->geometry.x + dposx,
4520 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004521 }
4522
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004523 /* Repaint implies weston_surface_update_transform(), which
4524 * lazily applies the damage due to rotation update.
4525 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004526 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004527}
4528
4529static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004530rotate_grab_button(struct weston_pointer_grab *grab,
4531 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004532{
4533 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004534 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004535 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004536 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004537 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004538
Daniel Stone4dbadb12012-05-30 16:31:51 +01004539 if (pointer->button_count == 0 &&
4540 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004541 if (shsurf)
4542 weston_matrix_multiply(&shsurf->rotation.rotation,
4543 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004544 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004545 free(rotate);
4546 }
4547}
4548
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004549static void
4550rotate_grab_cancel(struct weston_pointer_grab *grab)
4551{
4552 struct rotate_grab *rotate =
4553 container_of(grab, struct rotate_grab, base.grab);
4554
4555 shell_grab_end(&rotate->base);
4556 free(rotate);
4557}
4558
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004559static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004560 noop_grab_focus,
4561 rotate_grab_motion,
4562 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004563 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004564};
4565
4566static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004567surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004568{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004569 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004570 float dx, dy;
4571 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004572
Pekka Paalanen460099f2012-01-20 16:48:25 +02004573 rotate = malloc(sizeof *rotate);
4574 if (!rotate)
4575 return;
4576
Jason Ekstranda7af7042013-10-12 22:38:11 -05004577 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004578 surface->surface->width * 0.5f,
4579 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004580 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004581
Daniel Stone37816df2012-05-16 18:45:18 +01004582 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4583 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004584 r = sqrtf(dx * dx + dy * dy);
4585 if (r > 20.0f) {
4586 struct weston_matrix inverse;
4587
4588 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004589 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004590 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004591
4592 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004593 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004594 } else {
4595 weston_matrix_init(&surface->rotation.rotation);
4596 weston_matrix_init(&rotate->rotation);
4597 }
4598
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004599 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4600 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004601}
4602
4603static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004604rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004605 void *data)
4606{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004607 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004608 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004609 struct shell_surface *surface;
4610
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004611 if (seat->pointer->focus == NULL)
4612 return;
4613
4614 focus = seat->pointer->focus->surface;
4615
Pekka Paalanen01388e22013-04-25 13:57:44 +03004616 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004617 if (base_surface == NULL)
4618 return;
4619
4620 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004621 if (surface == NULL || surface->state.fullscreen ||
4622 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004623 return;
4624
4625 surface_rotate(surface, seat);
4626}
4627
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004628/* Move all fullscreen layers down to the current workspace and hide their
4629 * black views. The surfaces' state is set to both fullscreen and lowered,
4630 * and this is reversed when such a surface is re-configured, see
4631 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4632 *
4633 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004634 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004635void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004636lower_fullscreen_layer(struct desktop_shell *shell)
4637{
4638 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004639 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004640
4641 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004642 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004643 &shell->fullscreen_layer.view_list.link,
4644 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004645 struct shell_surface *shsurf = get_shell_surface(view->surface);
4646
4647 if (!shsurf)
4648 continue;
4649
4650 /* We can have a non-fullscreen popup for a fullscreen surface
4651 * in the fullscreen layer. */
4652 if (shsurf->state.fullscreen) {
4653 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004654 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4655 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004656 weston_view_damage_below(shsurf->fullscreen.black_view);
4657
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004658 }
4659
4660 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004661 weston_layer_entry_remove(&view->layer_link);
4662 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004663 weston_view_damage_below(view);
4664 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004665
4666 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004667 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004668}
4669
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004670void
Tiago Vignattibe143262012-04-16 17:31:41 +03004671activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004672 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004673{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004674 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004675 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004676 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004677 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004678 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004679
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004680 lower_fullscreen_layer(shell);
4681
Pekka Paalanen01388e22013-04-25 13:57:44 +03004682 main_surface = weston_surface_get_main_surface(es);
4683
Daniel Stone37816df2012-05-16 18:45:18 +01004684 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004685
Jonas Ådahl8538b222012-08-29 22:13:03 +02004686 state = ensure_focus_state(shell, seat);
4687 if (state == NULL)
4688 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004689
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004690 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004691 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004692
Rafael Antognolli03b16592013-12-03 15:35:42 -02004693 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004694 assert(shsurf);
4695
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004696 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004697 shell_configure_fullscreen(shsurf);
4698 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004699 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004700
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004701 /* Update the surface’s layer. This brings it to the top of the stacking
4702 * order as appropriate. */
4703 shell_surface_update_layer(shsurf);
4704
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004705 if (shell->focus_animation_type != ANIMATION_NONE) {
4706 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004707 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004708 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004709}
4710
Alex Wu21858432012-04-01 20:13:08 +08004711/* no-op func for checking black surface */
4712static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004713black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004714{
4715}
4716
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004717static bool
Alex Wu21858432012-04-01 20:13:08 +08004718is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4719{
4720 if (es->configure == black_surface_configure) {
4721 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004722 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004723 return true;
4724 }
4725 return false;
4726}
4727
Kristian Høgsberg75840622011-09-06 13:48:16 -04004728static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004729activate_binding(struct weston_seat *seat,
4730 struct desktop_shell *shell,
4731 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004732{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004733 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004734
Alex Wu9c35e6b2012-03-05 14:13:13 +08004735 if (!focus)
4736 return;
4737
Pekka Paalanen01388e22013-04-25 13:57:44 +03004738 if (is_black_surface(focus, &main_surface))
4739 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004740
Pekka Paalanen01388e22013-04-25 13:57:44 +03004741 main_surface = weston_surface_get_main_surface(focus);
4742 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004743 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004744
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004745 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004746}
4747
4748static void
4749click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4750 void *data)
4751{
4752 if (seat->pointer->grab != &seat->pointer->default_grab)
4753 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004754 if (seat->pointer->focus == NULL)
4755 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004756
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004757 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004758}
4759
4760static void
4761touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4762{
4763 if (seat->touch->grab != &seat->touch->default_grab)
4764 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004765 if (seat->touch->focus == NULL)
4766 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004767
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004768 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004769}
4770
4771static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004772unfocus_all_seats(struct desktop_shell *shell)
4773{
4774 struct weston_seat *seat, *next;
4775
4776 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4777 if (seat->keyboard == NULL)
4778 continue;
4779
4780 weston_keyboard_set_focus(seat->keyboard, NULL);
4781 }
4782}
4783
4784static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004785lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004786{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004787 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004788
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004789 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004790 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004791 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004792 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004793
4794 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004795
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004796 /* Hide all surfaces by removing the fullscreen, panel and
4797 * toplevel layers. This way nothing else can show or receive
4798 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004799
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004800 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004801 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004802 if (shell->showing_input_panels)
4803 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004804 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004805 wl_list_insert(&shell->compositor->cursor_layer.link,
4806 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004807
Pekka Paalanen77346a62011-11-30 16:26:35 +02004808 launch_screensaver(shell);
4809
Neil Robertsb4a91702014-04-09 16:33:32 +01004810 /* Remove the keyboard focus on all seats. This will be
4811 * restored to the workspace's saved state via
4812 * restore_focus_state when the compositor is unlocked */
4813 unfocus_all_seats(shell);
4814
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004815 /* TODO: disable bindings that should not work while locked. */
4816
4817 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004818}
4819
4820static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004821unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004822{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004823 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004824 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004825 return;
4826 }
4827
4828 /* If desktop-shell client has gone away, unlock immediately. */
4829 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004830 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004831 return;
4832 }
4833
4834 if (shell->prepare_event_sent)
4835 return;
4836
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004837 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004838 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004839}
4840
4841static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004842shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004843{
4844 struct desktop_shell *shell = data;
4845
4846 shell->fade.animation = NULL;
4847
4848 switch (shell->fade.type) {
4849 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004850 weston_surface_destroy(shell->fade.view->surface);
4851 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004852 break;
4853 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004854 lock(shell);
4855 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004856 default:
4857 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004858 }
4859}
4860
Jason Ekstranda7af7042013-10-12 22:38:11 -05004861static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004862shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004863{
4864 struct weston_compositor *compositor = shell->compositor;
4865 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004866 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004867
4868 surface = weston_surface_create(compositor);
4869 if (!surface)
4870 return NULL;
4871
Jason Ekstranda7af7042013-10-12 22:38:11 -05004872 view = weston_view_create(surface);
4873 if (!view) {
4874 weston_surface_destroy(surface);
4875 return NULL;
4876 }
4877
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004878 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004879 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004880 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004881 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4882 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004883 pixman_region32_init(&surface->input);
4884
Jason Ekstranda7af7042013-10-12 22:38:11 -05004885 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004886}
4887
4888static void
4889shell_fade(struct desktop_shell *shell, enum fade_type type)
4890{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004891 float tint;
4892
4893 switch (type) {
4894 case FADE_IN:
4895 tint = 0.0;
4896 break;
4897 case FADE_OUT:
4898 tint = 1.0;
4899 break;
4900 default:
4901 weston_log("shell: invalid fade type\n");
4902 return;
4903 }
4904
4905 shell->fade.type = type;
4906
Jason Ekstranda7af7042013-10-12 22:38:11 -05004907 if (shell->fade.view == NULL) {
4908 shell->fade.view = shell_fade_create_surface(shell);
4909 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004910 return;
4911
Jason Ekstranda7af7042013-10-12 22:38:11 -05004912 shell->fade.view->alpha = 1.0 - tint;
4913 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004914 }
4915
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004916 if (shell->fade.view->output == NULL) {
4917 /* If the black view gets a NULL output, we lost the
4918 * last output and we'll just cancel the fade. This
4919 * happens when you close the last window under the
4920 * X11 or Wayland backends. */
4921 shell->locked = false;
4922 weston_surface_destroy(shell->fade.view->surface);
4923 shell->fade.view = NULL;
4924 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004925 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004926 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004927 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004928 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004929 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004930 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004931 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004932}
4933
4934static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004935do_shell_fade_startup(void *data)
4936{
4937 struct desktop_shell *shell = data;
4938
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004939 if (shell->startup_animation_type == ANIMATION_FADE)
4940 shell_fade(shell, FADE_IN);
4941 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004942 weston_surface_destroy(shell->fade.view->surface);
4943 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004944 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004945}
4946
4947static void
4948shell_fade_startup(struct desktop_shell *shell)
4949{
4950 struct wl_event_loop *loop;
4951
4952 if (!shell->fade.startup_timer)
4953 return;
4954
4955 wl_event_source_remove(shell->fade.startup_timer);
4956 shell->fade.startup_timer = NULL;
4957
4958 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4959 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4960}
4961
4962static int
4963fade_startup_timeout(void *data)
4964{
4965 struct desktop_shell *shell = data;
4966
4967 shell_fade_startup(shell);
4968 return 0;
4969}
4970
4971static void
4972shell_fade_init(struct desktop_shell *shell)
4973{
4974 /* Make compositor output all black, and wait for the desktop-shell
4975 * client to signal it is ready, then fade in. The timer triggers a
4976 * fade-in, in case the desktop-shell client takes too long.
4977 */
4978
4979 struct wl_event_loop *loop;
4980
Jason Ekstranda7af7042013-10-12 22:38:11 -05004981 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004982 weston_log("%s: warning: fade surface already exists\n",
4983 __func__);
4984 return;
4985 }
4986
Jason Ekstranda7af7042013-10-12 22:38:11 -05004987 shell->fade.view = shell_fade_create_surface(shell);
4988 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004989 return;
4990
Jason Ekstranda7af7042013-10-12 22:38:11 -05004991 weston_view_update_transform(shell->fade.view);
4992 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004993
4994 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4995 shell->fade.startup_timer =
4996 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4997 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4998}
4999
5000static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005001idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005002{
5003 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005004 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005005 struct weston_seat *seat;
5006
5007 wl_list_for_each(seat, &shell->compositor->seat_list, link)
5008 if (seat->pointer)
5009 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005010
5011 shell_fade(shell, FADE_OUT);
5012 /* lock() is called from shell_fade_done() */
5013}
5014
5015static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005016wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005017{
5018 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005019 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005020
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005021 unlock(shell);
5022}
5023
5024static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005025center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005026{
Giulio Camuffob8366642013-04-25 13:57:46 +03005027 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005028 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005029
Jason Ekstranda7af7042013-10-12 22:38:11 -05005030 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005031
5032 x = output->x + (output->width - width) / 2 - surf_x / 2;
5033 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005034
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005035 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005036}
5037
5038static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005039weston_view_set_initial_position(struct weston_view *view,
5040 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005041{
5042 struct weston_compositor *compositor = shell->compositor;
5043 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005044 int32_t range_x, range_y;
5045 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005046 struct weston_output *output, *target_output = NULL;
5047 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005048 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005049
5050 /* As a heuristic place the new window on the same output as the
5051 * pointer. Falling back to the output containing 0, 0.
5052 *
5053 * TODO: Do something clever for touch too?
5054 */
5055 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005056 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005057 ix = wl_fixed_to_int(seat->pointer->x);
5058 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005059 break;
5060 }
5061 }
5062
5063 wl_list_for_each(output, &compositor->output_list, link) {
5064 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5065 target_output = output;
5066 break;
5067 }
5068 }
5069
5070 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005071 weston_view_set_position(view, 10 + random() % 400,
5072 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005073 return;
5074 }
5075
5076 /* Valid range within output where the surface will still be onscreen.
5077 * If this is negative it means that the surface is bigger than
5078 * output.
5079 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005080 get_output_work_area(shell, target_output, &area);
5081
5082 dx = area.x;
5083 dy = area.y;
5084 range_x = area.width - view->surface->width;
5085 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005086
Rob Bradford4cb88c72012-08-13 15:18:44 +01005087 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005088 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005089
5090 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005091 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005092
5093 x = target_output->x + dx;
5094 y = target_output->y + dy;
5095
Jason Ekstranda7af7042013-10-12 22:38:11 -05005096 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005097}
5098
5099static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005100set_maximized_position(struct desktop_shell *shell,
5101 struct shell_surface *shsurf)
5102{
5103 int32_t surf_x, surf_y;
5104 pixman_rectangle32_t area;
5105
5106 /* use surface configure to set the geometry */
5107 get_output_work_area(shell, shsurf->output, &area);
5108 surface_subsurfaces_boundingbox(shsurf->surface,
5109 &surf_x, &surf_y, NULL, NULL);
5110
5111 weston_view_set_position(shsurf->view,
5112 area.x - surf_x,
5113 area.y - surf_y);
5114}
5115
5116static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005117map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005118 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005119{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005120 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005121 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005122
Pekka Paalanen77346a62011-11-30 16:26:35 +02005123 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005124 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005125 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005126 if (shsurf->state.fullscreen) {
5127 center_on_output(shsurf->view, shsurf->fullscreen_output);
5128 shell_map_fullscreen(shsurf);
5129 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005130 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005131 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005132 weston_view_set_initial_position(shsurf->view, shell);
5133 }
Juan Zhao96879df2012-02-07 08:45:41 +08005134 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005135 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005136 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005137 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005138 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005139 weston_view_set_position(shsurf->view,
5140 shsurf->view->geometry.x + sx,
5141 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005142 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005143 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005144 default:
5145 ;
5146 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005147
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005148 /* Surface stacking order, see also activate(). */
5149 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005150
Jason Ekstranda7af7042013-10-12 22:38:11 -05005151 if (shsurf->type != SHELL_SURFACE_NONE) {
5152 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005153 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005154 shsurf->surface->output = shsurf->output;
5155 shsurf->view->output = shsurf->output;
5156 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005157 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005158
Jason Ekstranda7af7042013-10-12 22:38:11 -05005159 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005160 /* XXX: xwayland's using the same fields for transient type */
5161 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005162 if (shsurf->transient.flags ==
5163 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5164 break;
5165 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005166 if (shsurf->state.relative &&
5167 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5168 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005169 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005170 break;
5171 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005172 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005173 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005174 case SHELL_SURFACE_POPUP:
5175 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005176 default:
5177 break;
5178 }
5179
Rafael Antognolli03b16592013-12-03 15:35:42 -02005180 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5181 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005182 {
5183 switch (shell->win_animation_type) {
5184 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005185 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005186 break;
5187 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005188 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005189 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005190 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005191 default:
5192 break;
5193 }
5194 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005195}
5196
5197static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005198configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005199 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005200{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005201 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005202 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005203
Pekka Paalanen77346a62011-11-30 16:26:35 +02005204 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005205
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005206 assert(shsurf);
5207
Rafael Antognolli03b16592013-12-03 15:35:42 -02005208 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005209 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005210 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005211 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005212 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005213 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005214 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005215
Alex Wu4539b082012-03-01 12:57:46 +08005216 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005217 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005218 wl_list_for_each(view, &surface->views, surface_link)
5219 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005220
Rafael Antognolli03b16592013-12-03 15:35:42 -02005221 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005222 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005223 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005224}
5225
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005226static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005227shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005228{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005229 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005230 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005231 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005232
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005233 assert(shsurf);
5234
5235 shell = shsurf->shell;
5236
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005237 if (!weston_surface_is_mapped(es) &&
5238 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005239 remove_popup_grab(shsurf);
5240 }
5241
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005242 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005243 return;
5244
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005245 if (shsurf->has_next_geometry) {
5246 shsurf->geometry = shsurf->next_geometry;
5247 shsurf->has_next_geometry = false;
5248 shsurf->has_set_geometry = true;
5249 } else if (!shsurf->has_set_geometry) {
5250 surface_subsurfaces_boundingbox(shsurf->surface,
5251 &shsurf->geometry.x,
5252 &shsurf->geometry.y,
5253 &shsurf->geometry.width,
5254 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005255 }
5256
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005257 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005258 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005259 type_changed = 1;
5260 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005261
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005262 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005263 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005264 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005265 shsurf->last_width != es->width ||
5266 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005267 float from_x, from_y;
5268 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005269
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005270 if (shsurf->resize_edges) {
5271 sx = 0;
5272 sy = 0;
5273 }
5274
5275 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5276 sx = shsurf->last_width - es->width;
5277 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5278 sy = shsurf->last_height - es->height;
5279
5280 shsurf->last_width = es->width;
5281 shsurf->last_height = es->height;
5282
Jason Ekstranda7af7042013-10-12 22:38:11 -05005283 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5284 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005285 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005286 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005287 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005288 }
5289}
5290
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005291static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005292
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005293static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005294desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005295{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005296 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005297 struct desktop_shell *shell =
5298 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005299
5300 shell->child.process.pid = 0;
5301 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005302
5303 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5304 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005305 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005306 shell->child.deathstamp = time;
5307 shell->child.deathcount = 0;
5308 }
5309
5310 shell->child.deathcount++;
5311 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005312 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005313 return;
5314 }
5315
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005316 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005317 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005318 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005319}
5320
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005321static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005322desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5323{
5324 struct desktop_shell *shell;
5325
5326 shell = container_of(listener, struct desktop_shell,
5327 child.client_destroy_listener);
5328
5329 shell->child.client = NULL;
5330}
5331
5332static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005333launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005334{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005335 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005336
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005337 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005338 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005339 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005340 desktop_shell_sigchld);
5341
5342 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005343 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005344
5345 shell->child.client_destroy_listener.notify =
5346 desktop_shell_client_destroy;
5347 wl_client_add_destroy_listener(shell->child.client,
5348 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005349}
5350
5351static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005352handle_shell_client_destroy(struct wl_listener *listener, void *data)
5353{
5354 struct shell_client *sc =
5355 container_of(listener, struct shell_client, destroy_listener);
5356
5357 if (sc->ping_timer)
5358 wl_event_source_remove(sc->ping_timer);
5359 free(sc);
5360}
5361
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005362static struct shell_client *
5363shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5364 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005365{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005366 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005367
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005368 sc = zalloc(sizeof *sc);
5369 if (sc == NULL) {
5370 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005371 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005372 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005373
5374 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005375 if (sc->resource == NULL) {
5376 free(sc);
5377 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005378 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005379 }
5380
5381 sc->client = client;
5382 sc->shell = shell;
5383 sc->destroy_listener.notify = handle_shell_client_destroy;
5384 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5385
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005386 return sc;
5387}
5388
5389static void
5390bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5391{
5392 struct desktop_shell *shell = data;
5393 struct shell_client *sc;
5394
5395 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5396 if (sc)
5397 wl_resource_set_implementation(sc->resource,
5398 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005399 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005400}
5401
5402static void
5403bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5404{
5405 struct desktop_shell *shell = data;
5406 struct shell_client *sc;
5407
5408 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5409 if (sc)
5410 wl_resource_set_dispatcher(sc->resource,
5411 xdg_shell_unversioned_dispatch,
5412 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005413}
5414
5415static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005416unbind_desktop_shell(struct wl_resource *resource)
5417{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005418 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005419
5420 if (shell->locked)
5421 resume_desktop(shell);
5422
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005423 shell->child.desktop_shell = NULL;
5424 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005425}
5426
5427static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005428bind_desktop_shell(struct wl_client *client,
5429 void *data, uint32_t version, uint32_t id)
5430{
Tiago Vignattibe143262012-04-16 17:31:41 +03005431 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005432 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005433
Jason Ekstranda85118c2013-06-27 20:17:02 -05005434 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005435 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005436
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005437 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005438 wl_resource_set_implementation(resource,
5439 &desktop_shell_implementation,
5440 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005441 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005442
5443 if (version < 2)
5444 shell_fade_startup(shell);
5445
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005446 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005447 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005448
5449 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5450 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005451}
5452
Pekka Paalanen6e168112011-11-24 11:34:05 +02005453static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005454screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005455{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005456 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005457 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005458 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005459
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005460 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005461 return;
5462
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005463 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005464 if (!shell->locked)
5465 return;
5466
Jason Ekstranda7af7042013-10-12 22:38:11 -05005467 view = container_of(surface->views.next, struct weston_view, surface_link);
5468 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005469
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005470 if (wl_list_empty(&view->layer_link.link)) {
5471 prev = container_of(shell->lock_layer.view_list.link.prev,
5472 struct weston_layer_entry, link);
5473 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005474 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005475 wl_event_source_timer_update(shell->screensaver.timer,
5476 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005477 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005478 }
5479}
5480
5481static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005482screensaver_set_surface(struct wl_client *client,
5483 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005484 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005485 struct wl_resource *output_resource)
5486{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005487 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005488 struct weston_surface *surface =
5489 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005490 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005491 struct weston_view *view, *next;
5492
5493 /* Make sure we only have one view */
5494 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5495 weston_view_destroy(view);
5496 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005497
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005498 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005499 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005500 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005501}
5502
5503static const struct screensaver_interface screensaver_implementation = {
5504 screensaver_set_surface
5505};
5506
5507static void
5508unbind_screensaver(struct wl_resource *resource)
5509{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005510 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005511
Pekka Paalanen77346a62011-11-30 16:26:35 +02005512 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005513}
5514
5515static void
5516bind_screensaver(struct wl_client *client,
5517 void *data, uint32_t version, uint32_t id)
5518{
Tiago Vignattibe143262012-04-16 17:31:41 +03005519 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005520 struct wl_resource *resource;
5521
Jason Ekstranda85118c2013-06-27 20:17:02 -05005522 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005523
Pekka Paalanen77346a62011-11-30 16:26:35 +02005524 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005525 wl_resource_set_implementation(resource,
5526 &screensaver_implementation,
5527 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005528 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005529 return;
5530 }
5531
5532 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5533 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005534}
5535
Kristian Høgsberg07045392012-02-19 18:52:44 -05005536struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005537 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005538 struct weston_surface *current;
5539 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005540 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005541 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005542};
5543
5544static void
5545switcher_next(struct switcher *switcher)
5546{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005547 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005548 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005549 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005550 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005551
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005552 /* temporary re-display minimized surfaces */
5553 struct weston_view *tmp;
5554 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005555 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5556 weston_layer_entry_remove(&view->layer_link);
5557 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005558 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5559 *minimized = view;
5560 }
5561
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005562 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005563 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005564 if (shsurf &&
5565 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5566 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005567 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005568 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005569 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005570 next = view->surface;
5571 prev = view->surface;
5572 view->alpha = 0.25;
5573 weston_view_geometry_dirty(view);
5574 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005575 }
Alex Wu1659daa2012-04-01 20:13:09 +08005576
Jason Ekstranda7af7042013-10-12 22:38:11 -05005577 if (is_black_surface(view->surface, NULL)) {
5578 view->alpha = 0.25;
5579 weston_view_geometry_dirty(view);
5580 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005581 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005582 }
5583
5584 if (next == NULL)
5585 next = first;
5586
Alex Wu07b26062012-03-12 16:06:01 +08005587 if (next == NULL)
5588 return;
5589
Kristian Høgsberg07045392012-02-19 18:52:44 -05005590 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005591 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005592
5593 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005594 wl_list_for_each(view, &next->views, surface_link)
5595 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005596
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005597 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005598 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005599 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005600}
5601
5602static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005603switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005604{
5605 struct switcher *switcher =
5606 container_of(listener, struct switcher, listener);
5607
5608 switcher_next(switcher);
5609}
5610
5611static void
Daniel Stone351eb612012-05-31 15:27:47 -04005612switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005613{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005614 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005615 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005616 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005617
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005618 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005619 if (is_focus_view(view))
5620 continue;
5621
Jason Ekstranda7af7042013-10-12 22:38:11 -05005622 view->alpha = 1.0;
5623 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005624 }
5625
Alex Wu07b26062012-03-12 16:06:01 +08005626 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005627 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005628 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005629 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005630 weston_keyboard_end_grab(keyboard);
5631 if (keyboard->input_method_resource)
5632 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005633
5634 /* re-hide surfaces that were temporary shown during the switch */
5635 struct weston_view **minimized;
5636 wl_array_for_each(minimized, &switcher->minimized_array) {
5637 /* with the exception of the current selected */
5638 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005639 weston_layer_entry_remove(&(*minimized)->layer_link);
5640 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005641 weston_view_damage_below(*minimized);
5642 }
5643 }
5644 wl_array_release(&switcher->minimized_array);
5645
Kristian Høgsberg07045392012-02-19 18:52:44 -05005646 free(switcher);
5647}
5648
5649static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005650switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005651 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005652{
5653 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005654 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005655
Daniel Stonec9785ea2012-05-30 16:31:52 +01005656 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005657 switcher_next(switcher);
5658}
5659
5660static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005661switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005662 uint32_t mods_depressed, uint32_t mods_latched,
5663 uint32_t mods_locked, uint32_t group)
5664{
5665 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005666 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005667
Daniel Stone351eb612012-05-31 15:27:47 -04005668 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5669 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005670}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005671
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005672static void
5673switcher_cancel(struct weston_keyboard_grab *grab)
5674{
5675 struct switcher *switcher = container_of(grab, struct switcher, grab);
5676
5677 switcher_destroy(switcher);
5678}
5679
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005680static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005681 switcher_key,
5682 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005683 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005684};
5685
5686static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005687switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005688 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005689{
Tiago Vignattibe143262012-04-16 17:31:41 +03005690 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005691 struct switcher *switcher;
5692
5693 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005694 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005695 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005696 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005697 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005698 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005699
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005700 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005701 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005702 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005703 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5704 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005705 switcher_next(switcher);
5706}
5707
Pekka Paalanen3c647232011-12-22 13:43:43 +02005708static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005709backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005710 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005711{
5712 struct weston_compositor *compositor = data;
5713 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005714 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005715
5716 /* TODO: we're limiting to simple use cases, where we assume just
5717 * control on the primary display. We'd have to extend later if we
5718 * ever get support for setting backlights on random desktop LCD
5719 * panels though */
5720 output = get_default_output(compositor);
5721 if (!output)
5722 return;
5723
5724 if (!output->set_backlight)
5725 return;
5726
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005727 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5728 backlight_new = output->backlight_current - 25;
5729 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5730 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005731
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005732 if (backlight_new < 5)
5733 backlight_new = 5;
5734 if (backlight_new > 255)
5735 backlight_new = 255;
5736
5737 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005738 output->set_backlight(output, output->backlight_current);
5739}
5740
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005741struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005742 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005743 struct weston_seat *seat;
5744 uint32_t key[2];
5745 int key_released[2];
5746};
5747
5748static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005749debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005750 uint32_t key, uint32_t state)
5751{
5752 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005753 struct weston_compositor *ec = db->seat->compositor;
5754 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005755 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005756 uint32_t serial;
5757 int send = 0, terminate = 0;
5758 int check_binding = 1;
5759 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005760 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005761
5762 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5763 /* Do not run bindings on key releases */
5764 check_binding = 0;
5765
5766 for (i = 0; i < 2; i++)
5767 if (key == db->key[i])
5768 db->key_released[i] = 1;
5769
5770 if (db->key_released[0] && db->key_released[1]) {
5771 /* All key releases been swalled so end the grab */
5772 terminate = 1;
5773 } else if (key != db->key[0] && key != db->key[1]) {
5774 /* Should not swallow release of other keys */
5775 send = 1;
5776 }
5777 } else if (key == db->key[0] && !db->key_released[0]) {
5778 /* Do not check bindings for the first press of the binding
5779 * key. This allows it to be used as a debug shortcut.
5780 * We still need to swallow this event. */
5781 check_binding = 0;
5782 } else if (db->key[1]) {
5783 /* If we already ran a binding don't process another one since
5784 * we can't keep track of all the binding keys that were
5785 * pressed in order to swallow the release events. */
5786 send = 1;
5787 check_binding = 0;
5788 }
5789
5790 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005791 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5792 key, state)) {
5793 /* We ran a binding so swallow the press and keep the
5794 * grab to swallow the released too. */
5795 send = 0;
5796 terminate = 0;
5797 db->key[1] = key;
5798 } else {
5799 /* Terminate the grab since the key pressed is not a
5800 * debug binding key. */
5801 send = 1;
5802 terminate = 1;
5803 }
5804 }
5805
5806 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005807 serial = wl_display_next_serial(display);
5808 resource_list = &grab->keyboard->focus_resource_list;
5809 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005810 wl_keyboard_send_key(resource, serial, time, key, state);
5811 }
5812 }
5813
5814 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005815 weston_keyboard_end_grab(grab->keyboard);
5816 if (grab->keyboard->input_method_resource)
5817 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005818 free(db);
5819 }
5820}
5821
5822static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005823debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005824 uint32_t mods_depressed, uint32_t mods_latched,
5825 uint32_t mods_locked, uint32_t group)
5826{
5827 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005828 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005829
Neil Roberts96d790e2013-09-19 17:32:00 +01005830 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005831
Neil Roberts96d790e2013-09-19 17:32:00 +01005832 wl_resource_for_each(resource, resource_list) {
5833 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5834 mods_latched, mods_locked, group);
5835 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005836}
5837
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005838static void
5839debug_binding_cancel(struct weston_keyboard_grab *grab)
5840{
5841 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5842
5843 weston_keyboard_end_grab(grab->keyboard);
5844 free(db);
5845}
5846
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005847struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005848 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005849 debug_binding_modifiers,
5850 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005851};
5852
5853static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005854debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005855{
5856 struct debug_binding_grab *grab;
5857
5858 grab = calloc(1, sizeof *grab);
5859 if (!grab)
5860 return;
5861
5862 grab->seat = (struct weston_seat *) seat;
5863 grab->key[0] = key;
5864 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005865 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005866}
5867
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005868static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005869force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005870 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005871{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005872 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005873 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005874 struct desktop_shell *shell = data;
5875 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005876 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005877
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005878 focus_surface = seat->keyboard->focus;
5879 if (!focus_surface)
5880 return;
5881
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005882 wl_signal_emit(&compositor->kill_signal, focus_surface);
5883
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005884 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005885 wl_client_get_credentials(client, &pid, NULL, NULL);
5886
5887 /* Skip clients that we launched ourselves (the credentials of
5888 * the socketpair is ours) */
5889 if (pid == getpid())
5890 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005891
Daniel Stone325fc2d2012-05-30 16:31:58 +01005892 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005893}
5894
5895static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005896workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005897 uint32_t key, void *data)
5898{
5899 struct desktop_shell *shell = data;
5900 unsigned int new_index = shell->workspaces.current;
5901
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005902 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005903 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005904 if (new_index != 0)
5905 new_index--;
5906
5907 change_workspace(shell, new_index);
5908}
5909
5910static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005911workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005912 uint32_t key, void *data)
5913{
5914 struct desktop_shell *shell = data;
5915 unsigned int new_index = shell->workspaces.current;
5916
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005917 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005918 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005919 if (new_index < shell->workspaces.num - 1)
5920 new_index++;
5921
5922 change_workspace(shell, new_index);
5923}
5924
5925static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005926workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005927 uint32_t key, void *data)
5928{
5929 struct desktop_shell *shell = data;
5930 unsigned int new_index;
5931
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005932 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005933 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005934 new_index = key - KEY_F1;
5935 if (new_index >= shell->workspaces.num)
5936 new_index = shell->workspaces.num - 1;
5937
5938 change_workspace(shell, new_index);
5939}
5940
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005941static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005942workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005943 uint32_t key, void *data)
5944{
5945 struct desktop_shell *shell = data;
5946 unsigned int new_index = shell->workspaces.current;
5947
5948 if (shell->locked)
5949 return;
5950
5951 if (new_index != 0)
5952 new_index--;
5953
5954 take_surface_to_workspace_by_seat(shell, seat, new_index);
5955}
5956
5957static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005958workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005959 uint32_t key, void *data)
5960{
5961 struct desktop_shell *shell = data;
5962 unsigned int new_index = shell->workspaces.current;
5963
5964 if (shell->locked)
5965 return;
5966
5967 if (new_index < shell->workspaces.num - 1)
5968 new_index++;
5969
5970 take_surface_to_workspace_by_seat(shell, seat, new_index);
5971}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005972
5973static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005974shell_reposition_view_on_output_destroy(struct weston_view *view)
5975{
5976 struct weston_output *output, *first_output;
5977 struct weston_compositor *ec = view->surface->compositor;
5978 struct shell_surface *shsurf;
5979 float x, y;
5980 int visible;
5981
5982 x = view->geometry.x;
5983 y = view->geometry.y;
5984
5985 /* At this point the destroyed output is not in the list anymore.
5986 * If the view is still visible somewhere, we leave where it is,
5987 * otherwise, move it to the first output. */
5988 visible = 0;
5989 wl_list_for_each(output, &ec->output_list, link) {
5990 if (pixman_region32_contains_point(&output->region,
5991 x, y, NULL)) {
5992 visible = 1;
5993 break;
5994 }
5995 }
5996
5997 if (!visible) {
5998 first_output = container_of(ec->output_list.next,
5999 struct weston_output, link);
6000
6001 x = first_output->x + first_output->width / 4;
6002 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006003
6004 weston_view_set_position(view, x, y);
6005 } else {
6006 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006007 }
6008
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006009
6010 shsurf = get_shell_surface(view->surface);
6011
6012 if (shsurf) {
6013 shsurf->saved_position_valid = false;
6014 shsurf->next_state.maximized = false;
6015 shsurf->next_state.fullscreen = false;
6016 shsurf->state_changed = true;
6017 }
6018}
6019
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006020void
6021shell_for_each_layer(struct desktop_shell *shell,
6022 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006023{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006024 struct workspace **ws;
6025
6026 func(shell, &shell->fullscreen_layer, data);
6027 func(shell, &shell->panel_layer, data);
6028 func(shell, &shell->background_layer, data);
6029 func(shell, &shell->lock_layer, data);
6030 func(shell, &shell->input_panel_layer, data);
6031
6032 wl_array_for_each(ws, &shell->workspaces.array)
6033 func(shell, &(*ws)->layer, data);
6034}
6035
6036static void
6037shell_output_destroy_move_layer(struct desktop_shell *shell,
6038 struct weston_layer *layer,
6039 void *data)
6040{
6041 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006042 struct weston_view *view;
6043
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006044 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006045 if (view->output != output)
6046 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006047
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006048 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006049 }
6050}
6051
6052static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006053handle_output_destroy(struct wl_listener *listener, void *data)
6054{
6055 struct shell_output *output_listener =
6056 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006057 struct weston_output *output = output_listener->output;
6058 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006059
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006060 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006061
Xiong Zhang6b481422013-10-23 13:58:32 +08006062 wl_list_remove(&output_listener->destroy_listener.link);
6063 wl_list_remove(&output_listener->link);
6064 free(output_listener);
6065}
6066
6067static void
6068create_shell_output(struct desktop_shell *shell,
6069 struct weston_output *output)
6070{
6071 struct shell_output *shell_output;
6072
6073 shell_output = zalloc(sizeof *shell_output);
6074 if (shell_output == NULL)
6075 return;
6076
6077 shell_output->output = output;
6078 shell_output->shell = shell;
6079 shell_output->destroy_listener.notify = handle_output_destroy;
6080 wl_signal_add(&output->destroy_signal,
6081 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006082 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006083}
6084
6085static void
6086handle_output_create(struct wl_listener *listener, void *data)
6087{
6088 struct desktop_shell *shell =
6089 container_of(listener, struct desktop_shell, output_create_listener);
6090 struct weston_output *output = (struct weston_output *)data;
6091
6092 create_shell_output(shell, output);
6093}
6094
6095static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006096handle_output_move_layer(struct desktop_shell *shell,
6097 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006098{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006099 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006100 struct weston_view *view;
6101 float x, y;
6102
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006103 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006104 if (view->output != output)
6105 continue;
6106
6107 x = view->geometry.x + output->move_x;
6108 y = view->geometry.y + output->move_y;
6109 weston_view_set_position(view, x, y);
6110 }
6111}
6112
6113static void
6114handle_output_move(struct wl_listener *listener, void *data)
6115{
6116 struct desktop_shell *shell;
6117
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006118 shell = container_of(listener, struct desktop_shell,
6119 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006120
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006121 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006122}
6123
6124static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006125setup_output_destroy_handler(struct weston_compositor *ec,
6126 struct desktop_shell *shell)
6127{
6128 struct weston_output *output;
6129
6130 wl_list_init(&shell->output_list);
6131 wl_list_for_each(output, &ec->output_list, link)
6132 create_shell_output(shell, output);
6133
6134 shell->output_create_listener.notify = handle_output_create;
6135 wl_signal_add(&ec->output_created_signal,
6136 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006137
6138 shell->output_move_listener.notify = handle_output_move;
6139 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006140}
6141
6142static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006143shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006144{
Tiago Vignattibe143262012-04-16 17:31:41 +03006145 struct desktop_shell *shell =
6146 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006147 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006148 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006149
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006150 /* Force state to unlocked so we don't try to fade */
6151 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006152 if (shell->child.client)
6153 wl_client_destroy(shell->child.client);
6154
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006155 wl_list_remove(&shell->idle_listener.link);
6156 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006157
6158 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006159
Xiong Zhang6b481422013-10-23 13:58:32 +08006160 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6161 wl_list_remove(&shell_output->destroy_listener.link);
6162 wl_list_remove(&shell_output->link);
6163 free(shell_output);
6164 }
6165
6166 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006167 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006168
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006169 wl_array_for_each(ws, &shell->workspaces.array)
6170 workspace_destroy(*ws);
6171 wl_array_release(&shell->workspaces.array);
6172
Pekka Paalanen3c647232011-12-22 13:43:43 +02006173 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006174 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006175 free(shell);
6176}
6177
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006178static void
6179shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6180{
6181 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006182 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006183
6184 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006185 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6186 MODIFIER_CTRL | MODIFIER_ALT,
6187 terminate_binding, ec);
6188 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6189 click_to_activate_binding,
6190 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006191 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6192 click_to_activate_binding,
6193 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006194 weston_compositor_add_touch_binding(ec, 0,
6195 touch_to_activate_binding,
6196 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006197 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6198 MODIFIER_SUPER | MODIFIER_ALT,
6199 surface_opacity_binding, NULL);
6200 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6201 MODIFIER_SUPER, zoom_axis_binding,
6202 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006203
6204 /* configurable bindings */
6205 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006206 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6207 zoom_key_binding, NULL);
6208 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6209 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006210 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6211 maximize_binding, NULL);
6212 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6213 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006214 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6215 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006216 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006217 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6218 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006219 weston_compositor_add_button_binding(ec, BTN_LEFT,
6220 mod | MODIFIER_SHIFT,
6221 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006222
6223 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6224 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6225 rotate_binding, NULL);
6226
Daniel Stone325fc2d2012-05-30 16:31:58 +01006227 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6228 shell);
6229 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6230 ec);
6231 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6232 backlight_binding, ec);
6233 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6234 ec);
6235 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6236 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006237 weston_compositor_add_key_binding(ec, KEY_K, mod,
6238 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006239 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6240 workspace_up_binding, shell);
6241 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6242 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006243 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6244 workspace_move_surface_up_binding,
6245 shell);
6246 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6247 workspace_move_surface_down_binding,
6248 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006249
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006250 if (shell->exposay_modifier)
6251 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6252 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006253
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006254 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6255 if (shell->workspaces.num > 1) {
6256 num_workspace_bindings = shell->workspaces.num;
6257 if (num_workspace_bindings > 6)
6258 num_workspace_bindings = 6;
6259 for (i = 0; i < num_workspace_bindings; i++)
6260 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6261 workspace_f_binding,
6262 shell);
6263 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006264
6265 /* Debug bindings */
6266 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6267 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006268}
6269
Jason Ekstrand024177c2014-04-21 19:42:58 -05006270static void
6271handle_seat_created(struct wl_listener *listener, void *data)
6272{
6273 struct weston_seat *seat = data;
6274
6275 create_shell_seat(seat);
6276}
6277
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006278WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006279module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006280 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006281{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006282 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006283 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006284 struct workspace **pws;
6285 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006286 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006287
Peter Huttererf3d62272013-08-08 11:57:05 +10006288 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006289 if (shell == NULL)
6290 return -1;
6291
Kristian Høgsberg75840622011-09-06 13:48:16 -04006292 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006293
6294 shell->destroy_listener.notify = shell_destroy;
6295 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006296 shell->idle_listener.notify = idle_handler;
6297 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6298 shell->wake_listener.notify = wake_handler;
6299 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006300
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006301 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006302 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006303 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006304 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006305 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006306 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006307 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006308 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006309 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006310 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006311 ec->shell_interface.set_window_geometry = set_window_geometry;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006312
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006313 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6314 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006315 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6316 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006317 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006318
6319 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006320 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006321
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006322 if (input_panel_setup(shell) < 0)
6323 return -1;
6324
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006325 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006326
Daniel Stonedf8133b2013-11-19 11:37:14 +01006327 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6328 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6329
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006330 for (i = 0; i < shell->workspaces.num; i++) {
6331 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6332 if (pws == NULL)
6333 return -1;
6334
6335 *pws = workspace_create();
6336 if (*pws == NULL)
6337 return -1;
6338 }
6339 activate_workspace(shell, 0);
6340
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006341 weston_layer_init(&shell->minimized_layer, NULL);
6342
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006343 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006344 wl_list_init(&shell->workspaces.animation.link);
6345 shell->workspaces.animation.frame = animate_workspace_change_frame;
6346
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006347 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006348 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006349 return -1;
6350
Rafael Antognollie2a34552013-12-03 15:35:45 -02006351 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6352 shell, bind_xdg_shell) == NULL)
6353 return -1;
6354
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006355 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006356 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006357 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006358 return -1;
6359
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006360 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6361 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006362 return -1;
6363
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006364 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6365 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006366 return -1;
6367
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006368 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006369
Jonny Lamb765760d2014-08-20 15:53:19 +02006370 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6371
Xiong Zhang6b481422013-10-23 13:58:32 +08006372 setup_output_destroy_handler(ec, shell);
6373
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006374 loop = wl_display_get_event_loop(ec->wl_display);
6375 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006376
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006377 shell->screensaver.timer =
6378 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6379
Jason Ekstrand024177c2014-04-21 19:42:58 -05006380 wl_list_for_each(seat, &ec->seat_list, link)
6381 handle_seat_created(NULL, seat);
6382 shell->seat_create_listener.notify = handle_seat_created;
6383 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006384
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006385 screenshooter_create(ec);
6386
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006387 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006388
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006389 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006390
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006391 return 0;
6392}