blob: f7c928e7873049f39b3cd5f76dffac9f0093acce [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
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001841 if (width < 1)
1842 width = 1;
1843 if (height < 1)
1844 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001845 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001846}
1847
1848static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001849send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001850{
1851 struct shell_surface *shsurf = get_shell_surface(surface);
1852
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001853 assert(shsurf);
1854
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001855 if (shsurf->resource)
1856 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001857 shsurf->resize_edges,
1858 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001859}
1860
1861static const struct weston_shell_client shell_client = {
1862 send_configure
1863};
1864
1865static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001866resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001867 uint32_t time, uint32_t button, uint32_t state_w)
1868{
1869 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001870 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001871 enum wl_pointer_button_state state = state_w;
1872
1873 if (pointer->button_count == 0 &&
1874 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1875 shell_grab_end(&resize->base);
1876 free(grab);
1877 }
1878}
1879
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001880static void
1881resize_grab_cancel(struct weston_pointer_grab *grab)
1882{
1883 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1884
1885 shell_grab_end(&resize->base);
1886 free(grab);
1887}
1888
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001889static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001890 noop_grab_focus,
1891 resize_grab_motion,
1892 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001893 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001894};
1895
Giulio Camuffob8366642013-04-25 13:57:46 +03001896/*
1897 * Returns the bounding box of a surface and all its sub-surfaces,
1898 * in the surface coordinates system. */
1899static void
1900surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1901 int32_t *y, int32_t *w, int32_t *h) {
1902 pixman_region32_t region;
1903 pixman_box32_t *box;
1904 struct weston_subsurface *subsurface;
1905
1906 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001907 surface->width,
1908 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001909
1910 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1911 pixman_region32_union_rect(&region, &region,
1912 subsurface->position.x,
1913 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914 subsurface->surface->width,
1915 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001916 }
1917
1918 box = pixman_region32_extents(&region);
1919 if (x)
1920 *x = box->x1;
1921 if (y)
1922 *y = box->y1;
1923 if (w)
1924 *w = box->x2 - box->x1;
1925 if (h)
1926 *h = box->y2 - box->y1;
1927
1928 pixman_region32_fini(&region);
1929}
1930
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001931static int
1932surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001933 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001934{
1935 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001936 const unsigned resize_topbottom =
1937 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1938 const unsigned resize_leftright =
1939 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1940 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001941
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001942 if (shsurf->grabbed ||
1943 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001944 return 0;
1945
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001946 /* Check for invalid edge combinations. */
1947 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1948 (edges & resize_topbottom) == resize_topbottom ||
1949 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001950 return 0;
1951
1952 resize = malloc(sizeof *resize);
1953 if (!resize)
1954 return -1;
1955
1956 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001957
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001958 resize->width = shsurf->geometry.width;
1959 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001960
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001961 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001962 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001963 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001964 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001965
1966 return 0;
1967}
1968
1969static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001970common_surface_resize(struct wl_resource *resource,
1971 struct wl_resource *seat_resource, uint32_t serial,
1972 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001973{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001974 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001975 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001976 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001977
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001978 if (seat->pointer == NULL ||
1979 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001980 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001981 seat->pointer->focus == NULL)
1982 return;
1983
1984 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1985 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001986 return;
1987
Kristian Høgsberge3148752013-05-06 23:19:49 -04001988 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001989 wl_resource_post_no_memory(resource);
1990}
1991
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001992static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001993shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1994 struct wl_resource *seat_resource, uint32_t serial,
1995 uint32_t edges)
1996{
1997 common_surface_resize(resource, seat_resource, serial, edges);
1998}
1999
2000static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002001busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002002{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002003 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002004 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002005 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002006 wl_fixed_t sx, sy;
2007
Jason Ekstranda7af7042013-10-12 22:38:11 -05002008 view = weston_compositor_pick_view(pointer->seat->compositor,
2009 pointer->x, pointer->y,
2010 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002011
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002012 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2013 shell_grab_end(grab);
2014 free(grab);
2015 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002016}
2017
2018static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002019busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2020 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002021{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002022 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002023}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002024
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002025static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002026busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002027 uint32_t time, uint32_t button, uint32_t state)
2028{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002029 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002030 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002031 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002032
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002033 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002034 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002035 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002036 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002037 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002038 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002039 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002040}
2041
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002042static void
2043busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2044{
2045 struct shell_grab *grab = (struct shell_grab *) base;
2046
2047 shell_grab_end(grab);
2048 free(grab);
2049}
2050
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002051static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002052 busy_cursor_grab_focus,
2053 busy_cursor_grab_motion,
2054 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002055 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002056};
2057
2058static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002059set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002060{
2061 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002062
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002063 if (pointer->grab->interface == &busy_cursor_grab_interface)
2064 return;
2065
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002066 grab = malloc(sizeof *grab);
2067 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002068 return;
2069
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002070 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2071 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002072 /* Mark the shsurf as ungrabbed so that button binding is able
2073 * to move it. */
2074 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002075}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002076
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002077static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002078end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002079{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002080 struct shell_grab *grab;
2081 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002082
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002083 wl_list_for_each(seat, &compositor->seat_list, link) {
2084 if (seat->pointer == NULL)
2085 continue;
2086
2087 grab = (struct shell_grab *) seat->pointer->grab;
2088 if (grab->grab.interface == &busy_cursor_grab_interface &&
2089 wl_resource_get_client(grab->shsurf->resource) == client) {
2090 shell_grab_end(grab);
2091 free(grab);
2092 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002093 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002094}
2095
Scott Moreau9521d5e2012-04-19 13:06:17 -06002096static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002097handle_shell_client_destroy(struct wl_listener *listener, void *data);
2098
2099static int
2100xdg_ping_timeout_handler(void *data)
2101{
2102 struct shell_client *sc = data;
2103 struct weston_seat *seat;
2104 struct shell_surface *shsurf;
2105
2106 /* Client is not responding */
2107 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002108 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2109 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2110 continue;
2111 if (seat->pointer->focus->surface->resource == NULL)
2112 continue;
2113
2114 shsurf = get_shell_surface(seat->pointer->focus->surface);
2115 if (shsurf &&
2116 wl_resource_get_client(shsurf->resource) == sc->client)
2117 set_busy_cursor(shsurf, seat->pointer);
2118 }
2119
2120 return 1;
2121}
2122
2123static void
2124handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2125{
2126 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002127 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002128 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002129 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002130
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002131 if (sc->unresponsive) {
2132 xdg_ping_timeout_handler(sc);
2133 return;
2134 }
2135
2136 sc->ping_serial = serial;
2137 loop = wl_display_get_event_loop(compositor->wl_display);
2138 if (sc->ping_timer == NULL)
2139 sc->ping_timer =
2140 wl_event_loop_add_timer(loop,
2141 xdg_ping_timeout_handler, sc);
2142 if (sc->ping_timer == NULL)
2143 return;
2144
2145 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2146
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002147 if (shell_surface_is_xdg_surface(shsurf) ||
2148 shell_surface_is_xdg_popup(shsurf))
2149 xdg_shell_send_ping(sc->resource, serial);
2150 else if (shell_surface_is_wl_shell_surface(shsurf))
2151 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002152}
2153
Scott Moreauff1db4a2012-04-17 19:06:18 -06002154static void
2155ping_handler(struct weston_surface *surface, uint32_t serial)
2156{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002157 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002158
2159 if (!shsurf)
2160 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002161 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002162 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002163 if (shsurf->surface == shsurf->shell->grab_surface)
2164 return;
2165
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002166 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002167}
2168
2169static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002170handle_pointer_focus(struct wl_listener *listener, void *data)
2171{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002172 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002173 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002174 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002175 uint32_t serial;
2176
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002178 return;
2179
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002181 serial = wl_display_next_serial(compositor->wl_display);
2182 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002183}
2184
2185static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002186shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2187{
2188 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002189 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002190}
2191
2192static void
2193shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2194{
2195 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002196 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002197}
2198
2199static void
2200handle_keyboard_focus(struct wl_listener *listener, void *data)
2201{
2202 struct weston_keyboard *keyboard = data;
2203 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2204
2205 if (seat->focused_surface) {
2206 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2207 if (shsurf)
2208 shell_surface_lose_keyboard_focus(shsurf);
2209 }
2210
2211 seat->focused_surface = keyboard->focus;
2212
2213 if (seat->focused_surface) {
2214 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2215 if (shsurf)
2216 shell_surface_gain_keyboard_focus(shsurf);
2217 }
2218}
2219
2220static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002221shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002222{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002223 if (sc->ping_serial != serial)
2224 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002225
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002226 sc->unresponsive = 0;
2227 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002228
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002229 if (sc->ping_timer) {
2230 wl_event_source_remove(sc->ping_timer);
2231 sc->ping_timer = NULL;
2232 }
2233
2234}
2235
2236static void
2237shell_surface_pong(struct wl_client *client,
2238 struct wl_resource *resource, uint32_t serial)
2239{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002240 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2241 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002242
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002243 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002244}
2245
2246static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002247set_title(struct shell_surface *shsurf, const char *title)
2248{
2249 free(shsurf->title);
2250 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002251 shsurf->surface->timeline.force_refresh = 1;
2252}
2253
2254static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002255set_pid(struct shell_surface *shsurf, pid_t pid)
2256{
2257 /* We have no use for it */
2258}
2259
2260static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002261set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2262{
2263 shsurf->type = t;
2264 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002265}
2266
2267static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002268set_window_geometry(struct shell_surface *shsurf,
2269 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002270{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002271 shsurf->next_geometry.x = x;
2272 shsurf->next_geometry.y = y;
2273 shsurf->next_geometry.width = width;
2274 shsurf->next_geometry.height = height;
2275 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002276}
2277
2278static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002279shell_surface_set_title(struct wl_client *client,
2280 struct wl_resource *resource, const char *title)
2281{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002282 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002283
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002284 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002285}
2286
2287static void
2288shell_surface_set_class(struct wl_client *client,
2289 struct wl_resource *resource, const char *class)
2290{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002291 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002292
2293 free(shsurf->class);
2294 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002295 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002296}
2297
Alex Wu4539b082012-03-01 12:57:46 +08002298static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002299restore_output_mode(struct weston_output *output)
2300{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002301 if (output->original_mode)
2302 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002303}
2304
2305static void
2306restore_all_output_modes(struct weston_compositor *compositor)
2307{
2308 struct weston_output *output;
2309
2310 wl_list_for_each(output, &compositor->output_list, link)
2311 restore_output_mode(output);
2312}
2313
Philip Withnall07926d92013-11-25 18:01:40 +00002314/* The surface will be inserted into the list immediately after the link
2315 * returned by this function (i.e. will be stacked immediately above the
2316 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002317static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002318shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2319{
2320 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002321 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002322
2323 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002324 case SHELL_SURFACE_XWAYLAND:
2325 return &shsurf->shell->fullscreen_layer.view_list;
2326
2327 case SHELL_SURFACE_NONE:
2328 return NULL;
2329
Kristian Høgsbergead52422014-01-01 13:51:52 -08002330 case SHELL_SURFACE_POPUP:
2331 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002332 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002333 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002334 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002335 /* Move the surface to its parent layer so
2336 * that surfaces which are transient for
2337 * fullscreen surfaces don't get hidden by the
2338 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002339
2340 /* TODO: Handle a parent with multiple views */
2341 parent = get_default_view(shsurf->parent);
2342 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002343 return container_of(parent->layer_link.link.prev,
2344 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002345 }
Philip Withnall07926d92013-11-25 18:01:40 +00002346
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002347 /* Move the surface to a normal workspace layer so that surfaces
2348 * which were previously fullscreen or transient are no longer
2349 * rendered on top. */
2350 ws = get_current_workspace(shsurf->shell);
2351 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002352 }
2353
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002354 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002355}
2356
Philip Withnall648a4dd2013-11-25 18:01:44 +00002357static void
2358shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2359{
2360 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002361 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002362
2363 /* Move the child layers to the same workspace as shsurf. They will be
2364 * stacked above shsurf. */
2365 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002366 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002367 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002368 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002369 prev = container_of(shsurf->view->layer_link.link.prev,
2370 struct weston_layer_entry, link);
2371 weston_layer_entry_remove(&child->view->layer_link);
2372 weston_layer_entry_insert(prev,
2373 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002374 weston_view_geometry_dirty(child->view);
2375 weston_surface_damage(child->surface);
2376
2377 /* Recurse. We don’t expect this to recurse very far (if
2378 * at all) because that would imply we have transient
2379 * (or popup) children of transient surfaces, which
2380 * would be unusual. */
2381 shell_surface_update_child_surface_layers(child);
2382 }
2383 }
2384}
2385
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002386/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002387 * geometry to ensure the changes are redrawn.
2388 *
2389 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2390 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002391static void
2392shell_surface_update_layer(struct shell_surface *shsurf)
2393{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002394 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002395
2396 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2397
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002398 if (new_layer_link == NULL)
2399 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002400 if (new_layer_link == &shsurf->view->layer_link)
2401 return;
2402
2403 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002404 weston_layer_entry_remove(&shsurf->view->layer_link);
2405 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002406 weston_view_geometry_dirty(shsurf->view);
2407 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002408
2409 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002410}
2411
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002412static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002413shell_surface_set_parent(struct shell_surface *shsurf,
2414 struct weston_surface *parent)
2415{
2416 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002417
2418 wl_list_remove(&shsurf->children_link);
2419 wl_list_init(&shsurf->children_link);
2420
2421 /* Insert into the parent surface’s child list. */
2422 if (parent != NULL) {
2423 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2424 if (parent_shsurf != NULL)
2425 wl_list_insert(&parent_shsurf->children_list,
2426 &shsurf->children_link);
2427 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002428}
2429
2430static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002431shell_surface_set_output(struct shell_surface *shsurf,
2432 struct weston_output *output)
2433{
2434 struct weston_surface *es = shsurf->surface;
2435
2436 /* get the default output, if the client set it as NULL
2437 check whether the ouput is available */
2438 if (output)
2439 shsurf->output = output;
2440 else if (es->output)
2441 shsurf->output = es->output;
2442 else
2443 shsurf->output = get_default_output(es->compositor);
2444}
2445
2446static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002447surface_clear_next_states(struct shell_surface *shsurf)
2448{
2449 shsurf->next_state.maximized = false;
2450 shsurf->next_state.fullscreen = false;
2451
2452 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2453 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2454 shsurf->state_changed = true;
2455}
2456
2457static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002458set_toplevel(struct shell_surface *shsurf)
2459{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002460 shell_surface_set_parent(shsurf, NULL);
2461 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002462 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002463
2464 /* The layer_link is updated in set_surface_type(),
2465 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002466}
2467
2468static void
2469shell_surface_set_toplevel(struct wl_client *client,
2470 struct wl_resource *resource)
2471{
2472 struct shell_surface *surface = wl_resource_get_user_data(resource);
2473
2474 set_toplevel(surface);
2475}
2476
2477static void
2478set_transient(struct shell_surface *shsurf,
2479 struct weston_surface *parent, int x, int y, uint32_t flags)
2480{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002481 assert(parent != NULL);
2482
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002483 shell_surface_set_parent(shsurf, parent);
2484
2485 surface_clear_next_states(shsurf);
2486
Philip Withnallbecb77e2013-11-25 18:01:30 +00002487 shsurf->transient.x = x;
2488 shsurf->transient.y = y;
2489 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002490
Rafael Antognollied207b42013-12-03 15:35:43 -02002491 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002492 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002493 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002494
2495 /* The layer_link is updated in set_surface_type(),
2496 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002497}
2498
2499static void
2500shell_surface_set_transient(struct wl_client *client,
2501 struct wl_resource *resource,
2502 struct wl_resource *parent_resource,
2503 int x, int y, uint32_t flags)
2504{
2505 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2506 struct weston_surface *parent =
2507 wl_resource_get_user_data(parent_resource);
2508
2509 set_transient(shsurf, parent, x, y, flags);
2510}
2511
2512static void
2513set_fullscreen(struct shell_surface *shsurf,
2514 uint32_t method,
2515 uint32_t framerate,
2516 struct weston_output *output)
2517{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002518 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002519 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002520
2521 shsurf->fullscreen_output = shsurf->output;
2522 shsurf->fullscreen.type = method;
2523 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002524
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002525 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002526}
2527
2528static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002529weston_view_set_initial_position(struct weston_view *view,
2530 struct desktop_shell *shell);
2531
2532static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002533unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002534{
Philip Withnallf85fe842013-11-25 18:01:37 +00002535 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002536 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2537 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002538 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002539 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002540
Alex Wu4539b082012-03-01 12:57:46 +08002541 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2542 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002543
Alex Wu4539b082012-03-01 12:57:46 +08002544 wl_list_remove(&shsurf->fullscreen.transform.link);
2545 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002546
Jason Ekstranda7af7042013-10-12 22:38:11 -05002547 if (shsurf->fullscreen.black_view)
2548 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2549 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002550
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002551 if (shsurf->saved_position_valid)
2552 weston_view_set_position(shsurf->view,
2553 shsurf->saved_x, shsurf->saved_y);
2554 else
2555 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2556
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002557 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002558 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002559 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002560 shsurf->saved_rotation_valid = false;
2561 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002562
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002563 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002564}
2565
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002566static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002567shell_surface_set_fullscreen(struct wl_client *client,
2568 struct wl_resource *resource,
2569 uint32_t method,
2570 uint32_t framerate,
2571 struct wl_resource *output_resource)
2572{
2573 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2574 struct weston_output *output;
2575
2576 if (output_resource)
2577 output = wl_resource_get_user_data(output_resource);
2578 else
2579 output = NULL;
2580
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002581 shell_surface_set_parent(shsurf, NULL);
2582
Rafael Antognolli03b16592013-12-03 15:35:42 -02002583 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002584 shsurf->next_state.fullscreen = true;
2585 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002586 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002587}
2588
2589static void
2590set_popup(struct shell_surface *shsurf,
2591 struct weston_surface *parent,
2592 struct weston_seat *seat,
2593 uint32_t serial,
2594 int32_t x,
2595 int32_t y)
2596{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002597 assert(parent != NULL);
2598
Philip Withnallbecb77e2013-11-25 18:01:30 +00002599 shsurf->popup.shseat = get_shell_seat(seat);
2600 shsurf->popup.serial = serial;
2601 shsurf->popup.x = x;
2602 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002603
Pekka Paalanenb5026542014-11-12 15:09:24 +02002604 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002605}
2606
2607static void
2608shell_surface_set_popup(struct wl_client *client,
2609 struct wl_resource *resource,
2610 struct wl_resource *seat_resource,
2611 uint32_t serial,
2612 struct wl_resource *parent_resource,
2613 int32_t x, int32_t y, uint32_t flags)
2614{
2615 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002616 struct weston_surface *parent =
2617 wl_resource_get_user_data(parent_resource);
2618
2619 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002620
Rafael Antognolli03b16592013-12-03 15:35:42 -02002621 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002622 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002623 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002624 wl_resource_get_user_data(seat_resource),
2625 serial, x, y);
2626}
2627
2628static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002629unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002630{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002631 /* undo all maximized things here */
2632 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002633
2634 if (shsurf->saved_position_valid)
2635 weston_view_set_position(shsurf->view,
2636 shsurf->saved_x, shsurf->saved_y);
2637 else
2638 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002639
2640 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002641 wl_list_insert(&shsurf->view->geometry.transformation_list,
2642 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002643 shsurf->saved_rotation_valid = false;
2644 }
2645
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002646 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002647}
2648
Philip Withnallbecb77e2013-11-25 18:01:30 +00002649static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002650set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002651{
2652 struct shell_surface *shsurf;
2653 struct workspace *current_ws;
2654 struct weston_seat *seat;
2655 struct weston_surface *focus;
2656 struct weston_view *view;
2657
2658 view = get_default_view(surface);
2659 if (!view)
2660 return;
2661
2662 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2663
2664 shsurf = get_shell_surface(surface);
2665 current_ws = get_current_workspace(shsurf->shell);
2666
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002667 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002668 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002669
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002670 drop_focus_state(shsurf->shell, current_ws, view->surface);
2671 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2672 if (!seat->keyboard)
2673 continue;
2674 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2675 if (focus == view->surface)
2676 weston_keyboard_set_focus(seat->keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002677 }
2678
2679 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002680 weston_view_damage_below(view);
2681}
2682
2683static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002684shell_surface_set_maximized(struct wl_client *client,
2685 struct wl_resource *resource,
2686 struct wl_resource *output_resource)
2687{
2688 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2689 struct weston_output *output;
2690
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002691 surface_clear_next_states(shsurf);
2692 shsurf->next_state.maximized = true;
2693 shsurf->state_changed = true;
2694
Pekka Paalanenb5026542014-11-12 15:09:24 +02002695 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002696 shell_surface_set_parent(shsurf, NULL);
2697
Philip Withnallbecb77e2013-11-25 18:01:30 +00002698 if (output_resource)
2699 output = wl_resource_get_user_data(output_resource);
2700 else
2701 output = NULL;
2702
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002703 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002704
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002705 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002706}
2707
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002708/* This is only ever called from set_surface_type(), so there’s no need to
2709 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002710static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002711reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002712{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002713 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002714 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002715 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002716 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002717
Pekka Paalanen98262232011-12-01 10:42:22 +02002718 return 0;
2719}
2720
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002721static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002722set_full_output(struct shell_surface *shsurf)
2723{
2724 shsurf->saved_x = shsurf->view->geometry.x;
2725 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002726 shsurf->saved_width = shsurf->surface->width;
2727 shsurf->saved_height = shsurf->surface->height;
2728 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002729 shsurf->saved_position_valid = true;
2730
2731 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2732 wl_list_remove(&shsurf->rotation.transform.link);
2733 wl_list_init(&shsurf->rotation.transform.link);
2734 weston_view_geometry_dirty(shsurf->view);
2735 shsurf->saved_rotation_valid = true;
2736 }
2737}
2738
2739static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002740set_surface_type(struct shell_surface *shsurf)
2741{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002742 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002743 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002744
Philip Withnallbecb77e2013-11-25 18:01:30 +00002745 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002746
Rafael Antognolli03b16592013-12-03 15:35:42 -02002747 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002748 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002749
2750 switch (shsurf->type) {
2751 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002752 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002753 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002754 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002755 weston_view_set_position(shsurf->view,
2756 pev->geometry.x + shsurf->transient.x,
2757 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002758 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002759 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002760
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002761 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002762 weston_view_set_position(shsurf->view, shsurf->transient.x,
2763 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002764 break;
2765
Philip Withnall0f640e22013-11-25 18:01:31 +00002766 case SHELL_SURFACE_POPUP:
2767 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002768 default:
2769 break;
2770 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002771
2772 /* Update the surface’s layer. */
2773 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002774}
2775
Tiago Vignattibe143262012-04-16 17:31:41 +03002776static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002777shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002778{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002779 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002780}
2781
Pekka Paalanen8274d902014-08-06 19:36:51 +03002782static int
2783black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2784{
2785 struct weston_surface *fs_surface = surface->configure_private;
2786 int n;
2787 int rem;
2788 int ret;
2789
2790 n = snprintf(buf, len, "black background surface for ");
2791 if (n < 0)
2792 return n;
2793
2794 rem = (int)len - n;
2795 if (rem < 0)
2796 rem = 0;
2797
2798 if (fs_surface->get_label)
2799 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2800 else
2801 ret = snprintf(buf + n, rem, "<unknown>");
2802
2803 if (ret < 0)
2804 return n;
2805
2806 return n + ret;
2807}
2808
Alex Wu21858432012-04-01 20:13:08 +08002809static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002810black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002811
Jason Ekstranda7af7042013-10-12 22:38:11 -05002812static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002813create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002814 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002815 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002816{
2817 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002818 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002819
2820 surface = weston_surface_create(ec);
2821 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002822 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002823 return NULL;
2824 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 view = weston_view_create(surface);
2826 if (surface == NULL) {
2827 weston_log("no memory\n");
2828 weston_surface_destroy(surface);
2829 return NULL;
2830 }
Alex Wu4539b082012-03-01 12:57:46 +08002831
Alex Wu21858432012-04-01 20:13:08 +08002832 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002833 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002834 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002835 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002836 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002837 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002838 pixman_region32_fini(&surface->input);
2839 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002840
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002841 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002842 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002843
2844 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002845}
2846
Philip Withnalled8f1a92013-11-25 18:01:43 +00002847static void
2848shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2849{
2850 struct weston_output *output = shsurf->fullscreen_output;
2851
Rafael Antognolli03b16592013-12-03 15:35:42 -02002852 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002853
2854 if (!shsurf->fullscreen.black_view)
2855 shsurf->fullscreen.black_view =
2856 create_black_surface(shsurf->surface->compositor,
2857 shsurf->surface,
2858 output->x, output->y,
2859 output->width,
2860 output->height);
2861
2862 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002863 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2864 weston_layer_entry_insert(&shsurf->view->layer_link,
2865 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002866 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2867 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002868
2869 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002870}
2871
Alex Wu4539b082012-03-01 12:57:46 +08002872/* Create black surface and append it to the associated fullscreen surface.
2873 * Handle size dismatch and positioning according to the method. */
2874static void
2875shell_configure_fullscreen(struct shell_surface *shsurf)
2876{
2877 struct weston_output *output = shsurf->fullscreen_output;
2878 struct weston_surface *surface = shsurf->surface;
2879 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002880 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002881 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002882
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002883 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2884 restore_output_mode(output);
2885
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002886 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002887 weston_layer_entry_remove(&shsurf->view->layer_link);
2888 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002889
Philip Withnalled8f1a92013-11-25 18:01:43 +00002890 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002891
Jason Ekstranda7af7042013-10-12 22:38:11 -05002892 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002893 &surf_width, &surf_height);
2894
Alex Wu4539b082012-03-01 12:57:46 +08002895 switch (shsurf->fullscreen.type) {
2896 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002897 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002898 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002899 break;
2900 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002901 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002902 if (output->width == surf_width &&
2903 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002904 weston_view_set_position(shsurf->view,
2905 output->x - surf_x,
2906 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002907 break;
2908 }
2909
Alex Wu4539b082012-03-01 12:57:46 +08002910 matrix = &shsurf->fullscreen.transform.matrix;
2911 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002912
Scott Moreau1bad5db2012-08-18 01:04:05 -06002913 output_aspect = (float) output->width /
2914 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002915 /* XXX: Use surf_width and surf_height here? */
2916 surface_aspect = (float) surface->width /
2917 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002918 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002919 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002920 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002921 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002922 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002923 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002924
Alex Wu4539b082012-03-01 12:57:46 +08002925 weston_matrix_scale(matrix, scale, scale, 1);
2926 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002927 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002928 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002929 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2930 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002931 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002932
Alex Wu4539b082012-03-01 12:57:46 +08002933 break;
2934 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002935 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002936 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002937 surf_width * surface->buffer_viewport.buffer.scale,
2938 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002939 shsurf->fullscreen.framerate};
2940
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002941 if (weston_output_mode_switch_to_temporary(output, &mode,
2942 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002943 weston_view_set_position(shsurf->view,
2944 output->x - surf_x,
2945 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002946 shsurf->fullscreen.black_view->surface->width = output->width;
2947 shsurf->fullscreen.black_view->surface->height = output->height;
2948 weston_view_set_position(shsurf->fullscreen.black_view,
2949 output->x - surf_x,
2950 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002951 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002952 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002953 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002954 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002955 }
Alex Wubd3354b2012-04-17 17:20:49 +08002956 }
Alex Wu4539b082012-03-01 12:57:46 +08002957 break;
2958 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002959 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002960 break;
2961 default:
2962 break;
2963 }
2964}
2965
Alex Wu4539b082012-03-01 12:57:46 +08002966static void
2967shell_map_fullscreen(struct shell_surface *shsurf)
2968{
Alex Wubd3354b2012-04-17 17:20:49 +08002969 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002970}
2971
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002972static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002973set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2974{
2975 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002976 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002977 shsurf->transient.x = x;
2978 shsurf->transient.y = y;
2979 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002980
2981 shell_surface_set_parent(shsurf, NULL);
2982
Pekka Paalanenb5026542014-11-12 15:09:24 +02002983 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002984 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002985}
2986
Kristian Høgsberg47792412014-05-04 13:47:06 -07002987static void
2988shell_interface_set_fullscreen(struct shell_surface *shsurf,
2989 uint32_t method,
2990 uint32_t framerate,
2991 struct weston_output *output)
2992{
2993 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002994 shsurf->next_state.fullscreen = true;
2995 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002996
2997 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002998}
2999
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003000static struct weston_output *
3001get_focused_output(struct weston_compositor *compositor)
3002{
3003 struct weston_seat *seat;
3004 struct weston_output *output = NULL;
3005
3006 wl_list_for_each(seat, &compositor->seat_list, link) {
3007 /* Priority has touch focus, then pointer and
3008 * then keyboard focus. We should probably have
3009 * three for loops and check frist for touch,
3010 * then for pointer, etc. but unless somebody has some
3011 * objections, I think this is sufficient. */
3012 if (seat->touch && seat->touch->focus)
3013 output = seat->touch->focus->output;
3014 else if (seat->pointer && seat->pointer->focus)
3015 output = seat->pointer->focus->output;
3016 else if (seat->keyboard && seat->keyboard->focus)
3017 output = seat->keyboard->focus->output;
3018
3019 if (output)
3020 break;
3021 }
3022
3023 return output;
3024}
3025
3026static void
3027shell_interface_set_maximized(struct shell_surface *shsurf)
3028{
3029 struct weston_output *output;
3030
3031 surface_clear_next_states(shsurf);
3032 shsurf->next_state.maximized = true;
3033 shsurf->state_changed = true;
3034 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3035
3036 if (!weston_surface_is_mapped(shsurf->surface))
3037 output = get_focused_output(shsurf->surface->compositor);
3038 else
3039 output = shsurf->surface->output;
3040
3041 shell_surface_set_output(shsurf, output);
3042 send_configure_for_surface(shsurf);
3043}
3044
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003045static int
3046shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3047{
3048 return surface_move(shsurf, ws, 1);
3049}
3050
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003051static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003052
3053static void
3054destroy_shell_seat(struct wl_listener *listener, void *data)
3055{
3056 struct shell_seat *shseat =
3057 container_of(listener,
3058 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003059 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003060
3061 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003062 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003063 shseat->popup_grab.client = NULL;
3064
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003065 wl_list_for_each_safe(shsurf, next,
3066 &shseat->popup_grab.surfaces_list,
3067 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003068 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003069 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003070 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003071 }
3072
3073 wl_list_remove(&shseat->seat_destroy_listener.link);
3074 free(shseat);
3075}
3076
Jason Ekstrand024177c2014-04-21 19:42:58 -05003077static void
3078shell_seat_caps_changed(struct wl_listener *listener, void *data)
3079{
3080 struct shell_seat *seat;
3081
3082 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3083
3084 if (seat->seat->keyboard &&
3085 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3086 wl_signal_add(&seat->seat->keyboard->focus_signal,
3087 &seat->keyboard_focus_listener);
3088 } else if (!seat->seat->keyboard) {
3089 wl_list_init(&seat->keyboard_focus_listener.link);
3090 }
3091
3092 if (seat->seat->pointer &&
3093 wl_list_empty(&seat->pointer_focus_listener.link)) {
3094 wl_signal_add(&seat->seat->pointer->focus_signal,
3095 &seat->pointer_focus_listener);
3096 } else if (!seat->seat->pointer) {
3097 wl_list_init(&seat->pointer_focus_listener.link);
3098 }
3099}
3100
Giulio Camuffo5085a752013-03-25 21:42:45 +01003101static struct shell_seat *
3102create_shell_seat(struct weston_seat *seat)
3103{
3104 struct shell_seat *shseat;
3105
3106 shseat = calloc(1, sizeof *shseat);
3107 if (!shseat) {
3108 weston_log("no memory to allocate shell seat\n");
3109 return NULL;
3110 }
3111
3112 shseat->seat = seat;
3113 wl_list_init(&shseat->popup_grab.surfaces_list);
3114
3115 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3116 wl_signal_add(&seat->destroy_signal,
3117 &shseat->seat_destroy_listener);
3118
Jason Ekstrand024177c2014-04-21 19:42:58 -05003119 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3120 wl_list_init(&shseat->keyboard_focus_listener.link);
3121
3122 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3123 wl_list_init(&shseat->pointer_focus_listener.link);
3124
3125 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3126 wl_signal_add(&seat->updated_caps_signal,
3127 &shseat->caps_changed_listener);
3128 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3129
Giulio Camuffo5085a752013-03-25 21:42:45 +01003130 return shseat;
3131}
3132
3133static struct shell_seat *
3134get_shell_seat(struct weston_seat *seat)
3135{
3136 struct wl_listener *listener;
3137
3138 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003139 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003140
3141 return container_of(listener,
3142 struct shell_seat, seat_destroy_listener);
3143}
3144
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003145static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003146popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003147{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003148 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003149 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003150 struct shell_seat *shseat =
3151 container_of(grab, struct shell_seat, popup_grab.grab);
3152 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003153 wl_fixed_t sx, sy;
3154
Jason Ekstranda7af7042013-10-12 22:38:11 -05003155 view = weston_compositor_pick_view(pointer->seat->compositor,
3156 pointer->x, pointer->y,
3157 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003158
Jason Ekstranda7af7042013-10-12 22:38:11 -05003159 if (view && view->surface->resource &&
3160 wl_resource_get_client(view->surface->resource) == client) {
3161 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003162 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003163 weston_pointer_set_focus(pointer, NULL,
3164 wl_fixed_from_int(0),
3165 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003166 }
3167}
3168
3169static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003170popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3171 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003172{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003173 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003174 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003175 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003176
Jonas Ådahl61917c82014-08-11 22:44:02 +02003177 if (pointer->focus) {
3178 weston_view_from_global_fixed(pointer->focus, x, y,
3179 &pointer->sx, &pointer->sy);
3180 }
3181
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003182 weston_pointer_move(pointer, x, y);
3183
Neil Roberts96d790e2013-09-19 17:32:00 +01003184 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003185 weston_view_from_global_fixed(pointer->focus,
3186 pointer->x, pointer->y,
3187 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003188 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003189 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003190}
3191
3192static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003193popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003194 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003195{
3196 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003197 struct shell_seat *shseat =
3198 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003199 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003200 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003201 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003202 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003203
Neil Roberts96d790e2013-09-19 17:32:00 +01003204 resource_list = &grab->pointer->focus_resource_list;
3205 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003206 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003207 wl_resource_for_each(resource, resource_list) {
3208 wl_pointer_send_button(resource, serial,
3209 time, button, state);
3210 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003211 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003212 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003213 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003214 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003215 }
3216
Daniel Stone4dbadb12012-05-30 16:31:51 +01003217 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003218 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003219}
3220
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003221static void
3222popup_grab_cancel(struct weston_pointer_grab *grab)
3223{
3224 popup_grab_end(grab->pointer);
3225}
3226
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003227static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003228 popup_grab_focus,
3229 popup_grab_motion,
3230 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003231 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003232};
3233
3234static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003235touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3236 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3237{
3238 struct wl_resource *resource;
3239 struct shell_seat *shseat =
3240 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3241 struct wl_display *display = shseat->seat->compositor->wl_display;
3242 uint32_t serial;
3243 struct wl_list *resource_list;
3244
3245 resource_list = &grab->touch->focus_resource_list;
3246 if (!wl_list_empty(resource_list)) {
3247 serial = wl_display_get_serial(display);
3248 wl_resource_for_each(resource, resource_list) {
3249 wl_touch_send_down(resource, serial, time,
3250 grab->touch->focus->surface->resource,
3251 touch_id, sx, sy);
3252 }
3253 }
3254}
3255
3256static void
3257touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3258{
3259 struct wl_resource *resource;
3260 struct shell_seat *shseat =
3261 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3262 struct wl_display *display = shseat->seat->compositor->wl_display;
3263 uint32_t serial;
3264 struct wl_list *resource_list;
3265
3266 resource_list = &grab->touch->focus_resource_list;
3267 if (!wl_list_empty(resource_list)) {
3268 serial = wl_display_get_serial(display);
3269 wl_resource_for_each(resource, resource_list) {
3270 wl_touch_send_up(resource, serial, time, touch_id);
3271 }
3272 }
3273}
3274
3275static void
3276touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3277 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3278{
3279 struct wl_resource *resource;
3280 struct wl_list *resource_list;
3281
3282 resource_list = &grab->touch->focus_resource_list;
3283 if (!wl_list_empty(resource_list)) {
3284 wl_resource_for_each(resource, resource_list) {
3285 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3286 }
3287 }
3288}
3289
3290static void
3291touch_popup_grab_frame(struct weston_touch_grab *grab)
3292{
3293}
3294
3295static void
3296touch_popup_grab_cancel(struct weston_touch_grab *grab)
3297{
3298 touch_popup_grab_end(grab->touch);
3299}
3300
3301static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3302 touch_popup_grab_down,
3303 touch_popup_grab_up,
3304 touch_popup_grab_motion,
3305 touch_popup_grab_frame,
3306 touch_popup_grab_cancel,
3307};
3308
3309static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003310shell_surface_send_popup_done(struct shell_surface *shsurf)
3311{
3312 if (shell_surface_is_wl_shell_surface(shsurf))
3313 wl_shell_surface_send_popup_done(shsurf->resource);
3314 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003315 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003316}
3317
3318static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003319popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003320{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003321 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003322 struct shell_seat *shseat =
3323 container_of(grab, struct shell_seat, popup_grab.grab);
3324 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003325 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003326
3327 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003328 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003329 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003330 shseat->popup_grab.grab.interface = NULL;
3331 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003332 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003333 wl_list_for_each_safe(shsurf, next,
3334 &shseat->popup_grab.surfaces_list,
3335 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003336 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003337 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003338 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003339 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003340 wl_list_init(&shseat->popup_grab.surfaces_list);
3341 }
3342}
3343
3344static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003345touch_popup_grab_end(struct weston_touch *touch)
3346{
3347 struct weston_touch_grab *grab = touch->grab;
3348 struct shell_seat *shseat =
3349 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3350 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003351 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003352
3353 if (touch->grab->interface == &touch_popup_grab_interface) {
3354 weston_touch_end_grab(grab->touch);
3355 shseat->popup_grab.client = NULL;
3356 shseat->popup_grab.touch_grab.interface = NULL;
3357 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3358 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003359 wl_list_for_each_safe(shsurf, next,
3360 &shseat->popup_grab.surfaces_list,
3361 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003362 shell_surface_send_popup_done(shsurf);
3363 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003364 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003365 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003366 wl_list_init(&shseat->popup_grab.surfaces_list);
3367 }
3368}
3369
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003370static struct shell_surface *
3371get_top_popup(struct shell_seat *shseat)
3372{
3373 struct shell_surface *shsurf;
3374
3375 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3376 return NULL;
3377 } else {
3378 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3379 struct shell_surface,
3380 popup.grab_link);
3381 return shsurf;
3382 }
3383}
3384
3385static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003386add_popup_grab(struct shell_surface *shsurf,
3387 struct shell_seat *shseat,
3388 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003389{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003390 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003391 struct shell_surface *parent, *top_surface;
3392
3393 parent = get_shell_surface(shsurf->parent);
3394 top_surface = get_top_popup(shseat);
3395 if (shell_surface_is_xdg_popup(shsurf) &&
3396 ((top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
3397 (top_surface != NULL && parent != top_surface))) {
3398 wl_resource_post_error(shsurf->owner->resource,
3399 XDG_POPUP_ERROR_NOT_THE_TOPMOST_POPUP,
3400 "xdg_popup was not created on the "
3401 "topmost popup");
3402 return -1;
3403 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003404
3405 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003406 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003407 shseat->popup_grab.client =
3408 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003409
3410 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003411 shseat->popup_grab.grab.interface =
3412 &popup_grab_interface;
3413
3414 /* We must make sure here that this popup was opened
3415 * after a mouse press, and not just by moving around
3416 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003417 if (shseat->seat->pointer->button_count > 0)
3418 shseat->popup_grab.initial_up = 0;
3419 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003420 shseat->popup_grab.touch_grab.interface =
3421 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003422 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003423
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003424 wl_list_insert(&shseat->popup_grab.surfaces_list,
3425 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003426
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003427 if (type == POINTER) {
3428 weston_pointer_start_grab(seat->pointer,
3429 &shseat->popup_grab.grab);
3430 } else if (type == TOUCH) {
3431 weston_touch_start_grab(seat->touch,
3432 &shseat->popup_grab.touch_grab);
3433 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003434 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003435 wl_list_insert(&shseat->popup_grab.surfaces_list,
3436 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003437 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003438
3439 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003440}
3441
3442static void
3443remove_popup_grab(struct shell_surface *shsurf)
3444{
3445 struct shell_seat *shseat = shsurf->popup.shseat;
3446
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003447 if (shell_surface_is_xdg_popup(shsurf) &&
3448 get_top_popup(shseat) != shsurf) {
3449 wl_resource_post_error(shsurf->resource,
3450 XDG_POPUP_ERROR_NOT_THE_TOPMOST_POPUP,
3451 "xdg_popup was destroyed while it was "
3452 "not the topmost popup.");
3453 return;
3454 }
3455
Giulio Camuffo5085a752013-03-25 21:42:45 +01003456 wl_list_remove(&shsurf->popup.grab_link);
3457 wl_list_init(&shsurf->popup.grab_link);
3458 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003459 if (shseat->popup_grab.type == POINTER) {
3460 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3461 shseat->popup_grab.grab.interface = NULL;
3462 } else if (shseat->popup_grab.type == TOUCH) {
3463 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3464 shseat->popup_grab.touch_grab.interface = NULL;
3465 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003466 }
3467}
3468
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003469static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003470shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003471{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003472 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003473 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003474
Jason Ekstranda7af7042013-10-12 22:38:11 -05003475 shsurf->surface->output = parent_view->output;
3476 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003477
Jason Ekstranda7af7042013-10-12 22:38:11 -05003478 weston_view_set_transform_parent(shsurf->view, parent_view);
3479 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3480 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003481
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003482 if (shseat->seat->pointer &&
3483 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003484 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3485 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003486 } else if (shseat->seat->touch &&
3487 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003488 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3489 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003490 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003491 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003492 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003493 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003494
3495 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003496}
3497
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003498static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003499 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003500 shell_surface_move,
3501 shell_surface_resize,
3502 shell_surface_set_toplevel,
3503 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003504 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003505 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003506 shell_surface_set_maximized,
3507 shell_surface_set_title,
3508 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003509};
3510
3511static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003512destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003513{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003514 struct shell_surface *child, *next;
3515
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003516 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3517
Giulio Camuffo5085a752013-03-25 21:42:45 +01003518 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3519 remove_popup_grab(shsurf);
3520 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003521
Alex Wubd3354b2012-04-17 17:20:49 +08003522 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003523 shell_surface_is_top_fullscreen(shsurf))
3524 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003525
Jason Ekstranda7af7042013-10-12 22:38:11 -05003526 if (shsurf->fullscreen.black_view)
3527 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003528
Alex Wubd3354b2012-04-17 17:20:49 +08003529 /* As destroy_resource() use wl_list_for_each_safe(),
3530 * we can always remove the listener.
3531 */
3532 wl_list_remove(&shsurf->surface_destroy_listener.link);
3533 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003534 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003535 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003536
Jason Ekstranda7af7042013-10-12 22:38:11 -05003537 weston_view_destroy(shsurf->view);
3538
Philip Withnall648a4dd2013-11-25 18:01:44 +00003539 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003540 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3541 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003542
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003543 wl_list_remove(&shsurf->link);
3544 free(shsurf);
3545}
3546
3547static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003548shell_destroy_shell_surface(struct wl_resource *resource)
3549{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003550 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003551
Bryan Caina46b9462014-04-04 17:41:24 -05003552 if (!wl_list_empty(&shsurf->popup.grab_link))
3553 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003554 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003555}
3556
3557static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003558shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003559{
3560 struct shell_surface *shsurf = container_of(listener,
3561 struct shell_surface,
3562 surface_destroy_listener);
3563
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003564 if (shsurf->resource)
3565 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003566
3567 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003568}
3569
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003570static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003571fade_out_done(struct weston_view_animation *animation, void *data)
3572{
3573 struct shell_surface *shsurf = data;
3574
3575 weston_surface_destroy(shsurf->surface);
3576}
3577
3578static void
3579handle_resource_destroy(struct wl_listener *listener, void *data)
3580{
3581 struct shell_surface *shsurf =
3582 container_of(listener, struct shell_surface,
3583 resource_destroy_listener);
3584
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003585 if (!weston_surface_is_mapped(shsurf->surface))
3586 return;
3587
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003588 shsurf->surface->ref_count++;
3589
3590 pixman_region32_fini(&shsurf->surface->pending.input);
3591 pixman_region32_init(&shsurf->surface->pending.input);
3592 pixman_region32_fini(&shsurf->surface->input);
3593 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003594 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3595 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3596 fade_out_done, shsurf);
3597 } else {
3598 weston_surface_destroy(shsurf->surface);
3599 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003600}
3601
3602static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003603shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003604
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003605struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003606get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003607{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003608 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003609 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003610 else
3611 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003612}
3613
Rafael Antognollie2a34552013-12-03 15:35:45 -02003614static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003615create_common_surface(struct shell_client *owner, void *shell,
3616 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003617 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003618{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003619 struct shell_surface *shsurf;
3620
Pekka Paalanen50b67472014-10-01 15:02:41 +03003621 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003622
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003623 shsurf = calloc(1, sizeof *shsurf);
3624 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003625 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003626 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003627 }
3628
Jason Ekstranda7af7042013-10-12 22:38:11 -05003629 shsurf->view = weston_view_create(surface);
3630 if (!shsurf->view) {
3631 weston_log("no memory to allocate shell surface\n");
3632 free(shsurf);
3633 return NULL;
3634 }
3635
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003636 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003637 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003638 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003639
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003640 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3641 wl_resource_add_destroy_listener(surface->resource,
3642 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003643 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003644
Tiago Vignattibc052c92012-04-19 16:18:18 +03003645 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003646 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003647 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003648 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003649 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003650 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003651 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3652 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003653 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003654 wl_list_init(&shsurf->fullscreen.transform.link);
3655
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003656 shsurf->output = get_default_output(shsurf->shell->compositor);
3657
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003658 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003659 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003660 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003661 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003662
3663 /* init link so its safe to always remove it in destroy_shell_surface */
3664 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003665 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003666
Pekka Paalanen460099f2012-01-20 16:48:25 +02003667 /* empty when not in use */
3668 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003669 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003670
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003671 wl_list_init(&shsurf->workspace_transform.link);
3672
Philip Withnall648a4dd2013-11-25 18:01:44 +00003673 wl_list_init(&shsurf->children_link);
3674 wl_list_init(&shsurf->children_list);
3675 shsurf->parent = NULL;
3676
Pekka Paalanenb5026542014-11-12 15:09:24 +02003677 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003678
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003679 shsurf->client = client;
3680
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003681 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003682}
3683
Rafael Antognollie2a34552013-12-03 15:35:45 -02003684static struct shell_surface *
3685create_shell_surface(void *shell, struct weston_surface *surface,
3686 const struct weston_shell_client *client)
3687{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003688 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003689}
3690
Jason Ekstranda7af7042013-10-12 22:38:11 -05003691static struct weston_view *
3692get_primary_view(void *shell, struct shell_surface *shsurf)
3693{
3694 return shsurf->view;
3695}
3696
Tiago Vignattibc052c92012-04-19 16:18:18 +03003697static void
3698shell_get_shell_surface(struct wl_client *client,
3699 struct wl_resource *resource,
3700 uint32_t id,
3701 struct wl_resource *surface_resource)
3702{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003703 struct weston_surface *surface =
3704 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003705 struct shell_client *sc = wl_resource_get_user_data(resource);
3706 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003707 struct shell_surface *shsurf;
3708
Pekka Paalanen50b67472014-10-01 15:02:41 +03003709 if (weston_surface_set_role(surface, "wl_shell_surface",
3710 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003711 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003712
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003713 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003714 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003715 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003716 return;
3717 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003718
Jason Ekstranda85118c2013-06-27 20:17:02 -05003719 shsurf->resource =
3720 wl_resource_create(client,
3721 &wl_shell_surface_interface, 1, id);
3722 wl_resource_set_implementation(shsurf->resource,
3723 &shell_surface_implementation,
3724 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003725}
3726
Rafael Antognollie2a34552013-12-03 15:35:45 -02003727static bool
3728shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3729{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003730 /* A shell surface without a resource is created from xwayland
3731 * and is considered a wl_shell surface for now. */
3732
3733 return shsurf->resource == NULL ||
3734 wl_resource_instance_of(shsurf->resource,
3735 &wl_shell_surface_interface,
3736 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003737}
3738
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003739static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003740 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003741};
3742
Rafael Antognollie2a34552013-12-03 15:35:45 -02003743/****************************
3744 * xdg-shell implementation */
3745
3746static void
3747xdg_surface_destroy(struct wl_client *client,
3748 struct wl_resource *resource)
3749{
3750 wl_resource_destroy(resource);
3751}
3752
3753static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003754xdg_surface_set_parent(struct wl_client *client,
3755 struct wl_resource *resource,
3756 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003757{
3758 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003759 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003760
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003761 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003762 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003763 shell_surface_set_parent(shsurf, parent->surface);
3764 } else {
3765 shell_surface_set_parent(shsurf, NULL);
3766 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003767}
3768
3769static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003770xdg_surface_set_app_id(struct wl_client *client,
3771 struct wl_resource *resource,
3772 const char *app_id)
3773{
3774 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3775
3776 free(shsurf->class);
3777 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003778 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003779}
3780
3781static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003782xdg_surface_show_window_menu(struct wl_client *client,
3783 struct wl_resource *surface_resource,
3784 struct wl_resource *seat_resource,
3785 uint32_t serial,
3786 int32_t x,
3787 int32_t y)
3788{
3789 /* TODO */
3790}
3791
3792static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003793xdg_surface_set_title(struct wl_client *client,
3794 struct wl_resource *resource, const char *title)
3795{
3796 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3797
3798 set_title(shsurf, title);
3799}
3800
3801static void
3802xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3803 struct wl_resource *seat_resource, uint32_t serial)
3804{
3805 common_surface_move(resource, seat_resource, serial);
3806}
3807
3808static void
3809xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3810 struct wl_resource *seat_resource, uint32_t serial,
3811 uint32_t edges)
3812{
3813 common_surface_resize(resource, seat_resource, serial, edges);
3814}
3815
3816static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003817xdg_surface_ack_configure(struct wl_client *client,
3818 struct wl_resource *resource,
3819 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003820{
3821 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3822
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003823 if (shsurf->state_requested) {
3824 shsurf->next_state = shsurf->requested_state;
3825 shsurf->state_changed = true;
3826 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003827 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003828}
3829
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003830static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003831xdg_surface_set_window_geometry(struct wl_client *client,
3832 struct wl_resource *resource,
3833 int32_t x,
3834 int32_t y,
3835 int32_t width,
3836 int32_t height)
3837{
3838 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3839
3840 set_window_geometry(shsurf, x, y, width, height);
3841}
3842
3843static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003844xdg_surface_set_maximized(struct wl_client *client,
3845 struct wl_resource *resource)
3846{
3847 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003848 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003849
3850 shsurf->state_requested = true;
3851 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003852
3853 if (!weston_surface_is_mapped(shsurf->surface))
3854 output = get_focused_output(shsurf->surface->compositor);
3855 else
3856 output = shsurf->surface->output;
3857
3858 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003859 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003860}
3861
3862static void
3863xdg_surface_unset_maximized(struct wl_client *client,
3864 struct wl_resource *resource)
3865{
3866 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3867
3868 shsurf->state_requested = true;
3869 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003870 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003871}
3872
3873static void
3874xdg_surface_set_fullscreen(struct wl_client *client,
3875 struct wl_resource *resource,
3876 struct wl_resource *output_resource)
3877{
3878 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3879 struct weston_output *output;
3880
3881 shsurf->state_requested = true;
3882 shsurf->requested_state.fullscreen = true;
3883
3884 if (output_resource)
3885 output = wl_resource_get_user_data(output_resource);
3886 else
3887 output = NULL;
3888
Marek Chalupa052917a2014-09-02 11:35:12 +02003889 /* handle clients launching in fullscreen */
3890 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3891 /* Set the output to the one that has focus currently. */
3892 assert(shsurf->surface);
3893 output = get_focused_output(shsurf->surface->compositor);
3894 }
3895
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003896 shell_surface_set_output(shsurf, output);
3897 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003898
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003899 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003900}
3901
3902static void
3903xdg_surface_unset_fullscreen(struct wl_client *client,
3904 struct wl_resource *resource)
3905{
3906 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3907
3908 shsurf->state_requested = true;
3909 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003910 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003911}
3912
3913static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003914xdg_surface_set_minimized(struct wl_client *client,
3915 struct wl_resource *resource)
3916{
3917 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3918
3919 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3920 return;
3921
3922 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003923 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003924}
3925
Rafael Antognollie2a34552013-12-03 15:35:45 -02003926static const struct xdg_surface_interface xdg_surface_implementation = {
3927 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003928 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003929 xdg_surface_set_title,
3930 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003931 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003932 xdg_surface_move,
3933 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003934 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003935 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003936 xdg_surface_set_maximized,
3937 xdg_surface_unset_maximized,
3938 xdg_surface_set_fullscreen,
3939 xdg_surface_unset_fullscreen,
3940 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003941};
3942
3943static void
3944xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003945 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003946{
3947 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003948 uint32_t *s;
3949 struct wl_array states;
3950 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003951
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003952 assert(shsurf);
3953
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003954 if (!shsurf->resource)
3955 return;
3956
3957 wl_array_init(&states);
3958 if (shsurf->requested_state.fullscreen) {
3959 s = wl_array_add(&states, sizeof *s);
3960 *s = XDG_SURFACE_STATE_FULLSCREEN;
3961 } else if (shsurf->requested_state.maximized) {
3962 s = wl_array_add(&states, sizeof *s);
3963 *s = XDG_SURFACE_STATE_MAXIMIZED;
3964 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003965 if (shsurf->resize_edges != 0) {
3966 s = wl_array_add(&states, sizeof *s);
3967 *s = XDG_SURFACE_STATE_RESIZING;
3968 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003969 if (shsurf->focus_count > 0) {
3970 s = wl_array_add(&states, sizeof *s);
3971 *s = XDG_SURFACE_STATE_ACTIVATED;
3972 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003973
3974 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3975 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3976
3977 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003978}
3979
3980static const struct weston_shell_client xdg_client = {
3981 xdg_send_configure
3982};
3983
3984static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08003985xdg_shell_destroy(struct wl_client *client,
3986 struct wl_resource *resource)
3987{
3988 wl_resource_destroy(resource);
3989}
3990
3991static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003992xdg_use_unstable_version(struct wl_client *client,
3993 struct wl_resource *resource,
3994 int32_t version)
3995{
3996 if (version > 1) {
3997 wl_resource_post_error(resource,
3998 1,
3999 "xdg-shell:: version not implemented yet.");
4000 return;
4001 }
4002}
4003
4004static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004005create_xdg_surface(struct shell_client *owner, void *shell,
4006 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004007 const struct weston_shell_client *client)
4008{
4009 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004010
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004011 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004012 if (!shsurf)
4013 return NULL;
4014
Pekka Paalanenb5026542014-11-12 15:09:24 +02004015 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004016
4017 return shsurf;
4018}
4019
4020static void
4021xdg_get_xdg_surface(struct wl_client *client,
4022 struct wl_resource *resource,
4023 uint32_t id,
4024 struct wl_resource *surface_resource)
4025{
4026 struct weston_surface *surface =
4027 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004028 struct shell_client *sc = wl_resource_get_user_data(resource);
4029 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004030 struct shell_surface *shsurf;
4031
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004032 shsurf = get_shell_surface(surface);
4033 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4034 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4035 "This wl_surface is already an "
4036 "xdg_surface");
4037 return;
4038 }
4039
Pekka Paalanen50b67472014-10-01 15:02:41 +03004040 if (weston_surface_set_role(surface, "xdg_surface",
4041 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004042 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004043
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004044 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004045 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004046 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004047 return;
4048 }
4049
4050 shsurf->resource =
4051 wl_resource_create(client,
4052 &xdg_surface_interface, 1, id);
4053 wl_resource_set_implementation(shsurf->resource,
4054 &xdg_surface_implementation,
4055 shsurf, shell_destroy_shell_surface);
4056}
4057
4058static bool
4059shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4060{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004061 return shsurf->resource &&
4062 wl_resource_instance_of(shsurf->resource,
4063 &xdg_surface_interface,
4064 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004065}
4066
4067/* xdg-popup implementation */
4068
4069static void
4070xdg_popup_destroy(struct wl_client *client,
4071 struct wl_resource *resource)
4072{
4073 wl_resource_destroy(resource);
4074}
4075
Rafael Antognollie2a34552013-12-03 15:35:45 -02004076static const struct xdg_popup_interface xdg_popup_implementation = {
4077 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004078};
4079
4080static void
4081xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004082 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004083{
4084}
4085
4086static const struct weston_shell_client xdg_popup_client = {
4087 xdg_popup_send_configure
4088};
4089
4090static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004091create_xdg_popup(struct shell_client *owner, void *shell,
4092 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004093 const struct weston_shell_client *client,
4094 struct weston_surface *parent,
4095 struct shell_seat *seat,
4096 uint32_t serial,
4097 int32_t x, int32_t y)
4098{
Jonas Ådahl24185e22015-02-27 18:37:41 +08004099 struct shell_surface *shsurf, *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004100
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08004101 /* Verify that we are creating the top most popup when mapping,
4102 * as its not until then we know whether it was mapped as most
4103 * top level or not. */
4104
Jonas Ådahl24185e22015-02-27 18:37:41 +08004105 parent_shsurf = get_shell_surface(parent);
4106 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4107 !shell_surface_is_xdg_surface(parent_shsurf)) {
4108 wl_resource_post_error(owner->resource,
4109 XDG_POPUP_ERROR_INVALID_PARENT,
4110 "xdg_popup parent was invalid");
4111 return NULL;
4112 }
4113
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004114 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004115 if (!shsurf)
4116 return NULL;
4117
Pekka Paalanenb5026542014-11-12 15:09:24 +02004118 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004119 shsurf->popup.shseat = seat;
4120 shsurf->popup.serial = serial;
4121 shsurf->popup.x = x;
4122 shsurf->popup.y = y;
4123 shell_surface_set_parent(shsurf, parent);
4124
4125 return shsurf;
4126}
4127
4128static void
4129xdg_get_xdg_popup(struct wl_client *client,
4130 struct wl_resource *resource,
4131 uint32_t id,
4132 struct wl_resource *surface_resource,
4133 struct wl_resource *parent_resource,
4134 struct wl_resource *seat_resource,
4135 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004136 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004137{
4138 struct weston_surface *surface =
4139 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004140 struct shell_client *sc = wl_resource_get_user_data(resource);
4141 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004142 struct shell_surface *shsurf;
4143 struct weston_surface *parent;
4144 struct shell_seat *seat;
4145
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004146 shsurf = get_shell_surface(surface);
4147 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4148 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4149 "This wl_surface is already an "
4150 "xdg_popup");
4151 return;
4152 }
4153
Pekka Paalanen50b67472014-10-01 15:02:41 +03004154 if (weston_surface_set_role(surface, "xdg_popup",
4155 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004156 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004157
4158 if (!parent_resource) {
4159 wl_resource_post_error(surface_resource,
4160 WL_DISPLAY_ERROR_INVALID_OBJECT,
4161 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004162 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004163 }
4164
4165 parent = wl_resource_get_user_data(parent_resource);
4166 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4167
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004168 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004169 parent, seat, serial, x, y);
4170 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004171 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004172 return;
4173 }
4174
4175 shsurf->resource =
4176 wl_resource_create(client,
4177 &xdg_popup_interface, 1, id);
4178 wl_resource_set_implementation(shsurf->resource,
4179 &xdg_popup_implementation,
4180 shsurf, shell_destroy_shell_surface);
4181}
4182
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004183static void
4184xdg_pong(struct wl_client *client,
4185 struct wl_resource *resource, uint32_t serial)
4186{
4187 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004188
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004189 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004190}
4191
Rafael Antognollie2a34552013-12-03 15:35:45 -02004192static bool
4193shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4194{
4195 return wl_resource_instance_of(shsurf->resource,
4196 &xdg_popup_interface,
4197 &xdg_popup_implementation);
4198}
4199
4200static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004201 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004202 xdg_use_unstable_version,
4203 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004204 xdg_get_xdg_popup,
4205 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004206};
4207
4208static int
4209xdg_shell_unversioned_dispatch(const void *implementation,
4210 void *_target, uint32_t opcode,
4211 const struct wl_message *message,
4212 union wl_argument *args)
4213{
4214 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004215 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004216
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004217 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004218 wl_resource_post_error(resource,
4219 WL_DISPLAY_ERROR_INVALID_OBJECT,
4220 "must call use_unstable_version first");
4221 return 0;
4222 }
4223
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004224#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004225
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004226 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4227 "shell implementation doesn't match protocol version");
4228
Rafael Antognollie2a34552013-12-03 15:35:45 -02004229 if (args[0].i != XDG_SERVER_VERSION) {
4230 wl_resource_post_error(resource,
4231 WL_DISPLAY_ERROR_INVALID_OBJECT,
4232 "incompatible version, server is %d "
4233 "client wants %d",
4234 XDG_SERVER_VERSION, args[0].i);
4235 return 0;
4236 }
4237
4238 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004239 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004240
4241 return 1;
4242}
4243
4244/* end of xdg-shell implementation */
4245/***********************************/
4246
Kristian Høgsberg07937562011-04-12 17:25:42 -04004247static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004248shell_fade(struct desktop_shell *shell, enum fade_type type);
4249
4250static int
4251screensaver_timeout(void *data)
4252{
4253 struct desktop_shell *shell = data;
4254
4255 shell_fade(shell, FADE_OUT);
4256
4257 return 1;
4258}
4259
4260static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004261handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02004262{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004263 struct desktop_shell *shell =
4264 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004265
Pekka Paalanen18027e52011-12-02 16:31:49 +02004266 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004267
4268 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02004269 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02004270}
4271
4272static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004273launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004274{
4275 if (shell->screensaver.binding)
4276 return;
4277
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004278 if (!shell->screensaver.path) {
4279 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004280 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004281 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004282
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004283 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02004284 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004285 return;
4286 }
4287
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004288 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004289 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004290 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004291 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004292}
4293
4294static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004295terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004296{
Pekka Paalanen18027e52011-12-02 16:31:49 +02004297 if (shell->screensaver.process.pid == 0)
4298 return;
4299
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03004300 /* Disarm the screensaver timer, otherwise it may fire when the
4301 * compositor is not in the idle state. In that case, the screen will
4302 * be locked, but the wake_signal won't fire on user input, making the
4303 * system unresponsive. */
4304 wl_event_source_timer_update(shell->screensaver.timer, 0);
4305
Pekka Paalanen18027e52011-12-02 16:31:49 +02004306 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004307}
4308
4309static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004310configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004311{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004312 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004313
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004314 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004315 if (v->output == ev->output && v != ev) {
4316 weston_view_unmap(v);
4317 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004318 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004319 }
4320 }
4321
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004322 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004323
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004324 if (wl_list_empty(&ev->layer_link.link)) {
4325 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004326 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004327 }
4328}
4329
Pekka Paalanen8274d902014-08-06 19:36:51 +03004330static int
4331background_get_label(struct weston_surface *surface, char *buf, size_t len)
4332{
4333 return snprintf(buf, len, "background for output %s",
4334 surface->output->name);
4335}
4336
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004337static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004338background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004339{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004340 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004341 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004342
Jason Ekstranda7af7042013-10-12 22:38:11 -05004343 view = container_of(es->views.next, struct weston_view, surface_link);
4344
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004345 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004346}
4347
4348static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004349desktop_shell_set_background(struct wl_client *client,
4350 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004351 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004352 struct wl_resource *surface_resource)
4353{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004354 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004355 struct weston_surface *surface =
4356 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004357 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004358
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004359 if (surface->configure) {
4360 wl_resource_post_error(surface_resource,
4361 WL_DISPLAY_ERROR_INVALID_OBJECT,
4362 "surface role already assigned");
4363 return;
4364 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004365
Jason Ekstranda7af7042013-10-12 22:38:11 -05004366 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4367 weston_view_destroy(view);
4368 view = weston_view_create(surface);
4369
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004370 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004371 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004372 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004373 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004374 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004375 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004376 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004377 surface->output->width,
4378 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004379}
4380
Pekka Paalanen8274d902014-08-06 19:36:51 +03004381static int
4382panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4383{
4384 return snprintf(buf, len, "panel for output %s",
4385 surface->output->name);
4386}
4387
Kristian Høgsberg75840622011-09-06 13:48:16 -04004388static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004389panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004390{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004391 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004392 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004393
Jason Ekstranda7af7042013-10-12 22:38:11 -05004394 view = container_of(es->views.next, struct weston_view, surface_link);
4395
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004396 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004397}
4398
4399static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004400desktop_shell_set_panel(struct wl_client *client,
4401 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004402 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004403 struct wl_resource *surface_resource)
4404{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004405 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004406 struct weston_surface *surface =
4407 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004408 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004409
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004410 if (surface->configure) {
4411 wl_resource_post_error(surface_resource,
4412 WL_DISPLAY_ERROR_INVALID_OBJECT,
4413 "surface role already assigned");
4414 return;
4415 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004416
Jason Ekstranda7af7042013-10-12 22:38:11 -05004417 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4418 weston_view_destroy(view);
4419 view = weston_view_create(surface);
4420
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004421 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004422 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004423 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004424 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004425 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004426 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004427 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004428 surface->output->width,
4429 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004430}
4431
Pekka Paalanen8274d902014-08-06 19:36:51 +03004432static int
4433lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4434{
4435 return snprintf(buf, len, "lock window");
4436}
4437
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004438static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004439lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004440{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004441 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004442 struct weston_view *view;
4443
4444 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004445
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004446 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004447 return;
4448
Jason Ekstranda7af7042013-10-12 22:38:11 -05004449 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004450
4451 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004452 weston_layer_entry_insert(&shell->lock_layer.view_list,
4453 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004454 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004455 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004456 }
4457}
4458
4459static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004460handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004461{
Tiago Vignattibe143262012-04-16 17:31:41 +03004462 struct desktop_shell *shell =
4463 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004464
Martin Minarik6d118362012-06-07 18:01:59 +02004465 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004466 shell->lock_surface = NULL;
4467}
4468
4469static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004470desktop_shell_set_lock_surface(struct wl_client *client,
4471 struct wl_resource *resource,
4472 struct wl_resource *surface_resource)
4473{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004474 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004475 struct weston_surface *surface =
4476 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004477
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004478 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004479
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004480 if (!shell->locked)
4481 return;
4482
Pekka Paalanen98262232011-12-01 10:42:22 +02004483 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004484
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004485 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004486 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004487 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004488
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004489 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004490 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004491 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004492 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004493}
4494
4495static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004496resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004497{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004498 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004499
Pekka Paalanen77346a62011-11-30 16:26:35 +02004500 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004501
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004502 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004503 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004504 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004505 &shell->input_panel_layer.link);
4506 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004507 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004508 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004509 wl_list_insert(&shell->compositor->cursor_layer.link,
4510 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004511 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004512 wl_list_insert(&shell->fullscreen_layer.link,
4513 &shell->panel_layer.link);
4514 wl_list_insert(&shell->panel_layer.link,
4515 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004516
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004517 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004518
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004519 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004520 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004521 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004522}
4523
4524static void
4525desktop_shell_unlock(struct wl_client *client,
4526 struct wl_resource *resource)
4527{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004528 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004529
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004530 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004531
4532 if (shell->locked)
4533 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004534}
4535
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004536static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004537desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004538 struct wl_resource *resource,
4539 struct wl_resource *surface_resource)
4540{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004541 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004542
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004543 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004544 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004545}
4546
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004547static void
4548desktop_shell_desktop_ready(struct wl_client *client,
4549 struct wl_resource *resource)
4550{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004551 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004552
4553 shell_fade_startup(shell);
4554}
4555
Jonny Lamb765760d2014-08-20 15:53:19 +02004556static void
4557desktop_shell_set_panel_position(struct wl_client *client,
4558 struct wl_resource *resource,
4559 uint32_t position)
4560{
4561 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4562
4563 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4564 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4565 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4566 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4567 wl_resource_post_error(resource,
4568 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4569 "bad position argument");
4570 return;
4571 }
4572
4573 shell->panel_position = position;
4574}
4575
Kristian Høgsberg75840622011-09-06 13:48:16 -04004576static const struct desktop_shell_interface desktop_shell_implementation = {
4577 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004578 desktop_shell_set_panel,
4579 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004580 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004581 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004582 desktop_shell_desktop_ready,
4583 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004584};
4585
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004586static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004587get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004588{
4589 struct shell_surface *shsurf;
4590
4591 shsurf = get_shell_surface(surface);
4592 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004593 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004594 return shsurf->type;
4595}
4596
Kristian Høgsberg75840622011-09-06 13:48:16 -04004597static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004598move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004599{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004600 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004601 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004602 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004603
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004604 if (seat->pointer->focus == NULL)
4605 return;
4606
4607 focus = seat->pointer->focus->surface;
4608
Pekka Paalanen01388e22013-04-25 13:57:44 +03004609 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004610 if (surface == NULL)
4611 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004612
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004613 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004614 if (shsurf == NULL || shsurf->state.fullscreen ||
4615 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004616 return;
4617
Derek Foreman8aeeac82015-01-30 13:24:36 -06004618 surface_move(shsurf, seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004619}
4620
4621static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004622maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4623{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004624 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004625 struct weston_surface *surface;
4626 struct shell_surface *shsurf;
4627
4628 surface = weston_surface_get_main_surface(focus);
4629 if (surface == NULL)
4630 return;
4631
4632 shsurf = get_shell_surface(surface);
4633 if (shsurf == NULL)
4634 return;
4635
4636 if (!shell_surface_is_xdg_surface(shsurf))
4637 return;
4638
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004639 shsurf->state_requested = true;
4640 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004641 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004642}
4643
4644static void
4645fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4646{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004647 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004648 struct weston_surface *surface;
4649 struct shell_surface *shsurf;
4650
4651 surface = weston_surface_get_main_surface(focus);
4652 if (surface == NULL)
4653 return;
4654
4655 shsurf = get_shell_surface(surface);
4656 if (shsurf == NULL)
4657 return;
4658
4659 if (!shell_surface_is_xdg_surface(shsurf))
4660 return;
4661
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004662 shsurf->state_requested = true;
4663 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004664 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004665 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004666}
4667
4668static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004669touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4670{
Derek Foreman362656b2014-09-04 10:23:05 -05004671 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004672 struct weston_surface *surface;
4673 struct shell_surface *shsurf;
4674
Derek Foreman362656b2014-09-04 10:23:05 -05004675 if (seat->touch->focus == NULL)
4676 return;
4677
4678 focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004679 surface = weston_surface_get_main_surface(focus);
4680 if (surface == NULL)
4681 return;
4682
4683 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004684 if (shsurf == NULL || shsurf->state.fullscreen ||
4685 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004686 return;
4687
Derek Foreman8aeeac82015-01-30 13:24:36 -06004688 surface_touch_move(shsurf, seat);
Neil Robertsaba0f252013-10-03 16:43:05 +01004689}
4690
4691static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004692resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004693{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004694 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004695 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004696 uint32_t edges = 0;
4697 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004698 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004699
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004700 if (seat->pointer->focus == NULL)
4701 return;
4702
4703 focus = seat->pointer->focus->surface;
4704
Pekka Paalanen01388e22013-04-25 13:57:44 +03004705 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004706 if (surface == NULL)
4707 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004708
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004709 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004710 if (shsurf == NULL || shsurf->state.fullscreen ||
4711 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004712 return;
4713
Jason Ekstranda7af7042013-10-12 22:38:11 -05004714 weston_view_from_global(shsurf->view,
4715 wl_fixed_to_int(seat->pointer->grab_x),
4716 wl_fixed_to_int(seat->pointer->grab_y),
4717 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004718
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004719 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004720 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004721 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004722 edges |= 0;
4723 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004724 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004725
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004726 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004727 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004728 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004729 edges |= 0;
4730 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004731 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004732
Derek Foreman8aeeac82015-01-30 13:24:36 -06004733 surface_resize(shsurf, seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004734}
4735
4736static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004737surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004738 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004739{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004740 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004741 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004742 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004743 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004744
Pekka Paalanen01388e22013-04-25 13:57:44 +03004745 /* XXX: broken for windows containing sub-surfaces */
4746 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004747 if (surface == NULL)
4748 return;
4749
4750 shsurf = get_shell_surface(surface);
4751 if (!shsurf)
4752 return;
4753
Jason Ekstranda7af7042013-10-12 22:38:11 -05004754 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004755
Jason Ekstranda7af7042013-10-12 22:38:11 -05004756 if (shsurf->view->alpha > 1.0)
4757 shsurf->view->alpha = 1.0;
4758 if (shsurf->view->alpha < step)
4759 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004760
Jason Ekstranda7af7042013-10-12 22:38:11 -05004761 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004762 weston_surface_damage(surface);
4763}
4764
4765static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004766do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004767 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004768{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004769 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004770 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004771 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004772
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004773 if (!seat->pointer) {
4774 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4775 return;
4776 }
4777
Scott Moreauccbf29d2012-02-22 14:21:41 -07004778 wl_list_for_each(output, &compositor->output_list, link) {
4779 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004780 wl_fixed_to_double(seat->pointer->x),
4781 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004782 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004783 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004784 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004785 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004786 increment = -output->zoom.increment;
4787 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004788 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004789 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004790 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004791 else
4792 increment = 0;
4793
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004794 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004795
Scott Moreaue6603982012-06-11 13:07:51 -06004796 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004797 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004798 else if (output->zoom.level > output->zoom.max_level)
4799 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004800 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004801 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004802 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004803
Scott Moreaue6603982012-06-11 13:07:51 -06004804 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004805
Jason Ekstranda7af7042013-10-12 22:38:11 -05004806 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004807 }
4808 }
4809}
4810
Scott Moreauccbf29d2012-02-22 14:21:41 -07004811static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004812zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004813 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004814{
4815 do_zoom(seat, time, 0, axis, value);
4816}
4817
4818static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004819zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004820 void *data)
4821{
4822 do_zoom(seat, time, key, 0, 0);
4823}
4824
4825static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004826terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004827 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004828{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004829 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004830
Daniel Stone325fc2d2012-05-30 16:31:58 +01004831 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004832}
4833
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004834static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004835rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4836 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004837{
4838 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004839 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004840 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004841 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004842 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004843
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004844 weston_pointer_move(pointer, x, y);
4845
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004846 if (!shsurf)
4847 return;
4848
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004849 cx = 0.5f * shsurf->surface->width;
4850 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004851
Daniel Stone37816df2012-05-16 18:45:18 +01004852 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4853 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004854 r = sqrtf(dx * dx + dy * dy);
4855
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004856 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004857 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004858
4859 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004860 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004861 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004862
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004863 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004864 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004865
4866 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004867 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004868 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004869 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004870 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004871
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004872 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004873 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004874 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004875 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004876 wl_list_init(&shsurf->rotation.transform.link);
4877 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004878 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004879 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004880
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004881 /* We need to adjust the position of the surface
4882 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004883 cposx = shsurf->view->geometry.x + cx;
4884 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004885 dposx = rotate->center.x - cposx;
4886 dposy = rotate->center.y - cposy;
4887 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004888 weston_view_set_position(shsurf->view,
4889 shsurf->view->geometry.x + dposx,
4890 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004891 }
4892
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004893 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004894 * lazily applies the damage due to rotation update.
4895 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004896 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004897}
4898
4899static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004900rotate_grab_button(struct weston_pointer_grab *grab,
4901 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004902{
4903 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004904 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004905 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004906 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004907 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004908
Daniel Stone4dbadb12012-05-30 16:31:51 +01004909 if (pointer->button_count == 0 &&
4910 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004911 if (shsurf)
4912 weston_matrix_multiply(&shsurf->rotation.rotation,
4913 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004914 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004915 free(rotate);
4916 }
4917}
4918
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004919static void
4920rotate_grab_cancel(struct weston_pointer_grab *grab)
4921{
4922 struct rotate_grab *rotate =
4923 container_of(grab, struct rotate_grab, base.grab);
4924
4925 shell_grab_end(&rotate->base);
4926 free(rotate);
4927}
4928
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004929static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004930 noop_grab_focus,
4931 rotate_grab_motion,
4932 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004933 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004934};
4935
4936static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004937surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004938{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004939 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004940 float dx, dy;
4941 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004942
Pekka Paalanen460099f2012-01-20 16:48:25 +02004943 rotate = malloc(sizeof *rotate);
4944 if (!rotate)
4945 return;
4946
Jason Ekstranda7af7042013-10-12 22:38:11 -05004947 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004948 surface->surface->width * 0.5f,
4949 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004950 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004951
Daniel Stone37816df2012-05-16 18:45:18 +01004952 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4953 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004954 r = sqrtf(dx * dx + dy * dy);
4955 if (r > 20.0f) {
4956 struct weston_matrix inverse;
4957
4958 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004959 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004960 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004961
4962 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004963 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004964 } else {
4965 weston_matrix_init(&surface->rotation.rotation);
4966 weston_matrix_init(&rotate->rotation);
4967 }
4968
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004969 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4970 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004971}
4972
4973static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004974rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004975 void *data)
4976{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004977 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004978 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004979 struct shell_surface *surface;
4980
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004981 if (seat->pointer->focus == NULL)
4982 return;
4983
4984 focus = seat->pointer->focus->surface;
4985
Pekka Paalanen01388e22013-04-25 13:57:44 +03004986 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004987 if (base_surface == NULL)
4988 return;
4989
4990 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004991 if (surface == NULL || surface->state.fullscreen ||
4992 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004993 return;
4994
4995 surface_rotate(surface, seat);
4996}
4997
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004998/* Move all fullscreen layers down to the current workspace and hide their
4999 * black views. The surfaces' state is set to both fullscreen and lowered,
5000 * and this is reversed when such a surface is re-configured, see
5001 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5002 *
5003 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005004 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005005void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005006lower_fullscreen_layer(struct desktop_shell *shell)
5007{
5008 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005009 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005010
5011 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005012 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005013 &shell->fullscreen_layer.view_list.link,
5014 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005015 struct shell_surface *shsurf = get_shell_surface(view->surface);
5016
5017 if (!shsurf)
5018 continue;
5019
5020 /* We can have a non-fullscreen popup for a fullscreen surface
5021 * in the fullscreen layer. */
5022 if (shsurf->state.fullscreen) {
5023 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005024 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5025 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005026 weston_view_damage_below(shsurf->fullscreen.black_view);
5027
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005028 }
5029
5030 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005031 weston_layer_entry_remove(&view->layer_link);
5032 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005033 weston_view_damage_below(view);
5034 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005035
5036 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005037 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005038}
5039
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005040void
Tiago Vignattibe143262012-04-16 17:31:41 +03005041activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005042 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005043{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005044 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005045 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005046 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005047 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005048 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005049
Kristian Høgsberg6110d072014-04-29 15:15:45 -07005050 lower_fullscreen_layer(shell);
5051
Pekka Paalanen01388e22013-04-25 13:57:44 +03005052 main_surface = weston_surface_get_main_surface(es);
5053
Daniel Stone37816df2012-05-16 18:45:18 +01005054 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005055
Jonas Ådahl8538b222012-08-29 22:13:03 +02005056 state = ensure_focus_state(shell, seat);
5057 if (state == NULL)
5058 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005059
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005060 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005061 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005062
Rafael Antognolli03b16592013-12-03 15:35:42 -02005063 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005064 assert(shsurf);
5065
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005066 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005067 shell_configure_fullscreen(shsurf);
5068 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005069 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005070
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005071 /* Update the surface’s layer. This brings it to the top of the stacking
5072 * order as appropriate. */
5073 shell_surface_update_layer(shsurf);
5074
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005075 if (shell->focus_animation_type != ANIMATION_NONE) {
5076 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005077 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005078 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005079}
5080
Alex Wu21858432012-04-01 20:13:08 +08005081/* no-op func for checking black surface */
5082static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005083black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005084{
5085}
5086
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005087static bool
Alex Wu21858432012-04-01 20:13:08 +08005088is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5089{
5090 if (es->configure == black_surface_configure) {
5091 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005092 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005093 return true;
5094 }
5095 return false;
5096}
5097
Kristian Høgsberg75840622011-09-06 13:48:16 -04005098static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005099activate_binding(struct weston_seat *seat,
5100 struct desktop_shell *shell,
5101 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005102{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005103 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005104
Alex Wu9c35e6b2012-03-05 14:13:13 +08005105 if (!focus)
5106 return;
5107
Pekka Paalanen01388e22013-04-25 13:57:44 +03005108 if (is_black_surface(focus, &main_surface))
5109 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005110
Pekka Paalanen01388e22013-04-25 13:57:44 +03005111 main_surface = weston_surface_get_main_surface(focus);
5112 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005113 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005114
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005115 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005116}
5117
5118static void
5119click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
5120 void *data)
5121{
5122 if (seat->pointer->grab != &seat->pointer->default_grab)
5123 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005124 if (seat->pointer->focus == NULL)
5125 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005126
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005127 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005128}
5129
5130static void
5131touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
5132{
5133 if (seat->touch->grab != &seat->touch->default_grab)
5134 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005135 if (seat->touch->focus == NULL)
5136 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005137
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005138 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005139}
5140
5141static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005142unfocus_all_seats(struct desktop_shell *shell)
5143{
5144 struct weston_seat *seat, *next;
5145
5146 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5147 if (seat->keyboard == NULL)
5148 continue;
5149
5150 weston_keyboard_set_focus(seat->keyboard, NULL);
5151 }
5152}
5153
5154static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005155lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005156{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005157 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005158
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005159 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005160 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005161 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005162 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005163
5164 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005165
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005166 /* Hide all surfaces by removing the fullscreen, panel and
5167 * toplevel layers. This way nothing else can show or receive
5168 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005169
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005170 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005171 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005172 if (shell->showing_input_panels)
5173 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005174 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005175 wl_list_insert(&shell->compositor->cursor_layer.link,
5176 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005177
Pekka Paalanen77346a62011-11-30 16:26:35 +02005178 launch_screensaver(shell);
5179
Neil Robertsb4a91702014-04-09 16:33:32 +01005180 /* Remove the keyboard focus on all seats. This will be
5181 * restored to the workspace's saved state via
5182 * restore_focus_state when the compositor is unlocked */
5183 unfocus_all_seats(shell);
5184
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005185 /* TODO: disable bindings that should not work while locked. */
5186
5187 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005188}
5189
5190static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005191unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005192{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005193 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005194 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005195 return;
5196 }
5197
5198 /* If desktop-shell client has gone away, unlock immediately. */
5199 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005200 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005201 return;
5202 }
5203
5204 if (shell->prepare_event_sent)
5205 return;
5206
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005207 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005208 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005209}
5210
5211static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005212shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005213{
5214 struct desktop_shell *shell = data;
5215
5216 shell->fade.animation = NULL;
5217
5218 switch (shell->fade.type) {
5219 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005220 weston_surface_destroy(shell->fade.view->surface);
5221 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005222 break;
5223 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005224 lock(shell);
5225 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005226 default:
5227 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005228 }
5229}
5230
Jason Ekstranda7af7042013-10-12 22:38:11 -05005231static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005232shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005233{
5234 struct weston_compositor *compositor = shell->compositor;
5235 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005236 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005237
5238 surface = weston_surface_create(compositor);
5239 if (!surface)
5240 return NULL;
5241
Jason Ekstranda7af7042013-10-12 22:38:11 -05005242 view = weston_view_create(surface);
5243 if (!view) {
5244 weston_surface_destroy(surface);
5245 return NULL;
5246 }
5247
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005248 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005249 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005250 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005251 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5252 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005253 pixman_region32_init(&surface->input);
5254
Jason Ekstranda7af7042013-10-12 22:38:11 -05005255 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005256}
5257
5258static void
5259shell_fade(struct desktop_shell *shell, enum fade_type type)
5260{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005261 float tint;
5262
5263 switch (type) {
5264 case FADE_IN:
5265 tint = 0.0;
5266 break;
5267 case FADE_OUT:
5268 tint = 1.0;
5269 break;
5270 default:
5271 weston_log("shell: invalid fade type\n");
5272 return;
5273 }
5274
5275 shell->fade.type = type;
5276
Jason Ekstranda7af7042013-10-12 22:38:11 -05005277 if (shell->fade.view == NULL) {
5278 shell->fade.view = shell_fade_create_surface(shell);
5279 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005280 return;
5281
Jason Ekstranda7af7042013-10-12 22:38:11 -05005282 shell->fade.view->alpha = 1.0 - tint;
5283 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005284 }
5285
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005286 if (shell->fade.view->output == NULL) {
5287 /* If the black view gets a NULL output, we lost the
5288 * last output and we'll just cancel the fade. This
5289 * happens when you close the last window under the
5290 * X11 or Wayland backends. */
5291 shell->locked = false;
5292 weston_surface_destroy(shell->fade.view->surface);
5293 shell->fade.view = NULL;
5294 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005295 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005296 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005297 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005298 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005299 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005300 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005301 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005302}
5303
5304static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005305do_shell_fade_startup(void *data)
5306{
5307 struct desktop_shell *shell = data;
5308
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005309 if (shell->startup_animation_type == ANIMATION_FADE)
5310 shell_fade(shell, FADE_IN);
5311 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005312 weston_surface_destroy(shell->fade.view->surface);
5313 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005314 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005315}
5316
5317static void
5318shell_fade_startup(struct desktop_shell *shell)
5319{
5320 struct wl_event_loop *loop;
5321
5322 if (!shell->fade.startup_timer)
5323 return;
5324
5325 wl_event_source_remove(shell->fade.startup_timer);
5326 shell->fade.startup_timer = NULL;
5327
5328 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5329 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5330}
5331
5332static int
5333fade_startup_timeout(void *data)
5334{
5335 struct desktop_shell *shell = data;
5336
5337 shell_fade_startup(shell);
5338 return 0;
5339}
5340
5341static void
5342shell_fade_init(struct desktop_shell *shell)
5343{
5344 /* Make compositor output all black, and wait for the desktop-shell
5345 * client to signal it is ready, then fade in. The timer triggers a
5346 * fade-in, in case the desktop-shell client takes too long.
5347 */
5348
5349 struct wl_event_loop *loop;
5350
Jason Ekstranda7af7042013-10-12 22:38:11 -05005351 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005352 weston_log("%s: warning: fade surface already exists\n",
5353 __func__);
5354 return;
5355 }
5356
Jason Ekstranda7af7042013-10-12 22:38:11 -05005357 shell->fade.view = shell_fade_create_surface(shell);
5358 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005359 return;
5360
Jason Ekstranda7af7042013-10-12 22:38:11 -05005361 weston_view_update_transform(shell->fade.view);
5362 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005363
5364 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5365 shell->fade.startup_timer =
5366 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5367 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5368}
5369
5370static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005371idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005372{
5373 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005374 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005375 struct weston_seat *seat;
5376
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005377 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005378 if (seat->pointer)
5379 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005380 if (seat->touch)
5381 touch_popup_grab_end(seat->touch);
5382 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005383
5384 shell_fade(shell, FADE_OUT);
5385 /* lock() is called from shell_fade_done() */
5386}
5387
5388static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005389wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005390{
5391 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005392 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005393
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005394 unlock(shell);
5395}
5396
5397static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005398center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005399{
Giulio Camuffob8366642013-04-25 13:57:46 +03005400 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005401 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005402
Jason Ekstranda7af7042013-10-12 22:38:11 -05005403 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005404
5405 x = output->x + (output->width - width) / 2 - surf_x / 2;
5406 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005407
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005408 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005409}
5410
5411static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005412weston_view_set_initial_position(struct weston_view *view,
5413 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005414{
5415 struct weston_compositor *compositor = shell->compositor;
5416 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005417 int32_t range_x, range_y;
5418 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005419 struct weston_output *output, *target_output = NULL;
5420 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005421 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005422
5423 /* As a heuristic place the new window on the same output as the
5424 * pointer. Falling back to the output containing 0, 0.
5425 *
5426 * TODO: Do something clever for touch too?
5427 */
5428 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005429 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005430 ix = wl_fixed_to_int(seat->pointer->x);
5431 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005432 break;
5433 }
5434 }
5435
5436 wl_list_for_each(output, &compositor->output_list, link) {
5437 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5438 target_output = output;
5439 break;
5440 }
5441 }
5442
5443 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005444 weston_view_set_position(view, 10 + random() % 400,
5445 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005446 return;
5447 }
5448
5449 /* Valid range within output where the surface will still be onscreen.
5450 * If this is negative it means that the surface is bigger than
5451 * output.
5452 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005453 get_output_work_area(shell, target_output, &area);
5454
5455 dx = area.x;
5456 dy = area.y;
5457 range_x = area.width - view->surface->width;
5458 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005459
Rob Bradford4cb88c72012-08-13 15:18:44 +01005460 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005461 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005462
5463 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005464 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005465
5466 x = target_output->x + dx;
5467 y = target_output->y + dy;
5468
Jason Ekstranda7af7042013-10-12 22:38:11 -05005469 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005470}
5471
5472static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005473set_maximized_position(struct desktop_shell *shell,
5474 struct shell_surface *shsurf)
5475{
5476 int32_t surf_x, surf_y;
5477 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005478 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005479
Jonny Lambd73c6942014-08-20 15:53:20 +02005480 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005481 if (shsurf->has_set_geometry) {
5482 surf_x = shsurf->geometry.x;
5483 surf_y = shsurf->geometry.y;
5484 } else {
5485 surface_subsurfaces_boundingbox(shsurf->surface,
5486 &surf_x, &surf_y, NULL, NULL);
5487 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005488 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005489
5490 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005491 e->x1 + area.x - surf_x,
5492 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005493}
5494
5495static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005496map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005497 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005498{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005499 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005500 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005501
Pekka Paalanen77346a62011-11-30 16:26:35 +02005502 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005503 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005504 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005505 if (shsurf->state.fullscreen) {
5506 center_on_output(shsurf->view, shsurf->fullscreen_output);
5507 shell_map_fullscreen(shsurf);
5508 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005509 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005510 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005511 weston_view_set_initial_position(shsurf->view, shell);
5512 }
Juan Zhao96879df2012-02-07 08:45:41 +08005513 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005514 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005515 if (shell_map_popup(shsurf) != 0)
5516 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005517 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005518 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005519 weston_view_set_position(shsurf->view,
5520 shsurf->view->geometry.x + sx,
5521 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005522 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005523 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005524 default:
5525 ;
5526 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005527
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005528 /* Surface stacking order, see also activate(). */
5529 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005530
Jason Ekstranda7af7042013-10-12 22:38:11 -05005531 if (shsurf->type != SHELL_SURFACE_NONE) {
5532 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005533 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005534 shsurf->surface->output = shsurf->output;
5535 shsurf->view->output = shsurf->output;
5536 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005537 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005538
Jason Ekstranda7af7042013-10-12 22:38:11 -05005539 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005540 /* XXX: xwayland's using the same fields for transient type */
5541 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005542 if (shsurf->transient.flags ==
5543 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5544 break;
5545 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005546 if (shsurf->state.relative &&
5547 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5548 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005549 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005550 break;
5551 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005552 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005553 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005554 case SHELL_SURFACE_POPUP:
5555 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005556 default:
5557 break;
5558 }
5559
Rafael Antognolli03b16592013-12-03 15:35:42 -02005560 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5561 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005562 {
5563 switch (shell->win_animation_type) {
5564 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005565 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005566 break;
5567 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005568 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005569 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005570 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005571 default:
5572 break;
5573 }
5574 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005575}
5576
5577static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005578configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005579 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005580{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005581 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005582 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005583
Pekka Paalanen77346a62011-11-30 16:26:35 +02005584 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005585
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005586 assert(shsurf);
5587
Rafael Antognolli03b16592013-12-03 15:35:42 -02005588 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005589 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005590 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005591 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005592 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005593 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005594 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005595
Alex Wu4539b082012-03-01 12:57:46 +08005596 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005597 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005598 wl_list_for_each(view, &surface->views, surface_link)
5599 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005600
Rafael Antognolli03b16592013-12-03 15:35:42 -02005601 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005602 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005603 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005604}
5605
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005606static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005607shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005608{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005609 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005610 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005611 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005612
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005613 assert(shsurf);
5614
5615 shell = shsurf->shell;
5616
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005617 if (!weston_surface_is_mapped(es) &&
5618 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005619 remove_popup_grab(shsurf);
5620 }
5621
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005622 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005623 return;
5624
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005625 if (shsurf->has_next_geometry) {
5626 shsurf->geometry = shsurf->next_geometry;
5627 shsurf->has_next_geometry = false;
5628 shsurf->has_set_geometry = true;
5629 } else if (!shsurf->has_set_geometry) {
5630 surface_subsurfaces_boundingbox(shsurf->surface,
5631 &shsurf->geometry.x,
5632 &shsurf->geometry.y,
5633 &shsurf->geometry.width,
5634 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005635 }
5636
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005637 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005638 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005639 type_changed = 1;
5640 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005641
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005642 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005643 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005644 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005645 shsurf->last_width != es->width ||
5646 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005647 float from_x, from_y;
5648 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005649
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005650 if (shsurf->resize_edges) {
5651 sx = 0;
5652 sy = 0;
5653 }
5654
5655 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5656 sx = shsurf->last_width - es->width;
5657 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5658 sy = shsurf->last_height - es->height;
5659
5660 shsurf->last_width = es->width;
5661 shsurf->last_height = es->height;
5662
Jason Ekstranda7af7042013-10-12 22:38:11 -05005663 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5664 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005665 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005666 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005667 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005668 }
5669}
5670
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005671static bool
5672check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5673{
5674 struct timespec now;
5675
5676 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5677 return false;
5678
5679 /*
5680 * If the shell helper client dies before the session has been
5681 * up for roughly 30 seconds, better just make Weston shut down,
5682 * because the user likely has no way to interact with the desktop
5683 * anyway.
5684 */
5685 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5686 weston_log("Error: %s apparently cannot run at all.\n",
5687 shell->client);
5688 weston_log_continue(STAMP_SPACE "Quitting...");
5689 wl_display_terminate(shell->compositor->wl_display);
5690
5691 return true;
5692 }
5693
5694 return false;
5695}
5696
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005697static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005698
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005699static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005700respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005701{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005702 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005703
5704 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5705 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005706 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005707 shell->child.deathstamp = time;
5708 shell->child.deathcount = 0;
5709 }
5710
5711 shell->child.deathcount++;
5712 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005713 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005714 return;
5715 }
5716
Pekka Paalanen826dc142014-08-27 12:11:53 +03005717 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005718 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005719}
5720
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005721static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005722desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5723{
5724 struct desktop_shell *shell;
5725
5726 shell = container_of(listener, struct desktop_shell,
5727 child.client_destroy_listener);
5728
Pekka Paalanen826dc142014-08-27 12:11:53 +03005729 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005730 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005731 /*
5732 * unbind_desktop_shell() will reset shell->child.desktop_shell
5733 * before the respawned process has a chance to create a new
5734 * desktop_shell object, because we are being called from the
5735 * wl_client destructor which destroys all wl_resources before
5736 * returning.
5737 */
5738
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005739 if (!check_desktop_shell_crash_too_early(shell))
5740 respawn_desktop_shell_process(shell);
5741
Pekka Paalanen826dc142014-08-27 12:11:53 +03005742 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005743}
5744
5745static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005746launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005747{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005748 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005749
Pekka Paalanen826dc142014-08-27 12:11:53 +03005750 shell->child.client = weston_client_start(shell->compositor,
5751 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005752
Arnaud Vrac42631192014-08-25 20:56:46 +02005753 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005754 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005755 return;
5756 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005757
5758 shell->child.client_destroy_listener.notify =
5759 desktop_shell_client_destroy;
5760 wl_client_add_destroy_listener(shell->child.client,
5761 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005762}
5763
5764static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005765handle_shell_client_destroy(struct wl_listener *listener, void *data)
5766{
5767 struct shell_client *sc =
5768 container_of(listener, struct shell_client, destroy_listener);
5769
5770 if (sc->ping_timer)
5771 wl_event_source_remove(sc->ping_timer);
5772 free(sc);
5773}
5774
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005775static struct shell_client *
5776shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5777 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005778{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005779 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005780
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005781 sc = zalloc(sizeof *sc);
5782 if (sc == NULL) {
5783 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005784 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005785 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005786
5787 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005788 if (sc->resource == NULL) {
5789 free(sc);
5790 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005791 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005792 }
5793
5794 sc->client = client;
5795 sc->shell = shell;
5796 sc->destroy_listener.notify = handle_shell_client_destroy;
5797 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5798
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005799 return sc;
5800}
5801
5802static void
5803bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5804{
5805 struct desktop_shell *shell = data;
5806 struct shell_client *sc;
5807
5808 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5809 if (sc)
5810 wl_resource_set_implementation(sc->resource,
5811 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005812 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005813}
5814
5815static void
5816bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5817{
5818 struct desktop_shell *shell = data;
5819 struct shell_client *sc;
5820
5821 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5822 if (sc)
5823 wl_resource_set_dispatcher(sc->resource,
5824 xdg_shell_unversioned_dispatch,
5825 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005826}
5827
5828static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005829unbind_desktop_shell(struct wl_resource *resource)
5830{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005831 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005832
5833 if (shell->locked)
5834 resume_desktop(shell);
5835
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005836 shell->child.desktop_shell = NULL;
5837 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005838}
5839
5840static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005841bind_desktop_shell(struct wl_client *client,
5842 void *data, uint32_t version, uint32_t id)
5843{
Tiago Vignattibe143262012-04-16 17:31:41 +03005844 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005845 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005846
Jason Ekstranda85118c2013-06-27 20:17:02 -05005847 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005848 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005849
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005850 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005851 wl_resource_set_implementation(resource,
5852 &desktop_shell_implementation,
5853 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005854 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005855
5856 if (version < 2)
5857 shell_fade_startup(shell);
5858
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005859 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005860 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005861
5862 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5863 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005864}
5865
Pekka Paalanen8274d902014-08-06 19:36:51 +03005866static int
5867screensaver_get_label(struct weston_surface *surface, char *buf, size_t len)
5868{
5869 return snprintf(buf, len, "screensaver for output %s",
5870 surface->output->name);
5871}
5872
Pekka Paalanen6e168112011-11-24 11:34:05 +02005873static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005874screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005875{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005876 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005877 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005878 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005879
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005880 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005881 return;
5882
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005883 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005884 if (!shell->locked)
5885 return;
5886
Jason Ekstranda7af7042013-10-12 22:38:11 -05005887 view = container_of(surface->views.next, struct weston_view, surface_link);
5888 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005889
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005890 if (wl_list_empty(&view->layer_link.link)) {
5891 prev = container_of(shell->lock_layer.view_list.link.prev,
5892 struct weston_layer_entry, link);
5893 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005894 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005895 wl_event_source_timer_update(shell->screensaver.timer,
5896 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005897 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005898 }
5899}
5900
5901static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005902screensaver_set_surface(struct wl_client *client,
5903 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005904 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005905 struct wl_resource *output_resource)
5906{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005907 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005908 struct weston_surface *surface =
5909 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005910 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005911 struct weston_view *view, *next;
5912
5913 /* Make sure we only have one view */
5914 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5915 weston_view_destroy(view);
5916 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005917
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005918 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005919 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03005920 weston_surface_set_label_func(surface, screensaver_get_label);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005921 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005922}
5923
5924static const struct screensaver_interface screensaver_implementation = {
5925 screensaver_set_surface
5926};
5927
5928static void
5929unbind_screensaver(struct wl_resource *resource)
5930{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005931 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005932
Pekka Paalanen77346a62011-11-30 16:26:35 +02005933 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005934}
5935
5936static void
5937bind_screensaver(struct wl_client *client,
5938 void *data, uint32_t version, uint32_t id)
5939{
Tiago Vignattibe143262012-04-16 17:31:41 +03005940 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005941 struct wl_resource *resource;
5942
Jason Ekstranda85118c2013-06-27 20:17:02 -05005943 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005944
Pekka Paalanen77346a62011-11-30 16:26:35 +02005945 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005946 wl_resource_set_implementation(resource,
5947 &screensaver_implementation,
5948 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005949 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005950 return;
5951 }
5952
5953 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5954 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005955}
5956
Kristian Høgsberg07045392012-02-19 18:52:44 -05005957struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005958 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005959 struct weston_surface *current;
5960 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005961 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005962 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005963};
5964
5965static void
5966switcher_next(struct switcher *switcher)
5967{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005968 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005969 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005970 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005971 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005972
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005973 /* temporary re-display minimized surfaces */
5974 struct weston_view *tmp;
5975 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005976 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5977 weston_layer_entry_remove(&view->layer_link);
5978 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005979 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5980 *minimized = view;
5981 }
5982
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005983 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005984 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005985 if (shsurf &&
5986 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5987 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005988 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005989 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005990 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005991 next = view->surface;
5992 prev = view->surface;
5993 view->alpha = 0.25;
5994 weston_view_geometry_dirty(view);
5995 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005996 }
Alex Wu1659daa2012-04-01 20:13:09 +08005997
Jason Ekstranda7af7042013-10-12 22:38:11 -05005998 if (is_black_surface(view->surface, NULL)) {
5999 view->alpha = 0.25;
6000 weston_view_geometry_dirty(view);
6001 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006002 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006003 }
6004
6005 if (next == NULL)
6006 next = first;
6007
Alex Wu07b26062012-03-12 16:06:01 +08006008 if (next == NULL)
6009 return;
6010
Kristian Høgsberg07045392012-02-19 18:52:44 -05006011 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006012 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006013
6014 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006015 wl_list_for_each(view, &next->views, surface_link)
6016 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006017
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006018 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006019 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006020 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006021}
6022
6023static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006024switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006025{
6026 struct switcher *switcher =
6027 container_of(listener, struct switcher, listener);
6028
6029 switcher_next(switcher);
6030}
6031
6032static void
Daniel Stone351eb612012-05-31 15:27:47 -04006033switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006034{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006035 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006036 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006037 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006038
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006039 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006040 if (is_focus_view(view))
6041 continue;
6042
Jason Ekstranda7af7042013-10-12 22:38:11 -05006043 view->alpha = 1.0;
6044 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006045 }
6046
Alex Wu07b26062012-03-12 16:06:01 +08006047 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006048 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006049 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006050 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006051 weston_keyboard_end_grab(keyboard);
6052 if (keyboard->input_method_resource)
6053 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006054
6055 /* re-hide surfaces that were temporary shown during the switch */
6056 struct weston_view **minimized;
6057 wl_array_for_each(minimized, &switcher->minimized_array) {
6058 /* with the exception of the current selected */
6059 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006060 weston_layer_entry_remove(&(*minimized)->layer_link);
6061 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006062 weston_view_damage_below(*minimized);
6063 }
6064 }
6065 wl_array_release(&switcher->minimized_array);
6066
Kristian Høgsberg07045392012-02-19 18:52:44 -05006067 free(switcher);
6068}
6069
6070static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006071switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006072 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006073{
6074 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006075 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006076
Daniel Stonec9785ea2012-05-30 16:31:52 +01006077 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006078 switcher_next(switcher);
6079}
6080
6081static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006082switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006083 uint32_t mods_depressed, uint32_t mods_latched,
6084 uint32_t mods_locked, uint32_t group)
6085{
6086 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006087 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006088
Daniel Stone351eb612012-05-31 15:27:47 -04006089 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6090 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006091}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006092
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006093static void
6094switcher_cancel(struct weston_keyboard_grab *grab)
6095{
6096 struct switcher *switcher = container_of(grab, struct switcher, grab);
6097
6098 switcher_destroy(switcher);
6099}
6100
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006101static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006102 switcher_key,
6103 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006104 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006105};
6106
6107static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006108switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006109 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006110{
Tiago Vignattibe143262012-04-16 17:31:41 +03006111 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006112 struct switcher *switcher;
6113
6114 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006115 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006116 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006117 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006118 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006119 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006120
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006121 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006122 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006123 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006124 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
6125 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006126 switcher_next(switcher);
6127}
6128
Pekka Paalanen3c647232011-12-22 13:43:43 +02006129static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006130backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006131 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006132{
6133 struct weston_compositor *compositor = data;
6134 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006135 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006136
6137 /* TODO: we're limiting to simple use cases, where we assume just
6138 * control on the primary display. We'd have to extend later if we
6139 * ever get support for setting backlights on random desktop LCD
6140 * panels though */
6141 output = get_default_output(compositor);
6142 if (!output)
6143 return;
6144
6145 if (!output->set_backlight)
6146 return;
6147
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006148 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6149 backlight_new = output->backlight_current - 25;
6150 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6151 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006152
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006153 if (backlight_new < 5)
6154 backlight_new = 5;
6155 if (backlight_new > 255)
6156 backlight_new = 255;
6157
6158 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006159 output->set_backlight(output, output->backlight_current);
6160}
6161
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006162static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006163force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006164 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006165{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006166 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006167 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006168 struct desktop_shell *shell = data;
6169 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006170 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006171
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006172 focus_surface = seat->keyboard->focus;
6173 if (!focus_surface)
6174 return;
6175
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006176 wl_signal_emit(&compositor->kill_signal, focus_surface);
6177
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006178 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006179 wl_client_get_credentials(client, &pid, NULL, NULL);
6180
6181 /* Skip clients that we launched ourselves (the credentials of
6182 * the socketpair is ours) */
6183 if (pid == getpid())
6184 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006185
Daniel Stone325fc2d2012-05-30 16:31:58 +01006186 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006187}
6188
6189static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006190workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006191 uint32_t key, void *data)
6192{
6193 struct desktop_shell *shell = data;
6194 unsigned int new_index = shell->workspaces.current;
6195
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006196 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006197 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006198 if (new_index != 0)
6199 new_index--;
6200
6201 change_workspace(shell, new_index);
6202}
6203
6204static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006205workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006206 uint32_t key, void *data)
6207{
6208 struct desktop_shell *shell = data;
6209 unsigned int new_index = shell->workspaces.current;
6210
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006211 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006212 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006213 if (new_index < shell->workspaces.num - 1)
6214 new_index++;
6215
6216 change_workspace(shell, new_index);
6217}
6218
6219static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006220workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006221 uint32_t key, void *data)
6222{
6223 struct desktop_shell *shell = data;
6224 unsigned int new_index;
6225
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006226 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006227 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006228 new_index = key - KEY_F1;
6229 if (new_index >= shell->workspaces.num)
6230 new_index = shell->workspaces.num - 1;
6231
6232 change_workspace(shell, new_index);
6233}
6234
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006235static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006236workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006237 uint32_t key, void *data)
6238{
6239 struct desktop_shell *shell = data;
6240 unsigned int new_index = shell->workspaces.current;
6241
6242 if (shell->locked)
6243 return;
6244
6245 if (new_index != 0)
6246 new_index--;
6247
6248 take_surface_to_workspace_by_seat(shell, seat, new_index);
6249}
6250
6251static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006252workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006253 uint32_t key, void *data)
6254{
6255 struct desktop_shell *shell = data;
6256 unsigned int new_index = shell->workspaces.current;
6257
6258 if (shell->locked)
6259 return;
6260
6261 if (new_index < shell->workspaces.num - 1)
6262 new_index++;
6263
6264 take_surface_to_workspace_by_seat(shell, seat, new_index);
6265}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006266
6267static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006268shell_reposition_view_on_output_destroy(struct weston_view *view)
6269{
6270 struct weston_output *output, *first_output;
6271 struct weston_compositor *ec = view->surface->compositor;
6272 struct shell_surface *shsurf;
6273 float x, y;
6274 int visible;
6275
6276 x = view->geometry.x;
6277 y = view->geometry.y;
6278
6279 /* At this point the destroyed output is not in the list anymore.
6280 * If the view is still visible somewhere, we leave where it is,
6281 * otherwise, move it to the first output. */
6282 visible = 0;
6283 wl_list_for_each(output, &ec->output_list, link) {
6284 if (pixman_region32_contains_point(&output->region,
6285 x, y, NULL)) {
6286 visible = 1;
6287 break;
6288 }
6289 }
6290
6291 if (!visible) {
6292 first_output = container_of(ec->output_list.next,
6293 struct weston_output, link);
6294
6295 x = first_output->x + first_output->width / 4;
6296 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006297
6298 weston_view_set_position(view, x, y);
6299 } else {
6300 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006301 }
6302
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006303
6304 shsurf = get_shell_surface(view->surface);
6305
6306 if (shsurf) {
6307 shsurf->saved_position_valid = false;
6308 shsurf->next_state.maximized = false;
6309 shsurf->next_state.fullscreen = false;
6310 shsurf->state_changed = true;
6311 }
6312}
6313
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006314void
6315shell_for_each_layer(struct desktop_shell *shell,
6316 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006317{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006318 struct workspace **ws;
6319
6320 func(shell, &shell->fullscreen_layer, data);
6321 func(shell, &shell->panel_layer, data);
6322 func(shell, &shell->background_layer, data);
6323 func(shell, &shell->lock_layer, data);
6324 func(shell, &shell->input_panel_layer, data);
6325
6326 wl_array_for_each(ws, &shell->workspaces.array)
6327 func(shell, &(*ws)->layer, data);
6328}
6329
6330static void
6331shell_output_destroy_move_layer(struct desktop_shell *shell,
6332 struct weston_layer *layer,
6333 void *data)
6334{
6335 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006336 struct weston_view *view;
6337
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006338 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006339 if (view->output != output)
6340 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006341
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006342 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006343 }
6344}
6345
6346static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006347handle_output_destroy(struct wl_listener *listener, void *data)
6348{
6349 struct shell_output *output_listener =
6350 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006351 struct weston_output *output = output_listener->output;
6352 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006353
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006354 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006355
Xiong Zhang6b481422013-10-23 13:58:32 +08006356 wl_list_remove(&output_listener->destroy_listener.link);
6357 wl_list_remove(&output_listener->link);
6358 free(output_listener);
6359}
6360
6361static void
6362create_shell_output(struct desktop_shell *shell,
6363 struct weston_output *output)
6364{
6365 struct shell_output *shell_output;
6366
6367 shell_output = zalloc(sizeof *shell_output);
6368 if (shell_output == NULL)
6369 return;
6370
6371 shell_output->output = output;
6372 shell_output->shell = shell;
6373 shell_output->destroy_listener.notify = handle_output_destroy;
6374 wl_signal_add(&output->destroy_signal,
6375 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006376 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006377}
6378
6379static void
6380handle_output_create(struct wl_listener *listener, void *data)
6381{
6382 struct desktop_shell *shell =
6383 container_of(listener, struct desktop_shell, output_create_listener);
6384 struct weston_output *output = (struct weston_output *)data;
6385
6386 create_shell_output(shell, output);
6387}
6388
6389static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006390handle_output_move_layer(struct desktop_shell *shell,
6391 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006392{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006393 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006394 struct weston_view *view;
6395 float x, y;
6396
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006397 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006398 if (view->output != output)
6399 continue;
6400
6401 x = view->geometry.x + output->move_x;
6402 y = view->geometry.y + output->move_y;
6403 weston_view_set_position(view, x, y);
6404 }
6405}
6406
6407static void
6408handle_output_move(struct wl_listener *listener, void *data)
6409{
6410 struct desktop_shell *shell;
6411
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006412 shell = container_of(listener, struct desktop_shell,
6413 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006414
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006415 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006416}
6417
6418static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006419setup_output_destroy_handler(struct weston_compositor *ec,
6420 struct desktop_shell *shell)
6421{
6422 struct weston_output *output;
6423
6424 wl_list_init(&shell->output_list);
6425 wl_list_for_each(output, &ec->output_list, link)
6426 create_shell_output(shell, output);
6427
6428 shell->output_create_listener.notify = handle_output_create;
6429 wl_signal_add(&ec->output_created_signal,
6430 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006431
6432 shell->output_move_listener.notify = handle_output_move;
6433 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006434}
6435
6436static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006437shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006438{
Tiago Vignattibe143262012-04-16 17:31:41 +03006439 struct desktop_shell *shell =
6440 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006441 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006442 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006443
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006444 /* Force state to unlocked so we don't try to fade */
6445 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006446
6447 if (shell->child.client) {
6448 /* disable respawn */
6449 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006450 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006451 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006452
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09006453 wl_event_source_remove(shell->screensaver.timer);
6454
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006455 wl_list_remove(&shell->idle_listener.link);
6456 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006457
6458 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006459
Xiong Zhang6b481422013-10-23 13:58:32 +08006460 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6461 wl_list_remove(&shell_output->destroy_listener.link);
6462 wl_list_remove(&shell_output->link);
6463 free(shell_output);
6464 }
6465
6466 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006467 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006468
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006469 wl_array_for_each(ws, &shell->workspaces.array)
6470 workspace_destroy(*ws);
6471 wl_array_release(&shell->workspaces.array);
6472
Pekka Paalanen3c647232011-12-22 13:43:43 +02006473 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006474 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006475 free(shell);
6476}
6477
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006478static void
6479shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6480{
6481 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006482 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006483
6484 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006485 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6486 MODIFIER_CTRL | MODIFIER_ALT,
6487 terminate_binding, ec);
6488 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6489 click_to_activate_binding,
6490 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006491 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6492 click_to_activate_binding,
6493 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006494 weston_compositor_add_touch_binding(ec, 0,
6495 touch_to_activate_binding,
6496 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006497 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6498 MODIFIER_SUPER | MODIFIER_ALT,
6499 surface_opacity_binding, NULL);
6500 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6501 MODIFIER_SUPER, zoom_axis_binding,
6502 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006503
6504 /* configurable bindings */
6505 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006506 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6507 zoom_key_binding, NULL);
6508 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6509 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006510 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6511 maximize_binding, NULL);
6512 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6513 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006514 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6515 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006516 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006517 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6518 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006519 weston_compositor_add_button_binding(ec, BTN_LEFT,
6520 mod | MODIFIER_SHIFT,
6521 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006522
6523 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6524 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6525 rotate_binding, NULL);
6526
Daniel Stone325fc2d2012-05-30 16:31:58 +01006527 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6528 shell);
6529 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6530 ec);
6531 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6532 backlight_binding, ec);
6533 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6534 ec);
6535 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6536 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006537 weston_compositor_add_key_binding(ec, KEY_K, mod,
6538 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006539 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6540 workspace_up_binding, shell);
6541 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6542 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006543 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6544 workspace_move_surface_up_binding,
6545 shell);
6546 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6547 workspace_move_surface_down_binding,
6548 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006549
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006550 if (shell->exposay_modifier)
6551 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6552 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006553
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006554 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6555 if (shell->workspaces.num > 1) {
6556 num_workspace_bindings = shell->workspaces.num;
6557 if (num_workspace_bindings > 6)
6558 num_workspace_bindings = 6;
6559 for (i = 0; i < num_workspace_bindings; i++)
6560 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6561 workspace_f_binding,
6562 shell);
6563 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006564
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006565 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006566}
6567
Jason Ekstrand024177c2014-04-21 19:42:58 -05006568static void
6569handle_seat_created(struct wl_listener *listener, void *data)
6570{
6571 struct weston_seat *seat = data;
6572
6573 create_shell_seat(seat);
6574}
6575
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006576WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006577module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006578 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006579{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006580 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006581 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006582 struct workspace **pws;
6583 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006584 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006585
Peter Huttererf3d62272013-08-08 11:57:05 +10006586 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006587 if (shell == NULL)
6588 return -1;
6589
Kristian Høgsberg75840622011-09-06 13:48:16 -04006590 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006591
6592 shell->destroy_listener.notify = shell_destroy;
6593 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006594 shell->idle_listener.notify = idle_handler;
6595 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6596 shell->wake_listener.notify = wake_handler;
6597 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006598
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006599 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006600 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006601 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006602 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006603 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006604 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006605 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006606 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006607 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006608 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006609 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006610 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006611 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006612
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006613 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6614 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006615 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6616 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006617 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006618
6619 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006620 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006621
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006622 if (input_panel_setup(shell) < 0)
6623 return -1;
6624
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006625 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006626
Daniel Stonedf8133b2013-11-19 11:37:14 +01006627 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6628 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6629
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006630 for (i = 0; i < shell->workspaces.num; i++) {
6631 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6632 if (pws == NULL)
6633 return -1;
6634
6635 *pws = workspace_create();
6636 if (*pws == NULL)
6637 return -1;
6638 }
6639 activate_workspace(shell, 0);
6640
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006641 weston_layer_init(&shell->minimized_layer, NULL);
6642
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006643 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006644 wl_list_init(&shell->workspaces.animation.link);
6645 shell->workspaces.animation.frame = animate_workspace_change_frame;
6646
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006647 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006648 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006649 return -1;
6650
Rafael Antognollie2a34552013-12-03 15:35:45 -02006651 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6652 shell, bind_xdg_shell) == NULL)
6653 return -1;
6654
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006655 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006656 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006657 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006658 return -1;
6659
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006660 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6661 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006662 return -1;
6663
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006664 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6665 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006666 return -1;
6667
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006668 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006669
Jonny Lamb765760d2014-08-20 15:53:19 +02006670 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6671
Xiong Zhang6b481422013-10-23 13:58:32 +08006672 setup_output_destroy_handler(ec, shell);
6673
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006674 loop = wl_display_get_event_loop(ec->wl_display);
6675 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006676
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006677 shell->screensaver.timer =
6678 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6679
Jason Ekstrand024177c2014-04-21 19:42:58 -05006680 wl_list_for_each(seat, &ec->seat_list, link)
6681 handle_seat_created(NULL, seat);
6682 shell->seat_create_listener.notify = handle_seat_created;
6683 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006684
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006685 screenshooter_create(ec);
6686
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006687 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006688
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006689 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006690
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006691 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6692
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006693 return 0;
6694}