blob: ed7e8961ad6bcd0e2fde5b4c2fca88785f102a6d [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 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050012 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050028#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040029#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050030#include <string.h>
31#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040032#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020033#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020034#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020035#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040036#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080038#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020040#include "workspaces-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070041#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070042#include "shared/helpers.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020043#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050044
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020046#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020047
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010048#ifndef static_assert
49#define static_assert(cond, msg)
50#endif
51
Jonas Ådahl04769742012-06-13 00:01:24 +020052struct focus_state {
53 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040054 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_surface *keyboard_focus;
56 struct wl_list link;
57 struct wl_listener seat_destroy_listener;
58 struct wl_listener surface_destroy_listener;
59};
60
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020062 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050063 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030064 SHELL_SURFACE_POPUP,
65 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020066};
67
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050068struct shell_client;
69
Philip Withnall648a4dd2013-11-25 18:01:44 +000070/*
71 * Surface stacking and ordering.
72 *
73 * This is handled using several linked lists of surfaces, organised into
74 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
75 * above all of the surfaces in the layer below. The set of layers is static and
76 * in the following order (top-most first):
77 * • Lock layer (only ever displayed on its own)
78 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010079 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Fullscreen layer
81 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000082 * • Workspace layers
83 * • Background layer
84 *
85 * The list of layers may be manipulated to remove whole layers of surfaces from
86 * display. For example, when locking the screen, all layers except the lock
87 * layer are removed.
88 *
89 * A surface’s layer is modified on configuring the surface, in
90 * set_surface_type() (which is only called when the surface’s type change is
91 * _committed_). If a surface’s type changes (e.g. when making a window
92 * fullscreen) its layer changes too.
93 *
94 * In order to allow popup and transient surfaces to be correctly stacked above
95 * their parent surfaces, each surface tracks both its parent surface, and a
96 * linked list of its children. When a surface’s layer is updated, so are the
97 * layers of its children. Note that child surfaces are *not* the same as
98 * subsurfaces — child/parent surfaces are purely for maintaining stacking
99 * order.
100 *
101 * The children_link list of siblings of a surface (i.e. those surfaces which
102 * have the same parent) only contains weston_surfaces which have a
103 * shell_surface. Stacking is not implemented for non-shell_surface
104 * weston_surfaces. This means that the following implication does *not* hold:
105 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
106 */
107
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200108struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500109 struct wl_resource *resource;
110 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500111 struct shell_client *owner;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800112 struct wl_resource *owner_resource;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200113
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500114 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500115 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600116 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200117 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700118 struct wl_listener resource_destroy_listener;
119
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400120 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000121 struct wl_list children_list; /* child surfaces of this one */
122 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300123 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200124
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800125 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400126 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500127 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200128 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800129 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200130 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800131 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700132 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800133 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100134
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500135 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200136 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500137 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200138 } rotation;
139
140 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100141 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100143 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400144 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 } popup;
146
Alex Wu4539b082012-03-01 12:57:46 +0800147 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300148 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400149 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300150 } transient;
151
152 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800153 enum wl_shell_surface_fullscreen_method type;
154 struct weston_transform transform; /* matrix from x, y */
155 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500156 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800157 } fullscreen;
158
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200159 struct weston_transform workspace_transform;
160
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500161 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500162 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100163 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400164
165 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200166
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700167 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200168 bool maximized;
169 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200170 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100171 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500172 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200173 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500174 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500175
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700176 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400177 int32_t x, y, width, height;
178 } geometry, next_geometry;
179 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700180
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500181 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500182
183 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200184};
185
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300186struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400187 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300188 struct shell_surface *shsurf;
189 struct wl_listener shsurf_destroy_listener;
190};
191
Rusty Lynch1084da52013-08-15 09:10:08 -0700192struct shell_touch_grab {
193 struct weston_touch_grab grab;
194 struct shell_surface *shsurf;
195 struct wl_listener shsurf_destroy_listener;
196 struct weston_touch *touch;
197};
198
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300199struct weston_move_grab {
200 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100201 wl_fixed_t dx, dy;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700202 int client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500203};
204
Rusty Lynch1084da52013-08-15 09:10:08 -0700205struct weston_touch_move_grab {
206 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800207 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700208 wl_fixed_t dx, dy;
209};
210
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300212 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500213 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200214 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200215 float x;
216 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200217 } center;
218};
219
Giulio Camuffo5085a752013-03-25 21:42:45 +0100220struct shell_seat {
221 struct weston_seat *seat;
222 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500223 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100224
Jason Ekstrand024177c2014-04-21 19:42:58 -0500225 struct wl_listener caps_changed_listener;
226 struct wl_listener pointer_focus_listener;
227 struct wl_listener keyboard_focus_listener;
228
Giulio Camuffo5085a752013-03-25 21:42:45 +0100229 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400230 struct weston_pointer_grab grab;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200231 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100232 struct wl_list surfaces_list;
233 struct wl_client *client;
234 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200235 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100236 } popup_grab;
237};
238
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800239struct shell_client {
240 struct wl_resource *resource;
241 struct wl_client *client;
242 struct desktop_shell *shell;
243 struct wl_listener destroy_listener;
244 struct wl_event_source *ping_timer;
245 uint32_t ping_serial;
246 int unresponsive;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800247 struct wl_list surface_list;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800248};
249
Alex Wubd3354b2012-04-17 17:20:49 +0800250static struct desktop_shell *
251shell_surface_get_shell(struct shell_surface *shsurf);
252
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500253static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400254surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500255
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300256static void
257shell_fade_startup(struct desktop_shell *shell);
258
Philip Withnallbecb77e2013-11-25 18:01:30 +0000259static struct shell_seat *
260get_shell_seat(struct weston_seat *seat);
261
Jonny Lambd73c6942014-08-20 15:53:20 +0200262static void
263get_output_panel_size(struct desktop_shell *shell,
264 struct weston_output *output,
265 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700266
Philip Withnall648a4dd2013-11-25 18:01:44 +0000267static void
268shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
269
Alex Wubd3354b2012-04-17 17:20:49 +0800270static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200271shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
272
273static bool
274shell_surface_is_xdg_surface(struct shell_surface *shsurf);
275
276static bool
277shell_surface_is_xdg_popup(struct shell_surface *shsurf);
278
279static void
280shell_surface_set_parent(struct shell_surface *shsurf,
281 struct weston_surface *parent);
282
Pekka Paalanen8274d902014-08-06 19:36:51 +0300283static int
284shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
285{
286 struct shell_surface *shsurf;
287 const char *typestr[] = {
288 [SHELL_SURFACE_NONE] = "unidentified",
289 [SHELL_SURFACE_TOPLEVEL] = "top-level",
290 [SHELL_SURFACE_POPUP] = "popup",
291 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
292 };
293 const char *t, *c;
294
295 shsurf = get_shell_surface(surface);
296 if (!shsurf)
297 return snprintf(buf, len, "unidentified window");
298
299 t = shsurf->title;
300 c = shsurf->class;
301
302 return snprintf(buf, len, "%s window%s%s%s%s%s",
303 typestr[shsurf->type],
304 t ? " '" : "", t ?: "", t ? "'" : "",
305 c ? " of " : "", c ?: "");
306}
307
Rafael Antognollie2a34552013-12-03 15:35:45 -0200308static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800309shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
310{
311 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800313
314 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100315
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300316 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800317 return false;
318
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300319 top_fs_ev = container_of(shell->fullscreen_layer.view_list.link.next,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500320 struct weston_view,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300321 layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500322 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800323}
324
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500325static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400326destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300327{
328 struct shell_grab *grab;
329
330 grab = container_of(listener, struct shell_grab,
331 shsurf_destroy_listener);
332
333 grab->shsurf = NULL;
334}
335
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800336struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500337get_default_view(struct weston_surface *surface)
338{
339 struct shell_surface *shsurf;
340 struct weston_view *view;
341
342 if (!surface || wl_list_empty(&surface->views))
343 return NULL;
344
345 shsurf = get_shell_surface(surface);
346 if (shsurf)
347 return shsurf->view;
348
349 wl_list_for_each(view, &surface->views, surface_link)
350 if (weston_view_is_mapped(view))
351 return view;
352
353 return container_of(surface->views.next, struct weston_view, surface_link);
354}
355
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300356static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400357popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200358static void
359touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400360
361static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300362shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400363 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300364 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400365 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300366 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300367{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300368 struct desktop_shell *shell = shsurf->shell;
369
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400370 popup_grab_end(pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200371 if (pointer->seat->touch)
372 touch_popup_grab_end(pointer->seat->touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400373
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300374 grab->grab.interface = interface;
375 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400376 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500377 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400378 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300379
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700380 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400381 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400382 if (shell->child.desktop_shell) {
383 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
384 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500385 weston_pointer_set_focus(pointer,
386 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400387 wl_fixed_from_int(0),
388 wl_fixed_from_int(0));
389 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300390}
391
Jonny Lambd73c6942014-08-20 15:53:20 +0200392static void
393get_output_panel_size(struct desktop_shell *shell,
394 struct weston_output *output,
395 int *width,
396 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700397{
398 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200399
400 *width = 0;
401 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700402
403 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200404 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700405
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300406 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200407 float x, y;
408
409 if (view->surface->output != output)
410 continue;
411
412 switch (shell->panel_position) {
413 case DESKTOP_SHELL_PANEL_POSITION_TOP:
414 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
415
416 weston_view_to_global_float(view,
417 view->surface->width, 0,
418 &x, &y);
419
420 *width = (int) x;
421 *height = view->surface->height + (int) y;
422 return;
423
424 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
425 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
426 weston_view_to_global_float(view,
427 0, view->surface->height,
428 &x, &y);
429
430 *width = view->surface->width + (int) x;
431 *height = (int) y;
432 return;
433
434 default:
435 /* we've already set width and height to
436 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700437 break;
438 }
439 }
440
Jonny Lambd73c6942014-08-20 15:53:20 +0200441 /* the correct view wasn't found */
442}
443
444static void
445get_output_work_area(struct desktop_shell *shell,
446 struct weston_output *output,
447 pixman_rectangle32_t *area)
448{
449 int32_t panel_width = 0, panel_height = 0;
450
451 area->x = 0;
452 area->y = 0;
453
454 get_output_panel_size(shell, output, &panel_width, &panel_height);
455
456 switch (shell->panel_position) {
457 case DESKTOP_SHELL_PANEL_POSITION_TOP:
458 default:
459 area->y = panel_height;
460 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
461 area->width = output->width;
462 area->height = output->height - panel_height;
463 break;
464 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
465 area->x = panel_width;
466 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
467 area->width = output->width - panel_width;
468 area->height = output->height;
469 break;
470 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700471}
472
473static void
474send_configure_for_surface(struct shell_surface *shsurf)
475{
476 int32_t width, height;
477 struct surface_state *state;
478
479 if (shsurf->state_requested)
480 state = &shsurf->requested_state;
481 else if (shsurf->state_changed)
482 state = &shsurf->next_state;
483 else
484 state = &shsurf->state;
485
486 if (state->fullscreen) {
487 width = shsurf->output->width;
488 height = shsurf->output->height;
489 } else if (state->maximized) {
490 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200491 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700492
493 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200494 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700495
Jonny Lambd73c6942014-08-20 15:53:20 +0200496 width = area.width;
497 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900498 } else if (shsurf->resize_edges) {
499 width = shsurf->geometry.width;
500 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700501 } else {
502 width = 0;
503 height = 0;
504 }
505
506 shsurf->client->send_configure(shsurf->surface, width, height);
507}
508
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300509static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400510shell_surface_state_changed(struct shell_surface *shsurf)
511{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700512 if (shell_surface_is_xdg_surface(shsurf))
513 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400514}
515
516static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300517shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300518{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700519 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400520 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700521 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400522
523 if (grab->shsurf->resize_edges) {
524 grab->shsurf->resize_edges = 0;
525 shell_surface_state_changed(grab->shsurf);
526 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700527 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300528
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700529 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300530}
531
532static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700533shell_touch_grab_start(struct shell_touch_grab *grab,
534 const struct weston_touch_grab_interface *interface,
535 struct shell_surface *shsurf,
536 struct weston_touch *touch)
537{
538 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800539
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200540 touch_popup_grab_end(touch);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700541 if (touch->seat->pointer)
542 popup_grab_end(touch->seat->pointer);
543
Rusty Lynch1084da52013-08-15 09:10:08 -0700544 grab->grab.interface = interface;
545 grab->shsurf = shsurf;
546 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
547 wl_signal_add(&shsurf->destroy_signal,
548 &grab->shsurf_destroy_listener);
549
550 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700551 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700552
553 weston_touch_start_grab(touch, &grab->grab);
554 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500555 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500556 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700557}
558
559static void
560shell_touch_grab_end(struct shell_touch_grab *grab)
561{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700562 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700563 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700564 grab->shsurf->grabbed = 0;
565 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700566
567 weston_touch_end_grab(grab->touch);
568}
569
570static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500571center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800572 struct weston_output *output);
573
Daniel Stone496ca172012-05-30 16:31:42 +0100574static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300575get_modifier(char *modifier)
576{
577 if (!modifier)
578 return MODIFIER_SUPER;
579
580 if (!strcmp("ctrl", modifier))
581 return MODIFIER_CTRL;
582 else if (!strcmp("alt", modifier))
583 return MODIFIER_ALT;
584 else if (!strcmp("super", modifier))
585 return MODIFIER_SUPER;
586 else
587 return MODIFIER_SUPER;
588}
589
Juan Zhaoe10d2792012-04-25 19:09:52 +0800590static enum animation_type
591get_animation_type(char *animation)
592{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800593 if (!animation)
594 return ANIMATION_NONE;
595
Juan Zhaoe10d2792012-04-25 19:09:52 +0800596 if (!strcmp("zoom", animation))
597 return ANIMATION_ZOOM;
598 else if (!strcmp("fade", animation))
599 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100600 else if (!strcmp("dim-layer", animation))
601 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800602 else
603 return ANIMATION_NONE;
604}
605
Alex Wu4539b082012-03-01 12:57:46 +0800606static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400607shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200608{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400609 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500610 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300611 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200612
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400613 section = weston_config_get_section(shell->compositor->config,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400614 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300615 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
616 WESTON_SHELL_CLIENT);
617 if (ret < 0)
618 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400619 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500620 "client", &s, client);
621 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100622 shell->client = s;
623 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400624 "binding-modifier", &s, "super");
625 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200626 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800627
628 weston_config_section_get_string(section,
629 "exposay-modifier", &s, "none");
630 if (strcmp(s, "none") == 0)
631 shell->exposay_modifier = 0;
632 else
633 shell->exposay_modifier = get_modifier(s);
634 free(s);
635
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400636 weston_config_section_get_string(section, "animation", &s, "none");
637 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200638 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200639 weston_config_section_get_string(section, "close-animation", &s, "fade");
640 shell->win_close_animation_type = get_animation_type(s);
641 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700642 weston_config_section_get_string(section,
643 "startup-animation", &s, "fade");
644 shell->startup_animation_type = get_animation_type(s);
645 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700646 if (shell->startup_animation_type == ANIMATION_ZOOM)
647 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100648 weston_config_section_get_string(section, "focus-animation", &s, "none");
649 shell->focus_animation_type = get_animation_type(s);
650 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400651 weston_config_section_get_uint(section, "num-workspaces",
652 &shell->workspaces.num,
653 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200654}
655
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800656struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100657get_default_output(struct weston_compositor *compositor)
658{
659 return container_of(compositor->output_list.next,
660 struct weston_output, link);
661}
662
Pekka Paalanen8274d902014-08-06 19:36:51 +0300663static int
664focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
665{
666 return snprintf(buf, len, "focus highlight effect for output %s",
667 surface->output->name);
668}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100669
670/* no-op func for checking focus surface */
671static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600672focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100673{
674}
675
676static struct focus_surface *
677get_focus_surface(struct weston_surface *surface)
678{
679 if (surface->configure == focus_surface_configure)
680 return surface->configure_private;
681 else
682 return NULL;
683}
684
685static bool
686is_focus_surface (struct weston_surface *es)
687{
688 return (es->configure == focus_surface_configure);
689}
690
691static bool
692is_focus_view (struct weston_view *view)
693{
694 return is_focus_surface (view->surface);
695}
696
697static struct focus_surface *
698create_focus_surface(struct weston_compositor *ec,
699 struct weston_output *output)
700{
701 struct focus_surface *fsurf = NULL;
702 struct weston_surface *surface = NULL;
703
704 fsurf = malloc(sizeof *fsurf);
705 if (!fsurf)
706 return NULL;
707
708 fsurf->surface = weston_surface_create(ec);
709 surface = fsurf->surface;
710 if (surface == NULL) {
711 free(fsurf);
712 return NULL;
713 }
714
715 surface->configure = focus_surface_configure;
716 surface->output = output;
717 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300718 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100719
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800720 fsurf->view = weston_view_create(surface);
721 if (fsurf->view == NULL) {
722 weston_surface_destroy(surface);
723 free(fsurf);
724 return NULL;
725 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100726 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100727
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600728 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600729 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100730 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
731 pixman_region32_fini(&surface->opaque);
732 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
733 output->width, output->height);
734 pixman_region32_fini(&surface->input);
735 pixman_region32_init(&surface->input);
736
737 wl_list_init(&fsurf->workspace_transform.link);
738
739 return fsurf;
740}
741
742static void
743focus_surface_destroy(struct focus_surface *fsurf)
744{
745 weston_surface_destroy(fsurf->surface);
746 free(fsurf);
747}
748
749static void
750focus_animation_done(struct weston_view_animation *animation, void *data)
751{
752 struct workspace *ws = data;
753
754 ws->focus_animation = NULL;
755}
756
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200757static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200758focus_state_destroy(struct focus_state *state)
759{
760 wl_list_remove(&state->seat_destroy_listener.link);
761 wl_list_remove(&state->surface_destroy_listener.link);
762 free(state);
763}
764
765static void
766focus_state_seat_destroy(struct wl_listener *listener, void *data)
767{
768 struct focus_state *state = container_of(listener,
769 struct focus_state,
770 seat_destroy_listener);
771
772 wl_list_remove(&state->link);
773 focus_state_destroy(state);
774}
775
776static void
777focus_state_surface_destroy(struct wl_listener *listener, void *data)
778{
779 struct focus_state *state = container_of(listener,
780 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400781 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400782 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500783 struct weston_surface *main_surface, *next;
784 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200785
Pekka Paalanen01388e22013-04-25 13:57:44 +0300786 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
787
Kristian Høgsberge3778222012-07-31 17:29:30 -0400788 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300789 wl_list_for_each(view,
790 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500791 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400792 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100793 if (is_focus_view(view))
794 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400795
Jason Ekstranda7af7042013-10-12 22:38:11 -0500796 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400797 break;
798 }
799
Pekka Paalanen01388e22013-04-25 13:57:44 +0300800 /* if the focus was a sub-surface, activate its main surface */
801 if (main_surface != state->keyboard_focus)
802 next = main_surface;
803
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100804 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400805 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100806 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100807 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400808 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100809 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
810 if (state->ws->focus_animation)
811 weston_view_animation_destroy(state->ws->focus_animation);
812
813 state->ws->focus_animation = weston_fade_run(
814 state->ws->fsurf_front->view,
815 state->ws->fsurf_front->view->alpha, 0.0, 300,
816 focus_animation_done, state->ws);
817 }
818
Kristian Høgsberge3778222012-07-31 17:29:30 -0400819 wl_list_remove(&state->link);
820 focus_state_destroy(state);
821 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200822}
823
824static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400825focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200826{
Jonas Ådahl04769742012-06-13 00:01:24 +0200827 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200828
829 state = malloc(sizeof *state);
830 if (state == NULL)
831 return NULL;
832
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100833 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400834 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200835 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400836 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200837
838 state->seat_destroy_listener.notify = focus_state_seat_destroy;
839 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400840 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200841 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400842 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200843
844 return state;
845}
846
Jonas Ådahl8538b222012-08-29 22:13:03 +0200847static struct focus_state *
848ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
849{
850 struct workspace *ws = get_current_workspace(shell);
851 struct focus_state *state;
852
853 wl_list_for_each(state, &ws->focus_list, link)
854 if (state->seat == seat)
855 break;
856
857 if (&state->link == &ws->focus_list)
858 state = focus_state_create(seat, ws);
859
860 return state;
861}
862
Jonas Ådahl04769742012-06-13 00:01:24 +0200863static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800864focus_state_set_focus(struct focus_state *state,
865 struct weston_surface *surface)
866{
867 if (state->keyboard_focus) {
868 wl_list_remove(&state->surface_destroy_listener.link);
869 wl_list_init(&state->surface_destroy_listener.link);
870 }
871
872 state->keyboard_focus = surface;
873 if (surface)
874 wl_signal_add(&surface->destroy_signal,
875 &state->surface_destroy_listener);
876}
877
878static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400879restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200880{
881 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400882 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100883 struct wl_list pending_seat_list;
884 struct weston_seat *seat, *next_seat;
885
886 /* Temporarily steal the list of seats so that we can keep
887 * track of the seats we've already processed */
888 wl_list_init(&pending_seat_list);
889 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
890 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200891
892 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100893 wl_list_remove(&state->seat->link);
894 wl_list_insert(&shell->compositor->seat_list,
895 &state->seat->link);
896
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800897 if (state->seat->keyboard == NULL)
898 continue;
899
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400900 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200901
Kristian Høgsberge3148752013-05-06 23:19:49 -0400902 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200903 }
Neil Roberts4237f502014-04-09 16:33:31 +0100904
905 /* For any remaining seats that we don't have a focus state
906 * for we'll reset the keyboard focus to NULL */
907 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
908 wl_list_insert(&shell->compositor->seat_list, &seat->link);
909
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300910 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100911 continue;
912
913 weston_keyboard_set_focus(seat->keyboard, NULL);
914 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200915}
916
917static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200918replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
919 struct weston_seat *seat)
920{
921 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200922
923 wl_list_for_each(state, &ws->focus_list, link) {
924 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800925 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200926 return;
927 }
928 }
929}
930
931static void
932drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
933 struct weston_surface *surface)
934{
935 struct focus_state *state;
936
937 wl_list_for_each(state, &ws->focus_list, link)
938 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800939 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200940}
941
942static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100943animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
944 struct weston_view *from, struct weston_view *to)
945{
946 struct weston_output *output;
947 bool focus_surface_created = false;
948
949 /* FIXME: Only support dim animation using two layers */
950 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
951 return;
952
953 output = get_default_output(shell->compositor);
954 if (ws->fsurf_front == NULL && (from || to)) {
955 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800956 if (ws->fsurf_front == NULL)
957 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100958 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800959
960 ws->fsurf_back = create_focus_surface(shell->compositor, output);
961 if (ws->fsurf_back == NULL) {
962 focus_surface_destroy(ws->fsurf_front);
963 return;
964 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100965 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800966
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100967 focus_surface_created = true;
968 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300969 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
970 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100971 }
972
973 if (ws->focus_animation) {
974 weston_view_animation_destroy(ws->focus_animation);
975 ws->focus_animation = NULL;
976 }
977
978 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300979 weston_layer_entry_insert(&to->layer_link,
980 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100981 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300982 weston_layer_entry_insert(&ws->layer.view_list,
983 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100984
985 if (focus_surface_created) {
986 ws->focus_animation = weston_fade_run(
987 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200988 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100989 focus_animation_done, ws);
990 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300991 weston_layer_entry_insert(&from->layer_link,
992 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100993 ws->focus_animation = weston_stable_fade_run(
994 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200995 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996 focus_animation_done, ws);
997 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300998 weston_layer_entry_insert(&ws->layer.view_list,
999 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001000 ws->focus_animation = weston_stable_fade_run(
1001 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001002 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001003 focus_animation_done, ws);
1004 }
1005}
1006
1007static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001008workspace_destroy(struct workspace *ws)
1009{
Jonas Ådahl04769742012-06-13 00:01:24 +02001010 struct focus_state *state, *next;
1011
1012 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1013 focus_state_destroy(state);
1014
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001015 if (ws->fsurf_front)
1016 focus_surface_destroy(ws->fsurf_front);
1017 if (ws->fsurf_back)
1018 focus_surface_destroy(ws->fsurf_back);
1019
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001020 free(ws);
1021}
1022
Jonas Ådahl04769742012-06-13 00:01:24 +02001023static void
1024seat_destroyed(struct wl_listener *listener, void *data)
1025{
1026 struct weston_seat *seat = data;
1027 struct focus_state *state, *next;
1028 struct workspace *ws = container_of(listener,
1029 struct workspace,
1030 seat_destroyed_listener);
1031
1032 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1033 if (state->seat == seat)
1034 wl_list_remove(&state->link);
1035}
1036
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001037static struct workspace *
1038workspace_create(void)
1039{
1040 struct workspace *ws = malloc(sizeof *ws);
1041 if (ws == NULL)
1042 return NULL;
1043
1044 weston_layer_init(&ws->layer, NULL);
1045
Jonas Ådahl04769742012-06-13 00:01:24 +02001046 wl_list_init(&ws->focus_list);
1047 wl_list_init(&ws->seat_destroyed_listener.link);
1048 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001049 ws->fsurf_front = NULL;
1050 ws->fsurf_back = NULL;
1051 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001052
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001053 return ws;
1054}
1055
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001056static int
1057workspace_is_empty(struct workspace *ws)
1058{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001059 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001060}
1061
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001062static struct workspace *
1063get_workspace(struct desktop_shell *shell, unsigned int index)
1064{
1065 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001066 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001067 pws += index;
1068 return *pws;
1069}
1070
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001071struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001072get_current_workspace(struct desktop_shell *shell)
1073{
1074 return get_workspace(shell, shell->workspaces.current);
1075}
1076
1077static void
1078activate_workspace(struct desktop_shell *shell, unsigned int index)
1079{
1080 struct workspace *ws;
1081
1082 ws = get_workspace(shell, index);
1083 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1084
1085 shell->workspaces.current = index;
1086}
1087
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001088static unsigned int
1089get_output_height(struct weston_output *output)
1090{
1091 return abs(output->region.extents.y1 - output->region.extents.y2);
1092}
1093
1094static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001095view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001096{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097 struct weston_transform *transform;
1098
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001099 if (is_focus_view(view)) {
1100 struct focus_surface *fsurf = get_focus_surface(view->surface);
1101 transform = &fsurf->workspace_transform;
1102 } else {
1103 struct shell_surface *shsurf = get_shell_surface(view->surface);
1104 transform = &shsurf->workspace_transform;
1105 }
1106
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001108 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001109 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001110
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001111 weston_matrix_init(&transform->matrix);
1112 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001113 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001114 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001115}
1116
1117static void
1118workspace_translate_out(struct workspace *ws, double fraction)
1119{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001121 unsigned int height;
1122 double d;
1123
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001124 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126 d = height * fraction;
1127
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001128 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129 }
1130}
1131
1132static void
1133workspace_translate_in(struct workspace *ws, double fraction)
1134{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001136 unsigned int height;
1137 double d;
1138
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001139 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001141
1142 if (fraction > 0)
1143 d = -(height - height * fraction);
1144 else
1145 d = height + height * fraction;
1146
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001147 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001148 }
1149}
1150
1151static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001152broadcast_current_workspace_state(struct desktop_shell *shell)
1153{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001154 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001155
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001156 wl_resource_for_each(resource, &shell->workspaces.client_list)
1157 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001158 shell->workspaces.current,
1159 shell->workspaces.num);
1160}
1161
1162static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163reverse_workspace_change_animation(struct desktop_shell *shell,
1164 unsigned int index,
1165 struct workspace *from,
1166 struct workspace *to)
1167{
1168 shell->workspaces.current = index;
1169
1170 shell->workspaces.anim_to = to;
1171 shell->workspaces.anim_from = from;
1172 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1173 shell->workspaces.anim_timestamp = 0;
1174
Scott Moreau4272e632012-08-13 09:58:41 -06001175 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001176}
1177
1178static void
1179workspace_deactivate_transforms(struct workspace *ws)
1180{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001182 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001183
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001184 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001185 if (is_focus_view(view)) {
1186 struct focus_surface *fsurf = get_focus_surface(view->surface);
1187 transform = &fsurf->workspace_transform;
1188 } else {
1189 struct shell_surface *shsurf = get_shell_surface(view->surface);
1190 transform = &shsurf->workspace_transform;
1191 }
1192
1193 if (!wl_list_empty(&transform->link)) {
1194 wl_list_remove(&transform->link);
1195 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001196 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001198 }
1199}
1200
1201static void
1202finish_workspace_change_animation(struct desktop_shell *shell,
1203 struct workspace *from,
1204 struct workspace *to)
1205{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001206 struct weston_view *view;
1207
Scott Moreau4272e632012-08-13 09:58:41 -06001208 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001209
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001210 /* Views that extend past the bottom of the output are still
1211 * visible after the workspace animation ends but before its layer
1212 * is hidden. In that case, we need to damage below those views so
1213 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001214 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001215 weston_view_damage_below(view);
1216
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001217 wl_list_remove(&shell->workspaces.animation.link);
1218 workspace_deactivate_transforms(from);
1219 workspace_deactivate_transforms(to);
1220 shell->workspaces.anim_to = NULL;
1221
1222 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1223}
1224
1225static void
1226animate_workspace_change_frame(struct weston_animation *animation,
1227 struct weston_output *output, uint32_t msecs)
1228{
1229 struct desktop_shell *shell =
1230 container_of(animation, struct desktop_shell,
1231 workspaces.animation);
1232 struct workspace *from = shell->workspaces.anim_from;
1233 struct workspace *to = shell->workspaces.anim_to;
1234 uint32_t t;
1235 double x, y;
1236
1237 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1238 finish_workspace_change_animation(shell, from, to);
1239 return;
1240 }
1241
1242 if (shell->workspaces.anim_timestamp == 0) {
1243 if (shell->workspaces.anim_current == 0.0)
1244 shell->workspaces.anim_timestamp = msecs;
1245 else
1246 shell->workspaces.anim_timestamp =
1247 msecs -
1248 /* Invers of movement function 'y' below. */
1249 (asin(1.0 - shell->workspaces.anim_current) *
1250 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1251 M_2_PI);
1252 }
1253
1254 t = msecs - shell->workspaces.anim_timestamp;
1255
1256 /*
1257 * x = [0, π/2]
1258 * y(x) = sin(x)
1259 */
1260 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1261 y = sin(x);
1262
1263 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001264 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001265
1266 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1267 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1268 shell->workspaces.anim_current = y;
1269
Scott Moreau4272e632012-08-13 09:58:41 -06001270 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001271 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001272 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001273 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001274}
1275
1276static void
1277animate_workspace_change(struct desktop_shell *shell,
1278 unsigned int index,
1279 struct workspace *from,
1280 struct workspace *to)
1281{
1282 struct weston_output *output;
1283
1284 int dir;
1285
1286 if (index > shell->workspaces.current)
1287 dir = -1;
1288 else
1289 dir = 1;
1290
1291 shell->workspaces.current = index;
1292
1293 shell->workspaces.anim_dir = dir;
1294 shell->workspaces.anim_from = from;
1295 shell->workspaces.anim_to = to;
1296 shell->workspaces.anim_current = 0.0;
1297 shell->workspaces.anim_timestamp = 0;
1298
1299 output = container_of(shell->compositor->output_list.next,
1300 struct weston_output, link);
1301 wl_list_insert(&output->animation_list,
1302 &shell->workspaces.animation.link);
1303
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001304 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001305
1306 workspace_translate_in(to, 0);
1307
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001308 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001309
Scott Moreau4272e632012-08-13 09:58:41 -06001310 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001311}
1312
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001313static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001314update_workspace(struct desktop_shell *shell, unsigned int index,
1315 struct workspace *from, struct workspace *to)
1316{
1317 shell->workspaces.current = index;
1318 wl_list_insert(&from->layer.link, &to->layer.link);
1319 wl_list_remove(&from->layer.link);
1320}
1321
1322static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001323change_workspace(struct desktop_shell *shell, unsigned int index)
1324{
1325 struct workspace *from;
1326 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001327 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001328
1329 if (index == shell->workspaces.current)
1330 return;
1331
1332 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001333 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001334 return;
1335
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001336 from = get_current_workspace(shell);
1337 to = get_workspace(shell, index);
1338
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001339 if (shell->workspaces.anim_from == to &&
1340 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001341 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001342 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001343 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001344 return;
1345 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001346
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001347 if (shell->workspaces.anim_to != NULL)
1348 finish_workspace_change_animation(shell,
1349 shell->workspaces.anim_from,
1350 shell->workspaces.anim_to);
1351
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001352 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001353
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001354 if (shell->focus_animation_type != ANIMATION_NONE) {
1355 wl_list_for_each(state, &from->focus_list, link)
1356 if (state->keyboard_focus)
1357 animate_focus_change(shell, from,
1358 get_default_view(state->keyboard_focus), NULL);
1359
1360 wl_list_for_each(state, &to->focus_list, link)
1361 if (state->keyboard_focus)
1362 animate_focus_change(shell, to,
1363 NULL, get_default_view(state->keyboard_focus));
1364 }
1365
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001366 if (workspace_is_empty(to) && workspace_is_empty(from))
1367 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001368 else
1369 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001370
1371 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001372}
1373
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001374static bool
1375workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1376{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001377 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001378 struct wl_list *e;
1379
1380 if (wl_list_empty(list))
1381 return false;
1382
1383 e = list->next;
1384
1385 if (e->next != list)
1386 return false;
1387
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001388 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001389}
1390
1391static void
Philip Withnall659163d2013-11-25 18:01:36 +00001392move_surface_to_workspace(struct desktop_shell *shell,
1393 struct shell_surface *shsurf,
1394 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001395{
1396 struct workspace *from;
1397 struct workspace *to;
1398 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001399 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001400 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001401
1402 if (workspace == shell->workspaces.current)
1403 return;
1404
Philip Withnall659163d2013-11-25 18:01:36 +00001405 view = get_default_view(shsurf->surface);
1406 if (!view)
1407 return;
1408
1409 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1410
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001411 if (workspace >= shell->workspaces.num)
1412 workspace = shell->workspaces.num - 1;
1413
Jonas Ådahle9d22502012-08-29 22:13:01 +02001414 from = get_current_workspace(shell);
1415 to = get_workspace(shell, workspace);
1416
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001417 weston_layer_entry_remove(&view->layer_link);
1418 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001419
Philip Withnall648a4dd2013-11-25 18:01:44 +00001420 shell_surface_update_child_surface_layers(shsurf);
1421
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001423 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1424 if (!seat->keyboard)
1425 continue;
1426
1427 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001429 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001430 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001433}
1434
1435static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001436take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001437 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001438 unsigned int index)
1439{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001440 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001442 struct shell_surface *shsurf;
1443 struct workspace *from;
1444 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001445 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001446
Pekka Paalanen01388e22013-04-25 13:57:44 +03001447 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 view = get_default_view(surface);
1449 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001450 index == shell->workspaces.current ||
1451 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001452 return;
1453
1454 from = get_current_workspace(shell);
1455 to = get_workspace(shell, index);
1456
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001457 weston_layer_entry_remove(&view->layer_link);
1458 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001459
Philip Withnall659163d2013-11-25 18:01:36 +00001460 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001461 if (shsurf != NULL)
1462 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001463
Jonas Ådahle9d22502012-08-29 22:13:01 +02001464 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001465 drop_focus_state(shell, from, surface);
1466
1467 if (shell->workspaces.anim_from == to &&
1468 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001469 wl_list_remove(&to->layer.link);
1470 wl_list_insert(from->layer.link.prev, &to->layer.link);
1471
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001472 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001473 broadcast_current_workspace_state(shell);
1474
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001475 return;
1476 }
1477
1478 if (shell->workspaces.anim_to != NULL)
1479 finish_workspace_change_animation(shell,
1480 shell->workspaces.anim_from,
1481 shell->workspaces.anim_to);
1482
1483 if (workspace_is_empty(from) &&
1484 workspace_has_only(to, surface))
1485 update_workspace(shell, index, from, to);
1486 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001487 if (shsurf != NULL &&
1488 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001489 wl_list_insert(&shell->workspaces.anim_sticky_list,
1490 &shsurf->workspace_transform.link);
1491
1492 animate_workspace_change(shell, index, from, to);
1493 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001494
1495 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001496
1497 state = ensure_focus_state(shell, seat);
1498 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001499 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001500}
1501
1502static void
1503workspace_manager_move_surface(struct wl_client *client,
1504 struct wl_resource *resource,
1505 struct wl_resource *surface_resource,
1506 uint32_t workspace)
1507{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001508 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001509 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001510 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001511 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001512 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001513
Pekka Paalanen01388e22013-04-25 13:57:44 +03001514 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001515 shell_surface = get_shell_surface(main_surface);
1516 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001517 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001518
1519 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001520}
1521
1522static const struct workspace_manager_interface workspace_manager_implementation = {
1523 workspace_manager_move_surface,
1524};
1525
1526static void
1527unbind_resource(struct wl_resource *resource)
1528{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001529 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001530}
1531
1532static void
1533bind_workspace_manager(struct wl_client *client,
1534 void *data, uint32_t version, uint32_t id)
1535{
1536 struct desktop_shell *shell = data;
1537 struct wl_resource *resource;
1538
Jason Ekstranda85118c2013-06-27 20:17:02 -05001539 resource = wl_resource_create(client,
1540 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001541
1542 if (resource == NULL) {
1543 weston_log("couldn't add workspace manager object");
1544 return;
1545 }
1546
Jason Ekstranda85118c2013-06-27 20:17:02 -05001547 wl_resource_set_implementation(resource,
1548 &workspace_manager_implementation,
1549 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001550 wl_list_insert(&shell->workspaces.client_list,
1551 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001552
1553 workspace_manager_send_state(resource,
1554 shell->workspaces.current,
1555 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001556}
1557
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001558static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001559touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1560 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1561{
1562}
1563
1564static void
1565touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1566{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001567 struct weston_touch_move_grab *move =
1568 (struct weston_touch_move_grab *) container_of(
1569 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001570
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001571 if (touch_id == 0)
1572 move->active = 0;
1573
Jonas Ådahl9484b692013-12-02 22:05:03 +01001574 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001575 shell_touch_grab_end(&move->base);
1576 free(move);
1577 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001578}
1579
1580static void
1581touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1582 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1583{
1584 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1585 struct shell_surface *shsurf = move->base.shsurf;
1586 struct weston_surface *es;
1587 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1588 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1589
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001590 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001591 return;
1592
1593 es = shsurf->surface;
1594
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001595 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001596
1597 weston_compositor_schedule_repaint(es->compositor);
1598}
1599
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001600static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001601touch_move_grab_frame(struct weston_touch_grab *grab)
1602{
1603}
1604
1605static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001606touch_move_grab_cancel(struct weston_touch_grab *grab)
1607{
1608 struct weston_touch_move_grab *move =
1609 (struct weston_touch_move_grab *) container_of(
1610 grab, struct shell_touch_grab, grab);
1611
1612 shell_touch_grab_end(&move->base);
1613 free(move);
1614}
1615
Rusty Lynch1084da52013-08-15 09:10:08 -07001616static const struct weston_touch_grab_interface touch_move_grab_interface = {
1617 touch_move_grab_down,
1618 touch_move_grab_up,
1619 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001620 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001621 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001622};
1623
1624static int
1625surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1626{
1627 struct weston_touch_move_grab *move;
1628
1629 if (!shsurf)
1630 return -1;
1631
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001632 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001633 return 0;
1634
1635 move = malloc(sizeof *move);
1636 if (!move)
1637 return -1;
1638
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001639 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001641 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001642 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001643 seat->touch->grab_y;
1644
1645 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1646 seat->touch);
1647
1648 return 0;
1649}
1650
1651static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001652noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001653{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001654}
1655
1656static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001657constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1658{
1659 struct shell_surface *shsurf = move->base.shsurf;
1660 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001661 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001662 const int safety = 50;
1663
1664 x = wl_fixed_to_int(pointer->x + move->dx);
1665 y = wl_fixed_to_int(pointer->y + move->dy);
1666
Jonny Lambd73c6942014-08-20 15:53:20 +02001667 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1668 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1669 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001670
Jonny Lambd73c6942014-08-20 15:53:20 +02001671 bottom = y + shsurf->geometry.height;
1672 if (bottom - panel_height < safety)
1673 y = panel_height + safety -
1674 shsurf->geometry.height;
1675
1676 if (move->client_initiated &&
1677 y + shsurf->geometry.y < panel_height)
1678 y = panel_height - shsurf->geometry.y;
1679 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001680
1681 *cx = x;
1682 *cy = y;
1683}
1684
1685static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001686move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1687 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001688{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001689 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001690 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001691 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001692 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001693
1694 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001695 if (!shsurf)
1696 return;
1697
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001698 constrain_position(move, &cx, &cy);
1699
1700 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001701
Jason Ekstranda7af7042013-10-12 22:38:11 -05001702 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001703}
1704
1705static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001706move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001707 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001708{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001709 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1710 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001711 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001712 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001713
Daniel Stone4dbadb12012-05-30 16:31:51 +01001714 if (pointer->button_count == 0 &&
1715 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001716 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001717 free(grab);
1718 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001719}
1720
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001721static void
1722move_grab_cancel(struct weston_pointer_grab *grab)
1723{
1724 struct shell_grab *shell_grab =
1725 container_of(grab, struct shell_grab, grab);
1726
1727 shell_grab_end(shell_grab);
1728 free(grab);
1729}
1730
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001731static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001732 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001733 move_grab_motion,
1734 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001735 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001736};
1737
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001738static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001739surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1740 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001741{
1742 struct weston_move_grab *move;
1743
1744 if (!shsurf)
1745 return -1;
1746
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001747 if (shsurf->grabbed ||
1748 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001749 return 0;
1750
1751 move = malloc(sizeof *move);
1752 if (!move)
1753 return -1;
1754
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001756 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001758 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001759 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001760
1761 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001762 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001763
1764 return 0;
1765}
1766
1767static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001768common_surface_move(struct wl_resource *resource,
1769 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001770{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001771 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001772 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001773 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001775 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001776 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001777 seat->pointer->button_count > 0 &&
1778 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001780 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001781 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001782 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001783 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001784 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001785 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001787 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001788 (surface_touch_move(shsurf, seat) < 0))
1789 wl_resource_post_no_memory(resource);
1790 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001791}
1792
Rafael Antognollie2a34552013-12-03 15:35:45 -02001793static void
1794shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1795 struct wl_resource *seat_resource, uint32_t serial)
1796{
1797 common_surface_move(resource, seat_resource, serial);
1798}
1799
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001800struct weston_resize_grab {
1801 struct shell_grab base;
1802 uint32_t edges;
1803 int32_t width, height;
1804};
1805
1806static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001807resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1808 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001809{
1810 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001811 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001812 struct shell_surface *shsurf = resize->base.shsurf;
1813 int32_t width, height;
1814 wl_fixed_t from_x, from_y;
1815 wl_fixed_t to_x, to_y;
1816
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001817 weston_pointer_move(pointer, x, y);
1818
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001819 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001820 return;
1821
Jason Ekstranda7af7042013-10-12 22:38:11 -05001822 weston_view_from_global_fixed(shsurf->view,
1823 pointer->grab_x, pointer->grab_y,
1824 &from_x, &from_y);
1825 weston_view_from_global_fixed(shsurf->view,
1826 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001827
1828 width = resize->width;
1829 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1830 width += wl_fixed_to_int(from_x - to_x);
1831 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1832 width += wl_fixed_to_int(to_x - from_x);
1833 }
1834
1835 height = resize->height;
1836 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1837 height += wl_fixed_to_int(from_y - to_y);
1838 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1839 height += wl_fixed_to_int(to_y - from_y);
1840 }
1841
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001842 if (width < 1)
1843 width = 1;
1844 if (height < 1)
1845 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001846 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001847}
1848
1849static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001850send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001851{
1852 struct shell_surface *shsurf = get_shell_surface(surface);
1853
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001854 assert(shsurf);
1855
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001856 if (shsurf->resource)
1857 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001858 shsurf->resize_edges,
1859 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001860}
1861
1862static const struct weston_shell_client shell_client = {
1863 send_configure
1864};
1865
1866static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001867resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001868 uint32_t time, uint32_t button, uint32_t state_w)
1869{
1870 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001871 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001872 enum wl_pointer_button_state state = state_w;
1873
1874 if (pointer->button_count == 0 &&
1875 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1876 shell_grab_end(&resize->base);
1877 free(grab);
1878 }
1879}
1880
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001881static void
1882resize_grab_cancel(struct weston_pointer_grab *grab)
1883{
1884 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1885
1886 shell_grab_end(&resize->base);
1887 free(grab);
1888}
1889
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001890static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001891 noop_grab_focus,
1892 resize_grab_motion,
1893 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001894 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001895};
1896
Giulio Camuffob8366642013-04-25 13:57:46 +03001897/*
1898 * Returns the bounding box of a surface and all its sub-surfaces,
1899 * in the surface coordinates system. */
1900static void
1901surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1902 int32_t *y, int32_t *w, int32_t *h) {
1903 pixman_region32_t region;
1904 pixman_box32_t *box;
1905 struct weston_subsurface *subsurface;
1906
1907 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001908 surface->width,
1909 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001910
1911 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1912 pixman_region32_union_rect(&region, &region,
1913 subsurface->position.x,
1914 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001915 subsurface->surface->width,
1916 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001917 }
1918
1919 box = pixman_region32_extents(&region);
1920 if (x)
1921 *x = box->x1;
1922 if (y)
1923 *y = box->y1;
1924 if (w)
1925 *w = box->x2 - box->x1;
1926 if (h)
1927 *h = box->y2 - box->y1;
1928
1929 pixman_region32_fini(&region);
1930}
1931
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001932static int
1933surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001934 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001935{
1936 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001937 const unsigned resize_topbottom =
1938 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1939 const unsigned resize_leftright =
1940 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1941 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001942
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001943 if (shsurf->grabbed ||
1944 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001945 return 0;
1946
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001947 /* Check for invalid edge combinations. */
1948 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1949 (edges & resize_topbottom) == resize_topbottom ||
1950 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001951 return 0;
1952
1953 resize = malloc(sizeof *resize);
1954 if (!resize)
1955 return -1;
1956
1957 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001958
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001959 resize->width = shsurf->geometry.width;
1960 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001961
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001962 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001963 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001964 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001965 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001966
1967 return 0;
1968}
1969
1970static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001971common_surface_resize(struct wl_resource *resource,
1972 struct wl_resource *seat_resource, uint32_t serial,
1973 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001974{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001975 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001976 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001977 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001978
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001979 if (seat->pointer == NULL ||
1980 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001981 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001982 seat->pointer->focus == NULL)
1983 return;
1984
1985 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1986 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001987 return;
1988
Kristian Høgsberge3148752013-05-06 23:19:49 -04001989 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001990 wl_resource_post_no_memory(resource);
1991}
1992
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001993static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001994shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1995 struct wl_resource *seat_resource, uint32_t serial,
1996 uint32_t edges)
1997{
1998 common_surface_resize(resource, seat_resource, serial, edges);
1999}
2000
2001static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002002busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002003{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002004 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002005 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002006 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002007 wl_fixed_t sx, sy;
2008
Jason Ekstranda7af7042013-10-12 22:38:11 -05002009 view = weston_compositor_pick_view(pointer->seat->compositor,
2010 pointer->x, pointer->y,
2011 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002012
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002013 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2014 shell_grab_end(grab);
2015 free(grab);
2016 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002017}
2018
2019static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002020busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2021 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002022{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002023 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002024}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002025
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002026static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002027busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002028 uint32_t time, uint32_t button, uint32_t state)
2029{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002030 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002031 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002032 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002033
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002034 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002035 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002036 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002037 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002038 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002039 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002040 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002041}
2042
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002043static void
2044busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2045{
2046 struct shell_grab *grab = (struct shell_grab *) base;
2047
2048 shell_grab_end(grab);
2049 free(grab);
2050}
2051
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002052static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002053 busy_cursor_grab_focus,
2054 busy_cursor_grab_motion,
2055 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002056 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002057};
2058
2059static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002060set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002061{
2062 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002063
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002064 if (pointer->grab->interface == &busy_cursor_grab_interface)
2065 return;
2066
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002067 grab = malloc(sizeof *grab);
2068 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002069 return;
2070
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002071 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2072 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002073 /* Mark the shsurf as ungrabbed so that button binding is able
2074 * to move it. */
2075 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002076}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002077
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002078static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002079end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002080{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002081 struct shell_grab *grab;
2082 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002083
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002084 wl_list_for_each(seat, &compositor->seat_list, link) {
2085 if (seat->pointer == NULL)
2086 continue;
2087
2088 grab = (struct shell_grab *) seat->pointer->grab;
2089 if (grab->grab.interface == &busy_cursor_grab_interface &&
2090 wl_resource_get_client(grab->shsurf->resource) == client) {
2091 shell_grab_end(grab);
2092 free(grab);
2093 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002094 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002095}
2096
Scott Moreau9521d5e2012-04-19 13:06:17 -06002097static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002098handle_shell_client_destroy(struct wl_listener *listener, void *data);
2099
2100static int
2101xdg_ping_timeout_handler(void *data)
2102{
2103 struct shell_client *sc = data;
2104 struct weston_seat *seat;
2105 struct shell_surface *shsurf;
2106
2107 /* Client is not responding */
2108 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002109 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2110 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2111 continue;
2112 if (seat->pointer->focus->surface->resource == NULL)
2113 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002114
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002115 shsurf = get_shell_surface(seat->pointer->focus->surface);
2116 if (shsurf &&
2117 wl_resource_get_client(shsurf->resource) == sc->client)
2118 set_busy_cursor(shsurf, seat->pointer);
2119 }
2120
2121 return 1;
2122}
2123
2124static void
2125handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2126{
2127 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002128 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002129 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002130 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002131
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002132 if (sc->unresponsive) {
2133 xdg_ping_timeout_handler(sc);
2134 return;
2135 }
2136
2137 sc->ping_serial = serial;
2138 loop = wl_display_get_event_loop(compositor->wl_display);
2139 if (sc->ping_timer == NULL)
2140 sc->ping_timer =
2141 wl_event_loop_add_timer(loop,
2142 xdg_ping_timeout_handler, sc);
2143 if (sc->ping_timer == NULL)
2144 return;
2145
2146 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2147
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002148 if (shell_surface_is_xdg_surface(shsurf) ||
2149 shell_surface_is_xdg_popup(shsurf))
2150 xdg_shell_send_ping(sc->resource, serial);
2151 else if (shell_surface_is_wl_shell_surface(shsurf))
2152 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002153}
2154
Scott Moreauff1db4a2012-04-17 19:06:18 -06002155static void
2156ping_handler(struct weston_surface *surface, uint32_t serial)
2157{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002158 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002159
2160 if (!shsurf)
2161 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002162 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002163 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002164 if (shsurf->surface == shsurf->shell->grab_surface)
2165 return;
2166
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002167 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002168}
2169
2170static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002171handle_pointer_focus(struct wl_listener *listener, void *data)
2172{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002173 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002174 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002175 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002176 uint32_t serial;
2177
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002179 return;
2180
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002182 serial = wl_display_next_serial(compositor->wl_display);
2183 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002184}
2185
2186static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002187shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2188{
2189 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002190 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002191}
2192
2193static void
2194shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2195{
2196 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002197 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002198}
2199
2200static void
2201handle_keyboard_focus(struct wl_listener *listener, void *data)
2202{
2203 struct weston_keyboard *keyboard = data;
2204 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2205
2206 if (seat->focused_surface) {
2207 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2208 if (shsurf)
2209 shell_surface_lose_keyboard_focus(shsurf);
2210 }
2211
2212 seat->focused_surface = keyboard->focus;
2213
2214 if (seat->focused_surface) {
2215 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2216 if (shsurf)
2217 shell_surface_gain_keyboard_focus(shsurf);
2218 }
2219}
2220
2221static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002222shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002223{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002224 if (sc->ping_serial != serial)
2225 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002226
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002227 sc->unresponsive = 0;
2228 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002229
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002230 if (sc->ping_timer) {
2231 wl_event_source_remove(sc->ping_timer);
2232 sc->ping_timer = NULL;
2233 }
2234
2235}
2236
2237static void
2238shell_surface_pong(struct wl_client *client,
2239 struct wl_resource *resource, uint32_t serial)
2240{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002241 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2242 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002243
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002244 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002245}
2246
2247static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002248set_title(struct shell_surface *shsurf, const char *title)
2249{
2250 free(shsurf->title);
2251 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002252 shsurf->surface->timeline.force_refresh = 1;
2253}
2254
2255static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002256set_pid(struct shell_surface *shsurf, pid_t pid)
2257{
2258 /* We have no use for it */
2259}
2260
2261static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002262set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2263{
2264 shsurf->type = t;
2265 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002266}
2267
2268static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002269set_window_geometry(struct shell_surface *shsurf,
2270 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002271{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002272 shsurf->next_geometry.x = x;
2273 shsurf->next_geometry.y = y;
2274 shsurf->next_geometry.width = width;
2275 shsurf->next_geometry.height = height;
2276 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002277}
2278
2279static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002280shell_surface_set_title(struct wl_client *client,
2281 struct wl_resource *resource, const char *title)
2282{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002283 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002284
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002285 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002286}
2287
2288static void
2289shell_surface_set_class(struct wl_client *client,
2290 struct wl_resource *resource, const char *class)
2291{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002292 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002293
2294 free(shsurf->class);
2295 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002296 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002297}
2298
Alex Wu4539b082012-03-01 12:57:46 +08002299static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002300restore_output_mode(struct weston_output *output)
2301{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002302 if (output->original_mode)
2303 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002304}
2305
2306static void
2307restore_all_output_modes(struct weston_compositor *compositor)
2308{
2309 struct weston_output *output;
2310
2311 wl_list_for_each(output, &compositor->output_list, link)
2312 restore_output_mode(output);
2313}
2314
Philip Withnall07926d92013-11-25 18:01:40 +00002315/* The surface will be inserted into the list immediately after the link
2316 * returned by this function (i.e. will be stacked immediately above the
2317 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002318static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002319shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2320{
2321 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002322 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002323
2324 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002325 case SHELL_SURFACE_XWAYLAND:
2326 return &shsurf->shell->fullscreen_layer.view_list;
2327
2328 case SHELL_SURFACE_NONE:
2329 return NULL;
2330
Kristian Høgsbergead52422014-01-01 13:51:52 -08002331 case SHELL_SURFACE_POPUP:
2332 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002333 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002334 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002335 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002336 /* Move the surface to its parent layer so
2337 * that surfaces which are transient for
2338 * fullscreen surfaces don't get hidden by the
2339 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002340
2341 /* TODO: Handle a parent with multiple views */
2342 parent = get_default_view(shsurf->parent);
2343 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002344 return container_of(parent->layer_link.link.prev,
2345 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002346 }
Philip Withnall07926d92013-11-25 18:01:40 +00002347
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002348 /* Move the surface to a normal workspace layer so that surfaces
2349 * which were previously fullscreen or transient are no longer
2350 * rendered on top. */
2351 ws = get_current_workspace(shsurf->shell);
2352 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002353 }
2354
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002355 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002356}
2357
Philip Withnall648a4dd2013-11-25 18:01:44 +00002358static void
2359shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2360{
2361 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002362 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002363
2364 /* Move the child layers to the same workspace as shsurf. They will be
2365 * stacked above shsurf. */
2366 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002367 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002368 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002369 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002370 prev = container_of(shsurf->view->layer_link.link.prev,
2371 struct weston_layer_entry, link);
2372 weston_layer_entry_remove(&child->view->layer_link);
2373 weston_layer_entry_insert(prev,
2374 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002375 weston_view_geometry_dirty(child->view);
2376 weston_surface_damage(child->surface);
2377
2378 /* Recurse. We don’t expect this to recurse very far (if
2379 * at all) because that would imply we have transient
2380 * (or popup) children of transient surfaces, which
2381 * would be unusual. */
2382 shell_surface_update_child_surface_layers(child);
2383 }
2384 }
2385}
2386
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002387/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002388 * geometry to ensure the changes are redrawn.
2389 *
2390 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2391 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002392static void
2393shell_surface_update_layer(struct shell_surface *shsurf)
2394{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002395 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002396
2397 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2398
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002399 if (new_layer_link == NULL)
2400 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002401 if (new_layer_link == &shsurf->view->layer_link)
2402 return;
2403
2404 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002405 weston_layer_entry_remove(&shsurf->view->layer_link);
2406 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002407 weston_view_geometry_dirty(shsurf->view);
2408 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002409
2410 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002411}
2412
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002413static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002414shell_surface_set_parent(struct shell_surface *shsurf,
2415 struct weston_surface *parent)
2416{
2417 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002418
2419 wl_list_remove(&shsurf->children_link);
2420 wl_list_init(&shsurf->children_link);
2421
2422 /* Insert into the parent surface’s child list. */
2423 if (parent != NULL) {
2424 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2425 if (parent_shsurf != NULL)
2426 wl_list_insert(&parent_shsurf->children_list,
2427 &shsurf->children_link);
2428 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002429}
2430
2431static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002432shell_surface_set_output(struct shell_surface *shsurf,
2433 struct weston_output *output)
2434{
2435 struct weston_surface *es = shsurf->surface;
2436
2437 /* get the default output, if the client set it as NULL
2438 check whether the ouput is available */
2439 if (output)
2440 shsurf->output = output;
2441 else if (es->output)
2442 shsurf->output = es->output;
2443 else
2444 shsurf->output = get_default_output(es->compositor);
2445}
2446
2447static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002448surface_clear_next_states(struct shell_surface *shsurf)
2449{
2450 shsurf->next_state.maximized = false;
2451 shsurf->next_state.fullscreen = false;
2452
2453 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2454 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2455 shsurf->state_changed = true;
2456}
2457
2458static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002459set_toplevel(struct shell_surface *shsurf)
2460{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002461 shell_surface_set_parent(shsurf, NULL);
2462 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002463 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002464
2465 /* The layer_link is updated in set_surface_type(),
2466 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002467}
2468
2469static void
2470shell_surface_set_toplevel(struct wl_client *client,
2471 struct wl_resource *resource)
2472{
2473 struct shell_surface *surface = wl_resource_get_user_data(resource);
2474
2475 set_toplevel(surface);
2476}
2477
2478static void
2479set_transient(struct shell_surface *shsurf,
2480 struct weston_surface *parent, int x, int y, uint32_t flags)
2481{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002482 assert(parent != NULL);
2483
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002484 shell_surface_set_parent(shsurf, parent);
2485
2486 surface_clear_next_states(shsurf);
2487
Philip Withnallbecb77e2013-11-25 18:01:30 +00002488 shsurf->transient.x = x;
2489 shsurf->transient.y = y;
2490 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002491
Rafael Antognollied207b42013-12-03 15:35:43 -02002492 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002493 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002494 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002495
2496 /* The layer_link is updated in set_surface_type(),
2497 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002498}
2499
2500static void
2501shell_surface_set_transient(struct wl_client *client,
2502 struct wl_resource *resource,
2503 struct wl_resource *parent_resource,
2504 int x, int y, uint32_t flags)
2505{
2506 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2507 struct weston_surface *parent =
2508 wl_resource_get_user_data(parent_resource);
2509
2510 set_transient(shsurf, parent, x, y, flags);
2511}
2512
2513static void
2514set_fullscreen(struct shell_surface *shsurf,
2515 uint32_t method,
2516 uint32_t framerate,
2517 struct weston_output *output)
2518{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002519 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002520 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002521
2522 shsurf->fullscreen_output = shsurf->output;
2523 shsurf->fullscreen.type = method;
2524 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002525
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002526 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002527}
2528
2529static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002530weston_view_set_initial_position(struct weston_view *view,
2531 struct desktop_shell *shell);
2532
2533static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002534unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002535{
Philip Withnallf85fe842013-11-25 18:01:37 +00002536 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002537 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2538 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002539 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002540 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002541
Alex Wu4539b082012-03-01 12:57:46 +08002542 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2543 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002544
Alex Wu4539b082012-03-01 12:57:46 +08002545 wl_list_remove(&shsurf->fullscreen.transform.link);
2546 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002547
Jason Ekstranda7af7042013-10-12 22:38:11 -05002548 if (shsurf->fullscreen.black_view)
2549 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2550 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002551
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002552 if (shsurf->saved_position_valid)
2553 weston_view_set_position(shsurf->view,
2554 shsurf->saved_x, shsurf->saved_y);
2555 else
2556 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2557
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002558 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002559 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002560 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002561 shsurf->saved_rotation_valid = false;
2562 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002563
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002564 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002565}
2566
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002567static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002568shell_surface_set_fullscreen(struct wl_client *client,
2569 struct wl_resource *resource,
2570 uint32_t method,
2571 uint32_t framerate,
2572 struct wl_resource *output_resource)
2573{
2574 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2575 struct weston_output *output;
2576
2577 if (output_resource)
2578 output = wl_resource_get_user_data(output_resource);
2579 else
2580 output = NULL;
2581
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002582 shell_surface_set_parent(shsurf, NULL);
2583
Rafael Antognolli03b16592013-12-03 15:35:42 -02002584 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002585 shsurf->next_state.fullscreen = true;
2586 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002587 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002588}
2589
2590static void
2591set_popup(struct shell_surface *shsurf,
2592 struct weston_surface *parent,
2593 struct weston_seat *seat,
2594 uint32_t serial,
2595 int32_t x,
2596 int32_t y)
2597{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002598 assert(parent != NULL);
2599
Philip Withnallbecb77e2013-11-25 18:01:30 +00002600 shsurf->popup.shseat = get_shell_seat(seat);
2601 shsurf->popup.serial = serial;
2602 shsurf->popup.x = x;
2603 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002604
Pekka Paalanenb5026542014-11-12 15:09:24 +02002605 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002606}
2607
2608static void
2609shell_surface_set_popup(struct wl_client *client,
2610 struct wl_resource *resource,
2611 struct wl_resource *seat_resource,
2612 uint32_t serial,
2613 struct wl_resource *parent_resource,
2614 int32_t x, int32_t y, uint32_t flags)
2615{
2616 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002617 struct weston_surface *parent =
2618 wl_resource_get_user_data(parent_resource);
2619
2620 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002621
Rafael Antognolli03b16592013-12-03 15:35:42 -02002622 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002623 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002624 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002625 wl_resource_get_user_data(seat_resource),
2626 serial, x, y);
2627}
2628
2629static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002630unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002631{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002632 /* undo all maximized things here */
2633 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002634
2635 if (shsurf->saved_position_valid)
2636 weston_view_set_position(shsurf->view,
2637 shsurf->saved_x, shsurf->saved_y);
2638 else
2639 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002640
2641 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002642 wl_list_insert(&shsurf->view->geometry.transformation_list,
2643 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002644 shsurf->saved_rotation_valid = false;
2645 }
2646
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002647 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002648}
2649
Philip Withnallbecb77e2013-11-25 18:01:30 +00002650static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002651set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002652{
2653 struct shell_surface *shsurf;
2654 struct workspace *current_ws;
2655 struct weston_seat *seat;
2656 struct weston_surface *focus;
2657 struct weston_view *view;
2658
2659 view = get_default_view(surface);
2660 if (!view)
2661 return;
2662
2663 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2664
2665 shsurf = get_shell_surface(surface);
2666 current_ws = get_current_workspace(shsurf->shell);
2667
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002668 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002669 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002670
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002671 drop_focus_state(shsurf->shell, current_ws, view->surface);
2672 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2673 if (!seat->keyboard)
2674 continue;
2675 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2676 if (focus == view->surface)
2677 weston_keyboard_set_focus(seat->keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002678 }
2679
2680 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002681 weston_view_damage_below(view);
2682}
2683
2684static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002685shell_surface_set_maximized(struct wl_client *client,
2686 struct wl_resource *resource,
2687 struct wl_resource *output_resource)
2688{
2689 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2690 struct weston_output *output;
2691
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002692 surface_clear_next_states(shsurf);
2693 shsurf->next_state.maximized = true;
2694 shsurf->state_changed = true;
2695
Pekka Paalanenb5026542014-11-12 15:09:24 +02002696 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002697 shell_surface_set_parent(shsurf, NULL);
2698
Philip Withnallbecb77e2013-11-25 18:01:30 +00002699 if (output_resource)
2700 output = wl_resource_get_user_data(output_resource);
2701 else
2702 output = NULL;
2703
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002704 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002705
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002706 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002707}
2708
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002709/* This is only ever called from set_surface_type(), so there’s no need to
2710 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002711static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002712reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002713{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002714 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002715 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002716 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002717 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002718
Pekka Paalanen98262232011-12-01 10:42:22 +02002719 return 0;
2720}
2721
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002722static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002723set_full_output(struct shell_surface *shsurf)
2724{
2725 shsurf->saved_x = shsurf->view->geometry.x;
2726 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002727 shsurf->saved_width = shsurf->surface->width;
2728 shsurf->saved_height = shsurf->surface->height;
2729 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002730 shsurf->saved_position_valid = true;
2731
2732 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2733 wl_list_remove(&shsurf->rotation.transform.link);
2734 wl_list_init(&shsurf->rotation.transform.link);
2735 weston_view_geometry_dirty(shsurf->view);
2736 shsurf->saved_rotation_valid = true;
2737 }
2738}
2739
2740static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002741set_surface_type(struct shell_surface *shsurf)
2742{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002743 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002744 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002745
Philip Withnallbecb77e2013-11-25 18:01:30 +00002746 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002747
Rafael Antognolli03b16592013-12-03 15:35:42 -02002748 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002749 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002750
2751 switch (shsurf->type) {
2752 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002753 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002754 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002755 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756 weston_view_set_position(shsurf->view,
2757 pev->geometry.x + shsurf->transient.x,
2758 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002759 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002760 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002761
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002762 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763 weston_view_set_position(shsurf->view, shsurf->transient.x,
2764 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002765 break;
2766
Philip Withnall0f640e22013-11-25 18:01:31 +00002767 case SHELL_SURFACE_POPUP:
2768 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002769 default:
2770 break;
2771 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002772
2773 /* Update the surface’s layer. */
2774 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002775}
2776
Tiago Vignattibe143262012-04-16 17:31:41 +03002777static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002778shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002779{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002780 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002781}
2782
Pekka Paalanen8274d902014-08-06 19:36:51 +03002783static int
2784black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2785{
2786 struct weston_surface *fs_surface = surface->configure_private;
2787 int n;
2788 int rem;
2789 int ret;
2790
2791 n = snprintf(buf, len, "black background surface for ");
2792 if (n < 0)
2793 return n;
2794
2795 rem = (int)len - n;
2796 if (rem < 0)
2797 rem = 0;
2798
2799 if (fs_surface->get_label)
2800 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2801 else
2802 ret = snprintf(buf + n, rem, "<unknown>");
2803
2804 if (ret < 0)
2805 return n;
2806
2807 return n + ret;
2808}
2809
Alex Wu21858432012-04-01 20:13:08 +08002810static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002811black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002812
Jason Ekstranda7af7042013-10-12 22:38:11 -05002813static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002814create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002815 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002816 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002817{
2818 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002819 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002820
2821 surface = weston_surface_create(ec);
2822 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002823 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002824 return NULL;
2825 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002826 view = weston_view_create(surface);
2827 if (surface == NULL) {
2828 weston_log("no memory\n");
2829 weston_surface_destroy(surface);
2830 return NULL;
2831 }
Alex Wu4539b082012-03-01 12:57:46 +08002832
Alex Wu21858432012-04-01 20:13:08 +08002833 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002834 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002835 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002836 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002837 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002838 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002839 pixman_region32_fini(&surface->input);
2840 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002841
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002842 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002843 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002844
2845 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002846}
2847
Philip Withnalled8f1a92013-11-25 18:01:43 +00002848static void
2849shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2850{
2851 struct weston_output *output = shsurf->fullscreen_output;
2852
Rafael Antognolli03b16592013-12-03 15:35:42 -02002853 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002854
2855 if (!shsurf->fullscreen.black_view)
2856 shsurf->fullscreen.black_view =
2857 create_black_surface(shsurf->surface->compositor,
2858 shsurf->surface,
2859 output->x, output->y,
2860 output->width,
2861 output->height);
2862
2863 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002864 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2865 weston_layer_entry_insert(&shsurf->view->layer_link,
2866 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002867 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2868 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002869
2870 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002871}
2872
Alex Wu4539b082012-03-01 12:57:46 +08002873/* Create black surface and append it to the associated fullscreen surface.
2874 * Handle size dismatch and positioning according to the method. */
2875static void
2876shell_configure_fullscreen(struct shell_surface *shsurf)
2877{
2878 struct weston_output *output = shsurf->fullscreen_output;
2879 struct weston_surface *surface = shsurf->surface;
2880 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002881 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002882 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002883
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002884 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2885 restore_output_mode(output);
2886
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002887 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002888 weston_layer_entry_remove(&shsurf->view->layer_link);
2889 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002890
Philip Withnalled8f1a92013-11-25 18:01:43 +00002891 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002892
Jason Ekstranda7af7042013-10-12 22:38:11 -05002893 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002894 &surf_width, &surf_height);
2895
Alex Wu4539b082012-03-01 12:57:46 +08002896 switch (shsurf->fullscreen.type) {
2897 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002898 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002899 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002900 break;
2901 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002902 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002903 if (output->width == surf_width &&
2904 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002905 weston_view_set_position(shsurf->view,
2906 output->x - surf_x,
2907 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002908 break;
2909 }
2910
Alex Wu4539b082012-03-01 12:57:46 +08002911 matrix = &shsurf->fullscreen.transform.matrix;
2912 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002913
Scott Moreau1bad5db2012-08-18 01:04:05 -06002914 output_aspect = (float) output->width /
2915 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002916 /* XXX: Use surf_width and surf_height here? */
2917 surface_aspect = (float) surface->width /
2918 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002919 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002920 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002921 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002922 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002923 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002924 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002925
Alex Wu4539b082012-03-01 12:57:46 +08002926 weston_matrix_scale(matrix, scale, scale, 1);
2927 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002929 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002930 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2931 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002932 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002933
Alex Wu4539b082012-03-01 12:57:46 +08002934 break;
2935 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002936 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002937 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002938 surf_width * surface->buffer_viewport.buffer.scale,
2939 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002940 shsurf->fullscreen.framerate};
2941
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002942 if (weston_output_mode_switch_to_temporary(output, &mode,
2943 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002944 weston_view_set_position(shsurf->view,
2945 output->x - surf_x,
2946 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002947 shsurf->fullscreen.black_view->surface->width = output->width;
2948 shsurf->fullscreen.black_view->surface->height = output->height;
2949 weston_view_set_position(shsurf->fullscreen.black_view,
2950 output->x - surf_x,
2951 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002952 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002953 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002954 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002955 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002956 }
Alex Wubd3354b2012-04-17 17:20:49 +08002957 }
Alex Wu4539b082012-03-01 12:57:46 +08002958 break;
2959 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002960 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002961 break;
2962 default:
2963 break;
2964 }
2965}
2966
Alex Wu4539b082012-03-01 12:57:46 +08002967static void
2968shell_map_fullscreen(struct shell_surface *shsurf)
2969{
Alex Wubd3354b2012-04-17 17:20:49 +08002970 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002971}
2972
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002973static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002974set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2975{
2976 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002977 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002978 shsurf->transient.x = x;
2979 shsurf->transient.y = y;
2980 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002981
2982 shell_surface_set_parent(shsurf, NULL);
2983
Pekka Paalanenb5026542014-11-12 15:09:24 +02002984 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002985 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002986}
2987
Kristian Høgsberg47792412014-05-04 13:47:06 -07002988static void
2989shell_interface_set_fullscreen(struct shell_surface *shsurf,
2990 uint32_t method,
2991 uint32_t framerate,
2992 struct weston_output *output)
2993{
2994 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002995 shsurf->next_state.fullscreen = true;
2996 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002997
2998 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002999}
3000
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003001static struct weston_output *
3002get_focused_output(struct weston_compositor *compositor)
3003{
3004 struct weston_seat *seat;
3005 struct weston_output *output = NULL;
3006
3007 wl_list_for_each(seat, &compositor->seat_list, link) {
3008 /* Priority has touch focus, then pointer and
3009 * then keyboard focus. We should probably have
3010 * three for loops and check frist for touch,
3011 * then for pointer, etc. but unless somebody has some
3012 * objections, I think this is sufficient. */
3013 if (seat->touch && seat->touch->focus)
3014 output = seat->touch->focus->output;
3015 else if (seat->pointer && seat->pointer->focus)
3016 output = seat->pointer->focus->output;
3017 else if (seat->keyboard && seat->keyboard->focus)
3018 output = seat->keyboard->focus->output;
3019
3020 if (output)
3021 break;
3022 }
3023
3024 return output;
3025}
3026
3027static void
3028shell_interface_set_maximized(struct shell_surface *shsurf)
3029{
3030 struct weston_output *output;
3031
3032 surface_clear_next_states(shsurf);
3033 shsurf->next_state.maximized = true;
3034 shsurf->state_changed = true;
3035 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3036
3037 if (!weston_surface_is_mapped(shsurf->surface))
3038 output = get_focused_output(shsurf->surface->compositor);
3039 else
3040 output = shsurf->surface->output;
3041
3042 shell_surface_set_output(shsurf, output);
3043 send_configure_for_surface(shsurf);
3044}
3045
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003046static int
3047shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3048{
3049 return surface_move(shsurf, ws, 1);
3050}
3051
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003052static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003053
3054static void
3055destroy_shell_seat(struct wl_listener *listener, void *data)
3056{
3057 struct shell_seat *shseat =
3058 container_of(listener,
3059 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003060 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003061
3062 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003063 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003064 shseat->popup_grab.client = NULL;
3065
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003066 wl_list_for_each_safe(shsurf, next,
3067 &shseat->popup_grab.surfaces_list,
3068 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003069 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003070 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003071 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003072 }
3073
3074 wl_list_remove(&shseat->seat_destroy_listener.link);
3075 free(shseat);
3076}
3077
Jason Ekstrand024177c2014-04-21 19:42:58 -05003078static void
3079shell_seat_caps_changed(struct wl_listener *listener, void *data)
3080{
3081 struct shell_seat *seat;
3082
3083 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3084
3085 if (seat->seat->keyboard &&
3086 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3087 wl_signal_add(&seat->seat->keyboard->focus_signal,
3088 &seat->keyboard_focus_listener);
3089 } else if (!seat->seat->keyboard) {
3090 wl_list_init(&seat->keyboard_focus_listener.link);
3091 }
3092
3093 if (seat->seat->pointer &&
3094 wl_list_empty(&seat->pointer_focus_listener.link)) {
3095 wl_signal_add(&seat->seat->pointer->focus_signal,
3096 &seat->pointer_focus_listener);
3097 } else if (!seat->seat->pointer) {
3098 wl_list_init(&seat->pointer_focus_listener.link);
3099 }
3100}
3101
Giulio Camuffo5085a752013-03-25 21:42:45 +01003102static struct shell_seat *
3103create_shell_seat(struct weston_seat *seat)
3104{
3105 struct shell_seat *shseat;
3106
3107 shseat = calloc(1, sizeof *shseat);
3108 if (!shseat) {
3109 weston_log("no memory to allocate shell seat\n");
3110 return NULL;
3111 }
3112
3113 shseat->seat = seat;
3114 wl_list_init(&shseat->popup_grab.surfaces_list);
3115
3116 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3117 wl_signal_add(&seat->destroy_signal,
3118 &shseat->seat_destroy_listener);
3119
Jason Ekstrand024177c2014-04-21 19:42:58 -05003120 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3121 wl_list_init(&shseat->keyboard_focus_listener.link);
3122
3123 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3124 wl_list_init(&shseat->pointer_focus_listener.link);
3125
3126 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3127 wl_signal_add(&seat->updated_caps_signal,
3128 &shseat->caps_changed_listener);
3129 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3130
Giulio Camuffo5085a752013-03-25 21:42:45 +01003131 return shseat;
3132}
3133
3134static struct shell_seat *
3135get_shell_seat(struct weston_seat *seat)
3136{
3137 struct wl_listener *listener;
3138
3139 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003140 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003141
3142 return container_of(listener,
3143 struct shell_seat, seat_destroy_listener);
3144}
3145
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003146static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003147popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003148{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003149 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003150 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003151 struct shell_seat *shseat =
3152 container_of(grab, struct shell_seat, popup_grab.grab);
3153 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003154 wl_fixed_t sx, sy;
3155
Jason Ekstranda7af7042013-10-12 22:38:11 -05003156 view = weston_compositor_pick_view(pointer->seat->compositor,
3157 pointer->x, pointer->y,
3158 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003159
Jason Ekstranda7af7042013-10-12 22:38:11 -05003160 if (view && view->surface->resource &&
3161 wl_resource_get_client(view->surface->resource) == client) {
3162 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003163 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003164 weston_pointer_set_focus(pointer, NULL,
3165 wl_fixed_from_int(0),
3166 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003167 }
3168}
3169
3170static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003171popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3172 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003173{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003174 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003175 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003176 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003177
Jonas Ådahl61917c82014-08-11 22:44:02 +02003178 if (pointer->focus) {
3179 weston_view_from_global_fixed(pointer->focus, x, y,
3180 &pointer->sx, &pointer->sy);
3181 }
3182
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003183 weston_pointer_move(pointer, x, y);
3184
Neil Roberts96d790e2013-09-19 17:32:00 +01003185 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003186 weston_view_from_global_fixed(pointer->focus,
3187 pointer->x, pointer->y,
3188 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003189 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003190 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003191}
3192
3193static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003194popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003195 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003196{
3197 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003198 struct shell_seat *shseat =
3199 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003200 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003201 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003202 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003203 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003204
Neil Roberts96d790e2013-09-19 17:32:00 +01003205 resource_list = &grab->pointer->focus_resource_list;
3206 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003207 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003208 wl_resource_for_each(resource, resource_list) {
3209 wl_pointer_send_button(resource, serial,
3210 time, button, state);
3211 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003212 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003213 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003214 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003215 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003216 }
3217
Daniel Stone4dbadb12012-05-30 16:31:51 +01003218 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003219 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003220}
3221
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003222static void
3223popup_grab_cancel(struct weston_pointer_grab *grab)
3224{
3225 popup_grab_end(grab->pointer);
3226}
3227
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003228static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003229 popup_grab_focus,
3230 popup_grab_motion,
3231 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003232 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003233};
3234
3235static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003236touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3237 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3238{
3239 struct wl_resource *resource;
3240 struct shell_seat *shseat =
3241 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3242 struct wl_display *display = shseat->seat->compositor->wl_display;
3243 uint32_t serial;
3244 struct wl_list *resource_list;
3245
3246 resource_list = &grab->touch->focus_resource_list;
3247 if (!wl_list_empty(resource_list)) {
3248 serial = wl_display_get_serial(display);
3249 wl_resource_for_each(resource, resource_list) {
3250 wl_touch_send_down(resource, serial, time,
3251 grab->touch->focus->surface->resource,
3252 touch_id, sx, sy);
3253 }
3254 }
3255}
3256
3257static void
3258touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3259{
3260 struct wl_resource *resource;
3261 struct shell_seat *shseat =
3262 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3263 struct wl_display *display = shseat->seat->compositor->wl_display;
3264 uint32_t serial;
3265 struct wl_list *resource_list;
3266
3267 resource_list = &grab->touch->focus_resource_list;
3268 if (!wl_list_empty(resource_list)) {
3269 serial = wl_display_get_serial(display);
3270 wl_resource_for_each(resource, resource_list) {
3271 wl_touch_send_up(resource, serial, time, touch_id);
3272 }
3273 }
3274}
3275
3276static void
3277touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3278 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3279{
3280 struct wl_resource *resource;
3281 struct wl_list *resource_list;
3282
3283 resource_list = &grab->touch->focus_resource_list;
3284 if (!wl_list_empty(resource_list)) {
3285 wl_resource_for_each(resource, resource_list) {
3286 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3287 }
3288 }
3289}
3290
3291static void
3292touch_popup_grab_frame(struct weston_touch_grab *grab)
3293{
3294}
3295
3296static void
3297touch_popup_grab_cancel(struct weston_touch_grab *grab)
3298{
3299 touch_popup_grab_end(grab->touch);
3300}
3301
3302static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3303 touch_popup_grab_down,
3304 touch_popup_grab_up,
3305 touch_popup_grab_motion,
3306 touch_popup_grab_frame,
3307 touch_popup_grab_cancel,
3308};
3309
3310static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003311shell_surface_send_popup_done(struct shell_surface *shsurf)
3312{
3313 if (shell_surface_is_wl_shell_surface(shsurf))
3314 wl_shell_surface_send_popup_done(shsurf->resource);
3315 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003316 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003317}
3318
3319static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003320popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003321{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003322 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003323 struct shell_seat *shseat =
3324 container_of(grab, struct shell_seat, popup_grab.grab);
3325 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003326 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003327
3328 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003329 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003330 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003331 shseat->popup_grab.grab.interface = NULL;
3332 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003333 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003334 wl_list_for_each_safe(shsurf, next,
3335 &shseat->popup_grab.surfaces_list,
3336 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003337 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003338 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003339 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003340 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003341 wl_list_init(&shseat->popup_grab.surfaces_list);
3342 }
3343}
3344
3345static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003346touch_popup_grab_end(struct weston_touch *touch)
3347{
3348 struct weston_touch_grab *grab = touch->grab;
3349 struct shell_seat *shseat =
3350 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3351 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003352 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003353
3354 if (touch->grab->interface == &touch_popup_grab_interface) {
3355 weston_touch_end_grab(grab->touch);
3356 shseat->popup_grab.client = NULL;
3357 shseat->popup_grab.touch_grab.interface = NULL;
3358 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3359 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003360 wl_list_for_each_safe(shsurf, next,
3361 &shseat->popup_grab.surfaces_list,
3362 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003363 shell_surface_send_popup_done(shsurf);
3364 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003365 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003366 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003367 wl_list_init(&shseat->popup_grab.surfaces_list);
3368 }
3369}
3370
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003371static struct shell_surface *
3372get_top_popup(struct shell_seat *shseat)
3373{
3374 struct shell_surface *shsurf;
3375
3376 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3377 return NULL;
3378 } else {
3379 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3380 struct shell_surface,
3381 popup.grab_link);
3382 return shsurf;
3383 }
3384}
3385
3386static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003387add_popup_grab(struct shell_surface *shsurf,
3388 struct shell_seat *shseat,
3389 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003390{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003391 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003392 struct shell_surface *parent, *top_surface;
3393
3394 parent = get_shell_surface(shsurf->parent);
3395 top_surface = get_top_popup(shseat);
3396 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003397 (!parent ||
3398 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003399 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003400 wl_resource_post_error(shsurf->owner_resource,
3401 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003402 "xdg_popup was not created on the "
3403 "topmost popup");
3404 return -1;
3405 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003406
3407 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003408 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003409 shseat->popup_grab.client =
3410 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003411
3412 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003413 shseat->popup_grab.grab.interface =
3414 &popup_grab_interface;
3415
3416 /* We must make sure here that this popup was opened
3417 * after a mouse press, and not just by moving around
3418 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003419 if (shseat->seat->pointer->button_count > 0)
3420 shseat->popup_grab.initial_up = 0;
3421 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003422 shseat->popup_grab.touch_grab.interface =
3423 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003424 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003425
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003426 wl_list_insert(&shseat->popup_grab.surfaces_list,
3427 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003428
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003429 if (type == POINTER) {
3430 weston_pointer_start_grab(seat->pointer,
3431 &shseat->popup_grab.grab);
3432 } else if (type == TOUCH) {
3433 weston_touch_start_grab(seat->touch,
3434 &shseat->popup_grab.touch_grab);
3435 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003436 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003437 wl_list_insert(&shseat->popup_grab.surfaces_list,
3438 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003439 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003440
3441 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003442}
3443
3444static void
3445remove_popup_grab(struct shell_surface *shsurf)
3446{
3447 struct shell_seat *shseat = shsurf->popup.shseat;
3448
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003449 if (shell_surface_is_xdg_popup(shsurf) &&
3450 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003451 wl_resource_post_error(shsurf->owner_resource,
3452 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003453 "xdg_popup was destroyed while it was "
3454 "not the topmost popup.");
3455 return;
3456 }
3457
Giulio Camuffo5085a752013-03-25 21:42:45 +01003458 wl_list_remove(&shsurf->popup.grab_link);
3459 wl_list_init(&shsurf->popup.grab_link);
3460 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003461 if (shseat->popup_grab.type == POINTER) {
3462 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3463 shseat->popup_grab.grab.interface = NULL;
3464 } else if (shseat->popup_grab.type == TOUCH) {
3465 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3466 shseat->popup_grab.touch_grab.interface = NULL;
3467 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003468 }
3469}
3470
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003471static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003472shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003473{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003474 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003475 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003476
Jason Ekstranda7af7042013-10-12 22:38:11 -05003477 shsurf->surface->output = parent_view->output;
3478 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003479
Jason Ekstranda7af7042013-10-12 22:38:11 -05003480 weston_view_set_transform_parent(shsurf->view, parent_view);
3481 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3482 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003483
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003484 if (shseat->seat->pointer &&
3485 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003486 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3487 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003488 } else if (shseat->seat->touch &&
3489 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003490 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3491 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003492 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003493 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003494 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003495 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003496
3497 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003498}
3499
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003500static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003501 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003502 shell_surface_move,
3503 shell_surface_resize,
3504 shell_surface_set_toplevel,
3505 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003506 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003507 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003508 shell_surface_set_maximized,
3509 shell_surface_set_title,
3510 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003511};
3512
3513static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003514destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003515{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003516 struct shell_surface *child, *next;
3517
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003518 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3519
Giulio Camuffo5085a752013-03-25 21:42:45 +01003520 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3521 remove_popup_grab(shsurf);
3522 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003523
Alex Wubd3354b2012-04-17 17:20:49 +08003524 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003525 shell_surface_is_top_fullscreen(shsurf))
3526 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003527
Jason Ekstranda7af7042013-10-12 22:38:11 -05003528 if (shsurf->fullscreen.black_view)
3529 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003530
Alex Wubd3354b2012-04-17 17:20:49 +08003531 /* As destroy_resource() use wl_list_for_each_safe(),
3532 * we can always remove the listener.
3533 */
3534 wl_list_remove(&shsurf->surface_destroy_listener.link);
3535 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003536 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003537 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003538
Jason Ekstranda7af7042013-10-12 22:38:11 -05003539 weston_view_destroy(shsurf->view);
3540
Philip Withnall648a4dd2013-11-25 18:01:44 +00003541 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003542 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3543 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003544
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003545 wl_list_remove(&shsurf->link);
3546 free(shsurf);
3547}
3548
3549static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003550shell_destroy_shell_surface(struct wl_resource *resource)
3551{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003552 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003553
Bryan Caina46b9462014-04-04 17:41:24 -05003554 if (!wl_list_empty(&shsurf->popup.grab_link))
3555 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003556 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003557 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003558}
3559
3560static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003561shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003562{
3563 struct shell_surface *shsurf = container_of(listener,
3564 struct shell_surface,
3565 surface_destroy_listener);
3566
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003567 if (shsurf->resource)
3568 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003569
3570 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003571}
3572
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003573static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003574fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003575{
3576 struct shell_surface *shsurf = data;
3577
3578 weston_surface_destroy(shsurf->surface);
3579}
3580
3581static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003582fade_out_done(struct weston_view_animation *animation, void *data)
3583{
3584 struct shell_surface *shsurf = data;
3585 struct wl_event_loop *loop;
3586
3587 loop = wl_display_get_event_loop(
3588 shsurf->surface->compositor->wl_display);
3589
3590 if (!shsurf->destroying) {
3591 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3592 shsurf->destroying = true;
3593 }
3594}
3595
3596static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003597handle_resource_destroy(struct wl_listener *listener, void *data)
3598{
3599 struct shell_surface *shsurf =
3600 container_of(listener, struct shell_surface,
3601 resource_destroy_listener);
3602
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003603 if (!weston_surface_is_mapped(shsurf->surface))
3604 return;
3605
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003606 shsurf->surface->ref_count++;
3607
3608 pixman_region32_fini(&shsurf->surface->pending.input);
3609 pixman_region32_init(&shsurf->surface->pending.input);
3610 pixman_region32_fini(&shsurf->surface->input);
3611 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003612 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3613 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3614 fade_out_done, shsurf);
3615 } else {
3616 weston_surface_destroy(shsurf->surface);
3617 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003618}
3619
3620static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003621shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003622
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003623struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003624get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003625{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003626 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003627 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003628 else
3629 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003630}
3631
Rafael Antognollie2a34552013-12-03 15:35:45 -02003632static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003633create_common_surface(struct shell_client *owner, void *shell,
3634 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003635 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003636{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003637 struct shell_surface *shsurf;
3638
Pekka Paalanen50b67472014-10-01 15:02:41 +03003639 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003640
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003641 shsurf = calloc(1, sizeof *shsurf);
3642 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003643 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003644 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003645 }
3646
Jason Ekstranda7af7042013-10-12 22:38:11 -05003647 shsurf->view = weston_view_create(surface);
3648 if (!shsurf->view) {
3649 weston_log("no memory to allocate shell surface\n");
3650 free(shsurf);
3651 return NULL;
3652 }
3653
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003654 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003655 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003656 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003657
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003658 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3659 wl_resource_add_destroy_listener(surface->resource,
3660 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003661 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003662
Tiago Vignattibc052c92012-04-19 16:18:18 +03003663 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003664 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003665 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003666 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003667 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003668 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003669 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3670 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003671 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003672 wl_list_init(&shsurf->fullscreen.transform.link);
3673
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003674 shsurf->output = get_default_output(shsurf->shell->compositor);
3675
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003676 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003677 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003678 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003679 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003680
3681 /* init link so its safe to always remove it in destroy_shell_surface */
3682 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003683 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003684
Pekka Paalanen460099f2012-01-20 16:48:25 +02003685 /* empty when not in use */
3686 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003687 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003688
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003689 wl_list_init(&shsurf->workspace_transform.link);
3690
Philip Withnall648a4dd2013-11-25 18:01:44 +00003691 wl_list_init(&shsurf->children_link);
3692 wl_list_init(&shsurf->children_list);
3693 shsurf->parent = NULL;
3694
Pekka Paalanenb5026542014-11-12 15:09:24 +02003695 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003696
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003697 shsurf->client = client;
3698
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003699 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003700}
3701
Rafael Antognollie2a34552013-12-03 15:35:45 -02003702static struct shell_surface *
3703create_shell_surface(void *shell, struct weston_surface *surface,
3704 const struct weston_shell_client *client)
3705{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003706 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003707}
3708
Jason Ekstranda7af7042013-10-12 22:38:11 -05003709static struct weston_view *
3710get_primary_view(void *shell, struct shell_surface *shsurf)
3711{
3712 return shsurf->view;
3713}
3714
Tiago Vignattibc052c92012-04-19 16:18:18 +03003715static void
3716shell_get_shell_surface(struct wl_client *client,
3717 struct wl_resource *resource,
3718 uint32_t id,
3719 struct wl_resource *surface_resource)
3720{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003721 struct weston_surface *surface =
3722 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003723 struct shell_client *sc = wl_resource_get_user_data(resource);
3724 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003725 struct shell_surface *shsurf;
3726
Pekka Paalanen50b67472014-10-01 15:02:41 +03003727 if (weston_surface_set_role(surface, "wl_shell_surface",
3728 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003729 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003730
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003731 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003732 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003733 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003734 return;
3735 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003736
Jason Ekstranda85118c2013-06-27 20:17:02 -05003737 shsurf->resource =
3738 wl_resource_create(client,
3739 &wl_shell_surface_interface, 1, id);
3740 wl_resource_set_implementation(shsurf->resource,
3741 &shell_surface_implementation,
3742 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003743 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003744}
3745
Rafael Antognollie2a34552013-12-03 15:35:45 -02003746static bool
3747shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3748{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003749 /* A shell surface without a resource is created from xwayland
3750 * and is considered a wl_shell surface for now. */
3751
3752 return shsurf->resource == NULL ||
3753 wl_resource_instance_of(shsurf->resource,
3754 &wl_shell_surface_interface,
3755 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003756}
3757
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003758static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003759 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003760};
3761
Rafael Antognollie2a34552013-12-03 15:35:45 -02003762/****************************
3763 * xdg-shell implementation */
3764
3765static void
3766xdg_surface_destroy(struct wl_client *client,
3767 struct wl_resource *resource)
3768{
3769 wl_resource_destroy(resource);
3770}
3771
3772static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003773xdg_surface_set_parent(struct wl_client *client,
3774 struct wl_resource *resource,
3775 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003776{
3777 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003778 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003779
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003780 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003781 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003782 shell_surface_set_parent(shsurf, parent->surface);
3783 } else {
3784 shell_surface_set_parent(shsurf, NULL);
3785 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003786}
3787
3788static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003789xdg_surface_set_app_id(struct wl_client *client,
3790 struct wl_resource *resource,
3791 const char *app_id)
3792{
3793 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3794
3795 free(shsurf->class);
3796 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003797 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003798}
3799
3800static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003801xdg_surface_show_window_menu(struct wl_client *client,
3802 struct wl_resource *surface_resource,
3803 struct wl_resource *seat_resource,
3804 uint32_t serial,
3805 int32_t x,
3806 int32_t y)
3807{
3808 /* TODO */
3809}
3810
3811static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003812xdg_surface_set_title(struct wl_client *client,
3813 struct wl_resource *resource, const char *title)
3814{
3815 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3816
3817 set_title(shsurf, title);
3818}
3819
3820static void
3821xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3822 struct wl_resource *seat_resource, uint32_t serial)
3823{
3824 common_surface_move(resource, seat_resource, serial);
3825}
3826
3827static void
3828xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3829 struct wl_resource *seat_resource, uint32_t serial,
3830 uint32_t edges)
3831{
3832 common_surface_resize(resource, seat_resource, serial, edges);
3833}
3834
3835static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003836xdg_surface_ack_configure(struct wl_client *client,
3837 struct wl_resource *resource,
3838 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003839{
3840 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3841
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003842 if (shsurf->state_requested) {
3843 shsurf->next_state = shsurf->requested_state;
3844 shsurf->state_changed = true;
3845 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003846 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003847}
3848
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003849static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003850xdg_surface_set_window_geometry(struct wl_client *client,
3851 struct wl_resource *resource,
3852 int32_t x,
3853 int32_t y,
3854 int32_t width,
3855 int32_t height)
3856{
3857 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3858
3859 set_window_geometry(shsurf, x, y, width, height);
3860}
3861
3862static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003863xdg_surface_set_maximized(struct wl_client *client,
3864 struct wl_resource *resource)
3865{
3866 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003867 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003868
3869 shsurf->state_requested = true;
3870 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003871
3872 if (!weston_surface_is_mapped(shsurf->surface))
3873 output = get_focused_output(shsurf->surface->compositor);
3874 else
3875 output = shsurf->surface->output;
3876
3877 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003878 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003879}
3880
3881static void
3882xdg_surface_unset_maximized(struct wl_client *client,
3883 struct wl_resource *resource)
3884{
3885 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3886
3887 shsurf->state_requested = true;
3888 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003889 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003890}
3891
3892static void
3893xdg_surface_set_fullscreen(struct wl_client *client,
3894 struct wl_resource *resource,
3895 struct wl_resource *output_resource)
3896{
3897 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3898 struct weston_output *output;
3899
3900 shsurf->state_requested = true;
3901 shsurf->requested_state.fullscreen = true;
3902
3903 if (output_resource)
3904 output = wl_resource_get_user_data(output_resource);
3905 else
3906 output = NULL;
3907
Marek Chalupa052917a2014-09-02 11:35:12 +02003908 /* handle clients launching in fullscreen */
3909 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3910 /* Set the output to the one that has focus currently. */
3911 assert(shsurf->surface);
3912 output = get_focused_output(shsurf->surface->compositor);
3913 }
3914
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003915 shell_surface_set_output(shsurf, output);
3916 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003917
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003918 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003919}
3920
3921static void
3922xdg_surface_unset_fullscreen(struct wl_client *client,
3923 struct wl_resource *resource)
3924{
3925 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3926
3927 shsurf->state_requested = true;
3928 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003929 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003930}
3931
3932static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003933xdg_surface_set_minimized(struct wl_client *client,
3934 struct wl_resource *resource)
3935{
3936 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3937
3938 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3939 return;
3940
3941 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003942 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003943}
3944
Rafael Antognollie2a34552013-12-03 15:35:45 -02003945static const struct xdg_surface_interface xdg_surface_implementation = {
3946 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003947 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003948 xdg_surface_set_title,
3949 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003950 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003951 xdg_surface_move,
3952 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003953 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003954 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003955 xdg_surface_set_maximized,
3956 xdg_surface_unset_maximized,
3957 xdg_surface_set_fullscreen,
3958 xdg_surface_unset_fullscreen,
3959 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003960};
3961
3962static void
3963xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003964 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003965{
3966 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003967 uint32_t *s;
3968 struct wl_array states;
3969 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003970
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003971 assert(shsurf);
3972
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003973 if (!shsurf->resource)
3974 return;
3975
3976 wl_array_init(&states);
3977 if (shsurf->requested_state.fullscreen) {
3978 s = wl_array_add(&states, sizeof *s);
3979 *s = XDG_SURFACE_STATE_FULLSCREEN;
3980 } else if (shsurf->requested_state.maximized) {
3981 s = wl_array_add(&states, sizeof *s);
3982 *s = XDG_SURFACE_STATE_MAXIMIZED;
3983 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003984 if (shsurf->resize_edges != 0) {
3985 s = wl_array_add(&states, sizeof *s);
3986 *s = XDG_SURFACE_STATE_RESIZING;
3987 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003988 if (shsurf->focus_count > 0) {
3989 s = wl_array_add(&states, sizeof *s);
3990 *s = XDG_SURFACE_STATE_ACTIVATED;
3991 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003992
3993 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3994 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3995
3996 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003997}
3998
3999static const struct weston_shell_client xdg_client = {
4000 xdg_send_configure
4001};
4002
4003static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004004xdg_shell_destroy(struct wl_client *client,
4005 struct wl_resource *resource)
4006{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004007 struct shell_client *sc = wl_resource_get_user_data(resource);
4008 struct wl_resource *shsurf_resource;
4009 struct shell_surface *shsurf;
4010
4011 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4012 shsurf = wl_resource_get_user_data(shsurf_resource);
4013 if (shsurf->owner_resource == resource) {
4014 wl_resource_post_error(
4015 resource,
4016 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4017 "not all child surface objects destroyed");
4018 return;
4019 }
4020 }
4021
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004022 wl_resource_destroy(resource);
4023}
4024
4025static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004026xdg_use_unstable_version(struct wl_client *client,
4027 struct wl_resource *resource,
4028 int32_t version)
4029{
4030 if (version > 1) {
4031 wl_resource_post_error(resource,
4032 1,
4033 "xdg-shell:: version not implemented yet.");
4034 return;
4035 }
4036}
4037
4038static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004039create_xdg_surface(struct shell_client *owner, void *shell,
4040 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004041 const struct weston_shell_client *client)
4042{
4043 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004044
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004045 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004046 if (!shsurf)
4047 return NULL;
4048
Pekka Paalanenb5026542014-11-12 15:09:24 +02004049 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004050
4051 return shsurf;
4052}
4053
4054static void
4055xdg_get_xdg_surface(struct wl_client *client,
4056 struct wl_resource *resource,
4057 uint32_t id,
4058 struct wl_resource *surface_resource)
4059{
4060 struct weston_surface *surface =
4061 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004062 struct shell_client *sc = wl_resource_get_user_data(resource);
4063 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004064 struct shell_surface *shsurf;
4065
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004066 shsurf = get_shell_surface(surface);
4067 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4068 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4069 "This wl_surface is already an "
4070 "xdg_surface");
4071 return;
4072 }
4073
Pekka Paalanen50b67472014-10-01 15:02:41 +03004074 if (weston_surface_set_role(surface, "xdg_surface",
4075 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004076 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004077
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004078 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004079 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004080 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004081 return;
4082 }
4083
4084 shsurf->resource =
4085 wl_resource_create(client,
4086 &xdg_surface_interface, 1, id);
4087 wl_resource_set_implementation(shsurf->resource,
4088 &xdg_surface_implementation,
4089 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004090 shsurf->owner_resource = resource;
4091 wl_list_insert(&sc->surface_list,
4092 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004093}
4094
4095static bool
4096shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4097{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004098 return shsurf->resource &&
4099 wl_resource_instance_of(shsurf->resource,
4100 &xdg_surface_interface,
4101 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004102}
4103
4104/* xdg-popup implementation */
4105
4106static void
4107xdg_popup_destroy(struct wl_client *client,
4108 struct wl_resource *resource)
4109{
4110 wl_resource_destroy(resource);
4111}
4112
Rafael Antognollie2a34552013-12-03 15:35:45 -02004113static const struct xdg_popup_interface xdg_popup_implementation = {
4114 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004115};
4116
4117static void
4118xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004119 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004120{
4121}
4122
4123static const struct weston_shell_client xdg_popup_client = {
4124 xdg_popup_send_configure
4125};
4126
4127static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004128create_xdg_popup(struct shell_client *owner, void *shell,
4129 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004130 const struct weston_shell_client *client,
4131 struct weston_surface *parent,
4132 struct shell_seat *seat,
4133 uint32_t serial,
4134 int32_t x, int32_t y)
4135{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004136 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004137
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004138 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004139 if (!shsurf)
4140 return NULL;
4141
Pekka Paalanenb5026542014-11-12 15:09:24 +02004142 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004143 shsurf->popup.shseat = seat;
4144 shsurf->popup.serial = serial;
4145 shsurf->popup.x = x;
4146 shsurf->popup.y = y;
4147 shell_surface_set_parent(shsurf, parent);
4148
4149 return shsurf;
4150}
4151
4152static void
4153xdg_get_xdg_popup(struct wl_client *client,
4154 struct wl_resource *resource,
4155 uint32_t id,
4156 struct wl_resource *surface_resource,
4157 struct wl_resource *parent_resource,
4158 struct wl_resource *seat_resource,
4159 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004160 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004161{
4162 struct weston_surface *surface =
4163 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004164 struct shell_client *sc = wl_resource_get_user_data(resource);
4165 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004166 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004167 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004168 struct weston_surface *parent;
4169 struct shell_seat *seat;
4170
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004171 shsurf = get_shell_surface(surface);
4172 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4173 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4174 "This wl_surface is already an "
4175 "xdg_popup");
4176 return;
4177 }
4178
Pekka Paalanen50b67472014-10-01 15:02:41 +03004179 if (weston_surface_set_role(surface, "xdg_popup",
4180 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004181 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004182
4183 if (!parent_resource) {
4184 wl_resource_post_error(surface_resource,
4185 WL_DISPLAY_ERROR_INVALID_OBJECT,
4186 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004187 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004188 }
4189
4190 parent = wl_resource_get_user_data(parent_resource);
4191 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4192
Jonas Ådahlee45a552015-03-18 16:37:47 +08004193 /* Verify that we are creating the top most popup when mapping,
4194 * as it's not until then we know whether it was mapped as most
4195 * top level or not. */
4196
4197 parent_shsurf = get_shell_surface(parent);
4198 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4199 !shell_surface_is_xdg_surface(parent_shsurf)) {
4200 wl_resource_post_error(resource,
4201 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4202 "xdg_popup parent was invalid");
4203 return;
4204 }
4205
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004206 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004207 parent, seat, serial, x, y);
4208 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004209 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004210 return;
4211 }
4212
4213 shsurf->resource =
4214 wl_resource_create(client,
4215 &xdg_popup_interface, 1, id);
4216 wl_resource_set_implementation(shsurf->resource,
4217 &xdg_popup_implementation,
4218 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004219 shsurf->owner_resource = resource;
4220 wl_list_insert(&sc->surface_list,
4221 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004222}
4223
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004224static void
4225xdg_pong(struct wl_client *client,
4226 struct wl_resource *resource, uint32_t serial)
4227{
4228 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004229
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004230 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004231}
4232
Rafael Antognollie2a34552013-12-03 15:35:45 -02004233static bool
4234shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4235{
4236 return wl_resource_instance_of(shsurf->resource,
4237 &xdg_popup_interface,
4238 &xdg_popup_implementation);
4239}
4240
4241static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004242 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004243 xdg_use_unstable_version,
4244 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004245 xdg_get_xdg_popup,
4246 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004247};
4248
4249static int
4250xdg_shell_unversioned_dispatch(const void *implementation,
4251 void *_target, uint32_t opcode,
4252 const struct wl_message *message,
4253 union wl_argument *args)
4254{
4255 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004256 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004257
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004258 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004259 wl_resource_post_error(resource,
4260 WL_DISPLAY_ERROR_INVALID_OBJECT,
4261 "must call use_unstable_version first");
4262 return 0;
4263 }
4264
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004265#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004266
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004267 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4268 "shell implementation doesn't match protocol version");
4269
Rafael Antognollie2a34552013-12-03 15:35:45 -02004270 if (args[0].i != XDG_SERVER_VERSION) {
4271 wl_resource_post_error(resource,
4272 WL_DISPLAY_ERROR_INVALID_OBJECT,
4273 "incompatible version, server is %d "
4274 "client wants %d",
4275 XDG_SERVER_VERSION, args[0].i);
4276 return 0;
4277 }
4278
4279 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004280 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004281
4282 return 1;
4283}
4284
4285/* end of xdg-shell implementation */
4286/***********************************/
4287
Kristian Høgsberg07937562011-04-12 17:25:42 -04004288static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004289shell_fade(struct desktop_shell *shell, enum fade_type type);
4290
Pekka Paalanen77346a62011-11-30 16:26:35 +02004291static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004292configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004293{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004294 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004295
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004296 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004297 if (v->output == ev->output && v != ev) {
4298 weston_view_unmap(v);
4299 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004300 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004301 }
4302 }
4303
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004304 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004305
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004306 if (wl_list_empty(&ev->layer_link.link)) {
4307 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004308 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004309 }
4310}
4311
Pekka Paalanen8274d902014-08-06 19:36:51 +03004312static int
4313background_get_label(struct weston_surface *surface, char *buf, size_t len)
4314{
4315 return snprintf(buf, len, "background for output %s",
4316 surface->output->name);
4317}
4318
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004319static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004320background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004321{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004322 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004323 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004324
Jason Ekstranda7af7042013-10-12 22:38:11 -05004325 view = container_of(es->views.next, struct weston_view, surface_link);
4326
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004327 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004328}
4329
4330static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004331desktop_shell_set_background(struct wl_client *client,
4332 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004333 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004334 struct wl_resource *surface_resource)
4335{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004336 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004337 struct weston_surface *surface =
4338 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004339 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004340
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004341 if (surface->configure) {
4342 wl_resource_post_error(surface_resource,
4343 WL_DISPLAY_ERROR_INVALID_OBJECT,
4344 "surface role already assigned");
4345 return;
4346 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004347
Jason Ekstranda7af7042013-10-12 22:38:11 -05004348 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4349 weston_view_destroy(view);
4350 view = weston_view_create(surface);
4351
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004352 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004353 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004354 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004355 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004356 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004357 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004358 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004359 surface->output->width,
4360 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004361}
4362
Pekka Paalanen8274d902014-08-06 19:36:51 +03004363static int
4364panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4365{
4366 return snprintf(buf, len, "panel for output %s",
4367 surface->output->name);
4368}
4369
Kristian Høgsberg75840622011-09-06 13:48:16 -04004370static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004371panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004372{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004373 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004374 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004375
Jason Ekstranda7af7042013-10-12 22:38:11 -05004376 view = container_of(es->views.next, struct weston_view, surface_link);
4377
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004378 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004379}
4380
4381static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004382desktop_shell_set_panel(struct wl_client *client,
4383 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004384 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004385 struct wl_resource *surface_resource)
4386{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004387 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004388 struct weston_surface *surface =
4389 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004390 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004391
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004392 if (surface->configure) {
4393 wl_resource_post_error(surface_resource,
4394 WL_DISPLAY_ERROR_INVALID_OBJECT,
4395 "surface role already assigned");
4396 return;
4397 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004398
Jason Ekstranda7af7042013-10-12 22:38:11 -05004399 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4400 weston_view_destroy(view);
4401 view = weston_view_create(surface);
4402
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004403 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004404 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004405 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004406 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004407 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004408 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004409 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004410 surface->output->width,
4411 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004412}
4413
Pekka Paalanen8274d902014-08-06 19:36:51 +03004414static int
4415lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4416{
4417 return snprintf(buf, len, "lock window");
4418}
4419
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004420static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004421lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004422{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004423 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004424 struct weston_view *view;
4425
4426 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004427
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004428 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004429 return;
4430
Jason Ekstranda7af7042013-10-12 22:38:11 -05004431 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004432
4433 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004434 weston_layer_entry_insert(&shell->lock_layer.view_list,
4435 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004436 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004437 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004438 }
4439}
4440
4441static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004442handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004443{
Tiago Vignattibe143262012-04-16 17:31:41 +03004444 struct desktop_shell *shell =
4445 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004446
Martin Minarik6d118362012-06-07 18:01:59 +02004447 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004448 shell->lock_surface = NULL;
4449}
4450
4451static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004452desktop_shell_set_lock_surface(struct wl_client *client,
4453 struct wl_resource *resource,
4454 struct wl_resource *surface_resource)
4455{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004456 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004457 struct weston_surface *surface =
4458 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004459
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004460 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004461
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004462 if (!shell->locked)
4463 return;
4464
Pekka Paalanen98262232011-12-01 10:42:22 +02004465 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004466
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004467 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004468 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004469 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004470
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004471 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004472 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004473 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004474 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004475}
4476
4477static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004478resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004479{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004480 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004481
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004482 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004483 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004484 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004485 &shell->input_panel_layer.link);
4486 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004487 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004488 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004489 wl_list_insert(&shell->compositor->cursor_layer.link,
4490 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004491 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004492 wl_list_insert(&shell->fullscreen_layer.link,
4493 &shell->panel_layer.link);
4494 wl_list_insert(&shell->panel_layer.link,
4495 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004496
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004497 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004498
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004499 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004500 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004501 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004502}
4503
4504static void
4505desktop_shell_unlock(struct wl_client *client,
4506 struct wl_resource *resource)
4507{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004508 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004509
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004510 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004511
4512 if (shell->locked)
4513 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004514}
4515
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004516static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004517desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004518 struct wl_resource *resource,
4519 struct wl_resource *surface_resource)
4520{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004521 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004522
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004523 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004524 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004525}
4526
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004527static void
4528desktop_shell_desktop_ready(struct wl_client *client,
4529 struct wl_resource *resource)
4530{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004531 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004532
4533 shell_fade_startup(shell);
4534}
4535
Jonny Lamb765760d2014-08-20 15:53:19 +02004536static void
4537desktop_shell_set_panel_position(struct wl_client *client,
4538 struct wl_resource *resource,
4539 uint32_t position)
4540{
4541 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4542
4543 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4544 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4545 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4546 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4547 wl_resource_post_error(resource,
4548 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4549 "bad position argument");
4550 return;
4551 }
4552
4553 shell->panel_position = position;
4554}
4555
Kristian Høgsberg75840622011-09-06 13:48:16 -04004556static const struct desktop_shell_interface desktop_shell_implementation = {
4557 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004558 desktop_shell_set_panel,
4559 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004560 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004561 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004562 desktop_shell_desktop_ready,
4563 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004564};
4565
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004566static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004567get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004568{
4569 struct shell_surface *shsurf;
4570
4571 shsurf = get_shell_surface(surface);
4572 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004573 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004574 return shsurf->type;
4575}
4576
Kristian Høgsberg75840622011-09-06 13:48:16 -04004577static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004578move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004579{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004580 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004581 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004582 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004583
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004584 if (seat->pointer->focus == NULL)
4585 return;
4586
4587 focus = seat->pointer->focus->surface;
4588
Pekka Paalanen01388e22013-04-25 13:57:44 +03004589 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004590 if (surface == NULL)
4591 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004592
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004593 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004594 if (shsurf == NULL || shsurf->state.fullscreen ||
4595 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004596 return;
4597
Derek Foreman8aeeac82015-01-30 13:24:36 -06004598 surface_move(shsurf, seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004599}
4600
4601static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004602maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4603{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004604 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004605 struct weston_surface *surface;
4606 struct shell_surface *shsurf;
4607
4608 surface = weston_surface_get_main_surface(focus);
4609 if (surface == NULL)
4610 return;
4611
4612 shsurf = get_shell_surface(surface);
4613 if (shsurf == NULL)
4614 return;
4615
4616 if (!shell_surface_is_xdg_surface(shsurf))
4617 return;
4618
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004619 shsurf->state_requested = true;
4620 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004621 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004622}
4623
4624static void
4625fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4626{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004627 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004628 struct weston_surface *surface;
4629 struct shell_surface *shsurf;
4630
4631 surface = weston_surface_get_main_surface(focus);
4632 if (surface == NULL)
4633 return;
4634
4635 shsurf = get_shell_surface(surface);
4636 if (shsurf == NULL)
4637 return;
4638
4639 if (!shell_surface_is_xdg_surface(shsurf))
4640 return;
4641
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004642 shsurf->state_requested = true;
4643 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004644 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004645 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004646}
4647
4648static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004649touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4650{
Derek Foreman362656b2014-09-04 10:23:05 -05004651 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004652 struct weston_surface *surface;
4653 struct shell_surface *shsurf;
4654
Derek Foreman362656b2014-09-04 10:23:05 -05004655 if (seat->touch->focus == NULL)
4656 return;
4657
4658 focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004659 surface = weston_surface_get_main_surface(focus);
4660 if (surface == NULL)
4661 return;
4662
4663 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004664 if (shsurf == NULL || shsurf->state.fullscreen ||
4665 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004666 return;
4667
Derek Foreman8aeeac82015-01-30 13:24:36 -06004668 surface_touch_move(shsurf, seat);
Neil Robertsaba0f252013-10-03 16:43:05 +01004669}
4670
4671static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004672resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004673{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004674 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004675 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004676 uint32_t edges = 0;
4677 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004678 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004679
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004680 if (seat->pointer->focus == NULL)
4681 return;
4682
4683 focus = seat->pointer->focus->surface;
4684
Pekka Paalanen01388e22013-04-25 13:57:44 +03004685 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004686 if (surface == NULL)
4687 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004688
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004689 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004690 if (shsurf == NULL || shsurf->state.fullscreen ||
4691 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004692 return;
4693
Jason Ekstranda7af7042013-10-12 22:38:11 -05004694 weston_view_from_global(shsurf->view,
4695 wl_fixed_to_int(seat->pointer->grab_x),
4696 wl_fixed_to_int(seat->pointer->grab_y),
4697 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004698
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004699 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004700 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004701 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004702 edges |= 0;
4703 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004704 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004705
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004706 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004707 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004708 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004709 edges |= 0;
4710 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004711 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004712
Derek Foreman8aeeac82015-01-30 13:24:36 -06004713 surface_resize(shsurf, seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004714}
4715
4716static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004717surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004718 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004719{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004720 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004721 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004722 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004723 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004724
Pekka Paalanen01388e22013-04-25 13:57:44 +03004725 /* XXX: broken for windows containing sub-surfaces */
4726 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004727 if (surface == NULL)
4728 return;
4729
4730 shsurf = get_shell_surface(surface);
4731 if (!shsurf)
4732 return;
4733
Jason Ekstranda7af7042013-10-12 22:38:11 -05004734 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004735
Jason Ekstranda7af7042013-10-12 22:38:11 -05004736 if (shsurf->view->alpha > 1.0)
4737 shsurf->view->alpha = 1.0;
4738 if (shsurf->view->alpha < step)
4739 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004740
Jason Ekstranda7af7042013-10-12 22:38:11 -05004741 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004742 weston_surface_damage(surface);
4743}
4744
4745static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004746do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004747 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004748{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004749 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004750 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004751 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004752
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004753 if (!seat->pointer) {
4754 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4755 return;
4756 }
4757
Scott Moreauccbf29d2012-02-22 14:21:41 -07004758 wl_list_for_each(output, &compositor->output_list, link) {
4759 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004760 wl_fixed_to_double(seat->pointer->x),
4761 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004762 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004763 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004764 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004765 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004766 increment = -output->zoom.increment;
4767 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004768 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004769 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004770 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004771 else
4772 increment = 0;
4773
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004774 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004775
Scott Moreaue6603982012-06-11 13:07:51 -06004776 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004777 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004778 else if (output->zoom.level > output->zoom.max_level)
4779 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004780 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004781 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004782 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004783
Scott Moreaue6603982012-06-11 13:07:51 -06004784 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004785
Jason Ekstranda7af7042013-10-12 22:38:11 -05004786 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004787 }
4788 }
4789}
4790
Scott Moreauccbf29d2012-02-22 14:21:41 -07004791static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004792zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004793 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004794{
4795 do_zoom(seat, time, 0, axis, value);
4796}
4797
4798static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004799zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004800 void *data)
4801{
4802 do_zoom(seat, time, key, 0, 0);
4803}
4804
4805static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004806terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004807 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004808{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004809 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004810
Daniel Stone325fc2d2012-05-30 16:31:58 +01004811 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004812}
4813
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004814static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004815rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4816 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004817{
4818 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004819 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004820 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004821 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004822 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004823
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004824 weston_pointer_move(pointer, x, y);
4825
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004826 if (!shsurf)
4827 return;
4828
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004829 cx = 0.5f * shsurf->surface->width;
4830 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004831
Daniel Stone37816df2012-05-16 18:45:18 +01004832 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4833 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004834 r = sqrtf(dx * dx + dy * dy);
4835
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004836 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004837 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004838
4839 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004840 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004841 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004842
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004843 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004844 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004845
4846 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004847 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004848 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004849 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004850 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004851
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004852 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004853 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004854 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004855 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004856 wl_list_init(&shsurf->rotation.transform.link);
4857 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004858 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004859 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004860
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004861 /* We need to adjust the position of the surface
4862 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004863 cposx = shsurf->view->geometry.x + cx;
4864 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004865 dposx = rotate->center.x - cposx;
4866 dposy = rotate->center.y - cposy;
4867 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004868 weston_view_set_position(shsurf->view,
4869 shsurf->view->geometry.x + dposx,
4870 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004871 }
4872
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004873 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004874 * lazily applies the damage due to rotation update.
4875 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004876 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004877}
4878
4879static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004880rotate_grab_button(struct weston_pointer_grab *grab,
4881 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004882{
4883 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004884 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004885 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004886 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004887 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004888
Daniel Stone4dbadb12012-05-30 16:31:51 +01004889 if (pointer->button_count == 0 &&
4890 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004891 if (shsurf)
4892 weston_matrix_multiply(&shsurf->rotation.rotation,
4893 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004894 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004895 free(rotate);
4896 }
4897}
4898
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004899static void
4900rotate_grab_cancel(struct weston_pointer_grab *grab)
4901{
4902 struct rotate_grab *rotate =
4903 container_of(grab, struct rotate_grab, base.grab);
4904
4905 shell_grab_end(&rotate->base);
4906 free(rotate);
4907}
4908
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004909static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004910 noop_grab_focus,
4911 rotate_grab_motion,
4912 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004913 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004914};
4915
4916static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004917surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004918{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004919 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004920 float dx, dy;
4921 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004922
Pekka Paalanen460099f2012-01-20 16:48:25 +02004923 rotate = malloc(sizeof *rotate);
4924 if (!rotate)
4925 return;
4926
Jason Ekstranda7af7042013-10-12 22:38:11 -05004927 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004928 surface->surface->width * 0.5f,
4929 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004930 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004931
Daniel Stone37816df2012-05-16 18:45:18 +01004932 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4933 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004934 r = sqrtf(dx * dx + dy * dy);
4935 if (r > 20.0f) {
4936 struct weston_matrix inverse;
4937
4938 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004939 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004940 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004941
4942 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004943 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004944 } else {
4945 weston_matrix_init(&surface->rotation.rotation);
4946 weston_matrix_init(&rotate->rotation);
4947 }
4948
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004949 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4950 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004951}
4952
4953static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004954rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004955 void *data)
4956{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004957 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004958 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004959 struct shell_surface *surface;
4960
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004961 if (seat->pointer->focus == NULL)
4962 return;
4963
4964 focus = seat->pointer->focus->surface;
4965
Pekka Paalanen01388e22013-04-25 13:57:44 +03004966 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004967 if (base_surface == NULL)
4968 return;
4969
4970 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004971 if (surface == NULL || surface->state.fullscreen ||
4972 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004973 return;
4974
4975 surface_rotate(surface, seat);
4976}
4977
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004978/* Move all fullscreen layers down to the current workspace and hide their
4979 * black views. The surfaces' state is set to both fullscreen and lowered,
4980 * and this is reversed when such a surface is re-configured, see
4981 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4982 *
4983 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004984 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004985void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004986lower_fullscreen_layer(struct desktop_shell *shell)
4987{
4988 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004989 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004990
4991 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004992 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004993 &shell->fullscreen_layer.view_list.link,
4994 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004995 struct shell_surface *shsurf = get_shell_surface(view->surface);
4996
4997 if (!shsurf)
4998 continue;
4999
5000 /* We can have a non-fullscreen popup for a fullscreen surface
5001 * in the fullscreen layer. */
5002 if (shsurf->state.fullscreen) {
5003 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005004 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5005 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005006 weston_view_damage_below(shsurf->fullscreen.black_view);
5007
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005008 }
5009
5010 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005011 weston_layer_entry_remove(&view->layer_link);
5012 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005013 weston_view_damage_below(view);
5014 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005015
5016 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005017 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005018}
5019
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005020void
Tiago Vignattibe143262012-04-16 17:31:41 +03005021activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005022 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005023{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005024 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005025 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005026 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005027 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005028 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005029
Kristian Høgsberg6110d072014-04-29 15:15:45 -07005030 lower_fullscreen_layer(shell);
5031
Pekka Paalanen01388e22013-04-25 13:57:44 +03005032 main_surface = weston_surface_get_main_surface(es);
5033
Daniel Stone37816df2012-05-16 18:45:18 +01005034 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005035
Jonas Ådahl8538b222012-08-29 22:13:03 +02005036 state = ensure_focus_state(shell, seat);
5037 if (state == NULL)
5038 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005039
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005040 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005041 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005042
Rafael Antognolli03b16592013-12-03 15:35:42 -02005043 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005044 assert(shsurf);
5045
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005046 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005047 shell_configure_fullscreen(shsurf);
5048 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005049 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005050
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005051 /* Update the surface’s layer. This brings it to the top of the stacking
5052 * order as appropriate. */
5053 shell_surface_update_layer(shsurf);
5054
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005055 if (shell->focus_animation_type != ANIMATION_NONE) {
5056 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005057 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005058 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005059}
5060
Alex Wu21858432012-04-01 20:13:08 +08005061/* no-op func for checking black surface */
5062static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005063black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005064{
5065}
5066
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005067static bool
Alex Wu21858432012-04-01 20:13:08 +08005068is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5069{
5070 if (es->configure == black_surface_configure) {
5071 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005072 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005073 return true;
5074 }
5075 return false;
5076}
5077
Kristian Høgsberg75840622011-09-06 13:48:16 -04005078static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005079activate_binding(struct weston_seat *seat,
5080 struct desktop_shell *shell,
5081 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005082{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005083 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005084
Alex Wu9c35e6b2012-03-05 14:13:13 +08005085 if (!focus)
5086 return;
5087
Pekka Paalanen01388e22013-04-25 13:57:44 +03005088 if (is_black_surface(focus, &main_surface))
5089 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005090
Pekka Paalanen01388e22013-04-25 13:57:44 +03005091 main_surface = weston_surface_get_main_surface(focus);
5092 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005093 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005094
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005095 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005096}
5097
5098static void
5099click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
5100 void *data)
5101{
5102 if (seat->pointer->grab != &seat->pointer->default_grab)
5103 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005104 if (seat->pointer->focus == NULL)
5105 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005106
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005107 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005108}
5109
5110static void
5111touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
5112{
5113 if (seat->touch->grab != &seat->touch->default_grab)
5114 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005115 if (seat->touch->focus == NULL)
5116 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005117
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005118 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005119}
5120
5121static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005122unfocus_all_seats(struct desktop_shell *shell)
5123{
5124 struct weston_seat *seat, *next;
5125
5126 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5127 if (seat->keyboard == NULL)
5128 continue;
5129
5130 weston_keyboard_set_focus(seat->keyboard, NULL);
5131 }
5132}
5133
5134static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005135lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005136{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005137 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005138
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005139 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005140 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005141 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005142 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005143
5144 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005145
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005146 /* Hide all surfaces by removing the fullscreen, panel and
5147 * toplevel layers. This way nothing else can show or receive
5148 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005149
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005150 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005151 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005152 if (shell->showing_input_panels)
5153 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005154 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005155 wl_list_insert(&shell->compositor->cursor_layer.link,
5156 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005157
Neil Robertsb4a91702014-04-09 16:33:32 +01005158 /* Remove the keyboard focus on all seats. This will be
5159 * restored to the workspace's saved state via
5160 * restore_focus_state when the compositor is unlocked */
5161 unfocus_all_seats(shell);
5162
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005163 /* TODO: disable bindings that should not work while locked. */
5164
5165 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005166}
5167
5168static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005169unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005170{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005171 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005172 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005173 return;
5174 }
5175
5176 /* If desktop-shell client has gone away, unlock immediately. */
5177 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005178 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005179 return;
5180 }
5181
5182 if (shell->prepare_event_sent)
5183 return;
5184
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005185 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005186 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005187}
5188
5189static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005190shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005191{
5192 struct desktop_shell *shell = data;
5193
5194 shell->fade.animation = NULL;
5195
5196 switch (shell->fade.type) {
5197 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005198 weston_surface_destroy(shell->fade.view->surface);
5199 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005200 break;
5201 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005202 lock(shell);
5203 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005204 default:
5205 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005206 }
5207}
5208
Jason Ekstranda7af7042013-10-12 22:38:11 -05005209static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005210shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005211{
5212 struct weston_compositor *compositor = shell->compositor;
5213 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005214 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005215
5216 surface = weston_surface_create(compositor);
5217 if (!surface)
5218 return NULL;
5219
Jason Ekstranda7af7042013-10-12 22:38:11 -05005220 view = weston_view_create(surface);
5221 if (!view) {
5222 weston_surface_destroy(surface);
5223 return NULL;
5224 }
5225
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005226 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005227 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005228 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005229 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5230 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005231 pixman_region32_init(&surface->input);
5232
Jason Ekstranda7af7042013-10-12 22:38:11 -05005233 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005234}
5235
5236static void
5237shell_fade(struct desktop_shell *shell, enum fade_type type)
5238{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005239 float tint;
5240
5241 switch (type) {
5242 case FADE_IN:
5243 tint = 0.0;
5244 break;
5245 case FADE_OUT:
5246 tint = 1.0;
5247 break;
5248 default:
5249 weston_log("shell: invalid fade type\n");
5250 return;
5251 }
5252
5253 shell->fade.type = type;
5254
Jason Ekstranda7af7042013-10-12 22:38:11 -05005255 if (shell->fade.view == NULL) {
5256 shell->fade.view = shell_fade_create_surface(shell);
5257 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005258 return;
5259
Jason Ekstranda7af7042013-10-12 22:38:11 -05005260 shell->fade.view->alpha = 1.0 - tint;
5261 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005262 }
5263
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005264 if (shell->fade.view->output == NULL) {
5265 /* If the black view gets a NULL output, we lost the
5266 * last output and we'll just cancel the fade. This
5267 * happens when you close the last window under the
5268 * X11 or Wayland backends. */
5269 shell->locked = false;
5270 weston_surface_destroy(shell->fade.view->surface);
5271 shell->fade.view = NULL;
5272 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005273 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005274 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005275 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005276 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005277 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005278 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005279 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005280}
5281
5282static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005283do_shell_fade_startup(void *data)
5284{
5285 struct desktop_shell *shell = data;
5286
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005287 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005288 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005289 } else {
5290 weston_log("desktop shell: "
5291 "unexpected fade-in animation type %d\n",
5292 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005293 weston_surface_destroy(shell->fade.view->surface);
5294 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005295 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005296}
5297
5298static void
5299shell_fade_startup(struct desktop_shell *shell)
5300{
5301 struct wl_event_loop *loop;
5302
5303 if (!shell->fade.startup_timer)
5304 return;
5305
5306 wl_event_source_remove(shell->fade.startup_timer);
5307 shell->fade.startup_timer = NULL;
5308
5309 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5310 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5311}
5312
5313static int
5314fade_startup_timeout(void *data)
5315{
5316 struct desktop_shell *shell = data;
5317
5318 shell_fade_startup(shell);
5319 return 0;
5320}
5321
5322static void
5323shell_fade_init(struct desktop_shell *shell)
5324{
5325 /* Make compositor output all black, and wait for the desktop-shell
5326 * client to signal it is ready, then fade in. The timer triggers a
5327 * fade-in, in case the desktop-shell client takes too long.
5328 */
5329
5330 struct wl_event_loop *loop;
5331
Jason Ekstranda7af7042013-10-12 22:38:11 -05005332 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005333 weston_log("%s: warning: fade surface already exists\n",
5334 __func__);
5335 return;
5336 }
5337
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005338 if (shell->startup_animation_type == ANIMATION_NONE)
5339 return;
5340
Jason Ekstranda7af7042013-10-12 22:38:11 -05005341 shell->fade.view = shell_fade_create_surface(shell);
5342 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005343 return;
5344
Jason Ekstranda7af7042013-10-12 22:38:11 -05005345 weston_view_update_transform(shell->fade.view);
5346 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005347
5348 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5349 shell->fade.startup_timer =
5350 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5351 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5352}
5353
5354static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005355idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005356{
5357 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005358 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005359 struct weston_seat *seat;
5360
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005361 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005362 if (seat->pointer)
5363 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005364 if (seat->touch)
5365 touch_popup_grab_end(seat->touch);
5366 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005367
5368 shell_fade(shell, FADE_OUT);
5369 /* lock() is called from shell_fade_done() */
5370}
5371
5372static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005373wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005374{
5375 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005376 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005377
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005378 unlock(shell);
5379}
5380
5381static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005382center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005383{
Giulio Camuffob8366642013-04-25 13:57:46 +03005384 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005385 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005386
Jason Ekstranda7af7042013-10-12 22:38:11 -05005387 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005388
5389 x = output->x + (output->width - width) / 2 - surf_x / 2;
5390 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005391
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005392 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005393}
5394
5395static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005396weston_view_set_initial_position(struct weston_view *view,
5397 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005398{
5399 struct weston_compositor *compositor = shell->compositor;
5400 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005401 int32_t range_x, range_y;
5402 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005403 struct weston_output *output, *target_output = NULL;
5404 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005405 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005406
5407 /* As a heuristic place the new window on the same output as the
5408 * pointer. Falling back to the output containing 0, 0.
5409 *
5410 * TODO: Do something clever for touch too?
5411 */
5412 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005413 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005414 ix = wl_fixed_to_int(seat->pointer->x);
5415 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005416 break;
5417 }
5418 }
5419
5420 wl_list_for_each(output, &compositor->output_list, link) {
5421 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5422 target_output = output;
5423 break;
5424 }
5425 }
5426
5427 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005428 weston_view_set_position(view, 10 + random() % 400,
5429 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005430 return;
5431 }
5432
5433 /* Valid range within output where the surface will still be onscreen.
5434 * If this is negative it means that the surface is bigger than
5435 * output.
5436 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005437 get_output_work_area(shell, target_output, &area);
5438
5439 dx = area.x;
5440 dy = area.y;
5441 range_x = area.width - view->surface->width;
5442 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005443
Rob Bradford4cb88c72012-08-13 15:18:44 +01005444 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005445 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005446
5447 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005448 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005449
5450 x = target_output->x + dx;
5451 y = target_output->y + dy;
5452
Jason Ekstranda7af7042013-10-12 22:38:11 -05005453 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005454}
5455
5456static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005457set_maximized_position(struct desktop_shell *shell,
5458 struct shell_surface *shsurf)
5459{
5460 int32_t surf_x, surf_y;
5461 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005462 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005463
Jonny Lambd73c6942014-08-20 15:53:20 +02005464 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005465 if (shsurf->has_set_geometry) {
5466 surf_x = shsurf->geometry.x;
5467 surf_y = shsurf->geometry.y;
5468 } else {
5469 surface_subsurfaces_boundingbox(shsurf->surface,
5470 &surf_x, &surf_y, NULL, NULL);
5471 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005472 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005473
5474 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005475 e->x1 + area.x - surf_x,
5476 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005477}
5478
5479static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005480map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005481 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005482{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005483 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005484 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005485
Pekka Paalanen77346a62011-11-30 16:26:35 +02005486 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005487 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005488 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005489 if (shsurf->state.fullscreen) {
5490 center_on_output(shsurf->view, shsurf->fullscreen_output);
5491 shell_map_fullscreen(shsurf);
5492 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005493 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005494 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005495 weston_view_set_initial_position(shsurf->view, shell);
5496 }
Juan Zhao96879df2012-02-07 08:45:41 +08005497 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005498 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005499 if (shell_map_popup(shsurf) != 0)
5500 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005501 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005502 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005503 weston_view_set_position(shsurf->view,
5504 shsurf->view->geometry.x + sx,
5505 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005506 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005507 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005508 default:
5509 ;
5510 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005511
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005512 /* Surface stacking order, see also activate(). */
5513 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005514
Jason Ekstranda7af7042013-10-12 22:38:11 -05005515 if (shsurf->type != SHELL_SURFACE_NONE) {
5516 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005517 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005518 shsurf->surface->output = shsurf->output;
5519 shsurf->view->output = shsurf->output;
5520 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005521 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005522
Jason Ekstranda7af7042013-10-12 22:38:11 -05005523 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005524 /* XXX: xwayland's using the same fields for transient type */
5525 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005526 if (shsurf->transient.flags ==
5527 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5528 break;
5529 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005530 if (shsurf->state.relative &&
5531 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5532 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005533 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005534 break;
5535 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005536 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005537 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005538 case SHELL_SURFACE_POPUP:
5539 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005540 default:
5541 break;
5542 }
5543
Rafael Antognolli03b16592013-12-03 15:35:42 -02005544 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5545 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005546 {
5547 switch (shell->win_animation_type) {
5548 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005549 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005550 break;
5551 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005552 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005553 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005554 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005555 default:
5556 break;
5557 }
5558 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005559}
5560
5561static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005562configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005563 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005564{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005565 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005566 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005567
Pekka Paalanen77346a62011-11-30 16:26:35 +02005568 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005569
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005570 assert(shsurf);
5571
Rafael Antognolli03b16592013-12-03 15:35:42 -02005572 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005573 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005574 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005575 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005576 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005577 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005578 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005579
Alex Wu4539b082012-03-01 12:57:46 +08005580 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005581 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005582 wl_list_for_each(view, &surface->views, surface_link)
5583 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005584
Rafael Antognolli03b16592013-12-03 15:35:42 -02005585 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005586 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005587 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005588}
5589
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005590static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005591shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005592{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005593 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005594 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005595 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005596
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005597 assert(shsurf);
5598
5599 shell = shsurf->shell;
5600
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005601 if (!weston_surface_is_mapped(es) &&
5602 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005603 remove_popup_grab(shsurf);
5604 }
5605
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005606 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005607 return;
5608
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005609 if (shsurf->has_next_geometry) {
5610 shsurf->geometry = shsurf->next_geometry;
5611 shsurf->has_next_geometry = false;
5612 shsurf->has_set_geometry = true;
5613 } else if (!shsurf->has_set_geometry) {
5614 surface_subsurfaces_boundingbox(shsurf->surface,
5615 &shsurf->geometry.x,
5616 &shsurf->geometry.y,
5617 &shsurf->geometry.width,
5618 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005619 }
5620
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005621 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005622 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005623 type_changed = 1;
5624 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005625
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005626 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005627 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005628 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005629 shsurf->last_width != es->width ||
5630 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005631 float from_x, from_y;
5632 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005633
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005634 if (shsurf->resize_edges) {
5635 sx = 0;
5636 sy = 0;
5637 }
5638
5639 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5640 sx = shsurf->last_width - es->width;
5641 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5642 sy = shsurf->last_height - es->height;
5643
5644 shsurf->last_width = es->width;
5645 shsurf->last_height = es->height;
5646
Jason Ekstranda7af7042013-10-12 22:38:11 -05005647 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5648 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005649 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005650 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005651 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005652 }
5653}
5654
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005655static bool
5656check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5657{
5658 struct timespec now;
5659
5660 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5661 return false;
5662
5663 /*
5664 * If the shell helper client dies before the session has been
5665 * up for roughly 30 seconds, better just make Weston shut down,
5666 * because the user likely has no way to interact with the desktop
5667 * anyway.
5668 */
5669 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5670 weston_log("Error: %s apparently cannot run at all.\n",
5671 shell->client);
5672 weston_log_continue(STAMP_SPACE "Quitting...");
5673 wl_display_terminate(shell->compositor->wl_display);
5674
5675 return true;
5676 }
5677
5678 return false;
5679}
5680
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005681static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005682
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005683static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005684respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005685{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005686 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005687
5688 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5689 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005690 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005691 shell->child.deathstamp = time;
5692 shell->child.deathcount = 0;
5693 }
5694
5695 shell->child.deathcount++;
5696 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005697 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005698 return;
5699 }
5700
Pekka Paalanen826dc142014-08-27 12:11:53 +03005701 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005702 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005703}
5704
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005705static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005706desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5707{
5708 struct desktop_shell *shell;
5709
5710 shell = container_of(listener, struct desktop_shell,
5711 child.client_destroy_listener);
5712
Pekka Paalanen826dc142014-08-27 12:11:53 +03005713 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005714 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005715 /*
5716 * unbind_desktop_shell() will reset shell->child.desktop_shell
5717 * before the respawned process has a chance to create a new
5718 * desktop_shell object, because we are being called from the
5719 * wl_client destructor which destroys all wl_resources before
5720 * returning.
5721 */
5722
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005723 if (!check_desktop_shell_crash_too_early(shell))
5724 respawn_desktop_shell_process(shell);
5725
Pekka Paalanen826dc142014-08-27 12:11:53 +03005726 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005727}
5728
5729static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005730launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005731{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005732 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005733
Pekka Paalanen826dc142014-08-27 12:11:53 +03005734 shell->child.client = weston_client_start(shell->compositor,
5735 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005736
Arnaud Vrac42631192014-08-25 20:56:46 +02005737 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005738 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005739 return;
5740 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005741
5742 shell->child.client_destroy_listener.notify =
5743 desktop_shell_client_destroy;
5744 wl_client_add_destroy_listener(shell->child.client,
5745 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005746}
5747
5748static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005749handle_shell_client_destroy(struct wl_listener *listener, void *data)
5750{
5751 struct shell_client *sc =
5752 container_of(listener, struct shell_client, destroy_listener);
5753
5754 if (sc->ping_timer)
5755 wl_event_source_remove(sc->ping_timer);
5756 free(sc);
5757}
5758
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005759static struct shell_client *
5760shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5761 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005762{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005763 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005764
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005765 sc = zalloc(sizeof *sc);
5766 if (sc == NULL) {
5767 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005768 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005769 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005770
5771 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005772 if (sc->resource == NULL) {
5773 free(sc);
5774 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005775 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005776 }
5777
5778 sc->client = client;
5779 sc->shell = shell;
5780 sc->destroy_listener.notify = handle_shell_client_destroy;
5781 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005782 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005783
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005784 return sc;
5785}
5786
5787static void
5788bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5789{
5790 struct desktop_shell *shell = data;
5791 struct shell_client *sc;
5792
5793 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5794 if (sc)
5795 wl_resource_set_implementation(sc->resource,
5796 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005797 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005798}
5799
5800static void
5801bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5802{
5803 struct desktop_shell *shell = data;
5804 struct shell_client *sc;
5805
5806 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5807 if (sc)
5808 wl_resource_set_dispatcher(sc->resource,
5809 xdg_shell_unversioned_dispatch,
5810 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005811}
5812
5813static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005814unbind_desktop_shell(struct wl_resource *resource)
5815{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005816 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005817
5818 if (shell->locked)
5819 resume_desktop(shell);
5820
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005821 shell->child.desktop_shell = NULL;
5822 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005823}
5824
5825static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005826bind_desktop_shell(struct wl_client *client,
5827 void *data, uint32_t version, uint32_t id)
5828{
Tiago Vignattibe143262012-04-16 17:31:41 +03005829 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005830 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005831
Jason Ekstranda85118c2013-06-27 20:17:02 -05005832 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005833 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005834
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005835 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005836 wl_resource_set_implementation(resource,
5837 &desktop_shell_implementation,
5838 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005839 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005840
5841 if (version < 2)
5842 shell_fade_startup(shell);
5843
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005844 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005845 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005846
5847 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5848 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005849}
5850
Kristian Høgsberg07045392012-02-19 18:52:44 -05005851struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005852 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005853 struct weston_surface *current;
5854 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005855 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005856 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005857};
5858
5859static void
5860switcher_next(struct switcher *switcher)
5861{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005862 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005863 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005864 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005865 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005866
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005867 /* temporary re-display minimized surfaces */
5868 struct weston_view *tmp;
5869 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005870 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5871 weston_layer_entry_remove(&view->layer_link);
5872 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005873 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5874 *minimized = view;
5875 }
5876
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005877 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005878 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005879 if (shsurf &&
5880 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5881 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005882 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005883 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005884 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005885 next = view->surface;
5886 prev = view->surface;
5887 view->alpha = 0.25;
5888 weston_view_geometry_dirty(view);
5889 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005890 }
Alex Wu1659daa2012-04-01 20:13:09 +08005891
Jason Ekstranda7af7042013-10-12 22:38:11 -05005892 if (is_black_surface(view->surface, NULL)) {
5893 view->alpha = 0.25;
5894 weston_view_geometry_dirty(view);
5895 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005896 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005897 }
5898
5899 if (next == NULL)
5900 next = first;
5901
Alex Wu07b26062012-03-12 16:06:01 +08005902 if (next == NULL)
5903 return;
5904
Kristian Høgsberg07045392012-02-19 18:52:44 -05005905 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005906 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005907
5908 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005909 wl_list_for_each(view, &next->views, surface_link)
5910 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005911
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005912 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005913 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005914 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005915}
5916
5917static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005918switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005919{
5920 struct switcher *switcher =
5921 container_of(listener, struct switcher, listener);
5922
5923 switcher_next(switcher);
5924}
5925
5926static void
Daniel Stone351eb612012-05-31 15:27:47 -04005927switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005928{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005929 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005930 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005931 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005932
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005933 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005934 if (is_focus_view(view))
5935 continue;
5936
Jason Ekstranda7af7042013-10-12 22:38:11 -05005937 view->alpha = 1.0;
5938 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005939 }
5940
Alex Wu07b26062012-03-12 16:06:01 +08005941 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005942 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06005943 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005944 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005945 weston_keyboard_end_grab(keyboard);
5946 if (keyboard->input_method_resource)
5947 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005948
5949 /* re-hide surfaces that were temporary shown during the switch */
5950 struct weston_view **minimized;
5951 wl_array_for_each(minimized, &switcher->minimized_array) {
5952 /* with the exception of the current selected */
5953 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005954 weston_layer_entry_remove(&(*minimized)->layer_link);
5955 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005956 weston_view_damage_below(*minimized);
5957 }
5958 }
5959 wl_array_release(&switcher->minimized_array);
5960
Kristian Høgsberg07045392012-02-19 18:52:44 -05005961 free(switcher);
5962}
5963
5964static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005965switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005966 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005967{
5968 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005969 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005970
Daniel Stonec9785ea2012-05-30 16:31:52 +01005971 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005972 switcher_next(switcher);
5973}
5974
5975static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005976switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005977 uint32_t mods_depressed, uint32_t mods_latched,
5978 uint32_t mods_locked, uint32_t group)
5979{
5980 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06005981 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005982
Daniel Stone351eb612012-05-31 15:27:47 -04005983 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5984 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005985}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005986
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005987static void
5988switcher_cancel(struct weston_keyboard_grab *grab)
5989{
5990 struct switcher *switcher = container_of(grab, struct switcher, grab);
5991
5992 switcher_destroy(switcher);
5993}
5994
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005995static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005996 switcher_key,
5997 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005998 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005999};
6000
6001static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006002switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006003 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006004{
Tiago Vignattibe143262012-04-16 17:31:41 +03006005 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006006 struct switcher *switcher;
6007
6008 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006009 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006010 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006011 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006012 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006013 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006014
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006015 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006016 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006017 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006018 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
6019 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006020 switcher_next(switcher);
6021}
6022
Pekka Paalanen3c647232011-12-22 13:43:43 +02006023static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006024backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006025 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006026{
6027 struct weston_compositor *compositor = data;
6028 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006029 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006030
6031 /* TODO: we're limiting to simple use cases, where we assume just
6032 * control on the primary display. We'd have to extend later if we
6033 * ever get support for setting backlights on random desktop LCD
6034 * panels though */
6035 output = get_default_output(compositor);
6036 if (!output)
6037 return;
6038
6039 if (!output->set_backlight)
6040 return;
6041
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006042 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6043 backlight_new = output->backlight_current - 25;
6044 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6045 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006046
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006047 if (backlight_new < 5)
6048 backlight_new = 5;
6049 if (backlight_new > 255)
6050 backlight_new = 255;
6051
6052 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006053 output->set_backlight(output, output->backlight_current);
6054}
6055
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006056static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006057force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006058 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006059{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006060 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006061 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006062 struct desktop_shell *shell = data;
6063 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006064 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006065
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006066 focus_surface = seat->keyboard->focus;
6067 if (!focus_surface)
6068 return;
6069
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006070 wl_signal_emit(&compositor->kill_signal, focus_surface);
6071
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006072 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006073 wl_client_get_credentials(client, &pid, NULL, NULL);
6074
6075 /* Skip clients that we launched ourselves (the credentials of
6076 * the socketpair is ours) */
6077 if (pid == getpid())
6078 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006079
Daniel Stone325fc2d2012-05-30 16:31:58 +01006080 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006081}
6082
6083static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006084workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006085 uint32_t key, void *data)
6086{
6087 struct desktop_shell *shell = data;
6088 unsigned int new_index = shell->workspaces.current;
6089
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006090 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006091 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006092 if (new_index != 0)
6093 new_index--;
6094
6095 change_workspace(shell, new_index);
6096}
6097
6098static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006099workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006100 uint32_t key, void *data)
6101{
6102 struct desktop_shell *shell = data;
6103 unsigned int new_index = shell->workspaces.current;
6104
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006105 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006106 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006107 if (new_index < shell->workspaces.num - 1)
6108 new_index++;
6109
6110 change_workspace(shell, new_index);
6111}
6112
6113static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006114workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006115 uint32_t key, void *data)
6116{
6117 struct desktop_shell *shell = data;
6118 unsigned int new_index;
6119
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006120 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006121 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006122 new_index = key - KEY_F1;
6123 if (new_index >= shell->workspaces.num)
6124 new_index = shell->workspaces.num - 1;
6125
6126 change_workspace(shell, new_index);
6127}
6128
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006129static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006130workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006131 uint32_t key, void *data)
6132{
6133 struct desktop_shell *shell = data;
6134 unsigned int new_index = shell->workspaces.current;
6135
6136 if (shell->locked)
6137 return;
6138
6139 if (new_index != 0)
6140 new_index--;
6141
6142 take_surface_to_workspace_by_seat(shell, seat, new_index);
6143}
6144
6145static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006146workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006147 uint32_t key, void *data)
6148{
6149 struct desktop_shell *shell = data;
6150 unsigned int new_index = shell->workspaces.current;
6151
6152 if (shell->locked)
6153 return;
6154
6155 if (new_index < shell->workspaces.num - 1)
6156 new_index++;
6157
6158 take_surface_to_workspace_by_seat(shell, seat, new_index);
6159}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006160
6161static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006162shell_reposition_view_on_output_destroy(struct weston_view *view)
6163{
6164 struct weston_output *output, *first_output;
6165 struct weston_compositor *ec = view->surface->compositor;
6166 struct shell_surface *shsurf;
6167 float x, y;
6168 int visible;
6169
6170 x = view->geometry.x;
6171 y = view->geometry.y;
6172
6173 /* At this point the destroyed output is not in the list anymore.
6174 * If the view is still visible somewhere, we leave where it is,
6175 * otherwise, move it to the first output. */
6176 visible = 0;
6177 wl_list_for_each(output, &ec->output_list, link) {
6178 if (pixman_region32_contains_point(&output->region,
6179 x, y, NULL)) {
6180 visible = 1;
6181 break;
6182 }
6183 }
6184
6185 if (!visible) {
6186 first_output = container_of(ec->output_list.next,
6187 struct weston_output, link);
6188
6189 x = first_output->x + first_output->width / 4;
6190 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006191
6192 weston_view_set_position(view, x, y);
6193 } else {
6194 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006195 }
6196
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006197
6198 shsurf = get_shell_surface(view->surface);
6199
6200 if (shsurf) {
6201 shsurf->saved_position_valid = false;
6202 shsurf->next_state.maximized = false;
6203 shsurf->next_state.fullscreen = false;
6204 shsurf->state_changed = true;
6205 }
6206}
6207
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006208void
6209shell_for_each_layer(struct desktop_shell *shell,
6210 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006211{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006212 struct workspace **ws;
6213
6214 func(shell, &shell->fullscreen_layer, data);
6215 func(shell, &shell->panel_layer, data);
6216 func(shell, &shell->background_layer, data);
6217 func(shell, &shell->lock_layer, data);
6218 func(shell, &shell->input_panel_layer, data);
6219
6220 wl_array_for_each(ws, &shell->workspaces.array)
6221 func(shell, &(*ws)->layer, data);
6222}
6223
6224static void
6225shell_output_destroy_move_layer(struct desktop_shell *shell,
6226 struct weston_layer *layer,
6227 void *data)
6228{
6229 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006230 struct weston_view *view;
6231
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006232 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006233 if (view->output != output)
6234 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006235
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006236 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006237 }
6238}
6239
6240static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006241handle_output_destroy(struct wl_listener *listener, void *data)
6242{
6243 struct shell_output *output_listener =
6244 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006245 struct weston_output *output = output_listener->output;
6246 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006247
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006248 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006249
Xiong Zhang6b481422013-10-23 13:58:32 +08006250 wl_list_remove(&output_listener->destroy_listener.link);
6251 wl_list_remove(&output_listener->link);
6252 free(output_listener);
6253}
6254
6255static void
6256create_shell_output(struct desktop_shell *shell,
6257 struct weston_output *output)
6258{
6259 struct shell_output *shell_output;
6260
6261 shell_output = zalloc(sizeof *shell_output);
6262 if (shell_output == NULL)
6263 return;
6264
6265 shell_output->output = output;
6266 shell_output->shell = shell;
6267 shell_output->destroy_listener.notify = handle_output_destroy;
6268 wl_signal_add(&output->destroy_signal,
6269 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006270 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006271}
6272
6273static void
6274handle_output_create(struct wl_listener *listener, void *data)
6275{
6276 struct desktop_shell *shell =
6277 container_of(listener, struct desktop_shell, output_create_listener);
6278 struct weston_output *output = (struct weston_output *)data;
6279
6280 create_shell_output(shell, output);
6281}
6282
6283static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006284handle_output_move_layer(struct desktop_shell *shell,
6285 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006286{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006287 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006288 struct weston_view *view;
6289 float x, y;
6290
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006291 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006292 if (view->output != output)
6293 continue;
6294
6295 x = view->geometry.x + output->move_x;
6296 y = view->geometry.y + output->move_y;
6297 weston_view_set_position(view, x, y);
6298 }
6299}
6300
6301static void
6302handle_output_move(struct wl_listener *listener, void *data)
6303{
6304 struct desktop_shell *shell;
6305
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006306 shell = container_of(listener, struct desktop_shell,
6307 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006308
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006309 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006310}
6311
6312static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006313setup_output_destroy_handler(struct weston_compositor *ec,
6314 struct desktop_shell *shell)
6315{
6316 struct weston_output *output;
6317
6318 wl_list_init(&shell->output_list);
6319 wl_list_for_each(output, &ec->output_list, link)
6320 create_shell_output(shell, output);
6321
6322 shell->output_create_listener.notify = handle_output_create;
6323 wl_signal_add(&ec->output_created_signal,
6324 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006325
6326 shell->output_move_listener.notify = handle_output_move;
6327 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006328}
6329
6330static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006331shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006332{
Tiago Vignattibe143262012-04-16 17:31:41 +03006333 struct desktop_shell *shell =
6334 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006335 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006336 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006337
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006338 /* Force state to unlocked so we don't try to fade */
6339 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006340
6341 if (shell->child.client) {
6342 /* disable respawn */
6343 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006344 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006345 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006346
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006347 wl_list_remove(&shell->idle_listener.link);
6348 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006349
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006350 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006351 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006352
Xiong Zhang6b481422013-10-23 13:58:32 +08006353 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6354 wl_list_remove(&shell_output->destroy_listener.link);
6355 wl_list_remove(&shell_output->link);
6356 free(shell_output);
6357 }
6358
6359 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006360 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006361
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006362 wl_array_for_each(ws, &shell->workspaces.array)
6363 workspace_destroy(*ws);
6364 wl_array_release(&shell->workspaces.array);
6365
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006366 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006367 free(shell);
6368}
6369
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006370static void
6371shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6372{
6373 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006374 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006375
6376 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006377 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6378 MODIFIER_CTRL | MODIFIER_ALT,
6379 terminate_binding, ec);
6380 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6381 click_to_activate_binding,
6382 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006383 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6384 click_to_activate_binding,
6385 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006386 weston_compositor_add_touch_binding(ec, 0,
6387 touch_to_activate_binding,
6388 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006389 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6390 MODIFIER_SUPER | MODIFIER_ALT,
6391 surface_opacity_binding, NULL);
6392 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6393 MODIFIER_SUPER, zoom_axis_binding,
6394 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006395
6396 /* configurable bindings */
6397 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006398 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6399 zoom_key_binding, NULL);
6400 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6401 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006402 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6403 maximize_binding, NULL);
6404 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6405 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006406 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6407 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006408 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006409 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6410 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006411 weston_compositor_add_button_binding(ec, BTN_LEFT,
6412 mod | MODIFIER_SHIFT,
6413 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006414
6415 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6416 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6417 rotate_binding, NULL);
6418
Daniel Stone325fc2d2012-05-30 16:31:58 +01006419 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6420 shell);
6421 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6422 ec);
6423 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6424 backlight_binding, ec);
6425 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6426 ec);
6427 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6428 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006429 weston_compositor_add_key_binding(ec, KEY_K, mod,
6430 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006431 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6432 workspace_up_binding, shell);
6433 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6434 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006435 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6436 workspace_move_surface_up_binding,
6437 shell);
6438 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6439 workspace_move_surface_down_binding,
6440 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006441
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006442 if (shell->exposay_modifier)
6443 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6444 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006445
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006446 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6447 if (shell->workspaces.num > 1) {
6448 num_workspace_bindings = shell->workspaces.num;
6449 if (num_workspace_bindings > 6)
6450 num_workspace_bindings = 6;
6451 for (i = 0; i < num_workspace_bindings; i++)
6452 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6453 workspace_f_binding,
6454 shell);
6455 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006456
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006457 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006458}
6459
Jason Ekstrand024177c2014-04-21 19:42:58 -05006460static void
6461handle_seat_created(struct wl_listener *listener, void *data)
6462{
6463 struct weston_seat *seat = data;
6464
6465 create_shell_seat(seat);
6466}
6467
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006468WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006469module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006470 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006471{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006472 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006473 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006474 struct workspace **pws;
6475 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006476 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006477
Peter Huttererf3d62272013-08-08 11:57:05 +10006478 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006479 if (shell == NULL)
6480 return -1;
6481
Kristian Høgsberg75840622011-09-06 13:48:16 -04006482 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006483
6484 shell->destroy_listener.notify = shell_destroy;
6485 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006486 shell->idle_listener.notify = idle_handler;
6487 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6488 shell->wake_listener.notify = wake_handler;
6489 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006490
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006491 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006492 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006493 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006494 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006495 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006496 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006497 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006498 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006499 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006500 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006501 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006502 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006503 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006504
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006505 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6506 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006507 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6508 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006509 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006510
6511 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006512 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006513
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006514 if (input_panel_setup(shell) < 0)
6515 return -1;
6516
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006517 shell->text_backend = text_backend_init(ec);
6518 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006519 return -1;
6520
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006521 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006522
Daniel Stonedf8133b2013-11-19 11:37:14 +01006523 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6524 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6525
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006526 for (i = 0; i < shell->workspaces.num; i++) {
6527 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6528 if (pws == NULL)
6529 return -1;
6530
6531 *pws = workspace_create();
6532 if (*pws == NULL)
6533 return -1;
6534 }
6535 activate_workspace(shell, 0);
6536
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006537 weston_layer_init(&shell->minimized_layer, NULL);
6538
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006539 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006540 wl_list_init(&shell->workspaces.animation.link);
6541 shell->workspaces.animation.frame = animate_workspace_change_frame;
6542
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006543 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006544 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006545 return -1;
6546
Rafael Antognollie2a34552013-12-03 15:35:45 -02006547 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6548 shell, bind_xdg_shell) == NULL)
6549 return -1;
6550
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006551 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006552 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006553 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006554 return -1;
6555
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006556 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6557 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006558 return -1;
6559
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006560 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006561
Jonny Lamb765760d2014-08-20 15:53:19 +02006562 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6563
Xiong Zhang6b481422013-10-23 13:58:32 +08006564 setup_output_destroy_handler(ec, shell);
6565
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006566 loop = wl_display_get_event_loop(ec->wl_display);
6567 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006568
Jason Ekstrand024177c2014-04-21 19:42:58 -05006569 wl_list_for_each(seat, &ec->seat_list, link)
6570 handle_seat_created(NULL, seat);
6571 shell->seat_create_listener.notify = handle_seat_created;
6572 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006573
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006574 screenshooter_create(ec);
6575
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006576 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006577
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006578 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006579
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006580 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6581
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006582 return 0;
6583}