blob: db0c5a9041407e8ee4122cda2d33cca5fe893beb [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;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200226 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100227 struct wl_list surfaces_list;
228 struct wl_client *client;
229 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200230 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100231 } popup_grab;
232};
233
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800234struct shell_client {
235 struct wl_resource *resource;
236 struct wl_client *client;
237 struct desktop_shell *shell;
238 struct wl_listener destroy_listener;
239 struct wl_event_source *ping_timer;
240 uint32_t ping_serial;
241 int unresponsive;
242};
243
Alex Wubd3354b2012-04-17 17:20:49 +0800244static struct desktop_shell *
245shell_surface_get_shell(struct shell_surface *shsurf);
246
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500247static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400248surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500249
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300250static void
251shell_fade_startup(struct desktop_shell *shell);
252
Philip Withnallbecb77e2013-11-25 18:01:30 +0000253static struct shell_seat *
254get_shell_seat(struct weston_seat *seat);
255
Jonny Lambd73c6942014-08-20 15:53:20 +0200256static void
257get_output_panel_size(struct desktop_shell *shell,
258 struct weston_output *output,
259 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700260
Philip Withnall648a4dd2013-11-25 18:01:44 +0000261static void
262shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
263
Alex Wubd3354b2012-04-17 17:20:49 +0800264static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200265shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
266
267static bool
268shell_surface_is_xdg_surface(struct shell_surface *shsurf);
269
270static bool
271shell_surface_is_xdg_popup(struct shell_surface *shsurf);
272
273static void
274shell_surface_set_parent(struct shell_surface *shsurf,
275 struct weston_surface *parent);
276
Pekka Paalanen8274d902014-08-06 19:36:51 +0300277static int
278shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
279{
280 struct shell_surface *shsurf;
281 const char *typestr[] = {
282 [SHELL_SURFACE_NONE] = "unidentified",
283 [SHELL_SURFACE_TOPLEVEL] = "top-level",
284 [SHELL_SURFACE_POPUP] = "popup",
285 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
286 };
287 const char *t, *c;
288
289 shsurf = get_shell_surface(surface);
290 if (!shsurf)
291 return snprintf(buf, len, "unidentified window");
292
293 t = shsurf->title;
294 c = shsurf->class;
295
296 return snprintf(buf, len, "%s window%s%s%s%s%s",
297 typestr[shsurf->type],
298 t ? " '" : "", t ?: "", t ? "'" : "",
299 c ? " of " : "", c ?: "");
300}
301
Rafael Antognollie2a34552013-12-03 15:35:45 -0200302static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800303shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
304{
305 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500306 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800307
308 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100309
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300310 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800311 return false;
312
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300313 top_fs_ev = container_of(shell->fullscreen_layer.view_list.link.next,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500314 struct weston_view,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300315 layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500316 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800317}
318
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500319static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400320destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300321{
322 struct shell_grab *grab;
323
324 grab = container_of(listener, struct shell_grab,
325 shsurf_destroy_listener);
326
327 grab->shsurf = NULL;
328}
329
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800330struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500331get_default_view(struct weston_surface *surface)
332{
333 struct shell_surface *shsurf;
334 struct weston_view *view;
335
336 if (!surface || wl_list_empty(&surface->views))
337 return NULL;
338
339 shsurf = get_shell_surface(surface);
340 if (shsurf)
341 return shsurf->view;
342
343 wl_list_for_each(view, &surface->views, surface_link)
344 if (weston_view_is_mapped(view))
345 return view;
346
347 return container_of(surface->views.next, struct weston_view, surface_link);
348}
349
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300350static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400351popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200352static void
353touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400354
355static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300356shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400357 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300358 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400359 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300360 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300361{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300362 struct desktop_shell *shell = shsurf->shell;
363
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400364 popup_grab_end(pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200365 if (pointer->seat->touch)
366 touch_popup_grab_end(pointer->seat->touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400367
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300368 grab->grab.interface = interface;
369 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400370 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500371 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400372 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300373
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700374 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400375 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400376 if (shell->child.desktop_shell) {
377 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
378 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500379 weston_pointer_set_focus(pointer,
380 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400381 wl_fixed_from_int(0),
382 wl_fixed_from_int(0));
383 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300384}
385
Jonny Lambd73c6942014-08-20 15:53:20 +0200386static void
387get_output_panel_size(struct desktop_shell *shell,
388 struct weston_output *output,
389 int *width,
390 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700391{
392 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200393
394 *width = 0;
395 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700396
397 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200398 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700399
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300400 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200401 float x, y;
402
403 if (view->surface->output != output)
404 continue;
405
406 switch (shell->panel_position) {
407 case DESKTOP_SHELL_PANEL_POSITION_TOP:
408 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
409
410 weston_view_to_global_float(view,
411 view->surface->width, 0,
412 &x, &y);
413
414 *width = (int) x;
415 *height = view->surface->height + (int) y;
416 return;
417
418 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
419 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
420 weston_view_to_global_float(view,
421 0, view->surface->height,
422 &x, &y);
423
424 *width = view->surface->width + (int) x;
425 *height = (int) y;
426 return;
427
428 default:
429 /* we've already set width and height to
430 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700431 break;
432 }
433 }
434
Jonny Lambd73c6942014-08-20 15:53:20 +0200435 /* the correct view wasn't found */
436}
437
438static void
439get_output_work_area(struct desktop_shell *shell,
440 struct weston_output *output,
441 pixman_rectangle32_t *area)
442{
443 int32_t panel_width = 0, panel_height = 0;
444
445 area->x = 0;
446 area->y = 0;
447
448 get_output_panel_size(shell, output, &panel_width, &panel_height);
449
450 switch (shell->panel_position) {
451 case DESKTOP_SHELL_PANEL_POSITION_TOP:
452 default:
453 area->y = panel_height;
454 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
455 area->width = output->width;
456 area->height = output->height - panel_height;
457 break;
458 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
459 area->x = panel_width;
460 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
461 area->width = output->width - panel_width;
462 area->height = output->height;
463 break;
464 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700465}
466
467static void
468send_configure_for_surface(struct shell_surface *shsurf)
469{
470 int32_t width, height;
471 struct surface_state *state;
472
473 if (shsurf->state_requested)
474 state = &shsurf->requested_state;
475 else if (shsurf->state_changed)
476 state = &shsurf->next_state;
477 else
478 state = &shsurf->state;
479
480 if (state->fullscreen) {
481 width = shsurf->output->width;
482 height = shsurf->output->height;
483 } else if (state->maximized) {
484 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200485 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700486
487 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200488 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700489
Jonny Lambd73c6942014-08-20 15:53:20 +0200490 width = area.width;
491 height = area.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700492 } else {
493 width = 0;
494 height = 0;
495 }
496
497 shsurf->client->send_configure(shsurf->surface, width, height);
498}
499
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300500static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400501shell_surface_state_changed(struct shell_surface *shsurf)
502{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700503 if (shell_surface_is_xdg_surface(shsurf))
504 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400505}
506
507static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300508shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300509{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700510 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400511 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700512 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400513
514 if (grab->shsurf->resize_edges) {
515 grab->shsurf->resize_edges = 0;
516 shell_surface_state_changed(grab->shsurf);
517 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700518 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300519
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700520 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300521}
522
523static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700524shell_touch_grab_start(struct shell_touch_grab *grab,
525 const struct weston_touch_grab_interface *interface,
526 struct shell_surface *shsurf,
527 struct weston_touch *touch)
528{
529 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800530
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200531 touch_popup_grab_end(touch);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700532 if (touch->seat->pointer)
533 popup_grab_end(touch->seat->pointer);
534
Rusty Lynch1084da52013-08-15 09:10:08 -0700535 grab->grab.interface = interface;
536 grab->shsurf = shsurf;
537 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
538 wl_signal_add(&shsurf->destroy_signal,
539 &grab->shsurf_destroy_listener);
540
541 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700542 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700543
544 weston_touch_start_grab(touch, &grab->grab);
545 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500546 weston_touch_set_focus(touch->seat,
547 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700548}
549
550static void
551shell_touch_grab_end(struct shell_touch_grab *grab)
552{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700553 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700554 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700555 grab->shsurf->grabbed = 0;
556 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700557
558 weston_touch_end_grab(grab->touch);
559}
560
561static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500562center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800563 struct weston_output *output);
564
Daniel Stone496ca172012-05-30 16:31:42 +0100565static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300566get_modifier(char *modifier)
567{
568 if (!modifier)
569 return MODIFIER_SUPER;
570
571 if (!strcmp("ctrl", modifier))
572 return MODIFIER_CTRL;
573 else if (!strcmp("alt", modifier))
574 return MODIFIER_ALT;
575 else if (!strcmp("super", modifier))
576 return MODIFIER_SUPER;
577 else
578 return MODIFIER_SUPER;
579}
580
Juan Zhaoe10d2792012-04-25 19:09:52 +0800581static enum animation_type
582get_animation_type(char *animation)
583{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800584 if (!animation)
585 return ANIMATION_NONE;
586
Juan Zhaoe10d2792012-04-25 19:09:52 +0800587 if (!strcmp("zoom", animation))
588 return ANIMATION_ZOOM;
589 else if (!strcmp("fade", animation))
590 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100591 else if (!strcmp("dim-layer", animation))
592 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800593 else
594 return ANIMATION_NONE;
595}
596
Alex Wu4539b082012-03-01 12:57:46 +0800597static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400598shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200599{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400600 struct weston_config_section *section;
601 int duration;
Derek Foremanc7210432014-08-21 11:32:38 -0500602 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300603 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200604
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400605 section = weston_config_get_section(shell->compositor->config,
606 "screensaver", NULL, NULL);
607 weston_config_section_get_string(section,
608 "path", &shell->screensaver.path, NULL);
609 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200610 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400611
612 section = weston_config_get_section(shell->compositor->config,
613 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300614 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
615 WESTON_SHELL_CLIENT);
616 if (ret < 0)
617 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400618 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500619 "client", &s, client);
620 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100621 shell->client = s;
622 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400623 "binding-modifier", &s, "super");
624 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200625 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800626
627 weston_config_section_get_string(section,
628 "exposay-modifier", &s, "none");
629 if (strcmp(s, "none") == 0)
630 shell->exposay_modifier = 0;
631 else
632 shell->exposay_modifier = get_modifier(s);
633 free(s);
634
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400635 weston_config_section_get_string(section, "animation", &s, "none");
636 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200637 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200638 weston_config_section_get_string(section, "close-animation", &s, "fade");
639 shell->win_close_animation_type = get_animation_type(s);
640 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700641 weston_config_section_get_string(section,
642 "startup-animation", &s, "fade");
643 shell->startup_animation_type = get_animation_type(s);
644 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700645 if (shell->startup_animation_type == ANIMATION_ZOOM)
646 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100647 weston_config_section_get_string(section, "focus-animation", &s, "none");
648 shell->focus_animation_type = get_animation_type(s);
649 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400650 weston_config_section_get_uint(section, "num-workspaces",
651 &shell->workspaces.num,
652 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200653}
654
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800655struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100656get_default_output(struct weston_compositor *compositor)
657{
658 return container_of(compositor->output_list.next,
659 struct weston_output, link);
660}
661
Pekka Paalanen8274d902014-08-06 19:36:51 +0300662static int
663focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
664{
665 return snprintf(buf, len, "focus highlight effect for output %s",
666 surface->output->name);
667}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100668
669/* no-op func for checking focus surface */
670static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600671focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100672{
673}
674
675static struct focus_surface *
676get_focus_surface(struct weston_surface *surface)
677{
678 if (surface->configure == focus_surface_configure)
679 return surface->configure_private;
680 else
681 return NULL;
682}
683
684static bool
685is_focus_surface (struct weston_surface *es)
686{
687 return (es->configure == focus_surface_configure);
688}
689
690static bool
691is_focus_view (struct weston_view *view)
692{
693 return is_focus_surface (view->surface);
694}
695
696static struct focus_surface *
697create_focus_surface(struct weston_compositor *ec,
698 struct weston_output *output)
699{
700 struct focus_surface *fsurf = NULL;
701 struct weston_surface *surface = NULL;
702
703 fsurf = malloc(sizeof *fsurf);
704 if (!fsurf)
705 return NULL;
706
707 fsurf->surface = weston_surface_create(ec);
708 surface = fsurf->surface;
709 if (surface == NULL) {
710 free(fsurf);
711 return NULL;
712 }
713
714 surface->configure = focus_surface_configure;
715 surface->output = output;
716 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300717 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100718
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800719 fsurf->view = weston_view_create(surface);
720 if (fsurf->view == NULL) {
721 weston_surface_destroy(surface);
722 free(fsurf);
723 return NULL;
724 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100725 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100726
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600727 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600728 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100729 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
730 pixman_region32_fini(&surface->opaque);
731 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
732 output->width, output->height);
733 pixman_region32_fini(&surface->input);
734 pixman_region32_init(&surface->input);
735
736 wl_list_init(&fsurf->workspace_transform.link);
737
738 return fsurf;
739}
740
741static void
742focus_surface_destroy(struct focus_surface *fsurf)
743{
744 weston_surface_destroy(fsurf->surface);
745 free(fsurf);
746}
747
748static void
749focus_animation_done(struct weston_view_animation *animation, void *data)
750{
751 struct workspace *ws = data;
752
753 ws->focus_animation = NULL;
754}
755
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200756static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200757focus_state_destroy(struct focus_state *state)
758{
759 wl_list_remove(&state->seat_destroy_listener.link);
760 wl_list_remove(&state->surface_destroy_listener.link);
761 free(state);
762}
763
764static void
765focus_state_seat_destroy(struct wl_listener *listener, void *data)
766{
767 struct focus_state *state = container_of(listener,
768 struct focus_state,
769 seat_destroy_listener);
770
771 wl_list_remove(&state->link);
772 focus_state_destroy(state);
773}
774
775static void
776focus_state_surface_destroy(struct wl_listener *listener, void *data)
777{
778 struct focus_state *state = container_of(listener,
779 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400780 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400781 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500782 struct weston_surface *main_surface, *next;
783 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200784
Pekka Paalanen01388e22013-04-25 13:57:44 +0300785 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
786
Kristian Høgsberge3778222012-07-31 17:29:30 -0400787 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300788 wl_list_for_each(view,
789 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500790 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400791 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100792 if (is_focus_view(view))
793 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400794
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400796 break;
797 }
798
Pekka Paalanen01388e22013-04-25 13:57:44 +0300799 /* if the focus was a sub-surface, activate its main surface */
800 if (main_surface != state->keyboard_focus)
801 next = main_surface;
802
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100803 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400804 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100805 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100806 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400807 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100808 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
809 if (state->ws->focus_animation)
810 weston_view_animation_destroy(state->ws->focus_animation);
811
812 state->ws->focus_animation = weston_fade_run(
813 state->ws->fsurf_front->view,
814 state->ws->fsurf_front->view->alpha, 0.0, 300,
815 focus_animation_done, state->ws);
816 }
817
Kristian Høgsberge3778222012-07-31 17:29:30 -0400818 wl_list_remove(&state->link);
819 focus_state_destroy(state);
820 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200821}
822
823static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400824focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200825{
Jonas Ådahl04769742012-06-13 00:01:24 +0200826 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200827
828 state = malloc(sizeof *state);
829 if (state == NULL)
830 return NULL;
831
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100832 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400833 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200834 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400835 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200836
837 state->seat_destroy_listener.notify = focus_state_seat_destroy;
838 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400839 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200840 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400841 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200842
843 return state;
844}
845
Jonas Ådahl8538b222012-08-29 22:13:03 +0200846static struct focus_state *
847ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
848{
849 struct workspace *ws = get_current_workspace(shell);
850 struct focus_state *state;
851
852 wl_list_for_each(state, &ws->focus_list, link)
853 if (state->seat == seat)
854 break;
855
856 if (&state->link == &ws->focus_list)
857 state = focus_state_create(seat, ws);
858
859 return state;
860}
861
Jonas Ådahl04769742012-06-13 00:01:24 +0200862static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800863focus_state_set_focus(struct focus_state *state,
864 struct weston_surface *surface)
865{
866 if (state->keyboard_focus) {
867 wl_list_remove(&state->surface_destroy_listener.link);
868 wl_list_init(&state->surface_destroy_listener.link);
869 }
870
871 state->keyboard_focus = surface;
872 if (surface)
873 wl_signal_add(&surface->destroy_signal,
874 &state->surface_destroy_listener);
875}
876
877static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400878restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200879{
880 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400881 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100882 struct wl_list pending_seat_list;
883 struct weston_seat *seat, *next_seat;
884
885 /* Temporarily steal the list of seats so that we can keep
886 * track of the seats we've already processed */
887 wl_list_init(&pending_seat_list);
888 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
889 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200890
891 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100892 wl_list_remove(&state->seat->link);
893 wl_list_insert(&shell->compositor->seat_list,
894 &state->seat->link);
895
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800896 if (state->seat->keyboard == NULL)
897 continue;
898
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400899 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200900
Kristian Høgsberge3148752013-05-06 23:19:49 -0400901 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200902 }
Neil Roberts4237f502014-04-09 16:33:31 +0100903
904 /* For any remaining seats that we don't have a focus state
905 * for we'll reset the keyboard focus to NULL */
906 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
907 wl_list_insert(&shell->compositor->seat_list, &seat->link);
908
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300909 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100910 continue;
911
912 weston_keyboard_set_focus(seat->keyboard, NULL);
913 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200914}
915
916static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200917replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
918 struct weston_seat *seat)
919{
920 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200921
922 wl_list_for_each(state, &ws->focus_list, link) {
923 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800924 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200925 return;
926 }
927 }
928}
929
930static void
931drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
932 struct weston_surface *surface)
933{
934 struct focus_state *state;
935
936 wl_list_for_each(state, &ws->focus_list, link)
937 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800938 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200939}
940
941static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100942animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
943 struct weston_view *from, struct weston_view *to)
944{
945 struct weston_output *output;
946 bool focus_surface_created = false;
947
948 /* FIXME: Only support dim animation using two layers */
949 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
950 return;
951
952 output = get_default_output(shell->compositor);
953 if (ws->fsurf_front == NULL && (from || to)) {
954 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800955 if (ws->fsurf_front == NULL)
956 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100957 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800958
959 ws->fsurf_back = create_focus_surface(shell->compositor, output);
960 if (ws->fsurf_back == NULL) {
961 focus_surface_destroy(ws->fsurf_front);
962 return;
963 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100964 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800965
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 focus_surface_created = true;
967 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300968 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
969 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100970 }
971
972 if (ws->focus_animation) {
973 weston_view_animation_destroy(ws->focus_animation);
974 ws->focus_animation = NULL;
975 }
976
977 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300978 weston_layer_entry_insert(&to->layer_link,
979 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100980 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300981 weston_layer_entry_insert(&ws->layer.view_list,
982 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100983
984 if (focus_surface_created) {
985 ws->focus_animation = weston_fade_run(
986 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200987 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100988 focus_animation_done, ws);
989 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300990 weston_layer_entry_insert(&from->layer_link,
991 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100992 ws->focus_animation = weston_stable_fade_run(
993 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200994 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100995 focus_animation_done, ws);
996 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300997 weston_layer_entry_insert(&ws->layer.view_list,
998 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100999 ws->focus_animation = weston_stable_fade_run(
1000 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001001 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001002 focus_animation_done, ws);
1003 }
1004}
1005
1006static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001007workspace_destroy(struct workspace *ws)
1008{
Jonas Ådahl04769742012-06-13 00:01:24 +02001009 struct focus_state *state, *next;
1010
1011 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1012 focus_state_destroy(state);
1013
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001014 if (ws->fsurf_front)
1015 focus_surface_destroy(ws->fsurf_front);
1016 if (ws->fsurf_back)
1017 focus_surface_destroy(ws->fsurf_back);
1018
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001019 free(ws);
1020}
1021
Jonas Ådahl04769742012-06-13 00:01:24 +02001022static void
1023seat_destroyed(struct wl_listener *listener, void *data)
1024{
1025 struct weston_seat *seat = data;
1026 struct focus_state *state, *next;
1027 struct workspace *ws = container_of(listener,
1028 struct workspace,
1029 seat_destroyed_listener);
1030
1031 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1032 if (state->seat == seat)
1033 wl_list_remove(&state->link);
1034}
1035
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001036static struct workspace *
1037workspace_create(void)
1038{
1039 struct workspace *ws = malloc(sizeof *ws);
1040 if (ws == NULL)
1041 return NULL;
1042
1043 weston_layer_init(&ws->layer, NULL);
1044
Jonas Ådahl04769742012-06-13 00:01:24 +02001045 wl_list_init(&ws->focus_list);
1046 wl_list_init(&ws->seat_destroyed_listener.link);
1047 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001048 ws->fsurf_front = NULL;
1049 ws->fsurf_back = NULL;
1050 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001051
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001052 return ws;
1053}
1054
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055static int
1056workspace_is_empty(struct workspace *ws)
1057{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001058 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001059}
1060
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001061static struct workspace *
1062get_workspace(struct desktop_shell *shell, unsigned int index)
1063{
1064 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001065 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001066 pws += index;
1067 return *pws;
1068}
1069
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001070struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001071get_current_workspace(struct desktop_shell *shell)
1072{
1073 return get_workspace(shell, shell->workspaces.current);
1074}
1075
1076static void
1077activate_workspace(struct desktop_shell *shell, unsigned int index)
1078{
1079 struct workspace *ws;
1080
1081 ws = get_workspace(shell, index);
1082 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1083
1084 shell->workspaces.current = index;
1085}
1086
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001087static unsigned int
1088get_output_height(struct weston_output *output)
1089{
1090 return abs(output->region.extents.y1 - output->region.extents.y2);
1091}
1092
1093static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001094view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001095{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001096 struct weston_transform *transform;
1097
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001098 if (is_focus_view(view)) {
1099 struct focus_surface *fsurf = get_focus_surface(view->surface);
1100 transform = &fsurf->workspace_transform;
1101 } else {
1102 struct shell_surface *shsurf = get_shell_surface(view->surface);
1103 transform = &shsurf->workspace_transform;
1104 }
1105
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001107 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001108 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001109
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001110 weston_matrix_init(&transform->matrix);
1111 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001112 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114}
1115
1116static void
1117workspace_translate_out(struct workspace *ws, double fraction)
1118{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120 unsigned int height;
1121 double d;
1122
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001123 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125 d = height * fraction;
1126
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001127 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128 }
1129}
1130
1131static void
1132workspace_translate_in(struct workspace *ws, double fraction)
1133{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001135 unsigned int height;
1136 double d;
1137
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001138 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001140
1141 if (fraction > 0)
1142 d = -(height - height * fraction);
1143 else
1144 d = height + height * fraction;
1145
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001146 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001147 }
1148}
1149
1150static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001151broadcast_current_workspace_state(struct desktop_shell *shell)
1152{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001153 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001154
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001155 wl_resource_for_each(resource, &shell->workspaces.client_list)
1156 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001157 shell->workspaces.current,
1158 shell->workspaces.num);
1159}
1160
1161static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001162reverse_workspace_change_animation(struct desktop_shell *shell,
1163 unsigned int index,
1164 struct workspace *from,
1165 struct workspace *to)
1166{
1167 shell->workspaces.current = index;
1168
1169 shell->workspaces.anim_to = to;
1170 shell->workspaces.anim_from = from;
1171 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1172 shell->workspaces.anim_timestamp = 0;
1173
Scott Moreau4272e632012-08-13 09:58:41 -06001174 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001175}
1176
1177static void
1178workspace_deactivate_transforms(struct workspace *ws)
1179{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001181 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001183 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001184 if (is_focus_view(view)) {
1185 struct focus_surface *fsurf = get_focus_surface(view->surface);
1186 transform = &fsurf->workspace_transform;
1187 } else {
1188 struct shell_surface *shsurf = get_shell_surface(view->surface);
1189 transform = &shsurf->workspace_transform;
1190 }
1191
1192 if (!wl_list_empty(&transform->link)) {
1193 wl_list_remove(&transform->link);
1194 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001195 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001197 }
1198}
1199
1200static void
1201finish_workspace_change_animation(struct desktop_shell *shell,
1202 struct workspace *from,
1203 struct workspace *to)
1204{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001205 struct weston_view *view;
1206
Scott Moreau4272e632012-08-13 09:58:41 -06001207 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001208
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001209 /* Views that extend past the bottom of the output are still
1210 * visible after the workspace animation ends but before its layer
1211 * is hidden. In that case, we need to damage below those views so
1212 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001213 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001214 weston_view_damage_below(view);
1215
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001216 wl_list_remove(&shell->workspaces.animation.link);
1217 workspace_deactivate_transforms(from);
1218 workspace_deactivate_transforms(to);
1219 shell->workspaces.anim_to = NULL;
1220
1221 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1222}
1223
1224static void
1225animate_workspace_change_frame(struct weston_animation *animation,
1226 struct weston_output *output, uint32_t msecs)
1227{
1228 struct desktop_shell *shell =
1229 container_of(animation, struct desktop_shell,
1230 workspaces.animation);
1231 struct workspace *from = shell->workspaces.anim_from;
1232 struct workspace *to = shell->workspaces.anim_to;
1233 uint32_t t;
1234 double x, y;
1235
1236 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1237 finish_workspace_change_animation(shell, from, to);
1238 return;
1239 }
1240
1241 if (shell->workspaces.anim_timestamp == 0) {
1242 if (shell->workspaces.anim_current == 0.0)
1243 shell->workspaces.anim_timestamp = msecs;
1244 else
1245 shell->workspaces.anim_timestamp =
1246 msecs -
1247 /* Invers of movement function 'y' below. */
1248 (asin(1.0 - shell->workspaces.anim_current) *
1249 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1250 M_2_PI);
1251 }
1252
1253 t = msecs - shell->workspaces.anim_timestamp;
1254
1255 /*
1256 * x = [0, π/2]
1257 * y(x) = sin(x)
1258 */
1259 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1260 y = sin(x);
1261
1262 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001263 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001264
1265 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1266 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1267 shell->workspaces.anim_current = y;
1268
Scott Moreau4272e632012-08-13 09:58:41 -06001269 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001270 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001271 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001272 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001273}
1274
1275static void
1276animate_workspace_change(struct desktop_shell *shell,
1277 unsigned int index,
1278 struct workspace *from,
1279 struct workspace *to)
1280{
1281 struct weston_output *output;
1282
1283 int dir;
1284
1285 if (index > shell->workspaces.current)
1286 dir = -1;
1287 else
1288 dir = 1;
1289
1290 shell->workspaces.current = index;
1291
1292 shell->workspaces.anim_dir = dir;
1293 shell->workspaces.anim_from = from;
1294 shell->workspaces.anim_to = to;
1295 shell->workspaces.anim_current = 0.0;
1296 shell->workspaces.anim_timestamp = 0;
1297
1298 output = container_of(shell->compositor->output_list.next,
1299 struct weston_output, link);
1300 wl_list_insert(&output->animation_list,
1301 &shell->workspaces.animation.link);
1302
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001303 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001304
1305 workspace_translate_in(to, 0);
1306
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001307 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001308
Scott Moreau4272e632012-08-13 09:58:41 -06001309 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001310}
1311
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001312static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001313update_workspace(struct desktop_shell *shell, unsigned int index,
1314 struct workspace *from, struct workspace *to)
1315{
1316 shell->workspaces.current = index;
1317 wl_list_insert(&from->layer.link, &to->layer.link);
1318 wl_list_remove(&from->layer.link);
1319}
1320
1321static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001322change_workspace(struct desktop_shell *shell, unsigned int index)
1323{
1324 struct workspace *from;
1325 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001326 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001327
1328 if (index == shell->workspaces.current)
1329 return;
1330
1331 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001332 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001333 return;
1334
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001335 from = get_current_workspace(shell);
1336 to = get_workspace(shell, index);
1337
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001338 if (shell->workspaces.anim_from == to &&
1339 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001340 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001341 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001342 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001343 return;
1344 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001345
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001346 if (shell->workspaces.anim_to != NULL)
1347 finish_workspace_change_animation(shell,
1348 shell->workspaces.anim_from,
1349 shell->workspaces.anim_to);
1350
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001351 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001352
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001353 if (shell->focus_animation_type != ANIMATION_NONE) {
1354 wl_list_for_each(state, &from->focus_list, link)
1355 if (state->keyboard_focus)
1356 animate_focus_change(shell, from,
1357 get_default_view(state->keyboard_focus), NULL);
1358
1359 wl_list_for_each(state, &to->focus_list, link)
1360 if (state->keyboard_focus)
1361 animate_focus_change(shell, to,
1362 NULL, get_default_view(state->keyboard_focus));
1363 }
1364
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001365 if (workspace_is_empty(to) && workspace_is_empty(from))
1366 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001367 else
1368 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001369
1370 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001371}
1372
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001373static bool
1374workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1375{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001376 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001377 struct wl_list *e;
1378
1379 if (wl_list_empty(list))
1380 return false;
1381
1382 e = list->next;
1383
1384 if (e->next != list)
1385 return false;
1386
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001387 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001388}
1389
1390static void
Philip Withnall659163d2013-11-25 18:01:36 +00001391move_surface_to_workspace(struct desktop_shell *shell,
1392 struct shell_surface *shsurf,
1393 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001394{
1395 struct workspace *from;
1396 struct workspace *to;
1397 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001398 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001399 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001400
1401 if (workspace == shell->workspaces.current)
1402 return;
1403
Philip Withnall659163d2013-11-25 18:01:36 +00001404 view = get_default_view(shsurf->surface);
1405 if (!view)
1406 return;
1407
1408 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1409
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001410 if (workspace >= shell->workspaces.num)
1411 workspace = shell->workspaces.num - 1;
1412
Jonas Ådahle9d22502012-08-29 22:13:01 +02001413 from = get_current_workspace(shell);
1414 to = get_workspace(shell, workspace);
1415
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001416 weston_layer_entry_remove(&view->layer_link);
1417 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001418
Philip Withnall648a4dd2013-11-25 18:01:44 +00001419 shell_surface_update_child_surface_layers(shsurf);
1420
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001422 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1423 if (!seat->keyboard)
1424 continue;
1425
1426 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001428 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001429 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001430
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001432}
1433
1434static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001435take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001436 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001437 unsigned int index)
1438{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001439 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001441 struct shell_surface *shsurf;
1442 struct workspace *from;
1443 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001444 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001445
Pekka Paalanen01388e22013-04-25 13:57:44 +03001446 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 view = get_default_view(surface);
1448 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001449 index == shell->workspaces.current ||
1450 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001451 return;
1452
1453 from = get_current_workspace(shell);
1454 to = get_workspace(shell, index);
1455
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001456 weston_layer_entry_remove(&view->layer_link);
1457 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001458
Philip Withnall659163d2013-11-25 18:01:36 +00001459 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001460 if (shsurf != NULL)
1461 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001462
Jonas Ådahle9d22502012-08-29 22:13:01 +02001463 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001464 drop_focus_state(shell, from, surface);
1465
1466 if (shell->workspaces.anim_from == to &&
1467 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001468 wl_list_remove(&to->layer.link);
1469 wl_list_insert(from->layer.link.prev, &to->layer.link);
1470
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001471 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001472 broadcast_current_workspace_state(shell);
1473
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001474 return;
1475 }
1476
1477 if (shell->workspaces.anim_to != NULL)
1478 finish_workspace_change_animation(shell,
1479 shell->workspaces.anim_from,
1480 shell->workspaces.anim_to);
1481
1482 if (workspace_is_empty(from) &&
1483 workspace_has_only(to, surface))
1484 update_workspace(shell, index, from, to);
1485 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001486 if (shsurf != NULL &&
1487 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001488 wl_list_insert(&shell->workspaces.anim_sticky_list,
1489 &shsurf->workspace_transform.link);
1490
1491 animate_workspace_change(shell, index, from, to);
1492 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001493
1494 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001495
1496 state = ensure_focus_state(shell, seat);
1497 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001498 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001499}
1500
1501static void
1502workspace_manager_move_surface(struct wl_client *client,
1503 struct wl_resource *resource,
1504 struct wl_resource *surface_resource,
1505 uint32_t workspace)
1506{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001507 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001508 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001509 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001510 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001511 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001512
Pekka Paalanen01388e22013-04-25 13:57:44 +03001513 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001514 shell_surface = get_shell_surface(main_surface);
1515 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001517
1518 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001519}
1520
1521static const struct workspace_manager_interface workspace_manager_implementation = {
1522 workspace_manager_move_surface,
1523};
1524
1525static void
1526unbind_resource(struct wl_resource *resource)
1527{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001528 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001529}
1530
1531static void
1532bind_workspace_manager(struct wl_client *client,
1533 void *data, uint32_t version, uint32_t id)
1534{
1535 struct desktop_shell *shell = data;
1536 struct wl_resource *resource;
1537
Jason Ekstranda85118c2013-06-27 20:17:02 -05001538 resource = wl_resource_create(client,
1539 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001540
1541 if (resource == NULL) {
1542 weston_log("couldn't add workspace manager object");
1543 return;
1544 }
1545
Jason Ekstranda85118c2013-06-27 20:17:02 -05001546 wl_resource_set_implementation(resource,
1547 &workspace_manager_implementation,
1548 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001549 wl_list_insert(&shell->workspaces.client_list,
1550 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001551
1552 workspace_manager_send_state(resource,
1553 shell->workspaces.current,
1554 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001555}
1556
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001557static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001558touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1559 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1560{
1561}
1562
1563static void
1564touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1565{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001566 struct weston_touch_move_grab *move =
1567 (struct weston_touch_move_grab *) container_of(
1568 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001569
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001570 if (touch_id == 0)
1571 move->active = 0;
1572
Jonas Ådahl9484b692013-12-02 22:05:03 +01001573 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001574 shell_touch_grab_end(&move->base);
1575 free(move);
1576 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001577}
1578
1579static void
1580touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1581 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1582{
1583 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1584 struct shell_surface *shsurf = move->base.shsurf;
1585 struct weston_surface *es;
1586 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1587 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1588
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001589 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001590 return;
1591
1592 es = shsurf->surface;
1593
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001594 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001595
1596 weston_compositor_schedule_repaint(es->compositor);
1597}
1598
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001599static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001600touch_move_grab_frame(struct weston_touch_grab *grab)
1601{
1602}
1603
1604static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001605touch_move_grab_cancel(struct weston_touch_grab *grab)
1606{
1607 struct weston_touch_move_grab *move =
1608 (struct weston_touch_move_grab *) container_of(
1609 grab, struct shell_touch_grab, grab);
1610
1611 shell_touch_grab_end(&move->base);
1612 free(move);
1613}
1614
Rusty Lynch1084da52013-08-15 09:10:08 -07001615static const struct weston_touch_grab_interface touch_move_grab_interface = {
1616 touch_move_grab_down,
1617 touch_move_grab_up,
1618 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001619 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001620 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001621};
1622
1623static int
1624surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1625{
1626 struct weston_touch_move_grab *move;
1627
1628 if (!shsurf)
1629 return -1;
1630
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001631 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001632 return 0;
1633
1634 move = malloc(sizeof *move);
1635 if (!move)
1636 return -1;
1637
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001638 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001639 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001640 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001642 seat->touch->grab_y;
1643
1644 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1645 seat->touch);
1646
1647 return 0;
1648}
1649
1650static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001651noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001652{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001653}
1654
1655static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001656constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1657{
1658 struct shell_surface *shsurf = move->base.shsurf;
1659 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001660 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001661 const int safety = 50;
1662
1663 x = wl_fixed_to_int(pointer->x + move->dx);
1664 y = wl_fixed_to_int(pointer->y + move->dy);
1665
Jonny Lambd73c6942014-08-20 15:53:20 +02001666 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1667 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1668 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001669
Jonny Lambd73c6942014-08-20 15:53:20 +02001670 bottom = y + shsurf->geometry.height;
1671 if (bottom - panel_height < safety)
1672 y = panel_height + safety -
1673 shsurf->geometry.height;
1674
1675 if (move->client_initiated &&
1676 y + shsurf->geometry.y < panel_height)
1677 y = panel_height - shsurf->geometry.y;
1678 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001679
1680 *cx = x;
1681 *cy = y;
1682}
1683
1684static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001685move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1686 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001687{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001688 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001689 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001690 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001691 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001692
1693 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001694 if (!shsurf)
1695 return;
1696
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001697 constrain_position(move, &cx, &cy);
1698
1699 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001700
Jason Ekstranda7af7042013-10-12 22:38:11 -05001701 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001702}
1703
1704static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001705move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001706 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001707{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001708 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1709 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001710 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001711 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001712
Daniel Stone4dbadb12012-05-30 16:31:51 +01001713 if (pointer->button_count == 0 &&
1714 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001715 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001716 free(grab);
1717 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001718}
1719
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001720static void
1721move_grab_cancel(struct weston_pointer_grab *grab)
1722{
1723 struct shell_grab *shell_grab =
1724 container_of(grab, struct shell_grab, grab);
1725
1726 shell_grab_end(shell_grab);
1727 free(grab);
1728}
1729
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001730static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001731 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001732 move_grab_motion,
1733 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001734 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001735};
1736
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001737static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001738surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1739 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001740{
1741 struct weston_move_grab *move;
1742
1743 if (!shsurf)
1744 return -1;
1745
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001746 if (shsurf->grabbed ||
1747 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001748 return 0;
1749
1750 move = malloc(sizeof *move);
1751 if (!move)
1752 return -1;
1753
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001755 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001756 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001757 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001758 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001759
1760 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001761 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762
1763 return 0;
1764}
1765
1766static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001767common_surface_move(struct wl_resource *resource,
1768 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001769{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001770 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001771 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001772 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001773
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001774 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001775 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001776 seat->pointer->button_count > 0 &&
1777 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001779 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001780 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001781 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001782 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001783 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001784 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001786 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001787 (surface_touch_move(shsurf, seat) < 0))
1788 wl_resource_post_no_memory(resource);
1789 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001790}
1791
Rafael Antognollie2a34552013-12-03 15:35:45 -02001792static void
1793shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1794 struct wl_resource *seat_resource, uint32_t serial)
1795{
1796 common_surface_move(resource, seat_resource, serial);
1797}
1798
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001799struct weston_resize_grab {
1800 struct shell_grab base;
1801 uint32_t edges;
1802 int32_t width, height;
1803};
1804
1805static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001806resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1807 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001808{
1809 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001810 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001811 struct shell_surface *shsurf = resize->base.shsurf;
1812 int32_t width, height;
1813 wl_fixed_t from_x, from_y;
1814 wl_fixed_t to_x, to_y;
1815
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001816 weston_pointer_move(pointer, x, y);
1817
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001818 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001819 return;
1820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 weston_view_from_global_fixed(shsurf->view,
1822 pointer->grab_x, pointer->grab_y,
1823 &from_x, &from_y);
1824 weston_view_from_global_fixed(shsurf->view,
1825 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001826
1827 width = resize->width;
1828 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1829 width += wl_fixed_to_int(from_x - to_x);
1830 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1831 width += wl_fixed_to_int(to_x - from_x);
1832 }
1833
1834 height = resize->height;
1835 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1836 height += wl_fixed_to_int(from_y - to_y);
1837 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1838 height += wl_fixed_to_int(to_y - from_y);
1839 }
1840
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001841 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001842}
1843
1844static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001845send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001846{
1847 struct shell_surface *shsurf = get_shell_surface(surface);
1848
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001849 assert(shsurf);
1850
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001851 if (shsurf->resource)
1852 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001853 shsurf->resize_edges,
1854 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001855}
1856
1857static const struct weston_shell_client shell_client = {
1858 send_configure
1859};
1860
1861static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001862resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001863 uint32_t time, uint32_t button, uint32_t state_w)
1864{
1865 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001866 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001867 enum wl_pointer_button_state state = state_w;
1868
1869 if (pointer->button_count == 0 &&
1870 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1871 shell_grab_end(&resize->base);
1872 free(grab);
1873 }
1874}
1875
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001876static void
1877resize_grab_cancel(struct weston_pointer_grab *grab)
1878{
1879 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1880
1881 shell_grab_end(&resize->base);
1882 free(grab);
1883}
1884
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001885static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001886 noop_grab_focus,
1887 resize_grab_motion,
1888 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001889 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001890};
1891
Giulio Camuffob8366642013-04-25 13:57:46 +03001892/*
1893 * Returns the bounding box of a surface and all its sub-surfaces,
1894 * in the surface coordinates system. */
1895static void
1896surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1897 int32_t *y, int32_t *w, int32_t *h) {
1898 pixman_region32_t region;
1899 pixman_box32_t *box;
1900 struct weston_subsurface *subsurface;
1901
1902 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001903 surface->width,
1904 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001905
1906 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1907 pixman_region32_union_rect(&region, &region,
1908 subsurface->position.x,
1909 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001910 subsurface->surface->width,
1911 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001912 }
1913
1914 box = pixman_region32_extents(&region);
1915 if (x)
1916 *x = box->x1;
1917 if (y)
1918 *y = box->y1;
1919 if (w)
1920 *w = box->x2 - box->x1;
1921 if (h)
1922 *h = box->y2 - box->y1;
1923
1924 pixman_region32_fini(&region);
1925}
1926
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001927static int
1928surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001929 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001930{
1931 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001932 const unsigned resize_topbottom =
1933 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1934 const unsigned resize_leftright =
1935 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1936 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001937
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001938 if (shsurf->grabbed ||
1939 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001940 return 0;
1941
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001942 /* Check for invalid edge combinations. */
1943 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1944 (edges & resize_topbottom) == resize_topbottom ||
1945 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001946 return 0;
1947
1948 resize = malloc(sizeof *resize);
1949 if (!resize)
1950 return -1;
1951
1952 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001953
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001954 resize->width = shsurf->geometry.width;
1955 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001956
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001957 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001958 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001959 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001960 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001961
1962 return 0;
1963}
1964
1965static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001966common_surface_resize(struct wl_resource *resource,
1967 struct wl_resource *seat_resource, uint32_t serial,
1968 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001969{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001970 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001971 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001972 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001973
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001974 if (seat->pointer == NULL ||
1975 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001976 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001977 seat->pointer->focus == NULL)
1978 return;
1979
1980 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1981 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001982 return;
1983
Kristian Høgsberge3148752013-05-06 23:19:49 -04001984 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001985 wl_resource_post_no_memory(resource);
1986}
1987
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001988static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001989shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1990 struct wl_resource *seat_resource, uint32_t serial,
1991 uint32_t edges)
1992{
1993 common_surface_resize(resource, seat_resource, serial, edges);
1994}
1995
1996static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001997busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001998{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001999 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002000 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002001 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002002 wl_fixed_t sx, sy;
2003
Jason Ekstranda7af7042013-10-12 22:38:11 -05002004 view = weston_compositor_pick_view(pointer->seat->compositor,
2005 pointer->x, pointer->y,
2006 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002007
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002008 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2009 shell_grab_end(grab);
2010 free(grab);
2011 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002012}
2013
2014static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002015busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2016 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002017{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002018 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002019}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002020
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002021static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002022busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002023 uint32_t time, uint32_t button, uint32_t state)
2024{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002025 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002026 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002027 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002028
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002029 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002030 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002031 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002032 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002033 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002034 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002035 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002036}
2037
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002038static void
2039busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2040{
2041 struct shell_grab *grab = (struct shell_grab *) base;
2042
2043 shell_grab_end(grab);
2044 free(grab);
2045}
2046
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002047static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002048 busy_cursor_grab_focus,
2049 busy_cursor_grab_motion,
2050 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002051 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002052};
2053
2054static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002055set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002056{
2057 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002058
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002059 if (pointer->grab->interface == &busy_cursor_grab_interface)
2060 return;
2061
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002062 grab = malloc(sizeof *grab);
2063 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002064 return;
2065
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002066 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2067 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002068 /* Mark the shsurf as ungrabbed so that button binding is able
2069 * to move it. */
2070 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002071}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002072
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002073static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002074end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002075{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002076 struct shell_grab *grab;
2077 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002078
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002079 wl_list_for_each(seat, &compositor->seat_list, link) {
2080 if (seat->pointer == NULL)
2081 continue;
2082
2083 grab = (struct shell_grab *) seat->pointer->grab;
2084 if (grab->grab.interface == &busy_cursor_grab_interface &&
2085 wl_resource_get_client(grab->shsurf->resource) == client) {
2086 shell_grab_end(grab);
2087 free(grab);
2088 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002089 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002090}
2091
Scott Moreau9521d5e2012-04-19 13:06:17 -06002092static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002093handle_shell_client_destroy(struct wl_listener *listener, void *data);
2094
2095static int
2096xdg_ping_timeout_handler(void *data)
2097{
2098 struct shell_client *sc = data;
2099 struct weston_seat *seat;
2100 struct shell_surface *shsurf;
2101
2102 /* Client is not responding */
2103 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002104 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2105 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2106 continue;
2107 if (seat->pointer->focus->surface->resource == NULL)
2108 continue;
2109
2110 shsurf = get_shell_surface(seat->pointer->focus->surface);
2111 if (shsurf &&
2112 wl_resource_get_client(shsurf->resource) == sc->client)
2113 set_busy_cursor(shsurf, seat->pointer);
2114 }
2115
2116 return 1;
2117}
2118
2119static void
2120handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2121{
2122 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002123 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002124 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002125 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002126
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002127 if (sc->unresponsive) {
2128 xdg_ping_timeout_handler(sc);
2129 return;
2130 }
2131
2132 sc->ping_serial = serial;
2133 loop = wl_display_get_event_loop(compositor->wl_display);
2134 if (sc->ping_timer == NULL)
2135 sc->ping_timer =
2136 wl_event_loop_add_timer(loop,
2137 xdg_ping_timeout_handler, sc);
2138 if (sc->ping_timer == NULL)
2139 return;
2140
2141 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2142
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002143 if (shell_surface_is_xdg_surface(shsurf) ||
2144 shell_surface_is_xdg_popup(shsurf))
2145 xdg_shell_send_ping(sc->resource, serial);
2146 else if (shell_surface_is_wl_shell_surface(shsurf))
2147 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002148}
2149
Scott Moreauff1db4a2012-04-17 19:06:18 -06002150static void
2151ping_handler(struct weston_surface *surface, uint32_t serial)
2152{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002153 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002154
2155 if (!shsurf)
2156 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002157 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002158 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002159 if (shsurf->surface == shsurf->shell->grab_surface)
2160 return;
2161
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002162 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002163}
2164
2165static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002166handle_pointer_focus(struct wl_listener *listener, void *data)
2167{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002168 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002170 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002171 uint32_t serial;
2172
Jason Ekstranda7af7042013-10-12 22:38:11 -05002173 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002174 return;
2175
Jason Ekstranda7af7042013-10-12 22:38:11 -05002176 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002177 serial = wl_display_next_serial(compositor->wl_display);
2178 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002179}
2180
2181static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002182shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2183{
2184 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002185 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002186}
2187
2188static void
2189shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2190{
2191 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002192 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002193}
2194
2195static void
2196handle_keyboard_focus(struct wl_listener *listener, void *data)
2197{
2198 struct weston_keyboard *keyboard = data;
2199 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2200
2201 if (seat->focused_surface) {
2202 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2203 if (shsurf)
2204 shell_surface_lose_keyboard_focus(shsurf);
2205 }
2206
2207 seat->focused_surface = keyboard->focus;
2208
2209 if (seat->focused_surface) {
2210 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2211 if (shsurf)
2212 shell_surface_gain_keyboard_focus(shsurf);
2213 }
2214}
2215
2216static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002217shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002218{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002219 if (sc->ping_serial != serial)
2220 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002221
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002222 sc->unresponsive = 0;
2223 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002224
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002225 if (sc->ping_timer) {
2226 wl_event_source_remove(sc->ping_timer);
2227 sc->ping_timer = NULL;
2228 }
2229
2230}
2231
2232static void
2233shell_surface_pong(struct wl_client *client,
2234 struct wl_resource *resource, uint32_t serial)
2235{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002236 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2237 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002238
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002239 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002240}
2241
2242static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002243set_title(struct shell_surface *shsurf, const char *title)
2244{
2245 free(shsurf->title);
2246 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002247 shsurf->surface->timeline.force_refresh = 1;
2248}
2249
2250static void
2251set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2252{
2253 shsurf->type = t;
2254 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002255}
2256
2257static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002258set_window_geometry(struct shell_surface *shsurf,
2259 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002260{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002261 shsurf->next_geometry.x = x;
2262 shsurf->next_geometry.y = y;
2263 shsurf->next_geometry.width = width;
2264 shsurf->next_geometry.height = height;
2265 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002266}
2267
2268static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002269shell_surface_set_title(struct wl_client *client,
2270 struct wl_resource *resource, const char *title)
2271{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002272 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002273
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002274 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002275}
2276
2277static void
2278shell_surface_set_class(struct wl_client *client,
2279 struct wl_resource *resource, const char *class)
2280{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002281 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002282
2283 free(shsurf->class);
2284 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002285 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002286}
2287
Alex Wu4539b082012-03-01 12:57:46 +08002288static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002289restore_output_mode(struct weston_output *output)
2290{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002291 if (output->original_mode)
2292 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002293}
2294
2295static void
2296restore_all_output_modes(struct weston_compositor *compositor)
2297{
2298 struct weston_output *output;
2299
2300 wl_list_for_each(output, &compositor->output_list, link)
2301 restore_output_mode(output);
2302}
2303
Philip Withnall07926d92013-11-25 18:01:40 +00002304/* The surface will be inserted into the list immediately after the link
2305 * returned by this function (i.e. will be stacked immediately above the
2306 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002307static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002308shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2309{
2310 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002311 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002312
2313 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002314 case SHELL_SURFACE_XWAYLAND:
2315 return &shsurf->shell->fullscreen_layer.view_list;
2316
2317 case SHELL_SURFACE_NONE:
2318 return NULL;
2319
Kristian Høgsbergead52422014-01-01 13:51:52 -08002320 case SHELL_SURFACE_POPUP:
2321 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002322 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002323 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002324 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002325 /* Move the surface to its parent layer so
2326 * that surfaces which are transient for
2327 * fullscreen surfaces don't get hidden by the
2328 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002329
2330 /* TODO: Handle a parent with multiple views */
2331 parent = get_default_view(shsurf->parent);
2332 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002333 return container_of(parent->layer_link.link.prev,
2334 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002335 }
Philip Withnall07926d92013-11-25 18:01:40 +00002336
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002337 /* Move the surface to a normal workspace layer so that surfaces
2338 * which were previously fullscreen or transient are no longer
2339 * rendered on top. */
2340 ws = get_current_workspace(shsurf->shell);
2341 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002342 }
2343
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002344 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002345}
2346
Philip Withnall648a4dd2013-11-25 18:01:44 +00002347static void
2348shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2349{
2350 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002351 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002352
2353 /* Move the child layers to the same workspace as shsurf. They will be
2354 * stacked above shsurf. */
2355 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002356 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002357 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002358 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002359 prev = container_of(shsurf->view->layer_link.link.prev,
2360 struct weston_layer_entry, link);
2361 weston_layer_entry_remove(&child->view->layer_link);
2362 weston_layer_entry_insert(prev,
2363 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002364 weston_view_geometry_dirty(child->view);
2365 weston_surface_damage(child->surface);
2366
2367 /* Recurse. We don’t expect this to recurse very far (if
2368 * at all) because that would imply we have transient
2369 * (or popup) children of transient surfaces, which
2370 * would be unusual. */
2371 shell_surface_update_child_surface_layers(child);
2372 }
2373 }
2374}
2375
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002376/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002377 * geometry to ensure the changes are redrawn.
2378 *
2379 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2380 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002381static void
2382shell_surface_update_layer(struct shell_surface *shsurf)
2383{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002384 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002385
2386 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2387
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002388 if (new_layer_link == NULL)
2389 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002390 if (new_layer_link == &shsurf->view->layer_link)
2391 return;
2392
2393 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002394 weston_layer_entry_remove(&shsurf->view->layer_link);
2395 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002396 weston_view_geometry_dirty(shsurf->view);
2397 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002398
2399 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002400}
2401
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002402static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002403shell_surface_set_parent(struct shell_surface *shsurf,
2404 struct weston_surface *parent)
2405{
2406 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002407
2408 wl_list_remove(&shsurf->children_link);
2409 wl_list_init(&shsurf->children_link);
2410
2411 /* Insert into the parent surface’s child list. */
2412 if (parent != NULL) {
2413 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2414 if (parent_shsurf != NULL)
2415 wl_list_insert(&parent_shsurf->children_list,
2416 &shsurf->children_link);
2417 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002418}
2419
2420static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002421shell_surface_set_output(struct shell_surface *shsurf,
2422 struct weston_output *output)
2423{
2424 struct weston_surface *es = shsurf->surface;
2425
2426 /* get the default output, if the client set it as NULL
2427 check whether the ouput is available */
2428 if (output)
2429 shsurf->output = output;
2430 else if (es->output)
2431 shsurf->output = es->output;
2432 else
2433 shsurf->output = get_default_output(es->compositor);
2434}
2435
2436static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002437surface_clear_next_states(struct shell_surface *shsurf)
2438{
2439 shsurf->next_state.maximized = false;
2440 shsurf->next_state.fullscreen = false;
2441
2442 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2443 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2444 shsurf->state_changed = true;
2445}
2446
2447static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002448set_toplevel(struct shell_surface *shsurf)
2449{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002450 shell_surface_set_parent(shsurf, NULL);
2451 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002452 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002453
2454 /* The layer_link is updated in set_surface_type(),
2455 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002456}
2457
2458static void
2459shell_surface_set_toplevel(struct wl_client *client,
2460 struct wl_resource *resource)
2461{
2462 struct shell_surface *surface = wl_resource_get_user_data(resource);
2463
2464 set_toplevel(surface);
2465}
2466
2467static void
2468set_transient(struct shell_surface *shsurf,
2469 struct weston_surface *parent, int x, int y, uint32_t flags)
2470{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002471 assert(parent != NULL);
2472
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002473 shell_surface_set_parent(shsurf, parent);
2474
2475 surface_clear_next_states(shsurf);
2476
Philip Withnallbecb77e2013-11-25 18:01:30 +00002477 shsurf->transient.x = x;
2478 shsurf->transient.y = y;
2479 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002480
Rafael Antognollied207b42013-12-03 15:35:43 -02002481 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002482 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002483 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002484
2485 /* The layer_link is updated in set_surface_type(),
2486 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002487}
2488
2489static void
2490shell_surface_set_transient(struct wl_client *client,
2491 struct wl_resource *resource,
2492 struct wl_resource *parent_resource,
2493 int x, int y, uint32_t flags)
2494{
2495 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2496 struct weston_surface *parent =
2497 wl_resource_get_user_data(parent_resource);
2498
2499 set_transient(shsurf, parent, x, y, flags);
2500}
2501
2502static void
2503set_fullscreen(struct shell_surface *shsurf,
2504 uint32_t method,
2505 uint32_t framerate,
2506 struct weston_output *output)
2507{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002508 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002509 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002510
2511 shsurf->fullscreen_output = shsurf->output;
2512 shsurf->fullscreen.type = method;
2513 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002514
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002515 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002516}
2517
2518static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002519weston_view_set_initial_position(struct weston_view *view,
2520 struct desktop_shell *shell);
2521
2522static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002523unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002524{
Philip Withnallf85fe842013-11-25 18:01:37 +00002525 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002526 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2527 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002528 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002529 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002530
Alex Wu4539b082012-03-01 12:57:46 +08002531 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2532 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002533
Alex Wu4539b082012-03-01 12:57:46 +08002534 wl_list_remove(&shsurf->fullscreen.transform.link);
2535 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002536
Jason Ekstranda7af7042013-10-12 22:38:11 -05002537 if (shsurf->fullscreen.black_view)
2538 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2539 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002540
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002541 if (shsurf->saved_position_valid)
2542 weston_view_set_position(shsurf->view,
2543 shsurf->saved_x, shsurf->saved_y);
2544 else
2545 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2546
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002547 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002548 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002549 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002550 shsurf->saved_rotation_valid = false;
2551 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002552
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002553 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002554}
2555
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002556static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002557shell_surface_set_fullscreen(struct wl_client *client,
2558 struct wl_resource *resource,
2559 uint32_t method,
2560 uint32_t framerate,
2561 struct wl_resource *output_resource)
2562{
2563 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2564 struct weston_output *output;
2565
2566 if (output_resource)
2567 output = wl_resource_get_user_data(output_resource);
2568 else
2569 output = NULL;
2570
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002571 shell_surface_set_parent(shsurf, NULL);
2572
Rafael Antognolli03b16592013-12-03 15:35:42 -02002573 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002574 shsurf->next_state.fullscreen = true;
2575 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002576 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002577}
2578
2579static void
2580set_popup(struct shell_surface *shsurf,
2581 struct weston_surface *parent,
2582 struct weston_seat *seat,
2583 uint32_t serial,
2584 int32_t x,
2585 int32_t y)
2586{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002587 assert(parent != NULL);
2588
Philip Withnallbecb77e2013-11-25 18:01:30 +00002589 shsurf->popup.shseat = get_shell_seat(seat);
2590 shsurf->popup.serial = serial;
2591 shsurf->popup.x = x;
2592 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002593
Pekka Paalanenb5026542014-11-12 15:09:24 +02002594 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002595}
2596
2597static void
2598shell_surface_set_popup(struct wl_client *client,
2599 struct wl_resource *resource,
2600 struct wl_resource *seat_resource,
2601 uint32_t serial,
2602 struct wl_resource *parent_resource,
2603 int32_t x, int32_t y, uint32_t flags)
2604{
2605 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002606 struct weston_surface *parent =
2607 wl_resource_get_user_data(parent_resource);
2608
2609 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002610
Rafael Antognolli03b16592013-12-03 15:35:42 -02002611 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002612 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002613 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002614 wl_resource_get_user_data(seat_resource),
2615 serial, x, y);
2616}
2617
2618static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002619unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002620{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002621 /* undo all maximized things here */
2622 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002623
2624 if (shsurf->saved_position_valid)
2625 weston_view_set_position(shsurf->view,
2626 shsurf->saved_x, shsurf->saved_y);
2627 else
2628 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002629
2630 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002631 wl_list_insert(&shsurf->view->geometry.transformation_list,
2632 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002633 shsurf->saved_rotation_valid = false;
2634 }
2635
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002636 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002637}
2638
Philip Withnallbecb77e2013-11-25 18:01:30 +00002639static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002640set_minimized(struct weston_surface *surface, uint32_t is_true)
2641{
2642 struct shell_surface *shsurf;
2643 struct workspace *current_ws;
2644 struct weston_seat *seat;
2645 struct weston_surface *focus;
2646 struct weston_view *view;
2647
2648 view = get_default_view(surface);
2649 if (!view)
2650 return;
2651
2652 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2653
2654 shsurf = get_shell_surface(surface);
2655 current_ws = get_current_workspace(shsurf->shell);
2656
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002657 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002658 /* hide or show, depending on the state */
2659 if (is_true) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002660 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002661
2662 drop_focus_state(shsurf->shell, current_ws, view->surface);
2663 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2664 if (!seat->keyboard)
2665 continue;
2666 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2667 if (focus == view->surface)
2668 weston_keyboard_set_focus(seat->keyboard, NULL);
2669 }
2670 }
2671 else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002672 weston_layer_entry_insert(&current_ws->layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002673
2674 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2675 if (!seat->keyboard)
2676 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002677 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002678 }
2679 }
2680
2681 shell_surface_update_child_surface_layers(shsurf);
2682
2683 weston_view_damage_below(view);
2684}
2685
2686static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002687shell_surface_set_maximized(struct wl_client *client,
2688 struct wl_resource *resource,
2689 struct wl_resource *output_resource)
2690{
2691 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2692 struct weston_output *output;
2693
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002694 surface_clear_next_states(shsurf);
2695 shsurf->next_state.maximized = true;
2696 shsurf->state_changed = true;
2697
Pekka Paalanenb5026542014-11-12 15:09:24 +02002698 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002699 shell_surface_set_parent(shsurf, NULL);
2700
Philip Withnallbecb77e2013-11-25 18:01:30 +00002701 if (output_resource)
2702 output = wl_resource_get_user_data(output_resource);
2703 else
2704 output = NULL;
2705
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002706 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002707
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002708 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002709}
2710
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002711/* This is only ever called from set_surface_type(), so there’s no need to
2712 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002713static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002714reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002715{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002716 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002717 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002718 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002719 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002720
Pekka Paalanen98262232011-12-01 10:42:22 +02002721 return 0;
2722}
2723
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002724static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002725set_full_output(struct shell_surface *shsurf)
2726{
2727 shsurf->saved_x = shsurf->view->geometry.x;
2728 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002729 shsurf->saved_width = shsurf->surface->width;
2730 shsurf->saved_height = shsurf->surface->height;
2731 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002732 shsurf->saved_position_valid = true;
2733
2734 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2735 wl_list_remove(&shsurf->rotation.transform.link);
2736 wl_list_init(&shsurf->rotation.transform.link);
2737 weston_view_geometry_dirty(shsurf->view);
2738 shsurf->saved_rotation_valid = true;
2739 }
2740}
2741
2742static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002743set_surface_type(struct shell_surface *shsurf)
2744{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002745 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002746 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002747
Philip Withnallbecb77e2013-11-25 18:01:30 +00002748 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002749
Rafael Antognolli03b16592013-12-03 15:35:42 -02002750 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002751 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002752
2753 switch (shsurf->type) {
2754 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002755 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002756 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002757 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002758 weston_view_set_position(shsurf->view,
2759 pev->geometry.x + shsurf->transient.x,
2760 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002761 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002762 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002763
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002764 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002765 weston_view_set_position(shsurf->view, shsurf->transient.x,
2766 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002767 break;
2768
Philip Withnall0f640e22013-11-25 18:01:31 +00002769 case SHELL_SURFACE_POPUP:
2770 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002771 default:
2772 break;
2773 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002774
2775 /* Update the surface’s layer. */
2776 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002777}
2778
Tiago Vignattibe143262012-04-16 17:31:41 +03002779static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002780shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002781{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002782 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002783}
2784
Pekka Paalanen8274d902014-08-06 19:36:51 +03002785static int
2786black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2787{
2788 struct weston_surface *fs_surface = surface->configure_private;
2789 int n;
2790 int rem;
2791 int ret;
2792
2793 n = snprintf(buf, len, "black background surface for ");
2794 if (n < 0)
2795 return n;
2796
2797 rem = (int)len - n;
2798 if (rem < 0)
2799 rem = 0;
2800
2801 if (fs_surface->get_label)
2802 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2803 else
2804 ret = snprintf(buf + n, rem, "<unknown>");
2805
2806 if (ret < 0)
2807 return n;
2808
2809 return n + ret;
2810}
2811
Alex Wu21858432012-04-01 20:13:08 +08002812static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002813black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002814
Jason Ekstranda7af7042013-10-12 22:38:11 -05002815static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002816create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002817 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002818 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002819{
2820 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002821 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002822
2823 surface = weston_surface_create(ec);
2824 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002825 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002826 return NULL;
2827 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002828 view = weston_view_create(surface);
2829 if (surface == NULL) {
2830 weston_log("no memory\n");
2831 weston_surface_destroy(surface);
2832 return NULL;
2833 }
Alex Wu4539b082012-03-01 12:57:46 +08002834
Alex Wu21858432012-04-01 20:13:08 +08002835 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002836 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002837 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002838 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002839 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002840 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002841 pixman_region32_fini(&surface->input);
2842 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002843
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002844 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002845 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002846
2847 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002848}
2849
Philip Withnalled8f1a92013-11-25 18:01:43 +00002850static void
2851shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2852{
2853 struct weston_output *output = shsurf->fullscreen_output;
2854
Rafael Antognolli03b16592013-12-03 15:35:42 -02002855 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002856
2857 if (!shsurf->fullscreen.black_view)
2858 shsurf->fullscreen.black_view =
2859 create_black_surface(shsurf->surface->compositor,
2860 shsurf->surface,
2861 output->x, output->y,
2862 output->width,
2863 output->height);
2864
2865 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002866 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2867 weston_layer_entry_insert(&shsurf->view->layer_link,
2868 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002869 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2870 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002871
2872 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002873}
2874
Alex Wu4539b082012-03-01 12:57:46 +08002875/* Create black surface and append it to the associated fullscreen surface.
2876 * Handle size dismatch and positioning according to the method. */
2877static void
2878shell_configure_fullscreen(struct shell_surface *shsurf)
2879{
2880 struct weston_output *output = shsurf->fullscreen_output;
2881 struct weston_surface *surface = shsurf->surface;
2882 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002883 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002884 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002885
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002886 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2887 restore_output_mode(output);
2888
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002889 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002890 weston_layer_entry_remove(&shsurf->view->layer_link);
2891 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002892
Philip Withnalled8f1a92013-11-25 18:01:43 +00002893 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002894
Jason Ekstranda7af7042013-10-12 22:38:11 -05002895 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002896 &surf_width, &surf_height);
2897
Alex Wu4539b082012-03-01 12:57:46 +08002898 switch (shsurf->fullscreen.type) {
2899 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002900 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002901 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002902 break;
2903 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002904 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002905 if (output->width == surf_width &&
2906 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002907 weston_view_set_position(shsurf->view,
2908 output->x - surf_x,
2909 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002910 break;
2911 }
2912
Alex Wu4539b082012-03-01 12:57:46 +08002913 matrix = &shsurf->fullscreen.transform.matrix;
2914 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002915
Scott Moreau1bad5db2012-08-18 01:04:05 -06002916 output_aspect = (float) output->width /
2917 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002918 /* XXX: Use surf_width and surf_height here? */
2919 surface_aspect = (float) surface->width /
2920 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002921 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002922 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002923 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002924 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002925 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002926 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002927
Alex Wu4539b082012-03-01 12:57:46 +08002928 weston_matrix_scale(matrix, scale, scale, 1);
2929 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002930 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002931 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002932 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2933 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002934 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002935
Alex Wu4539b082012-03-01 12:57:46 +08002936 break;
2937 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002938 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002939 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002940 surf_width * surface->buffer_viewport.buffer.scale,
2941 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002942 shsurf->fullscreen.framerate};
2943
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002944 if (weston_output_mode_switch_to_temporary(output, &mode,
2945 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002946 weston_view_set_position(shsurf->view,
2947 output->x - surf_x,
2948 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002949 shsurf->fullscreen.black_view->surface->width = output->width;
2950 shsurf->fullscreen.black_view->surface->height = output->height;
2951 weston_view_set_position(shsurf->fullscreen.black_view,
2952 output->x - surf_x,
2953 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002954 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002955 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002956 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002957 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002958 }
Alex Wubd3354b2012-04-17 17:20:49 +08002959 }
Alex Wu4539b082012-03-01 12:57:46 +08002960 break;
2961 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002962 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002963 break;
2964 default:
2965 break;
2966 }
2967}
2968
Alex Wu4539b082012-03-01 12:57:46 +08002969static void
2970shell_map_fullscreen(struct shell_surface *shsurf)
2971{
Alex Wubd3354b2012-04-17 17:20:49 +08002972 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002973}
2974
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002975static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002976set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2977{
2978 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002979 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002980 shsurf->transient.x = x;
2981 shsurf->transient.y = y;
2982 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002983
2984 shell_surface_set_parent(shsurf, NULL);
2985
Pekka Paalanenb5026542014-11-12 15:09:24 +02002986 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002987 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002988}
2989
Kristian Høgsberg47792412014-05-04 13:47:06 -07002990static void
2991shell_interface_set_fullscreen(struct shell_surface *shsurf,
2992 uint32_t method,
2993 uint32_t framerate,
2994 struct weston_output *output)
2995{
2996 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002997 shsurf->next_state.fullscreen = true;
2998 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002999
3000 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003001}
3002
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003003static struct weston_output *
3004get_focused_output(struct weston_compositor *compositor)
3005{
3006 struct weston_seat *seat;
3007 struct weston_output *output = NULL;
3008
3009 wl_list_for_each(seat, &compositor->seat_list, link) {
3010 /* Priority has touch focus, then pointer and
3011 * then keyboard focus. We should probably have
3012 * three for loops and check frist for touch,
3013 * then for pointer, etc. but unless somebody has some
3014 * objections, I think this is sufficient. */
3015 if (seat->touch && seat->touch->focus)
3016 output = seat->touch->focus->output;
3017 else if (seat->pointer && seat->pointer->focus)
3018 output = seat->pointer->focus->output;
3019 else if (seat->keyboard && seat->keyboard->focus)
3020 output = seat->keyboard->focus->output;
3021
3022 if (output)
3023 break;
3024 }
3025
3026 return output;
3027}
3028
3029static void
3030shell_interface_set_maximized(struct shell_surface *shsurf)
3031{
3032 struct weston_output *output;
3033
3034 surface_clear_next_states(shsurf);
3035 shsurf->next_state.maximized = true;
3036 shsurf->state_changed = true;
3037 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3038
3039 if (!weston_surface_is_mapped(shsurf->surface))
3040 output = get_focused_output(shsurf->surface->compositor);
3041 else
3042 output = shsurf->surface->output;
3043
3044 shell_surface_set_output(shsurf, output);
3045 send_configure_for_surface(shsurf);
3046}
3047
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003048static int
3049shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3050{
3051 return surface_move(shsurf, ws, 1);
3052}
3053
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003054static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003055
3056static void
3057destroy_shell_seat(struct wl_listener *listener, void *data)
3058{
3059 struct shell_seat *shseat =
3060 container_of(listener,
3061 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003062 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003063
3064 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003065 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003066 shseat->popup_grab.client = NULL;
3067
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003068 wl_list_for_each_safe(shsurf, next,
3069 &shseat->popup_grab.surfaces_list,
3070 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003071 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003072 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003073 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003074 }
3075
3076 wl_list_remove(&shseat->seat_destroy_listener.link);
3077 free(shseat);
3078}
3079
Jason Ekstrand024177c2014-04-21 19:42:58 -05003080static void
3081shell_seat_caps_changed(struct wl_listener *listener, void *data)
3082{
3083 struct shell_seat *seat;
3084
3085 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3086
3087 if (seat->seat->keyboard &&
3088 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3089 wl_signal_add(&seat->seat->keyboard->focus_signal,
3090 &seat->keyboard_focus_listener);
3091 } else if (!seat->seat->keyboard) {
3092 wl_list_init(&seat->keyboard_focus_listener.link);
3093 }
3094
3095 if (seat->seat->pointer &&
3096 wl_list_empty(&seat->pointer_focus_listener.link)) {
3097 wl_signal_add(&seat->seat->pointer->focus_signal,
3098 &seat->pointer_focus_listener);
3099 } else if (!seat->seat->pointer) {
3100 wl_list_init(&seat->pointer_focus_listener.link);
3101 }
3102}
3103
Giulio Camuffo5085a752013-03-25 21:42:45 +01003104static struct shell_seat *
3105create_shell_seat(struct weston_seat *seat)
3106{
3107 struct shell_seat *shseat;
3108
3109 shseat = calloc(1, sizeof *shseat);
3110 if (!shseat) {
3111 weston_log("no memory to allocate shell seat\n");
3112 return NULL;
3113 }
3114
3115 shseat->seat = seat;
3116 wl_list_init(&shseat->popup_grab.surfaces_list);
3117
3118 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3119 wl_signal_add(&seat->destroy_signal,
3120 &shseat->seat_destroy_listener);
3121
Jason Ekstrand024177c2014-04-21 19:42:58 -05003122 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3123 wl_list_init(&shseat->keyboard_focus_listener.link);
3124
3125 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3126 wl_list_init(&shseat->pointer_focus_listener.link);
3127
3128 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3129 wl_signal_add(&seat->updated_caps_signal,
3130 &shseat->caps_changed_listener);
3131 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3132
Giulio Camuffo5085a752013-03-25 21:42:45 +01003133 return shseat;
3134}
3135
3136static struct shell_seat *
3137get_shell_seat(struct weston_seat *seat)
3138{
3139 struct wl_listener *listener;
3140
3141 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003142 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003143
3144 return container_of(listener,
3145 struct shell_seat, seat_destroy_listener);
3146}
3147
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003148static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003149popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003150{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003151 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003152 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003153 struct shell_seat *shseat =
3154 container_of(grab, struct shell_seat, popup_grab.grab);
3155 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003156 wl_fixed_t sx, sy;
3157
Jason Ekstranda7af7042013-10-12 22:38:11 -05003158 view = weston_compositor_pick_view(pointer->seat->compositor,
3159 pointer->x, pointer->y,
3160 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003161
Jason Ekstranda7af7042013-10-12 22:38:11 -05003162 if (view && view->surface->resource &&
3163 wl_resource_get_client(view->surface->resource) == client) {
3164 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003165 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003166 weston_pointer_set_focus(pointer, NULL,
3167 wl_fixed_from_int(0),
3168 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003169 }
3170}
3171
3172static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003173popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3174 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003175{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003176 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003177 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003178 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003179
Jonas Ådahl61917c82014-08-11 22:44:02 +02003180 if (pointer->focus) {
3181 weston_view_from_global_fixed(pointer->focus, x, y,
3182 &pointer->sx, &pointer->sy);
3183 }
3184
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003185 weston_pointer_move(pointer, x, y);
3186
Neil Roberts96d790e2013-09-19 17:32:00 +01003187 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003188 weston_view_from_global_fixed(pointer->focus,
3189 pointer->x, pointer->y,
3190 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003191 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003192 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003193}
3194
3195static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003196popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003197 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003198{
3199 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003200 struct shell_seat *shseat =
3201 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003202 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003203 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003204 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003205 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003206
Neil Roberts96d790e2013-09-19 17:32:00 +01003207 resource_list = &grab->pointer->focus_resource_list;
3208 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003209 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003210 wl_resource_for_each(resource, resource_list) {
3211 wl_pointer_send_button(resource, serial,
3212 time, button, state);
3213 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003214 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003215 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003216 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003217 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003218 }
3219
Daniel Stone4dbadb12012-05-30 16:31:51 +01003220 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003221 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003222}
3223
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003224static void
3225popup_grab_cancel(struct weston_pointer_grab *grab)
3226{
3227 popup_grab_end(grab->pointer);
3228}
3229
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003230static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003231 popup_grab_focus,
3232 popup_grab_motion,
3233 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003234 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003235};
3236
3237static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003238touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3239 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3240{
3241 struct wl_resource *resource;
3242 struct shell_seat *shseat =
3243 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3244 struct wl_display *display = shseat->seat->compositor->wl_display;
3245 uint32_t serial;
3246 struct wl_list *resource_list;
3247
3248 resource_list = &grab->touch->focus_resource_list;
3249 if (!wl_list_empty(resource_list)) {
3250 serial = wl_display_get_serial(display);
3251 wl_resource_for_each(resource, resource_list) {
3252 wl_touch_send_down(resource, serial, time,
3253 grab->touch->focus->surface->resource,
3254 touch_id, sx, sy);
3255 }
3256 }
3257}
3258
3259static void
3260touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3261{
3262 struct wl_resource *resource;
3263 struct shell_seat *shseat =
3264 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3265 struct wl_display *display = shseat->seat->compositor->wl_display;
3266 uint32_t serial;
3267 struct wl_list *resource_list;
3268
3269 resource_list = &grab->touch->focus_resource_list;
3270 if (!wl_list_empty(resource_list)) {
3271 serial = wl_display_get_serial(display);
3272 wl_resource_for_each(resource, resource_list) {
3273 wl_touch_send_up(resource, serial, time, touch_id);
3274 }
3275 }
3276}
3277
3278static void
3279touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3280 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3281{
3282 struct wl_resource *resource;
3283 struct wl_list *resource_list;
3284
3285 resource_list = &grab->touch->focus_resource_list;
3286 if (!wl_list_empty(resource_list)) {
3287 wl_resource_for_each(resource, resource_list) {
3288 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3289 }
3290 }
3291}
3292
3293static void
3294touch_popup_grab_frame(struct weston_touch_grab *grab)
3295{
3296}
3297
3298static void
3299touch_popup_grab_cancel(struct weston_touch_grab *grab)
3300{
3301 touch_popup_grab_end(grab->touch);
3302}
3303
3304static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3305 touch_popup_grab_down,
3306 touch_popup_grab_up,
3307 touch_popup_grab_motion,
3308 touch_popup_grab_frame,
3309 touch_popup_grab_cancel,
3310};
3311
3312static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003313shell_surface_send_popup_done(struct shell_surface *shsurf)
3314{
3315 if (shell_surface_is_wl_shell_surface(shsurf))
3316 wl_shell_surface_send_popup_done(shsurf->resource);
3317 else if (shell_surface_is_xdg_popup(shsurf))
3318 xdg_popup_send_popup_done(shsurf->resource,
3319 shsurf->popup.serial);
3320}
3321
3322static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003323popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003324{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003325 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003326 struct shell_seat *shseat =
3327 container_of(grab, struct shell_seat, popup_grab.grab);
3328 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003329 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003330
3331 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003332 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003333 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003334 shseat->popup_grab.grab.interface = NULL;
3335 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003336 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003337 wl_list_for_each_safe(shsurf, next,
3338 &shseat->popup_grab.surfaces_list,
3339 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003340 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003341 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003342 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003343 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003344 wl_list_init(&shseat->popup_grab.surfaces_list);
3345 }
3346}
3347
3348static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003349touch_popup_grab_end(struct weston_touch *touch)
3350{
3351 struct weston_touch_grab *grab = touch->grab;
3352 struct shell_seat *shseat =
3353 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3354 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003355 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003356
3357 if (touch->grab->interface == &touch_popup_grab_interface) {
3358 weston_touch_end_grab(grab->touch);
3359 shseat->popup_grab.client = NULL;
3360 shseat->popup_grab.touch_grab.interface = NULL;
3361 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3362 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003363 wl_list_for_each_safe(shsurf, next,
3364 &shseat->popup_grab.surfaces_list,
3365 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003366 shell_surface_send_popup_done(shsurf);
3367 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003368 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003369 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003370 wl_list_init(&shseat->popup_grab.surfaces_list);
3371 }
3372}
3373
3374static void
3375add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat, int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003376{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003377 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003378
3379 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003380 shseat->popup_grab.type = type;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003381 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003382
3383 if (type == POINTER) {
3384 shseat->popup_grab.grab.interface = &popup_grab_interface;
3385 /* We must make sure here that this popup was opened after
3386 * a mouse press, and not just by moving around with other
3387 * popups already open. */
3388 if (shseat->seat->pointer->button_count > 0)
3389 shseat->popup_grab.initial_up = 0;
3390 } else if (type == TOUCH) {
3391 shseat->popup_grab.touch_grab.interface = &touch_popup_grab_interface;
3392 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003393
Rob Bradforddfe31052013-06-26 19:49:11 +01003394 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003395
3396 if (type == POINTER)
3397 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
3398 else if (type == TOUCH)
3399 weston_touch_start_grab(seat->touch, &shseat->popup_grab.touch_grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003400 } else {
3401 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003402 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003403}
3404
3405static void
3406remove_popup_grab(struct shell_surface *shsurf)
3407{
3408 struct shell_seat *shseat = shsurf->popup.shseat;
3409
3410 wl_list_remove(&shsurf->popup.grab_link);
3411 wl_list_init(&shsurf->popup.grab_link);
3412 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003413 if (shseat->popup_grab.type == POINTER) {
3414 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3415 shseat->popup_grab.grab.interface = NULL;
3416 } else if (shseat->popup_grab.type == TOUCH) {
3417 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3418 shseat->popup_grab.touch_grab.interface = NULL;
3419 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003420 }
3421}
3422
3423static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003424shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003425{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003426 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003427 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003428
Jason Ekstranda7af7042013-10-12 22:38:11 -05003429 shsurf->surface->output = parent_view->output;
3430 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003431
Jason Ekstranda7af7042013-10-12 22:38:11 -05003432 weston_view_set_transform_parent(shsurf->view, parent_view);
3433 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3434 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003435
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003436 if (shseat->seat->pointer &&
3437 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003438 add_popup_grab(shsurf, shseat, POINTER);
3439 } else if (shseat->seat->touch &&
3440 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
3441 add_popup_grab(shsurf, shseat, TOUCH);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003442 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003443 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003444 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003445 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003446}
3447
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003448static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003449 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003450 shell_surface_move,
3451 shell_surface_resize,
3452 shell_surface_set_toplevel,
3453 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003454 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003455 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003456 shell_surface_set_maximized,
3457 shell_surface_set_title,
3458 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003459};
3460
3461static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003462destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003463{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003464 struct shell_surface *child, *next;
3465
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003466 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3467
Giulio Camuffo5085a752013-03-25 21:42:45 +01003468 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3469 remove_popup_grab(shsurf);
3470 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003471
Alex Wubd3354b2012-04-17 17:20:49 +08003472 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003473 shell_surface_is_top_fullscreen(shsurf))
3474 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003475
Jason Ekstranda7af7042013-10-12 22:38:11 -05003476 if (shsurf->fullscreen.black_view)
3477 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003478
Alex Wubd3354b2012-04-17 17:20:49 +08003479 /* As destroy_resource() use wl_list_for_each_safe(),
3480 * we can always remove the listener.
3481 */
3482 wl_list_remove(&shsurf->surface_destroy_listener.link);
3483 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003484 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003485 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003486
Jason Ekstranda7af7042013-10-12 22:38:11 -05003487 weston_view_destroy(shsurf->view);
3488
Philip Withnall648a4dd2013-11-25 18:01:44 +00003489 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003490 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3491 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003492
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003493 wl_list_remove(&shsurf->link);
3494 free(shsurf);
3495}
3496
3497static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003498shell_destroy_shell_surface(struct wl_resource *resource)
3499{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003500 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003501
Bryan Caina46b9462014-04-04 17:41:24 -05003502 if (!wl_list_empty(&shsurf->popup.grab_link))
3503 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003504 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003505}
3506
3507static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003508shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003509{
3510 struct shell_surface *shsurf = container_of(listener,
3511 struct shell_surface,
3512 surface_destroy_listener);
3513
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003514 if (shsurf->resource)
3515 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003516
3517 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003518}
3519
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003520static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003521fade_out_done(struct weston_view_animation *animation, void *data)
3522{
3523 struct shell_surface *shsurf = data;
3524
3525 weston_surface_destroy(shsurf->surface);
3526}
3527
3528static void
3529handle_resource_destroy(struct wl_listener *listener, void *data)
3530{
3531 struct shell_surface *shsurf =
3532 container_of(listener, struct shell_surface,
3533 resource_destroy_listener);
3534
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003535 if (!weston_surface_is_mapped(shsurf->surface))
3536 return;
3537
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003538 shsurf->surface->ref_count++;
3539
3540 pixman_region32_fini(&shsurf->surface->pending.input);
3541 pixman_region32_init(&shsurf->surface->pending.input);
3542 pixman_region32_fini(&shsurf->surface->input);
3543 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003544 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3545 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3546 fade_out_done, shsurf);
3547 } else {
3548 weston_surface_destroy(shsurf->surface);
3549 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003550}
3551
3552static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003553shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003554
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003555struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003556get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003557{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003558 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003559 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003560 else
3561 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003562}
3563
Rafael Antognollie2a34552013-12-03 15:35:45 -02003564static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003565create_common_surface(struct shell_client *owner, void *shell,
3566 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003567 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003568{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003569 struct shell_surface *shsurf;
3570
Pekka Paalanen50b67472014-10-01 15:02:41 +03003571 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003572
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003573 shsurf = calloc(1, sizeof *shsurf);
3574 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003575 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003576 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003577 }
3578
Jason Ekstranda7af7042013-10-12 22:38:11 -05003579 shsurf->view = weston_view_create(surface);
3580 if (!shsurf->view) {
3581 weston_log("no memory to allocate shell surface\n");
3582 free(shsurf);
3583 return NULL;
3584 }
3585
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003586 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003587 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003588 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003589
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003590 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3591 wl_resource_add_destroy_listener(surface->resource,
3592 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003593 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003594
Tiago Vignattibc052c92012-04-19 16:18:18 +03003595 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003596 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003597 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003598 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003599 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003600 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003601 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3602 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003603 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003604 wl_list_init(&shsurf->fullscreen.transform.link);
3605
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003606 shsurf->output = get_default_output(shsurf->shell->compositor);
3607
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003608 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003609 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003610 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003611 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003612
3613 /* init link so its safe to always remove it in destroy_shell_surface */
3614 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003615 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003616
Pekka Paalanen460099f2012-01-20 16:48:25 +02003617 /* empty when not in use */
3618 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003619 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003620
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003621 wl_list_init(&shsurf->workspace_transform.link);
3622
Philip Withnall648a4dd2013-11-25 18:01:44 +00003623 wl_list_init(&shsurf->children_link);
3624 wl_list_init(&shsurf->children_list);
3625 shsurf->parent = NULL;
3626
Pekka Paalanenb5026542014-11-12 15:09:24 +02003627 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003628
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003629 shsurf->client = client;
3630
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003631 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003632}
3633
Rafael Antognollie2a34552013-12-03 15:35:45 -02003634static struct shell_surface *
3635create_shell_surface(void *shell, struct weston_surface *surface,
3636 const struct weston_shell_client *client)
3637{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003638 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003639}
3640
Jason Ekstranda7af7042013-10-12 22:38:11 -05003641static struct weston_view *
3642get_primary_view(void *shell, struct shell_surface *shsurf)
3643{
3644 return shsurf->view;
3645}
3646
Tiago Vignattibc052c92012-04-19 16:18:18 +03003647static void
3648shell_get_shell_surface(struct wl_client *client,
3649 struct wl_resource *resource,
3650 uint32_t id,
3651 struct wl_resource *surface_resource)
3652{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003653 struct weston_surface *surface =
3654 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003655 struct shell_client *sc = wl_resource_get_user_data(resource);
3656 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003657 struct shell_surface *shsurf;
3658
Pekka Paalanen50b67472014-10-01 15:02:41 +03003659 if (weston_surface_set_role(surface, "wl_shell_surface",
3660 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003661 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003662
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003663 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003664 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003665 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003666 return;
3667 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003668
Jason Ekstranda85118c2013-06-27 20:17:02 -05003669 shsurf->resource =
3670 wl_resource_create(client,
3671 &wl_shell_surface_interface, 1, id);
3672 wl_resource_set_implementation(shsurf->resource,
3673 &shell_surface_implementation,
3674 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003675}
3676
Rafael Antognollie2a34552013-12-03 15:35:45 -02003677static bool
3678shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3679{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003680 /* A shell surface without a resource is created from xwayland
3681 * and is considered a wl_shell surface for now. */
3682
3683 return shsurf->resource == NULL ||
3684 wl_resource_instance_of(shsurf->resource,
3685 &wl_shell_surface_interface,
3686 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003687}
3688
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003689static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003690 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003691};
3692
Rafael Antognollie2a34552013-12-03 15:35:45 -02003693/****************************
3694 * xdg-shell implementation */
3695
3696static void
3697xdg_surface_destroy(struct wl_client *client,
3698 struct wl_resource *resource)
3699{
3700 wl_resource_destroy(resource);
3701}
3702
3703static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003704xdg_surface_set_parent(struct wl_client *client,
3705 struct wl_resource *resource,
3706 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003707{
3708 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3709 struct weston_surface *parent;
3710
3711 if (parent_resource)
3712 parent = wl_resource_get_user_data(parent_resource);
3713 else
3714 parent = NULL;
3715
3716 shell_surface_set_parent(shsurf, parent);
3717}
3718
3719static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003720xdg_surface_set_app_id(struct wl_client *client,
3721 struct wl_resource *resource,
3722 const char *app_id)
3723{
3724 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3725
3726 free(shsurf->class);
3727 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003728 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003729}
3730
3731static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003732xdg_surface_show_window_menu(struct wl_client *client,
3733 struct wl_resource *surface_resource,
3734 struct wl_resource *seat_resource,
3735 uint32_t serial,
3736 int32_t x,
3737 int32_t y)
3738{
3739 /* TODO */
3740}
3741
3742static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003743xdg_surface_set_title(struct wl_client *client,
3744 struct wl_resource *resource, const char *title)
3745{
3746 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3747
3748 set_title(shsurf, title);
3749}
3750
3751static void
3752xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3753 struct wl_resource *seat_resource, uint32_t serial)
3754{
3755 common_surface_move(resource, seat_resource, serial);
3756}
3757
3758static void
3759xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3760 struct wl_resource *seat_resource, uint32_t serial,
3761 uint32_t edges)
3762{
3763 common_surface_resize(resource, seat_resource, serial, edges);
3764}
3765
3766static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003767xdg_surface_ack_configure(struct wl_client *client,
3768 struct wl_resource *resource,
3769 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003770{
3771 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3772
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003773 if (shsurf->state_requested) {
3774 shsurf->next_state = shsurf->requested_state;
3775 shsurf->state_changed = true;
3776 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003777 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003778}
3779
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003780static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003781xdg_surface_set_window_geometry(struct wl_client *client,
3782 struct wl_resource *resource,
3783 int32_t x,
3784 int32_t y,
3785 int32_t width,
3786 int32_t height)
3787{
3788 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3789
3790 set_window_geometry(shsurf, x, y, width, height);
3791}
3792
3793static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003794xdg_surface_set_maximized(struct wl_client *client,
3795 struct wl_resource *resource)
3796{
3797 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003798 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003799
3800 shsurf->state_requested = true;
3801 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003802
3803 if (!weston_surface_is_mapped(shsurf->surface))
3804 output = get_focused_output(shsurf->surface->compositor);
3805 else
3806 output = shsurf->surface->output;
3807
3808 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003809 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003810}
3811
3812static void
3813xdg_surface_unset_maximized(struct wl_client *client,
3814 struct wl_resource *resource)
3815{
3816 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3817
3818 shsurf->state_requested = true;
3819 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003820 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003821}
3822
3823static void
3824xdg_surface_set_fullscreen(struct wl_client *client,
3825 struct wl_resource *resource,
3826 struct wl_resource *output_resource)
3827{
3828 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3829 struct weston_output *output;
3830
3831 shsurf->state_requested = true;
3832 shsurf->requested_state.fullscreen = true;
3833
3834 if (output_resource)
3835 output = wl_resource_get_user_data(output_resource);
3836 else
3837 output = NULL;
3838
Marek Chalupa052917a2014-09-02 11:35:12 +02003839 /* handle clients launching in fullscreen */
3840 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3841 /* Set the output to the one that has focus currently. */
3842 assert(shsurf->surface);
3843 output = get_focused_output(shsurf->surface->compositor);
3844 }
3845
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003846 shell_surface_set_output(shsurf, output);
3847 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003848
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003849 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003850}
3851
3852static void
3853xdg_surface_unset_fullscreen(struct wl_client *client,
3854 struct wl_resource *resource)
3855{
3856 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3857
3858 shsurf->state_requested = true;
3859 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003860 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003861}
3862
3863static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003864xdg_surface_set_minimized(struct wl_client *client,
3865 struct wl_resource *resource)
3866{
3867 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3868
3869 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3870 return;
3871
3872 /* apply compositor's own minimization logic (hide) */
3873 set_minimized(shsurf->surface, 1);
3874}
3875
Rafael Antognollie2a34552013-12-03 15:35:45 -02003876static const struct xdg_surface_interface xdg_surface_implementation = {
3877 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003878 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003879 xdg_surface_set_title,
3880 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003881 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003882 xdg_surface_move,
3883 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003884 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003885 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003886 xdg_surface_set_maximized,
3887 xdg_surface_unset_maximized,
3888 xdg_surface_set_fullscreen,
3889 xdg_surface_unset_fullscreen,
3890 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003891};
3892
3893static void
3894xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003895 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003896{
3897 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003898 uint32_t *s;
3899 struct wl_array states;
3900 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003901
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003902 assert(shsurf);
3903
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003904 if (!shsurf->resource)
3905 return;
3906
3907 wl_array_init(&states);
3908 if (shsurf->requested_state.fullscreen) {
3909 s = wl_array_add(&states, sizeof *s);
3910 *s = XDG_SURFACE_STATE_FULLSCREEN;
3911 } else if (shsurf->requested_state.maximized) {
3912 s = wl_array_add(&states, sizeof *s);
3913 *s = XDG_SURFACE_STATE_MAXIMIZED;
3914 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003915 if (shsurf->resize_edges != 0) {
3916 s = wl_array_add(&states, sizeof *s);
3917 *s = XDG_SURFACE_STATE_RESIZING;
3918 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003919 if (shsurf->focus_count > 0) {
3920 s = wl_array_add(&states, sizeof *s);
3921 *s = XDG_SURFACE_STATE_ACTIVATED;
3922 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003923
3924 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3925 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3926
3927 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003928}
3929
3930static const struct weston_shell_client xdg_client = {
3931 xdg_send_configure
3932};
3933
3934static void
3935xdg_use_unstable_version(struct wl_client *client,
3936 struct wl_resource *resource,
3937 int32_t version)
3938{
3939 if (version > 1) {
3940 wl_resource_post_error(resource,
3941 1,
3942 "xdg-shell:: version not implemented yet.");
3943 return;
3944 }
3945}
3946
3947static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003948create_xdg_surface(struct shell_client *owner, void *shell,
3949 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003950 const struct weston_shell_client *client)
3951{
3952 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003953
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003954 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03003955 if (!shsurf)
3956 return NULL;
3957
Pekka Paalanenb5026542014-11-12 15:09:24 +02003958 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003959
3960 return shsurf;
3961}
3962
3963static void
3964xdg_get_xdg_surface(struct wl_client *client,
3965 struct wl_resource *resource,
3966 uint32_t id,
3967 struct wl_resource *surface_resource)
3968{
3969 struct weston_surface *surface =
3970 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003971 struct shell_client *sc = wl_resource_get_user_data(resource);
3972 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003973 struct shell_surface *shsurf;
3974
Jonas Ådahlbf48e212015-02-06 10:15:28 +08003975 shsurf = get_shell_surface(surface);
3976 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
3977 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
3978 "This wl_surface is already an "
3979 "xdg_surface");
3980 return;
3981 }
3982
Pekka Paalanen50b67472014-10-01 15:02:41 +03003983 if (weston_surface_set_role(surface, "xdg_surface",
3984 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003985 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003986
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003987 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003988 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003989 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003990 return;
3991 }
3992
3993 shsurf->resource =
3994 wl_resource_create(client,
3995 &xdg_surface_interface, 1, id);
3996 wl_resource_set_implementation(shsurf->resource,
3997 &xdg_surface_implementation,
3998 shsurf, shell_destroy_shell_surface);
3999}
4000
4001static bool
4002shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4003{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004004 return shsurf->resource &&
4005 wl_resource_instance_of(shsurf->resource,
4006 &xdg_surface_interface,
4007 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004008}
4009
4010/* xdg-popup implementation */
4011
4012static void
4013xdg_popup_destroy(struct wl_client *client,
4014 struct wl_resource *resource)
4015{
4016 wl_resource_destroy(resource);
4017}
4018
Rafael Antognollie2a34552013-12-03 15:35:45 -02004019static const struct xdg_popup_interface xdg_popup_implementation = {
4020 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004021};
4022
4023static void
4024xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004025 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004026{
4027}
4028
4029static const struct weston_shell_client xdg_popup_client = {
4030 xdg_popup_send_configure
4031};
4032
4033static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004034create_xdg_popup(struct shell_client *owner, void *shell,
4035 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004036 const struct weston_shell_client *client,
4037 struct weston_surface *parent,
4038 struct shell_seat *seat,
4039 uint32_t serial,
4040 int32_t x, int32_t y)
4041{
4042 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004043
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004044 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004045 if (!shsurf)
4046 return NULL;
4047
Pekka Paalanenb5026542014-11-12 15:09:24 +02004048 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004049 shsurf->popup.shseat = seat;
4050 shsurf->popup.serial = serial;
4051 shsurf->popup.x = x;
4052 shsurf->popup.y = y;
4053 shell_surface_set_parent(shsurf, parent);
4054
4055 return shsurf;
4056}
4057
4058static void
4059xdg_get_xdg_popup(struct wl_client *client,
4060 struct wl_resource *resource,
4061 uint32_t id,
4062 struct wl_resource *surface_resource,
4063 struct wl_resource *parent_resource,
4064 struct wl_resource *seat_resource,
4065 uint32_t serial,
4066 int32_t x, int32_t y, uint32_t flags)
4067{
4068 struct weston_surface *surface =
4069 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004070 struct shell_client *sc = wl_resource_get_user_data(resource);
4071 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004072 struct shell_surface *shsurf;
4073 struct weston_surface *parent;
4074 struct shell_seat *seat;
4075
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004076 shsurf = get_shell_surface(surface);
4077 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4078 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4079 "This wl_surface is already an "
4080 "xdg_popup");
4081 return;
4082 }
4083
Pekka Paalanen50b67472014-10-01 15:02:41 +03004084 if (weston_surface_set_role(surface, "xdg_popup",
4085 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004086 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004087
4088 if (!parent_resource) {
4089 wl_resource_post_error(surface_resource,
4090 WL_DISPLAY_ERROR_INVALID_OBJECT,
4091 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004092 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004093 }
4094
4095 parent = wl_resource_get_user_data(parent_resource);
4096 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4097
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004098 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004099 parent, seat, serial, x, y);
4100 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004101 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004102 return;
4103 }
4104
4105 shsurf->resource =
4106 wl_resource_create(client,
4107 &xdg_popup_interface, 1, id);
4108 wl_resource_set_implementation(shsurf->resource,
4109 &xdg_popup_implementation,
4110 shsurf, shell_destroy_shell_surface);
4111}
4112
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004113static void
4114xdg_pong(struct wl_client *client,
4115 struct wl_resource *resource, uint32_t serial)
4116{
4117 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004118
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004119 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004120}
4121
Rafael Antognollie2a34552013-12-03 15:35:45 -02004122static bool
4123shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4124{
4125 return wl_resource_instance_of(shsurf->resource,
4126 &xdg_popup_interface,
4127 &xdg_popup_implementation);
4128}
4129
4130static const struct xdg_shell_interface xdg_implementation = {
4131 xdg_use_unstable_version,
4132 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004133 xdg_get_xdg_popup,
4134 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004135};
4136
4137static int
4138xdg_shell_unversioned_dispatch(const void *implementation,
4139 void *_target, uint32_t opcode,
4140 const struct wl_message *message,
4141 union wl_argument *args)
4142{
4143 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004144 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004145
4146 if (opcode != 0) {
4147 wl_resource_post_error(resource,
4148 WL_DISPLAY_ERROR_INVALID_OBJECT,
4149 "must call use_unstable_version first");
4150 return 0;
4151 }
4152
Pekka Paalanen71182ae2014-08-21 17:47:20 +03004153#define XDG_SERVER_VERSION 4
Rafael Antognollie2a34552013-12-03 15:35:45 -02004154
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004155 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4156 "shell implementation doesn't match protocol version");
4157
Rafael Antognollie2a34552013-12-03 15:35:45 -02004158 if (args[0].i != XDG_SERVER_VERSION) {
4159 wl_resource_post_error(resource,
4160 WL_DISPLAY_ERROR_INVALID_OBJECT,
4161 "incompatible version, server is %d "
4162 "client wants %d",
4163 XDG_SERVER_VERSION, args[0].i);
4164 return 0;
4165 }
4166
4167 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004168 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004169
4170 return 1;
4171}
4172
4173/* end of xdg-shell implementation */
4174/***********************************/
4175
Kristian Høgsberg07937562011-04-12 17:25:42 -04004176static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004177shell_fade(struct desktop_shell *shell, enum fade_type type);
4178
4179static int
4180screensaver_timeout(void *data)
4181{
4182 struct desktop_shell *shell = data;
4183
4184 shell_fade(shell, FADE_OUT);
4185
4186 return 1;
4187}
4188
4189static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004190handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02004191{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004192 struct desktop_shell *shell =
4193 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004194
Pekka Paalanen18027e52011-12-02 16:31:49 +02004195 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004196
4197 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02004198 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02004199}
4200
4201static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004202launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004203{
4204 if (shell->screensaver.binding)
4205 return;
4206
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004207 if (!shell->screensaver.path) {
4208 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004209 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004210 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004211
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004212 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02004213 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004214 return;
4215 }
4216
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004217 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004218 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004219 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004220 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004221}
4222
4223static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004224terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004225{
Pekka Paalanen18027e52011-12-02 16:31:49 +02004226 if (shell->screensaver.process.pid == 0)
4227 return;
4228
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03004229 /* Disarm the screensaver timer, otherwise it may fire when the
4230 * compositor is not in the idle state. In that case, the screen will
4231 * be locked, but the wake_signal won't fire on user input, making the
4232 * system unresponsive. */
4233 wl_event_source_timer_update(shell->screensaver.timer, 0);
4234
Pekka Paalanen18027e52011-12-02 16:31:49 +02004235 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004236}
4237
4238static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004239configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004240{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004241 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004242
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004243 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004244 if (v->output == ev->output && v != ev) {
4245 weston_view_unmap(v);
4246 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004247 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004248 }
4249 }
4250
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004251 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004252
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004253 if (wl_list_empty(&ev->layer_link.link)) {
4254 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004255 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004256 }
4257}
4258
Pekka Paalanen8274d902014-08-06 19:36:51 +03004259static int
4260background_get_label(struct weston_surface *surface, char *buf, size_t len)
4261{
4262 return snprintf(buf, len, "background for output %s",
4263 surface->output->name);
4264}
4265
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004266static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004267background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004268{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004269 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004270 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004271
Jason Ekstranda7af7042013-10-12 22:38:11 -05004272 view = container_of(es->views.next, struct weston_view, surface_link);
4273
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004274 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004275}
4276
4277static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004278desktop_shell_set_background(struct wl_client *client,
4279 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004280 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004281 struct wl_resource *surface_resource)
4282{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004283 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004284 struct weston_surface *surface =
4285 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004286 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004287
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004288 if (surface->configure) {
4289 wl_resource_post_error(surface_resource,
4290 WL_DISPLAY_ERROR_INVALID_OBJECT,
4291 "surface role already assigned");
4292 return;
4293 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004294
Jason Ekstranda7af7042013-10-12 22:38:11 -05004295 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4296 weston_view_destroy(view);
4297 view = weston_view_create(surface);
4298
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004299 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004300 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004301 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004302 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004303 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004304 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004305 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004306 surface->output->width,
4307 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004308}
4309
Pekka Paalanen8274d902014-08-06 19:36:51 +03004310static int
4311panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4312{
4313 return snprintf(buf, len, "panel for output %s",
4314 surface->output->name);
4315}
4316
Kristian Høgsberg75840622011-09-06 13:48:16 -04004317static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004318panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004319{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004320 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004321 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004322
Jason Ekstranda7af7042013-10-12 22:38:11 -05004323 view = container_of(es->views.next, struct weston_view, surface_link);
4324
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004325 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004326}
4327
4328static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004329desktop_shell_set_panel(struct wl_client *client,
4330 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004331 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004332 struct wl_resource *surface_resource)
4333{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004334 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004335 struct weston_surface *surface =
4336 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004337 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004338
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004339 if (surface->configure) {
4340 wl_resource_post_error(surface_resource,
4341 WL_DISPLAY_ERROR_INVALID_OBJECT,
4342 "surface role already assigned");
4343 return;
4344 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004345
Jason Ekstranda7af7042013-10-12 22:38:11 -05004346 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4347 weston_view_destroy(view);
4348 view = weston_view_create(surface);
4349
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004350 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004351 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004352 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004353 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004354 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004355 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004356 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004357 surface->output->width,
4358 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004359}
4360
Pekka Paalanen8274d902014-08-06 19:36:51 +03004361static int
4362lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4363{
4364 return snprintf(buf, len, "lock window");
4365}
4366
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004367static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004368lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004369{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004370 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004371 struct weston_view *view;
4372
4373 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004374
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004375 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004376 return;
4377
Jason Ekstranda7af7042013-10-12 22:38:11 -05004378 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004379
4380 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004381 weston_layer_entry_insert(&shell->lock_layer.view_list,
4382 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004383 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004384 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004385 }
4386}
4387
4388static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004389handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004390{
Tiago Vignattibe143262012-04-16 17:31:41 +03004391 struct desktop_shell *shell =
4392 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004393
Martin Minarik6d118362012-06-07 18:01:59 +02004394 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004395 shell->lock_surface = NULL;
4396}
4397
4398static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004399desktop_shell_set_lock_surface(struct wl_client *client,
4400 struct wl_resource *resource,
4401 struct wl_resource *surface_resource)
4402{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004403 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004404 struct weston_surface *surface =
4405 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004406
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004407 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004408
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004409 if (!shell->locked)
4410 return;
4411
Pekka Paalanen98262232011-12-01 10:42:22 +02004412 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004413
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004414 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004415 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004416 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004417
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004418 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004419 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004420 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004421 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004422}
4423
4424static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004425resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004426{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004427 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004428
Pekka Paalanen77346a62011-11-30 16:26:35 +02004429 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004430
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004431 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004432 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004433 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004434 &shell->input_panel_layer.link);
4435 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004436 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004437 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004438 wl_list_insert(&shell->compositor->cursor_layer.link,
4439 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004440 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004441 wl_list_insert(&shell->fullscreen_layer.link,
4442 &shell->panel_layer.link);
4443 wl_list_insert(&shell->panel_layer.link,
4444 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004445
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004446 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004447
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004448 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004449 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004450 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004451}
4452
4453static void
4454desktop_shell_unlock(struct wl_client *client,
4455 struct wl_resource *resource)
4456{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004457 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004458
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004459 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004460
4461 if (shell->locked)
4462 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004463}
4464
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004465static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004466desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004467 struct wl_resource *resource,
4468 struct wl_resource *surface_resource)
4469{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004470 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004471
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004472 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004473 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004474}
4475
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004476static void
4477desktop_shell_desktop_ready(struct wl_client *client,
4478 struct wl_resource *resource)
4479{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004480 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004481
4482 shell_fade_startup(shell);
4483}
4484
Jonny Lamb765760d2014-08-20 15:53:19 +02004485static void
4486desktop_shell_set_panel_position(struct wl_client *client,
4487 struct wl_resource *resource,
4488 uint32_t position)
4489{
4490 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4491
4492 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4493 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4494 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4495 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4496 wl_resource_post_error(resource,
4497 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4498 "bad position argument");
4499 return;
4500 }
4501
4502 shell->panel_position = position;
4503}
4504
Kristian Høgsberg75840622011-09-06 13:48:16 -04004505static const struct desktop_shell_interface desktop_shell_implementation = {
4506 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004507 desktop_shell_set_panel,
4508 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004509 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004510 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004511 desktop_shell_desktop_ready,
4512 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004513};
4514
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004515static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004516get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004517{
4518 struct shell_surface *shsurf;
4519
4520 shsurf = get_shell_surface(surface);
4521 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004522 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004523 return shsurf->type;
4524}
4525
Kristian Høgsberg75840622011-09-06 13:48:16 -04004526static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004527move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004528{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004529 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004530 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004531 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004532
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004533 if (seat->pointer->focus == NULL)
4534 return;
4535
4536 focus = seat->pointer->focus->surface;
4537
Pekka Paalanen01388e22013-04-25 13:57:44 +03004538 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004539 if (surface == NULL)
4540 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004541
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004542 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004543 if (shsurf == NULL || shsurf->state.fullscreen ||
4544 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004545 return;
4546
Derek Foreman8aeeac82015-01-30 13:24:36 -06004547 surface_move(shsurf, seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004548}
4549
4550static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004551maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4552{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004553 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004554 struct weston_surface *surface;
4555 struct shell_surface *shsurf;
4556
4557 surface = weston_surface_get_main_surface(focus);
4558 if (surface == NULL)
4559 return;
4560
4561 shsurf = get_shell_surface(surface);
4562 if (shsurf == NULL)
4563 return;
4564
4565 if (!shell_surface_is_xdg_surface(shsurf))
4566 return;
4567
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004568 shsurf->state_requested = true;
4569 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004570 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004571}
4572
4573static void
4574fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4575{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004576 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004577 struct weston_surface *surface;
4578 struct shell_surface *shsurf;
4579
4580 surface = weston_surface_get_main_surface(focus);
4581 if (surface == NULL)
4582 return;
4583
4584 shsurf = get_shell_surface(surface);
4585 if (shsurf == NULL)
4586 return;
4587
4588 if (!shell_surface_is_xdg_surface(shsurf))
4589 return;
4590
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004591 shsurf->state_requested = true;
4592 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004593 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004594 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004595}
4596
4597static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004598touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4599{
Derek Foreman362656b2014-09-04 10:23:05 -05004600 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004601 struct weston_surface *surface;
4602 struct shell_surface *shsurf;
4603
Derek Foreman362656b2014-09-04 10:23:05 -05004604 if (seat->touch->focus == NULL)
4605 return;
4606
4607 focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004608 surface = weston_surface_get_main_surface(focus);
4609 if (surface == NULL)
4610 return;
4611
4612 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004613 if (shsurf == NULL || shsurf->state.fullscreen ||
4614 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004615 return;
4616
Derek Foreman8aeeac82015-01-30 13:24:36 -06004617 surface_touch_move(shsurf, seat);
Neil Robertsaba0f252013-10-03 16:43:05 +01004618}
4619
4620static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004621resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004622{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004623 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004624 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004625 uint32_t edges = 0;
4626 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004627 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004628
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004629 if (seat->pointer->focus == NULL)
4630 return;
4631
4632 focus = seat->pointer->focus->surface;
4633
Pekka Paalanen01388e22013-04-25 13:57:44 +03004634 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004635 if (surface == NULL)
4636 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004637
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004638 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004639 if (shsurf == NULL || shsurf->state.fullscreen ||
4640 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004641 return;
4642
Jason Ekstranda7af7042013-10-12 22:38:11 -05004643 weston_view_from_global(shsurf->view,
4644 wl_fixed_to_int(seat->pointer->grab_x),
4645 wl_fixed_to_int(seat->pointer->grab_y),
4646 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004647
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004648 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004649 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004650 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004651 edges |= 0;
4652 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004653 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004654
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004655 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004656 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004657 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004658 edges |= 0;
4659 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004660 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004661
Derek Foreman8aeeac82015-01-30 13:24:36 -06004662 surface_resize(shsurf, seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004663}
4664
4665static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004666surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004667 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004668{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004669 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004670 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004671 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004672 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004673
Pekka Paalanen01388e22013-04-25 13:57:44 +03004674 /* XXX: broken for windows containing sub-surfaces */
4675 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004676 if (surface == NULL)
4677 return;
4678
4679 shsurf = get_shell_surface(surface);
4680 if (!shsurf)
4681 return;
4682
Jason Ekstranda7af7042013-10-12 22:38:11 -05004683 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004684
Jason Ekstranda7af7042013-10-12 22:38:11 -05004685 if (shsurf->view->alpha > 1.0)
4686 shsurf->view->alpha = 1.0;
4687 if (shsurf->view->alpha < step)
4688 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004689
Jason Ekstranda7af7042013-10-12 22:38:11 -05004690 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004691 weston_surface_damage(surface);
4692}
4693
4694static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004695do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004696 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004697{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004698 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004699 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004700 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004701
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004702 if (!seat->pointer) {
4703 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4704 return;
4705 }
4706
Scott Moreauccbf29d2012-02-22 14:21:41 -07004707 wl_list_for_each(output, &compositor->output_list, link) {
4708 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004709 wl_fixed_to_double(seat->pointer->x),
4710 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004711 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004712 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004713 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004714 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004715 increment = -output->zoom.increment;
4716 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004717 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004718 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004719 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004720 else
4721 increment = 0;
4722
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004723 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004724
Scott Moreaue6603982012-06-11 13:07:51 -06004725 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004726 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004727 else if (output->zoom.level > output->zoom.max_level)
4728 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004729 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004730 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004731 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004732
Scott Moreaue6603982012-06-11 13:07:51 -06004733 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004734
Jason Ekstranda7af7042013-10-12 22:38:11 -05004735 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004736 }
4737 }
4738}
4739
Scott Moreauccbf29d2012-02-22 14:21:41 -07004740static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004741zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004742 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004743{
4744 do_zoom(seat, time, 0, axis, value);
4745}
4746
4747static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004748zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004749 void *data)
4750{
4751 do_zoom(seat, time, key, 0, 0);
4752}
4753
4754static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004755terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004756 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004757{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004758 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004759
Daniel Stone325fc2d2012-05-30 16:31:58 +01004760 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004761}
4762
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004763static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004764rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4765 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004766{
4767 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004768 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004769 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004770 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004771 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004772
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004773 weston_pointer_move(pointer, x, y);
4774
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004775 if (!shsurf)
4776 return;
4777
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004778 cx = 0.5f * shsurf->surface->width;
4779 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004780
Daniel Stone37816df2012-05-16 18:45:18 +01004781 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4782 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004783 r = sqrtf(dx * dx + dy * dy);
4784
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004785 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004786 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004787
4788 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004789 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004790 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004791
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004792 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004793 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004794
4795 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004796 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004797 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004798 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004799 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004800
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004801 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004802 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004803 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004804 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004805 wl_list_init(&shsurf->rotation.transform.link);
4806 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004807 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004808 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004809
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004810 /* We need to adjust the position of the surface
4811 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004812 cposx = shsurf->view->geometry.x + cx;
4813 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004814 dposx = rotate->center.x - cposx;
4815 dposy = rotate->center.y - cposy;
4816 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004817 weston_view_set_position(shsurf->view,
4818 shsurf->view->geometry.x + dposx,
4819 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004820 }
4821
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004822 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004823 * lazily applies the damage due to rotation update.
4824 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004825 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004826}
4827
4828static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004829rotate_grab_button(struct weston_pointer_grab *grab,
4830 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004831{
4832 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004833 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004834 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004835 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004836 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004837
Daniel Stone4dbadb12012-05-30 16:31:51 +01004838 if (pointer->button_count == 0 &&
4839 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004840 if (shsurf)
4841 weston_matrix_multiply(&shsurf->rotation.rotation,
4842 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004843 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004844 free(rotate);
4845 }
4846}
4847
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004848static void
4849rotate_grab_cancel(struct weston_pointer_grab *grab)
4850{
4851 struct rotate_grab *rotate =
4852 container_of(grab, struct rotate_grab, base.grab);
4853
4854 shell_grab_end(&rotate->base);
4855 free(rotate);
4856}
4857
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004858static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004859 noop_grab_focus,
4860 rotate_grab_motion,
4861 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004862 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004863};
4864
4865static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004866surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004867{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004868 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004869 float dx, dy;
4870 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004871
Pekka Paalanen460099f2012-01-20 16:48:25 +02004872 rotate = malloc(sizeof *rotate);
4873 if (!rotate)
4874 return;
4875
Jason Ekstranda7af7042013-10-12 22:38:11 -05004876 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004877 surface->surface->width * 0.5f,
4878 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004879 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004880
Daniel Stone37816df2012-05-16 18:45:18 +01004881 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4882 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004883 r = sqrtf(dx * dx + dy * dy);
4884 if (r > 20.0f) {
4885 struct weston_matrix inverse;
4886
4887 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004888 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004889 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004890
4891 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004892 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004893 } else {
4894 weston_matrix_init(&surface->rotation.rotation);
4895 weston_matrix_init(&rotate->rotation);
4896 }
4897
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004898 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4899 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004900}
4901
4902static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004903rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004904 void *data)
4905{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004906 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004907 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004908 struct shell_surface *surface;
4909
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004910 if (seat->pointer->focus == NULL)
4911 return;
4912
4913 focus = seat->pointer->focus->surface;
4914
Pekka Paalanen01388e22013-04-25 13:57:44 +03004915 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004916 if (base_surface == NULL)
4917 return;
4918
4919 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004920 if (surface == NULL || surface->state.fullscreen ||
4921 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004922 return;
4923
4924 surface_rotate(surface, seat);
4925}
4926
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004927/* Move all fullscreen layers down to the current workspace and hide their
4928 * black views. The surfaces' state is set to both fullscreen and lowered,
4929 * and this is reversed when such a surface is re-configured, see
4930 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4931 *
4932 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004933 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004934void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004935lower_fullscreen_layer(struct desktop_shell *shell)
4936{
4937 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004938 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004939
4940 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004941 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004942 &shell->fullscreen_layer.view_list.link,
4943 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004944 struct shell_surface *shsurf = get_shell_surface(view->surface);
4945
4946 if (!shsurf)
4947 continue;
4948
4949 /* We can have a non-fullscreen popup for a fullscreen surface
4950 * in the fullscreen layer. */
4951 if (shsurf->state.fullscreen) {
4952 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004953 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4954 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004955 weston_view_damage_below(shsurf->fullscreen.black_view);
4956
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004957 }
4958
4959 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004960 weston_layer_entry_remove(&view->layer_link);
4961 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004962 weston_view_damage_below(view);
4963 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004964
4965 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004966 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004967}
4968
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004969void
Tiago Vignattibe143262012-04-16 17:31:41 +03004970activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004971 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004972{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004973 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004974 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004975 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004976 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004977 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004978
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004979 lower_fullscreen_layer(shell);
4980
Pekka Paalanen01388e22013-04-25 13:57:44 +03004981 main_surface = weston_surface_get_main_surface(es);
4982
Daniel Stone37816df2012-05-16 18:45:18 +01004983 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004984
Jonas Ådahl8538b222012-08-29 22:13:03 +02004985 state = ensure_focus_state(shell, seat);
4986 if (state == NULL)
4987 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004988
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004989 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004990 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004991
Rafael Antognolli03b16592013-12-03 15:35:42 -02004992 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004993 assert(shsurf);
4994
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004995 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004996 shell_configure_fullscreen(shsurf);
4997 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004998 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004999
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005000 /* Update the surface’s layer. This brings it to the top of the stacking
5001 * order as appropriate. */
5002 shell_surface_update_layer(shsurf);
5003
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005004 if (shell->focus_animation_type != ANIMATION_NONE) {
5005 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005006 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005007 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005008}
5009
Alex Wu21858432012-04-01 20:13:08 +08005010/* no-op func for checking black surface */
5011static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005012black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005013{
5014}
5015
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005016static bool
Alex Wu21858432012-04-01 20:13:08 +08005017is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5018{
5019 if (es->configure == black_surface_configure) {
5020 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005021 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005022 return true;
5023 }
5024 return false;
5025}
5026
Kristian Høgsberg75840622011-09-06 13:48:16 -04005027static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005028activate_binding(struct weston_seat *seat,
5029 struct desktop_shell *shell,
5030 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005031{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005032 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005033
Alex Wu9c35e6b2012-03-05 14:13:13 +08005034 if (!focus)
5035 return;
5036
Pekka Paalanen01388e22013-04-25 13:57:44 +03005037 if (is_black_surface(focus, &main_surface))
5038 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005039
Pekka Paalanen01388e22013-04-25 13:57:44 +03005040 main_surface = weston_surface_get_main_surface(focus);
5041 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005042 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005043
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005044 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005045}
5046
5047static void
5048click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
5049 void *data)
5050{
5051 if (seat->pointer->grab != &seat->pointer->default_grab)
5052 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005053 if (seat->pointer->focus == NULL)
5054 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005055
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005056 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005057}
5058
5059static void
5060touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
5061{
5062 if (seat->touch->grab != &seat->touch->default_grab)
5063 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005064 if (seat->touch->focus == NULL)
5065 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005066
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005067 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005068}
5069
5070static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005071unfocus_all_seats(struct desktop_shell *shell)
5072{
5073 struct weston_seat *seat, *next;
5074
5075 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5076 if (seat->keyboard == NULL)
5077 continue;
5078
5079 weston_keyboard_set_focus(seat->keyboard, NULL);
5080 }
5081}
5082
5083static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005084lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005085{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005086 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005087
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005088 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005089 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005090 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005091 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005092
5093 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005094
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005095 /* Hide all surfaces by removing the fullscreen, panel and
5096 * toplevel layers. This way nothing else can show or receive
5097 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005098
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005099 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005100 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005101 if (shell->showing_input_panels)
5102 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005103 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005104 wl_list_insert(&shell->compositor->cursor_layer.link,
5105 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005106
Pekka Paalanen77346a62011-11-30 16:26:35 +02005107 launch_screensaver(shell);
5108
Neil Robertsb4a91702014-04-09 16:33:32 +01005109 /* Remove the keyboard focus on all seats. This will be
5110 * restored to the workspace's saved state via
5111 * restore_focus_state when the compositor is unlocked */
5112 unfocus_all_seats(shell);
5113
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005114 /* TODO: disable bindings that should not work while locked. */
5115
5116 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005117}
5118
5119static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005120unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005121{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005122 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005123 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005124 return;
5125 }
5126
5127 /* If desktop-shell client has gone away, unlock immediately. */
5128 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005129 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005130 return;
5131 }
5132
5133 if (shell->prepare_event_sent)
5134 return;
5135
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005136 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005137 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005138}
5139
5140static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005141shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005142{
5143 struct desktop_shell *shell = data;
5144
5145 shell->fade.animation = NULL;
5146
5147 switch (shell->fade.type) {
5148 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005149 weston_surface_destroy(shell->fade.view->surface);
5150 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005151 break;
5152 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005153 lock(shell);
5154 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005155 default:
5156 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005157 }
5158}
5159
Jason Ekstranda7af7042013-10-12 22:38:11 -05005160static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005161shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005162{
5163 struct weston_compositor *compositor = shell->compositor;
5164 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005165 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005166
5167 surface = weston_surface_create(compositor);
5168 if (!surface)
5169 return NULL;
5170
Jason Ekstranda7af7042013-10-12 22:38:11 -05005171 view = weston_view_create(surface);
5172 if (!view) {
5173 weston_surface_destroy(surface);
5174 return NULL;
5175 }
5176
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005177 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005178 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005179 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005180 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5181 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005182 pixman_region32_init(&surface->input);
5183
Jason Ekstranda7af7042013-10-12 22:38:11 -05005184 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005185}
5186
5187static void
5188shell_fade(struct desktop_shell *shell, enum fade_type type)
5189{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005190 float tint;
5191
5192 switch (type) {
5193 case FADE_IN:
5194 tint = 0.0;
5195 break;
5196 case FADE_OUT:
5197 tint = 1.0;
5198 break;
5199 default:
5200 weston_log("shell: invalid fade type\n");
5201 return;
5202 }
5203
5204 shell->fade.type = type;
5205
Jason Ekstranda7af7042013-10-12 22:38:11 -05005206 if (shell->fade.view == NULL) {
5207 shell->fade.view = shell_fade_create_surface(shell);
5208 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005209 return;
5210
Jason Ekstranda7af7042013-10-12 22:38:11 -05005211 shell->fade.view->alpha = 1.0 - tint;
5212 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005213 }
5214
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005215 if (shell->fade.view->output == NULL) {
5216 /* If the black view gets a NULL output, we lost the
5217 * last output and we'll just cancel the fade. This
5218 * happens when you close the last window under the
5219 * X11 or Wayland backends. */
5220 shell->locked = false;
5221 weston_surface_destroy(shell->fade.view->surface);
5222 shell->fade.view = NULL;
5223 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005224 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005225 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005226 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005227 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005228 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005229 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005230 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005231}
5232
5233static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005234do_shell_fade_startup(void *data)
5235{
5236 struct desktop_shell *shell = data;
5237
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005238 if (shell->startup_animation_type == ANIMATION_FADE)
5239 shell_fade(shell, FADE_IN);
5240 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005241 weston_surface_destroy(shell->fade.view->surface);
5242 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005243 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005244}
5245
5246static void
5247shell_fade_startup(struct desktop_shell *shell)
5248{
5249 struct wl_event_loop *loop;
5250
5251 if (!shell->fade.startup_timer)
5252 return;
5253
5254 wl_event_source_remove(shell->fade.startup_timer);
5255 shell->fade.startup_timer = NULL;
5256
5257 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5258 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5259}
5260
5261static int
5262fade_startup_timeout(void *data)
5263{
5264 struct desktop_shell *shell = data;
5265
5266 shell_fade_startup(shell);
5267 return 0;
5268}
5269
5270static void
5271shell_fade_init(struct desktop_shell *shell)
5272{
5273 /* Make compositor output all black, and wait for the desktop-shell
5274 * client to signal it is ready, then fade in. The timer triggers a
5275 * fade-in, in case the desktop-shell client takes too long.
5276 */
5277
5278 struct wl_event_loop *loop;
5279
Jason Ekstranda7af7042013-10-12 22:38:11 -05005280 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005281 weston_log("%s: warning: fade surface already exists\n",
5282 __func__);
5283 return;
5284 }
5285
Jason Ekstranda7af7042013-10-12 22:38:11 -05005286 shell->fade.view = shell_fade_create_surface(shell);
5287 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005288 return;
5289
Jason Ekstranda7af7042013-10-12 22:38:11 -05005290 weston_view_update_transform(shell->fade.view);
5291 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005292
5293 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5294 shell->fade.startup_timer =
5295 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5296 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5297}
5298
5299static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005300idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005301{
5302 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005303 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005304 struct weston_seat *seat;
5305
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005306 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005307 if (seat->pointer)
5308 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005309 if (seat->touch)
5310 touch_popup_grab_end(seat->touch);
5311 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005312
5313 shell_fade(shell, FADE_OUT);
5314 /* lock() is called from shell_fade_done() */
5315}
5316
5317static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005318wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005319{
5320 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005321 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005322
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005323 unlock(shell);
5324}
5325
5326static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005327center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005328{
Giulio Camuffob8366642013-04-25 13:57:46 +03005329 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005330 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005331
Jason Ekstranda7af7042013-10-12 22:38:11 -05005332 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005333
5334 x = output->x + (output->width - width) / 2 - surf_x / 2;
5335 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005336
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005337 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005338}
5339
5340static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005341weston_view_set_initial_position(struct weston_view *view,
5342 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005343{
5344 struct weston_compositor *compositor = shell->compositor;
5345 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005346 int32_t range_x, range_y;
5347 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005348 struct weston_output *output, *target_output = NULL;
5349 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005350 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005351
5352 /* As a heuristic place the new window on the same output as the
5353 * pointer. Falling back to the output containing 0, 0.
5354 *
5355 * TODO: Do something clever for touch too?
5356 */
5357 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005358 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005359 ix = wl_fixed_to_int(seat->pointer->x);
5360 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005361 break;
5362 }
5363 }
5364
5365 wl_list_for_each(output, &compositor->output_list, link) {
5366 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5367 target_output = output;
5368 break;
5369 }
5370 }
5371
5372 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005373 weston_view_set_position(view, 10 + random() % 400,
5374 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005375 return;
5376 }
5377
5378 /* Valid range within output where the surface will still be onscreen.
5379 * If this is negative it means that the surface is bigger than
5380 * output.
5381 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005382 get_output_work_area(shell, target_output, &area);
5383
5384 dx = area.x;
5385 dy = area.y;
5386 range_x = area.width - view->surface->width;
5387 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005388
Rob Bradford4cb88c72012-08-13 15:18:44 +01005389 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005390 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005391
5392 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005393 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005394
5395 x = target_output->x + dx;
5396 y = target_output->y + dy;
5397
Jason Ekstranda7af7042013-10-12 22:38:11 -05005398 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005399}
5400
5401static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005402set_maximized_position(struct desktop_shell *shell,
5403 struct shell_surface *shsurf)
5404{
5405 int32_t surf_x, surf_y;
5406 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005407 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005408
Jonny Lambd73c6942014-08-20 15:53:20 +02005409 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005410 if (shsurf->has_set_geometry) {
5411 surf_x = shsurf->geometry.x;
5412 surf_y = shsurf->geometry.y;
5413 } else {
5414 surface_subsurfaces_boundingbox(shsurf->surface,
5415 &surf_x, &surf_y, NULL, NULL);
5416 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005417 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005418
5419 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005420 e->x1 + area.x - surf_x,
5421 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005422}
5423
5424static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005425map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005426 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005427{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005428 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005429 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005430
Pekka Paalanen77346a62011-11-30 16:26:35 +02005431 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005432 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005433 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005434 if (shsurf->state.fullscreen) {
5435 center_on_output(shsurf->view, shsurf->fullscreen_output);
5436 shell_map_fullscreen(shsurf);
5437 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005438 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005439 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005440 weston_view_set_initial_position(shsurf->view, shell);
5441 }
Juan Zhao96879df2012-02-07 08:45:41 +08005442 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005443 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005444 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005445 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005446 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005447 weston_view_set_position(shsurf->view,
5448 shsurf->view->geometry.x + sx,
5449 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005450 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005451 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005452 default:
5453 ;
5454 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005455
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005456 /* Surface stacking order, see also activate(). */
5457 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005458
Jason Ekstranda7af7042013-10-12 22:38:11 -05005459 if (shsurf->type != SHELL_SURFACE_NONE) {
5460 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005461 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005462 shsurf->surface->output = shsurf->output;
5463 shsurf->view->output = shsurf->output;
5464 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005465 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005466
Jason Ekstranda7af7042013-10-12 22:38:11 -05005467 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005468 /* XXX: xwayland's using the same fields for transient type */
5469 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005470 if (shsurf->transient.flags ==
5471 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5472 break;
5473 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005474 if (shsurf->state.relative &&
5475 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5476 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005477 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005478 break;
5479 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005480 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005481 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005482 case SHELL_SURFACE_POPUP:
5483 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005484 default:
5485 break;
5486 }
5487
Rafael Antognolli03b16592013-12-03 15:35:42 -02005488 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5489 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005490 {
5491 switch (shell->win_animation_type) {
5492 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005493 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005494 break;
5495 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005496 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005497 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005498 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005499 default:
5500 break;
5501 }
5502 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005503}
5504
5505static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005506configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005507 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005508{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005509 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005510 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005511
Pekka Paalanen77346a62011-11-30 16:26:35 +02005512 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005513
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005514 assert(shsurf);
5515
Rafael Antognolli03b16592013-12-03 15:35:42 -02005516 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005517 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005518 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005519 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005520 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005521 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005522 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005523
Alex Wu4539b082012-03-01 12:57:46 +08005524 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005525 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005526 wl_list_for_each(view, &surface->views, surface_link)
5527 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005528
Rafael Antognolli03b16592013-12-03 15:35:42 -02005529 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005530 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005531 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005532}
5533
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005534static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005535shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005536{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005537 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005538 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005539 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005540
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005541 assert(shsurf);
5542
5543 shell = shsurf->shell;
5544
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005545 if (!weston_surface_is_mapped(es) &&
5546 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005547 remove_popup_grab(shsurf);
5548 }
5549
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005550 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005551 return;
5552
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005553 if (shsurf->has_next_geometry) {
5554 shsurf->geometry = shsurf->next_geometry;
5555 shsurf->has_next_geometry = false;
5556 shsurf->has_set_geometry = true;
5557 } else if (!shsurf->has_set_geometry) {
5558 surface_subsurfaces_boundingbox(shsurf->surface,
5559 &shsurf->geometry.x,
5560 &shsurf->geometry.y,
5561 &shsurf->geometry.width,
5562 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005563 }
5564
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005565 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005566 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005567 type_changed = 1;
5568 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005569
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005570 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005571 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005572 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005573 shsurf->last_width != es->width ||
5574 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005575 float from_x, from_y;
5576 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005577
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005578 if (shsurf->resize_edges) {
5579 sx = 0;
5580 sy = 0;
5581 }
5582
5583 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5584 sx = shsurf->last_width - es->width;
5585 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5586 sy = shsurf->last_height - es->height;
5587
5588 shsurf->last_width = es->width;
5589 shsurf->last_height = es->height;
5590
Jason Ekstranda7af7042013-10-12 22:38:11 -05005591 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5592 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005593 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005594 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005595 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005596 }
5597}
5598
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005599static bool
5600check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5601{
5602 struct timespec now;
5603
5604 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5605 return false;
5606
5607 /*
5608 * If the shell helper client dies before the session has been
5609 * up for roughly 30 seconds, better just make Weston shut down,
5610 * because the user likely has no way to interact with the desktop
5611 * anyway.
5612 */
5613 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5614 weston_log("Error: %s apparently cannot run at all.\n",
5615 shell->client);
5616 weston_log_continue(STAMP_SPACE "Quitting...");
5617 wl_display_terminate(shell->compositor->wl_display);
5618
5619 return true;
5620 }
5621
5622 return false;
5623}
5624
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005625static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005626
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005627static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005628respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005629{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005630 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005631
5632 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5633 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005634 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005635 shell->child.deathstamp = time;
5636 shell->child.deathcount = 0;
5637 }
5638
5639 shell->child.deathcount++;
5640 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005641 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005642 return;
5643 }
5644
Pekka Paalanen826dc142014-08-27 12:11:53 +03005645 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005646 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005647}
5648
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005649static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005650desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5651{
5652 struct desktop_shell *shell;
5653
5654 shell = container_of(listener, struct desktop_shell,
5655 child.client_destroy_listener);
5656
Pekka Paalanen826dc142014-08-27 12:11:53 +03005657 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005658 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005659 /*
5660 * unbind_desktop_shell() will reset shell->child.desktop_shell
5661 * before the respawned process has a chance to create a new
5662 * desktop_shell object, because we are being called from the
5663 * wl_client destructor which destroys all wl_resources before
5664 * returning.
5665 */
5666
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005667 if (!check_desktop_shell_crash_too_early(shell))
5668 respawn_desktop_shell_process(shell);
5669
Pekka Paalanen826dc142014-08-27 12:11:53 +03005670 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005671}
5672
5673static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005674launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005675{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005676 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005677
Pekka Paalanen826dc142014-08-27 12:11:53 +03005678 shell->child.client = weston_client_start(shell->compositor,
5679 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005680
Arnaud Vrac42631192014-08-25 20:56:46 +02005681 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005682 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005683 return;
5684 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005685
5686 shell->child.client_destroy_listener.notify =
5687 desktop_shell_client_destroy;
5688 wl_client_add_destroy_listener(shell->child.client,
5689 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005690}
5691
5692static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005693handle_shell_client_destroy(struct wl_listener *listener, void *data)
5694{
5695 struct shell_client *sc =
5696 container_of(listener, struct shell_client, destroy_listener);
5697
5698 if (sc->ping_timer)
5699 wl_event_source_remove(sc->ping_timer);
5700 free(sc);
5701}
5702
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005703static struct shell_client *
5704shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5705 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005706{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005707 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005708
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005709 sc = zalloc(sizeof *sc);
5710 if (sc == NULL) {
5711 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005712 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005713 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005714
5715 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005716 if (sc->resource == NULL) {
5717 free(sc);
5718 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005719 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005720 }
5721
5722 sc->client = client;
5723 sc->shell = shell;
5724 sc->destroy_listener.notify = handle_shell_client_destroy;
5725 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5726
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005727 return sc;
5728}
5729
5730static void
5731bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5732{
5733 struct desktop_shell *shell = data;
5734 struct shell_client *sc;
5735
5736 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5737 if (sc)
5738 wl_resource_set_implementation(sc->resource,
5739 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005740 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005741}
5742
5743static void
5744bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5745{
5746 struct desktop_shell *shell = data;
5747 struct shell_client *sc;
5748
5749 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5750 if (sc)
5751 wl_resource_set_dispatcher(sc->resource,
5752 xdg_shell_unversioned_dispatch,
5753 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005754}
5755
5756static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005757unbind_desktop_shell(struct wl_resource *resource)
5758{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005759 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005760
5761 if (shell->locked)
5762 resume_desktop(shell);
5763
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005764 shell->child.desktop_shell = NULL;
5765 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005766}
5767
5768static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005769bind_desktop_shell(struct wl_client *client,
5770 void *data, uint32_t version, uint32_t id)
5771{
Tiago Vignattibe143262012-04-16 17:31:41 +03005772 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005773 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005774
Jason Ekstranda85118c2013-06-27 20:17:02 -05005775 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005776 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005777
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005778 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005779 wl_resource_set_implementation(resource,
5780 &desktop_shell_implementation,
5781 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005782 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005783
5784 if (version < 2)
5785 shell_fade_startup(shell);
5786
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005787 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005788 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005789
5790 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5791 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005792}
5793
Pekka Paalanen8274d902014-08-06 19:36:51 +03005794static int
5795screensaver_get_label(struct weston_surface *surface, char *buf, size_t len)
5796{
5797 return snprintf(buf, len, "screensaver for output %s",
5798 surface->output->name);
5799}
5800
Pekka Paalanen6e168112011-11-24 11:34:05 +02005801static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005802screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005803{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005804 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005805 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005806 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005807
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005808 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005809 return;
5810
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005811 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005812 if (!shell->locked)
5813 return;
5814
Jason Ekstranda7af7042013-10-12 22:38:11 -05005815 view = container_of(surface->views.next, struct weston_view, surface_link);
5816 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005817
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005818 if (wl_list_empty(&view->layer_link.link)) {
5819 prev = container_of(shell->lock_layer.view_list.link.prev,
5820 struct weston_layer_entry, link);
5821 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005822 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005823 wl_event_source_timer_update(shell->screensaver.timer,
5824 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005825 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005826 }
5827}
5828
5829static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005830screensaver_set_surface(struct wl_client *client,
5831 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005832 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005833 struct wl_resource *output_resource)
5834{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005835 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005836 struct weston_surface *surface =
5837 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005838 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005839 struct weston_view *view, *next;
5840
5841 /* Make sure we only have one view */
5842 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5843 weston_view_destroy(view);
5844 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005845
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005846 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005847 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03005848 weston_surface_set_label_func(surface, screensaver_get_label);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005849 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005850}
5851
5852static const struct screensaver_interface screensaver_implementation = {
5853 screensaver_set_surface
5854};
5855
5856static void
5857unbind_screensaver(struct wl_resource *resource)
5858{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005859 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005860
Pekka Paalanen77346a62011-11-30 16:26:35 +02005861 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005862}
5863
5864static void
5865bind_screensaver(struct wl_client *client,
5866 void *data, uint32_t version, uint32_t id)
5867{
Tiago Vignattibe143262012-04-16 17:31:41 +03005868 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005869 struct wl_resource *resource;
5870
Jason Ekstranda85118c2013-06-27 20:17:02 -05005871 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005872
Pekka Paalanen77346a62011-11-30 16:26:35 +02005873 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005874 wl_resource_set_implementation(resource,
5875 &screensaver_implementation,
5876 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005877 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005878 return;
5879 }
5880
5881 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5882 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005883}
5884
Kristian Høgsberg07045392012-02-19 18:52:44 -05005885struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005886 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005887 struct weston_surface *current;
5888 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005889 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005890 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005891};
5892
5893static void
5894switcher_next(struct switcher *switcher)
5895{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005896 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005897 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005898 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005899 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005900
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005901 /* temporary re-display minimized surfaces */
5902 struct weston_view *tmp;
5903 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005904 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5905 weston_layer_entry_remove(&view->layer_link);
5906 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005907 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5908 *minimized = view;
5909 }
5910
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005911 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005912 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005913 if (shsurf &&
5914 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5915 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005916 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005917 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005918 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005919 next = view->surface;
5920 prev = view->surface;
5921 view->alpha = 0.25;
5922 weston_view_geometry_dirty(view);
5923 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005924 }
Alex Wu1659daa2012-04-01 20:13:09 +08005925
Jason Ekstranda7af7042013-10-12 22:38:11 -05005926 if (is_black_surface(view->surface, NULL)) {
5927 view->alpha = 0.25;
5928 weston_view_geometry_dirty(view);
5929 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005930 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005931 }
5932
5933 if (next == NULL)
5934 next = first;
5935
Alex Wu07b26062012-03-12 16:06:01 +08005936 if (next == NULL)
5937 return;
5938
Kristian Høgsberg07045392012-02-19 18:52:44 -05005939 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005940 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005941
5942 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005943 wl_list_for_each(view, &next->views, surface_link)
5944 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005945
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005946 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005947 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005948 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005949}
5950
5951static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005952switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005953{
5954 struct switcher *switcher =
5955 container_of(listener, struct switcher, listener);
5956
5957 switcher_next(switcher);
5958}
5959
5960static void
Daniel Stone351eb612012-05-31 15:27:47 -04005961switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005962{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005963 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005964 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005965 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005966
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005967 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005968 if (is_focus_view(view))
5969 continue;
5970
Jason Ekstranda7af7042013-10-12 22:38:11 -05005971 view->alpha = 1.0;
5972 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005973 }
5974
Alex Wu07b26062012-03-12 16:06:01 +08005975 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005976 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06005977 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005978 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005979 weston_keyboard_end_grab(keyboard);
5980 if (keyboard->input_method_resource)
5981 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005982
5983 /* re-hide surfaces that were temporary shown during the switch */
5984 struct weston_view **minimized;
5985 wl_array_for_each(minimized, &switcher->minimized_array) {
5986 /* with the exception of the current selected */
5987 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005988 weston_layer_entry_remove(&(*minimized)->layer_link);
5989 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005990 weston_view_damage_below(*minimized);
5991 }
5992 }
5993 wl_array_release(&switcher->minimized_array);
5994
Kristian Høgsberg07045392012-02-19 18:52:44 -05005995 free(switcher);
5996}
5997
5998static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005999switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006000 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006001{
6002 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006003 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006004
Daniel Stonec9785ea2012-05-30 16:31:52 +01006005 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006006 switcher_next(switcher);
6007}
6008
6009static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006010switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006011 uint32_t mods_depressed, uint32_t mods_latched,
6012 uint32_t mods_locked, uint32_t group)
6013{
6014 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006015 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006016
Daniel Stone351eb612012-05-31 15:27:47 -04006017 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6018 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006019}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006020
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006021static void
6022switcher_cancel(struct weston_keyboard_grab *grab)
6023{
6024 struct switcher *switcher = container_of(grab, struct switcher, grab);
6025
6026 switcher_destroy(switcher);
6027}
6028
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006029static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006030 switcher_key,
6031 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006032 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006033};
6034
6035static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006036switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006037 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006038{
Tiago Vignattibe143262012-04-16 17:31:41 +03006039 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006040 struct switcher *switcher;
6041
6042 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006043 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006044 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006045 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006046 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006047 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006048
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006049 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006050 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006051 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006052 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
6053 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006054 switcher_next(switcher);
6055}
6056
Pekka Paalanen3c647232011-12-22 13:43:43 +02006057static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006058backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006059 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006060{
6061 struct weston_compositor *compositor = data;
6062 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006063 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006064
6065 /* TODO: we're limiting to simple use cases, where we assume just
6066 * control on the primary display. We'd have to extend later if we
6067 * ever get support for setting backlights on random desktop LCD
6068 * panels though */
6069 output = get_default_output(compositor);
6070 if (!output)
6071 return;
6072
6073 if (!output->set_backlight)
6074 return;
6075
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006076 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6077 backlight_new = output->backlight_current - 25;
6078 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6079 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006080
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006081 if (backlight_new < 5)
6082 backlight_new = 5;
6083 if (backlight_new > 255)
6084 backlight_new = 255;
6085
6086 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006087 output->set_backlight(output, output->backlight_current);
6088}
6089
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006090struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006091 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006092 struct weston_seat *seat;
6093 uint32_t key[2];
6094 int key_released[2];
6095};
6096
6097static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006098debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006099 uint32_t key, uint32_t state)
6100{
6101 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01006102 struct weston_compositor *ec = db->seat->compositor;
6103 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006104 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006105 uint32_t serial;
6106 int send = 0, terminate = 0;
6107 int check_binding = 1;
6108 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01006109 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006110
6111 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
6112 /* Do not run bindings on key releases */
6113 check_binding = 0;
6114
6115 for (i = 0; i < 2; i++)
6116 if (key == db->key[i])
6117 db->key_released[i] = 1;
6118
6119 if (db->key_released[0] && db->key_released[1]) {
6120 /* All key releases been swalled so end the grab */
6121 terminate = 1;
6122 } else if (key != db->key[0] && key != db->key[1]) {
6123 /* Should not swallow release of other keys */
6124 send = 1;
6125 }
6126 } else if (key == db->key[0] && !db->key_released[0]) {
6127 /* Do not check bindings for the first press of the binding
6128 * key. This allows it to be used as a debug shortcut.
6129 * We still need to swallow this event. */
6130 check_binding = 0;
6131 } else if (db->key[1]) {
6132 /* If we already ran a binding don't process another one since
6133 * we can't keep track of all the binding keys that were
6134 * pressed in order to swallow the release events. */
6135 send = 1;
6136 check_binding = 0;
6137 }
6138
6139 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006140 if (weston_compositor_run_debug_binding(ec, db->seat, time,
6141 key, state)) {
6142 /* We ran a binding so swallow the press and keep the
6143 * grab to swallow the released too. */
6144 send = 0;
6145 terminate = 0;
6146 db->key[1] = key;
6147 } else {
6148 /* Terminate the grab since the key pressed is not a
6149 * debug binding key. */
6150 send = 1;
6151 terminate = 1;
6152 }
6153 }
6154
6155 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01006156 serial = wl_display_next_serial(display);
6157 resource_list = &grab->keyboard->focus_resource_list;
6158 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006159 wl_keyboard_send_key(resource, serial, time, key, state);
6160 }
6161 }
6162
6163 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006164 weston_keyboard_end_grab(grab->keyboard);
6165 if (grab->keyboard->input_method_resource)
6166 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006167 free(db);
6168 }
6169}
6170
6171static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006172debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006173 uint32_t mods_depressed, uint32_t mods_latched,
6174 uint32_t mods_locked, uint32_t group)
6175{
6176 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01006177 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006178
Neil Roberts96d790e2013-09-19 17:32:00 +01006179 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006180
Neil Roberts96d790e2013-09-19 17:32:00 +01006181 wl_resource_for_each(resource, resource_list) {
6182 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
6183 mods_latched, mods_locked, group);
6184 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006185}
6186
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006187static void
6188debug_binding_cancel(struct weston_keyboard_grab *grab)
6189{
6190 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
6191
6192 weston_keyboard_end_grab(grab->keyboard);
6193 free(db);
6194}
6195
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006196struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006197 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006198 debug_binding_modifiers,
6199 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006200};
6201
6202static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006203debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006204{
6205 struct debug_binding_grab *grab;
6206
6207 grab = calloc(1, sizeof *grab);
6208 if (!grab)
6209 return;
6210
Derek Foreman8aeeac82015-01-30 13:24:36 -06006211 grab->seat = seat;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006212 grab->key[0] = key;
6213 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006214 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006215}
6216
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006217static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006218force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006219 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006220{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006221 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006222 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006223 struct desktop_shell *shell = data;
6224 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006225 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006226
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006227 focus_surface = seat->keyboard->focus;
6228 if (!focus_surface)
6229 return;
6230
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006231 wl_signal_emit(&compositor->kill_signal, focus_surface);
6232
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006233 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006234 wl_client_get_credentials(client, &pid, NULL, NULL);
6235
6236 /* Skip clients that we launched ourselves (the credentials of
6237 * the socketpair is ours) */
6238 if (pid == getpid())
6239 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006240
Daniel Stone325fc2d2012-05-30 16:31:58 +01006241 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006242}
6243
6244static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006245workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006246 uint32_t key, void *data)
6247{
6248 struct desktop_shell *shell = data;
6249 unsigned int new_index = shell->workspaces.current;
6250
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006251 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006252 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006253 if (new_index != 0)
6254 new_index--;
6255
6256 change_workspace(shell, new_index);
6257}
6258
6259static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006260workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006261 uint32_t key, void *data)
6262{
6263 struct desktop_shell *shell = data;
6264 unsigned int new_index = shell->workspaces.current;
6265
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006266 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006267 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006268 if (new_index < shell->workspaces.num - 1)
6269 new_index++;
6270
6271 change_workspace(shell, new_index);
6272}
6273
6274static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006275workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006276 uint32_t key, void *data)
6277{
6278 struct desktop_shell *shell = data;
6279 unsigned int new_index;
6280
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006281 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006282 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006283 new_index = key - KEY_F1;
6284 if (new_index >= shell->workspaces.num)
6285 new_index = shell->workspaces.num - 1;
6286
6287 change_workspace(shell, new_index);
6288}
6289
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006290static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006291workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006292 uint32_t key, void *data)
6293{
6294 struct desktop_shell *shell = data;
6295 unsigned int new_index = shell->workspaces.current;
6296
6297 if (shell->locked)
6298 return;
6299
6300 if (new_index != 0)
6301 new_index--;
6302
6303 take_surface_to_workspace_by_seat(shell, seat, new_index);
6304}
6305
6306static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006307workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006308 uint32_t key, void *data)
6309{
6310 struct desktop_shell *shell = data;
6311 unsigned int new_index = shell->workspaces.current;
6312
6313 if (shell->locked)
6314 return;
6315
6316 if (new_index < shell->workspaces.num - 1)
6317 new_index++;
6318
6319 take_surface_to_workspace_by_seat(shell, seat, new_index);
6320}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006321
6322static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006323shell_reposition_view_on_output_destroy(struct weston_view *view)
6324{
6325 struct weston_output *output, *first_output;
6326 struct weston_compositor *ec = view->surface->compositor;
6327 struct shell_surface *shsurf;
6328 float x, y;
6329 int visible;
6330
6331 x = view->geometry.x;
6332 y = view->geometry.y;
6333
6334 /* At this point the destroyed output is not in the list anymore.
6335 * If the view is still visible somewhere, we leave where it is,
6336 * otherwise, move it to the first output. */
6337 visible = 0;
6338 wl_list_for_each(output, &ec->output_list, link) {
6339 if (pixman_region32_contains_point(&output->region,
6340 x, y, NULL)) {
6341 visible = 1;
6342 break;
6343 }
6344 }
6345
6346 if (!visible) {
6347 first_output = container_of(ec->output_list.next,
6348 struct weston_output, link);
6349
6350 x = first_output->x + first_output->width / 4;
6351 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006352
6353 weston_view_set_position(view, x, y);
6354 } else {
6355 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006356 }
6357
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006358
6359 shsurf = get_shell_surface(view->surface);
6360
6361 if (shsurf) {
6362 shsurf->saved_position_valid = false;
6363 shsurf->next_state.maximized = false;
6364 shsurf->next_state.fullscreen = false;
6365 shsurf->state_changed = true;
6366 }
6367}
6368
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006369void
6370shell_for_each_layer(struct desktop_shell *shell,
6371 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006372{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006373 struct workspace **ws;
6374
6375 func(shell, &shell->fullscreen_layer, data);
6376 func(shell, &shell->panel_layer, data);
6377 func(shell, &shell->background_layer, data);
6378 func(shell, &shell->lock_layer, data);
6379 func(shell, &shell->input_panel_layer, data);
6380
6381 wl_array_for_each(ws, &shell->workspaces.array)
6382 func(shell, &(*ws)->layer, data);
6383}
6384
6385static void
6386shell_output_destroy_move_layer(struct desktop_shell *shell,
6387 struct weston_layer *layer,
6388 void *data)
6389{
6390 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006391 struct weston_view *view;
6392
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006393 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006394 if (view->output != output)
6395 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006396
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006397 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006398 }
6399}
6400
6401static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006402handle_output_destroy(struct wl_listener *listener, void *data)
6403{
6404 struct shell_output *output_listener =
6405 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006406 struct weston_output *output = output_listener->output;
6407 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006408
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006409 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006410
Xiong Zhang6b481422013-10-23 13:58:32 +08006411 wl_list_remove(&output_listener->destroy_listener.link);
6412 wl_list_remove(&output_listener->link);
6413 free(output_listener);
6414}
6415
6416static void
6417create_shell_output(struct desktop_shell *shell,
6418 struct weston_output *output)
6419{
6420 struct shell_output *shell_output;
6421
6422 shell_output = zalloc(sizeof *shell_output);
6423 if (shell_output == NULL)
6424 return;
6425
6426 shell_output->output = output;
6427 shell_output->shell = shell;
6428 shell_output->destroy_listener.notify = handle_output_destroy;
6429 wl_signal_add(&output->destroy_signal,
6430 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006431 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006432}
6433
6434static void
6435handle_output_create(struct wl_listener *listener, void *data)
6436{
6437 struct desktop_shell *shell =
6438 container_of(listener, struct desktop_shell, output_create_listener);
6439 struct weston_output *output = (struct weston_output *)data;
6440
6441 create_shell_output(shell, output);
6442}
6443
6444static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006445handle_output_move_layer(struct desktop_shell *shell,
6446 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006447{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006448 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006449 struct weston_view *view;
6450 float x, y;
6451
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006452 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006453 if (view->output != output)
6454 continue;
6455
6456 x = view->geometry.x + output->move_x;
6457 y = view->geometry.y + output->move_y;
6458 weston_view_set_position(view, x, y);
6459 }
6460}
6461
6462static void
6463handle_output_move(struct wl_listener *listener, void *data)
6464{
6465 struct desktop_shell *shell;
6466
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006467 shell = container_of(listener, struct desktop_shell,
6468 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006469
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006470 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006471}
6472
6473static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006474setup_output_destroy_handler(struct weston_compositor *ec,
6475 struct desktop_shell *shell)
6476{
6477 struct weston_output *output;
6478
6479 wl_list_init(&shell->output_list);
6480 wl_list_for_each(output, &ec->output_list, link)
6481 create_shell_output(shell, output);
6482
6483 shell->output_create_listener.notify = handle_output_create;
6484 wl_signal_add(&ec->output_created_signal,
6485 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006486
6487 shell->output_move_listener.notify = handle_output_move;
6488 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006489}
6490
6491static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006492shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006493{
Tiago Vignattibe143262012-04-16 17:31:41 +03006494 struct desktop_shell *shell =
6495 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006496 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006497 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006498
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006499 /* Force state to unlocked so we don't try to fade */
6500 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006501
6502 if (shell->child.client) {
6503 /* disable respawn */
6504 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006505 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006506 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006507
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006508 wl_list_remove(&shell->idle_listener.link);
6509 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006510
6511 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006512
Xiong Zhang6b481422013-10-23 13:58:32 +08006513 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6514 wl_list_remove(&shell_output->destroy_listener.link);
6515 wl_list_remove(&shell_output->link);
6516 free(shell_output);
6517 }
6518
6519 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006520 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006521
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006522 wl_array_for_each(ws, &shell->workspaces.array)
6523 workspace_destroy(*ws);
6524 wl_array_release(&shell->workspaces.array);
6525
Pekka Paalanen3c647232011-12-22 13:43:43 +02006526 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006527 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006528 free(shell);
6529}
6530
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006531static void
6532shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6533{
6534 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006535 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006536
6537 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006538 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6539 MODIFIER_CTRL | MODIFIER_ALT,
6540 terminate_binding, ec);
6541 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6542 click_to_activate_binding,
6543 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006544 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6545 click_to_activate_binding,
6546 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006547 weston_compositor_add_touch_binding(ec, 0,
6548 touch_to_activate_binding,
6549 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006550 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6551 MODIFIER_SUPER | MODIFIER_ALT,
6552 surface_opacity_binding, NULL);
6553 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6554 MODIFIER_SUPER, zoom_axis_binding,
6555 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006556
6557 /* configurable bindings */
6558 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006559 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6560 zoom_key_binding, NULL);
6561 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6562 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006563 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6564 maximize_binding, NULL);
6565 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6566 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006567 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6568 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006569 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006570 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6571 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006572 weston_compositor_add_button_binding(ec, BTN_LEFT,
6573 mod | MODIFIER_SHIFT,
6574 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006575
6576 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6577 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6578 rotate_binding, NULL);
6579
Daniel Stone325fc2d2012-05-30 16:31:58 +01006580 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6581 shell);
6582 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6583 ec);
6584 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6585 backlight_binding, ec);
6586 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6587 ec);
6588 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6589 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006590 weston_compositor_add_key_binding(ec, KEY_K, mod,
6591 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006592 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6593 workspace_up_binding, shell);
6594 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6595 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006596 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6597 workspace_move_surface_up_binding,
6598 shell);
6599 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6600 workspace_move_surface_down_binding,
6601 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006602
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006603 if (shell->exposay_modifier)
6604 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6605 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006606
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006607 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6608 if (shell->workspaces.num > 1) {
6609 num_workspace_bindings = shell->workspaces.num;
6610 if (num_workspace_bindings > 6)
6611 num_workspace_bindings = 6;
6612 for (i = 0; i < num_workspace_bindings; i++)
6613 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6614 workspace_f_binding,
6615 shell);
6616 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006617
6618 /* Debug bindings */
6619 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6620 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006621}
6622
Jason Ekstrand024177c2014-04-21 19:42:58 -05006623static void
6624handle_seat_created(struct wl_listener *listener, void *data)
6625{
6626 struct weston_seat *seat = data;
6627
6628 create_shell_seat(seat);
6629}
6630
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006631WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006632module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006633 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006634{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006635 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006636 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006637 struct workspace **pws;
6638 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006639 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006640
Peter Huttererf3d62272013-08-08 11:57:05 +10006641 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006642 if (shell == NULL)
6643 return -1;
6644
Kristian Høgsberg75840622011-09-06 13:48:16 -04006645 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006646
6647 shell->destroy_listener.notify = shell_destroy;
6648 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006649 shell->idle_listener.notify = idle_handler;
6650 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6651 shell->wake_listener.notify = wake_handler;
6652 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006653
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006654 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006655 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006656 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006657 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006658 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006659 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006660 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006661 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006662 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006663 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006664 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006665 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006666
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006667 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6668 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006669 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6670 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006671 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006672
6673 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006674 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006675
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006676 if (input_panel_setup(shell) < 0)
6677 return -1;
6678
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006679 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006680
Daniel Stonedf8133b2013-11-19 11:37:14 +01006681 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6682 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6683
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006684 for (i = 0; i < shell->workspaces.num; i++) {
6685 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6686 if (pws == NULL)
6687 return -1;
6688
6689 *pws = workspace_create();
6690 if (*pws == NULL)
6691 return -1;
6692 }
6693 activate_workspace(shell, 0);
6694
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006695 weston_layer_init(&shell->minimized_layer, NULL);
6696
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006697 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006698 wl_list_init(&shell->workspaces.animation.link);
6699 shell->workspaces.animation.frame = animate_workspace_change_frame;
6700
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006701 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006702 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006703 return -1;
6704
Rafael Antognollie2a34552013-12-03 15:35:45 -02006705 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6706 shell, bind_xdg_shell) == NULL)
6707 return -1;
6708
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006709 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006710 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006711 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006712 return -1;
6713
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006714 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6715 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006716 return -1;
6717
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006718 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6719 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006720 return -1;
6721
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006722 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006723
Jonny Lamb765760d2014-08-20 15:53:19 +02006724 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6725
Xiong Zhang6b481422013-10-23 13:58:32 +08006726 setup_output_destroy_handler(ec, shell);
6727
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006728 loop = wl_display_get_event_loop(ec->wl_display);
6729 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006730
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006731 shell->screensaver.timer =
6732 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6733
Jason Ekstrand024177c2014-04-21 19:42:58 -05006734 wl_list_for_each(seat, &ec->seat_list, link)
6735 handle_seat_created(NULL, seat);
6736 shell->seat_create_listener.notify = handle_seat_created;
6737 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006738
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006739 screenshooter_create(ec);
6740
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006741 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006742
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006743 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006744
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006745 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6746
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006747 return 0;
6748}