blob: 99f3343f0f90696c8384d1bccc0392817ae92d73 [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;
1890
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001891 if (shsurf->grabbed ||
1892 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001893 return 0;
1894
1895 if (edges == 0 || edges > 15 ||
1896 (edges & 3) == 3 || (edges & 12) == 12)
1897 return 0;
1898
1899 resize = malloc(sizeof *resize);
1900 if (!resize)
1901 return -1;
1902
1903 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001904
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001905 resize->width = shsurf->geometry.width;
1906 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001907
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001908 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001909 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001910 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001911 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001912
1913 return 0;
1914}
1915
1916static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001917common_surface_resize(struct wl_resource *resource,
1918 struct wl_resource *seat_resource, uint32_t serial,
1919 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001920{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001921 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001922 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001923 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001924
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001925 if (seat->pointer == NULL ||
1926 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001927 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001928 seat->pointer->focus == NULL)
1929 return;
1930
1931 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1932 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001933 return;
1934
Kristian Høgsberge3148752013-05-06 23:19:49 -04001935 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001936 wl_resource_post_no_memory(resource);
1937}
1938
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001939static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001940shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1941 struct wl_resource *seat_resource, uint32_t serial,
1942 uint32_t edges)
1943{
1944 common_surface_resize(resource, seat_resource, serial, edges);
1945}
1946
1947static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001948busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001949{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001950 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001951 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001952 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001953 wl_fixed_t sx, sy;
1954
Jason Ekstranda7af7042013-10-12 22:38:11 -05001955 view = weston_compositor_pick_view(pointer->seat->compositor,
1956 pointer->x, pointer->y,
1957 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001958
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001959 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1960 shell_grab_end(grab);
1961 free(grab);
1962 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001963}
1964
1965static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001966busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1967 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001968{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001969 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001970}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001971
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001972static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001973busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001974 uint32_t time, uint32_t button, uint32_t state)
1975{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001976 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001977 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001978 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001979
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001980 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001981 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001982 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001983 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001984 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001985 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001986 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001987}
1988
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001989static void
1990busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1991{
1992 struct shell_grab *grab = (struct shell_grab *) base;
1993
1994 shell_grab_end(grab);
1995 free(grab);
1996}
1997
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001998static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001999 busy_cursor_grab_focus,
2000 busy_cursor_grab_motion,
2001 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002002 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002003};
2004
2005static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002006set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002007{
2008 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002009
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002010 if (pointer->grab->interface == &busy_cursor_grab_interface)
2011 return;
2012
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002013 grab = malloc(sizeof *grab);
2014 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002015 return;
2016
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002017 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2018 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002019 /* Mark the shsurf as ungrabbed so that button binding is able
2020 * to move it. */
2021 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002022}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002023
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002024static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002025end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002026{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002027 struct shell_grab *grab;
2028 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002029
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002030 wl_list_for_each(seat, &compositor->seat_list, link) {
2031 if (seat->pointer == NULL)
2032 continue;
2033
2034 grab = (struct shell_grab *) seat->pointer->grab;
2035 if (grab->grab.interface == &busy_cursor_grab_interface &&
2036 wl_resource_get_client(grab->shsurf->resource) == client) {
2037 shell_grab_end(grab);
2038 free(grab);
2039 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002040 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002041}
2042
Scott Moreau9521d5e2012-04-19 13:06:17 -06002043static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002044handle_shell_client_destroy(struct wl_listener *listener, void *data);
2045
2046static int
2047xdg_ping_timeout_handler(void *data)
2048{
2049 struct shell_client *sc = data;
2050 struct weston_seat *seat;
2051 struct shell_surface *shsurf;
2052
2053 /* Client is not responding */
2054 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002055 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2056 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2057 continue;
2058 if (seat->pointer->focus->surface->resource == NULL)
2059 continue;
2060
2061 shsurf = get_shell_surface(seat->pointer->focus->surface);
2062 if (shsurf &&
2063 wl_resource_get_client(shsurf->resource) == sc->client)
2064 set_busy_cursor(shsurf, seat->pointer);
2065 }
2066
2067 return 1;
2068}
2069
2070static void
2071handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2072{
2073 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002074 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002075 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002076 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002077
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002078 if (sc->unresponsive) {
2079 xdg_ping_timeout_handler(sc);
2080 return;
2081 }
2082
2083 sc->ping_serial = serial;
2084 loop = wl_display_get_event_loop(compositor->wl_display);
2085 if (sc->ping_timer == NULL)
2086 sc->ping_timer =
2087 wl_event_loop_add_timer(loop,
2088 xdg_ping_timeout_handler, sc);
2089 if (sc->ping_timer == NULL)
2090 return;
2091
2092 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2093
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002094 if (shell_surface_is_xdg_surface(shsurf) ||
2095 shell_surface_is_xdg_popup(shsurf))
2096 xdg_shell_send_ping(sc->resource, serial);
2097 else if (shell_surface_is_wl_shell_surface(shsurf))
2098 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002099}
2100
Scott Moreauff1db4a2012-04-17 19:06:18 -06002101static void
2102ping_handler(struct weston_surface *surface, uint32_t serial)
2103{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002104 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002105
2106 if (!shsurf)
2107 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002108 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002109 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002110 if (shsurf->surface == shsurf->shell->grab_surface)
2111 return;
2112
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002113 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002114}
2115
2116static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002117handle_pointer_focus(struct wl_listener *listener, void *data)
2118{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002119 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002121 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002122 uint32_t serial;
2123
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002125 return;
2126
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002128 serial = wl_display_next_serial(compositor->wl_display);
2129 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002130}
2131
2132static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002133shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2134{
2135 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002136 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002137}
2138
2139static void
2140shell_surface_gain_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
2147handle_keyboard_focus(struct wl_listener *listener, void *data)
2148{
2149 struct weston_keyboard *keyboard = data;
2150 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2151
2152 if (seat->focused_surface) {
2153 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2154 if (shsurf)
2155 shell_surface_lose_keyboard_focus(shsurf);
2156 }
2157
2158 seat->focused_surface = keyboard->focus;
2159
2160 if (seat->focused_surface) {
2161 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2162 if (shsurf)
2163 shell_surface_gain_keyboard_focus(shsurf);
2164 }
2165}
2166
2167static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002168shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002169{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002170 if (sc->ping_serial != serial)
2171 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002172
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002173 sc->unresponsive = 0;
2174 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002175
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002176 if (sc->ping_timer) {
2177 wl_event_source_remove(sc->ping_timer);
2178 sc->ping_timer = NULL;
2179 }
2180
2181}
2182
2183static void
2184shell_surface_pong(struct wl_client *client,
2185 struct wl_resource *resource, uint32_t serial)
2186{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002187 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2188 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002189
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002190 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002191}
2192
2193static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002194set_title(struct shell_surface *shsurf, const char *title)
2195{
2196 free(shsurf->title);
2197 shsurf->title = strdup(title);
2198}
2199
2200static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002201set_window_geometry(struct shell_surface *shsurf,
2202 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002203{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002204 shsurf->next_geometry.x = x;
2205 shsurf->next_geometry.y = y;
2206 shsurf->next_geometry.width = width;
2207 shsurf->next_geometry.height = height;
2208 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002209}
2210
2211static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002212shell_surface_set_title(struct wl_client *client,
2213 struct wl_resource *resource, const char *title)
2214{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002215 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002216
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002217 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002218}
2219
2220static void
2221shell_surface_set_class(struct wl_client *client,
2222 struct wl_resource *resource, const char *class)
2223{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002224 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002225
2226 free(shsurf->class);
2227 shsurf->class = strdup(class);
2228}
2229
Alex Wu4539b082012-03-01 12:57:46 +08002230static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002231restore_output_mode(struct weston_output *output)
2232{
Hardening57388e42013-09-18 23:56:36 +02002233 if (output->current_mode != output->original_mode ||
2234 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002235 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002236 output->original_mode,
2237 output->original_scale,
2238 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002239}
2240
2241static void
2242restore_all_output_modes(struct weston_compositor *compositor)
2243{
2244 struct weston_output *output;
2245
2246 wl_list_for_each(output, &compositor->output_list, link)
2247 restore_output_mode(output);
2248}
2249
Philip Withnall07926d92013-11-25 18:01:40 +00002250/* The surface will be inserted into the list immediately after the link
2251 * returned by this function (i.e. will be stacked immediately above the
2252 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002253static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002254shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2255{
2256 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002257 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002258
2259 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002260 case SHELL_SURFACE_XWAYLAND:
2261 return &shsurf->shell->fullscreen_layer.view_list;
2262
2263 case SHELL_SURFACE_NONE:
2264 return NULL;
2265
Kristian Høgsbergead52422014-01-01 13:51:52 -08002266 case SHELL_SURFACE_POPUP:
2267 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002268 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002269 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002270 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002271 /* Move the surface to its parent layer so
2272 * that surfaces which are transient for
2273 * fullscreen surfaces don't get hidden by the
2274 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002275
2276 /* TODO: Handle a parent with multiple views */
2277 parent = get_default_view(shsurf->parent);
2278 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002279 return container_of(parent->layer_link.link.prev,
2280 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002281 }
Philip Withnall07926d92013-11-25 18:01:40 +00002282
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002283 /* Move the surface to a normal workspace layer so that surfaces
2284 * which were previously fullscreen or transient are no longer
2285 * rendered on top. */
2286 ws = get_current_workspace(shsurf->shell);
2287 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002288 }
2289
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002290 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002291}
2292
Philip Withnall648a4dd2013-11-25 18:01:44 +00002293static void
2294shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2295{
2296 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002297 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002298
2299 /* Move the child layers to the same workspace as shsurf. They will be
2300 * stacked above shsurf. */
2301 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002302 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002303 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002304 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002305 prev = container_of(shsurf->view->layer_link.link.prev,
2306 struct weston_layer_entry, link);
2307 weston_layer_entry_remove(&child->view->layer_link);
2308 weston_layer_entry_insert(prev,
2309 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002310 weston_view_geometry_dirty(child->view);
2311 weston_surface_damage(child->surface);
2312
2313 /* Recurse. We don’t expect this to recurse very far (if
2314 * at all) because that would imply we have transient
2315 * (or popup) children of transient surfaces, which
2316 * would be unusual. */
2317 shell_surface_update_child_surface_layers(child);
2318 }
2319 }
2320}
2321
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002322/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002323 * geometry to ensure the changes are redrawn.
2324 *
2325 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2326 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002327static void
2328shell_surface_update_layer(struct shell_surface *shsurf)
2329{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002330 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002331
2332 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2333
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002334 if (new_layer_link == NULL)
2335 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002336 if (new_layer_link == &shsurf->view->layer_link)
2337 return;
2338
2339 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002340 weston_layer_entry_remove(&shsurf->view->layer_link);
2341 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002342 weston_view_geometry_dirty(shsurf->view);
2343 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002344
2345 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002346}
2347
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002348static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002349shell_surface_set_parent(struct shell_surface *shsurf,
2350 struct weston_surface *parent)
2351{
2352 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002353
2354 wl_list_remove(&shsurf->children_link);
2355 wl_list_init(&shsurf->children_link);
2356
2357 /* Insert into the parent surface’s child list. */
2358 if (parent != NULL) {
2359 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2360 if (parent_shsurf != NULL)
2361 wl_list_insert(&parent_shsurf->children_list,
2362 &shsurf->children_link);
2363 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002364}
2365
2366static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002367shell_surface_set_output(struct shell_surface *shsurf,
2368 struct weston_output *output)
2369{
2370 struct weston_surface *es = shsurf->surface;
2371
2372 /* get the default output, if the client set it as NULL
2373 check whether the ouput is available */
2374 if (output)
2375 shsurf->output = output;
2376 else if (es->output)
2377 shsurf->output = es->output;
2378 else
2379 shsurf->output = get_default_output(es->compositor);
2380}
2381
2382static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002383surface_clear_next_states(struct shell_surface *shsurf)
2384{
2385 shsurf->next_state.maximized = false;
2386 shsurf->next_state.fullscreen = false;
2387
2388 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2389 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2390 shsurf->state_changed = true;
2391}
2392
2393static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002394set_toplevel(struct shell_surface *shsurf)
2395{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002396 shell_surface_set_parent(shsurf, NULL);
2397 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002398 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002399
2400 /* The layer_link is updated in set_surface_type(),
2401 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002402}
2403
2404static void
2405shell_surface_set_toplevel(struct wl_client *client,
2406 struct wl_resource *resource)
2407{
2408 struct shell_surface *surface = wl_resource_get_user_data(resource);
2409
2410 set_toplevel(surface);
2411}
2412
2413static void
2414set_transient(struct shell_surface *shsurf,
2415 struct weston_surface *parent, int x, int y, uint32_t flags)
2416{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002417 assert(parent != NULL);
2418
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002419 shell_surface_set_parent(shsurf, parent);
2420
2421 surface_clear_next_states(shsurf);
2422
Philip Withnallbecb77e2013-11-25 18:01:30 +00002423 shsurf->transient.x = x;
2424 shsurf->transient.y = y;
2425 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002426
Rafael Antognollied207b42013-12-03 15:35:43 -02002427 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002428 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002429 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002430
2431 /* The layer_link is updated in set_surface_type(),
2432 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002433}
2434
2435static void
2436shell_surface_set_transient(struct wl_client *client,
2437 struct wl_resource *resource,
2438 struct wl_resource *parent_resource,
2439 int x, int y, uint32_t flags)
2440{
2441 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2442 struct weston_surface *parent =
2443 wl_resource_get_user_data(parent_resource);
2444
2445 set_transient(shsurf, parent, x, y, flags);
2446}
2447
2448static void
2449set_fullscreen(struct shell_surface *shsurf,
2450 uint32_t method,
2451 uint32_t framerate,
2452 struct weston_output *output)
2453{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002454 shell_surface_set_output(shsurf, output);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002455 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002456
2457 shsurf->fullscreen_output = shsurf->output;
2458 shsurf->fullscreen.type = method;
2459 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002460
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002461 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002462}
2463
2464static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002465weston_view_set_initial_position(struct weston_view *view,
2466 struct desktop_shell *shell);
2467
2468static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002469unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002470{
Philip Withnallf85fe842013-11-25 18:01:37 +00002471 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002472 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2473 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002474 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002475 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002476
Alex Wu4539b082012-03-01 12:57:46 +08002477 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2478 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002479
Alex Wu4539b082012-03-01 12:57:46 +08002480 wl_list_remove(&shsurf->fullscreen.transform.link);
2481 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002482
Jason Ekstranda7af7042013-10-12 22:38:11 -05002483 if (shsurf->fullscreen.black_view)
2484 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2485 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002486
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002487 if (shsurf->saved_position_valid)
2488 weston_view_set_position(shsurf->view,
2489 shsurf->saved_x, shsurf->saved_y);
2490 else
2491 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2492
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002493 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002494 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002495 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002496 shsurf->saved_rotation_valid = false;
2497 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002498
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002499 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002500}
2501
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002502static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002503shell_surface_set_fullscreen(struct wl_client *client,
2504 struct wl_resource *resource,
2505 uint32_t method,
2506 uint32_t framerate,
2507 struct wl_resource *output_resource)
2508{
2509 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2510 struct weston_output *output;
2511
2512 if (output_resource)
2513 output = wl_resource_get_user_data(output_resource);
2514 else
2515 output = NULL;
2516
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002517 shell_surface_set_parent(shsurf, NULL);
2518
Rafael Antognolli03b16592013-12-03 15:35:42 -02002519 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002520 shsurf->next_state.fullscreen = true;
2521 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002522 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002523}
2524
2525static void
2526set_popup(struct shell_surface *shsurf,
2527 struct weston_surface *parent,
2528 struct weston_seat *seat,
2529 uint32_t serial,
2530 int32_t x,
2531 int32_t y)
2532{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002533 assert(parent != NULL);
2534
Philip Withnallbecb77e2013-11-25 18:01:30 +00002535 shsurf->popup.shseat = get_shell_seat(seat);
2536 shsurf->popup.serial = serial;
2537 shsurf->popup.x = x;
2538 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002539
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002540 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002541}
2542
2543static void
2544shell_surface_set_popup(struct wl_client *client,
2545 struct wl_resource *resource,
2546 struct wl_resource *seat_resource,
2547 uint32_t serial,
2548 struct wl_resource *parent_resource,
2549 int32_t x, int32_t y, uint32_t flags)
2550{
2551 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002552 struct weston_surface *parent =
2553 wl_resource_get_user_data(parent_resource);
2554
2555 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002556
Rafael Antognolli03b16592013-12-03 15:35:42 -02002557 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002558 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002559 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002560 wl_resource_get_user_data(seat_resource),
2561 serial, x, y);
2562}
2563
2564static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002565unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002566{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002567 /* undo all maximized things here */
2568 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002569
2570 if (shsurf->saved_position_valid)
2571 weston_view_set_position(shsurf->view,
2572 shsurf->saved_x, shsurf->saved_y);
2573 else
2574 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002575
2576 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002577 wl_list_insert(&shsurf->view->geometry.transformation_list,
2578 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002579 shsurf->saved_rotation_valid = false;
2580 }
2581
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002582 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002583}
2584
Philip Withnallbecb77e2013-11-25 18:01:30 +00002585static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002586set_minimized(struct weston_surface *surface, uint32_t is_true)
2587{
2588 struct shell_surface *shsurf;
2589 struct workspace *current_ws;
2590 struct weston_seat *seat;
2591 struct weston_surface *focus;
2592 struct weston_view *view;
2593
2594 view = get_default_view(surface);
2595 if (!view)
2596 return;
2597
2598 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2599
2600 shsurf = get_shell_surface(surface);
2601 current_ws = get_current_workspace(shsurf->shell);
2602
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002603 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002604 /* hide or show, depending on the state */
2605 if (is_true) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002606 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002607
2608 drop_focus_state(shsurf->shell, current_ws, view->surface);
2609 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2610 if (!seat->keyboard)
2611 continue;
2612 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2613 if (focus == view->surface)
2614 weston_keyboard_set_focus(seat->keyboard, NULL);
2615 }
2616 }
2617 else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002618 weston_layer_entry_insert(&current_ws->layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002619
2620 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2621 if (!seat->keyboard)
2622 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002623 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002624 }
2625 }
2626
2627 shell_surface_update_child_surface_layers(shsurf);
2628
2629 weston_view_damage_below(view);
2630}
2631
2632static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002633shell_surface_set_maximized(struct wl_client *client,
2634 struct wl_resource *resource,
2635 struct wl_resource *output_resource)
2636{
2637 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2638 struct weston_output *output;
2639
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002640 surface_clear_next_states(shsurf);
2641 shsurf->next_state.maximized = true;
2642 shsurf->state_changed = true;
2643
2644 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2645 shell_surface_set_parent(shsurf, NULL);
2646
Philip Withnallbecb77e2013-11-25 18:01:30 +00002647 if (output_resource)
2648 output = wl_resource_get_user_data(output_resource);
2649 else
2650 output = NULL;
2651
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002652 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002653
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002654 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002655}
2656
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002657/* This is only ever called from set_surface_type(), so there’s no need to
2658 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002659static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002660reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002661{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002662 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002663 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002664 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002665 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002666
Pekka Paalanen98262232011-12-01 10:42:22 +02002667 return 0;
2668}
2669
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002670static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002671set_full_output(struct shell_surface *shsurf)
2672{
2673 shsurf->saved_x = shsurf->view->geometry.x;
2674 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002675 shsurf->saved_width = shsurf->surface->width;
2676 shsurf->saved_height = shsurf->surface->height;
2677 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002678 shsurf->saved_position_valid = true;
2679
2680 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2681 wl_list_remove(&shsurf->rotation.transform.link);
2682 wl_list_init(&shsurf->rotation.transform.link);
2683 weston_view_geometry_dirty(shsurf->view);
2684 shsurf->saved_rotation_valid = true;
2685 }
2686}
2687
2688static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002689set_surface_type(struct shell_surface *shsurf)
2690{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002691 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002692 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002693
Philip Withnallbecb77e2013-11-25 18:01:30 +00002694 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002695
Rafael Antognolli03b16592013-12-03 15:35:42 -02002696 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002697 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002698
2699 switch (shsurf->type) {
2700 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002701 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002702 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002703 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002704 weston_view_set_position(shsurf->view,
2705 pev->geometry.x + shsurf->transient.x,
2706 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002707 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002708 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002709
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002710 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002711 weston_view_set_position(shsurf->view, shsurf->transient.x,
2712 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002713 break;
2714
Philip Withnall0f640e22013-11-25 18:01:31 +00002715 case SHELL_SURFACE_POPUP:
2716 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002717 default:
2718 break;
2719 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002720
2721 /* Update the surface’s layer. */
2722 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002723}
2724
Tiago Vignattibe143262012-04-16 17:31:41 +03002725static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002726shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002727{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002728 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002729}
2730
Alex Wu21858432012-04-01 20:13:08 +08002731static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002732black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002733
Jason Ekstranda7af7042013-10-12 22:38:11 -05002734static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002735create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002736 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002737 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002738{
2739 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002741
2742 surface = weston_surface_create(ec);
2743 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002744 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002745 return NULL;
2746 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002747 view = weston_view_create(surface);
2748 if (surface == NULL) {
2749 weston_log("no memory\n");
2750 weston_surface_destroy(surface);
2751 return NULL;
2752 }
Alex Wu4539b082012-03-01 12:57:46 +08002753
Alex Wu21858432012-04-01 20:13:08 +08002754 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002755 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002756 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002757 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002758 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002759 pixman_region32_fini(&surface->input);
2760 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002761
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002762 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002763 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002764
2765 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002766}
2767
Philip Withnalled8f1a92013-11-25 18:01:43 +00002768static void
2769shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2770{
2771 struct weston_output *output = shsurf->fullscreen_output;
2772
Rafael Antognolli03b16592013-12-03 15:35:42 -02002773 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002774
2775 if (!shsurf->fullscreen.black_view)
2776 shsurf->fullscreen.black_view =
2777 create_black_surface(shsurf->surface->compositor,
2778 shsurf->surface,
2779 output->x, output->y,
2780 output->width,
2781 output->height);
2782
2783 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002784 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2785 weston_layer_entry_insert(&shsurf->view->layer_link,
2786 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002787 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2788 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002789
2790 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002791}
2792
Alex Wu4539b082012-03-01 12:57:46 +08002793/* Create black surface and append it to the associated fullscreen surface.
2794 * Handle size dismatch and positioning according to the method. */
2795static void
2796shell_configure_fullscreen(struct shell_surface *shsurf)
2797{
2798 struct weston_output *output = shsurf->fullscreen_output;
2799 struct weston_surface *surface = shsurf->surface;
2800 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002801 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002802 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002803
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002804 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2805 restore_output_mode(output);
2806
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002807 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002808 weston_layer_entry_remove(&shsurf->view->layer_link);
2809 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002810
Philip Withnalled8f1a92013-11-25 18:01:43 +00002811 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002812
Jason Ekstranda7af7042013-10-12 22:38:11 -05002813 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002814 &surf_width, &surf_height);
2815
Alex Wu4539b082012-03-01 12:57:46 +08002816 switch (shsurf->fullscreen.type) {
2817 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002818 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002819 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002820 break;
2821 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002822 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002823 if (output->width == surf_width &&
2824 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 weston_view_set_position(shsurf->view,
2826 output->x - surf_x,
2827 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002828 break;
2829 }
2830
Alex Wu4539b082012-03-01 12:57:46 +08002831 matrix = &shsurf->fullscreen.transform.matrix;
2832 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002833
Scott Moreau1bad5db2012-08-18 01:04:05 -06002834 output_aspect = (float) output->width /
2835 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002836 /* XXX: Use surf_width and surf_height here? */
2837 surface_aspect = (float) surface->width /
2838 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002839 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002840 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002841 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002842 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002843 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002844 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002845
Alex Wu4539b082012-03-01 12:57:46 +08002846 weston_matrix_scale(matrix, scale, scale, 1);
2847 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002848 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002849 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002850 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2851 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002852 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002853
Alex Wu4539b082012-03-01 12:57:46 +08002854 break;
2855 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002856 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002857 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002858 surf_width * surface->buffer_viewport.buffer.scale,
2859 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002860 shsurf->fullscreen.framerate};
2861
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002862 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002863 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002864 weston_view_set_position(shsurf->view,
2865 output->x - surf_x,
2866 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002867 shsurf->fullscreen.black_view->surface->width = output->width;
2868 shsurf->fullscreen.black_view->surface->height = output->height;
2869 weston_view_set_position(shsurf->fullscreen.black_view,
2870 output->x - surf_x,
2871 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002872 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002873 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002874 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002875 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002876 }
Alex Wubd3354b2012-04-17 17:20:49 +08002877 }
Alex Wu4539b082012-03-01 12:57:46 +08002878 break;
2879 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002880 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002881 break;
2882 default:
2883 break;
2884 }
2885}
2886
Alex Wu4539b082012-03-01 12:57:46 +08002887static void
2888shell_map_fullscreen(struct shell_surface *shsurf)
2889{
Alex Wubd3354b2012-04-17 17:20:49 +08002890 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002891}
2892
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002893static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002894set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2895{
2896 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002897 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002898 shsurf->transient.x = x;
2899 shsurf->transient.y = y;
2900 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002901
2902 shell_surface_set_parent(shsurf, NULL);
2903
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002904 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002905 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002906}
2907
Kristian Høgsberg47792412014-05-04 13:47:06 -07002908static void
2909shell_interface_set_fullscreen(struct shell_surface *shsurf,
2910 uint32_t method,
2911 uint32_t framerate,
2912 struct weston_output *output)
2913{
2914 surface_clear_next_states(shsurf);
2915 set_fullscreen(shsurf, method, framerate, output);
2916
2917 shsurf->next_state.fullscreen = true;
2918 shsurf->state_changed = true;
2919}
2920
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002921static int
2922shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2923{
2924 return surface_move(shsurf, ws, 1);
2925}
2926
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002927static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002928
2929static void
2930destroy_shell_seat(struct wl_listener *listener, void *data)
2931{
2932 struct shell_seat *shseat =
2933 container_of(listener,
2934 struct shell_seat, seat_destroy_listener);
2935 struct shell_surface *shsurf, *prev = NULL;
2936
2937 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002938 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002939 shseat->popup_grab.client = NULL;
2940
2941 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2942 shsurf->popup.shseat = NULL;
2943 if (prev) {
2944 wl_list_init(&prev->popup.grab_link);
2945 }
2946 prev = shsurf;
2947 }
2948 wl_list_init(&prev->popup.grab_link);
2949 }
2950
2951 wl_list_remove(&shseat->seat_destroy_listener.link);
2952 free(shseat);
2953}
2954
Jason Ekstrand024177c2014-04-21 19:42:58 -05002955static void
2956shell_seat_caps_changed(struct wl_listener *listener, void *data)
2957{
2958 struct shell_seat *seat;
2959
2960 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2961
2962 if (seat->seat->keyboard &&
2963 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2964 wl_signal_add(&seat->seat->keyboard->focus_signal,
2965 &seat->keyboard_focus_listener);
2966 } else if (!seat->seat->keyboard) {
2967 wl_list_init(&seat->keyboard_focus_listener.link);
2968 }
2969
2970 if (seat->seat->pointer &&
2971 wl_list_empty(&seat->pointer_focus_listener.link)) {
2972 wl_signal_add(&seat->seat->pointer->focus_signal,
2973 &seat->pointer_focus_listener);
2974 } else if (!seat->seat->pointer) {
2975 wl_list_init(&seat->pointer_focus_listener.link);
2976 }
2977}
2978
Giulio Camuffo5085a752013-03-25 21:42:45 +01002979static struct shell_seat *
2980create_shell_seat(struct weston_seat *seat)
2981{
2982 struct shell_seat *shseat;
2983
2984 shseat = calloc(1, sizeof *shseat);
2985 if (!shseat) {
2986 weston_log("no memory to allocate shell seat\n");
2987 return NULL;
2988 }
2989
2990 shseat->seat = seat;
2991 wl_list_init(&shseat->popup_grab.surfaces_list);
2992
2993 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2994 wl_signal_add(&seat->destroy_signal,
2995 &shseat->seat_destroy_listener);
2996
Jason Ekstrand024177c2014-04-21 19:42:58 -05002997 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2998 wl_list_init(&shseat->keyboard_focus_listener.link);
2999
3000 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3001 wl_list_init(&shseat->pointer_focus_listener.link);
3002
3003 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3004 wl_signal_add(&seat->updated_caps_signal,
3005 &shseat->caps_changed_listener);
3006 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3007
Giulio Camuffo5085a752013-03-25 21:42:45 +01003008 return shseat;
3009}
3010
3011static struct shell_seat *
3012get_shell_seat(struct weston_seat *seat)
3013{
3014 struct wl_listener *listener;
3015
3016 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003017 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003018
3019 return container_of(listener,
3020 struct shell_seat, seat_destroy_listener);
3021}
3022
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003023static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003024popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003025{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003026 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003027 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003028 struct shell_seat *shseat =
3029 container_of(grab, struct shell_seat, popup_grab.grab);
3030 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003031 wl_fixed_t sx, sy;
3032
Jason Ekstranda7af7042013-10-12 22:38:11 -05003033 view = weston_compositor_pick_view(pointer->seat->compositor,
3034 pointer->x, pointer->y,
3035 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003036
Jason Ekstranda7af7042013-10-12 22:38:11 -05003037 if (view && view->surface->resource &&
3038 wl_resource_get_client(view->surface->resource) == client) {
3039 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003040 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003041 weston_pointer_set_focus(pointer, NULL,
3042 wl_fixed_from_int(0),
3043 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003044 }
3045}
3046
3047static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003048popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3049 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003050{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003051 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003052 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003053 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003054
Jonas Ådahl61917c82014-08-11 22:44:02 +02003055 if (pointer->focus) {
3056 weston_view_from_global_fixed(pointer->focus, x, y,
3057 &pointer->sx, &pointer->sy);
3058 }
3059
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003060 weston_pointer_move(pointer, x, y);
3061
Neil Roberts96d790e2013-09-19 17:32:00 +01003062 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003063 weston_view_from_global_fixed(pointer->focus,
3064 pointer->x, pointer->y,
3065 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003066 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003067 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003068}
3069
3070static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003071popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003072 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003073{
3074 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003075 struct shell_seat *shseat =
3076 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003077 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003078 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003079 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003080 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003081
Neil Roberts96d790e2013-09-19 17:32:00 +01003082 resource_list = &grab->pointer->focus_resource_list;
3083 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003084 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003085 wl_resource_for_each(resource, resource_list) {
3086 wl_pointer_send_button(resource, serial,
3087 time, button, state);
3088 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003089 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003090 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003091 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003092 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003093 }
3094
Daniel Stone4dbadb12012-05-30 16:31:51 +01003095 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003096 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003097}
3098
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003099static void
3100popup_grab_cancel(struct weston_pointer_grab *grab)
3101{
3102 popup_grab_end(grab->pointer);
3103}
3104
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003105static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003106 popup_grab_focus,
3107 popup_grab_motion,
3108 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003109 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003110};
3111
3112static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003113shell_surface_send_popup_done(struct shell_surface *shsurf)
3114{
3115 if (shell_surface_is_wl_shell_surface(shsurf))
3116 wl_shell_surface_send_popup_done(shsurf->resource);
3117 else if (shell_surface_is_xdg_popup(shsurf))
3118 xdg_popup_send_popup_done(shsurf->resource,
3119 shsurf->popup.serial);
3120}
3121
3122static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003123popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003124{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003125 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003126 struct shell_seat *shseat =
3127 container_of(grab, struct shell_seat, popup_grab.grab);
3128 struct shell_surface *shsurf;
3129 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003130
3131 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003132 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003133 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003134 shseat->popup_grab.grab.interface = NULL;
3135 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003136 /* Send the popup_done event to all the popups open */
3137 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003138 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003139 shsurf->popup.shseat = NULL;
3140 if (prev) {
3141 wl_list_init(&prev->popup.grab_link);
3142 }
3143 prev = shsurf;
3144 }
3145 wl_list_init(&prev->popup.grab_link);
3146 wl_list_init(&shseat->popup_grab.surfaces_list);
3147 }
3148}
3149
3150static void
3151add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3152{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003153 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003154
3155 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003156 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003157 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003158 /* We must make sure here that this popup was opened after
3159 * a mouse press, and not just by moving around with other
3160 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003161 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003162 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003163
Rob Bradforddfe31052013-06-26 19:49:11 +01003164 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003165 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003166 } else {
3167 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003168 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003169}
3170
3171static void
3172remove_popup_grab(struct shell_surface *shsurf)
3173{
3174 struct shell_seat *shseat = shsurf->popup.shseat;
3175
3176 wl_list_remove(&shsurf->popup.grab_link);
3177 wl_list_init(&shsurf->popup.grab_link);
3178 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003179 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003180 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003181 }
3182}
3183
3184static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003185shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003186{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003187 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003188 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003189
Jason Ekstranda7af7042013-10-12 22:38:11 -05003190 shsurf->surface->output = parent_view->output;
3191 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003192
Jason Ekstranda7af7042013-10-12 22:38:11 -05003193 weston_view_set_transform_parent(shsurf->view, parent_view);
3194 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3195 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003196
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003197 if (shseat->seat->pointer &&
3198 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003199 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003200 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003201 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003202 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003203 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003204}
3205
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003206static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003207 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003208 shell_surface_move,
3209 shell_surface_resize,
3210 shell_surface_set_toplevel,
3211 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003212 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003213 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003214 shell_surface_set_maximized,
3215 shell_surface_set_title,
3216 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003217};
3218
3219static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003220destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003221{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003222 struct shell_surface *child, *next;
3223
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003224 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3225
Giulio Camuffo5085a752013-03-25 21:42:45 +01003226 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3227 remove_popup_grab(shsurf);
3228 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003229
Alex Wubd3354b2012-04-17 17:20:49 +08003230 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003231 shell_surface_is_top_fullscreen(shsurf))
3232 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003233
Jason Ekstranda7af7042013-10-12 22:38:11 -05003234 if (shsurf->fullscreen.black_view)
3235 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003236
Alex Wubd3354b2012-04-17 17:20:49 +08003237 /* As destroy_resource() use wl_list_for_each_safe(),
3238 * we can always remove the listener.
3239 */
3240 wl_list_remove(&shsurf->surface_destroy_listener.link);
3241 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003242 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003243
Jason Ekstranda7af7042013-10-12 22:38:11 -05003244 weston_view_destroy(shsurf->view);
3245
Philip Withnall648a4dd2013-11-25 18:01:44 +00003246 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003247 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3248 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003249
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003250 wl_list_remove(&shsurf->link);
3251 free(shsurf);
3252}
3253
3254static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003255shell_destroy_shell_surface(struct wl_resource *resource)
3256{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003257 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003258
Bryan Caina46b9462014-04-04 17:41:24 -05003259 if (!wl_list_empty(&shsurf->popup.grab_link))
3260 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003261 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003262}
3263
3264static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003265shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003266{
3267 struct shell_surface *shsurf = container_of(listener,
3268 struct shell_surface,
3269 surface_destroy_listener);
3270
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003271 if (shsurf->resource)
3272 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003273
3274 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003275}
3276
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003277static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003278fade_out_done(struct weston_view_animation *animation, void *data)
3279{
3280 struct shell_surface *shsurf = data;
3281
3282 weston_surface_destroy(shsurf->surface);
3283}
3284
3285static void
3286handle_resource_destroy(struct wl_listener *listener, void *data)
3287{
3288 struct shell_surface *shsurf =
3289 container_of(listener, struct shell_surface,
3290 resource_destroy_listener);
3291
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003292 if (!weston_surface_is_mapped(shsurf->surface))
3293 return;
3294
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003295 shsurf->surface->ref_count++;
3296
3297 pixman_region32_fini(&shsurf->surface->pending.input);
3298 pixman_region32_init(&shsurf->surface->pending.input);
3299 pixman_region32_fini(&shsurf->surface->input);
3300 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003301 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3302 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3303 fade_out_done, shsurf);
3304 } else {
3305 weston_surface_destroy(shsurf->surface);
3306 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003307}
3308
3309static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003310shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003311
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003312struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003313get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003314{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003315 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003316 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003317 else
3318 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003319}
3320
Rafael Antognollie2a34552013-12-03 15:35:45 -02003321static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003322create_common_surface(struct shell_client *owner, void *shell,
3323 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003324 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003325{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003326 struct shell_surface *shsurf;
3327
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003328 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003329 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003330 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003331 }
3332
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003333 shsurf = calloc(1, sizeof *shsurf);
3334 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003335 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003336 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003337 }
3338
Jason Ekstranda7af7042013-10-12 22:38:11 -05003339 shsurf->view = weston_view_create(surface);
3340 if (!shsurf->view) {
3341 weston_log("no memory to allocate shell surface\n");
3342 free(shsurf);
3343 return NULL;
3344 }
3345
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003346 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003347 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003348
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003349 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3350 wl_resource_add_destroy_listener(surface->resource,
3351 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003352 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003353
Tiago Vignattibc052c92012-04-19 16:18:18 +03003354 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003355 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003356 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003357 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003358 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003359 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003360 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3361 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003362 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003363 wl_list_init(&shsurf->fullscreen.transform.link);
3364
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003365 shsurf->output = get_default_output(shsurf->shell->compositor);
3366
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003367 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003368 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003369 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003370 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003371
3372 /* init link so its safe to always remove it in destroy_shell_surface */
3373 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003374 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003375
Pekka Paalanen460099f2012-01-20 16:48:25 +02003376 /* empty when not in use */
3377 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003378 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003379
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003380 wl_list_init(&shsurf->workspace_transform.link);
3381
Philip Withnall648a4dd2013-11-25 18:01:44 +00003382 wl_list_init(&shsurf->children_link);
3383 wl_list_init(&shsurf->children_list);
3384 shsurf->parent = NULL;
3385
Pekka Paalanen98262232011-12-01 10:42:22 +02003386 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003387
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003388 shsurf->client = client;
3389
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003390 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003391}
3392
Rafael Antognollie2a34552013-12-03 15:35:45 -02003393static struct shell_surface *
3394create_shell_surface(void *shell, struct weston_surface *surface,
3395 const struct weston_shell_client *client)
3396{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003397 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003398}
3399
Jason Ekstranda7af7042013-10-12 22:38:11 -05003400static struct weston_view *
3401get_primary_view(void *shell, struct shell_surface *shsurf)
3402{
3403 return shsurf->view;
3404}
3405
Tiago Vignattibc052c92012-04-19 16:18:18 +03003406static void
3407shell_get_shell_surface(struct wl_client *client,
3408 struct wl_resource *resource,
3409 uint32_t id,
3410 struct wl_resource *surface_resource)
3411{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003412 struct weston_surface *surface =
3413 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003414 struct shell_client *sc = wl_resource_get_user_data(resource);
3415 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003416 struct shell_surface *shsurf;
3417
3418 if (get_shell_surface(surface)) {
3419 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003420 WL_DISPLAY_ERROR_INVALID_OBJECT,
3421 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003422 return;
3423 }
3424
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003425 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003426 if (!shsurf) {
3427 wl_resource_post_error(surface_resource,
3428 WL_DISPLAY_ERROR_INVALID_OBJECT,
3429 "surface->configure already set");
3430 return;
3431 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003432
Jason Ekstranda85118c2013-06-27 20:17:02 -05003433 shsurf->resource =
3434 wl_resource_create(client,
3435 &wl_shell_surface_interface, 1, id);
3436 wl_resource_set_implementation(shsurf->resource,
3437 &shell_surface_implementation,
3438 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003439}
3440
Rafael Antognollie2a34552013-12-03 15:35:45 -02003441static bool
3442shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3443{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003444 /* A shell surface without a resource is created from xwayland
3445 * and is considered a wl_shell surface for now. */
3446
3447 return shsurf->resource == NULL ||
3448 wl_resource_instance_of(shsurf->resource,
3449 &wl_shell_surface_interface,
3450 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003451}
3452
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003453static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003454 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003455};
3456
Rafael Antognollie2a34552013-12-03 15:35:45 -02003457/****************************
3458 * xdg-shell implementation */
3459
3460static void
3461xdg_surface_destroy(struct wl_client *client,
3462 struct wl_resource *resource)
3463{
3464 wl_resource_destroy(resource);
3465}
3466
3467static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003468xdg_surface_set_parent(struct wl_client *client,
3469 struct wl_resource *resource,
3470 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003471{
3472 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3473 struct weston_surface *parent;
3474
3475 if (parent_resource)
3476 parent = wl_resource_get_user_data(parent_resource);
3477 else
3478 parent = NULL;
3479
3480 shell_surface_set_parent(shsurf, parent);
3481}
3482
3483static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003484xdg_surface_set_app_id(struct wl_client *client,
3485 struct wl_resource *resource,
3486 const char *app_id)
3487{
3488 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3489
3490 free(shsurf->class);
3491 shsurf->class = strdup(app_id);
3492}
3493
3494static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003495xdg_surface_show_window_menu(struct wl_client *client,
3496 struct wl_resource *surface_resource,
3497 struct wl_resource *seat_resource,
3498 uint32_t serial,
3499 int32_t x,
3500 int32_t y)
3501{
3502 /* TODO */
3503}
3504
3505static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003506xdg_surface_set_title(struct wl_client *client,
3507 struct wl_resource *resource, const char *title)
3508{
3509 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3510
3511 set_title(shsurf, title);
3512}
3513
3514static void
3515xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3516 struct wl_resource *seat_resource, uint32_t serial)
3517{
3518 common_surface_move(resource, seat_resource, serial);
3519}
3520
3521static void
3522xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3523 struct wl_resource *seat_resource, uint32_t serial,
3524 uint32_t edges)
3525{
3526 common_surface_resize(resource, seat_resource, serial, edges);
3527}
3528
3529static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003530xdg_surface_ack_configure(struct wl_client *client,
3531 struct wl_resource *resource,
3532 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003533{
3534 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3535
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003536 if (shsurf->state_requested) {
3537 shsurf->next_state = shsurf->requested_state;
3538 shsurf->state_changed = true;
3539 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003540 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003541}
3542
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003543static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003544xdg_surface_set_window_geometry(struct wl_client *client,
3545 struct wl_resource *resource,
3546 int32_t x,
3547 int32_t y,
3548 int32_t width,
3549 int32_t height)
3550{
3551 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3552
3553 set_window_geometry(shsurf, x, y, width, height);
3554}
3555
3556static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003557xdg_surface_set_maximized(struct wl_client *client,
3558 struct wl_resource *resource)
3559{
3560 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3561
3562 shsurf->state_requested = true;
3563 shsurf->requested_state.maximized = true;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003564 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003565}
3566
3567static void
3568xdg_surface_unset_maximized(struct wl_client *client,
3569 struct wl_resource *resource)
3570{
3571 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3572
3573 shsurf->state_requested = true;
3574 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003575 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003576}
3577
3578static void
3579xdg_surface_set_fullscreen(struct wl_client *client,
3580 struct wl_resource *resource,
3581 struct wl_resource *output_resource)
3582{
3583 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3584 struct weston_output *output;
3585
3586 shsurf->state_requested = true;
3587 shsurf->requested_state.fullscreen = true;
3588
3589 if (output_resource)
3590 output = wl_resource_get_user_data(output_resource);
3591 else
3592 output = NULL;
3593
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003594 shell_surface_set_output(shsurf, output);
3595 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003596
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003597 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003598}
3599
3600static void
3601xdg_surface_unset_fullscreen(struct wl_client *client,
3602 struct wl_resource *resource)
3603{
3604 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3605
3606 shsurf->state_requested = true;
3607 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003608 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003609}
3610
3611static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003612xdg_surface_set_minimized(struct wl_client *client,
3613 struct wl_resource *resource)
3614{
3615 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3616
3617 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3618 return;
3619
3620 /* apply compositor's own minimization logic (hide) */
3621 set_minimized(shsurf->surface, 1);
3622}
3623
Rafael Antognollie2a34552013-12-03 15:35:45 -02003624static const struct xdg_surface_interface xdg_surface_implementation = {
3625 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003626 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003627 xdg_surface_set_title,
3628 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003629 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003630 xdg_surface_move,
3631 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003632 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003633 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003634 xdg_surface_set_maximized,
3635 xdg_surface_unset_maximized,
3636 xdg_surface_set_fullscreen,
3637 xdg_surface_unset_fullscreen,
3638 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003639};
3640
3641static void
3642xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003643 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003644{
3645 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003646 uint32_t *s;
3647 struct wl_array states;
3648 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003649
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003650 assert(shsurf);
3651
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003652 if (!shsurf->resource)
3653 return;
3654
3655 wl_array_init(&states);
3656 if (shsurf->requested_state.fullscreen) {
3657 s = wl_array_add(&states, sizeof *s);
3658 *s = XDG_SURFACE_STATE_FULLSCREEN;
3659 } else if (shsurf->requested_state.maximized) {
3660 s = wl_array_add(&states, sizeof *s);
3661 *s = XDG_SURFACE_STATE_MAXIMIZED;
3662 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003663 if (shsurf->resize_edges != 0) {
3664 s = wl_array_add(&states, sizeof *s);
3665 *s = XDG_SURFACE_STATE_RESIZING;
3666 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003667 if (shsurf->focus_count > 0) {
3668 s = wl_array_add(&states, sizeof *s);
3669 *s = XDG_SURFACE_STATE_ACTIVATED;
3670 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003671
3672 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3673 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3674
3675 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003676}
3677
3678static const struct weston_shell_client xdg_client = {
3679 xdg_send_configure
3680};
3681
3682static void
3683xdg_use_unstable_version(struct wl_client *client,
3684 struct wl_resource *resource,
3685 int32_t version)
3686{
3687 if (version > 1) {
3688 wl_resource_post_error(resource,
3689 1,
3690 "xdg-shell:: version not implemented yet.");
3691 return;
3692 }
3693}
3694
3695static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003696create_xdg_surface(struct shell_client *owner, void *shell,
3697 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003698 const struct weston_shell_client *client)
3699{
3700 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003701
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003702 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003703 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003704
3705 return shsurf;
3706}
3707
3708static void
3709xdg_get_xdg_surface(struct wl_client *client,
3710 struct wl_resource *resource,
3711 uint32_t id,
3712 struct wl_resource *surface_resource)
3713{
3714 struct weston_surface *surface =
3715 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003716 struct shell_client *sc = wl_resource_get_user_data(resource);
3717 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003718 struct shell_surface *shsurf;
3719
3720 if (get_shell_surface(surface)) {
3721 wl_resource_post_error(surface_resource,
3722 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003723 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003724 return;
3725 }
3726
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003727 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003728 if (!shsurf) {
3729 wl_resource_post_error(surface_resource,
3730 WL_DISPLAY_ERROR_INVALID_OBJECT,
3731 "surface->configure already set");
3732 return;
3733 }
3734
3735 shsurf->resource =
3736 wl_resource_create(client,
3737 &xdg_surface_interface, 1, id);
3738 wl_resource_set_implementation(shsurf->resource,
3739 &xdg_surface_implementation,
3740 shsurf, shell_destroy_shell_surface);
3741}
3742
3743static bool
3744shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3745{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003746 return shsurf->resource &&
3747 wl_resource_instance_of(shsurf->resource,
3748 &xdg_surface_interface,
3749 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003750}
3751
3752/* xdg-popup implementation */
3753
3754static void
3755xdg_popup_destroy(struct wl_client *client,
3756 struct wl_resource *resource)
3757{
3758 wl_resource_destroy(resource);
3759}
3760
Rafael Antognollie2a34552013-12-03 15:35:45 -02003761static const struct xdg_popup_interface xdg_popup_implementation = {
3762 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003763};
3764
3765static void
3766xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003767 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003768{
3769}
3770
3771static const struct weston_shell_client xdg_popup_client = {
3772 xdg_popup_send_configure
3773};
3774
3775static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003776create_xdg_popup(struct shell_client *owner, void *shell,
3777 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003778 const struct weston_shell_client *client,
3779 struct weston_surface *parent,
3780 struct shell_seat *seat,
3781 uint32_t serial,
3782 int32_t x, int32_t y)
3783{
3784 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003785
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003786 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003787 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003788 shsurf->popup.shseat = seat;
3789 shsurf->popup.serial = serial;
3790 shsurf->popup.x = x;
3791 shsurf->popup.y = y;
3792 shell_surface_set_parent(shsurf, parent);
3793
3794 return shsurf;
3795}
3796
3797static void
3798xdg_get_xdg_popup(struct wl_client *client,
3799 struct wl_resource *resource,
3800 uint32_t id,
3801 struct wl_resource *surface_resource,
3802 struct wl_resource *parent_resource,
3803 struct wl_resource *seat_resource,
3804 uint32_t serial,
3805 int32_t x, int32_t y, uint32_t flags)
3806{
3807 struct weston_surface *surface =
3808 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003809 struct shell_client *sc = wl_resource_get_user_data(resource);
3810 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003811 struct shell_surface *shsurf;
3812 struct weston_surface *parent;
3813 struct shell_seat *seat;
3814
3815 if (get_shell_surface(surface)) {
3816 wl_resource_post_error(surface_resource,
3817 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003818 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003819 return;
3820 }
3821
3822 if (!parent_resource) {
3823 wl_resource_post_error(surface_resource,
3824 WL_DISPLAY_ERROR_INVALID_OBJECT,
3825 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04003826 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003827 }
3828
3829 parent = wl_resource_get_user_data(parent_resource);
3830 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3831
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003832 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003833 parent, seat, serial, x, y);
3834 if (!shsurf) {
3835 wl_resource_post_error(surface_resource,
3836 WL_DISPLAY_ERROR_INVALID_OBJECT,
3837 "surface->configure already set");
3838 return;
3839 }
3840
3841 shsurf->resource =
3842 wl_resource_create(client,
3843 &xdg_popup_interface, 1, id);
3844 wl_resource_set_implementation(shsurf->resource,
3845 &xdg_popup_implementation,
3846 shsurf, shell_destroy_shell_surface);
3847}
3848
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003849static void
3850xdg_pong(struct wl_client *client,
3851 struct wl_resource *resource, uint32_t serial)
3852{
3853 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003854
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003855 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003856}
3857
Rafael Antognollie2a34552013-12-03 15:35:45 -02003858static bool
3859shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3860{
3861 return wl_resource_instance_of(shsurf->resource,
3862 &xdg_popup_interface,
3863 &xdg_popup_implementation);
3864}
3865
3866static const struct xdg_shell_interface xdg_implementation = {
3867 xdg_use_unstable_version,
3868 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003869 xdg_get_xdg_popup,
3870 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003871};
3872
3873static int
3874xdg_shell_unversioned_dispatch(const void *implementation,
3875 void *_target, uint32_t opcode,
3876 const struct wl_message *message,
3877 union wl_argument *args)
3878{
3879 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003880 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003881
3882 if (opcode != 0) {
3883 wl_resource_post_error(resource,
3884 WL_DISPLAY_ERROR_INVALID_OBJECT,
3885 "must call use_unstable_version first");
3886 return 0;
3887 }
3888
Pekka Paalanen71182ae2014-08-21 17:47:20 +03003889#define XDG_SERVER_VERSION 4
Rafael Antognollie2a34552013-12-03 15:35:45 -02003890
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003891 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3892 "shell implementation doesn't match protocol version");
3893
Rafael Antognollie2a34552013-12-03 15:35:45 -02003894 if (args[0].i != XDG_SERVER_VERSION) {
3895 wl_resource_post_error(resource,
3896 WL_DISPLAY_ERROR_INVALID_OBJECT,
3897 "incompatible version, server is %d "
3898 "client wants %d",
3899 XDG_SERVER_VERSION, args[0].i);
3900 return 0;
3901 }
3902
3903 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003904 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003905
3906 return 1;
3907}
3908
3909/* end of xdg-shell implementation */
3910/***********************************/
3911
Kristian Høgsberg07937562011-04-12 17:25:42 -04003912static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003913shell_fade(struct desktop_shell *shell, enum fade_type type);
3914
3915static int
3916screensaver_timeout(void *data)
3917{
3918 struct desktop_shell *shell = data;
3919
3920 shell_fade(shell, FADE_OUT);
3921
3922 return 1;
3923}
3924
3925static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003926handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003927{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003928 struct desktop_shell *shell =
3929 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003930
Pekka Paalanen18027e52011-12-02 16:31:49 +02003931 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003932
3933 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003934 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003935}
3936
3937static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003938launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003939{
3940 if (shell->screensaver.binding)
3941 return;
3942
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003943 if (!shell->screensaver.path) {
3944 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003945 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003946 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003947
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003948 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003949 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003950 return;
3951 }
3952
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003953 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003954 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003955 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003956 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003957}
3958
3959static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003960terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003961{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003962 if (shell->screensaver.process.pid == 0)
3963 return;
3964
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003965 /* Disarm the screensaver timer, otherwise it may fire when the
3966 * compositor is not in the idle state. In that case, the screen will
3967 * be locked, but the wake_signal won't fire on user input, making the
3968 * system unresponsive. */
3969 wl_event_source_timer_update(shell->screensaver.timer, 0);
3970
Pekka Paalanen18027e52011-12-02 16:31:49 +02003971 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003972}
3973
3974static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003975configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003976{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003977 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003978
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003979 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003980 if (v->output == ev->output && v != ev) {
3981 weston_view_unmap(v);
3982 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003983 }
3984 }
3985
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003986 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003987
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003988 if (wl_list_empty(&ev->layer_link.link)) {
3989 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003990 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003991 }
3992}
3993
3994static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003995background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003996{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003997 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003998 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003999
Jason Ekstranda7af7042013-10-12 22:38:11 -05004000 view = container_of(es->views.next, struct weston_view, surface_link);
4001
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004002 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004003}
4004
4005static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004006desktop_shell_set_background(struct wl_client *client,
4007 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004008 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004009 struct wl_resource *surface_resource)
4010{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004011 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004012 struct weston_surface *surface =
4013 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004014 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004015
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004016 if (surface->configure) {
4017 wl_resource_post_error(surface_resource,
4018 WL_DISPLAY_ERROR_INVALID_OBJECT,
4019 "surface role already assigned");
4020 return;
4021 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004022
Jason Ekstranda7af7042013-10-12 22:38:11 -05004023 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4024 weston_view_destroy(view);
4025 view = weston_view_create(surface);
4026
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004027 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004028 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004029 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004030 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004031 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004032 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004033 surface->output->width,
4034 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004035}
4036
4037static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004038panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004039{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004040 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004041 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004042
Jason Ekstranda7af7042013-10-12 22:38:11 -05004043 view = container_of(es->views.next, struct weston_view, surface_link);
4044
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004045 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004046}
4047
4048static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004049desktop_shell_set_panel(struct wl_client *client,
4050 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004051 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004052 struct wl_resource *surface_resource)
4053{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004054 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004055 struct weston_surface *surface =
4056 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004057 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004058
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004059 if (surface->configure) {
4060 wl_resource_post_error(surface_resource,
4061 WL_DISPLAY_ERROR_INVALID_OBJECT,
4062 "surface role already assigned");
4063 return;
4064 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004065
Jason Ekstranda7af7042013-10-12 22:38:11 -05004066 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4067 weston_view_destroy(view);
4068 view = weston_view_create(surface);
4069
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004070 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004071 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004072 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004073 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004074 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004075 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004076 surface->output->width,
4077 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004078}
4079
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004080static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004081lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004082{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004083 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004084 struct weston_view *view;
4085
4086 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004087
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004088 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004089 return;
4090
Jason Ekstranda7af7042013-10-12 22:38:11 -05004091 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004092
4093 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004094 weston_layer_entry_insert(&shell->lock_layer.view_list,
4095 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004096 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004097 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004098 }
4099}
4100
4101static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004102handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004103{
Tiago Vignattibe143262012-04-16 17:31:41 +03004104 struct desktop_shell *shell =
4105 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004106
Martin Minarik6d118362012-06-07 18:01:59 +02004107 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004108 shell->lock_surface = NULL;
4109}
4110
4111static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004112desktop_shell_set_lock_surface(struct wl_client *client,
4113 struct wl_resource *resource,
4114 struct wl_resource *surface_resource)
4115{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004116 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004117 struct weston_surface *surface =
4118 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004119
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004120 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004121
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004122 if (!shell->locked)
4123 return;
4124
Pekka Paalanen98262232011-12-01 10:42:22 +02004125 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004126
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004127 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004128 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004129 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004130
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004131 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004132 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004133 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004134}
4135
4136static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004137resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004138{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004139 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004140
Pekka Paalanen77346a62011-11-30 16:26:35 +02004141 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004142
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004143 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004144 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004145 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004146 &shell->input_panel_layer.link);
4147 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004148 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004149 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004150 wl_list_insert(&shell->compositor->cursor_layer.link,
4151 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004152 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004153 wl_list_insert(&shell->fullscreen_layer.link,
4154 &shell->panel_layer.link);
4155 wl_list_insert(&shell->panel_layer.link,
4156 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004157
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004158 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004159
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004160 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004161 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004162 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004163}
4164
4165static void
4166desktop_shell_unlock(struct wl_client *client,
4167 struct wl_resource *resource)
4168{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004169 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004170
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004171 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004172
4173 if (shell->locked)
4174 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004175}
4176
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004177static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004178desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004179 struct wl_resource *resource,
4180 struct wl_resource *surface_resource)
4181{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004182 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004183
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004184 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004185 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004186}
4187
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004188static void
4189desktop_shell_desktop_ready(struct wl_client *client,
4190 struct wl_resource *resource)
4191{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004192 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004193
4194 shell_fade_startup(shell);
4195}
4196
Jonny Lamb765760d2014-08-20 15:53:19 +02004197static void
4198desktop_shell_set_panel_position(struct wl_client *client,
4199 struct wl_resource *resource,
4200 uint32_t position)
4201{
4202 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4203
4204 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4205 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4206 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4207 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4208 wl_resource_post_error(resource,
4209 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4210 "bad position argument");
4211 return;
4212 }
4213
4214 shell->panel_position = position;
4215}
4216
Kristian Høgsberg75840622011-09-06 13:48:16 -04004217static const struct desktop_shell_interface desktop_shell_implementation = {
4218 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004219 desktop_shell_set_panel,
4220 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004221 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004222 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004223 desktop_shell_desktop_ready,
4224 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004225};
4226
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004227static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004228get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004229{
4230 struct shell_surface *shsurf;
4231
4232 shsurf = get_shell_surface(surface);
4233 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004234 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004235 return shsurf->type;
4236}
4237
Kristian Høgsberg75840622011-09-06 13:48:16 -04004238static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004239move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004240{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004241 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004242 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004243 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004244
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004245 if (seat->pointer->focus == NULL)
4246 return;
4247
4248 focus = seat->pointer->focus->surface;
4249
Pekka Paalanen01388e22013-04-25 13:57:44 +03004250 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004251 if (surface == NULL)
4252 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004253
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004254 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004255 if (shsurf == NULL || shsurf->state.fullscreen ||
4256 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004257 return;
4258
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004259 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004260}
4261
4262static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004263maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4264{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004265 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004266 struct weston_surface *surface;
4267 struct shell_surface *shsurf;
4268
4269 surface = weston_surface_get_main_surface(focus);
4270 if (surface == NULL)
4271 return;
4272
4273 shsurf = get_shell_surface(surface);
4274 if (shsurf == NULL)
4275 return;
4276
4277 if (!shell_surface_is_xdg_surface(shsurf))
4278 return;
4279
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004280 shsurf->state_requested = true;
4281 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004282 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004283}
4284
4285static void
4286fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4287{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004288 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004289 struct weston_surface *surface;
4290 struct shell_surface *shsurf;
4291
4292 surface = weston_surface_get_main_surface(focus);
4293 if (surface == NULL)
4294 return;
4295
4296 shsurf = get_shell_surface(surface);
4297 if (shsurf == NULL)
4298 return;
4299
4300 if (!shell_surface_is_xdg_surface(shsurf))
4301 return;
4302
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004303 shsurf->state_requested = true;
4304 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004305 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004306 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004307}
4308
4309static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004310touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4311{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004312 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004313 struct weston_surface *surface;
4314 struct shell_surface *shsurf;
4315
4316 surface = weston_surface_get_main_surface(focus);
4317 if (surface == NULL)
4318 return;
4319
4320 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004321 if (shsurf == NULL || shsurf->state.fullscreen ||
4322 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004323 return;
4324
4325 surface_touch_move(shsurf, (struct weston_seat *) seat);
4326}
4327
4328static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004329resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004330{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004331 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004332 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004333 uint32_t edges = 0;
4334 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004335 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004336
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004337 if (seat->pointer->focus == NULL)
4338 return;
4339
4340 focus = seat->pointer->focus->surface;
4341
Pekka Paalanen01388e22013-04-25 13:57:44 +03004342 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004343 if (surface == NULL)
4344 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004345
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004346 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004347 if (shsurf == NULL || shsurf->state.fullscreen ||
4348 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004349 return;
4350
Jason Ekstranda7af7042013-10-12 22:38:11 -05004351 weston_view_from_global(shsurf->view,
4352 wl_fixed_to_int(seat->pointer->grab_x),
4353 wl_fixed_to_int(seat->pointer->grab_y),
4354 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004355
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004356 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004357 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004358 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004359 edges |= 0;
4360 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004361 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004362
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004363 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004364 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004365 else if (y < 2 * shsurf->surface->height / 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_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004369
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004370 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004371}
4372
4373static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004374surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004375 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004376{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004377 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004378 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004379 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004380 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004381
Pekka Paalanen01388e22013-04-25 13:57:44 +03004382 /* XXX: broken for windows containing sub-surfaces */
4383 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004384 if (surface == NULL)
4385 return;
4386
4387 shsurf = get_shell_surface(surface);
4388 if (!shsurf)
4389 return;
4390
Jason Ekstranda7af7042013-10-12 22:38:11 -05004391 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004392
Jason Ekstranda7af7042013-10-12 22:38:11 -05004393 if (shsurf->view->alpha > 1.0)
4394 shsurf->view->alpha = 1.0;
4395 if (shsurf->view->alpha < step)
4396 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004397
Jason Ekstranda7af7042013-10-12 22:38:11 -05004398 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004399 weston_surface_damage(surface);
4400}
4401
4402static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004403do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004404 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004405{
Daniel Stone37816df2012-05-16 18:45:18 +01004406 struct weston_seat *ws = (struct weston_seat *) seat;
4407 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004408 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004409 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004410
4411 wl_list_for_each(output, &compositor->output_list, link) {
4412 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004413 wl_fixed_to_double(seat->pointer->x),
4414 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004415 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004416 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004417 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004418 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004419 increment = -output->zoom.increment;
4420 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004421 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004422 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004423 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004424 else
4425 increment = 0;
4426
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004427 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004428
Scott Moreaue6603982012-06-11 13:07:51 -06004429 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004430 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004431 else if (output->zoom.level > output->zoom.max_level)
4432 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004433 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004434 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004435 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004436
Scott Moreaue6603982012-06-11 13:07:51 -06004437 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004438
Jason Ekstranda7af7042013-10-12 22:38:11 -05004439 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004440 }
4441 }
4442}
4443
Scott Moreauccbf29d2012-02-22 14:21:41 -07004444static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004445zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004446 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004447{
4448 do_zoom(seat, time, 0, axis, value);
4449}
4450
4451static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004452zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004453 void *data)
4454{
4455 do_zoom(seat, time, key, 0, 0);
4456}
4457
4458static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004459terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004460 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004461{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004462 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004463
Daniel Stone325fc2d2012-05-30 16:31:58 +01004464 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004465}
4466
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004467static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004468rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4469 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004470{
4471 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004472 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004473 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004474 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004475 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004476
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004477 weston_pointer_move(pointer, x, y);
4478
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004479 if (!shsurf)
4480 return;
4481
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004482 cx = 0.5f * shsurf->surface->width;
4483 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004484
Daniel Stone37816df2012-05-16 18:45:18 +01004485 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4486 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004487 r = sqrtf(dx * dx + dy * dy);
4488
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004489 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004490 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004491
4492 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004493 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004494 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004495
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004496 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004497 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004498
4499 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004500 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004501 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004502 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004503 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004504
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004505 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004506 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004507 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004508 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004509 wl_list_init(&shsurf->rotation.transform.link);
4510 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004511 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004512 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004513
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004514 /* We need to adjust the position of the surface
4515 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004516 cposx = shsurf->view->geometry.x + cx;
4517 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004518 dposx = rotate->center.x - cposx;
4519 dposy = rotate->center.y - cposy;
4520 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004521 weston_view_set_position(shsurf->view,
4522 shsurf->view->geometry.x + dposx,
4523 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004524 }
4525
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004526 /* Repaint implies weston_surface_update_transform(), which
4527 * lazily applies the damage due to rotation update.
4528 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004529 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004530}
4531
4532static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004533rotate_grab_button(struct weston_pointer_grab *grab,
4534 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004535{
4536 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004537 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004538 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004539 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004540 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004541
Daniel Stone4dbadb12012-05-30 16:31:51 +01004542 if (pointer->button_count == 0 &&
4543 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004544 if (shsurf)
4545 weston_matrix_multiply(&shsurf->rotation.rotation,
4546 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004547 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004548 free(rotate);
4549 }
4550}
4551
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004552static void
4553rotate_grab_cancel(struct weston_pointer_grab *grab)
4554{
4555 struct rotate_grab *rotate =
4556 container_of(grab, struct rotate_grab, base.grab);
4557
4558 shell_grab_end(&rotate->base);
4559 free(rotate);
4560}
4561
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004562static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004563 noop_grab_focus,
4564 rotate_grab_motion,
4565 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004566 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004567};
4568
4569static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004570surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004571{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004572 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004573 float dx, dy;
4574 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004575
Pekka Paalanen460099f2012-01-20 16:48:25 +02004576 rotate = malloc(sizeof *rotate);
4577 if (!rotate)
4578 return;
4579
Jason Ekstranda7af7042013-10-12 22:38:11 -05004580 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004581 surface->surface->width * 0.5f,
4582 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004583 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004584
Daniel Stone37816df2012-05-16 18:45:18 +01004585 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4586 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004587 r = sqrtf(dx * dx + dy * dy);
4588 if (r > 20.0f) {
4589 struct weston_matrix inverse;
4590
4591 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004592 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004593 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004594
4595 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004596 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004597 } else {
4598 weston_matrix_init(&surface->rotation.rotation);
4599 weston_matrix_init(&rotate->rotation);
4600 }
4601
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004602 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4603 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004604}
4605
4606static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004607rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004608 void *data)
4609{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004610 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004611 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004612 struct shell_surface *surface;
4613
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004614 if (seat->pointer->focus == NULL)
4615 return;
4616
4617 focus = seat->pointer->focus->surface;
4618
Pekka Paalanen01388e22013-04-25 13:57:44 +03004619 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004620 if (base_surface == NULL)
4621 return;
4622
4623 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004624 if (surface == NULL || surface->state.fullscreen ||
4625 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004626 return;
4627
4628 surface_rotate(surface, seat);
4629}
4630
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004631/* Move all fullscreen layers down to the current workspace and hide their
4632 * black views. The surfaces' state is set to both fullscreen and lowered,
4633 * and this is reversed when such a surface is re-configured, see
4634 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4635 *
4636 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004637 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004638void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004639lower_fullscreen_layer(struct desktop_shell *shell)
4640{
4641 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004642 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004643
4644 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004645 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004646 &shell->fullscreen_layer.view_list.link,
4647 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004648 struct shell_surface *shsurf = get_shell_surface(view->surface);
4649
4650 if (!shsurf)
4651 continue;
4652
4653 /* We can have a non-fullscreen popup for a fullscreen surface
4654 * in the fullscreen layer. */
4655 if (shsurf->state.fullscreen) {
4656 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004657 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4658 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004659 weston_view_damage_below(shsurf->fullscreen.black_view);
4660
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004661 }
4662
4663 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004664 weston_layer_entry_remove(&view->layer_link);
4665 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004666 weston_view_damage_below(view);
4667 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004668
4669 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004670 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004671}
4672
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004673void
Tiago Vignattibe143262012-04-16 17:31:41 +03004674activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004675 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004676{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004677 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004678 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004679 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004680 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004681 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004682
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004683 lower_fullscreen_layer(shell);
4684
Pekka Paalanen01388e22013-04-25 13:57:44 +03004685 main_surface = weston_surface_get_main_surface(es);
4686
Daniel Stone37816df2012-05-16 18:45:18 +01004687 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004688
Jonas Ådahl8538b222012-08-29 22:13:03 +02004689 state = ensure_focus_state(shell, seat);
4690 if (state == NULL)
4691 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004692
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004693 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004694 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004695
Rafael Antognolli03b16592013-12-03 15:35:42 -02004696 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004697 assert(shsurf);
4698
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004699 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004700 shell_configure_fullscreen(shsurf);
4701 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004702 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004703
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004704 /* Update the surface’s layer. This brings it to the top of the stacking
4705 * order as appropriate. */
4706 shell_surface_update_layer(shsurf);
4707
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004708 if (shell->focus_animation_type != ANIMATION_NONE) {
4709 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004710 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004711 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004712}
4713
Alex Wu21858432012-04-01 20:13:08 +08004714/* no-op func for checking black surface */
4715static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004716black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004717{
4718}
4719
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004720static bool
Alex Wu21858432012-04-01 20:13:08 +08004721is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4722{
4723 if (es->configure == black_surface_configure) {
4724 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004725 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004726 return true;
4727 }
4728 return false;
4729}
4730
Kristian Høgsberg75840622011-09-06 13:48:16 -04004731static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004732activate_binding(struct weston_seat *seat,
4733 struct desktop_shell *shell,
4734 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004735{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004736 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004737
Alex Wu9c35e6b2012-03-05 14:13:13 +08004738 if (!focus)
4739 return;
4740
Pekka Paalanen01388e22013-04-25 13:57:44 +03004741 if (is_black_surface(focus, &main_surface))
4742 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004743
Pekka Paalanen01388e22013-04-25 13:57:44 +03004744 main_surface = weston_surface_get_main_surface(focus);
4745 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004746 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004747
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004748 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004749}
4750
4751static void
4752click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4753 void *data)
4754{
4755 if (seat->pointer->grab != &seat->pointer->default_grab)
4756 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004757 if (seat->pointer->focus == NULL)
4758 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004759
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004760 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004761}
4762
4763static void
4764touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4765{
4766 if (seat->touch->grab != &seat->touch->default_grab)
4767 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004768 if (seat->touch->focus == NULL)
4769 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004770
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004771 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004772}
4773
4774static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004775unfocus_all_seats(struct desktop_shell *shell)
4776{
4777 struct weston_seat *seat, *next;
4778
4779 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4780 if (seat->keyboard == NULL)
4781 continue;
4782
4783 weston_keyboard_set_focus(seat->keyboard, NULL);
4784 }
4785}
4786
4787static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004788lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004789{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004790 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004791
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004792 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004793 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004794 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004795 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004796
4797 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004798
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004799 /* Hide all surfaces by removing the fullscreen, panel and
4800 * toplevel layers. This way nothing else can show or receive
4801 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004802
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004803 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004804 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004805 if (shell->showing_input_panels)
4806 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004807 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004808 wl_list_insert(&shell->compositor->cursor_layer.link,
4809 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004810
Pekka Paalanen77346a62011-11-30 16:26:35 +02004811 launch_screensaver(shell);
4812
Neil Robertsb4a91702014-04-09 16:33:32 +01004813 /* Remove the keyboard focus on all seats. This will be
4814 * restored to the workspace's saved state via
4815 * restore_focus_state when the compositor is unlocked */
4816 unfocus_all_seats(shell);
4817
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004818 /* TODO: disable bindings that should not work while locked. */
4819
4820 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004821}
4822
4823static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004824unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004825{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004826 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004827 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004828 return;
4829 }
4830
4831 /* If desktop-shell client has gone away, unlock immediately. */
4832 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004833 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004834 return;
4835 }
4836
4837 if (shell->prepare_event_sent)
4838 return;
4839
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004840 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004841 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004842}
4843
4844static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004845shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004846{
4847 struct desktop_shell *shell = data;
4848
4849 shell->fade.animation = NULL;
4850
4851 switch (shell->fade.type) {
4852 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004853 weston_surface_destroy(shell->fade.view->surface);
4854 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004855 break;
4856 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004857 lock(shell);
4858 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004859 default:
4860 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004861 }
4862}
4863
Jason Ekstranda7af7042013-10-12 22:38:11 -05004864static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004865shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004866{
4867 struct weston_compositor *compositor = shell->compositor;
4868 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004869 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004870
4871 surface = weston_surface_create(compositor);
4872 if (!surface)
4873 return NULL;
4874
Jason Ekstranda7af7042013-10-12 22:38:11 -05004875 view = weston_view_create(surface);
4876 if (!view) {
4877 weston_surface_destroy(surface);
4878 return NULL;
4879 }
4880
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004881 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004882 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004883 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004884 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4885 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004886 pixman_region32_init(&surface->input);
4887
Jason Ekstranda7af7042013-10-12 22:38:11 -05004888 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004889}
4890
4891static void
4892shell_fade(struct desktop_shell *shell, enum fade_type type)
4893{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004894 float tint;
4895
4896 switch (type) {
4897 case FADE_IN:
4898 tint = 0.0;
4899 break;
4900 case FADE_OUT:
4901 tint = 1.0;
4902 break;
4903 default:
4904 weston_log("shell: invalid fade type\n");
4905 return;
4906 }
4907
4908 shell->fade.type = type;
4909
Jason Ekstranda7af7042013-10-12 22:38:11 -05004910 if (shell->fade.view == NULL) {
4911 shell->fade.view = shell_fade_create_surface(shell);
4912 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004913 return;
4914
Jason Ekstranda7af7042013-10-12 22:38:11 -05004915 shell->fade.view->alpha = 1.0 - tint;
4916 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004917 }
4918
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004919 if (shell->fade.view->output == NULL) {
4920 /* If the black view gets a NULL output, we lost the
4921 * last output and we'll just cancel the fade. This
4922 * happens when you close the last window under the
4923 * X11 or Wayland backends. */
4924 shell->locked = false;
4925 weston_surface_destroy(shell->fade.view->surface);
4926 shell->fade.view = NULL;
4927 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004928 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004929 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004930 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004931 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004932 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004933 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004934 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004935}
4936
4937static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004938do_shell_fade_startup(void *data)
4939{
4940 struct desktop_shell *shell = data;
4941
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004942 if (shell->startup_animation_type == ANIMATION_FADE)
4943 shell_fade(shell, FADE_IN);
4944 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004945 weston_surface_destroy(shell->fade.view->surface);
4946 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004947 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004948}
4949
4950static void
4951shell_fade_startup(struct desktop_shell *shell)
4952{
4953 struct wl_event_loop *loop;
4954
4955 if (!shell->fade.startup_timer)
4956 return;
4957
4958 wl_event_source_remove(shell->fade.startup_timer);
4959 shell->fade.startup_timer = NULL;
4960
4961 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4962 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4963}
4964
4965static int
4966fade_startup_timeout(void *data)
4967{
4968 struct desktop_shell *shell = data;
4969
4970 shell_fade_startup(shell);
4971 return 0;
4972}
4973
4974static void
4975shell_fade_init(struct desktop_shell *shell)
4976{
4977 /* Make compositor output all black, and wait for the desktop-shell
4978 * client to signal it is ready, then fade in. The timer triggers a
4979 * fade-in, in case the desktop-shell client takes too long.
4980 */
4981
4982 struct wl_event_loop *loop;
4983
Jason Ekstranda7af7042013-10-12 22:38:11 -05004984 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004985 weston_log("%s: warning: fade surface already exists\n",
4986 __func__);
4987 return;
4988 }
4989
Jason Ekstranda7af7042013-10-12 22:38:11 -05004990 shell->fade.view = shell_fade_create_surface(shell);
4991 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004992 return;
4993
Jason Ekstranda7af7042013-10-12 22:38:11 -05004994 weston_view_update_transform(shell->fade.view);
4995 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004996
4997 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4998 shell->fade.startup_timer =
4999 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5000 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5001}
5002
5003static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005004idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005005{
5006 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005007 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005008 struct weston_seat *seat;
5009
5010 wl_list_for_each(seat, &shell->compositor->seat_list, link)
5011 if (seat->pointer)
5012 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005013
5014 shell_fade(shell, FADE_OUT);
5015 /* lock() is called from shell_fade_done() */
5016}
5017
5018static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005019wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005020{
5021 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005022 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005023
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005024 unlock(shell);
5025}
5026
5027static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005028center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005029{
Giulio Camuffob8366642013-04-25 13:57:46 +03005030 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005031 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005032
Jason Ekstranda7af7042013-10-12 22:38:11 -05005033 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005034
5035 x = output->x + (output->width - width) / 2 - surf_x / 2;
5036 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005037
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005038 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005039}
5040
5041static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005042weston_view_set_initial_position(struct weston_view *view,
5043 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005044{
5045 struct weston_compositor *compositor = shell->compositor;
5046 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005047 int32_t range_x, range_y;
5048 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005049 struct weston_output *output, *target_output = NULL;
5050 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005051 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005052
5053 /* As a heuristic place the new window on the same output as the
5054 * pointer. Falling back to the output containing 0, 0.
5055 *
5056 * TODO: Do something clever for touch too?
5057 */
5058 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005059 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005060 ix = wl_fixed_to_int(seat->pointer->x);
5061 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005062 break;
5063 }
5064 }
5065
5066 wl_list_for_each(output, &compositor->output_list, link) {
5067 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5068 target_output = output;
5069 break;
5070 }
5071 }
5072
5073 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005074 weston_view_set_position(view, 10 + random() % 400,
5075 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005076 return;
5077 }
5078
5079 /* Valid range within output where the surface will still be onscreen.
5080 * If this is negative it means that the surface is bigger than
5081 * output.
5082 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005083 get_output_work_area(shell, target_output, &area);
5084
5085 dx = area.x;
5086 dy = area.y;
5087 range_x = area.width - view->surface->width;
5088 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005089
Rob Bradford4cb88c72012-08-13 15:18:44 +01005090 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005091 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005092
5093 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005094 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005095
5096 x = target_output->x + dx;
5097 y = target_output->y + dy;
5098
Jason Ekstranda7af7042013-10-12 22:38:11 -05005099 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005100}
5101
5102static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005103set_maximized_position(struct desktop_shell *shell,
5104 struct shell_surface *shsurf)
5105{
5106 int32_t surf_x, surf_y;
5107 pixman_rectangle32_t area;
5108
5109 /* use surface configure to set the geometry */
5110 get_output_work_area(shell, shsurf->output, &area);
5111 surface_subsurfaces_boundingbox(shsurf->surface,
5112 &surf_x, &surf_y, NULL, NULL);
5113
5114 weston_view_set_position(shsurf->view,
5115 area.x - surf_x,
5116 area.y - surf_y);
5117}
5118
5119static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005120map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005121 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005122{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005123 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005124 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005125
Pekka Paalanen77346a62011-11-30 16:26:35 +02005126 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005127 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005128 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005129 if (shsurf->state.fullscreen) {
5130 center_on_output(shsurf->view, shsurf->fullscreen_output);
5131 shell_map_fullscreen(shsurf);
5132 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005133 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005134 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005135 weston_view_set_initial_position(shsurf->view, shell);
5136 }
Juan Zhao96879df2012-02-07 08:45:41 +08005137 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005138 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005139 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005140 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005141 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005142 weston_view_set_position(shsurf->view,
5143 shsurf->view->geometry.x + sx,
5144 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005145 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005146 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005147 default:
5148 ;
5149 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005150
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005151 /* Surface stacking order, see also activate(). */
5152 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005153
Jason Ekstranda7af7042013-10-12 22:38:11 -05005154 if (shsurf->type != SHELL_SURFACE_NONE) {
5155 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005156 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005157 shsurf->surface->output = shsurf->output;
5158 shsurf->view->output = shsurf->output;
5159 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005160 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005161
Jason Ekstranda7af7042013-10-12 22:38:11 -05005162 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005163 /* XXX: xwayland's using the same fields for transient type */
5164 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005165 if (shsurf->transient.flags ==
5166 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5167 break;
5168 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005169 if (shsurf->state.relative &&
5170 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5171 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005172 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005173 break;
5174 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005175 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005176 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005177 case SHELL_SURFACE_POPUP:
5178 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005179 default:
5180 break;
5181 }
5182
Rafael Antognolli03b16592013-12-03 15:35:42 -02005183 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5184 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005185 {
5186 switch (shell->win_animation_type) {
5187 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005188 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005189 break;
5190 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005191 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005192 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005193 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005194 default:
5195 break;
5196 }
5197 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005198}
5199
5200static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005201configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005202 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005203{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005204 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005205 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005206
Pekka Paalanen77346a62011-11-30 16:26:35 +02005207 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005208
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005209 assert(shsurf);
5210
Rafael Antognolli03b16592013-12-03 15:35:42 -02005211 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005212 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005213 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005214 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005215 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005216 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005217 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005218
Alex Wu4539b082012-03-01 12:57:46 +08005219 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005220 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005221 wl_list_for_each(view, &surface->views, surface_link)
5222 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005223
Rafael Antognolli03b16592013-12-03 15:35:42 -02005224 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005225 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005226 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005227}
5228
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005229static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005230shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005231{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005232 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005233 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005234 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005235
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005236 assert(shsurf);
5237
5238 shell = shsurf->shell;
5239
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005240 if (!weston_surface_is_mapped(es) &&
5241 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005242 remove_popup_grab(shsurf);
5243 }
5244
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005245 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005246 return;
5247
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005248 if (shsurf->has_next_geometry) {
5249 shsurf->geometry = shsurf->next_geometry;
5250 shsurf->has_next_geometry = false;
5251 shsurf->has_set_geometry = true;
5252 } else if (!shsurf->has_set_geometry) {
5253 surface_subsurfaces_boundingbox(shsurf->surface,
5254 &shsurf->geometry.x,
5255 &shsurf->geometry.y,
5256 &shsurf->geometry.width,
5257 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005258 }
5259
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005260 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005261 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005262 type_changed = 1;
5263 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005264
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005265 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005266 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005267 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005268 shsurf->last_width != es->width ||
5269 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005270 float from_x, from_y;
5271 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005272
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005273 if (shsurf->resize_edges) {
5274 sx = 0;
5275 sy = 0;
5276 }
5277
5278 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5279 sx = shsurf->last_width - es->width;
5280 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5281 sy = shsurf->last_height - es->height;
5282
5283 shsurf->last_width = es->width;
5284 shsurf->last_height = es->height;
5285
Jason Ekstranda7af7042013-10-12 22:38:11 -05005286 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5287 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005288 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005289 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005290 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005291 }
5292}
5293
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005294static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005295
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005296static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005297desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005298{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005299 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005300 struct desktop_shell *shell =
5301 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005302
5303 shell->child.process.pid = 0;
5304 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005305
5306 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5307 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005308 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005309 shell->child.deathstamp = time;
5310 shell->child.deathcount = 0;
5311 }
5312
5313 shell->child.deathcount++;
5314 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005315 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005316 return;
5317 }
5318
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005319 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005320 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005321 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005322}
5323
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005324static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005325desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5326{
5327 struct desktop_shell *shell;
5328
5329 shell = container_of(listener, struct desktop_shell,
5330 child.client_destroy_listener);
5331
5332 shell->child.client = NULL;
5333}
5334
5335static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005336launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005337{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005338 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005339
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005340 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005341 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005342 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005343 desktop_shell_sigchld);
5344
5345 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005346 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005347
5348 shell->child.client_destroy_listener.notify =
5349 desktop_shell_client_destroy;
5350 wl_client_add_destroy_listener(shell->child.client,
5351 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005352}
5353
5354static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005355handle_shell_client_destroy(struct wl_listener *listener, void *data)
5356{
5357 struct shell_client *sc =
5358 container_of(listener, struct shell_client, destroy_listener);
5359
5360 if (sc->ping_timer)
5361 wl_event_source_remove(sc->ping_timer);
5362 free(sc);
5363}
5364
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005365static struct shell_client *
5366shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5367 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005368{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005369 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005370
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005371 sc = zalloc(sizeof *sc);
5372 if (sc == NULL) {
5373 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005374 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005375 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005376
5377 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005378 if (sc->resource == NULL) {
5379 free(sc);
5380 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005381 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005382 }
5383
5384 sc->client = client;
5385 sc->shell = shell;
5386 sc->destroy_listener.notify = handle_shell_client_destroy;
5387 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5388
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005389 return sc;
5390}
5391
5392static void
5393bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5394{
5395 struct desktop_shell *shell = data;
5396 struct shell_client *sc;
5397
5398 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5399 if (sc)
5400 wl_resource_set_implementation(sc->resource,
5401 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005402 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005403}
5404
5405static void
5406bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5407{
5408 struct desktop_shell *shell = data;
5409 struct shell_client *sc;
5410
5411 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5412 if (sc)
5413 wl_resource_set_dispatcher(sc->resource,
5414 xdg_shell_unversioned_dispatch,
5415 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005416}
5417
5418static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005419unbind_desktop_shell(struct wl_resource *resource)
5420{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005421 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005422
5423 if (shell->locked)
5424 resume_desktop(shell);
5425
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005426 shell->child.desktop_shell = NULL;
5427 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005428}
5429
5430static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005431bind_desktop_shell(struct wl_client *client,
5432 void *data, uint32_t version, uint32_t id)
5433{
Tiago Vignattibe143262012-04-16 17:31:41 +03005434 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005435 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005436
Jason Ekstranda85118c2013-06-27 20:17:02 -05005437 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005438 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005439
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005440 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005441 wl_resource_set_implementation(resource,
5442 &desktop_shell_implementation,
5443 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005444 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005445
5446 if (version < 2)
5447 shell_fade_startup(shell);
5448
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005449 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005450 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005451
5452 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5453 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005454}
5455
Pekka Paalanen6e168112011-11-24 11:34:05 +02005456static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005457screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005458{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005459 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005460 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005461 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005462
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005463 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005464 return;
5465
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005466 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005467 if (!shell->locked)
5468 return;
5469
Jason Ekstranda7af7042013-10-12 22:38:11 -05005470 view = container_of(surface->views.next, struct weston_view, surface_link);
5471 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005472
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005473 if (wl_list_empty(&view->layer_link.link)) {
5474 prev = container_of(shell->lock_layer.view_list.link.prev,
5475 struct weston_layer_entry, link);
5476 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005477 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005478 wl_event_source_timer_update(shell->screensaver.timer,
5479 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005480 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005481 }
5482}
5483
5484static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005485screensaver_set_surface(struct wl_client *client,
5486 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005487 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005488 struct wl_resource *output_resource)
5489{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005490 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005491 struct weston_surface *surface =
5492 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005493 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005494 struct weston_view *view, *next;
5495
5496 /* Make sure we only have one view */
5497 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5498 weston_view_destroy(view);
5499 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005500
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005501 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005502 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005503 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005504}
5505
5506static const struct screensaver_interface screensaver_implementation = {
5507 screensaver_set_surface
5508};
5509
5510static void
5511unbind_screensaver(struct wl_resource *resource)
5512{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005513 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005514
Pekka Paalanen77346a62011-11-30 16:26:35 +02005515 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005516}
5517
5518static void
5519bind_screensaver(struct wl_client *client,
5520 void *data, uint32_t version, uint32_t id)
5521{
Tiago Vignattibe143262012-04-16 17:31:41 +03005522 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005523 struct wl_resource *resource;
5524
Jason Ekstranda85118c2013-06-27 20:17:02 -05005525 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005526
Pekka Paalanen77346a62011-11-30 16:26:35 +02005527 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005528 wl_resource_set_implementation(resource,
5529 &screensaver_implementation,
5530 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005531 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005532 return;
5533 }
5534
5535 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5536 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005537}
5538
Kristian Høgsberg07045392012-02-19 18:52:44 -05005539struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005540 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005541 struct weston_surface *current;
5542 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005543 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005544 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005545};
5546
5547static void
5548switcher_next(struct switcher *switcher)
5549{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005550 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005551 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005552 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005553 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005554
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005555 /* temporary re-display minimized surfaces */
5556 struct weston_view *tmp;
5557 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005558 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5559 weston_layer_entry_remove(&view->layer_link);
5560 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005561 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5562 *minimized = view;
5563 }
5564
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005565 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005566 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005567 if (shsurf &&
5568 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5569 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005570 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005571 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005572 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005573 next = view->surface;
5574 prev = view->surface;
5575 view->alpha = 0.25;
5576 weston_view_geometry_dirty(view);
5577 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005578 }
Alex Wu1659daa2012-04-01 20:13:09 +08005579
Jason Ekstranda7af7042013-10-12 22:38:11 -05005580 if (is_black_surface(view->surface, NULL)) {
5581 view->alpha = 0.25;
5582 weston_view_geometry_dirty(view);
5583 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005584 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005585 }
5586
5587 if (next == NULL)
5588 next = first;
5589
Alex Wu07b26062012-03-12 16:06:01 +08005590 if (next == NULL)
5591 return;
5592
Kristian Høgsberg07045392012-02-19 18:52:44 -05005593 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005594 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005595
5596 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005597 wl_list_for_each(view, &next->views, surface_link)
5598 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005599
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005600 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005601 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005602 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005603}
5604
5605static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005606switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005607{
5608 struct switcher *switcher =
5609 container_of(listener, struct switcher, listener);
5610
5611 switcher_next(switcher);
5612}
5613
5614static void
Daniel Stone351eb612012-05-31 15:27:47 -04005615switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005616{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005617 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005618 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005619 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005620
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005621 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005622 if (is_focus_view(view))
5623 continue;
5624
Jason Ekstranda7af7042013-10-12 22:38:11 -05005625 view->alpha = 1.0;
5626 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005627 }
5628
Alex Wu07b26062012-03-12 16:06:01 +08005629 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005630 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005631 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005632 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005633 weston_keyboard_end_grab(keyboard);
5634 if (keyboard->input_method_resource)
5635 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005636
5637 /* re-hide surfaces that were temporary shown during the switch */
5638 struct weston_view **minimized;
5639 wl_array_for_each(minimized, &switcher->minimized_array) {
5640 /* with the exception of the current selected */
5641 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005642 weston_layer_entry_remove(&(*minimized)->layer_link);
5643 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005644 weston_view_damage_below(*minimized);
5645 }
5646 }
5647 wl_array_release(&switcher->minimized_array);
5648
Kristian Høgsberg07045392012-02-19 18:52:44 -05005649 free(switcher);
5650}
5651
5652static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005653switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005654 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005655{
5656 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005657 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005658
Daniel Stonec9785ea2012-05-30 16:31:52 +01005659 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005660 switcher_next(switcher);
5661}
5662
5663static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005664switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005665 uint32_t mods_depressed, uint32_t mods_latched,
5666 uint32_t mods_locked, uint32_t group)
5667{
5668 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005669 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005670
Daniel Stone351eb612012-05-31 15:27:47 -04005671 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5672 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005673}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005674
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005675static void
5676switcher_cancel(struct weston_keyboard_grab *grab)
5677{
5678 struct switcher *switcher = container_of(grab, struct switcher, grab);
5679
5680 switcher_destroy(switcher);
5681}
5682
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005683static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005684 switcher_key,
5685 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005686 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005687};
5688
5689static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005690switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005691 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005692{
Tiago Vignattibe143262012-04-16 17:31:41 +03005693 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005694 struct switcher *switcher;
5695
5696 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005697 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005698 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005699 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005700 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005701 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005702
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005703 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005704 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005705 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005706 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5707 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005708 switcher_next(switcher);
5709}
5710
Pekka Paalanen3c647232011-12-22 13:43:43 +02005711static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005712backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005713 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005714{
5715 struct weston_compositor *compositor = data;
5716 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005717 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005718
5719 /* TODO: we're limiting to simple use cases, where we assume just
5720 * control on the primary display. We'd have to extend later if we
5721 * ever get support for setting backlights on random desktop LCD
5722 * panels though */
5723 output = get_default_output(compositor);
5724 if (!output)
5725 return;
5726
5727 if (!output->set_backlight)
5728 return;
5729
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005730 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5731 backlight_new = output->backlight_current - 25;
5732 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5733 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005734
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005735 if (backlight_new < 5)
5736 backlight_new = 5;
5737 if (backlight_new > 255)
5738 backlight_new = 255;
5739
5740 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005741 output->set_backlight(output, output->backlight_current);
5742}
5743
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005744struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005745 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005746 struct weston_seat *seat;
5747 uint32_t key[2];
5748 int key_released[2];
5749};
5750
5751static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005752debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005753 uint32_t key, uint32_t state)
5754{
5755 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005756 struct weston_compositor *ec = db->seat->compositor;
5757 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005758 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005759 uint32_t serial;
5760 int send = 0, terminate = 0;
5761 int check_binding = 1;
5762 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005763 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005764
5765 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5766 /* Do not run bindings on key releases */
5767 check_binding = 0;
5768
5769 for (i = 0; i < 2; i++)
5770 if (key == db->key[i])
5771 db->key_released[i] = 1;
5772
5773 if (db->key_released[0] && db->key_released[1]) {
5774 /* All key releases been swalled so end the grab */
5775 terminate = 1;
5776 } else if (key != db->key[0] && key != db->key[1]) {
5777 /* Should not swallow release of other keys */
5778 send = 1;
5779 }
5780 } else if (key == db->key[0] && !db->key_released[0]) {
5781 /* Do not check bindings for the first press of the binding
5782 * key. This allows it to be used as a debug shortcut.
5783 * We still need to swallow this event. */
5784 check_binding = 0;
5785 } else if (db->key[1]) {
5786 /* If we already ran a binding don't process another one since
5787 * we can't keep track of all the binding keys that were
5788 * pressed in order to swallow the release events. */
5789 send = 1;
5790 check_binding = 0;
5791 }
5792
5793 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005794 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5795 key, state)) {
5796 /* We ran a binding so swallow the press and keep the
5797 * grab to swallow the released too. */
5798 send = 0;
5799 terminate = 0;
5800 db->key[1] = key;
5801 } else {
5802 /* Terminate the grab since the key pressed is not a
5803 * debug binding key. */
5804 send = 1;
5805 terminate = 1;
5806 }
5807 }
5808
5809 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005810 serial = wl_display_next_serial(display);
5811 resource_list = &grab->keyboard->focus_resource_list;
5812 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005813 wl_keyboard_send_key(resource, serial, time, key, state);
5814 }
5815 }
5816
5817 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005818 weston_keyboard_end_grab(grab->keyboard);
5819 if (grab->keyboard->input_method_resource)
5820 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005821 free(db);
5822 }
5823}
5824
5825static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005826debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005827 uint32_t mods_depressed, uint32_t mods_latched,
5828 uint32_t mods_locked, uint32_t group)
5829{
5830 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005831 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005832
Neil Roberts96d790e2013-09-19 17:32:00 +01005833 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005834
Neil Roberts96d790e2013-09-19 17:32:00 +01005835 wl_resource_for_each(resource, resource_list) {
5836 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5837 mods_latched, mods_locked, group);
5838 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005839}
5840
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005841static void
5842debug_binding_cancel(struct weston_keyboard_grab *grab)
5843{
5844 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5845
5846 weston_keyboard_end_grab(grab->keyboard);
5847 free(db);
5848}
5849
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005850struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005851 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005852 debug_binding_modifiers,
5853 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005854};
5855
5856static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005857debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005858{
5859 struct debug_binding_grab *grab;
5860
5861 grab = calloc(1, sizeof *grab);
5862 if (!grab)
5863 return;
5864
5865 grab->seat = (struct weston_seat *) seat;
5866 grab->key[0] = key;
5867 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005868 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005869}
5870
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005871static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005872force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005873 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005874{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005875 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005876 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005877 struct desktop_shell *shell = data;
5878 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005879 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005880
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005881 focus_surface = seat->keyboard->focus;
5882 if (!focus_surface)
5883 return;
5884
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005885 wl_signal_emit(&compositor->kill_signal, focus_surface);
5886
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005887 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005888 wl_client_get_credentials(client, &pid, NULL, NULL);
5889
5890 /* Skip clients that we launched ourselves (the credentials of
5891 * the socketpair is ours) */
5892 if (pid == getpid())
5893 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005894
Daniel Stone325fc2d2012-05-30 16:31:58 +01005895 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005896}
5897
5898static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005899workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005900 uint32_t key, void *data)
5901{
5902 struct desktop_shell *shell = data;
5903 unsigned int new_index = shell->workspaces.current;
5904
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005905 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005906 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005907 if (new_index != 0)
5908 new_index--;
5909
5910 change_workspace(shell, new_index);
5911}
5912
5913static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005914workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005915 uint32_t key, void *data)
5916{
5917 struct desktop_shell *shell = data;
5918 unsigned int new_index = shell->workspaces.current;
5919
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005920 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005921 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005922 if (new_index < shell->workspaces.num - 1)
5923 new_index++;
5924
5925 change_workspace(shell, new_index);
5926}
5927
5928static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005929workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005930 uint32_t key, void *data)
5931{
5932 struct desktop_shell *shell = data;
5933 unsigned int new_index;
5934
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005935 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005936 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005937 new_index = key - KEY_F1;
5938 if (new_index >= shell->workspaces.num)
5939 new_index = shell->workspaces.num - 1;
5940
5941 change_workspace(shell, new_index);
5942}
5943
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005944static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005945workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005946 uint32_t key, void *data)
5947{
5948 struct desktop_shell *shell = data;
5949 unsigned int new_index = shell->workspaces.current;
5950
5951 if (shell->locked)
5952 return;
5953
5954 if (new_index != 0)
5955 new_index--;
5956
5957 take_surface_to_workspace_by_seat(shell, seat, new_index);
5958}
5959
5960static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005961workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005962 uint32_t key, void *data)
5963{
5964 struct desktop_shell *shell = data;
5965 unsigned int new_index = shell->workspaces.current;
5966
5967 if (shell->locked)
5968 return;
5969
5970 if (new_index < shell->workspaces.num - 1)
5971 new_index++;
5972
5973 take_surface_to_workspace_by_seat(shell, seat, new_index);
5974}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005975
5976static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005977shell_reposition_view_on_output_destroy(struct weston_view *view)
5978{
5979 struct weston_output *output, *first_output;
5980 struct weston_compositor *ec = view->surface->compositor;
5981 struct shell_surface *shsurf;
5982 float x, y;
5983 int visible;
5984
5985 x = view->geometry.x;
5986 y = view->geometry.y;
5987
5988 /* At this point the destroyed output is not in the list anymore.
5989 * If the view is still visible somewhere, we leave where it is,
5990 * otherwise, move it to the first output. */
5991 visible = 0;
5992 wl_list_for_each(output, &ec->output_list, link) {
5993 if (pixman_region32_contains_point(&output->region,
5994 x, y, NULL)) {
5995 visible = 1;
5996 break;
5997 }
5998 }
5999
6000 if (!visible) {
6001 first_output = container_of(ec->output_list.next,
6002 struct weston_output, link);
6003
6004 x = first_output->x + first_output->width / 4;
6005 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006006
6007 weston_view_set_position(view, x, y);
6008 } else {
6009 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006010 }
6011
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006012
6013 shsurf = get_shell_surface(view->surface);
6014
6015 if (shsurf) {
6016 shsurf->saved_position_valid = false;
6017 shsurf->next_state.maximized = false;
6018 shsurf->next_state.fullscreen = false;
6019 shsurf->state_changed = true;
6020 }
6021}
6022
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006023void
6024shell_for_each_layer(struct desktop_shell *shell,
6025 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006026{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006027 struct workspace **ws;
6028
6029 func(shell, &shell->fullscreen_layer, data);
6030 func(shell, &shell->panel_layer, data);
6031 func(shell, &shell->background_layer, data);
6032 func(shell, &shell->lock_layer, data);
6033 func(shell, &shell->input_panel_layer, data);
6034
6035 wl_array_for_each(ws, &shell->workspaces.array)
6036 func(shell, &(*ws)->layer, data);
6037}
6038
6039static void
6040shell_output_destroy_move_layer(struct desktop_shell *shell,
6041 struct weston_layer *layer,
6042 void *data)
6043{
6044 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006045 struct weston_view *view;
6046
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006047 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006048 if (view->output != output)
6049 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006050
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006051 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006052 }
6053}
6054
6055static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006056handle_output_destroy(struct wl_listener *listener, void *data)
6057{
6058 struct shell_output *output_listener =
6059 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006060 struct weston_output *output = output_listener->output;
6061 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006062
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006063 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006064
Xiong Zhang6b481422013-10-23 13:58:32 +08006065 wl_list_remove(&output_listener->destroy_listener.link);
6066 wl_list_remove(&output_listener->link);
6067 free(output_listener);
6068}
6069
6070static void
6071create_shell_output(struct desktop_shell *shell,
6072 struct weston_output *output)
6073{
6074 struct shell_output *shell_output;
6075
6076 shell_output = zalloc(sizeof *shell_output);
6077 if (shell_output == NULL)
6078 return;
6079
6080 shell_output->output = output;
6081 shell_output->shell = shell;
6082 shell_output->destroy_listener.notify = handle_output_destroy;
6083 wl_signal_add(&output->destroy_signal,
6084 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006085 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006086}
6087
6088static void
6089handle_output_create(struct wl_listener *listener, void *data)
6090{
6091 struct desktop_shell *shell =
6092 container_of(listener, struct desktop_shell, output_create_listener);
6093 struct weston_output *output = (struct weston_output *)data;
6094
6095 create_shell_output(shell, output);
6096}
6097
6098static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006099handle_output_move_layer(struct desktop_shell *shell,
6100 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006101{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006102 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006103 struct weston_view *view;
6104 float x, y;
6105
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006106 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006107 if (view->output != output)
6108 continue;
6109
6110 x = view->geometry.x + output->move_x;
6111 y = view->geometry.y + output->move_y;
6112 weston_view_set_position(view, x, y);
6113 }
6114}
6115
6116static void
6117handle_output_move(struct wl_listener *listener, void *data)
6118{
6119 struct desktop_shell *shell;
6120
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006121 shell = container_of(listener, struct desktop_shell,
6122 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006123
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006124 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006125}
6126
6127static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006128setup_output_destroy_handler(struct weston_compositor *ec,
6129 struct desktop_shell *shell)
6130{
6131 struct weston_output *output;
6132
6133 wl_list_init(&shell->output_list);
6134 wl_list_for_each(output, &ec->output_list, link)
6135 create_shell_output(shell, output);
6136
6137 shell->output_create_listener.notify = handle_output_create;
6138 wl_signal_add(&ec->output_created_signal,
6139 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006140
6141 shell->output_move_listener.notify = handle_output_move;
6142 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006143}
6144
6145static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006146shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006147{
Tiago Vignattibe143262012-04-16 17:31:41 +03006148 struct desktop_shell *shell =
6149 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006150 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006151 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006152
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006153 /* Force state to unlocked so we don't try to fade */
6154 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006155 if (shell->child.client)
6156 wl_client_destroy(shell->child.client);
6157
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006158 wl_list_remove(&shell->idle_listener.link);
6159 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006160
6161 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006162
Xiong Zhang6b481422013-10-23 13:58:32 +08006163 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6164 wl_list_remove(&shell_output->destroy_listener.link);
6165 wl_list_remove(&shell_output->link);
6166 free(shell_output);
6167 }
6168
6169 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006170 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006171
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006172 wl_array_for_each(ws, &shell->workspaces.array)
6173 workspace_destroy(*ws);
6174 wl_array_release(&shell->workspaces.array);
6175
Pekka Paalanen3c647232011-12-22 13:43:43 +02006176 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006177 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006178 free(shell);
6179}
6180
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006181static void
6182shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6183{
6184 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006185 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006186
6187 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006188 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6189 MODIFIER_CTRL | MODIFIER_ALT,
6190 terminate_binding, ec);
6191 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6192 click_to_activate_binding,
6193 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006194 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6195 click_to_activate_binding,
6196 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006197 weston_compositor_add_touch_binding(ec, 0,
6198 touch_to_activate_binding,
6199 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006200 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6201 MODIFIER_SUPER | MODIFIER_ALT,
6202 surface_opacity_binding, NULL);
6203 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6204 MODIFIER_SUPER, zoom_axis_binding,
6205 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006206
6207 /* configurable bindings */
6208 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006209 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6210 zoom_key_binding, NULL);
6211 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6212 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006213 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6214 maximize_binding, NULL);
6215 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6216 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006217 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6218 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006219 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006220 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6221 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006222 weston_compositor_add_button_binding(ec, BTN_LEFT,
6223 mod | MODIFIER_SHIFT,
6224 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006225
6226 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6227 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6228 rotate_binding, NULL);
6229
Daniel Stone325fc2d2012-05-30 16:31:58 +01006230 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6231 shell);
6232 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6233 ec);
6234 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6235 backlight_binding, ec);
6236 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6237 ec);
6238 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6239 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006240 weston_compositor_add_key_binding(ec, KEY_K, mod,
6241 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006242 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6243 workspace_up_binding, shell);
6244 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6245 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006246 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6247 workspace_move_surface_up_binding,
6248 shell);
6249 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6250 workspace_move_surface_down_binding,
6251 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006252
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006253 if (shell->exposay_modifier)
6254 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6255 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006256
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006257 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6258 if (shell->workspaces.num > 1) {
6259 num_workspace_bindings = shell->workspaces.num;
6260 if (num_workspace_bindings > 6)
6261 num_workspace_bindings = 6;
6262 for (i = 0; i < num_workspace_bindings; i++)
6263 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6264 workspace_f_binding,
6265 shell);
6266 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006267
6268 /* Debug bindings */
6269 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6270 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006271}
6272
Jason Ekstrand024177c2014-04-21 19:42:58 -05006273static void
6274handle_seat_created(struct wl_listener *listener, void *data)
6275{
6276 struct weston_seat *seat = data;
6277
6278 create_shell_seat(seat);
6279}
6280
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006281WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006282module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006283 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006284{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006285 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006286 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006287 struct workspace **pws;
6288 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006289 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006290
Peter Huttererf3d62272013-08-08 11:57:05 +10006291 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006292 if (shell == NULL)
6293 return -1;
6294
Kristian Høgsberg75840622011-09-06 13:48:16 -04006295 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006296
6297 shell->destroy_listener.notify = shell_destroy;
6298 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006299 shell->idle_listener.notify = idle_handler;
6300 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6301 shell->wake_listener.notify = wake_handler;
6302 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006303
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006304 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006305 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006306 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006307 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006308 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006309 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006310 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006311 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006312 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006313 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006314 ec->shell_interface.set_window_geometry = set_window_geometry;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006315
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006316 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6317 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006318 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6319 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006320 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006321
6322 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006323 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006324
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006325 if (input_panel_setup(shell) < 0)
6326 return -1;
6327
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006328 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006329
Daniel Stonedf8133b2013-11-19 11:37:14 +01006330 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6331 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6332
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006333 for (i = 0; i < shell->workspaces.num; i++) {
6334 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6335 if (pws == NULL)
6336 return -1;
6337
6338 *pws = workspace_create();
6339 if (*pws == NULL)
6340 return -1;
6341 }
6342 activate_workspace(shell, 0);
6343
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006344 weston_layer_init(&shell->minimized_layer, NULL);
6345
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006346 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006347 wl_list_init(&shell->workspaces.animation.link);
6348 shell->workspaces.animation.frame = animate_workspace_change_frame;
6349
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006350 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006351 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006352 return -1;
6353
Rafael Antognollie2a34552013-12-03 15:35:45 -02006354 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6355 shell, bind_xdg_shell) == NULL)
6356 return -1;
6357
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006358 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006359 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006360 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006361 return -1;
6362
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006363 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6364 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006365 return -1;
6366
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006367 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6368 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006369 return -1;
6370
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006371 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006372
Jonny Lamb765760d2014-08-20 15:53:19 +02006373 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6374
Xiong Zhang6b481422013-10-23 13:58:32 +08006375 setup_output_destroy_handler(ec, shell);
6376
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006377 loop = wl_display_get_event_loop(ec->wl_display);
6378 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006379
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006380 shell->screensaver.timer =
6381 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6382
Jason Ekstrand024177c2014-04-21 19:42:58 -05006383 wl_list_for_each(seat, &ec->seat_list, link)
6384 handle_seat_created(NULL, seat);
6385 shell->seat_create_listener.notify = handle_seat_created;
6386 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006387
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006388 screenshooter_create(ec);
6389
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006390 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006391
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006392 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006393
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006394 return 0;
6395}