blob: c21d3645ae6cfe0af1cd7a628310a9603b4b701d [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;
Derek Foremanc7210432014-08-21 11:32:38 -0500570 char *s, *client;
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);
Derek Foremanc7210432014-08-21 11:32:38 -0500581 asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
582 WESTON_SHELL_CLIENT);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400583 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500584 "client", &s, client);
585 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100586 shell->client = s;
587 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400588 "binding-modifier", &s, "super");
589 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200590 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800591
592 weston_config_section_get_string(section,
593 "exposay-modifier", &s, "none");
594 if (strcmp(s, "none") == 0)
595 shell->exposay_modifier = 0;
596 else
597 shell->exposay_modifier = get_modifier(s);
598 free(s);
599
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400600 weston_config_section_get_string(section, "animation", &s, "none");
601 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200602 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200603 weston_config_section_get_string(section, "close-animation", &s, "fade");
604 shell->win_close_animation_type = get_animation_type(s);
605 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700606 weston_config_section_get_string(section,
607 "startup-animation", &s, "fade");
608 shell->startup_animation_type = get_animation_type(s);
609 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700610 if (shell->startup_animation_type == ANIMATION_ZOOM)
611 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100612 weston_config_section_get_string(section, "focus-animation", &s, "none");
613 shell->focus_animation_type = get_animation_type(s);
614 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400615 weston_config_section_get_uint(section, "num-workspaces",
616 &shell->workspaces.num,
617 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200618}
619
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800620struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100621get_default_output(struct weston_compositor *compositor)
622{
623 return container_of(compositor->output_list.next,
624 struct weston_output, link);
625}
626
627
628/* no-op func for checking focus surface */
629static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600630focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100631{
632}
633
634static struct focus_surface *
635get_focus_surface(struct weston_surface *surface)
636{
637 if (surface->configure == focus_surface_configure)
638 return surface->configure_private;
639 else
640 return NULL;
641}
642
643static bool
644is_focus_surface (struct weston_surface *es)
645{
646 return (es->configure == focus_surface_configure);
647}
648
649static bool
650is_focus_view (struct weston_view *view)
651{
652 return is_focus_surface (view->surface);
653}
654
655static struct focus_surface *
656create_focus_surface(struct weston_compositor *ec,
657 struct weston_output *output)
658{
659 struct focus_surface *fsurf = NULL;
660 struct weston_surface *surface = NULL;
661
662 fsurf = malloc(sizeof *fsurf);
663 if (!fsurf)
664 return NULL;
665
666 fsurf->surface = weston_surface_create(ec);
667 surface = fsurf->surface;
668 if (surface == NULL) {
669 free(fsurf);
670 return NULL;
671 }
672
673 surface->configure = focus_surface_configure;
674 surface->output = output;
675 surface->configure_private = fsurf;
676
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800677 fsurf->view = weston_view_create(surface);
678 if (fsurf->view == NULL) {
679 weston_surface_destroy(surface);
680 free(fsurf);
681 return NULL;
682 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100683 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100684
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600685 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600686 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100687 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
688 pixman_region32_fini(&surface->opaque);
689 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
690 output->width, output->height);
691 pixman_region32_fini(&surface->input);
692 pixman_region32_init(&surface->input);
693
694 wl_list_init(&fsurf->workspace_transform.link);
695
696 return fsurf;
697}
698
699static void
700focus_surface_destroy(struct focus_surface *fsurf)
701{
702 weston_surface_destroy(fsurf->surface);
703 free(fsurf);
704}
705
706static void
707focus_animation_done(struct weston_view_animation *animation, void *data)
708{
709 struct workspace *ws = data;
710
711 ws->focus_animation = NULL;
712}
713
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200714static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200715focus_state_destroy(struct focus_state *state)
716{
717 wl_list_remove(&state->seat_destroy_listener.link);
718 wl_list_remove(&state->surface_destroy_listener.link);
719 free(state);
720}
721
722static void
723focus_state_seat_destroy(struct wl_listener *listener, void *data)
724{
725 struct focus_state *state = container_of(listener,
726 struct focus_state,
727 seat_destroy_listener);
728
729 wl_list_remove(&state->link);
730 focus_state_destroy(state);
731}
732
733static void
734focus_state_surface_destroy(struct wl_listener *listener, void *data)
735{
736 struct focus_state *state = container_of(listener,
737 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400738 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400739 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500740 struct weston_surface *main_surface, *next;
741 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200742
Pekka Paalanen01388e22013-04-25 13:57:44 +0300743 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
744
Kristian Høgsberge3778222012-07-31 17:29:30 -0400745 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300746 wl_list_for_each(view,
747 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500748 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400749 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100750 if (is_focus_view(view))
751 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400752
Jason Ekstranda7af7042013-10-12 22:38:11 -0500753 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400754 break;
755 }
756
Pekka Paalanen01388e22013-04-25 13:57:44 +0300757 /* if the focus was a sub-surface, activate its main surface */
758 if (main_surface != state->keyboard_focus)
759 next = main_surface;
760
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100761 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400762 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100763 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100764 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400765 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100766 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
767 if (state->ws->focus_animation)
768 weston_view_animation_destroy(state->ws->focus_animation);
769
770 state->ws->focus_animation = weston_fade_run(
771 state->ws->fsurf_front->view,
772 state->ws->fsurf_front->view->alpha, 0.0, 300,
773 focus_animation_done, state->ws);
774 }
775
Kristian Høgsberge3778222012-07-31 17:29:30 -0400776 wl_list_remove(&state->link);
777 focus_state_destroy(state);
778 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200779}
780
781static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400782focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200783{
Jonas Ådahl04769742012-06-13 00:01:24 +0200784 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200785
786 state = malloc(sizeof *state);
787 if (state == NULL)
788 return NULL;
789
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100790 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400791 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200792 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400793 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200794
795 state->seat_destroy_listener.notify = focus_state_seat_destroy;
796 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400797 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200798 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400799 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200800
801 return state;
802}
803
Jonas Ådahl8538b222012-08-29 22:13:03 +0200804static struct focus_state *
805ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
806{
807 struct workspace *ws = get_current_workspace(shell);
808 struct focus_state *state;
809
810 wl_list_for_each(state, &ws->focus_list, link)
811 if (state->seat == seat)
812 break;
813
814 if (&state->link == &ws->focus_list)
815 state = focus_state_create(seat, ws);
816
817 return state;
818}
819
Jonas Ådahl04769742012-06-13 00:01:24 +0200820static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800821focus_state_set_focus(struct focus_state *state,
822 struct weston_surface *surface)
823{
824 if (state->keyboard_focus) {
825 wl_list_remove(&state->surface_destroy_listener.link);
826 wl_list_init(&state->surface_destroy_listener.link);
827 }
828
829 state->keyboard_focus = surface;
830 if (surface)
831 wl_signal_add(&surface->destroy_signal,
832 &state->surface_destroy_listener);
833}
834
835static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400836restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200837{
838 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400839 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100840 struct wl_list pending_seat_list;
841 struct weston_seat *seat, *next_seat;
842
843 /* Temporarily steal the list of seats so that we can keep
844 * track of the seats we've already processed */
845 wl_list_init(&pending_seat_list);
846 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
847 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200848
849 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100850 wl_list_remove(&state->seat->link);
851 wl_list_insert(&shell->compositor->seat_list,
852 &state->seat->link);
853
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800854 if (state->seat->keyboard == NULL)
855 continue;
856
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400857 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200858
Kristian Høgsberge3148752013-05-06 23:19:49 -0400859 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200860 }
Neil Roberts4237f502014-04-09 16:33:31 +0100861
862 /* For any remaining seats that we don't have a focus state
863 * for we'll reset the keyboard focus to NULL */
864 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
865 wl_list_insert(&shell->compositor->seat_list, &seat->link);
866
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300867 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100868 continue;
869
870 weston_keyboard_set_focus(seat->keyboard, NULL);
871 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200872}
873
874static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200875replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
876 struct weston_seat *seat)
877{
878 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200879
880 wl_list_for_each(state, &ws->focus_list, link) {
881 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800882 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200883 return;
884 }
885 }
886}
887
888static void
889drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
890 struct weston_surface *surface)
891{
892 struct focus_state *state;
893
894 wl_list_for_each(state, &ws->focus_list, link)
895 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800896 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200897}
898
899static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100900animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
901 struct weston_view *from, struct weston_view *to)
902{
903 struct weston_output *output;
904 bool focus_surface_created = false;
905
906 /* FIXME: Only support dim animation using two layers */
907 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
908 return;
909
910 output = get_default_output(shell->compositor);
911 if (ws->fsurf_front == NULL && (from || to)) {
912 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800913 if (ws->fsurf_front == NULL)
914 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100915 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800916
917 ws->fsurf_back = create_focus_surface(shell->compositor, output);
918 if (ws->fsurf_back == NULL) {
919 focus_surface_destroy(ws->fsurf_front);
920 return;
921 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100922 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800923
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100924 focus_surface_created = true;
925 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300926 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
927 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100928 }
929
930 if (ws->focus_animation) {
931 weston_view_animation_destroy(ws->focus_animation);
932 ws->focus_animation = NULL;
933 }
934
935 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300936 weston_layer_entry_insert(&to->layer_link,
937 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100938 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300939 weston_layer_entry_insert(&ws->layer.view_list,
940 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100941
942 if (focus_surface_created) {
943 ws->focus_animation = weston_fade_run(
944 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200945 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100946 focus_animation_done, ws);
947 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300948 weston_layer_entry_insert(&from->layer_link,
949 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100950 ws->focus_animation = weston_stable_fade_run(
951 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200952 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100953 focus_animation_done, ws);
954 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300955 weston_layer_entry_insert(&ws->layer.view_list,
956 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100957 ws->focus_animation = weston_stable_fade_run(
958 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200959 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100960 focus_animation_done, ws);
961 }
962}
963
964static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200965workspace_destroy(struct workspace *ws)
966{
Jonas Ådahl04769742012-06-13 00:01:24 +0200967 struct focus_state *state, *next;
968
969 wl_list_for_each_safe(state, next, &ws->focus_list, link)
970 focus_state_destroy(state);
971
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100972 if (ws->fsurf_front)
973 focus_surface_destroy(ws->fsurf_front);
974 if (ws->fsurf_back)
975 focus_surface_destroy(ws->fsurf_back);
976
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200977 free(ws);
978}
979
Jonas Ådahl04769742012-06-13 00:01:24 +0200980static void
981seat_destroyed(struct wl_listener *listener, void *data)
982{
983 struct weston_seat *seat = data;
984 struct focus_state *state, *next;
985 struct workspace *ws = container_of(listener,
986 struct workspace,
987 seat_destroyed_listener);
988
989 wl_list_for_each_safe(state, next, &ws->focus_list, link)
990 if (state->seat == seat)
991 wl_list_remove(&state->link);
992}
993
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200994static struct workspace *
995workspace_create(void)
996{
997 struct workspace *ws = malloc(sizeof *ws);
998 if (ws == NULL)
999 return NULL;
1000
1001 weston_layer_init(&ws->layer, NULL);
1002
Jonas Ådahl04769742012-06-13 00:01:24 +02001003 wl_list_init(&ws->focus_list);
1004 wl_list_init(&ws->seat_destroyed_listener.link);
1005 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001006 ws->fsurf_front = NULL;
1007 ws->fsurf_back = NULL;
1008 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001009
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001010 return ws;
1011}
1012
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013static int
1014workspace_is_empty(struct workspace *ws)
1015{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001016 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001017}
1018
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001019static struct workspace *
1020get_workspace(struct desktop_shell *shell, unsigned int index)
1021{
1022 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001023 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001024 pws += index;
1025 return *pws;
1026}
1027
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001028struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001029get_current_workspace(struct desktop_shell *shell)
1030{
1031 return get_workspace(shell, shell->workspaces.current);
1032}
1033
1034static void
1035activate_workspace(struct desktop_shell *shell, unsigned int index)
1036{
1037 struct workspace *ws;
1038
1039 ws = get_workspace(shell, index);
1040 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1041
1042 shell->workspaces.current = index;
1043}
1044
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001045static unsigned int
1046get_output_height(struct weston_output *output)
1047{
1048 return abs(output->region.extents.y1 - output->region.extents.y2);
1049}
1050
1051static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001052view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001053{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001054 struct weston_transform *transform;
1055
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001056 if (is_focus_view(view)) {
1057 struct focus_surface *fsurf = get_focus_surface(view->surface);
1058 transform = &fsurf->workspace_transform;
1059 } else {
1060 struct shell_surface *shsurf = get_shell_surface(view->surface);
1061 transform = &shsurf->workspace_transform;
1062 }
1063
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001064 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001065 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001066 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001067
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001068 weston_matrix_init(&transform->matrix);
1069 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001070 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001071 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001072}
1073
1074static void
1075workspace_translate_out(struct workspace *ws, double fraction)
1076{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001077 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001078 unsigned int height;
1079 double d;
1080
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001081 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001083 d = height * fraction;
1084
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001085 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001086 }
1087}
1088
1089static void
1090workspace_translate_in(struct workspace *ws, double fraction)
1091{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001092 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001093 unsigned int height;
1094 double d;
1095
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001096 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001097 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098
1099 if (fraction > 0)
1100 d = -(height - height * fraction);
1101 else
1102 d = height + height * fraction;
1103
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001104 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105 }
1106}
1107
1108static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001109broadcast_current_workspace_state(struct desktop_shell *shell)
1110{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001111 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001112
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001113 wl_resource_for_each(resource, &shell->workspaces.client_list)
1114 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001115 shell->workspaces.current,
1116 shell->workspaces.num);
1117}
1118
1119static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120reverse_workspace_change_animation(struct desktop_shell *shell,
1121 unsigned int index,
1122 struct workspace *from,
1123 struct workspace *to)
1124{
1125 shell->workspaces.current = index;
1126
1127 shell->workspaces.anim_to = to;
1128 shell->workspaces.anim_from = from;
1129 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1130 shell->workspaces.anim_timestamp = 0;
1131
Scott Moreau4272e632012-08-13 09:58:41 -06001132 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133}
1134
1135static void
1136workspace_deactivate_transforms(struct workspace *ws)
1137{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001139 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001140
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001141 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001142 if (is_focus_view(view)) {
1143 struct focus_surface *fsurf = get_focus_surface(view->surface);
1144 transform = &fsurf->workspace_transform;
1145 } else {
1146 struct shell_surface *shsurf = get_shell_surface(view->surface);
1147 transform = &shsurf->workspace_transform;
1148 }
1149
1150 if (!wl_list_empty(&transform->link)) {
1151 wl_list_remove(&transform->link);
1152 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001153 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155 }
1156}
1157
1158static void
1159finish_workspace_change_animation(struct desktop_shell *shell,
1160 struct workspace *from,
1161 struct workspace *to)
1162{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001163 struct weston_view *view;
1164
Scott Moreau4272e632012-08-13 09:58:41 -06001165 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001166
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001167 /* Views that extend past the bottom of the output are still
1168 * visible after the workspace animation ends but before its layer
1169 * is hidden. In that case, we need to damage below those views so
1170 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001171 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001172 weston_view_damage_below(view);
1173
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001174 wl_list_remove(&shell->workspaces.animation.link);
1175 workspace_deactivate_transforms(from);
1176 workspace_deactivate_transforms(to);
1177 shell->workspaces.anim_to = NULL;
1178
1179 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1180}
1181
1182static void
1183animate_workspace_change_frame(struct weston_animation *animation,
1184 struct weston_output *output, uint32_t msecs)
1185{
1186 struct desktop_shell *shell =
1187 container_of(animation, struct desktop_shell,
1188 workspaces.animation);
1189 struct workspace *from = shell->workspaces.anim_from;
1190 struct workspace *to = shell->workspaces.anim_to;
1191 uint32_t t;
1192 double x, y;
1193
1194 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1195 finish_workspace_change_animation(shell, from, to);
1196 return;
1197 }
1198
1199 if (shell->workspaces.anim_timestamp == 0) {
1200 if (shell->workspaces.anim_current == 0.0)
1201 shell->workspaces.anim_timestamp = msecs;
1202 else
1203 shell->workspaces.anim_timestamp =
1204 msecs -
1205 /* Invers of movement function 'y' below. */
1206 (asin(1.0 - shell->workspaces.anim_current) *
1207 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1208 M_2_PI);
1209 }
1210
1211 t = msecs - shell->workspaces.anim_timestamp;
1212
1213 /*
1214 * x = [0, π/2]
1215 * y(x) = sin(x)
1216 */
1217 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1218 y = sin(x);
1219
1220 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001221 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001222
1223 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1224 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1225 shell->workspaces.anim_current = y;
1226
Scott Moreau4272e632012-08-13 09:58:41 -06001227 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001228 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001229 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001230 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001231}
1232
1233static void
1234animate_workspace_change(struct desktop_shell *shell,
1235 unsigned int index,
1236 struct workspace *from,
1237 struct workspace *to)
1238{
1239 struct weston_output *output;
1240
1241 int dir;
1242
1243 if (index > shell->workspaces.current)
1244 dir = -1;
1245 else
1246 dir = 1;
1247
1248 shell->workspaces.current = index;
1249
1250 shell->workspaces.anim_dir = dir;
1251 shell->workspaces.anim_from = from;
1252 shell->workspaces.anim_to = to;
1253 shell->workspaces.anim_current = 0.0;
1254 shell->workspaces.anim_timestamp = 0;
1255
1256 output = container_of(shell->compositor->output_list.next,
1257 struct weston_output, link);
1258 wl_list_insert(&output->animation_list,
1259 &shell->workspaces.animation.link);
1260
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001261 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001262
1263 workspace_translate_in(to, 0);
1264
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001265 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001266
Scott Moreau4272e632012-08-13 09:58:41 -06001267 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001268}
1269
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001270static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001271update_workspace(struct desktop_shell *shell, unsigned int index,
1272 struct workspace *from, struct workspace *to)
1273{
1274 shell->workspaces.current = index;
1275 wl_list_insert(&from->layer.link, &to->layer.link);
1276 wl_list_remove(&from->layer.link);
1277}
1278
1279static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001280change_workspace(struct desktop_shell *shell, unsigned int index)
1281{
1282 struct workspace *from;
1283 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001284 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001285
1286 if (index == shell->workspaces.current)
1287 return;
1288
1289 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001290 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001291 return;
1292
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001293 from = get_current_workspace(shell);
1294 to = get_workspace(shell, index);
1295
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001296 if (shell->workspaces.anim_from == to &&
1297 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001298 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001299 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001300 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001301 return;
1302 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001303
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001304 if (shell->workspaces.anim_to != NULL)
1305 finish_workspace_change_animation(shell,
1306 shell->workspaces.anim_from,
1307 shell->workspaces.anim_to);
1308
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001309 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001310
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001311 if (shell->focus_animation_type != ANIMATION_NONE) {
1312 wl_list_for_each(state, &from->focus_list, link)
1313 if (state->keyboard_focus)
1314 animate_focus_change(shell, from,
1315 get_default_view(state->keyboard_focus), NULL);
1316
1317 wl_list_for_each(state, &to->focus_list, link)
1318 if (state->keyboard_focus)
1319 animate_focus_change(shell, to,
1320 NULL, get_default_view(state->keyboard_focus));
1321 }
1322
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001323 if (workspace_is_empty(to) && workspace_is_empty(from))
1324 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001325 else
1326 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001327
1328 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001329}
1330
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001331static bool
1332workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1333{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001334 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001335 struct wl_list *e;
1336
1337 if (wl_list_empty(list))
1338 return false;
1339
1340 e = list->next;
1341
1342 if (e->next != list)
1343 return false;
1344
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001345 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001346}
1347
1348static void
Philip Withnall659163d2013-11-25 18:01:36 +00001349move_surface_to_workspace(struct desktop_shell *shell,
1350 struct shell_surface *shsurf,
1351 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001352{
1353 struct workspace *from;
1354 struct workspace *to;
1355 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001356 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001357 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001358
1359 if (workspace == shell->workspaces.current)
1360 return;
1361
Philip Withnall659163d2013-11-25 18:01:36 +00001362 view = get_default_view(shsurf->surface);
1363 if (!view)
1364 return;
1365
1366 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1367
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001368 if (workspace >= shell->workspaces.num)
1369 workspace = shell->workspaces.num - 1;
1370
Jonas Ådahle9d22502012-08-29 22:13:01 +02001371 from = get_current_workspace(shell);
1372 to = get_workspace(shell, workspace);
1373
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001374 weston_layer_entry_remove(&view->layer_link);
1375 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001376
Philip Withnall648a4dd2013-11-25 18:01:44 +00001377 shell_surface_update_child_surface_layers(shsurf);
1378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001380 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1381 if (!seat->keyboard)
1382 continue;
1383
1384 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001386 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001387 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001388
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001390}
1391
1392static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001393take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001394 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001395 unsigned int index)
1396{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001397 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001399 struct shell_surface *shsurf;
1400 struct workspace *from;
1401 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001402 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001403
Pekka Paalanen01388e22013-04-25 13:57:44 +03001404 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 view = get_default_view(surface);
1406 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001407 index == shell->workspaces.current ||
1408 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001409 return;
1410
1411 from = get_current_workspace(shell);
1412 to = get_workspace(shell, index);
1413
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001414 weston_layer_entry_remove(&view->layer_link);
1415 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001416
Philip Withnall659163d2013-11-25 18:01:36 +00001417 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001418 if (shsurf != NULL)
1419 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001420
Jonas Ådahle9d22502012-08-29 22:13:01 +02001421 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001422 drop_focus_state(shell, from, surface);
1423
1424 if (shell->workspaces.anim_from == to &&
1425 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001426 wl_list_remove(&to->layer.link);
1427 wl_list_insert(from->layer.link.prev, &to->layer.link);
1428
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001429 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001430 broadcast_current_workspace_state(shell);
1431
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001432 return;
1433 }
1434
1435 if (shell->workspaces.anim_to != NULL)
1436 finish_workspace_change_animation(shell,
1437 shell->workspaces.anim_from,
1438 shell->workspaces.anim_to);
1439
1440 if (workspace_is_empty(from) &&
1441 workspace_has_only(to, surface))
1442 update_workspace(shell, index, from, to);
1443 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001444 if (shsurf != NULL &&
1445 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001446 wl_list_insert(&shell->workspaces.anim_sticky_list,
1447 &shsurf->workspace_transform.link);
1448
1449 animate_workspace_change(shell, index, from, to);
1450 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001451
1452 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001453
1454 state = ensure_focus_state(shell, seat);
1455 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001456 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001457}
1458
1459static void
1460workspace_manager_move_surface(struct wl_client *client,
1461 struct wl_resource *resource,
1462 struct wl_resource *surface_resource,
1463 uint32_t workspace)
1464{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001465 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001466 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001467 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001468 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001469 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001470
Pekka Paalanen01388e22013-04-25 13:57:44 +03001471 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001472 shell_surface = get_shell_surface(main_surface);
1473 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001475
1476 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001477}
1478
1479static const struct workspace_manager_interface workspace_manager_implementation = {
1480 workspace_manager_move_surface,
1481};
1482
1483static void
1484unbind_resource(struct wl_resource *resource)
1485{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001486 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001487}
1488
1489static void
1490bind_workspace_manager(struct wl_client *client,
1491 void *data, uint32_t version, uint32_t id)
1492{
1493 struct desktop_shell *shell = data;
1494 struct wl_resource *resource;
1495
Jason Ekstranda85118c2013-06-27 20:17:02 -05001496 resource = wl_resource_create(client,
1497 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001498
1499 if (resource == NULL) {
1500 weston_log("couldn't add workspace manager object");
1501 return;
1502 }
1503
Jason Ekstranda85118c2013-06-27 20:17:02 -05001504 wl_resource_set_implementation(resource,
1505 &workspace_manager_implementation,
1506 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001507 wl_list_insert(&shell->workspaces.client_list,
1508 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001509
1510 workspace_manager_send_state(resource,
1511 shell->workspaces.current,
1512 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001513}
1514
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001515static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001516touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1517 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1518{
1519}
1520
1521static void
1522touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1523{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001524 struct weston_touch_move_grab *move =
1525 (struct weston_touch_move_grab *) container_of(
1526 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001527
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001528 if (touch_id == 0)
1529 move->active = 0;
1530
Jonas Ådahl9484b692013-12-02 22:05:03 +01001531 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001532 shell_touch_grab_end(&move->base);
1533 free(move);
1534 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001535}
1536
1537static void
1538touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1539 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1540{
1541 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1542 struct shell_surface *shsurf = move->base.shsurf;
1543 struct weston_surface *es;
1544 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1545 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1546
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001547 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001548 return;
1549
1550 es = shsurf->surface;
1551
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001552 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001553
1554 weston_compositor_schedule_repaint(es->compositor);
1555}
1556
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001557static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001558touch_move_grab_frame(struct weston_touch_grab *grab)
1559{
1560}
1561
1562static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001563touch_move_grab_cancel(struct weston_touch_grab *grab)
1564{
1565 struct weston_touch_move_grab *move =
1566 (struct weston_touch_move_grab *) container_of(
1567 grab, struct shell_touch_grab, grab);
1568
1569 shell_touch_grab_end(&move->base);
1570 free(move);
1571}
1572
Rusty Lynch1084da52013-08-15 09:10:08 -07001573static const struct weston_touch_grab_interface touch_move_grab_interface = {
1574 touch_move_grab_down,
1575 touch_move_grab_up,
1576 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001577 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001578 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001579};
1580
1581static int
1582surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1583{
1584 struct weston_touch_move_grab *move;
1585
1586 if (!shsurf)
1587 return -1;
1588
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001589 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001590 return 0;
1591
1592 move = malloc(sizeof *move);
1593 if (!move)
1594 return -1;
1595
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001596 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001598 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001600 seat->touch->grab_y;
1601
1602 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1603 seat->touch);
1604
1605 return 0;
1606}
1607
1608static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001609noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001610{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001611}
1612
1613static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001614constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1615{
1616 struct shell_surface *shsurf = move->base.shsurf;
1617 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001618 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001619 const int safety = 50;
1620
1621 x = wl_fixed_to_int(pointer->x + move->dx);
1622 y = wl_fixed_to_int(pointer->y + move->dy);
1623
Jonny Lambd73c6942014-08-20 15:53:20 +02001624 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1625 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1626 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001627
Jonny Lambd73c6942014-08-20 15:53:20 +02001628 bottom = y + shsurf->geometry.height;
1629 if (bottom - panel_height < safety)
1630 y = panel_height + safety -
1631 shsurf->geometry.height;
1632
1633 if (move->client_initiated &&
1634 y + shsurf->geometry.y < panel_height)
1635 y = panel_height - shsurf->geometry.y;
1636 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001637
1638 *cx = x;
1639 *cy = y;
1640}
1641
1642static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001643move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1644 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001645{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001646 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001647 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001648 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001649 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001650
1651 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001652 if (!shsurf)
1653 return;
1654
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001655 constrain_position(move, &cx, &cy);
1656
1657 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001658
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001660}
1661
1662static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001663move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001664 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001665{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001666 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1667 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001668 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001669 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001670
Daniel Stone4dbadb12012-05-30 16:31:51 +01001671 if (pointer->button_count == 0 &&
1672 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001673 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001674 free(grab);
1675 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001676}
1677
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001678static void
1679move_grab_cancel(struct weston_pointer_grab *grab)
1680{
1681 struct shell_grab *shell_grab =
1682 container_of(grab, struct shell_grab, grab);
1683
1684 shell_grab_end(shell_grab);
1685 free(grab);
1686}
1687
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001688static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001689 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001690 move_grab_motion,
1691 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001692 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001693};
1694
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001695static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001696surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1697 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001698{
1699 struct weston_move_grab *move;
1700
1701 if (!shsurf)
1702 return -1;
1703
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001704 if (shsurf->grabbed ||
1705 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001706 return 0;
1707
1708 move = malloc(sizeof *move);
1709 if (!move)
1710 return -1;
1711
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001713 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001715 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001716 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001717
1718 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001719 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001720
1721 return 0;
1722}
1723
1724static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001725common_surface_move(struct wl_resource *resource,
1726 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001728 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001729 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001730 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001732 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001733 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001734 seat->pointer->button_count > 0 &&
1735 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001737 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001738 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001739 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001740 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001741 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001742 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001744 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001745 (surface_touch_move(shsurf, seat) < 0))
1746 wl_resource_post_no_memory(resource);
1747 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001748}
1749
Rafael Antognollie2a34552013-12-03 15:35:45 -02001750static void
1751shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1752 struct wl_resource *seat_resource, uint32_t serial)
1753{
1754 common_surface_move(resource, seat_resource, serial);
1755}
1756
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001757struct weston_resize_grab {
1758 struct shell_grab base;
1759 uint32_t edges;
1760 int32_t width, height;
1761};
1762
1763static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001764resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1765 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001766{
1767 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001768 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001769 struct shell_surface *shsurf = resize->base.shsurf;
1770 int32_t width, height;
1771 wl_fixed_t from_x, from_y;
1772 wl_fixed_t to_x, to_y;
1773
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001774 weston_pointer_move(pointer, x, y);
1775
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001776 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001777 return;
1778
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779 weston_view_from_global_fixed(shsurf->view,
1780 pointer->grab_x, pointer->grab_y,
1781 &from_x, &from_y);
1782 weston_view_from_global_fixed(shsurf->view,
1783 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001784
1785 width = resize->width;
1786 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1787 width += wl_fixed_to_int(from_x - to_x);
1788 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1789 width += wl_fixed_to_int(to_x - from_x);
1790 }
1791
1792 height = resize->height;
1793 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1794 height += wl_fixed_to_int(from_y - to_y);
1795 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1796 height += wl_fixed_to_int(to_y - from_y);
1797 }
1798
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001799 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001800}
1801
1802static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001803send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001804{
1805 struct shell_surface *shsurf = get_shell_surface(surface);
1806
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001807 assert(shsurf);
1808
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001809 if (shsurf->resource)
1810 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001811 shsurf->resize_edges,
1812 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001813}
1814
1815static const struct weston_shell_client shell_client = {
1816 send_configure
1817};
1818
1819static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001820resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001821 uint32_t time, uint32_t button, uint32_t state_w)
1822{
1823 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001824 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001825 enum wl_pointer_button_state state = state_w;
1826
1827 if (pointer->button_count == 0 &&
1828 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1829 shell_grab_end(&resize->base);
1830 free(grab);
1831 }
1832}
1833
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001834static void
1835resize_grab_cancel(struct weston_pointer_grab *grab)
1836{
1837 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1838
1839 shell_grab_end(&resize->base);
1840 free(grab);
1841}
1842
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001843static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001844 noop_grab_focus,
1845 resize_grab_motion,
1846 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001847 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001848};
1849
Giulio Camuffob8366642013-04-25 13:57:46 +03001850/*
1851 * Returns the bounding box of a surface and all its sub-surfaces,
1852 * in the surface coordinates system. */
1853static void
1854surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1855 int32_t *y, int32_t *w, int32_t *h) {
1856 pixman_region32_t region;
1857 pixman_box32_t *box;
1858 struct weston_subsurface *subsurface;
1859
1860 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001861 surface->width,
1862 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001863
1864 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1865 pixman_region32_union_rect(&region, &region,
1866 subsurface->position.x,
1867 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001868 subsurface->surface->width,
1869 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001870 }
1871
1872 box = pixman_region32_extents(&region);
1873 if (x)
1874 *x = box->x1;
1875 if (y)
1876 *y = box->y1;
1877 if (w)
1878 *w = box->x2 - box->x1;
1879 if (h)
1880 *h = box->y2 - box->y1;
1881
1882 pixman_region32_fini(&region);
1883}
1884
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001885static int
1886surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001887 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001888{
1889 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001890 const unsigned resize_topbottom =
1891 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1892 const unsigned resize_leftright =
1893 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1894 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001895
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001896 if (shsurf->grabbed ||
1897 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001898 return 0;
1899
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001900 /* Check for invalid edge combinations. */
1901 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1902 (edges & resize_topbottom) == resize_topbottom ||
1903 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001904 return 0;
1905
1906 resize = malloc(sizeof *resize);
1907 if (!resize)
1908 return -1;
1909
1910 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001911
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001912 resize->width = shsurf->geometry.width;
1913 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001914
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001915 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001916 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001917 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001918 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001919
1920 return 0;
1921}
1922
1923static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001924common_surface_resize(struct wl_resource *resource,
1925 struct wl_resource *seat_resource, uint32_t serial,
1926 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001927{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001928 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001929 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001930 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001931
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001932 if (seat->pointer == NULL ||
1933 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001934 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001935 seat->pointer->focus == NULL)
1936 return;
1937
1938 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1939 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001940 return;
1941
Kristian Høgsberge3148752013-05-06 23:19:49 -04001942 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001943 wl_resource_post_no_memory(resource);
1944}
1945
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001946static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001947shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1948 struct wl_resource *seat_resource, uint32_t serial,
1949 uint32_t edges)
1950{
1951 common_surface_resize(resource, seat_resource, serial, edges);
1952}
1953
1954static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001955busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001956{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001957 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001958 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001959 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001960 wl_fixed_t sx, sy;
1961
Jason Ekstranda7af7042013-10-12 22:38:11 -05001962 view = weston_compositor_pick_view(pointer->seat->compositor,
1963 pointer->x, pointer->y,
1964 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001965
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001966 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1967 shell_grab_end(grab);
1968 free(grab);
1969 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001970}
1971
1972static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001973busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1974 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001975{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001976 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001977}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001978
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001979static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001980busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001981 uint32_t time, uint32_t button, uint32_t state)
1982{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001983 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001984 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001985 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001986
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001987 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001988 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001989 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001990 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001991 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001992 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001993 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001994}
1995
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001996static void
1997busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1998{
1999 struct shell_grab *grab = (struct shell_grab *) base;
2000
2001 shell_grab_end(grab);
2002 free(grab);
2003}
2004
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002005static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002006 busy_cursor_grab_focus,
2007 busy_cursor_grab_motion,
2008 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002009 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002010};
2011
2012static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002013set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002014{
2015 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002016
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002017 if (pointer->grab->interface == &busy_cursor_grab_interface)
2018 return;
2019
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002020 grab = malloc(sizeof *grab);
2021 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002022 return;
2023
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002024 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2025 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002026 /* Mark the shsurf as ungrabbed so that button binding is able
2027 * to move it. */
2028 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002029}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002030
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002031static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002032end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002033{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002034 struct shell_grab *grab;
2035 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002036
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002037 wl_list_for_each(seat, &compositor->seat_list, link) {
2038 if (seat->pointer == NULL)
2039 continue;
2040
2041 grab = (struct shell_grab *) seat->pointer->grab;
2042 if (grab->grab.interface == &busy_cursor_grab_interface &&
2043 wl_resource_get_client(grab->shsurf->resource) == client) {
2044 shell_grab_end(grab);
2045 free(grab);
2046 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002047 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002048}
2049
Scott Moreau9521d5e2012-04-19 13:06:17 -06002050static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002051handle_shell_client_destroy(struct wl_listener *listener, void *data);
2052
2053static int
2054xdg_ping_timeout_handler(void *data)
2055{
2056 struct shell_client *sc = data;
2057 struct weston_seat *seat;
2058 struct shell_surface *shsurf;
2059
2060 /* Client is not responding */
2061 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002062 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2063 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2064 continue;
2065 if (seat->pointer->focus->surface->resource == NULL)
2066 continue;
2067
2068 shsurf = get_shell_surface(seat->pointer->focus->surface);
2069 if (shsurf &&
2070 wl_resource_get_client(shsurf->resource) == sc->client)
2071 set_busy_cursor(shsurf, seat->pointer);
2072 }
2073
2074 return 1;
2075}
2076
2077static void
2078handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2079{
2080 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002081 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002082 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002083 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002084
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002085 if (sc->unresponsive) {
2086 xdg_ping_timeout_handler(sc);
2087 return;
2088 }
2089
2090 sc->ping_serial = serial;
2091 loop = wl_display_get_event_loop(compositor->wl_display);
2092 if (sc->ping_timer == NULL)
2093 sc->ping_timer =
2094 wl_event_loop_add_timer(loop,
2095 xdg_ping_timeout_handler, sc);
2096 if (sc->ping_timer == NULL)
2097 return;
2098
2099 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2100
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002101 if (shell_surface_is_xdg_surface(shsurf) ||
2102 shell_surface_is_xdg_popup(shsurf))
2103 xdg_shell_send_ping(sc->resource, serial);
2104 else if (shell_surface_is_wl_shell_surface(shsurf))
2105 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002106}
2107
Scott Moreauff1db4a2012-04-17 19:06:18 -06002108static void
2109ping_handler(struct weston_surface *surface, uint32_t serial)
2110{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002111 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002112
2113 if (!shsurf)
2114 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002115 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002116 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002117 if (shsurf->surface == shsurf->shell->grab_surface)
2118 return;
2119
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002120 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002121}
2122
2123static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002124handle_pointer_focus(struct wl_listener *listener, void *data)
2125{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002126 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002128 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002129 uint32_t serial;
2130
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002132 return;
2133
Jason Ekstranda7af7042013-10-12 22:38:11 -05002134 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002135 serial = wl_display_next_serial(compositor->wl_display);
2136 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002137}
2138
2139static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002140shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2141{
2142 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002143 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002144}
2145
2146static void
2147shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2148{
2149 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002150 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002151}
2152
2153static void
2154handle_keyboard_focus(struct wl_listener *listener, void *data)
2155{
2156 struct weston_keyboard *keyboard = data;
2157 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2158
2159 if (seat->focused_surface) {
2160 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2161 if (shsurf)
2162 shell_surface_lose_keyboard_focus(shsurf);
2163 }
2164
2165 seat->focused_surface = keyboard->focus;
2166
2167 if (seat->focused_surface) {
2168 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2169 if (shsurf)
2170 shell_surface_gain_keyboard_focus(shsurf);
2171 }
2172}
2173
2174static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002175shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002176{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002177 if (sc->ping_serial != serial)
2178 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002179
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002180 sc->unresponsive = 0;
2181 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002182
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002183 if (sc->ping_timer) {
2184 wl_event_source_remove(sc->ping_timer);
2185 sc->ping_timer = NULL;
2186 }
2187
2188}
2189
2190static void
2191shell_surface_pong(struct wl_client *client,
2192 struct wl_resource *resource, uint32_t serial)
2193{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002194 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2195 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002196
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002197 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002198}
2199
2200static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002201set_title(struct shell_surface *shsurf, const char *title)
2202{
2203 free(shsurf->title);
2204 shsurf->title = strdup(title);
2205}
2206
2207static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002208set_window_geometry(struct shell_surface *shsurf,
2209 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002210{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002211 shsurf->next_geometry.x = x;
2212 shsurf->next_geometry.y = y;
2213 shsurf->next_geometry.width = width;
2214 shsurf->next_geometry.height = height;
2215 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002216}
2217
2218static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002219shell_surface_set_title(struct wl_client *client,
2220 struct wl_resource *resource, const char *title)
2221{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002222 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002223
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002224 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002225}
2226
2227static void
2228shell_surface_set_class(struct wl_client *client,
2229 struct wl_resource *resource, const char *class)
2230{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002231 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002232
2233 free(shsurf->class);
2234 shsurf->class = strdup(class);
2235}
2236
Alex Wu4539b082012-03-01 12:57:46 +08002237static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002238restore_output_mode(struct weston_output *output)
2239{
Hardening57388e42013-09-18 23:56:36 +02002240 if (output->current_mode != output->original_mode ||
2241 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002242 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002243 output->original_mode,
2244 output->original_scale,
2245 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002246}
2247
2248static void
2249restore_all_output_modes(struct weston_compositor *compositor)
2250{
2251 struct weston_output *output;
2252
2253 wl_list_for_each(output, &compositor->output_list, link)
2254 restore_output_mode(output);
2255}
2256
Philip Withnall07926d92013-11-25 18:01:40 +00002257/* The surface will be inserted into the list immediately after the link
2258 * returned by this function (i.e. will be stacked immediately above the
2259 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002260static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002261shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2262{
2263 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002264 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002265
2266 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002267 case SHELL_SURFACE_XWAYLAND:
2268 return &shsurf->shell->fullscreen_layer.view_list;
2269
2270 case SHELL_SURFACE_NONE:
2271 return NULL;
2272
Kristian Høgsbergead52422014-01-01 13:51:52 -08002273 case SHELL_SURFACE_POPUP:
2274 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002275 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002276 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002277 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002278 /* Move the surface to its parent layer so
2279 * that surfaces which are transient for
2280 * fullscreen surfaces don't get hidden by the
2281 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002282
2283 /* TODO: Handle a parent with multiple views */
2284 parent = get_default_view(shsurf->parent);
2285 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002286 return container_of(parent->layer_link.link.prev,
2287 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002288 }
Philip Withnall07926d92013-11-25 18:01:40 +00002289
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002290 /* Move the surface to a normal workspace layer so that surfaces
2291 * which were previously fullscreen or transient are no longer
2292 * rendered on top. */
2293 ws = get_current_workspace(shsurf->shell);
2294 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002295 }
2296
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002297 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002298}
2299
Philip Withnall648a4dd2013-11-25 18:01:44 +00002300static void
2301shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2302{
2303 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002304 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002305
2306 /* Move the child layers to the same workspace as shsurf. They will be
2307 * stacked above shsurf. */
2308 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002309 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002310 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002311 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002312 prev = container_of(shsurf->view->layer_link.link.prev,
2313 struct weston_layer_entry, link);
2314 weston_layer_entry_remove(&child->view->layer_link);
2315 weston_layer_entry_insert(prev,
2316 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002317 weston_view_geometry_dirty(child->view);
2318 weston_surface_damage(child->surface);
2319
2320 /* Recurse. We don’t expect this to recurse very far (if
2321 * at all) because that would imply we have transient
2322 * (or popup) children of transient surfaces, which
2323 * would be unusual. */
2324 shell_surface_update_child_surface_layers(child);
2325 }
2326 }
2327}
2328
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002329/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002330 * geometry to ensure the changes are redrawn.
2331 *
2332 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2333 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002334static void
2335shell_surface_update_layer(struct shell_surface *shsurf)
2336{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002337 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002338
2339 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2340
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002341 if (new_layer_link == NULL)
2342 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002343 if (new_layer_link == &shsurf->view->layer_link)
2344 return;
2345
2346 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002347 weston_layer_entry_remove(&shsurf->view->layer_link);
2348 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002349 weston_view_geometry_dirty(shsurf->view);
2350 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002351
2352 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002353}
2354
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002355static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002356shell_surface_set_parent(struct shell_surface *shsurf,
2357 struct weston_surface *parent)
2358{
2359 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002360
2361 wl_list_remove(&shsurf->children_link);
2362 wl_list_init(&shsurf->children_link);
2363
2364 /* Insert into the parent surface’s child list. */
2365 if (parent != NULL) {
2366 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2367 if (parent_shsurf != NULL)
2368 wl_list_insert(&parent_shsurf->children_list,
2369 &shsurf->children_link);
2370 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002371}
2372
2373static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002374shell_surface_set_output(struct shell_surface *shsurf,
2375 struct weston_output *output)
2376{
2377 struct weston_surface *es = shsurf->surface;
2378
2379 /* get the default output, if the client set it as NULL
2380 check whether the ouput is available */
2381 if (output)
2382 shsurf->output = output;
2383 else if (es->output)
2384 shsurf->output = es->output;
2385 else
2386 shsurf->output = get_default_output(es->compositor);
2387}
2388
2389static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002390surface_clear_next_states(struct shell_surface *shsurf)
2391{
2392 shsurf->next_state.maximized = false;
2393 shsurf->next_state.fullscreen = false;
2394
2395 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2396 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2397 shsurf->state_changed = true;
2398}
2399
2400static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002401set_toplevel(struct shell_surface *shsurf)
2402{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002403 shell_surface_set_parent(shsurf, NULL);
2404 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002405 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002406
2407 /* The layer_link is updated in set_surface_type(),
2408 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002409}
2410
2411static void
2412shell_surface_set_toplevel(struct wl_client *client,
2413 struct wl_resource *resource)
2414{
2415 struct shell_surface *surface = wl_resource_get_user_data(resource);
2416
2417 set_toplevel(surface);
2418}
2419
2420static void
2421set_transient(struct shell_surface *shsurf,
2422 struct weston_surface *parent, int x, int y, uint32_t flags)
2423{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002424 assert(parent != NULL);
2425
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002426 shell_surface_set_parent(shsurf, parent);
2427
2428 surface_clear_next_states(shsurf);
2429
Philip Withnallbecb77e2013-11-25 18:01:30 +00002430 shsurf->transient.x = x;
2431 shsurf->transient.y = y;
2432 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002433
Rafael Antognollied207b42013-12-03 15:35:43 -02002434 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002435 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002436 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002437
2438 /* The layer_link is updated in set_surface_type(),
2439 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002440}
2441
2442static void
2443shell_surface_set_transient(struct wl_client *client,
2444 struct wl_resource *resource,
2445 struct wl_resource *parent_resource,
2446 int x, int y, uint32_t flags)
2447{
2448 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2449 struct weston_surface *parent =
2450 wl_resource_get_user_data(parent_resource);
2451
2452 set_transient(shsurf, parent, x, y, flags);
2453}
2454
2455static void
2456set_fullscreen(struct shell_surface *shsurf,
2457 uint32_t method,
2458 uint32_t framerate,
2459 struct weston_output *output)
2460{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002461 shell_surface_set_output(shsurf, output);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002462 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002463
2464 shsurf->fullscreen_output = shsurf->output;
2465 shsurf->fullscreen.type = method;
2466 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002467
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002468 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002469}
2470
2471static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002472weston_view_set_initial_position(struct weston_view *view,
2473 struct desktop_shell *shell);
2474
2475static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002476unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002477{
Philip Withnallf85fe842013-11-25 18:01:37 +00002478 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002479 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2480 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002481 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002482 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002483
Alex Wu4539b082012-03-01 12:57:46 +08002484 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2485 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002486
Alex Wu4539b082012-03-01 12:57:46 +08002487 wl_list_remove(&shsurf->fullscreen.transform.link);
2488 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002489
Jason Ekstranda7af7042013-10-12 22:38:11 -05002490 if (shsurf->fullscreen.black_view)
2491 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2492 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002493
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002494 if (shsurf->saved_position_valid)
2495 weston_view_set_position(shsurf->view,
2496 shsurf->saved_x, shsurf->saved_y);
2497 else
2498 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2499
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002500 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002501 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002502 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002503 shsurf->saved_rotation_valid = false;
2504 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002505
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002506 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002507}
2508
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002509static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002510shell_surface_set_fullscreen(struct wl_client *client,
2511 struct wl_resource *resource,
2512 uint32_t method,
2513 uint32_t framerate,
2514 struct wl_resource *output_resource)
2515{
2516 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2517 struct weston_output *output;
2518
2519 if (output_resource)
2520 output = wl_resource_get_user_data(output_resource);
2521 else
2522 output = NULL;
2523
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002524 shell_surface_set_parent(shsurf, NULL);
2525
Rafael Antognolli03b16592013-12-03 15:35:42 -02002526 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002527 shsurf->next_state.fullscreen = true;
2528 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002529 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002530}
2531
2532static void
2533set_popup(struct shell_surface *shsurf,
2534 struct weston_surface *parent,
2535 struct weston_seat *seat,
2536 uint32_t serial,
2537 int32_t x,
2538 int32_t y)
2539{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002540 assert(parent != NULL);
2541
Philip Withnallbecb77e2013-11-25 18:01:30 +00002542 shsurf->popup.shseat = get_shell_seat(seat);
2543 shsurf->popup.serial = serial;
2544 shsurf->popup.x = x;
2545 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002546
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002547 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002548}
2549
2550static void
2551shell_surface_set_popup(struct wl_client *client,
2552 struct wl_resource *resource,
2553 struct wl_resource *seat_resource,
2554 uint32_t serial,
2555 struct wl_resource *parent_resource,
2556 int32_t x, int32_t y, uint32_t flags)
2557{
2558 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002559 struct weston_surface *parent =
2560 wl_resource_get_user_data(parent_resource);
2561
2562 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002563
Rafael Antognolli03b16592013-12-03 15:35:42 -02002564 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002565 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002566 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002567 wl_resource_get_user_data(seat_resource),
2568 serial, x, y);
2569}
2570
2571static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002572unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002573{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002574 /* undo all maximized things here */
2575 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002576
2577 if (shsurf->saved_position_valid)
2578 weston_view_set_position(shsurf->view,
2579 shsurf->saved_x, shsurf->saved_y);
2580 else
2581 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002582
2583 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002584 wl_list_insert(&shsurf->view->geometry.transformation_list,
2585 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002586 shsurf->saved_rotation_valid = false;
2587 }
2588
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002589 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002590}
2591
Philip Withnallbecb77e2013-11-25 18:01:30 +00002592static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002593set_minimized(struct weston_surface *surface, uint32_t is_true)
2594{
2595 struct shell_surface *shsurf;
2596 struct workspace *current_ws;
2597 struct weston_seat *seat;
2598 struct weston_surface *focus;
2599 struct weston_view *view;
2600
2601 view = get_default_view(surface);
2602 if (!view)
2603 return;
2604
2605 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2606
2607 shsurf = get_shell_surface(surface);
2608 current_ws = get_current_workspace(shsurf->shell);
2609
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002610 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002611 /* hide or show, depending on the state */
2612 if (is_true) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002613 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002614
2615 drop_focus_state(shsurf->shell, current_ws, view->surface);
2616 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2617 if (!seat->keyboard)
2618 continue;
2619 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2620 if (focus == view->surface)
2621 weston_keyboard_set_focus(seat->keyboard, NULL);
2622 }
2623 }
2624 else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002625 weston_layer_entry_insert(&current_ws->layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002626
2627 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2628 if (!seat->keyboard)
2629 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002630 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002631 }
2632 }
2633
2634 shell_surface_update_child_surface_layers(shsurf);
2635
2636 weston_view_damage_below(view);
2637}
2638
2639static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002640shell_surface_set_maximized(struct wl_client *client,
2641 struct wl_resource *resource,
2642 struct wl_resource *output_resource)
2643{
2644 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2645 struct weston_output *output;
2646
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002647 surface_clear_next_states(shsurf);
2648 shsurf->next_state.maximized = true;
2649 shsurf->state_changed = true;
2650
2651 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2652 shell_surface_set_parent(shsurf, NULL);
2653
Philip Withnallbecb77e2013-11-25 18:01:30 +00002654 if (output_resource)
2655 output = wl_resource_get_user_data(output_resource);
2656 else
2657 output = NULL;
2658
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002659 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002660
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002661 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002662}
2663
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002664/* This is only ever called from set_surface_type(), so there’s no need to
2665 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002666static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002667reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002668{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002669 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002670 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002671 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002672 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002673
Pekka Paalanen98262232011-12-01 10:42:22 +02002674 return 0;
2675}
2676
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002677static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002678set_full_output(struct shell_surface *shsurf)
2679{
2680 shsurf->saved_x = shsurf->view->geometry.x;
2681 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002682 shsurf->saved_width = shsurf->surface->width;
2683 shsurf->saved_height = shsurf->surface->height;
2684 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002685 shsurf->saved_position_valid = true;
2686
2687 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2688 wl_list_remove(&shsurf->rotation.transform.link);
2689 wl_list_init(&shsurf->rotation.transform.link);
2690 weston_view_geometry_dirty(shsurf->view);
2691 shsurf->saved_rotation_valid = true;
2692 }
2693}
2694
2695static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002696set_surface_type(struct shell_surface *shsurf)
2697{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002698 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002699 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002700
Philip Withnallbecb77e2013-11-25 18:01:30 +00002701 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002702
Rafael Antognolli03b16592013-12-03 15:35:42 -02002703 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002704 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002705
2706 switch (shsurf->type) {
2707 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002708 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002709 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002710 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002711 weston_view_set_position(shsurf->view,
2712 pev->geometry.x + shsurf->transient.x,
2713 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002714 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002715 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002716
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002717 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002718 weston_view_set_position(shsurf->view, shsurf->transient.x,
2719 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002720 break;
2721
Philip Withnall0f640e22013-11-25 18:01:31 +00002722 case SHELL_SURFACE_POPUP:
2723 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002724 default:
2725 break;
2726 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002727
2728 /* Update the surface’s layer. */
2729 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002730}
2731
Tiago Vignattibe143262012-04-16 17:31:41 +03002732static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002733shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002734{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002735 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002736}
2737
Alex Wu21858432012-04-01 20:13:08 +08002738static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002739black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002740
Jason Ekstranda7af7042013-10-12 22:38:11 -05002741static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002742create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002743 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002744 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002745{
2746 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002747 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002748
2749 surface = weston_surface_create(ec);
2750 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002751 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002752 return NULL;
2753 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002754 view = weston_view_create(surface);
2755 if (surface == NULL) {
2756 weston_log("no memory\n");
2757 weston_surface_destroy(surface);
2758 return NULL;
2759 }
Alex Wu4539b082012-03-01 12:57:46 +08002760
Alex Wu21858432012-04-01 20:13:08 +08002761 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002762 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002763 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002764 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002765 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002766 pixman_region32_fini(&surface->input);
2767 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002768
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002769 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002770 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002771
2772 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002773}
2774
Philip Withnalled8f1a92013-11-25 18:01:43 +00002775static void
2776shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2777{
2778 struct weston_output *output = shsurf->fullscreen_output;
2779
Rafael Antognolli03b16592013-12-03 15:35:42 -02002780 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002781
2782 if (!shsurf->fullscreen.black_view)
2783 shsurf->fullscreen.black_view =
2784 create_black_surface(shsurf->surface->compositor,
2785 shsurf->surface,
2786 output->x, output->y,
2787 output->width,
2788 output->height);
2789
2790 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002791 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2792 weston_layer_entry_insert(&shsurf->view->layer_link,
2793 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002794 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2795 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002796
2797 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002798}
2799
Alex Wu4539b082012-03-01 12:57:46 +08002800/* Create black surface and append it to the associated fullscreen surface.
2801 * Handle size dismatch and positioning according to the method. */
2802static void
2803shell_configure_fullscreen(struct shell_surface *shsurf)
2804{
2805 struct weston_output *output = shsurf->fullscreen_output;
2806 struct weston_surface *surface = shsurf->surface;
2807 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002808 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002809 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002810
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002811 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2812 restore_output_mode(output);
2813
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002814 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002815 weston_layer_entry_remove(&shsurf->view->layer_link);
2816 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002817
Philip Withnalled8f1a92013-11-25 18:01:43 +00002818 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002819
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002821 &surf_width, &surf_height);
2822
Alex Wu4539b082012-03-01 12:57:46 +08002823 switch (shsurf->fullscreen.type) {
2824 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002825 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002826 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002827 break;
2828 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002829 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002830 if (output->width == surf_width &&
2831 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002832 weston_view_set_position(shsurf->view,
2833 output->x - surf_x,
2834 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002835 break;
2836 }
2837
Alex Wu4539b082012-03-01 12:57:46 +08002838 matrix = &shsurf->fullscreen.transform.matrix;
2839 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002840
Scott Moreau1bad5db2012-08-18 01:04:05 -06002841 output_aspect = (float) output->width /
2842 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002843 /* XXX: Use surf_width and surf_height here? */
2844 surface_aspect = (float) surface->width /
2845 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002846 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002847 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002848 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002849 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002850 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002851 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002852
Alex Wu4539b082012-03-01 12:57:46 +08002853 weston_matrix_scale(matrix, scale, scale, 1);
2854 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002855 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002856 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002857 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2858 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002859 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002860
Alex Wu4539b082012-03-01 12:57:46 +08002861 break;
2862 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002863 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002864 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002865 surf_width * surface->buffer_viewport.buffer.scale,
2866 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002867 shsurf->fullscreen.framerate};
2868
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002869 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002870 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002871 weston_view_set_position(shsurf->view,
2872 output->x - surf_x,
2873 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002874 shsurf->fullscreen.black_view->surface->width = output->width;
2875 shsurf->fullscreen.black_view->surface->height = output->height;
2876 weston_view_set_position(shsurf->fullscreen.black_view,
2877 output->x - surf_x,
2878 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002879 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002880 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002881 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002882 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002883 }
Alex Wubd3354b2012-04-17 17:20:49 +08002884 }
Alex Wu4539b082012-03-01 12:57:46 +08002885 break;
2886 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002887 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002888 break;
2889 default:
2890 break;
2891 }
2892}
2893
Alex Wu4539b082012-03-01 12:57:46 +08002894static void
2895shell_map_fullscreen(struct shell_surface *shsurf)
2896{
Alex Wubd3354b2012-04-17 17:20:49 +08002897 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002898}
2899
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002900static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002901set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2902{
2903 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002904 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002905 shsurf->transient.x = x;
2906 shsurf->transient.y = y;
2907 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002908
2909 shell_surface_set_parent(shsurf, NULL);
2910
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002911 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002912 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002913}
2914
Kristian Høgsberg47792412014-05-04 13:47:06 -07002915static void
2916shell_interface_set_fullscreen(struct shell_surface *shsurf,
2917 uint32_t method,
2918 uint32_t framerate,
2919 struct weston_output *output)
2920{
2921 surface_clear_next_states(shsurf);
2922 set_fullscreen(shsurf, method, framerate, output);
2923
2924 shsurf->next_state.fullscreen = true;
2925 shsurf->state_changed = true;
2926}
2927
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002928static int
2929shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2930{
2931 return surface_move(shsurf, ws, 1);
2932}
2933
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002934static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002935
2936static void
2937destroy_shell_seat(struct wl_listener *listener, void *data)
2938{
2939 struct shell_seat *shseat =
2940 container_of(listener,
2941 struct shell_seat, seat_destroy_listener);
2942 struct shell_surface *shsurf, *prev = NULL;
2943
2944 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002945 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002946 shseat->popup_grab.client = NULL;
2947
2948 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2949 shsurf->popup.shseat = NULL;
2950 if (prev) {
2951 wl_list_init(&prev->popup.grab_link);
2952 }
2953 prev = shsurf;
2954 }
2955 wl_list_init(&prev->popup.grab_link);
2956 }
2957
2958 wl_list_remove(&shseat->seat_destroy_listener.link);
2959 free(shseat);
2960}
2961
Jason Ekstrand024177c2014-04-21 19:42:58 -05002962static void
2963shell_seat_caps_changed(struct wl_listener *listener, void *data)
2964{
2965 struct shell_seat *seat;
2966
2967 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2968
2969 if (seat->seat->keyboard &&
2970 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2971 wl_signal_add(&seat->seat->keyboard->focus_signal,
2972 &seat->keyboard_focus_listener);
2973 } else if (!seat->seat->keyboard) {
2974 wl_list_init(&seat->keyboard_focus_listener.link);
2975 }
2976
2977 if (seat->seat->pointer &&
2978 wl_list_empty(&seat->pointer_focus_listener.link)) {
2979 wl_signal_add(&seat->seat->pointer->focus_signal,
2980 &seat->pointer_focus_listener);
2981 } else if (!seat->seat->pointer) {
2982 wl_list_init(&seat->pointer_focus_listener.link);
2983 }
2984}
2985
Giulio Camuffo5085a752013-03-25 21:42:45 +01002986static struct shell_seat *
2987create_shell_seat(struct weston_seat *seat)
2988{
2989 struct shell_seat *shseat;
2990
2991 shseat = calloc(1, sizeof *shseat);
2992 if (!shseat) {
2993 weston_log("no memory to allocate shell seat\n");
2994 return NULL;
2995 }
2996
2997 shseat->seat = seat;
2998 wl_list_init(&shseat->popup_grab.surfaces_list);
2999
3000 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3001 wl_signal_add(&seat->destroy_signal,
3002 &shseat->seat_destroy_listener);
3003
Jason Ekstrand024177c2014-04-21 19:42:58 -05003004 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3005 wl_list_init(&shseat->keyboard_focus_listener.link);
3006
3007 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3008 wl_list_init(&shseat->pointer_focus_listener.link);
3009
3010 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3011 wl_signal_add(&seat->updated_caps_signal,
3012 &shseat->caps_changed_listener);
3013 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3014
Giulio Camuffo5085a752013-03-25 21:42:45 +01003015 return shseat;
3016}
3017
3018static struct shell_seat *
3019get_shell_seat(struct weston_seat *seat)
3020{
3021 struct wl_listener *listener;
3022
3023 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003024 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003025
3026 return container_of(listener,
3027 struct shell_seat, seat_destroy_listener);
3028}
3029
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003030static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003031popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003032{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003033 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003034 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003035 struct shell_seat *shseat =
3036 container_of(grab, struct shell_seat, popup_grab.grab);
3037 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003038 wl_fixed_t sx, sy;
3039
Jason Ekstranda7af7042013-10-12 22:38:11 -05003040 view = weston_compositor_pick_view(pointer->seat->compositor,
3041 pointer->x, pointer->y,
3042 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003043
Jason Ekstranda7af7042013-10-12 22:38:11 -05003044 if (view && view->surface->resource &&
3045 wl_resource_get_client(view->surface->resource) == client) {
3046 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003047 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003048 weston_pointer_set_focus(pointer, NULL,
3049 wl_fixed_from_int(0),
3050 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003051 }
3052}
3053
3054static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003055popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3056 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003057{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003058 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003059 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003060 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003061
Jonas Ådahl61917c82014-08-11 22:44:02 +02003062 if (pointer->focus) {
3063 weston_view_from_global_fixed(pointer->focus, x, y,
3064 &pointer->sx, &pointer->sy);
3065 }
3066
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003067 weston_pointer_move(pointer, x, y);
3068
Neil Roberts96d790e2013-09-19 17:32:00 +01003069 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003070 weston_view_from_global_fixed(pointer->focus,
3071 pointer->x, pointer->y,
3072 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003073 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003074 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003075}
3076
3077static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003078popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003079 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003080{
3081 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003082 struct shell_seat *shseat =
3083 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003084 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003085 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003086 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003087 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003088
Neil Roberts96d790e2013-09-19 17:32:00 +01003089 resource_list = &grab->pointer->focus_resource_list;
3090 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003091 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003092 wl_resource_for_each(resource, resource_list) {
3093 wl_pointer_send_button(resource, serial,
3094 time, button, state);
3095 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003096 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003097 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003098 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003099 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003100 }
3101
Daniel Stone4dbadb12012-05-30 16:31:51 +01003102 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003103 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003104}
3105
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003106static void
3107popup_grab_cancel(struct weston_pointer_grab *grab)
3108{
3109 popup_grab_end(grab->pointer);
3110}
3111
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003112static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003113 popup_grab_focus,
3114 popup_grab_motion,
3115 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003116 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003117};
3118
3119static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003120shell_surface_send_popup_done(struct shell_surface *shsurf)
3121{
3122 if (shell_surface_is_wl_shell_surface(shsurf))
3123 wl_shell_surface_send_popup_done(shsurf->resource);
3124 else if (shell_surface_is_xdg_popup(shsurf))
3125 xdg_popup_send_popup_done(shsurf->resource,
3126 shsurf->popup.serial);
3127}
3128
3129static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003130popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003131{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003132 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003133 struct shell_seat *shseat =
3134 container_of(grab, struct shell_seat, popup_grab.grab);
3135 struct shell_surface *shsurf;
3136 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003137
3138 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003139 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003140 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003141 shseat->popup_grab.grab.interface = NULL;
3142 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003143 /* Send the popup_done event to all the popups open */
3144 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003145 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003146 shsurf->popup.shseat = NULL;
3147 if (prev) {
3148 wl_list_init(&prev->popup.grab_link);
3149 }
3150 prev = shsurf;
3151 }
3152 wl_list_init(&prev->popup.grab_link);
3153 wl_list_init(&shseat->popup_grab.surfaces_list);
3154 }
3155}
3156
3157static void
3158add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3159{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003160 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003161
3162 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003163 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003164 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003165 /* We must make sure here that this popup was opened after
3166 * a mouse press, and not just by moving around with other
3167 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003168 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003169 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003170
Rob Bradforddfe31052013-06-26 19:49:11 +01003171 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003172 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003173 } else {
3174 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003175 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003176}
3177
3178static void
3179remove_popup_grab(struct shell_surface *shsurf)
3180{
3181 struct shell_seat *shseat = shsurf->popup.shseat;
3182
3183 wl_list_remove(&shsurf->popup.grab_link);
3184 wl_list_init(&shsurf->popup.grab_link);
3185 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003186 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003187 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003188 }
3189}
3190
3191static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003192shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003193{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003194 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003195 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003196
Jason Ekstranda7af7042013-10-12 22:38:11 -05003197 shsurf->surface->output = parent_view->output;
3198 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003199
Jason Ekstranda7af7042013-10-12 22:38:11 -05003200 weston_view_set_transform_parent(shsurf->view, parent_view);
3201 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3202 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003203
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003204 if (shseat->seat->pointer &&
3205 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003206 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003207 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003208 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003209 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003210 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003211}
3212
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003213static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003214 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003215 shell_surface_move,
3216 shell_surface_resize,
3217 shell_surface_set_toplevel,
3218 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003219 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003220 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003221 shell_surface_set_maximized,
3222 shell_surface_set_title,
3223 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003224};
3225
3226static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003227destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003228{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003229 struct shell_surface *child, *next;
3230
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003231 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3232
Giulio Camuffo5085a752013-03-25 21:42:45 +01003233 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3234 remove_popup_grab(shsurf);
3235 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003236
Alex Wubd3354b2012-04-17 17:20:49 +08003237 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003238 shell_surface_is_top_fullscreen(shsurf))
3239 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003240
Jason Ekstranda7af7042013-10-12 22:38:11 -05003241 if (shsurf->fullscreen.black_view)
3242 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003243
Alex Wubd3354b2012-04-17 17:20:49 +08003244 /* As destroy_resource() use wl_list_for_each_safe(),
3245 * we can always remove the listener.
3246 */
3247 wl_list_remove(&shsurf->surface_destroy_listener.link);
3248 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003249 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003250
Jason Ekstranda7af7042013-10-12 22:38:11 -05003251 weston_view_destroy(shsurf->view);
3252
Philip Withnall648a4dd2013-11-25 18:01:44 +00003253 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003254 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3255 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003256
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003257 wl_list_remove(&shsurf->link);
3258 free(shsurf);
3259}
3260
3261static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003262shell_destroy_shell_surface(struct wl_resource *resource)
3263{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003264 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003265
Bryan Caina46b9462014-04-04 17:41:24 -05003266 if (!wl_list_empty(&shsurf->popup.grab_link))
3267 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003268 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003269}
3270
3271static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003272shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003273{
3274 struct shell_surface *shsurf = container_of(listener,
3275 struct shell_surface,
3276 surface_destroy_listener);
3277
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003278 if (shsurf->resource)
3279 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003280
3281 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003282}
3283
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003284static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003285fade_out_done(struct weston_view_animation *animation, void *data)
3286{
3287 struct shell_surface *shsurf = data;
3288
3289 weston_surface_destroy(shsurf->surface);
3290}
3291
3292static void
3293handle_resource_destroy(struct wl_listener *listener, void *data)
3294{
3295 struct shell_surface *shsurf =
3296 container_of(listener, struct shell_surface,
3297 resource_destroy_listener);
3298
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003299 if (!weston_surface_is_mapped(shsurf->surface))
3300 return;
3301
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003302 shsurf->surface->ref_count++;
3303
3304 pixman_region32_fini(&shsurf->surface->pending.input);
3305 pixman_region32_init(&shsurf->surface->pending.input);
3306 pixman_region32_fini(&shsurf->surface->input);
3307 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003308 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3309 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3310 fade_out_done, shsurf);
3311 } else {
3312 weston_surface_destroy(shsurf->surface);
3313 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003314}
3315
3316static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003317shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003318
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003319struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003320get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003321{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003322 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003323 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003324 else
3325 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003326}
3327
Rafael Antognollie2a34552013-12-03 15:35:45 -02003328static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003329create_common_surface(struct shell_client *owner, void *shell,
3330 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003331 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003332{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003333 struct shell_surface *shsurf;
3334
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003335 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003336 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003337 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003338 }
3339
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003340 shsurf = calloc(1, sizeof *shsurf);
3341 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003342 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003343 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003344 }
3345
Jason Ekstranda7af7042013-10-12 22:38:11 -05003346 shsurf->view = weston_view_create(surface);
3347 if (!shsurf->view) {
3348 weston_log("no memory to allocate shell surface\n");
3349 free(shsurf);
3350 return NULL;
3351 }
3352
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003353 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003354 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003355
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003356 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3357 wl_resource_add_destroy_listener(surface->resource,
3358 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003359 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003360
Tiago Vignattibc052c92012-04-19 16:18:18 +03003361 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003362 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003363 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003364 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003365 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003366 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003367 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3368 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003369 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003370 wl_list_init(&shsurf->fullscreen.transform.link);
3371
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003372 shsurf->output = get_default_output(shsurf->shell->compositor);
3373
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003374 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003375 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003376 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003377 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003378
3379 /* init link so its safe to always remove it in destroy_shell_surface */
3380 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003381 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003382
Pekka Paalanen460099f2012-01-20 16:48:25 +02003383 /* empty when not in use */
3384 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003385 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003386
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003387 wl_list_init(&shsurf->workspace_transform.link);
3388
Philip Withnall648a4dd2013-11-25 18:01:44 +00003389 wl_list_init(&shsurf->children_link);
3390 wl_list_init(&shsurf->children_list);
3391 shsurf->parent = NULL;
3392
Pekka Paalanen98262232011-12-01 10:42:22 +02003393 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003394
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003395 shsurf->client = client;
3396
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003397 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003398}
3399
Rafael Antognollie2a34552013-12-03 15:35:45 -02003400static struct shell_surface *
3401create_shell_surface(void *shell, struct weston_surface *surface,
3402 const struct weston_shell_client *client)
3403{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003404 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003405}
3406
Jason Ekstranda7af7042013-10-12 22:38:11 -05003407static struct weston_view *
3408get_primary_view(void *shell, struct shell_surface *shsurf)
3409{
3410 return shsurf->view;
3411}
3412
Tiago Vignattibc052c92012-04-19 16:18:18 +03003413static void
3414shell_get_shell_surface(struct wl_client *client,
3415 struct wl_resource *resource,
3416 uint32_t id,
3417 struct wl_resource *surface_resource)
3418{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003419 struct weston_surface *surface =
3420 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003421 struct shell_client *sc = wl_resource_get_user_data(resource);
3422 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003423 struct shell_surface *shsurf;
3424
3425 if (get_shell_surface(surface)) {
3426 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003427 WL_DISPLAY_ERROR_INVALID_OBJECT,
3428 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003429 return;
3430 }
3431
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003432 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003433 if (!shsurf) {
3434 wl_resource_post_error(surface_resource,
3435 WL_DISPLAY_ERROR_INVALID_OBJECT,
3436 "surface->configure already set");
3437 return;
3438 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003439
Jason Ekstranda85118c2013-06-27 20:17:02 -05003440 shsurf->resource =
3441 wl_resource_create(client,
3442 &wl_shell_surface_interface, 1, id);
3443 wl_resource_set_implementation(shsurf->resource,
3444 &shell_surface_implementation,
3445 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003446}
3447
Rafael Antognollie2a34552013-12-03 15:35:45 -02003448static bool
3449shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3450{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003451 /* A shell surface without a resource is created from xwayland
3452 * and is considered a wl_shell surface for now. */
3453
3454 return shsurf->resource == NULL ||
3455 wl_resource_instance_of(shsurf->resource,
3456 &wl_shell_surface_interface,
3457 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003458}
3459
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003460static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003461 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003462};
3463
Rafael Antognollie2a34552013-12-03 15:35:45 -02003464/****************************
3465 * xdg-shell implementation */
3466
3467static void
3468xdg_surface_destroy(struct wl_client *client,
3469 struct wl_resource *resource)
3470{
3471 wl_resource_destroy(resource);
3472}
3473
3474static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003475xdg_surface_set_parent(struct wl_client *client,
3476 struct wl_resource *resource,
3477 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003478{
3479 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3480 struct weston_surface *parent;
3481
3482 if (parent_resource)
3483 parent = wl_resource_get_user_data(parent_resource);
3484 else
3485 parent = NULL;
3486
3487 shell_surface_set_parent(shsurf, parent);
3488}
3489
3490static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003491xdg_surface_set_app_id(struct wl_client *client,
3492 struct wl_resource *resource,
3493 const char *app_id)
3494{
3495 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3496
3497 free(shsurf->class);
3498 shsurf->class = strdup(app_id);
3499}
3500
3501static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003502xdg_surface_show_window_menu(struct wl_client *client,
3503 struct wl_resource *surface_resource,
3504 struct wl_resource *seat_resource,
3505 uint32_t serial,
3506 int32_t x,
3507 int32_t y)
3508{
3509 /* TODO */
3510}
3511
3512static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003513xdg_surface_set_title(struct wl_client *client,
3514 struct wl_resource *resource, const char *title)
3515{
3516 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3517
3518 set_title(shsurf, title);
3519}
3520
3521static void
3522xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3523 struct wl_resource *seat_resource, uint32_t serial)
3524{
3525 common_surface_move(resource, seat_resource, serial);
3526}
3527
3528static void
3529xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3530 struct wl_resource *seat_resource, uint32_t serial,
3531 uint32_t edges)
3532{
3533 common_surface_resize(resource, seat_resource, serial, edges);
3534}
3535
3536static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003537xdg_surface_ack_configure(struct wl_client *client,
3538 struct wl_resource *resource,
3539 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003540{
3541 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3542
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003543 if (shsurf->state_requested) {
3544 shsurf->next_state = shsurf->requested_state;
3545 shsurf->state_changed = true;
3546 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003547 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003548}
3549
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003550static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003551xdg_surface_set_window_geometry(struct wl_client *client,
3552 struct wl_resource *resource,
3553 int32_t x,
3554 int32_t y,
3555 int32_t width,
3556 int32_t height)
3557{
3558 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3559
3560 set_window_geometry(shsurf, x, y, width, height);
3561}
3562
3563static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003564xdg_surface_set_maximized(struct wl_client *client,
3565 struct wl_resource *resource)
3566{
3567 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3568
3569 shsurf->state_requested = true;
3570 shsurf->requested_state.maximized = true;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003571 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003572}
3573
3574static void
3575xdg_surface_unset_maximized(struct wl_client *client,
3576 struct wl_resource *resource)
3577{
3578 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3579
3580 shsurf->state_requested = true;
3581 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003582 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003583}
3584
3585static void
3586xdg_surface_set_fullscreen(struct wl_client *client,
3587 struct wl_resource *resource,
3588 struct wl_resource *output_resource)
3589{
3590 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3591 struct weston_output *output;
3592
3593 shsurf->state_requested = true;
3594 shsurf->requested_state.fullscreen = true;
3595
3596 if (output_resource)
3597 output = wl_resource_get_user_data(output_resource);
3598 else
3599 output = NULL;
3600
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003601 shell_surface_set_output(shsurf, output);
3602 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003603
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003604 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003605}
3606
3607static void
3608xdg_surface_unset_fullscreen(struct wl_client *client,
3609 struct wl_resource *resource)
3610{
3611 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3612
3613 shsurf->state_requested = true;
3614 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003615 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003616}
3617
3618static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003619xdg_surface_set_minimized(struct wl_client *client,
3620 struct wl_resource *resource)
3621{
3622 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3623
3624 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3625 return;
3626
3627 /* apply compositor's own minimization logic (hide) */
3628 set_minimized(shsurf->surface, 1);
3629}
3630
Rafael Antognollie2a34552013-12-03 15:35:45 -02003631static const struct xdg_surface_interface xdg_surface_implementation = {
3632 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003633 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003634 xdg_surface_set_title,
3635 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003636 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003637 xdg_surface_move,
3638 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003639 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003640 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003641 xdg_surface_set_maximized,
3642 xdg_surface_unset_maximized,
3643 xdg_surface_set_fullscreen,
3644 xdg_surface_unset_fullscreen,
3645 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003646};
3647
3648static void
3649xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003650 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003651{
3652 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003653 uint32_t *s;
3654 struct wl_array states;
3655 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003656
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003657 assert(shsurf);
3658
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003659 if (!shsurf->resource)
3660 return;
3661
3662 wl_array_init(&states);
3663 if (shsurf->requested_state.fullscreen) {
3664 s = wl_array_add(&states, sizeof *s);
3665 *s = XDG_SURFACE_STATE_FULLSCREEN;
3666 } else if (shsurf->requested_state.maximized) {
3667 s = wl_array_add(&states, sizeof *s);
3668 *s = XDG_SURFACE_STATE_MAXIMIZED;
3669 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003670 if (shsurf->resize_edges != 0) {
3671 s = wl_array_add(&states, sizeof *s);
3672 *s = XDG_SURFACE_STATE_RESIZING;
3673 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003674 if (shsurf->focus_count > 0) {
3675 s = wl_array_add(&states, sizeof *s);
3676 *s = XDG_SURFACE_STATE_ACTIVATED;
3677 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003678
3679 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3680 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3681
3682 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003683}
3684
3685static const struct weston_shell_client xdg_client = {
3686 xdg_send_configure
3687};
3688
3689static void
3690xdg_use_unstable_version(struct wl_client *client,
3691 struct wl_resource *resource,
3692 int32_t version)
3693{
3694 if (version > 1) {
3695 wl_resource_post_error(resource,
3696 1,
3697 "xdg-shell:: version not implemented yet.");
3698 return;
3699 }
3700}
3701
3702static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003703create_xdg_surface(struct shell_client *owner, void *shell,
3704 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003705 const struct weston_shell_client *client)
3706{
3707 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003708
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003709 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003710 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003711
3712 return shsurf;
3713}
3714
3715static void
3716xdg_get_xdg_surface(struct wl_client *client,
3717 struct wl_resource *resource,
3718 uint32_t id,
3719 struct wl_resource *surface_resource)
3720{
3721 struct weston_surface *surface =
3722 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003723 struct shell_client *sc = wl_resource_get_user_data(resource);
3724 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003725 struct shell_surface *shsurf;
3726
3727 if (get_shell_surface(surface)) {
3728 wl_resource_post_error(surface_resource,
3729 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003730 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003731 return;
3732 }
3733
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003734 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003735 if (!shsurf) {
3736 wl_resource_post_error(surface_resource,
3737 WL_DISPLAY_ERROR_INVALID_OBJECT,
3738 "surface->configure already set");
3739 return;
3740 }
3741
3742 shsurf->resource =
3743 wl_resource_create(client,
3744 &xdg_surface_interface, 1, id);
3745 wl_resource_set_implementation(shsurf->resource,
3746 &xdg_surface_implementation,
3747 shsurf, shell_destroy_shell_surface);
3748}
3749
3750static bool
3751shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3752{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003753 return shsurf->resource &&
3754 wl_resource_instance_of(shsurf->resource,
3755 &xdg_surface_interface,
3756 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003757}
3758
3759/* xdg-popup implementation */
3760
3761static void
3762xdg_popup_destroy(struct wl_client *client,
3763 struct wl_resource *resource)
3764{
3765 wl_resource_destroy(resource);
3766}
3767
Rafael Antognollie2a34552013-12-03 15:35:45 -02003768static const struct xdg_popup_interface xdg_popup_implementation = {
3769 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003770};
3771
3772static void
3773xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003774 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003775{
3776}
3777
3778static const struct weston_shell_client xdg_popup_client = {
3779 xdg_popup_send_configure
3780};
3781
3782static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003783create_xdg_popup(struct shell_client *owner, void *shell,
3784 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003785 const struct weston_shell_client *client,
3786 struct weston_surface *parent,
3787 struct shell_seat *seat,
3788 uint32_t serial,
3789 int32_t x, int32_t y)
3790{
3791 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003792
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003793 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003794 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003795 shsurf->popup.shseat = seat;
3796 shsurf->popup.serial = serial;
3797 shsurf->popup.x = x;
3798 shsurf->popup.y = y;
3799 shell_surface_set_parent(shsurf, parent);
3800
3801 return shsurf;
3802}
3803
3804static void
3805xdg_get_xdg_popup(struct wl_client *client,
3806 struct wl_resource *resource,
3807 uint32_t id,
3808 struct wl_resource *surface_resource,
3809 struct wl_resource *parent_resource,
3810 struct wl_resource *seat_resource,
3811 uint32_t serial,
3812 int32_t x, int32_t y, uint32_t flags)
3813{
3814 struct weston_surface *surface =
3815 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003816 struct shell_client *sc = wl_resource_get_user_data(resource);
3817 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003818 struct shell_surface *shsurf;
3819 struct weston_surface *parent;
3820 struct shell_seat *seat;
3821
3822 if (get_shell_surface(surface)) {
3823 wl_resource_post_error(surface_resource,
3824 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003825 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003826 return;
3827 }
3828
3829 if (!parent_resource) {
3830 wl_resource_post_error(surface_resource,
3831 WL_DISPLAY_ERROR_INVALID_OBJECT,
3832 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04003833 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003834 }
3835
3836 parent = wl_resource_get_user_data(parent_resource);
3837 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3838
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003839 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003840 parent, seat, serial, x, y);
3841 if (!shsurf) {
3842 wl_resource_post_error(surface_resource,
3843 WL_DISPLAY_ERROR_INVALID_OBJECT,
3844 "surface->configure already set");
3845 return;
3846 }
3847
3848 shsurf->resource =
3849 wl_resource_create(client,
3850 &xdg_popup_interface, 1, id);
3851 wl_resource_set_implementation(shsurf->resource,
3852 &xdg_popup_implementation,
3853 shsurf, shell_destroy_shell_surface);
3854}
3855
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003856static void
3857xdg_pong(struct wl_client *client,
3858 struct wl_resource *resource, uint32_t serial)
3859{
3860 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003861
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003862 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003863}
3864
Rafael Antognollie2a34552013-12-03 15:35:45 -02003865static bool
3866shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3867{
3868 return wl_resource_instance_of(shsurf->resource,
3869 &xdg_popup_interface,
3870 &xdg_popup_implementation);
3871}
3872
3873static const struct xdg_shell_interface xdg_implementation = {
3874 xdg_use_unstable_version,
3875 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003876 xdg_get_xdg_popup,
3877 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003878};
3879
3880static int
3881xdg_shell_unversioned_dispatch(const void *implementation,
3882 void *_target, uint32_t opcode,
3883 const struct wl_message *message,
3884 union wl_argument *args)
3885{
3886 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003887 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003888
3889 if (opcode != 0) {
3890 wl_resource_post_error(resource,
3891 WL_DISPLAY_ERROR_INVALID_OBJECT,
3892 "must call use_unstable_version first");
3893 return 0;
3894 }
3895
Pekka Paalanen71182ae2014-08-21 17:47:20 +03003896#define XDG_SERVER_VERSION 4
Rafael Antognollie2a34552013-12-03 15:35:45 -02003897
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003898 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3899 "shell implementation doesn't match protocol version");
3900
Rafael Antognollie2a34552013-12-03 15:35:45 -02003901 if (args[0].i != XDG_SERVER_VERSION) {
3902 wl_resource_post_error(resource,
3903 WL_DISPLAY_ERROR_INVALID_OBJECT,
3904 "incompatible version, server is %d "
3905 "client wants %d",
3906 XDG_SERVER_VERSION, args[0].i);
3907 return 0;
3908 }
3909
3910 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003911 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003912
3913 return 1;
3914}
3915
3916/* end of xdg-shell implementation */
3917/***********************************/
3918
Kristian Høgsberg07937562011-04-12 17:25:42 -04003919static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003920shell_fade(struct desktop_shell *shell, enum fade_type type);
3921
3922static int
3923screensaver_timeout(void *data)
3924{
3925 struct desktop_shell *shell = data;
3926
3927 shell_fade(shell, FADE_OUT);
3928
3929 return 1;
3930}
3931
3932static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003933handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003934{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003935 struct desktop_shell *shell =
3936 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003937
Pekka Paalanen18027e52011-12-02 16:31:49 +02003938 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003939
3940 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003941 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003942}
3943
3944static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003945launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003946{
3947 if (shell->screensaver.binding)
3948 return;
3949
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003950 if (!shell->screensaver.path) {
3951 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003952 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003953 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003954
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003955 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003956 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003957 return;
3958 }
3959
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003960 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003961 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003962 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003963 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003964}
3965
3966static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003967terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003968{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003969 if (shell->screensaver.process.pid == 0)
3970 return;
3971
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003972 /* Disarm the screensaver timer, otherwise it may fire when the
3973 * compositor is not in the idle state. In that case, the screen will
3974 * be locked, but the wake_signal won't fire on user input, making the
3975 * system unresponsive. */
3976 wl_event_source_timer_update(shell->screensaver.timer, 0);
3977
Pekka Paalanen18027e52011-12-02 16:31:49 +02003978 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003979}
3980
3981static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003982configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003983{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003984 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003985
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003986 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003987 if (v->output == ev->output && v != ev) {
3988 weston_view_unmap(v);
3989 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003990 }
3991 }
3992
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003993 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003994
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003995 if (wl_list_empty(&ev->layer_link.link)) {
3996 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003997 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003998 }
3999}
4000
4001static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004002background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004003{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004004 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004005 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004006
Jason Ekstranda7af7042013-10-12 22:38:11 -05004007 view = container_of(es->views.next, struct weston_view, surface_link);
4008
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004009 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004010}
4011
4012static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004013desktop_shell_set_background(struct wl_client *client,
4014 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004015 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004016 struct wl_resource *surface_resource)
4017{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004018 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004019 struct weston_surface *surface =
4020 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004021 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004022
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004023 if (surface->configure) {
4024 wl_resource_post_error(surface_resource,
4025 WL_DISPLAY_ERROR_INVALID_OBJECT,
4026 "surface role already assigned");
4027 return;
4028 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004029
Jason Ekstranda7af7042013-10-12 22:38:11 -05004030 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4031 weston_view_destroy(view);
4032 view = weston_view_create(surface);
4033
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004034 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004035 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004036 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004037 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004038 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004039 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004040 surface->output->width,
4041 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004042}
4043
4044static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004045panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004046{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004047 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004048 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004049
Jason Ekstranda7af7042013-10-12 22:38:11 -05004050 view = container_of(es->views.next, struct weston_view, surface_link);
4051
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004052 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004053}
4054
4055static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004056desktop_shell_set_panel(struct wl_client *client,
4057 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004058 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004059 struct wl_resource *surface_resource)
4060{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004061 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004062 struct weston_surface *surface =
4063 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004064 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004065
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004066 if (surface->configure) {
4067 wl_resource_post_error(surface_resource,
4068 WL_DISPLAY_ERROR_INVALID_OBJECT,
4069 "surface role already assigned");
4070 return;
4071 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004072
Jason Ekstranda7af7042013-10-12 22:38:11 -05004073 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4074 weston_view_destroy(view);
4075 view = weston_view_create(surface);
4076
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004077 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004078 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004079 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004080 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004081 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004082 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004083 surface->output->width,
4084 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004085}
4086
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004087static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004088lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004089{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004090 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004091 struct weston_view *view;
4092
4093 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004094
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004095 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004096 return;
4097
Jason Ekstranda7af7042013-10-12 22:38:11 -05004098 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004099
4100 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004101 weston_layer_entry_insert(&shell->lock_layer.view_list,
4102 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004103 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004104 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004105 }
4106}
4107
4108static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004109handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004110{
Tiago Vignattibe143262012-04-16 17:31:41 +03004111 struct desktop_shell *shell =
4112 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004113
Martin Minarik6d118362012-06-07 18:01:59 +02004114 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004115 shell->lock_surface = NULL;
4116}
4117
4118static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004119desktop_shell_set_lock_surface(struct wl_client *client,
4120 struct wl_resource *resource,
4121 struct wl_resource *surface_resource)
4122{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004123 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004124 struct weston_surface *surface =
4125 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004126
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004127 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004128
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004129 if (!shell->locked)
4130 return;
4131
Pekka Paalanen98262232011-12-01 10:42:22 +02004132 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004133
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004134 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004135 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004136 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004137
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004138 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004139 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004140 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004141}
4142
4143static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004144resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004145{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004146 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004147
Pekka Paalanen77346a62011-11-30 16:26:35 +02004148 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004149
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004150 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004151 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004152 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004153 &shell->input_panel_layer.link);
4154 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004155 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004156 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004157 wl_list_insert(&shell->compositor->cursor_layer.link,
4158 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004159 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004160 wl_list_insert(&shell->fullscreen_layer.link,
4161 &shell->panel_layer.link);
4162 wl_list_insert(&shell->panel_layer.link,
4163 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004164
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004165 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004166
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004167 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004168 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004169 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004170}
4171
4172static void
4173desktop_shell_unlock(struct wl_client *client,
4174 struct wl_resource *resource)
4175{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004176 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004177
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004178 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004179
4180 if (shell->locked)
4181 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004182}
4183
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004184static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004185desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004186 struct wl_resource *resource,
4187 struct wl_resource *surface_resource)
4188{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004189 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004190
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004191 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004192 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004193}
4194
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004195static void
4196desktop_shell_desktop_ready(struct wl_client *client,
4197 struct wl_resource *resource)
4198{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004199 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004200
4201 shell_fade_startup(shell);
4202}
4203
Jonny Lamb765760d2014-08-20 15:53:19 +02004204static void
4205desktop_shell_set_panel_position(struct wl_client *client,
4206 struct wl_resource *resource,
4207 uint32_t position)
4208{
4209 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4210
4211 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4212 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4213 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4214 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4215 wl_resource_post_error(resource,
4216 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4217 "bad position argument");
4218 return;
4219 }
4220
4221 shell->panel_position = position;
4222}
4223
Kristian Høgsberg75840622011-09-06 13:48:16 -04004224static const struct desktop_shell_interface desktop_shell_implementation = {
4225 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004226 desktop_shell_set_panel,
4227 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004228 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004229 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004230 desktop_shell_desktop_ready,
4231 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004232};
4233
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004234static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004235get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004236{
4237 struct shell_surface *shsurf;
4238
4239 shsurf = get_shell_surface(surface);
4240 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004241 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004242 return shsurf->type;
4243}
4244
Kristian Høgsberg75840622011-09-06 13:48:16 -04004245static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004246move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004247{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004248 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004249 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004250 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004251
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004252 if (seat->pointer->focus == NULL)
4253 return;
4254
4255 focus = seat->pointer->focus->surface;
4256
Pekka Paalanen01388e22013-04-25 13:57:44 +03004257 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004258 if (surface == NULL)
4259 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004260
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004261 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004262 if (shsurf == NULL || shsurf->state.fullscreen ||
4263 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004264 return;
4265
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004266 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004267}
4268
4269static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004270maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4271{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004272 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004273 struct weston_surface *surface;
4274 struct shell_surface *shsurf;
4275
4276 surface = weston_surface_get_main_surface(focus);
4277 if (surface == NULL)
4278 return;
4279
4280 shsurf = get_shell_surface(surface);
4281 if (shsurf == NULL)
4282 return;
4283
4284 if (!shell_surface_is_xdg_surface(shsurf))
4285 return;
4286
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004287 shsurf->state_requested = true;
4288 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004289 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004290}
4291
4292static void
4293fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4294{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004295 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004296 struct weston_surface *surface;
4297 struct shell_surface *shsurf;
4298
4299 surface = weston_surface_get_main_surface(focus);
4300 if (surface == NULL)
4301 return;
4302
4303 shsurf = get_shell_surface(surface);
4304 if (shsurf == NULL)
4305 return;
4306
4307 if (!shell_surface_is_xdg_surface(shsurf))
4308 return;
4309
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004310 shsurf->state_requested = true;
4311 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004312 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004313 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004314}
4315
4316static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004317touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4318{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004319 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004320 struct weston_surface *surface;
4321 struct shell_surface *shsurf;
4322
4323 surface = weston_surface_get_main_surface(focus);
4324 if (surface == NULL)
4325 return;
4326
4327 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004328 if (shsurf == NULL || shsurf->state.fullscreen ||
4329 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004330 return;
4331
4332 surface_touch_move(shsurf, (struct weston_seat *) seat);
4333}
4334
4335static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004336resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004337{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004338 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004339 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004340 uint32_t edges = 0;
4341 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004342 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004343
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004344 if (seat->pointer->focus == NULL)
4345 return;
4346
4347 focus = seat->pointer->focus->surface;
4348
Pekka Paalanen01388e22013-04-25 13:57:44 +03004349 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004350 if (surface == NULL)
4351 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004352
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004353 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004354 if (shsurf == NULL || shsurf->state.fullscreen ||
4355 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004356 return;
4357
Jason Ekstranda7af7042013-10-12 22:38:11 -05004358 weston_view_from_global(shsurf->view,
4359 wl_fixed_to_int(seat->pointer->grab_x),
4360 wl_fixed_to_int(seat->pointer->grab_y),
4361 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004362
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004363 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004364 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004365 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004366 edges |= 0;
4367 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004368 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004369
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004370 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004371 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004372 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004373 edges |= 0;
4374 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004375 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004376
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004377 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004378}
4379
4380static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004381surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004382 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004383{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004384 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004385 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004386 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004387 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004388
Pekka Paalanen01388e22013-04-25 13:57:44 +03004389 /* XXX: broken for windows containing sub-surfaces */
4390 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004391 if (surface == NULL)
4392 return;
4393
4394 shsurf = get_shell_surface(surface);
4395 if (!shsurf)
4396 return;
4397
Jason Ekstranda7af7042013-10-12 22:38:11 -05004398 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004399
Jason Ekstranda7af7042013-10-12 22:38:11 -05004400 if (shsurf->view->alpha > 1.0)
4401 shsurf->view->alpha = 1.0;
4402 if (shsurf->view->alpha < step)
4403 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004404
Jason Ekstranda7af7042013-10-12 22:38:11 -05004405 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004406 weston_surface_damage(surface);
4407}
4408
4409static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004410do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004411 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004412{
Daniel Stone37816df2012-05-16 18:45:18 +01004413 struct weston_seat *ws = (struct weston_seat *) seat;
4414 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004415 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004416 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004417
4418 wl_list_for_each(output, &compositor->output_list, link) {
4419 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004420 wl_fixed_to_double(seat->pointer->x),
4421 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004422 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004423 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004424 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004425 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004426 increment = -output->zoom.increment;
4427 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004428 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004429 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004430 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004431 else
4432 increment = 0;
4433
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004434 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004435
Scott Moreaue6603982012-06-11 13:07:51 -06004436 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004437 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004438 else if (output->zoom.level > output->zoom.max_level)
4439 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004440 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004441 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004442 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004443
Scott Moreaue6603982012-06-11 13:07:51 -06004444 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004445
Jason Ekstranda7af7042013-10-12 22:38:11 -05004446 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004447 }
4448 }
4449}
4450
Scott Moreauccbf29d2012-02-22 14:21:41 -07004451static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004452zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004453 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004454{
4455 do_zoom(seat, time, 0, axis, value);
4456}
4457
4458static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004459zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004460 void *data)
4461{
4462 do_zoom(seat, time, key, 0, 0);
4463}
4464
4465static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004466terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004467 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004468{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004469 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004470
Daniel Stone325fc2d2012-05-30 16:31:58 +01004471 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004472}
4473
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004474static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004475rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4476 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004477{
4478 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004479 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004480 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004481 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004482 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004483
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004484 weston_pointer_move(pointer, x, y);
4485
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004486 if (!shsurf)
4487 return;
4488
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004489 cx = 0.5f * shsurf->surface->width;
4490 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004491
Daniel Stone37816df2012-05-16 18:45:18 +01004492 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4493 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004494 r = sqrtf(dx * dx + dy * dy);
4495
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004496 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004497 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004498
4499 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004500 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004501 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004502
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004503 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004504 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004505
4506 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004507 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004508 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004509 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004510 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004511
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004512 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004513 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004514 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004515 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004516 wl_list_init(&shsurf->rotation.transform.link);
4517 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004518 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004519 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004520
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004521 /* We need to adjust the position of the surface
4522 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004523 cposx = shsurf->view->geometry.x + cx;
4524 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004525 dposx = rotate->center.x - cposx;
4526 dposy = rotate->center.y - cposy;
4527 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004528 weston_view_set_position(shsurf->view,
4529 shsurf->view->geometry.x + dposx,
4530 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004531 }
4532
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004533 /* Repaint implies weston_surface_update_transform(), which
4534 * lazily applies the damage due to rotation update.
4535 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004536 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004537}
4538
4539static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004540rotate_grab_button(struct weston_pointer_grab *grab,
4541 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004542{
4543 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004544 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004545 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004546 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004547 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004548
Daniel Stone4dbadb12012-05-30 16:31:51 +01004549 if (pointer->button_count == 0 &&
4550 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004551 if (shsurf)
4552 weston_matrix_multiply(&shsurf->rotation.rotation,
4553 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004554 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004555 free(rotate);
4556 }
4557}
4558
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004559static void
4560rotate_grab_cancel(struct weston_pointer_grab *grab)
4561{
4562 struct rotate_grab *rotate =
4563 container_of(grab, struct rotate_grab, base.grab);
4564
4565 shell_grab_end(&rotate->base);
4566 free(rotate);
4567}
4568
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004569static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004570 noop_grab_focus,
4571 rotate_grab_motion,
4572 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004573 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004574};
4575
4576static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004577surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004578{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004579 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004580 float dx, dy;
4581 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004582
Pekka Paalanen460099f2012-01-20 16:48:25 +02004583 rotate = malloc(sizeof *rotate);
4584 if (!rotate)
4585 return;
4586
Jason Ekstranda7af7042013-10-12 22:38:11 -05004587 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004588 surface->surface->width * 0.5f,
4589 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004590 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004591
Daniel Stone37816df2012-05-16 18:45:18 +01004592 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4593 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004594 r = sqrtf(dx * dx + dy * dy);
4595 if (r > 20.0f) {
4596 struct weston_matrix inverse;
4597
4598 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004599 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004600 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004601
4602 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004603 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004604 } else {
4605 weston_matrix_init(&surface->rotation.rotation);
4606 weston_matrix_init(&rotate->rotation);
4607 }
4608
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004609 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4610 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004611}
4612
4613static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004614rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004615 void *data)
4616{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004617 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004618 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004619 struct shell_surface *surface;
4620
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004621 if (seat->pointer->focus == NULL)
4622 return;
4623
4624 focus = seat->pointer->focus->surface;
4625
Pekka Paalanen01388e22013-04-25 13:57:44 +03004626 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004627 if (base_surface == NULL)
4628 return;
4629
4630 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004631 if (surface == NULL || surface->state.fullscreen ||
4632 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004633 return;
4634
4635 surface_rotate(surface, seat);
4636}
4637
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004638/* Move all fullscreen layers down to the current workspace and hide their
4639 * black views. The surfaces' state is set to both fullscreen and lowered,
4640 * and this is reversed when such a surface is re-configured, see
4641 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4642 *
4643 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004644 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004645void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004646lower_fullscreen_layer(struct desktop_shell *shell)
4647{
4648 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004649 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004650
4651 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004652 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004653 &shell->fullscreen_layer.view_list.link,
4654 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004655 struct shell_surface *shsurf = get_shell_surface(view->surface);
4656
4657 if (!shsurf)
4658 continue;
4659
4660 /* We can have a non-fullscreen popup for a fullscreen surface
4661 * in the fullscreen layer. */
4662 if (shsurf->state.fullscreen) {
4663 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004664 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4665 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004666 weston_view_damage_below(shsurf->fullscreen.black_view);
4667
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004668 }
4669
4670 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004671 weston_layer_entry_remove(&view->layer_link);
4672 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004673 weston_view_damage_below(view);
4674 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004675
4676 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004677 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004678}
4679
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004680void
Tiago Vignattibe143262012-04-16 17:31:41 +03004681activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004682 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004683{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004684 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004685 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004686 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004687 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004688 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004689
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004690 lower_fullscreen_layer(shell);
4691
Pekka Paalanen01388e22013-04-25 13:57:44 +03004692 main_surface = weston_surface_get_main_surface(es);
4693
Daniel Stone37816df2012-05-16 18:45:18 +01004694 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004695
Jonas Ådahl8538b222012-08-29 22:13:03 +02004696 state = ensure_focus_state(shell, seat);
4697 if (state == NULL)
4698 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004699
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004700 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004701 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004702
Rafael Antognolli03b16592013-12-03 15:35:42 -02004703 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004704 assert(shsurf);
4705
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004706 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004707 shell_configure_fullscreen(shsurf);
4708 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004709 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004710
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004711 /* Update the surface’s layer. This brings it to the top of the stacking
4712 * order as appropriate. */
4713 shell_surface_update_layer(shsurf);
4714
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004715 if (shell->focus_animation_type != ANIMATION_NONE) {
4716 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004717 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004718 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004719}
4720
Alex Wu21858432012-04-01 20:13:08 +08004721/* no-op func for checking black surface */
4722static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004723black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004724{
4725}
4726
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004727static bool
Alex Wu21858432012-04-01 20:13:08 +08004728is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4729{
4730 if (es->configure == black_surface_configure) {
4731 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004732 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004733 return true;
4734 }
4735 return false;
4736}
4737
Kristian Høgsberg75840622011-09-06 13:48:16 -04004738static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004739activate_binding(struct weston_seat *seat,
4740 struct desktop_shell *shell,
4741 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004742{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004743 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004744
Alex Wu9c35e6b2012-03-05 14:13:13 +08004745 if (!focus)
4746 return;
4747
Pekka Paalanen01388e22013-04-25 13:57:44 +03004748 if (is_black_surface(focus, &main_surface))
4749 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004750
Pekka Paalanen01388e22013-04-25 13:57:44 +03004751 main_surface = weston_surface_get_main_surface(focus);
4752 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004753 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004754
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004755 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004756}
4757
4758static void
4759click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4760 void *data)
4761{
4762 if (seat->pointer->grab != &seat->pointer->default_grab)
4763 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004764 if (seat->pointer->focus == NULL)
4765 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004766
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004767 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004768}
4769
4770static void
4771touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4772{
4773 if (seat->touch->grab != &seat->touch->default_grab)
4774 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004775 if (seat->touch->focus == NULL)
4776 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004777
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004778 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004779}
4780
4781static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004782unfocus_all_seats(struct desktop_shell *shell)
4783{
4784 struct weston_seat *seat, *next;
4785
4786 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4787 if (seat->keyboard == NULL)
4788 continue;
4789
4790 weston_keyboard_set_focus(seat->keyboard, NULL);
4791 }
4792}
4793
4794static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004795lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004796{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004797 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004798
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004799 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004800 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004801 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004802 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004803
4804 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004805
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004806 /* Hide all surfaces by removing the fullscreen, panel and
4807 * toplevel layers. This way nothing else can show or receive
4808 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004809
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004810 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004811 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004812 if (shell->showing_input_panels)
4813 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004814 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004815 wl_list_insert(&shell->compositor->cursor_layer.link,
4816 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004817
Pekka Paalanen77346a62011-11-30 16:26:35 +02004818 launch_screensaver(shell);
4819
Neil Robertsb4a91702014-04-09 16:33:32 +01004820 /* Remove the keyboard focus on all seats. This will be
4821 * restored to the workspace's saved state via
4822 * restore_focus_state when the compositor is unlocked */
4823 unfocus_all_seats(shell);
4824
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004825 /* TODO: disable bindings that should not work while locked. */
4826
4827 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004828}
4829
4830static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004831unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004832{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004833 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004834 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004835 return;
4836 }
4837
4838 /* If desktop-shell client has gone away, unlock immediately. */
4839 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004840 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004841 return;
4842 }
4843
4844 if (shell->prepare_event_sent)
4845 return;
4846
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004847 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004848 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004849}
4850
4851static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004852shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004853{
4854 struct desktop_shell *shell = data;
4855
4856 shell->fade.animation = NULL;
4857
4858 switch (shell->fade.type) {
4859 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004860 weston_surface_destroy(shell->fade.view->surface);
4861 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004862 break;
4863 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004864 lock(shell);
4865 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004866 default:
4867 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004868 }
4869}
4870
Jason Ekstranda7af7042013-10-12 22:38:11 -05004871static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004872shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004873{
4874 struct weston_compositor *compositor = shell->compositor;
4875 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004876 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004877
4878 surface = weston_surface_create(compositor);
4879 if (!surface)
4880 return NULL;
4881
Jason Ekstranda7af7042013-10-12 22:38:11 -05004882 view = weston_view_create(surface);
4883 if (!view) {
4884 weston_surface_destroy(surface);
4885 return NULL;
4886 }
4887
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004888 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004889 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004890 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004891 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4892 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004893 pixman_region32_init(&surface->input);
4894
Jason Ekstranda7af7042013-10-12 22:38:11 -05004895 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004896}
4897
4898static void
4899shell_fade(struct desktop_shell *shell, enum fade_type type)
4900{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004901 float tint;
4902
4903 switch (type) {
4904 case FADE_IN:
4905 tint = 0.0;
4906 break;
4907 case FADE_OUT:
4908 tint = 1.0;
4909 break;
4910 default:
4911 weston_log("shell: invalid fade type\n");
4912 return;
4913 }
4914
4915 shell->fade.type = type;
4916
Jason Ekstranda7af7042013-10-12 22:38:11 -05004917 if (shell->fade.view == NULL) {
4918 shell->fade.view = shell_fade_create_surface(shell);
4919 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004920 return;
4921
Jason Ekstranda7af7042013-10-12 22:38:11 -05004922 shell->fade.view->alpha = 1.0 - tint;
4923 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004924 }
4925
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004926 if (shell->fade.view->output == NULL) {
4927 /* If the black view gets a NULL output, we lost the
4928 * last output and we'll just cancel the fade. This
4929 * happens when you close the last window under the
4930 * X11 or Wayland backends. */
4931 shell->locked = false;
4932 weston_surface_destroy(shell->fade.view->surface);
4933 shell->fade.view = NULL;
4934 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004935 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004936 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004937 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004938 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004939 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004940 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004941 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004942}
4943
4944static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004945do_shell_fade_startup(void *data)
4946{
4947 struct desktop_shell *shell = data;
4948
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004949 if (shell->startup_animation_type == ANIMATION_FADE)
4950 shell_fade(shell, FADE_IN);
4951 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004952 weston_surface_destroy(shell->fade.view->surface);
4953 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004954 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004955}
4956
4957static void
4958shell_fade_startup(struct desktop_shell *shell)
4959{
4960 struct wl_event_loop *loop;
4961
4962 if (!shell->fade.startup_timer)
4963 return;
4964
4965 wl_event_source_remove(shell->fade.startup_timer);
4966 shell->fade.startup_timer = NULL;
4967
4968 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4969 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4970}
4971
4972static int
4973fade_startup_timeout(void *data)
4974{
4975 struct desktop_shell *shell = data;
4976
4977 shell_fade_startup(shell);
4978 return 0;
4979}
4980
4981static void
4982shell_fade_init(struct desktop_shell *shell)
4983{
4984 /* Make compositor output all black, and wait for the desktop-shell
4985 * client to signal it is ready, then fade in. The timer triggers a
4986 * fade-in, in case the desktop-shell client takes too long.
4987 */
4988
4989 struct wl_event_loop *loop;
4990
Jason Ekstranda7af7042013-10-12 22:38:11 -05004991 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004992 weston_log("%s: warning: fade surface already exists\n",
4993 __func__);
4994 return;
4995 }
4996
Jason Ekstranda7af7042013-10-12 22:38:11 -05004997 shell->fade.view = shell_fade_create_surface(shell);
4998 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004999 return;
5000
Jason Ekstranda7af7042013-10-12 22:38:11 -05005001 weston_view_update_transform(shell->fade.view);
5002 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005003
5004 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5005 shell->fade.startup_timer =
5006 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5007 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5008}
5009
5010static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005011idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005012{
5013 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005014 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005015 struct weston_seat *seat;
5016
5017 wl_list_for_each(seat, &shell->compositor->seat_list, link)
5018 if (seat->pointer)
5019 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005020
5021 shell_fade(shell, FADE_OUT);
5022 /* lock() is called from shell_fade_done() */
5023}
5024
5025static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005026wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005027{
5028 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005029 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005030
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005031 unlock(shell);
5032}
5033
5034static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005035center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005036{
Giulio Camuffob8366642013-04-25 13:57:46 +03005037 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005038 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005039
Jason Ekstranda7af7042013-10-12 22:38:11 -05005040 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005041
5042 x = output->x + (output->width - width) / 2 - surf_x / 2;
5043 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005044
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005045 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005046}
5047
5048static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005049weston_view_set_initial_position(struct weston_view *view,
5050 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005051{
5052 struct weston_compositor *compositor = shell->compositor;
5053 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005054 int32_t range_x, range_y;
5055 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005056 struct weston_output *output, *target_output = NULL;
5057 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005058 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005059
5060 /* As a heuristic place the new window on the same output as the
5061 * pointer. Falling back to the output containing 0, 0.
5062 *
5063 * TODO: Do something clever for touch too?
5064 */
5065 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005066 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005067 ix = wl_fixed_to_int(seat->pointer->x);
5068 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005069 break;
5070 }
5071 }
5072
5073 wl_list_for_each(output, &compositor->output_list, link) {
5074 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5075 target_output = output;
5076 break;
5077 }
5078 }
5079
5080 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005081 weston_view_set_position(view, 10 + random() % 400,
5082 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005083 return;
5084 }
5085
5086 /* Valid range within output where the surface will still be onscreen.
5087 * If this is negative it means that the surface is bigger than
5088 * output.
5089 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005090 get_output_work_area(shell, target_output, &area);
5091
5092 dx = area.x;
5093 dy = area.y;
5094 range_x = area.width - view->surface->width;
5095 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005096
Rob Bradford4cb88c72012-08-13 15:18:44 +01005097 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005098 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005099
5100 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005101 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005102
5103 x = target_output->x + dx;
5104 y = target_output->y + dy;
5105
Jason Ekstranda7af7042013-10-12 22:38:11 -05005106 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005107}
5108
5109static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005110set_maximized_position(struct desktop_shell *shell,
5111 struct shell_surface *shsurf)
5112{
5113 int32_t surf_x, surf_y;
5114 pixman_rectangle32_t area;
5115
5116 /* use surface configure to set the geometry */
5117 get_output_work_area(shell, shsurf->output, &area);
5118 surface_subsurfaces_boundingbox(shsurf->surface,
5119 &surf_x, &surf_y, NULL, NULL);
5120
5121 weston_view_set_position(shsurf->view,
5122 area.x - surf_x,
5123 area.y - surf_y);
5124}
5125
5126static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005127map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005128 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005129{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005130 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005131 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005132
Pekka Paalanen77346a62011-11-30 16:26:35 +02005133 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005134 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005135 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005136 if (shsurf->state.fullscreen) {
5137 center_on_output(shsurf->view, shsurf->fullscreen_output);
5138 shell_map_fullscreen(shsurf);
5139 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005140 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005141 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005142 weston_view_set_initial_position(shsurf->view, shell);
5143 }
Juan Zhao96879df2012-02-07 08:45:41 +08005144 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005145 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005146 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005147 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005148 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005149 weston_view_set_position(shsurf->view,
5150 shsurf->view->geometry.x + sx,
5151 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005152 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005153 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005154 default:
5155 ;
5156 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005157
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005158 /* Surface stacking order, see also activate(). */
5159 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005160
Jason Ekstranda7af7042013-10-12 22:38:11 -05005161 if (shsurf->type != SHELL_SURFACE_NONE) {
5162 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005163 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005164 shsurf->surface->output = shsurf->output;
5165 shsurf->view->output = shsurf->output;
5166 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005167 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005168
Jason Ekstranda7af7042013-10-12 22:38:11 -05005169 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005170 /* XXX: xwayland's using the same fields for transient type */
5171 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005172 if (shsurf->transient.flags ==
5173 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5174 break;
5175 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005176 if (shsurf->state.relative &&
5177 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5178 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005179 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005180 break;
5181 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005182 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005183 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005184 case SHELL_SURFACE_POPUP:
5185 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005186 default:
5187 break;
5188 }
5189
Rafael Antognolli03b16592013-12-03 15:35:42 -02005190 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5191 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005192 {
5193 switch (shell->win_animation_type) {
5194 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005195 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005196 break;
5197 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005198 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005199 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005200 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005201 default:
5202 break;
5203 }
5204 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005205}
5206
5207static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005208configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005209 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005210{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005211 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005212 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005213
Pekka Paalanen77346a62011-11-30 16:26:35 +02005214 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005215
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005216 assert(shsurf);
5217
Rafael Antognolli03b16592013-12-03 15:35:42 -02005218 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005219 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005220 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005221 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005222 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005223 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005224 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005225
Alex Wu4539b082012-03-01 12:57:46 +08005226 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005227 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005228 wl_list_for_each(view, &surface->views, surface_link)
5229 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005230
Rafael Antognolli03b16592013-12-03 15:35:42 -02005231 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005232 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005233 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005234}
5235
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005236static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005237shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005238{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005239 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005240 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005241 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005242
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005243 assert(shsurf);
5244
5245 shell = shsurf->shell;
5246
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005247 if (!weston_surface_is_mapped(es) &&
5248 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005249 remove_popup_grab(shsurf);
5250 }
5251
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005252 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005253 return;
5254
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005255 if (shsurf->has_next_geometry) {
5256 shsurf->geometry = shsurf->next_geometry;
5257 shsurf->has_next_geometry = false;
5258 shsurf->has_set_geometry = true;
5259 } else if (!shsurf->has_set_geometry) {
5260 surface_subsurfaces_boundingbox(shsurf->surface,
5261 &shsurf->geometry.x,
5262 &shsurf->geometry.y,
5263 &shsurf->geometry.width,
5264 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005265 }
5266
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005267 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005268 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005269 type_changed = 1;
5270 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005271
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005272 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005273 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005274 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005275 shsurf->last_width != es->width ||
5276 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005277 float from_x, from_y;
5278 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005279
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005280 if (shsurf->resize_edges) {
5281 sx = 0;
5282 sy = 0;
5283 }
5284
5285 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5286 sx = shsurf->last_width - es->width;
5287 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5288 sy = shsurf->last_height - es->height;
5289
5290 shsurf->last_width = es->width;
5291 shsurf->last_height = es->height;
5292
Jason Ekstranda7af7042013-10-12 22:38:11 -05005293 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5294 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005295 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005296 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005297 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005298 }
5299}
5300
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005301static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005302
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005303static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005304respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005305{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005306 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005307
5308 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5309 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005310 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005311 shell->child.deathstamp = time;
5312 shell->child.deathcount = 0;
5313 }
5314
5315 shell->child.deathcount++;
5316 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005317 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005318 return;
5319 }
5320
Pekka Paalanen826dc142014-08-27 12:11:53 +03005321 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005322 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005323}
5324
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005325static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005326desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5327{
5328 struct desktop_shell *shell;
5329
5330 shell = container_of(listener, struct desktop_shell,
5331 child.client_destroy_listener);
5332
Pekka Paalanen826dc142014-08-27 12:11:53 +03005333 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005334 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005335 /*
5336 * unbind_desktop_shell() will reset shell->child.desktop_shell
5337 * before the respawned process has a chance to create a new
5338 * desktop_shell object, because we are being called from the
5339 * wl_client destructor which destroys all wl_resources before
5340 * returning.
5341 */
5342
5343 respawn_desktop_shell_process(shell);
5344 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005345}
5346
5347static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005348launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005349{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005350 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005351
Pekka Paalanen826dc142014-08-27 12:11:53 +03005352 shell->child.client = weston_client_start(shell->compositor,
5353 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005354
Arnaud Vrac42631192014-08-25 20:56:46 +02005355 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005356 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005357 return;
5358 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005359
5360 shell->child.client_destroy_listener.notify =
5361 desktop_shell_client_destroy;
5362 wl_client_add_destroy_listener(shell->child.client,
5363 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005364}
5365
5366static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005367handle_shell_client_destroy(struct wl_listener *listener, void *data)
5368{
5369 struct shell_client *sc =
5370 container_of(listener, struct shell_client, destroy_listener);
5371
5372 if (sc->ping_timer)
5373 wl_event_source_remove(sc->ping_timer);
5374 free(sc);
5375}
5376
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005377static struct shell_client *
5378shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5379 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005380{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005381 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005382
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005383 sc = zalloc(sizeof *sc);
5384 if (sc == NULL) {
5385 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005386 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005387 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005388
5389 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005390 if (sc->resource == NULL) {
5391 free(sc);
5392 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005393 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005394 }
5395
5396 sc->client = client;
5397 sc->shell = shell;
5398 sc->destroy_listener.notify = handle_shell_client_destroy;
5399 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5400
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005401 return sc;
5402}
5403
5404static void
5405bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5406{
5407 struct desktop_shell *shell = data;
5408 struct shell_client *sc;
5409
5410 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5411 if (sc)
5412 wl_resource_set_implementation(sc->resource,
5413 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005414 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005415}
5416
5417static void
5418bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5419{
5420 struct desktop_shell *shell = data;
5421 struct shell_client *sc;
5422
5423 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5424 if (sc)
5425 wl_resource_set_dispatcher(sc->resource,
5426 xdg_shell_unversioned_dispatch,
5427 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005428}
5429
5430static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005431unbind_desktop_shell(struct wl_resource *resource)
5432{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005433 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005434
5435 if (shell->locked)
5436 resume_desktop(shell);
5437
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005438 shell->child.desktop_shell = NULL;
5439 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005440}
5441
5442static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005443bind_desktop_shell(struct wl_client *client,
5444 void *data, uint32_t version, uint32_t id)
5445{
Tiago Vignattibe143262012-04-16 17:31:41 +03005446 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005447 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005448
Jason Ekstranda85118c2013-06-27 20:17:02 -05005449 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005450 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005451
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005452 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005453 wl_resource_set_implementation(resource,
5454 &desktop_shell_implementation,
5455 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005456 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005457
5458 if (version < 2)
5459 shell_fade_startup(shell);
5460
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005461 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005462 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005463
5464 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5465 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005466}
5467
Pekka Paalanen6e168112011-11-24 11:34:05 +02005468static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005469screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005470{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005471 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005472 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005473 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005474
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005475 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005476 return;
5477
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005478 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005479 if (!shell->locked)
5480 return;
5481
Jason Ekstranda7af7042013-10-12 22:38:11 -05005482 view = container_of(surface->views.next, struct weston_view, surface_link);
5483 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005484
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005485 if (wl_list_empty(&view->layer_link.link)) {
5486 prev = container_of(shell->lock_layer.view_list.link.prev,
5487 struct weston_layer_entry, link);
5488 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005489 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005490 wl_event_source_timer_update(shell->screensaver.timer,
5491 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005492 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005493 }
5494}
5495
5496static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005497screensaver_set_surface(struct wl_client *client,
5498 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005499 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005500 struct wl_resource *output_resource)
5501{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005502 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005503 struct weston_surface *surface =
5504 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005505 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005506 struct weston_view *view, *next;
5507
5508 /* Make sure we only have one view */
5509 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5510 weston_view_destroy(view);
5511 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005512
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005513 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005514 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005515 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005516}
5517
5518static const struct screensaver_interface screensaver_implementation = {
5519 screensaver_set_surface
5520};
5521
5522static void
5523unbind_screensaver(struct wl_resource *resource)
5524{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005525 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005526
Pekka Paalanen77346a62011-11-30 16:26:35 +02005527 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005528}
5529
5530static void
5531bind_screensaver(struct wl_client *client,
5532 void *data, uint32_t version, uint32_t id)
5533{
Tiago Vignattibe143262012-04-16 17:31:41 +03005534 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005535 struct wl_resource *resource;
5536
Jason Ekstranda85118c2013-06-27 20:17:02 -05005537 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005538
Pekka Paalanen77346a62011-11-30 16:26:35 +02005539 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005540 wl_resource_set_implementation(resource,
5541 &screensaver_implementation,
5542 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005543 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005544 return;
5545 }
5546
5547 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5548 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005549}
5550
Kristian Høgsberg07045392012-02-19 18:52:44 -05005551struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005552 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005553 struct weston_surface *current;
5554 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005555 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005556 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005557};
5558
5559static void
5560switcher_next(struct switcher *switcher)
5561{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005562 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005563 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005564 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005565 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005566
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005567 /* temporary re-display minimized surfaces */
5568 struct weston_view *tmp;
5569 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005570 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5571 weston_layer_entry_remove(&view->layer_link);
5572 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005573 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5574 *minimized = view;
5575 }
5576
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005577 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005578 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005579 if (shsurf &&
5580 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5581 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005582 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005583 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005584 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005585 next = view->surface;
5586 prev = view->surface;
5587 view->alpha = 0.25;
5588 weston_view_geometry_dirty(view);
5589 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005590 }
Alex Wu1659daa2012-04-01 20:13:09 +08005591
Jason Ekstranda7af7042013-10-12 22:38:11 -05005592 if (is_black_surface(view->surface, NULL)) {
5593 view->alpha = 0.25;
5594 weston_view_geometry_dirty(view);
5595 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005596 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005597 }
5598
5599 if (next == NULL)
5600 next = first;
5601
Alex Wu07b26062012-03-12 16:06:01 +08005602 if (next == NULL)
5603 return;
5604
Kristian Høgsberg07045392012-02-19 18:52:44 -05005605 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005606 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005607
5608 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005609 wl_list_for_each(view, &next->views, surface_link)
5610 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005611
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005612 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005613 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005614 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005615}
5616
5617static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005618switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005619{
5620 struct switcher *switcher =
5621 container_of(listener, struct switcher, listener);
5622
5623 switcher_next(switcher);
5624}
5625
5626static void
Daniel Stone351eb612012-05-31 15:27:47 -04005627switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005628{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005629 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005630 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005631 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005632
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005633 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005634 if (is_focus_view(view))
5635 continue;
5636
Jason Ekstranda7af7042013-10-12 22:38:11 -05005637 view->alpha = 1.0;
5638 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005639 }
5640
Alex Wu07b26062012-03-12 16:06:01 +08005641 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005642 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005643 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005644 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005645 weston_keyboard_end_grab(keyboard);
5646 if (keyboard->input_method_resource)
5647 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005648
5649 /* re-hide surfaces that were temporary shown during the switch */
5650 struct weston_view **minimized;
5651 wl_array_for_each(minimized, &switcher->minimized_array) {
5652 /* with the exception of the current selected */
5653 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005654 weston_layer_entry_remove(&(*minimized)->layer_link);
5655 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005656 weston_view_damage_below(*minimized);
5657 }
5658 }
5659 wl_array_release(&switcher->minimized_array);
5660
Kristian Høgsberg07045392012-02-19 18:52:44 -05005661 free(switcher);
5662}
5663
5664static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005665switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005666 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005667{
5668 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005669 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005670
Daniel Stonec9785ea2012-05-30 16:31:52 +01005671 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005672 switcher_next(switcher);
5673}
5674
5675static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005676switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005677 uint32_t mods_depressed, uint32_t mods_latched,
5678 uint32_t mods_locked, uint32_t group)
5679{
5680 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005681 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005682
Daniel Stone351eb612012-05-31 15:27:47 -04005683 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5684 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005685}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005686
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005687static void
5688switcher_cancel(struct weston_keyboard_grab *grab)
5689{
5690 struct switcher *switcher = container_of(grab, struct switcher, grab);
5691
5692 switcher_destroy(switcher);
5693}
5694
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005695static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005696 switcher_key,
5697 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005698 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005699};
5700
5701static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005702switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005703 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005704{
Tiago Vignattibe143262012-04-16 17:31:41 +03005705 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005706 struct switcher *switcher;
5707
5708 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005709 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005710 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005711 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005712 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005713 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005714
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005715 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005716 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005717 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005718 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5719 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005720 switcher_next(switcher);
5721}
5722
Pekka Paalanen3c647232011-12-22 13:43:43 +02005723static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005724backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005725 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005726{
5727 struct weston_compositor *compositor = data;
5728 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005729 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005730
5731 /* TODO: we're limiting to simple use cases, where we assume just
5732 * control on the primary display. We'd have to extend later if we
5733 * ever get support for setting backlights on random desktop LCD
5734 * panels though */
5735 output = get_default_output(compositor);
5736 if (!output)
5737 return;
5738
5739 if (!output->set_backlight)
5740 return;
5741
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005742 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5743 backlight_new = output->backlight_current - 25;
5744 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5745 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005746
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005747 if (backlight_new < 5)
5748 backlight_new = 5;
5749 if (backlight_new > 255)
5750 backlight_new = 255;
5751
5752 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005753 output->set_backlight(output, output->backlight_current);
5754}
5755
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005756struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005757 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005758 struct weston_seat *seat;
5759 uint32_t key[2];
5760 int key_released[2];
5761};
5762
5763static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005764debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005765 uint32_t key, uint32_t state)
5766{
5767 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005768 struct weston_compositor *ec = db->seat->compositor;
5769 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005770 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005771 uint32_t serial;
5772 int send = 0, terminate = 0;
5773 int check_binding = 1;
5774 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005775 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005776
5777 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5778 /* Do not run bindings on key releases */
5779 check_binding = 0;
5780
5781 for (i = 0; i < 2; i++)
5782 if (key == db->key[i])
5783 db->key_released[i] = 1;
5784
5785 if (db->key_released[0] && db->key_released[1]) {
5786 /* All key releases been swalled so end the grab */
5787 terminate = 1;
5788 } else if (key != db->key[0] && key != db->key[1]) {
5789 /* Should not swallow release of other keys */
5790 send = 1;
5791 }
5792 } else if (key == db->key[0] && !db->key_released[0]) {
5793 /* Do not check bindings for the first press of the binding
5794 * key. This allows it to be used as a debug shortcut.
5795 * We still need to swallow this event. */
5796 check_binding = 0;
5797 } else if (db->key[1]) {
5798 /* If we already ran a binding don't process another one since
5799 * we can't keep track of all the binding keys that were
5800 * pressed in order to swallow the release events. */
5801 send = 1;
5802 check_binding = 0;
5803 }
5804
5805 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005806 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5807 key, state)) {
5808 /* We ran a binding so swallow the press and keep the
5809 * grab to swallow the released too. */
5810 send = 0;
5811 terminate = 0;
5812 db->key[1] = key;
5813 } else {
5814 /* Terminate the grab since the key pressed is not a
5815 * debug binding key. */
5816 send = 1;
5817 terminate = 1;
5818 }
5819 }
5820
5821 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005822 serial = wl_display_next_serial(display);
5823 resource_list = &grab->keyboard->focus_resource_list;
5824 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005825 wl_keyboard_send_key(resource, serial, time, key, state);
5826 }
5827 }
5828
5829 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005830 weston_keyboard_end_grab(grab->keyboard);
5831 if (grab->keyboard->input_method_resource)
5832 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005833 free(db);
5834 }
5835}
5836
5837static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005838debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005839 uint32_t mods_depressed, uint32_t mods_latched,
5840 uint32_t mods_locked, uint32_t group)
5841{
5842 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005843 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005844
Neil Roberts96d790e2013-09-19 17:32:00 +01005845 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005846
Neil Roberts96d790e2013-09-19 17:32:00 +01005847 wl_resource_for_each(resource, resource_list) {
5848 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5849 mods_latched, mods_locked, group);
5850 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005851}
5852
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005853static void
5854debug_binding_cancel(struct weston_keyboard_grab *grab)
5855{
5856 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5857
5858 weston_keyboard_end_grab(grab->keyboard);
5859 free(db);
5860}
5861
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005862struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005863 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005864 debug_binding_modifiers,
5865 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005866};
5867
5868static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005869debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005870{
5871 struct debug_binding_grab *grab;
5872
5873 grab = calloc(1, sizeof *grab);
5874 if (!grab)
5875 return;
5876
5877 grab->seat = (struct weston_seat *) seat;
5878 grab->key[0] = key;
5879 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005880 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005881}
5882
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005883static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005884force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005885 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005886{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005887 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005888 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005889 struct desktop_shell *shell = data;
5890 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005891 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005892
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005893 focus_surface = seat->keyboard->focus;
5894 if (!focus_surface)
5895 return;
5896
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005897 wl_signal_emit(&compositor->kill_signal, focus_surface);
5898
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005899 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005900 wl_client_get_credentials(client, &pid, NULL, NULL);
5901
5902 /* Skip clients that we launched ourselves (the credentials of
5903 * the socketpair is ours) */
5904 if (pid == getpid())
5905 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005906
Daniel Stone325fc2d2012-05-30 16:31:58 +01005907 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005908}
5909
5910static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005911workspace_up_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 != 0)
5920 new_index--;
5921
5922 change_workspace(shell, new_index);
5923}
5924
5925static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005926workspace_down_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 = shell->workspaces.current;
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 if (new_index < shell->workspaces.num - 1)
5935 new_index++;
5936
5937 change_workspace(shell, new_index);
5938}
5939
5940static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005941workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005942 uint32_t key, void *data)
5943{
5944 struct desktop_shell *shell = data;
5945 unsigned int new_index;
5946
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005947 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005948 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005949 new_index = key - KEY_F1;
5950 if (new_index >= shell->workspaces.num)
5951 new_index = shell->workspaces.num - 1;
5952
5953 change_workspace(shell, new_index);
5954}
5955
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005956static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005957workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005958 uint32_t key, void *data)
5959{
5960 struct desktop_shell *shell = data;
5961 unsigned int new_index = shell->workspaces.current;
5962
5963 if (shell->locked)
5964 return;
5965
5966 if (new_index != 0)
5967 new_index--;
5968
5969 take_surface_to_workspace_by_seat(shell, seat, new_index);
5970}
5971
5972static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005973workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005974 uint32_t key, void *data)
5975{
5976 struct desktop_shell *shell = data;
5977 unsigned int new_index = shell->workspaces.current;
5978
5979 if (shell->locked)
5980 return;
5981
5982 if (new_index < shell->workspaces.num - 1)
5983 new_index++;
5984
5985 take_surface_to_workspace_by_seat(shell, seat, new_index);
5986}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005987
5988static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005989shell_reposition_view_on_output_destroy(struct weston_view *view)
5990{
5991 struct weston_output *output, *first_output;
5992 struct weston_compositor *ec = view->surface->compositor;
5993 struct shell_surface *shsurf;
5994 float x, y;
5995 int visible;
5996
5997 x = view->geometry.x;
5998 y = view->geometry.y;
5999
6000 /* At this point the destroyed output is not in the list anymore.
6001 * If the view is still visible somewhere, we leave where it is,
6002 * otherwise, move it to the first output. */
6003 visible = 0;
6004 wl_list_for_each(output, &ec->output_list, link) {
6005 if (pixman_region32_contains_point(&output->region,
6006 x, y, NULL)) {
6007 visible = 1;
6008 break;
6009 }
6010 }
6011
6012 if (!visible) {
6013 first_output = container_of(ec->output_list.next,
6014 struct weston_output, link);
6015
6016 x = first_output->x + first_output->width / 4;
6017 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006018
6019 weston_view_set_position(view, x, y);
6020 } else {
6021 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006022 }
6023
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006024
6025 shsurf = get_shell_surface(view->surface);
6026
6027 if (shsurf) {
6028 shsurf->saved_position_valid = false;
6029 shsurf->next_state.maximized = false;
6030 shsurf->next_state.fullscreen = false;
6031 shsurf->state_changed = true;
6032 }
6033}
6034
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006035void
6036shell_for_each_layer(struct desktop_shell *shell,
6037 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006038{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006039 struct workspace **ws;
6040
6041 func(shell, &shell->fullscreen_layer, data);
6042 func(shell, &shell->panel_layer, data);
6043 func(shell, &shell->background_layer, data);
6044 func(shell, &shell->lock_layer, data);
6045 func(shell, &shell->input_panel_layer, data);
6046
6047 wl_array_for_each(ws, &shell->workspaces.array)
6048 func(shell, &(*ws)->layer, data);
6049}
6050
6051static void
6052shell_output_destroy_move_layer(struct desktop_shell *shell,
6053 struct weston_layer *layer,
6054 void *data)
6055{
6056 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006057 struct weston_view *view;
6058
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006059 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006060 if (view->output != output)
6061 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006062
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006063 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006064 }
6065}
6066
6067static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006068handle_output_destroy(struct wl_listener *listener, void *data)
6069{
6070 struct shell_output *output_listener =
6071 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006072 struct weston_output *output = output_listener->output;
6073 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006074
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006075 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006076
Xiong Zhang6b481422013-10-23 13:58:32 +08006077 wl_list_remove(&output_listener->destroy_listener.link);
6078 wl_list_remove(&output_listener->link);
6079 free(output_listener);
6080}
6081
6082static void
6083create_shell_output(struct desktop_shell *shell,
6084 struct weston_output *output)
6085{
6086 struct shell_output *shell_output;
6087
6088 shell_output = zalloc(sizeof *shell_output);
6089 if (shell_output == NULL)
6090 return;
6091
6092 shell_output->output = output;
6093 shell_output->shell = shell;
6094 shell_output->destroy_listener.notify = handle_output_destroy;
6095 wl_signal_add(&output->destroy_signal,
6096 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006097 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006098}
6099
6100static void
6101handle_output_create(struct wl_listener *listener, void *data)
6102{
6103 struct desktop_shell *shell =
6104 container_of(listener, struct desktop_shell, output_create_listener);
6105 struct weston_output *output = (struct weston_output *)data;
6106
6107 create_shell_output(shell, output);
6108}
6109
6110static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006111handle_output_move_layer(struct desktop_shell *shell,
6112 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006113{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006114 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006115 struct weston_view *view;
6116 float x, y;
6117
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006118 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006119 if (view->output != output)
6120 continue;
6121
6122 x = view->geometry.x + output->move_x;
6123 y = view->geometry.y + output->move_y;
6124 weston_view_set_position(view, x, y);
6125 }
6126}
6127
6128static void
6129handle_output_move(struct wl_listener *listener, void *data)
6130{
6131 struct desktop_shell *shell;
6132
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006133 shell = container_of(listener, struct desktop_shell,
6134 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006135
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006136 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006137}
6138
6139static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006140setup_output_destroy_handler(struct weston_compositor *ec,
6141 struct desktop_shell *shell)
6142{
6143 struct weston_output *output;
6144
6145 wl_list_init(&shell->output_list);
6146 wl_list_for_each(output, &ec->output_list, link)
6147 create_shell_output(shell, output);
6148
6149 shell->output_create_listener.notify = handle_output_create;
6150 wl_signal_add(&ec->output_created_signal,
6151 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006152
6153 shell->output_move_listener.notify = handle_output_move;
6154 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006155}
6156
6157static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006158shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006159{
Tiago Vignattibe143262012-04-16 17:31:41 +03006160 struct desktop_shell *shell =
6161 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006162 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006163 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006164
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006165 /* Force state to unlocked so we don't try to fade */
6166 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006167
6168 if (shell->child.client) {
6169 /* disable respawn */
6170 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006171 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006172 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006173
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006174 wl_list_remove(&shell->idle_listener.link);
6175 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006176
6177 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006178
Xiong Zhang6b481422013-10-23 13:58:32 +08006179 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6180 wl_list_remove(&shell_output->destroy_listener.link);
6181 wl_list_remove(&shell_output->link);
6182 free(shell_output);
6183 }
6184
6185 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006186 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006187
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006188 wl_array_for_each(ws, &shell->workspaces.array)
6189 workspace_destroy(*ws);
6190 wl_array_release(&shell->workspaces.array);
6191
Pekka Paalanen3c647232011-12-22 13:43:43 +02006192 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006193 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006194 free(shell);
6195}
6196
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006197static void
6198shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6199{
6200 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006201 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006202
6203 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006204 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6205 MODIFIER_CTRL | MODIFIER_ALT,
6206 terminate_binding, ec);
6207 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6208 click_to_activate_binding,
6209 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006210 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6211 click_to_activate_binding,
6212 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006213 weston_compositor_add_touch_binding(ec, 0,
6214 touch_to_activate_binding,
6215 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006216 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6217 MODIFIER_SUPER | MODIFIER_ALT,
6218 surface_opacity_binding, NULL);
6219 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6220 MODIFIER_SUPER, zoom_axis_binding,
6221 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006222
6223 /* configurable bindings */
6224 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006225 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6226 zoom_key_binding, NULL);
6227 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6228 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006229 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6230 maximize_binding, NULL);
6231 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6232 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006233 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6234 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006235 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006236 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6237 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006238 weston_compositor_add_button_binding(ec, BTN_LEFT,
6239 mod | MODIFIER_SHIFT,
6240 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006241
6242 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6243 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6244 rotate_binding, NULL);
6245
Daniel Stone325fc2d2012-05-30 16:31:58 +01006246 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6247 shell);
6248 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6249 ec);
6250 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6251 backlight_binding, ec);
6252 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6253 ec);
6254 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6255 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006256 weston_compositor_add_key_binding(ec, KEY_K, mod,
6257 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006258 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6259 workspace_up_binding, shell);
6260 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6261 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006262 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6263 workspace_move_surface_up_binding,
6264 shell);
6265 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6266 workspace_move_surface_down_binding,
6267 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006268
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006269 if (shell->exposay_modifier)
6270 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6271 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006272
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006273 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6274 if (shell->workspaces.num > 1) {
6275 num_workspace_bindings = shell->workspaces.num;
6276 if (num_workspace_bindings > 6)
6277 num_workspace_bindings = 6;
6278 for (i = 0; i < num_workspace_bindings; i++)
6279 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6280 workspace_f_binding,
6281 shell);
6282 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006283
6284 /* Debug bindings */
6285 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6286 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006287}
6288
Jason Ekstrand024177c2014-04-21 19:42:58 -05006289static void
6290handle_seat_created(struct wl_listener *listener, void *data)
6291{
6292 struct weston_seat *seat = data;
6293
6294 create_shell_seat(seat);
6295}
6296
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006297WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006298module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006299 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006300{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006301 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006302 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006303 struct workspace **pws;
6304 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006305 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006306
Peter Huttererf3d62272013-08-08 11:57:05 +10006307 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006308 if (shell == NULL)
6309 return -1;
6310
Kristian Høgsberg75840622011-09-06 13:48:16 -04006311 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006312
6313 shell->destroy_listener.notify = shell_destroy;
6314 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006315 shell->idle_listener.notify = idle_handler;
6316 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6317 shell->wake_listener.notify = wake_handler;
6318 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006319
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006320 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006321 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006322 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006323 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006324 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006325 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006326 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006327 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006328 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006329 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006330 ec->shell_interface.set_window_geometry = set_window_geometry;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006331
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006332 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6333 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006334 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6335 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006336 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006337
6338 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006339 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006340
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006341 if (input_panel_setup(shell) < 0)
6342 return -1;
6343
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006344 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006345
Daniel Stonedf8133b2013-11-19 11:37:14 +01006346 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6347 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6348
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006349 for (i = 0; i < shell->workspaces.num; i++) {
6350 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6351 if (pws == NULL)
6352 return -1;
6353
6354 *pws = workspace_create();
6355 if (*pws == NULL)
6356 return -1;
6357 }
6358 activate_workspace(shell, 0);
6359
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006360 weston_layer_init(&shell->minimized_layer, NULL);
6361
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006362 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006363 wl_list_init(&shell->workspaces.animation.link);
6364 shell->workspaces.animation.frame = animate_workspace_change_frame;
6365
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006366 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006367 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006368 return -1;
6369
Rafael Antognollie2a34552013-12-03 15:35:45 -02006370 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6371 shell, bind_xdg_shell) == NULL)
6372 return -1;
6373
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006374 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006375 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006376 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006377 return -1;
6378
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006379 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6380 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006381 return -1;
6382
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006383 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6384 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006385 return -1;
6386
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006387 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006388
Jonny Lamb765760d2014-08-20 15:53:19 +02006389 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6390
Xiong Zhang6b481422013-10-23 13:58:32 +08006391 setup_output_destroy_handler(ec, shell);
6392
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006393 loop = wl_display_get_event_loop(ec->wl_display);
6394 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006395
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006396 shell->screensaver.timer =
6397 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6398
Jason Ekstrand024177c2014-04-21 19:42:58 -05006399 wl_list_for_each(seat, &ec->seat_list, link)
6400 handle_seat_created(NULL, seat);
6401 shell->seat_create_listener.notify = handle_seat_created;
6402 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006403
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006404 screenshooter_create(ec);
6405
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006406 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006407
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006408 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006409
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006410 return 0;
6411}