blob: a60a3aa404c84c48f1666a5956c368dd5b53bf5d [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;
610 int duration;
Derek Foremanc7210432014-08-21 11:32:38 -0500611 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300612 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200613
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400614 section = weston_config_get_section(shell->compositor->config,
615 "screensaver", NULL, NULL);
616 weston_config_section_get_string(section,
617 "path", &shell->screensaver.path, NULL);
618 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200619 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400620
621 section = weston_config_get_section(shell->compositor->config,
622 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300623 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
624 WESTON_SHELL_CLIENT);
625 if (ret < 0)
626 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400627 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500628 "client", &s, client);
629 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100630 shell->client = s;
631 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400632 "binding-modifier", &s, "super");
633 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200634 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800635
636 weston_config_section_get_string(section,
637 "exposay-modifier", &s, "none");
638 if (strcmp(s, "none") == 0)
639 shell->exposay_modifier = 0;
640 else
641 shell->exposay_modifier = get_modifier(s);
642 free(s);
643
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400644 weston_config_section_get_string(section, "animation", &s, "none");
645 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200646 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200647 weston_config_section_get_string(section, "close-animation", &s, "fade");
648 shell->win_close_animation_type = get_animation_type(s);
649 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700650 weston_config_section_get_string(section,
651 "startup-animation", &s, "fade");
652 shell->startup_animation_type = get_animation_type(s);
653 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700654 if (shell->startup_animation_type == ANIMATION_ZOOM)
655 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100656 weston_config_section_get_string(section, "focus-animation", &s, "none");
657 shell->focus_animation_type = get_animation_type(s);
658 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400659 weston_config_section_get_uint(section, "num-workspaces",
660 &shell->workspaces.num,
661 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200662}
663
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800664struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100665get_default_output(struct weston_compositor *compositor)
666{
667 return container_of(compositor->output_list.next,
668 struct weston_output, link);
669}
670
Pekka Paalanen8274d902014-08-06 19:36:51 +0300671static int
672focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
673{
674 return snprintf(buf, len, "focus highlight effect for output %s",
675 surface->output->name);
676}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100677
678/* no-op func for checking focus surface */
679static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600680focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100681{
682}
683
684static struct focus_surface *
685get_focus_surface(struct weston_surface *surface)
686{
687 if (surface->configure == focus_surface_configure)
688 return surface->configure_private;
689 else
690 return NULL;
691}
692
693static bool
694is_focus_surface (struct weston_surface *es)
695{
696 return (es->configure == focus_surface_configure);
697}
698
699static bool
700is_focus_view (struct weston_view *view)
701{
702 return is_focus_surface (view->surface);
703}
704
705static struct focus_surface *
706create_focus_surface(struct weston_compositor *ec,
707 struct weston_output *output)
708{
709 struct focus_surface *fsurf = NULL;
710 struct weston_surface *surface = NULL;
711
712 fsurf = malloc(sizeof *fsurf);
713 if (!fsurf)
714 return NULL;
715
716 fsurf->surface = weston_surface_create(ec);
717 surface = fsurf->surface;
718 if (surface == NULL) {
719 free(fsurf);
720 return NULL;
721 }
722
723 surface->configure = focus_surface_configure;
724 surface->output = output;
725 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300726 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100727
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800728 fsurf->view = weston_view_create(surface);
729 if (fsurf->view == NULL) {
730 weston_surface_destroy(surface);
731 free(fsurf);
732 return NULL;
733 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100734 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100735
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600736 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600737 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100738 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
739 pixman_region32_fini(&surface->opaque);
740 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
741 output->width, output->height);
742 pixman_region32_fini(&surface->input);
743 pixman_region32_init(&surface->input);
744
745 wl_list_init(&fsurf->workspace_transform.link);
746
747 return fsurf;
748}
749
750static void
751focus_surface_destroy(struct focus_surface *fsurf)
752{
753 weston_surface_destroy(fsurf->surface);
754 free(fsurf);
755}
756
757static void
758focus_animation_done(struct weston_view_animation *animation, void *data)
759{
760 struct workspace *ws = data;
761
762 ws->focus_animation = NULL;
763}
764
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200765static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200766focus_state_destroy(struct focus_state *state)
767{
768 wl_list_remove(&state->seat_destroy_listener.link);
769 wl_list_remove(&state->surface_destroy_listener.link);
770 free(state);
771}
772
773static void
774focus_state_seat_destroy(struct wl_listener *listener, void *data)
775{
776 struct focus_state *state = container_of(listener,
777 struct focus_state,
778 seat_destroy_listener);
779
780 wl_list_remove(&state->link);
781 focus_state_destroy(state);
782}
783
784static void
785focus_state_surface_destroy(struct wl_listener *listener, void *data)
786{
787 struct focus_state *state = container_of(listener,
788 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400789 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400790 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500791 struct weston_surface *main_surface, *next;
792 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200793
Pekka Paalanen01388e22013-04-25 13:57:44 +0300794 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
795
Kristian Høgsberge3778222012-07-31 17:29:30 -0400796 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300797 wl_list_for_each(view,
798 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500799 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400800 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100801 if (is_focus_view(view))
802 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400803
Jason Ekstranda7af7042013-10-12 22:38:11 -0500804 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400805 break;
806 }
807
Pekka Paalanen01388e22013-04-25 13:57:44 +0300808 /* if the focus was a sub-surface, activate its main surface */
809 if (main_surface != state->keyboard_focus)
810 next = main_surface;
811
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100812 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400813 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100814 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100815 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400816 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100817 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
818 if (state->ws->focus_animation)
819 weston_view_animation_destroy(state->ws->focus_animation);
820
821 state->ws->focus_animation = weston_fade_run(
822 state->ws->fsurf_front->view,
823 state->ws->fsurf_front->view->alpha, 0.0, 300,
824 focus_animation_done, state->ws);
825 }
826
Kristian Høgsberge3778222012-07-31 17:29:30 -0400827 wl_list_remove(&state->link);
828 focus_state_destroy(state);
829 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200830}
831
832static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400833focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200834{
Jonas Ådahl04769742012-06-13 00:01:24 +0200835 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200836
837 state = malloc(sizeof *state);
838 if (state == NULL)
839 return NULL;
840
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400842 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200843 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400844 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200845
846 state->seat_destroy_listener.notify = focus_state_seat_destroy;
847 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400848 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200849 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400850 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200851
852 return state;
853}
854
Jonas Ådahl8538b222012-08-29 22:13:03 +0200855static struct focus_state *
856ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
857{
858 struct workspace *ws = get_current_workspace(shell);
859 struct focus_state *state;
860
861 wl_list_for_each(state, &ws->focus_list, link)
862 if (state->seat == seat)
863 break;
864
865 if (&state->link == &ws->focus_list)
866 state = focus_state_create(seat, ws);
867
868 return state;
869}
870
Jonas Ådahl04769742012-06-13 00:01:24 +0200871static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800872focus_state_set_focus(struct focus_state *state,
873 struct weston_surface *surface)
874{
875 if (state->keyboard_focus) {
876 wl_list_remove(&state->surface_destroy_listener.link);
877 wl_list_init(&state->surface_destroy_listener.link);
878 }
879
880 state->keyboard_focus = surface;
881 if (surface)
882 wl_signal_add(&surface->destroy_signal,
883 &state->surface_destroy_listener);
884}
885
886static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400887restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200888{
889 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400890 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100891 struct wl_list pending_seat_list;
892 struct weston_seat *seat, *next_seat;
893
894 /* Temporarily steal the list of seats so that we can keep
895 * track of the seats we've already processed */
896 wl_list_init(&pending_seat_list);
897 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
898 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200899
900 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100901 wl_list_remove(&state->seat->link);
902 wl_list_insert(&shell->compositor->seat_list,
903 &state->seat->link);
904
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800905 if (state->seat->keyboard == NULL)
906 continue;
907
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400908 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200909
Kristian Høgsberge3148752013-05-06 23:19:49 -0400910 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200911 }
Neil Roberts4237f502014-04-09 16:33:31 +0100912
913 /* For any remaining seats that we don't have a focus state
914 * for we'll reset the keyboard focus to NULL */
915 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
916 wl_list_insert(&shell->compositor->seat_list, &seat->link);
917
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300918 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100919 continue;
920
921 weston_keyboard_set_focus(seat->keyboard, NULL);
922 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200923}
924
925static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200926replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
927 struct weston_seat *seat)
928{
929 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200930
931 wl_list_for_each(state, &ws->focus_list, link) {
932 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800933 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200934 return;
935 }
936 }
937}
938
939static void
940drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
941 struct weston_surface *surface)
942{
943 struct focus_state *state;
944
945 wl_list_for_each(state, &ws->focus_list, link)
946 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800947 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200948}
949
950static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100951animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
952 struct weston_view *from, struct weston_view *to)
953{
954 struct weston_output *output;
955 bool focus_surface_created = false;
956
957 /* FIXME: Only support dim animation using two layers */
958 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
959 return;
960
961 output = get_default_output(shell->compositor);
962 if (ws->fsurf_front == NULL && (from || to)) {
963 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800964 if (ws->fsurf_front == NULL)
965 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800967
968 ws->fsurf_back = create_focus_surface(shell->compositor, output);
969 if (ws->fsurf_back == NULL) {
970 focus_surface_destroy(ws->fsurf_front);
971 return;
972 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100973 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800974
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100975 focus_surface_created = true;
976 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300977 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
978 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100979 }
980
981 if (ws->focus_animation) {
982 weston_view_animation_destroy(ws->focus_animation);
983 ws->focus_animation = NULL;
984 }
985
986 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300987 weston_layer_entry_insert(&to->layer_link,
988 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100989 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300990 weston_layer_entry_insert(&ws->layer.view_list,
991 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100992
993 if (focus_surface_created) {
994 ws->focus_animation = weston_fade_run(
995 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200996 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100997 focus_animation_done, ws);
998 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300999 weston_layer_entry_insert(&from->layer_link,
1000 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001001 ws->focus_animation = weston_stable_fade_run(
1002 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001003 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001004 focus_animation_done, ws);
1005 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001006 weston_layer_entry_insert(&ws->layer.view_list,
1007 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001008 ws->focus_animation = weston_stable_fade_run(
1009 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001010 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001011 focus_animation_done, ws);
1012 }
1013}
1014
1015static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001016workspace_destroy(struct workspace *ws)
1017{
Jonas Ådahl04769742012-06-13 00:01:24 +02001018 struct focus_state *state, *next;
1019
1020 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1021 focus_state_destroy(state);
1022
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001023 if (ws->fsurf_front)
1024 focus_surface_destroy(ws->fsurf_front);
1025 if (ws->fsurf_back)
1026 focus_surface_destroy(ws->fsurf_back);
1027
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001028 free(ws);
1029}
1030
Jonas Ådahl04769742012-06-13 00:01:24 +02001031static void
1032seat_destroyed(struct wl_listener *listener, void *data)
1033{
1034 struct weston_seat *seat = data;
1035 struct focus_state *state, *next;
1036 struct workspace *ws = container_of(listener,
1037 struct workspace,
1038 seat_destroyed_listener);
1039
1040 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1041 if (state->seat == seat)
1042 wl_list_remove(&state->link);
1043}
1044
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001045static struct workspace *
1046workspace_create(void)
1047{
1048 struct workspace *ws = malloc(sizeof *ws);
1049 if (ws == NULL)
1050 return NULL;
1051
1052 weston_layer_init(&ws->layer, NULL);
1053
Jonas Ådahl04769742012-06-13 00:01:24 +02001054 wl_list_init(&ws->focus_list);
1055 wl_list_init(&ws->seat_destroyed_listener.link);
1056 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001057 ws->fsurf_front = NULL;
1058 ws->fsurf_back = NULL;
1059 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001060
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001061 return ws;
1062}
1063
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001064static int
1065workspace_is_empty(struct workspace *ws)
1066{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001067 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001068}
1069
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001070static struct workspace *
1071get_workspace(struct desktop_shell *shell, unsigned int index)
1072{
1073 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001074 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001075 pws += index;
1076 return *pws;
1077}
1078
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001079struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001080get_current_workspace(struct desktop_shell *shell)
1081{
1082 return get_workspace(shell, shell->workspaces.current);
1083}
1084
1085static void
1086activate_workspace(struct desktop_shell *shell, unsigned int index)
1087{
1088 struct workspace *ws;
1089
1090 ws = get_workspace(shell, index);
1091 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1092
1093 shell->workspaces.current = index;
1094}
1095
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001096static unsigned int
1097get_output_height(struct weston_output *output)
1098{
1099 return abs(output->region.extents.y1 - output->region.extents.y2);
1100}
1101
1102static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001103view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001104{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105 struct weston_transform *transform;
1106
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001107 if (is_focus_view(view)) {
1108 struct focus_surface *fsurf = get_focus_surface(view->surface);
1109 transform = &fsurf->workspace_transform;
1110 } else {
1111 struct shell_surface *shsurf = get_shell_surface(view->surface);
1112 transform = &shsurf->workspace_transform;
1113 }
1114
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001115 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001117 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001118
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001119 weston_matrix_init(&transform->matrix);
1120 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001121 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001123}
1124
1125static void
1126workspace_translate_out(struct workspace *ws, double fraction)
1127{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129 unsigned int height;
1130 double d;
1131
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001132 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001134 d = height * fraction;
1135
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001136 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001137 }
1138}
1139
1140static void
1141workspace_translate_in(struct workspace *ws, double fraction)
1142{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001144 unsigned int height;
1145 double d;
1146
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001147 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001149
1150 if (fraction > 0)
1151 d = -(height - height * fraction);
1152 else
1153 d = height + height * fraction;
1154
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001155 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001156 }
1157}
1158
1159static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001160broadcast_current_workspace_state(struct desktop_shell *shell)
1161{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001162 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001163
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001164 wl_resource_for_each(resource, &shell->workspaces.client_list)
1165 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001166 shell->workspaces.current,
1167 shell->workspaces.num);
1168}
1169
1170static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001171reverse_workspace_change_animation(struct desktop_shell *shell,
1172 unsigned int index,
1173 struct workspace *from,
1174 struct workspace *to)
1175{
1176 shell->workspaces.current = index;
1177
1178 shell->workspaces.anim_to = to;
1179 shell->workspaces.anim_from = from;
1180 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1181 shell->workspaces.anim_timestamp = 0;
1182
Scott Moreau4272e632012-08-13 09:58:41 -06001183 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001184}
1185
1186static void
1187workspace_deactivate_transforms(struct workspace *ws)
1188{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001190 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001191
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001192 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001193 if (is_focus_view(view)) {
1194 struct focus_surface *fsurf = get_focus_surface(view->surface);
1195 transform = &fsurf->workspace_transform;
1196 } else {
1197 struct shell_surface *shsurf = get_shell_surface(view->surface);
1198 transform = &shsurf->workspace_transform;
1199 }
1200
1201 if (!wl_list_empty(&transform->link)) {
1202 wl_list_remove(&transform->link);
1203 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001206 }
1207}
1208
1209static void
1210finish_workspace_change_animation(struct desktop_shell *shell,
1211 struct workspace *from,
1212 struct workspace *to)
1213{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001214 struct weston_view *view;
1215
Scott Moreau4272e632012-08-13 09:58:41 -06001216 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001217
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001218 /* Views that extend past the bottom of the output are still
1219 * visible after the workspace animation ends but before its layer
1220 * is hidden. In that case, we need to damage below those views so
1221 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001222 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001223 weston_view_damage_below(view);
1224
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001225 wl_list_remove(&shell->workspaces.animation.link);
1226 workspace_deactivate_transforms(from);
1227 workspace_deactivate_transforms(to);
1228 shell->workspaces.anim_to = NULL;
1229
1230 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1231}
1232
1233static void
1234animate_workspace_change_frame(struct weston_animation *animation,
1235 struct weston_output *output, uint32_t msecs)
1236{
1237 struct desktop_shell *shell =
1238 container_of(animation, struct desktop_shell,
1239 workspaces.animation);
1240 struct workspace *from = shell->workspaces.anim_from;
1241 struct workspace *to = shell->workspaces.anim_to;
1242 uint32_t t;
1243 double x, y;
1244
1245 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1246 finish_workspace_change_animation(shell, from, to);
1247 return;
1248 }
1249
1250 if (shell->workspaces.anim_timestamp == 0) {
1251 if (shell->workspaces.anim_current == 0.0)
1252 shell->workspaces.anim_timestamp = msecs;
1253 else
1254 shell->workspaces.anim_timestamp =
1255 msecs -
1256 /* Invers of movement function 'y' below. */
1257 (asin(1.0 - shell->workspaces.anim_current) *
1258 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1259 M_2_PI);
1260 }
1261
1262 t = msecs - shell->workspaces.anim_timestamp;
1263
1264 /*
1265 * x = [0, π/2]
1266 * y(x) = sin(x)
1267 */
1268 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1269 y = sin(x);
1270
1271 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001272 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001273
1274 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1275 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1276 shell->workspaces.anim_current = y;
1277
Scott Moreau4272e632012-08-13 09:58:41 -06001278 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001279 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001280 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001281 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001282}
1283
1284static void
1285animate_workspace_change(struct desktop_shell *shell,
1286 unsigned int index,
1287 struct workspace *from,
1288 struct workspace *to)
1289{
1290 struct weston_output *output;
1291
1292 int dir;
1293
1294 if (index > shell->workspaces.current)
1295 dir = -1;
1296 else
1297 dir = 1;
1298
1299 shell->workspaces.current = index;
1300
1301 shell->workspaces.anim_dir = dir;
1302 shell->workspaces.anim_from = from;
1303 shell->workspaces.anim_to = to;
1304 shell->workspaces.anim_current = 0.0;
1305 shell->workspaces.anim_timestamp = 0;
1306
1307 output = container_of(shell->compositor->output_list.next,
1308 struct weston_output, link);
1309 wl_list_insert(&output->animation_list,
1310 &shell->workspaces.animation.link);
1311
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001312 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001313
1314 workspace_translate_in(to, 0);
1315
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001316 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001317
Scott Moreau4272e632012-08-13 09:58:41 -06001318 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001319}
1320
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001321static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001322update_workspace(struct desktop_shell *shell, unsigned int index,
1323 struct workspace *from, struct workspace *to)
1324{
1325 shell->workspaces.current = index;
1326 wl_list_insert(&from->layer.link, &to->layer.link);
1327 wl_list_remove(&from->layer.link);
1328}
1329
1330static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001331change_workspace(struct desktop_shell *shell, unsigned int index)
1332{
1333 struct workspace *from;
1334 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001335 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001336
1337 if (index == shell->workspaces.current)
1338 return;
1339
1340 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001341 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001342 return;
1343
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001344 from = get_current_workspace(shell);
1345 to = get_workspace(shell, index);
1346
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001347 if (shell->workspaces.anim_from == to &&
1348 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001349 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001350 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001351 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001352 return;
1353 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001354
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001355 if (shell->workspaces.anim_to != NULL)
1356 finish_workspace_change_animation(shell,
1357 shell->workspaces.anim_from,
1358 shell->workspaces.anim_to);
1359
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001360 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001361
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001362 if (shell->focus_animation_type != ANIMATION_NONE) {
1363 wl_list_for_each(state, &from->focus_list, link)
1364 if (state->keyboard_focus)
1365 animate_focus_change(shell, from,
1366 get_default_view(state->keyboard_focus), NULL);
1367
1368 wl_list_for_each(state, &to->focus_list, link)
1369 if (state->keyboard_focus)
1370 animate_focus_change(shell, to,
1371 NULL, get_default_view(state->keyboard_focus));
1372 }
1373
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001374 if (workspace_is_empty(to) && workspace_is_empty(from))
1375 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001376 else
1377 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001378
1379 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001380}
1381
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001382static bool
1383workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1384{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001385 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001386 struct wl_list *e;
1387
1388 if (wl_list_empty(list))
1389 return false;
1390
1391 e = list->next;
1392
1393 if (e->next != list)
1394 return false;
1395
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001396 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001397}
1398
1399static void
Philip Withnall659163d2013-11-25 18:01:36 +00001400move_surface_to_workspace(struct desktop_shell *shell,
1401 struct shell_surface *shsurf,
1402 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001403{
1404 struct workspace *from;
1405 struct workspace *to;
1406 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001407 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001408 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001409
1410 if (workspace == shell->workspaces.current)
1411 return;
1412
Philip Withnall659163d2013-11-25 18:01:36 +00001413 view = get_default_view(shsurf->surface);
1414 if (!view)
1415 return;
1416
1417 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1418
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001419 if (workspace >= shell->workspaces.num)
1420 workspace = shell->workspaces.num - 1;
1421
Jonas Ådahle9d22502012-08-29 22:13:01 +02001422 from = get_current_workspace(shell);
1423 to = get_workspace(shell, workspace);
1424
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001425 weston_layer_entry_remove(&view->layer_link);
1426 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001427
Philip Withnall648a4dd2013-11-25 18:01:44 +00001428 shell_surface_update_child_surface_layers(shsurf);
1429
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001431 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1432 if (!seat->keyboard)
1433 continue;
1434
1435 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001437 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001438 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001439
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001441}
1442
1443static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001444take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001445 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001446 unsigned int index)
1447{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001448 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001450 struct shell_surface *shsurf;
1451 struct workspace *from;
1452 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001453 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001454
Pekka Paalanen01388e22013-04-25 13:57:44 +03001455 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 view = get_default_view(surface);
1457 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001458 index == shell->workspaces.current ||
1459 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001460 return;
1461
1462 from = get_current_workspace(shell);
1463 to = get_workspace(shell, index);
1464
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001465 weston_layer_entry_remove(&view->layer_link);
1466 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001467
Philip Withnall659163d2013-11-25 18:01:36 +00001468 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001469 if (shsurf != NULL)
1470 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001471
Jonas Ådahle9d22502012-08-29 22:13:01 +02001472 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001473 drop_focus_state(shell, from, surface);
1474
1475 if (shell->workspaces.anim_from == to &&
1476 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001477 wl_list_remove(&to->layer.link);
1478 wl_list_insert(from->layer.link.prev, &to->layer.link);
1479
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001480 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001481 broadcast_current_workspace_state(shell);
1482
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001483 return;
1484 }
1485
1486 if (shell->workspaces.anim_to != NULL)
1487 finish_workspace_change_animation(shell,
1488 shell->workspaces.anim_from,
1489 shell->workspaces.anim_to);
1490
1491 if (workspace_is_empty(from) &&
1492 workspace_has_only(to, surface))
1493 update_workspace(shell, index, from, to);
1494 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001495 if (shsurf != NULL &&
1496 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001497 wl_list_insert(&shell->workspaces.anim_sticky_list,
1498 &shsurf->workspace_transform.link);
1499
1500 animate_workspace_change(shell, index, from, to);
1501 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001502
1503 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001504
1505 state = ensure_focus_state(shell, seat);
1506 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001507 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001508}
1509
1510static void
1511workspace_manager_move_surface(struct wl_client *client,
1512 struct wl_resource *resource,
1513 struct wl_resource *surface_resource,
1514 uint32_t workspace)
1515{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001516 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001517 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001518 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001519 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001520 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001521
Pekka Paalanen01388e22013-04-25 13:57:44 +03001522 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001523 shell_surface = get_shell_surface(main_surface);
1524 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001526
1527 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001528}
1529
1530static const struct workspace_manager_interface workspace_manager_implementation = {
1531 workspace_manager_move_surface,
1532};
1533
1534static void
1535unbind_resource(struct wl_resource *resource)
1536{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001537 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001538}
1539
1540static void
1541bind_workspace_manager(struct wl_client *client,
1542 void *data, uint32_t version, uint32_t id)
1543{
1544 struct desktop_shell *shell = data;
1545 struct wl_resource *resource;
1546
Jason Ekstranda85118c2013-06-27 20:17:02 -05001547 resource = wl_resource_create(client,
1548 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001549
1550 if (resource == NULL) {
1551 weston_log("couldn't add workspace manager object");
1552 return;
1553 }
1554
Jason Ekstranda85118c2013-06-27 20:17:02 -05001555 wl_resource_set_implementation(resource,
1556 &workspace_manager_implementation,
1557 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001558 wl_list_insert(&shell->workspaces.client_list,
1559 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001560
1561 workspace_manager_send_state(resource,
1562 shell->workspaces.current,
1563 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001564}
1565
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001566static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001567touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1568 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1569{
1570}
1571
1572static void
1573touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1574{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001575 struct weston_touch_move_grab *move =
1576 (struct weston_touch_move_grab *) container_of(
1577 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001578
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001579 if (touch_id == 0)
1580 move->active = 0;
1581
Jonas Ådahl9484b692013-12-02 22:05:03 +01001582 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001583 shell_touch_grab_end(&move->base);
1584 free(move);
1585 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001586}
1587
1588static void
1589touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1590 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1591{
1592 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1593 struct shell_surface *shsurf = move->base.shsurf;
1594 struct weston_surface *es;
1595 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1596 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1597
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001598 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001599 return;
1600
1601 es = shsurf->surface;
1602
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001603 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001604
1605 weston_compositor_schedule_repaint(es->compositor);
1606}
1607
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001608static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001609touch_move_grab_frame(struct weston_touch_grab *grab)
1610{
1611}
1612
1613static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001614touch_move_grab_cancel(struct weston_touch_grab *grab)
1615{
1616 struct weston_touch_move_grab *move =
1617 (struct weston_touch_move_grab *) container_of(
1618 grab, struct shell_touch_grab, grab);
1619
1620 shell_touch_grab_end(&move->base);
1621 free(move);
1622}
1623
Rusty Lynch1084da52013-08-15 09:10:08 -07001624static const struct weston_touch_grab_interface touch_move_grab_interface = {
1625 touch_move_grab_down,
1626 touch_move_grab_up,
1627 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001628 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001629 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001630};
1631
1632static int
1633surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1634{
1635 struct weston_touch_move_grab *move;
1636
1637 if (!shsurf)
1638 return -1;
1639
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001640 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001641 return 0;
1642
1643 move = malloc(sizeof *move);
1644 if (!move)
1645 return -1;
1646
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001647 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001648 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001649 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001651 seat->touch->grab_y;
1652
1653 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1654 seat->touch);
1655
1656 return 0;
1657}
1658
1659static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001660noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001661{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001662}
1663
1664static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001665constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1666{
1667 struct shell_surface *shsurf = move->base.shsurf;
1668 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001669 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001670 const int safety = 50;
1671
1672 x = wl_fixed_to_int(pointer->x + move->dx);
1673 y = wl_fixed_to_int(pointer->y + move->dy);
1674
Jonny Lambd73c6942014-08-20 15:53:20 +02001675 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1676 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1677 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001678
Jonny Lambd73c6942014-08-20 15:53:20 +02001679 bottom = y + shsurf->geometry.height;
1680 if (bottom - panel_height < safety)
1681 y = panel_height + safety -
1682 shsurf->geometry.height;
1683
1684 if (move->client_initiated &&
1685 y + shsurf->geometry.y < panel_height)
1686 y = panel_height - shsurf->geometry.y;
1687 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001688
1689 *cx = x;
1690 *cy = y;
1691}
1692
1693static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001694move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1695 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001696{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001697 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001698 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001699 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001700 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001701
1702 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001703 if (!shsurf)
1704 return;
1705
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001706 constrain_position(move, &cx, &cy);
1707
1708 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001709
Jason Ekstranda7af7042013-10-12 22:38:11 -05001710 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001711}
1712
1713static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001714move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001715 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001716{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001717 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1718 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001719 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001720 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001721
Daniel Stone4dbadb12012-05-30 16:31:51 +01001722 if (pointer->button_count == 0 &&
1723 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001724 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001725 free(grab);
1726 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001727}
1728
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001729static void
1730move_grab_cancel(struct weston_pointer_grab *grab)
1731{
1732 struct shell_grab *shell_grab =
1733 container_of(grab, struct shell_grab, grab);
1734
1735 shell_grab_end(shell_grab);
1736 free(grab);
1737}
1738
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001739static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001740 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001741 move_grab_motion,
1742 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001743 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001744};
1745
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001746static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001747surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1748 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001749{
1750 struct weston_move_grab *move;
1751
1752 if (!shsurf)
1753 return -1;
1754
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001755 if (shsurf->grabbed ||
1756 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001757 return 0;
1758
1759 move = malloc(sizeof *move);
1760 if (!move)
1761 return -1;
1762
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001764 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001766 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001767 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001768
1769 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001770 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001771
1772 return 0;
1773}
1774
1775static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001776common_surface_move(struct wl_resource *resource,
1777 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001778{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001779 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001780 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001781 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001782
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001783 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001784 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001785 seat->pointer->button_count > 0 &&
1786 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001788 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001789 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001790 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001791 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001792 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001793 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001795 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001796 (surface_touch_move(shsurf, seat) < 0))
1797 wl_resource_post_no_memory(resource);
1798 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001799}
1800
Rafael Antognollie2a34552013-12-03 15:35:45 -02001801static void
1802shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1803 struct wl_resource *seat_resource, uint32_t serial)
1804{
1805 common_surface_move(resource, seat_resource, serial);
1806}
1807
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001808struct weston_resize_grab {
1809 struct shell_grab base;
1810 uint32_t edges;
1811 int32_t width, height;
1812};
1813
1814static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001815resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1816 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001817{
1818 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001819 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001820 struct shell_surface *shsurf = resize->base.shsurf;
1821 int32_t width, height;
1822 wl_fixed_t from_x, from_y;
1823 wl_fixed_t to_x, to_y;
1824
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001825 weston_pointer_move(pointer, x, y);
1826
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001827 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001828 return;
1829
Jason Ekstranda7af7042013-10-12 22:38:11 -05001830 weston_view_from_global_fixed(shsurf->view,
1831 pointer->grab_x, pointer->grab_y,
1832 &from_x, &from_y);
1833 weston_view_from_global_fixed(shsurf->view,
1834 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001835
1836 width = resize->width;
1837 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1838 width += wl_fixed_to_int(from_x - to_x);
1839 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1840 width += wl_fixed_to_int(to_x - from_x);
1841 }
1842
1843 height = resize->height;
1844 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1845 height += wl_fixed_to_int(from_y - to_y);
1846 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1847 height += wl_fixed_to_int(to_y - from_y);
1848 }
1849
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001850 if (width < 1)
1851 width = 1;
1852 if (height < 1)
1853 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001854 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001855}
1856
1857static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001858send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001859{
1860 struct shell_surface *shsurf = get_shell_surface(surface);
1861
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001862 assert(shsurf);
1863
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001864 if (shsurf->resource)
1865 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001866 shsurf->resize_edges,
1867 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001868}
1869
1870static const struct weston_shell_client shell_client = {
1871 send_configure
1872};
1873
1874static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001875resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001876 uint32_t time, uint32_t button, uint32_t state_w)
1877{
1878 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001879 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001880 enum wl_pointer_button_state state = state_w;
1881
1882 if (pointer->button_count == 0 &&
1883 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1884 shell_grab_end(&resize->base);
1885 free(grab);
1886 }
1887}
1888
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001889static void
1890resize_grab_cancel(struct weston_pointer_grab *grab)
1891{
1892 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1893
1894 shell_grab_end(&resize->base);
1895 free(grab);
1896}
1897
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001898static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001899 noop_grab_focus,
1900 resize_grab_motion,
1901 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001902 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001903};
1904
Giulio Camuffob8366642013-04-25 13:57:46 +03001905/*
1906 * Returns the bounding box of a surface and all its sub-surfaces,
1907 * in the surface coordinates system. */
1908static void
1909surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1910 int32_t *y, int32_t *w, int32_t *h) {
1911 pixman_region32_t region;
1912 pixman_box32_t *box;
1913 struct weston_subsurface *subsurface;
1914
1915 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916 surface->width,
1917 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001918
1919 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1920 pixman_region32_union_rect(&region, &region,
1921 subsurface->position.x,
1922 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001923 subsurface->surface->width,
1924 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001925 }
1926
1927 box = pixman_region32_extents(&region);
1928 if (x)
1929 *x = box->x1;
1930 if (y)
1931 *y = box->y1;
1932 if (w)
1933 *w = box->x2 - box->x1;
1934 if (h)
1935 *h = box->y2 - box->y1;
1936
1937 pixman_region32_fini(&region);
1938}
1939
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001940static int
1941surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001942 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001943{
1944 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001945 const unsigned resize_topbottom =
1946 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1947 const unsigned resize_leftright =
1948 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1949 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001950
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001951 if (shsurf->grabbed ||
1952 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001953 return 0;
1954
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001955 /* Check for invalid edge combinations. */
1956 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1957 (edges & resize_topbottom) == resize_topbottom ||
1958 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001959 return 0;
1960
1961 resize = malloc(sizeof *resize);
1962 if (!resize)
1963 return -1;
1964
1965 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001966
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001967 resize->width = shsurf->geometry.width;
1968 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001969
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001970 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001971 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001972 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001973 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001974
1975 return 0;
1976}
1977
1978static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001979common_surface_resize(struct wl_resource *resource,
1980 struct wl_resource *seat_resource, uint32_t serial,
1981 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001982{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001983 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001984 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001985 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001986
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001987 if (seat->pointer == NULL ||
1988 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001989 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001990 seat->pointer->focus == NULL)
1991 return;
1992
1993 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1994 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001995 return;
1996
Kristian Høgsberge3148752013-05-06 23:19:49 -04001997 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001998 wl_resource_post_no_memory(resource);
1999}
2000
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002001static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002002shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
2003 struct wl_resource *seat_resource, uint32_t serial,
2004 uint32_t edges)
2005{
2006 common_surface_resize(resource, seat_resource, serial, edges);
2007}
2008
2009static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002010busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002011{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002012 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002013 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002014 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002015 wl_fixed_t sx, sy;
2016
Jason Ekstranda7af7042013-10-12 22:38:11 -05002017 view = weston_compositor_pick_view(pointer->seat->compositor,
2018 pointer->x, pointer->y,
2019 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002020
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002021 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2022 shell_grab_end(grab);
2023 free(grab);
2024 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002025}
2026
2027static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002028busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2029 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002030{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002031 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002032}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002033
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002034static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002035busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002036 uint32_t time, uint32_t button, uint32_t state)
2037{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002038 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002039 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002040 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002041
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002042 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002043 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002044 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002045 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002046 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002047 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002048 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002049}
2050
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002051static void
2052busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2053{
2054 struct shell_grab *grab = (struct shell_grab *) base;
2055
2056 shell_grab_end(grab);
2057 free(grab);
2058}
2059
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002060static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002061 busy_cursor_grab_focus,
2062 busy_cursor_grab_motion,
2063 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002064 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002065};
2066
2067static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002068set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002069{
2070 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002071
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002072 if (pointer->grab->interface == &busy_cursor_grab_interface)
2073 return;
2074
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002075 grab = malloc(sizeof *grab);
2076 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002077 return;
2078
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002079 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2080 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002081 /* Mark the shsurf as ungrabbed so that button binding is able
2082 * to move it. */
2083 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002084}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002085
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002086static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002087end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002088{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002089 struct shell_grab *grab;
2090 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002091
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002092 wl_list_for_each(seat, &compositor->seat_list, link) {
2093 if (seat->pointer == NULL)
2094 continue;
2095
2096 grab = (struct shell_grab *) seat->pointer->grab;
2097 if (grab->grab.interface == &busy_cursor_grab_interface &&
2098 wl_resource_get_client(grab->shsurf->resource) == client) {
2099 shell_grab_end(grab);
2100 free(grab);
2101 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002102 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002103}
2104
Scott Moreau9521d5e2012-04-19 13:06:17 -06002105static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002106handle_shell_client_destroy(struct wl_listener *listener, void *data);
2107
2108static int
2109xdg_ping_timeout_handler(void *data)
2110{
2111 struct shell_client *sc = data;
2112 struct weston_seat *seat;
2113 struct shell_surface *shsurf;
2114
2115 /* Client is not responding */
2116 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002117 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2118 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2119 continue;
2120 if (seat->pointer->focus->surface->resource == NULL)
2121 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002122
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002123 shsurf = get_shell_surface(seat->pointer->focus->surface);
2124 if (shsurf &&
2125 wl_resource_get_client(shsurf->resource) == sc->client)
2126 set_busy_cursor(shsurf, seat->pointer);
2127 }
2128
2129 return 1;
2130}
2131
2132static void
2133handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2134{
2135 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002136 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002137 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002138 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002139
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002140 if (sc->unresponsive) {
2141 xdg_ping_timeout_handler(sc);
2142 return;
2143 }
2144
2145 sc->ping_serial = serial;
2146 loop = wl_display_get_event_loop(compositor->wl_display);
2147 if (sc->ping_timer == NULL)
2148 sc->ping_timer =
2149 wl_event_loop_add_timer(loop,
2150 xdg_ping_timeout_handler, sc);
2151 if (sc->ping_timer == NULL)
2152 return;
2153
2154 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2155
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002156 if (shell_surface_is_xdg_surface(shsurf) ||
2157 shell_surface_is_xdg_popup(shsurf))
2158 xdg_shell_send_ping(sc->resource, serial);
2159 else if (shell_surface_is_wl_shell_surface(shsurf))
2160 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002161}
2162
Scott Moreauff1db4a2012-04-17 19:06:18 -06002163static void
2164ping_handler(struct weston_surface *surface, uint32_t serial)
2165{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002166 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002167
2168 if (!shsurf)
2169 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002170 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002171 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002172 if (shsurf->surface == shsurf->shell->grab_surface)
2173 return;
2174
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002175 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002176}
2177
2178static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002179handle_pointer_focus(struct wl_listener *listener, void *data)
2180{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002181 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002183 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002184 uint32_t serial;
2185
Jason Ekstranda7af7042013-10-12 22:38:11 -05002186 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002187 return;
2188
Jason Ekstranda7af7042013-10-12 22:38:11 -05002189 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002190 serial = wl_display_next_serial(compositor->wl_display);
2191 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002192}
2193
2194static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002195shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2196{
2197 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002198 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002199}
2200
2201static void
2202shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2203{
2204 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002205 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002206}
2207
2208static void
2209handle_keyboard_focus(struct wl_listener *listener, void *data)
2210{
2211 struct weston_keyboard *keyboard = data;
2212 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2213
2214 if (seat->focused_surface) {
2215 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2216 if (shsurf)
2217 shell_surface_lose_keyboard_focus(shsurf);
2218 }
2219
2220 seat->focused_surface = keyboard->focus;
2221
2222 if (seat->focused_surface) {
2223 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2224 if (shsurf)
2225 shell_surface_gain_keyboard_focus(shsurf);
2226 }
2227}
2228
2229static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002230shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002231{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002232 if (sc->ping_serial != serial)
2233 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002234
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002235 sc->unresponsive = 0;
2236 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002237
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002238 if (sc->ping_timer) {
2239 wl_event_source_remove(sc->ping_timer);
2240 sc->ping_timer = NULL;
2241 }
2242
2243}
2244
2245static void
2246shell_surface_pong(struct wl_client *client,
2247 struct wl_resource *resource, uint32_t serial)
2248{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002249 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2250 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002251
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002252 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002253}
2254
2255static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002256set_title(struct shell_surface *shsurf, const char *title)
2257{
2258 free(shsurf->title);
2259 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002260 shsurf->surface->timeline.force_refresh = 1;
2261}
2262
2263static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002264set_pid(struct shell_surface *shsurf, pid_t pid)
2265{
2266 /* We have no use for it */
2267}
2268
2269static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002270set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2271{
2272 shsurf->type = t;
2273 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002274}
2275
2276static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002277set_window_geometry(struct shell_surface *shsurf,
2278 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002279{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002280 shsurf->next_geometry.x = x;
2281 shsurf->next_geometry.y = y;
2282 shsurf->next_geometry.width = width;
2283 shsurf->next_geometry.height = height;
2284 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002285}
2286
2287static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002288shell_surface_set_title(struct wl_client *client,
2289 struct wl_resource *resource, const char *title)
2290{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002291 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002292
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002293 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002294}
2295
2296static void
2297shell_surface_set_class(struct wl_client *client,
2298 struct wl_resource *resource, const char *class)
2299{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002300 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002301
2302 free(shsurf->class);
2303 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002304 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002305}
2306
Alex Wu4539b082012-03-01 12:57:46 +08002307static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002308restore_output_mode(struct weston_output *output)
2309{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002310 if (output->original_mode)
2311 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002312}
2313
2314static void
2315restore_all_output_modes(struct weston_compositor *compositor)
2316{
2317 struct weston_output *output;
2318
2319 wl_list_for_each(output, &compositor->output_list, link)
2320 restore_output_mode(output);
2321}
2322
Philip Withnall07926d92013-11-25 18:01:40 +00002323/* The surface will be inserted into the list immediately after the link
2324 * returned by this function (i.e. will be stacked immediately above the
2325 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002326static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002327shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2328{
2329 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002330 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002331
2332 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002333 case SHELL_SURFACE_XWAYLAND:
2334 return &shsurf->shell->fullscreen_layer.view_list;
2335
2336 case SHELL_SURFACE_NONE:
2337 return NULL;
2338
Kristian Høgsbergead52422014-01-01 13:51:52 -08002339 case SHELL_SURFACE_POPUP:
2340 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002341 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002342 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002343 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002344 /* Move the surface to its parent layer so
2345 * that surfaces which are transient for
2346 * fullscreen surfaces don't get hidden by the
2347 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002348
2349 /* TODO: Handle a parent with multiple views */
2350 parent = get_default_view(shsurf->parent);
2351 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002352 return container_of(parent->layer_link.link.prev,
2353 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002354 }
Philip Withnall07926d92013-11-25 18:01:40 +00002355
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002356 /* Move the surface to a normal workspace layer so that surfaces
2357 * which were previously fullscreen or transient are no longer
2358 * rendered on top. */
2359 ws = get_current_workspace(shsurf->shell);
2360 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002361 }
2362
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002363 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002364}
2365
Philip Withnall648a4dd2013-11-25 18:01:44 +00002366static void
2367shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2368{
2369 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002370 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002371
2372 /* Move the child layers to the same workspace as shsurf. They will be
2373 * stacked above shsurf. */
2374 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002375 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002376 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002377 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002378 prev = container_of(shsurf->view->layer_link.link.prev,
2379 struct weston_layer_entry, link);
2380 weston_layer_entry_remove(&child->view->layer_link);
2381 weston_layer_entry_insert(prev,
2382 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002383 weston_view_geometry_dirty(child->view);
2384 weston_surface_damage(child->surface);
2385
2386 /* Recurse. We don’t expect this to recurse very far (if
2387 * at all) because that would imply we have transient
2388 * (or popup) children of transient surfaces, which
2389 * would be unusual. */
2390 shell_surface_update_child_surface_layers(child);
2391 }
2392 }
2393}
2394
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002395/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002396 * geometry to ensure the changes are redrawn.
2397 *
2398 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2399 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002400static void
2401shell_surface_update_layer(struct shell_surface *shsurf)
2402{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002403 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002404
2405 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2406
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002407 if (new_layer_link == NULL)
2408 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002409 if (new_layer_link == &shsurf->view->layer_link)
2410 return;
2411
2412 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002413 weston_layer_entry_remove(&shsurf->view->layer_link);
2414 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002415 weston_view_geometry_dirty(shsurf->view);
2416 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002417
2418 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002419}
2420
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002421static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002422shell_surface_set_parent(struct shell_surface *shsurf,
2423 struct weston_surface *parent)
2424{
2425 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002426
2427 wl_list_remove(&shsurf->children_link);
2428 wl_list_init(&shsurf->children_link);
2429
2430 /* Insert into the parent surface’s child list. */
2431 if (parent != NULL) {
2432 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2433 if (parent_shsurf != NULL)
2434 wl_list_insert(&parent_shsurf->children_list,
2435 &shsurf->children_link);
2436 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002437}
2438
2439static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002440shell_surface_set_output(struct shell_surface *shsurf,
2441 struct weston_output *output)
2442{
2443 struct weston_surface *es = shsurf->surface;
2444
2445 /* get the default output, if the client set it as NULL
2446 check whether the ouput is available */
2447 if (output)
2448 shsurf->output = output;
2449 else if (es->output)
2450 shsurf->output = es->output;
2451 else
2452 shsurf->output = get_default_output(es->compositor);
2453}
2454
2455static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002456surface_clear_next_states(struct shell_surface *shsurf)
2457{
2458 shsurf->next_state.maximized = false;
2459 shsurf->next_state.fullscreen = false;
2460
2461 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2462 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2463 shsurf->state_changed = true;
2464}
2465
2466static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002467set_toplevel(struct shell_surface *shsurf)
2468{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002469 shell_surface_set_parent(shsurf, NULL);
2470 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002471 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002472
2473 /* The layer_link is updated in set_surface_type(),
2474 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002475}
2476
2477static void
2478shell_surface_set_toplevel(struct wl_client *client,
2479 struct wl_resource *resource)
2480{
2481 struct shell_surface *surface = wl_resource_get_user_data(resource);
2482
2483 set_toplevel(surface);
2484}
2485
2486static void
2487set_transient(struct shell_surface *shsurf,
2488 struct weston_surface *parent, int x, int y, uint32_t flags)
2489{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002490 assert(parent != NULL);
2491
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002492 shell_surface_set_parent(shsurf, parent);
2493
2494 surface_clear_next_states(shsurf);
2495
Philip Withnallbecb77e2013-11-25 18:01:30 +00002496 shsurf->transient.x = x;
2497 shsurf->transient.y = y;
2498 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002499
Rafael Antognollied207b42013-12-03 15:35:43 -02002500 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002501 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002502 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002503
2504 /* The layer_link is updated in set_surface_type(),
2505 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002506}
2507
2508static void
2509shell_surface_set_transient(struct wl_client *client,
2510 struct wl_resource *resource,
2511 struct wl_resource *parent_resource,
2512 int x, int y, uint32_t flags)
2513{
2514 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2515 struct weston_surface *parent =
2516 wl_resource_get_user_data(parent_resource);
2517
2518 set_transient(shsurf, parent, x, y, flags);
2519}
2520
2521static void
2522set_fullscreen(struct shell_surface *shsurf,
2523 uint32_t method,
2524 uint32_t framerate,
2525 struct weston_output *output)
2526{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002527 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002528 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002529
2530 shsurf->fullscreen_output = shsurf->output;
2531 shsurf->fullscreen.type = method;
2532 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002533
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002534 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002535}
2536
2537static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002538weston_view_set_initial_position(struct weston_view *view,
2539 struct desktop_shell *shell);
2540
2541static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002542unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002543{
Philip Withnallf85fe842013-11-25 18:01:37 +00002544 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002545 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2546 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002547 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002548 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002549
Alex Wu4539b082012-03-01 12:57:46 +08002550 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2551 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002552
Alex Wu4539b082012-03-01 12:57:46 +08002553 wl_list_remove(&shsurf->fullscreen.transform.link);
2554 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002555
Jason Ekstranda7af7042013-10-12 22:38:11 -05002556 if (shsurf->fullscreen.black_view)
2557 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2558 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002559
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002560 if (shsurf->saved_position_valid)
2561 weston_view_set_position(shsurf->view,
2562 shsurf->saved_x, shsurf->saved_y);
2563 else
2564 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2565
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002566 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002567 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002568 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002569 shsurf->saved_rotation_valid = false;
2570 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002571
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002572 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002573}
2574
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002575static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002576shell_surface_set_fullscreen(struct wl_client *client,
2577 struct wl_resource *resource,
2578 uint32_t method,
2579 uint32_t framerate,
2580 struct wl_resource *output_resource)
2581{
2582 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2583 struct weston_output *output;
2584
2585 if (output_resource)
2586 output = wl_resource_get_user_data(output_resource);
2587 else
2588 output = NULL;
2589
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002590 shell_surface_set_parent(shsurf, NULL);
2591
Rafael Antognolli03b16592013-12-03 15:35:42 -02002592 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002593 shsurf->next_state.fullscreen = true;
2594 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002595 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002596}
2597
2598static void
2599set_popup(struct shell_surface *shsurf,
2600 struct weston_surface *parent,
2601 struct weston_seat *seat,
2602 uint32_t serial,
2603 int32_t x,
2604 int32_t y)
2605{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002606 assert(parent != NULL);
2607
Philip Withnallbecb77e2013-11-25 18:01:30 +00002608 shsurf->popup.shseat = get_shell_seat(seat);
2609 shsurf->popup.serial = serial;
2610 shsurf->popup.x = x;
2611 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002612
Pekka Paalanenb5026542014-11-12 15:09:24 +02002613 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002614}
2615
2616static void
2617shell_surface_set_popup(struct wl_client *client,
2618 struct wl_resource *resource,
2619 struct wl_resource *seat_resource,
2620 uint32_t serial,
2621 struct wl_resource *parent_resource,
2622 int32_t x, int32_t y, uint32_t flags)
2623{
2624 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002625 struct weston_surface *parent =
2626 wl_resource_get_user_data(parent_resource);
2627
2628 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002629
Rafael Antognolli03b16592013-12-03 15:35:42 -02002630 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002631 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002632 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002633 wl_resource_get_user_data(seat_resource),
2634 serial, x, y);
2635}
2636
2637static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002638unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002639{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002640 /* undo all maximized things here */
2641 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002642
2643 if (shsurf->saved_position_valid)
2644 weston_view_set_position(shsurf->view,
2645 shsurf->saved_x, shsurf->saved_y);
2646 else
2647 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002648
2649 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002650 wl_list_insert(&shsurf->view->geometry.transformation_list,
2651 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002652 shsurf->saved_rotation_valid = false;
2653 }
2654
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002655 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002656}
2657
Philip Withnallbecb77e2013-11-25 18:01:30 +00002658static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002659set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002660{
2661 struct shell_surface *shsurf;
2662 struct workspace *current_ws;
2663 struct weston_seat *seat;
2664 struct weston_surface *focus;
2665 struct weston_view *view;
2666
2667 view = get_default_view(surface);
2668 if (!view)
2669 return;
2670
2671 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2672
2673 shsurf = get_shell_surface(surface);
2674 current_ws = get_current_workspace(shsurf->shell);
2675
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002676 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002677 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002678
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002679 drop_focus_state(shsurf->shell, current_ws, view->surface);
2680 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2681 if (!seat->keyboard)
2682 continue;
2683 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2684 if (focus == view->surface)
2685 weston_keyboard_set_focus(seat->keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002686 }
2687
2688 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002689 weston_view_damage_below(view);
2690}
2691
2692static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002693shell_surface_set_maximized(struct wl_client *client,
2694 struct wl_resource *resource,
2695 struct wl_resource *output_resource)
2696{
2697 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2698 struct weston_output *output;
2699
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002700 surface_clear_next_states(shsurf);
2701 shsurf->next_state.maximized = true;
2702 shsurf->state_changed = true;
2703
Pekka Paalanenb5026542014-11-12 15:09:24 +02002704 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002705 shell_surface_set_parent(shsurf, NULL);
2706
Philip Withnallbecb77e2013-11-25 18:01:30 +00002707 if (output_resource)
2708 output = wl_resource_get_user_data(output_resource);
2709 else
2710 output = NULL;
2711
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002712 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002713
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002714 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002715}
2716
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002717/* This is only ever called from set_surface_type(), so there’s no need to
2718 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002719static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002720reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002721{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002722 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002723 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002724 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002725 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002726
Pekka Paalanen98262232011-12-01 10:42:22 +02002727 return 0;
2728}
2729
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002730static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002731set_full_output(struct shell_surface *shsurf)
2732{
2733 shsurf->saved_x = shsurf->view->geometry.x;
2734 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002735 shsurf->saved_width = shsurf->surface->width;
2736 shsurf->saved_height = shsurf->surface->height;
2737 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002738 shsurf->saved_position_valid = true;
2739
2740 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2741 wl_list_remove(&shsurf->rotation.transform.link);
2742 wl_list_init(&shsurf->rotation.transform.link);
2743 weston_view_geometry_dirty(shsurf->view);
2744 shsurf->saved_rotation_valid = true;
2745 }
2746}
2747
2748static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002749set_surface_type(struct shell_surface *shsurf)
2750{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002751 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002752 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002753
Philip Withnallbecb77e2013-11-25 18:01:30 +00002754 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002755
Rafael Antognolli03b16592013-12-03 15:35:42 -02002756 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002757 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002758
2759 switch (shsurf->type) {
2760 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002761 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002762 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002763 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002764 weston_view_set_position(shsurf->view,
2765 pev->geometry.x + shsurf->transient.x,
2766 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002767 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002768 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002769
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002770 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002771 weston_view_set_position(shsurf->view, shsurf->transient.x,
2772 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002773 break;
2774
Philip Withnall0f640e22013-11-25 18:01:31 +00002775 case SHELL_SURFACE_POPUP:
2776 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002777 default:
2778 break;
2779 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002780
2781 /* Update the surface’s layer. */
2782 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002783}
2784
Tiago Vignattibe143262012-04-16 17:31:41 +03002785static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002786shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002787{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002788 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002789}
2790
Pekka Paalanen8274d902014-08-06 19:36:51 +03002791static int
2792black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2793{
2794 struct weston_surface *fs_surface = surface->configure_private;
2795 int n;
2796 int rem;
2797 int ret;
2798
2799 n = snprintf(buf, len, "black background surface for ");
2800 if (n < 0)
2801 return n;
2802
2803 rem = (int)len - n;
2804 if (rem < 0)
2805 rem = 0;
2806
2807 if (fs_surface->get_label)
2808 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2809 else
2810 ret = snprintf(buf + n, rem, "<unknown>");
2811
2812 if (ret < 0)
2813 return n;
2814
2815 return n + ret;
2816}
2817
Alex Wu21858432012-04-01 20:13:08 +08002818static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002819black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002820
Jason Ekstranda7af7042013-10-12 22:38:11 -05002821static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002822create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002823 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002824 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002825{
2826 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002828
2829 surface = weston_surface_create(ec);
2830 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002831 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002832 return NULL;
2833 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002834 view = weston_view_create(surface);
2835 if (surface == NULL) {
2836 weston_log("no memory\n");
2837 weston_surface_destroy(surface);
2838 return NULL;
2839 }
Alex Wu4539b082012-03-01 12:57:46 +08002840
Alex Wu21858432012-04-01 20:13:08 +08002841 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002842 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002843 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002844 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002845 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002846 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002847 pixman_region32_fini(&surface->input);
2848 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002849
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002850 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002851 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002852
2853 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002854}
2855
Philip Withnalled8f1a92013-11-25 18:01:43 +00002856static void
2857shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2858{
2859 struct weston_output *output = shsurf->fullscreen_output;
2860
Rafael Antognolli03b16592013-12-03 15:35:42 -02002861 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002862
2863 if (!shsurf->fullscreen.black_view)
2864 shsurf->fullscreen.black_view =
2865 create_black_surface(shsurf->surface->compositor,
2866 shsurf->surface,
2867 output->x, output->y,
2868 output->width,
2869 output->height);
2870
2871 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002872 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2873 weston_layer_entry_insert(&shsurf->view->layer_link,
2874 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002875 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2876 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002877
2878 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002879}
2880
Alex Wu4539b082012-03-01 12:57:46 +08002881/* Create black surface and append it to the associated fullscreen surface.
2882 * Handle size dismatch and positioning according to the method. */
2883static void
2884shell_configure_fullscreen(struct shell_surface *shsurf)
2885{
2886 struct weston_output *output = shsurf->fullscreen_output;
2887 struct weston_surface *surface = shsurf->surface;
2888 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002889 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002890 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002891
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002892 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2893 restore_output_mode(output);
2894
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002895 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002896 weston_layer_entry_remove(&shsurf->view->layer_link);
2897 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002898
Philip Withnalled8f1a92013-11-25 18:01:43 +00002899 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002900
Jason Ekstranda7af7042013-10-12 22:38:11 -05002901 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002902 &surf_width, &surf_height);
2903
Alex Wu4539b082012-03-01 12:57:46 +08002904 switch (shsurf->fullscreen.type) {
2905 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002906 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002907 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002908 break;
2909 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002910 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002911 if (output->width == surf_width &&
2912 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002913 weston_view_set_position(shsurf->view,
2914 output->x - surf_x,
2915 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002916 break;
2917 }
2918
Alex Wu4539b082012-03-01 12:57:46 +08002919 matrix = &shsurf->fullscreen.transform.matrix;
2920 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002921
Scott Moreau1bad5db2012-08-18 01:04:05 -06002922 output_aspect = (float) output->width /
2923 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002924 /* XXX: Use surf_width and surf_height here? */
2925 surface_aspect = (float) surface->width /
2926 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002927 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002928 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002929 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002930 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002931 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002932 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002933
Alex Wu4539b082012-03-01 12:57:46 +08002934 weston_matrix_scale(matrix, scale, scale, 1);
2935 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002936 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002937 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002938 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2939 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002941
Alex Wu4539b082012-03-01 12:57:46 +08002942 break;
2943 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002944 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002945 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002946 surf_width * surface->buffer_viewport.buffer.scale,
2947 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002948 shsurf->fullscreen.framerate};
2949
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002950 if (weston_output_mode_switch_to_temporary(output, &mode,
2951 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 weston_view_set_position(shsurf->view,
2953 output->x - surf_x,
2954 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002955 shsurf->fullscreen.black_view->surface->width = output->width;
2956 shsurf->fullscreen.black_view->surface->height = output->height;
2957 weston_view_set_position(shsurf->fullscreen.black_view,
2958 output->x - surf_x,
2959 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002960 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002961 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002962 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002963 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002964 }
Alex Wubd3354b2012-04-17 17:20:49 +08002965 }
Alex Wu4539b082012-03-01 12:57:46 +08002966 break;
2967 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002968 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002969 break;
2970 default:
2971 break;
2972 }
2973}
2974
Alex Wu4539b082012-03-01 12:57:46 +08002975static void
2976shell_map_fullscreen(struct shell_surface *shsurf)
2977{
Alex Wubd3354b2012-04-17 17:20:49 +08002978 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002979}
2980
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002981static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002982set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2983{
2984 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002985 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002986 shsurf->transient.x = x;
2987 shsurf->transient.y = y;
2988 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002989
2990 shell_surface_set_parent(shsurf, NULL);
2991
Pekka Paalanenb5026542014-11-12 15:09:24 +02002992 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002993 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002994}
2995
Kristian Høgsberg47792412014-05-04 13:47:06 -07002996static void
2997shell_interface_set_fullscreen(struct shell_surface *shsurf,
2998 uint32_t method,
2999 uint32_t framerate,
3000 struct weston_output *output)
3001{
3002 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003003 shsurf->next_state.fullscreen = true;
3004 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003005
3006 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003007}
3008
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003009static struct weston_output *
3010get_focused_output(struct weston_compositor *compositor)
3011{
3012 struct weston_seat *seat;
3013 struct weston_output *output = NULL;
3014
3015 wl_list_for_each(seat, &compositor->seat_list, link) {
3016 /* Priority has touch focus, then pointer and
3017 * then keyboard focus. We should probably have
3018 * three for loops and check frist for touch,
3019 * then for pointer, etc. but unless somebody has some
3020 * objections, I think this is sufficient. */
3021 if (seat->touch && seat->touch->focus)
3022 output = seat->touch->focus->output;
3023 else if (seat->pointer && seat->pointer->focus)
3024 output = seat->pointer->focus->output;
3025 else if (seat->keyboard && seat->keyboard->focus)
3026 output = seat->keyboard->focus->output;
3027
3028 if (output)
3029 break;
3030 }
3031
3032 return output;
3033}
3034
3035static void
3036shell_interface_set_maximized(struct shell_surface *shsurf)
3037{
3038 struct weston_output *output;
3039
3040 surface_clear_next_states(shsurf);
3041 shsurf->next_state.maximized = true;
3042 shsurf->state_changed = true;
3043 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3044
3045 if (!weston_surface_is_mapped(shsurf->surface))
3046 output = get_focused_output(shsurf->surface->compositor);
3047 else
3048 output = shsurf->surface->output;
3049
3050 shell_surface_set_output(shsurf, output);
3051 send_configure_for_surface(shsurf);
3052}
3053
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003054static int
3055shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3056{
3057 return surface_move(shsurf, ws, 1);
3058}
3059
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003060static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003061
3062static void
3063destroy_shell_seat(struct wl_listener *listener, void *data)
3064{
3065 struct shell_seat *shseat =
3066 container_of(listener,
3067 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003068 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003069
3070 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003071 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003072 shseat->popup_grab.client = NULL;
3073
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003074 wl_list_for_each_safe(shsurf, next,
3075 &shseat->popup_grab.surfaces_list,
3076 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003077 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003078 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003079 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003080 }
3081
3082 wl_list_remove(&shseat->seat_destroy_listener.link);
3083 free(shseat);
3084}
3085
Jason Ekstrand024177c2014-04-21 19:42:58 -05003086static void
3087shell_seat_caps_changed(struct wl_listener *listener, void *data)
3088{
3089 struct shell_seat *seat;
3090
3091 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3092
3093 if (seat->seat->keyboard &&
3094 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3095 wl_signal_add(&seat->seat->keyboard->focus_signal,
3096 &seat->keyboard_focus_listener);
3097 } else if (!seat->seat->keyboard) {
3098 wl_list_init(&seat->keyboard_focus_listener.link);
3099 }
3100
3101 if (seat->seat->pointer &&
3102 wl_list_empty(&seat->pointer_focus_listener.link)) {
3103 wl_signal_add(&seat->seat->pointer->focus_signal,
3104 &seat->pointer_focus_listener);
3105 } else if (!seat->seat->pointer) {
3106 wl_list_init(&seat->pointer_focus_listener.link);
3107 }
3108}
3109
Giulio Camuffo5085a752013-03-25 21:42:45 +01003110static struct shell_seat *
3111create_shell_seat(struct weston_seat *seat)
3112{
3113 struct shell_seat *shseat;
3114
3115 shseat = calloc(1, sizeof *shseat);
3116 if (!shseat) {
3117 weston_log("no memory to allocate shell seat\n");
3118 return NULL;
3119 }
3120
3121 shseat->seat = seat;
3122 wl_list_init(&shseat->popup_grab.surfaces_list);
3123
3124 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3125 wl_signal_add(&seat->destroy_signal,
3126 &shseat->seat_destroy_listener);
3127
Jason Ekstrand024177c2014-04-21 19:42:58 -05003128 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3129 wl_list_init(&shseat->keyboard_focus_listener.link);
3130
3131 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3132 wl_list_init(&shseat->pointer_focus_listener.link);
3133
3134 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3135 wl_signal_add(&seat->updated_caps_signal,
3136 &shseat->caps_changed_listener);
3137 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3138
Giulio Camuffo5085a752013-03-25 21:42:45 +01003139 return shseat;
3140}
3141
3142static struct shell_seat *
3143get_shell_seat(struct weston_seat *seat)
3144{
3145 struct wl_listener *listener;
3146
3147 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003148 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003149
3150 return container_of(listener,
3151 struct shell_seat, seat_destroy_listener);
3152}
3153
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003154static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003155popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003156{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003157 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003158 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003159 struct shell_seat *shseat =
3160 container_of(grab, struct shell_seat, popup_grab.grab);
3161 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003162 wl_fixed_t sx, sy;
3163
Jason Ekstranda7af7042013-10-12 22:38:11 -05003164 view = weston_compositor_pick_view(pointer->seat->compositor,
3165 pointer->x, pointer->y,
3166 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003167
Jason Ekstranda7af7042013-10-12 22:38:11 -05003168 if (view && view->surface->resource &&
3169 wl_resource_get_client(view->surface->resource) == client) {
3170 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003171 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003172 weston_pointer_set_focus(pointer, NULL,
3173 wl_fixed_from_int(0),
3174 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003175 }
3176}
3177
3178static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003179popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3180 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003181{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003182 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003183 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003184 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003185
Jonas Ådahl61917c82014-08-11 22:44:02 +02003186 if (pointer->focus) {
3187 weston_view_from_global_fixed(pointer->focus, x, y,
3188 &pointer->sx, &pointer->sy);
3189 }
3190
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003191 weston_pointer_move(pointer, x, y);
3192
Neil Roberts96d790e2013-09-19 17:32:00 +01003193 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003194 weston_view_from_global_fixed(pointer->focus,
3195 pointer->x, pointer->y,
3196 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003197 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003198 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003199}
3200
3201static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003202popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003203 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003204{
3205 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003206 struct shell_seat *shseat =
3207 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003208 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003209 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003210 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003211 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003212
Neil Roberts96d790e2013-09-19 17:32:00 +01003213 resource_list = &grab->pointer->focus_resource_list;
3214 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003215 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003216 wl_resource_for_each(resource, resource_list) {
3217 wl_pointer_send_button(resource, serial,
3218 time, button, state);
3219 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003220 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003221 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003222 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003223 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003224 }
3225
Daniel Stone4dbadb12012-05-30 16:31:51 +01003226 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003227 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003228}
3229
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003230static void
3231popup_grab_cancel(struct weston_pointer_grab *grab)
3232{
3233 popup_grab_end(grab->pointer);
3234}
3235
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003236static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003237 popup_grab_focus,
3238 popup_grab_motion,
3239 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003240 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003241};
3242
3243static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003244touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3245 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3246{
3247 struct wl_resource *resource;
3248 struct shell_seat *shseat =
3249 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3250 struct wl_display *display = shseat->seat->compositor->wl_display;
3251 uint32_t serial;
3252 struct wl_list *resource_list;
3253
3254 resource_list = &grab->touch->focus_resource_list;
3255 if (!wl_list_empty(resource_list)) {
3256 serial = wl_display_get_serial(display);
3257 wl_resource_for_each(resource, resource_list) {
3258 wl_touch_send_down(resource, serial, time,
3259 grab->touch->focus->surface->resource,
3260 touch_id, sx, sy);
3261 }
3262 }
3263}
3264
3265static void
3266touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3267{
3268 struct wl_resource *resource;
3269 struct shell_seat *shseat =
3270 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3271 struct wl_display *display = shseat->seat->compositor->wl_display;
3272 uint32_t serial;
3273 struct wl_list *resource_list;
3274
3275 resource_list = &grab->touch->focus_resource_list;
3276 if (!wl_list_empty(resource_list)) {
3277 serial = wl_display_get_serial(display);
3278 wl_resource_for_each(resource, resource_list) {
3279 wl_touch_send_up(resource, serial, time, touch_id);
3280 }
3281 }
3282}
3283
3284static void
3285touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3286 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3287{
3288 struct wl_resource *resource;
3289 struct wl_list *resource_list;
3290
3291 resource_list = &grab->touch->focus_resource_list;
3292 if (!wl_list_empty(resource_list)) {
3293 wl_resource_for_each(resource, resource_list) {
3294 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3295 }
3296 }
3297}
3298
3299static void
3300touch_popup_grab_frame(struct weston_touch_grab *grab)
3301{
3302}
3303
3304static void
3305touch_popup_grab_cancel(struct weston_touch_grab *grab)
3306{
3307 touch_popup_grab_end(grab->touch);
3308}
3309
3310static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3311 touch_popup_grab_down,
3312 touch_popup_grab_up,
3313 touch_popup_grab_motion,
3314 touch_popup_grab_frame,
3315 touch_popup_grab_cancel,
3316};
3317
3318static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003319shell_surface_send_popup_done(struct shell_surface *shsurf)
3320{
3321 if (shell_surface_is_wl_shell_surface(shsurf))
3322 wl_shell_surface_send_popup_done(shsurf->resource);
3323 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003324 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003325}
3326
3327static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003328popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003329{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003330 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003331 struct shell_seat *shseat =
3332 container_of(grab, struct shell_seat, popup_grab.grab);
3333 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003334 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003335
3336 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003337 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003338 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003339 shseat->popup_grab.grab.interface = NULL;
3340 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003341 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003342 wl_list_for_each_safe(shsurf, next,
3343 &shseat->popup_grab.surfaces_list,
3344 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003345 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003346 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003347 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003348 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003349 wl_list_init(&shseat->popup_grab.surfaces_list);
3350 }
3351}
3352
3353static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003354touch_popup_grab_end(struct weston_touch *touch)
3355{
3356 struct weston_touch_grab *grab = touch->grab;
3357 struct shell_seat *shseat =
3358 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3359 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003360 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003361
3362 if (touch->grab->interface == &touch_popup_grab_interface) {
3363 weston_touch_end_grab(grab->touch);
3364 shseat->popup_grab.client = NULL;
3365 shseat->popup_grab.touch_grab.interface = NULL;
3366 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3367 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003368 wl_list_for_each_safe(shsurf, next,
3369 &shseat->popup_grab.surfaces_list,
3370 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003371 shell_surface_send_popup_done(shsurf);
3372 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003373 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003374 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003375 wl_list_init(&shseat->popup_grab.surfaces_list);
3376 }
3377}
3378
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003379static struct shell_surface *
3380get_top_popup(struct shell_seat *shseat)
3381{
3382 struct shell_surface *shsurf;
3383
3384 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3385 return NULL;
3386 } else {
3387 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3388 struct shell_surface,
3389 popup.grab_link);
3390 return shsurf;
3391 }
3392}
3393
3394static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003395add_popup_grab(struct shell_surface *shsurf,
3396 struct shell_seat *shseat,
3397 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003398{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003399 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003400 struct shell_surface *parent, *top_surface;
3401
3402 parent = get_shell_surface(shsurf->parent);
3403 top_surface = get_top_popup(shseat);
3404 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003405 (!parent ||
3406 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003407 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003408 wl_resource_post_error(shsurf->owner_resource,
3409 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003410 "xdg_popup was not created on the "
3411 "topmost popup");
3412 return -1;
3413 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003414
3415 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003416 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003417 shseat->popup_grab.client =
3418 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003419
3420 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003421 shseat->popup_grab.grab.interface =
3422 &popup_grab_interface;
3423
3424 /* We must make sure here that this popup was opened
3425 * after a mouse press, and not just by moving around
3426 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003427 if (shseat->seat->pointer->button_count > 0)
3428 shseat->popup_grab.initial_up = 0;
3429 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003430 shseat->popup_grab.touch_grab.interface =
3431 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003432 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003433
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003434 wl_list_insert(&shseat->popup_grab.surfaces_list,
3435 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003436
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003437 if (type == POINTER) {
3438 weston_pointer_start_grab(seat->pointer,
3439 &shseat->popup_grab.grab);
3440 } else if (type == TOUCH) {
3441 weston_touch_start_grab(seat->touch,
3442 &shseat->popup_grab.touch_grab);
3443 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003444 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003445 wl_list_insert(&shseat->popup_grab.surfaces_list,
3446 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003447 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003448
3449 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003450}
3451
3452static void
3453remove_popup_grab(struct shell_surface *shsurf)
3454{
3455 struct shell_seat *shseat = shsurf->popup.shseat;
3456
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003457 if (shell_surface_is_xdg_popup(shsurf) &&
3458 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003459 wl_resource_post_error(shsurf->owner_resource,
3460 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003461 "xdg_popup was destroyed while it was "
3462 "not the topmost popup.");
3463 return;
3464 }
3465
Giulio Camuffo5085a752013-03-25 21:42:45 +01003466 wl_list_remove(&shsurf->popup.grab_link);
3467 wl_list_init(&shsurf->popup.grab_link);
3468 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003469 if (shseat->popup_grab.type == POINTER) {
3470 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3471 shseat->popup_grab.grab.interface = NULL;
3472 } else if (shseat->popup_grab.type == TOUCH) {
3473 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3474 shseat->popup_grab.touch_grab.interface = NULL;
3475 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003476 }
3477}
3478
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003479static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003480shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003481{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003482 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003483 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003484
Jason Ekstranda7af7042013-10-12 22:38:11 -05003485 shsurf->surface->output = parent_view->output;
3486 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003487
Jason Ekstranda7af7042013-10-12 22:38:11 -05003488 weston_view_set_transform_parent(shsurf->view, parent_view);
3489 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3490 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003491
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003492 if (shseat->seat->pointer &&
3493 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003494 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3495 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003496 } else if (shseat->seat->touch &&
3497 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003498 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3499 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003500 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003501 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003502 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003503 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003504
3505 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003506}
3507
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003508static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003509 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003510 shell_surface_move,
3511 shell_surface_resize,
3512 shell_surface_set_toplevel,
3513 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003514 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003515 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003516 shell_surface_set_maximized,
3517 shell_surface_set_title,
3518 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003519};
3520
3521static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003522destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003523{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003524 struct shell_surface *child, *next;
3525
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003526 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3527
Giulio Camuffo5085a752013-03-25 21:42:45 +01003528 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3529 remove_popup_grab(shsurf);
3530 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003531
Alex Wubd3354b2012-04-17 17:20:49 +08003532 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003533 shell_surface_is_top_fullscreen(shsurf))
3534 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003535
Jason Ekstranda7af7042013-10-12 22:38:11 -05003536 if (shsurf->fullscreen.black_view)
3537 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003538
Alex Wubd3354b2012-04-17 17:20:49 +08003539 /* As destroy_resource() use wl_list_for_each_safe(),
3540 * we can always remove the listener.
3541 */
3542 wl_list_remove(&shsurf->surface_destroy_listener.link);
3543 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003544 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003545 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003546
Jason Ekstranda7af7042013-10-12 22:38:11 -05003547 weston_view_destroy(shsurf->view);
3548
Philip Withnall648a4dd2013-11-25 18:01:44 +00003549 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003550 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3551 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003552
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003553 wl_list_remove(&shsurf->link);
3554 free(shsurf);
3555}
3556
3557static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003558shell_destroy_shell_surface(struct wl_resource *resource)
3559{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003560 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003561
Bryan Caina46b9462014-04-04 17:41:24 -05003562 if (!wl_list_empty(&shsurf->popup.grab_link))
3563 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003564 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003565 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003566}
3567
3568static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003569shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003570{
3571 struct shell_surface *shsurf = container_of(listener,
3572 struct shell_surface,
3573 surface_destroy_listener);
3574
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003575 if (shsurf->resource)
3576 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003577
3578 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003579}
3580
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003581static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003582fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003583{
3584 struct shell_surface *shsurf = data;
3585
3586 weston_surface_destroy(shsurf->surface);
3587}
3588
3589static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003590fade_out_done(struct weston_view_animation *animation, void *data)
3591{
3592 struct shell_surface *shsurf = data;
3593 struct wl_event_loop *loop;
3594
3595 loop = wl_display_get_event_loop(
3596 shsurf->surface->compositor->wl_display);
3597
3598 if (!shsurf->destroying) {
3599 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3600 shsurf->destroying = true;
3601 }
3602}
3603
3604static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003605handle_resource_destroy(struct wl_listener *listener, void *data)
3606{
3607 struct shell_surface *shsurf =
3608 container_of(listener, struct shell_surface,
3609 resource_destroy_listener);
3610
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003611 if (!weston_surface_is_mapped(shsurf->surface))
3612 return;
3613
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003614 shsurf->surface->ref_count++;
3615
3616 pixman_region32_fini(&shsurf->surface->pending.input);
3617 pixman_region32_init(&shsurf->surface->pending.input);
3618 pixman_region32_fini(&shsurf->surface->input);
3619 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003620 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3621 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3622 fade_out_done, shsurf);
3623 } else {
3624 weston_surface_destroy(shsurf->surface);
3625 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003626}
3627
3628static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003629shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003630
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003631struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003632get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003633{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003634 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003635 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003636 else
3637 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003638}
3639
Rafael Antognollie2a34552013-12-03 15:35:45 -02003640static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003641create_common_surface(struct shell_client *owner, void *shell,
3642 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003643 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003644{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003645 struct shell_surface *shsurf;
3646
Pekka Paalanen50b67472014-10-01 15:02:41 +03003647 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003648
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003649 shsurf = calloc(1, sizeof *shsurf);
3650 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003651 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003652 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003653 }
3654
Jason Ekstranda7af7042013-10-12 22:38:11 -05003655 shsurf->view = weston_view_create(surface);
3656 if (!shsurf->view) {
3657 weston_log("no memory to allocate shell surface\n");
3658 free(shsurf);
3659 return NULL;
3660 }
3661
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003662 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003663 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003664 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003665
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003666 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3667 wl_resource_add_destroy_listener(surface->resource,
3668 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003669 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003670
Tiago Vignattibc052c92012-04-19 16:18:18 +03003671 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003672 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003673 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003674 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003675 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003676 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003677 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3678 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003679 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003680 wl_list_init(&shsurf->fullscreen.transform.link);
3681
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003682 shsurf->output = get_default_output(shsurf->shell->compositor);
3683
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003684 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003685 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003686 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003687 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003688
3689 /* init link so its safe to always remove it in destroy_shell_surface */
3690 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003691 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003692
Pekka Paalanen460099f2012-01-20 16:48:25 +02003693 /* empty when not in use */
3694 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003695 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003696
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003697 wl_list_init(&shsurf->workspace_transform.link);
3698
Philip Withnall648a4dd2013-11-25 18:01:44 +00003699 wl_list_init(&shsurf->children_link);
3700 wl_list_init(&shsurf->children_list);
3701 shsurf->parent = NULL;
3702
Pekka Paalanenb5026542014-11-12 15:09:24 +02003703 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003704
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003705 shsurf->client = client;
3706
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003707 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003708}
3709
Rafael Antognollie2a34552013-12-03 15:35:45 -02003710static struct shell_surface *
3711create_shell_surface(void *shell, struct weston_surface *surface,
3712 const struct weston_shell_client *client)
3713{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003714 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003715}
3716
Jason Ekstranda7af7042013-10-12 22:38:11 -05003717static struct weston_view *
3718get_primary_view(void *shell, struct shell_surface *shsurf)
3719{
3720 return shsurf->view;
3721}
3722
Tiago Vignattibc052c92012-04-19 16:18:18 +03003723static void
3724shell_get_shell_surface(struct wl_client *client,
3725 struct wl_resource *resource,
3726 uint32_t id,
3727 struct wl_resource *surface_resource)
3728{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003729 struct weston_surface *surface =
3730 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003731 struct shell_client *sc = wl_resource_get_user_data(resource);
3732 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003733 struct shell_surface *shsurf;
3734
Pekka Paalanen50b67472014-10-01 15:02:41 +03003735 if (weston_surface_set_role(surface, "wl_shell_surface",
3736 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003737 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003738
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003739 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003740 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003741 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003742 return;
3743 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003744
Jason Ekstranda85118c2013-06-27 20:17:02 -05003745 shsurf->resource =
3746 wl_resource_create(client,
3747 &wl_shell_surface_interface, 1, id);
3748 wl_resource_set_implementation(shsurf->resource,
3749 &shell_surface_implementation,
3750 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003751 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003752}
3753
Rafael Antognollie2a34552013-12-03 15:35:45 -02003754static bool
3755shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3756{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003757 /* A shell surface without a resource is created from xwayland
3758 * and is considered a wl_shell surface for now. */
3759
3760 return shsurf->resource == NULL ||
3761 wl_resource_instance_of(shsurf->resource,
3762 &wl_shell_surface_interface,
3763 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003764}
3765
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003766static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003767 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003768};
3769
Rafael Antognollie2a34552013-12-03 15:35:45 -02003770/****************************
3771 * xdg-shell implementation */
3772
3773static void
3774xdg_surface_destroy(struct wl_client *client,
3775 struct wl_resource *resource)
3776{
3777 wl_resource_destroy(resource);
3778}
3779
3780static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003781xdg_surface_set_parent(struct wl_client *client,
3782 struct wl_resource *resource,
3783 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003784{
3785 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003786 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003787
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003788 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003789 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003790 shell_surface_set_parent(shsurf, parent->surface);
3791 } else {
3792 shell_surface_set_parent(shsurf, NULL);
3793 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003794}
3795
3796static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003797xdg_surface_set_app_id(struct wl_client *client,
3798 struct wl_resource *resource,
3799 const char *app_id)
3800{
3801 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3802
3803 free(shsurf->class);
3804 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003805 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003806}
3807
3808static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003809xdg_surface_show_window_menu(struct wl_client *client,
3810 struct wl_resource *surface_resource,
3811 struct wl_resource *seat_resource,
3812 uint32_t serial,
3813 int32_t x,
3814 int32_t y)
3815{
3816 /* TODO */
3817}
3818
3819static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003820xdg_surface_set_title(struct wl_client *client,
3821 struct wl_resource *resource, const char *title)
3822{
3823 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3824
3825 set_title(shsurf, title);
3826}
3827
3828static void
3829xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3830 struct wl_resource *seat_resource, uint32_t serial)
3831{
3832 common_surface_move(resource, seat_resource, serial);
3833}
3834
3835static void
3836xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3837 struct wl_resource *seat_resource, uint32_t serial,
3838 uint32_t edges)
3839{
3840 common_surface_resize(resource, seat_resource, serial, edges);
3841}
3842
3843static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003844xdg_surface_ack_configure(struct wl_client *client,
3845 struct wl_resource *resource,
3846 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003847{
3848 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3849
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003850 if (shsurf->state_requested) {
3851 shsurf->next_state = shsurf->requested_state;
3852 shsurf->state_changed = true;
3853 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003854 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003855}
3856
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003857static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003858xdg_surface_set_window_geometry(struct wl_client *client,
3859 struct wl_resource *resource,
3860 int32_t x,
3861 int32_t y,
3862 int32_t width,
3863 int32_t height)
3864{
3865 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3866
3867 set_window_geometry(shsurf, x, y, width, height);
3868}
3869
3870static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003871xdg_surface_set_maximized(struct wl_client *client,
3872 struct wl_resource *resource)
3873{
3874 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003875 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003876
3877 shsurf->state_requested = true;
3878 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003879
3880 if (!weston_surface_is_mapped(shsurf->surface))
3881 output = get_focused_output(shsurf->surface->compositor);
3882 else
3883 output = shsurf->surface->output;
3884
3885 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003886 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003887}
3888
3889static void
3890xdg_surface_unset_maximized(struct wl_client *client,
3891 struct wl_resource *resource)
3892{
3893 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3894
3895 shsurf->state_requested = true;
3896 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003897 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003898}
3899
3900static void
3901xdg_surface_set_fullscreen(struct wl_client *client,
3902 struct wl_resource *resource,
3903 struct wl_resource *output_resource)
3904{
3905 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3906 struct weston_output *output;
3907
3908 shsurf->state_requested = true;
3909 shsurf->requested_state.fullscreen = true;
3910
3911 if (output_resource)
3912 output = wl_resource_get_user_data(output_resource);
3913 else
3914 output = NULL;
3915
Marek Chalupa052917a2014-09-02 11:35:12 +02003916 /* handle clients launching in fullscreen */
3917 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3918 /* Set the output to the one that has focus currently. */
3919 assert(shsurf->surface);
3920 output = get_focused_output(shsurf->surface->compositor);
3921 }
3922
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003923 shell_surface_set_output(shsurf, output);
3924 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003925
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003926 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003927}
3928
3929static void
3930xdg_surface_unset_fullscreen(struct wl_client *client,
3931 struct wl_resource *resource)
3932{
3933 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3934
3935 shsurf->state_requested = true;
3936 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003937 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003938}
3939
3940static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003941xdg_surface_set_minimized(struct wl_client *client,
3942 struct wl_resource *resource)
3943{
3944 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3945
3946 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3947 return;
3948
3949 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003950 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003951}
3952
Rafael Antognollie2a34552013-12-03 15:35:45 -02003953static const struct xdg_surface_interface xdg_surface_implementation = {
3954 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003955 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003956 xdg_surface_set_title,
3957 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003958 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003959 xdg_surface_move,
3960 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003961 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003962 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003963 xdg_surface_set_maximized,
3964 xdg_surface_unset_maximized,
3965 xdg_surface_set_fullscreen,
3966 xdg_surface_unset_fullscreen,
3967 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003968};
3969
3970static void
3971xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003972 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003973{
3974 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003975 uint32_t *s;
3976 struct wl_array states;
3977 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003978
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003979 assert(shsurf);
3980
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003981 if (!shsurf->resource)
3982 return;
3983
3984 wl_array_init(&states);
3985 if (shsurf->requested_state.fullscreen) {
3986 s = wl_array_add(&states, sizeof *s);
3987 *s = XDG_SURFACE_STATE_FULLSCREEN;
3988 } else if (shsurf->requested_state.maximized) {
3989 s = wl_array_add(&states, sizeof *s);
3990 *s = XDG_SURFACE_STATE_MAXIMIZED;
3991 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003992 if (shsurf->resize_edges != 0) {
3993 s = wl_array_add(&states, sizeof *s);
3994 *s = XDG_SURFACE_STATE_RESIZING;
3995 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003996 if (shsurf->focus_count > 0) {
3997 s = wl_array_add(&states, sizeof *s);
3998 *s = XDG_SURFACE_STATE_ACTIVATED;
3999 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004000
4001 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4002 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4003
4004 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004005}
4006
4007static const struct weston_shell_client xdg_client = {
4008 xdg_send_configure
4009};
4010
4011static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004012xdg_shell_destroy(struct wl_client *client,
4013 struct wl_resource *resource)
4014{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004015 struct shell_client *sc = wl_resource_get_user_data(resource);
4016 struct wl_resource *shsurf_resource;
4017 struct shell_surface *shsurf;
4018
4019 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4020 shsurf = wl_resource_get_user_data(shsurf_resource);
4021 if (shsurf->owner_resource == resource) {
4022 wl_resource_post_error(
4023 resource,
4024 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4025 "not all child surface objects destroyed");
4026 return;
4027 }
4028 }
4029
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004030 wl_resource_destroy(resource);
4031}
4032
4033static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004034xdg_use_unstable_version(struct wl_client *client,
4035 struct wl_resource *resource,
4036 int32_t version)
4037{
4038 if (version > 1) {
4039 wl_resource_post_error(resource,
4040 1,
4041 "xdg-shell:: version not implemented yet.");
4042 return;
4043 }
4044}
4045
4046static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004047create_xdg_surface(struct shell_client *owner, void *shell,
4048 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004049 const struct weston_shell_client *client)
4050{
4051 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004052
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004053 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004054 if (!shsurf)
4055 return NULL;
4056
Pekka Paalanenb5026542014-11-12 15:09:24 +02004057 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004058
4059 return shsurf;
4060}
4061
4062static void
4063xdg_get_xdg_surface(struct wl_client *client,
4064 struct wl_resource *resource,
4065 uint32_t id,
4066 struct wl_resource *surface_resource)
4067{
4068 struct weston_surface *surface =
4069 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004070 struct shell_client *sc = wl_resource_get_user_data(resource);
4071 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004072 struct shell_surface *shsurf;
4073
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004074 shsurf = get_shell_surface(surface);
4075 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4076 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4077 "This wl_surface is already an "
4078 "xdg_surface");
4079 return;
4080 }
4081
Pekka Paalanen50b67472014-10-01 15:02:41 +03004082 if (weston_surface_set_role(surface, "xdg_surface",
4083 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004084 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004085
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004086 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004087 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004088 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004089 return;
4090 }
4091
4092 shsurf->resource =
4093 wl_resource_create(client,
4094 &xdg_surface_interface, 1, id);
4095 wl_resource_set_implementation(shsurf->resource,
4096 &xdg_surface_implementation,
4097 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004098 shsurf->owner_resource = resource;
4099 wl_list_insert(&sc->surface_list,
4100 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004101}
4102
4103static bool
4104shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4105{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004106 return shsurf->resource &&
4107 wl_resource_instance_of(shsurf->resource,
4108 &xdg_surface_interface,
4109 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004110}
4111
4112/* xdg-popup implementation */
4113
4114static void
4115xdg_popup_destroy(struct wl_client *client,
4116 struct wl_resource *resource)
4117{
4118 wl_resource_destroy(resource);
4119}
4120
Rafael Antognollie2a34552013-12-03 15:35:45 -02004121static const struct xdg_popup_interface xdg_popup_implementation = {
4122 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004123};
4124
4125static void
4126xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004127 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004128{
4129}
4130
4131static const struct weston_shell_client xdg_popup_client = {
4132 xdg_popup_send_configure
4133};
4134
4135static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004136create_xdg_popup(struct shell_client *owner, void *shell,
4137 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004138 const struct weston_shell_client *client,
4139 struct weston_surface *parent,
4140 struct shell_seat *seat,
4141 uint32_t serial,
4142 int32_t x, int32_t y)
4143{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004144 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004145
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004146 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004147 if (!shsurf)
4148 return NULL;
4149
Pekka Paalanenb5026542014-11-12 15:09:24 +02004150 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004151 shsurf->popup.shseat = seat;
4152 shsurf->popup.serial = serial;
4153 shsurf->popup.x = x;
4154 shsurf->popup.y = y;
4155 shell_surface_set_parent(shsurf, parent);
4156
4157 return shsurf;
4158}
4159
4160static void
4161xdg_get_xdg_popup(struct wl_client *client,
4162 struct wl_resource *resource,
4163 uint32_t id,
4164 struct wl_resource *surface_resource,
4165 struct wl_resource *parent_resource,
4166 struct wl_resource *seat_resource,
4167 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004168 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004169{
4170 struct weston_surface *surface =
4171 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004172 struct shell_client *sc = wl_resource_get_user_data(resource);
4173 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004174 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004175 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004176 struct weston_surface *parent;
4177 struct shell_seat *seat;
4178
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004179 shsurf = get_shell_surface(surface);
4180 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4181 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4182 "This wl_surface is already an "
4183 "xdg_popup");
4184 return;
4185 }
4186
Pekka Paalanen50b67472014-10-01 15:02:41 +03004187 if (weston_surface_set_role(surface, "xdg_popup",
4188 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004189 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004190
4191 if (!parent_resource) {
4192 wl_resource_post_error(surface_resource,
4193 WL_DISPLAY_ERROR_INVALID_OBJECT,
4194 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004195 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004196 }
4197
4198 parent = wl_resource_get_user_data(parent_resource);
4199 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4200
Jonas Ådahlee45a552015-03-18 16:37:47 +08004201 /* Verify that we are creating the top most popup when mapping,
4202 * as it's not until then we know whether it was mapped as most
4203 * top level or not. */
4204
4205 parent_shsurf = get_shell_surface(parent);
4206 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4207 !shell_surface_is_xdg_surface(parent_shsurf)) {
4208 wl_resource_post_error(resource,
4209 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4210 "xdg_popup parent was invalid");
4211 return;
4212 }
4213
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004214 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004215 parent, seat, serial, x, y);
4216 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004217 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004218 return;
4219 }
4220
4221 shsurf->resource =
4222 wl_resource_create(client,
4223 &xdg_popup_interface, 1, id);
4224 wl_resource_set_implementation(shsurf->resource,
4225 &xdg_popup_implementation,
4226 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004227 shsurf->owner_resource = resource;
4228 wl_list_insert(&sc->surface_list,
4229 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004230}
4231
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004232static void
4233xdg_pong(struct wl_client *client,
4234 struct wl_resource *resource, uint32_t serial)
4235{
4236 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004237
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004238 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004239}
4240
Rafael Antognollie2a34552013-12-03 15:35:45 -02004241static bool
4242shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4243{
4244 return wl_resource_instance_of(shsurf->resource,
4245 &xdg_popup_interface,
4246 &xdg_popup_implementation);
4247}
4248
4249static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004250 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004251 xdg_use_unstable_version,
4252 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004253 xdg_get_xdg_popup,
4254 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004255};
4256
4257static int
4258xdg_shell_unversioned_dispatch(const void *implementation,
4259 void *_target, uint32_t opcode,
4260 const struct wl_message *message,
4261 union wl_argument *args)
4262{
4263 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004264 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004265
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004266 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004267 wl_resource_post_error(resource,
4268 WL_DISPLAY_ERROR_INVALID_OBJECT,
4269 "must call use_unstable_version first");
4270 return 0;
4271 }
4272
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004273#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004274
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004275 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4276 "shell implementation doesn't match protocol version");
4277
Rafael Antognollie2a34552013-12-03 15:35:45 -02004278 if (args[0].i != XDG_SERVER_VERSION) {
4279 wl_resource_post_error(resource,
4280 WL_DISPLAY_ERROR_INVALID_OBJECT,
4281 "incompatible version, server is %d "
4282 "client wants %d",
4283 XDG_SERVER_VERSION, args[0].i);
4284 return 0;
4285 }
4286
4287 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004288 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004289
4290 return 1;
4291}
4292
4293/* end of xdg-shell implementation */
4294/***********************************/
4295
Kristian Høgsberg07937562011-04-12 17:25:42 -04004296static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004297shell_fade(struct desktop_shell *shell, enum fade_type type);
4298
4299static int
4300screensaver_timeout(void *data)
4301{
4302 struct desktop_shell *shell = data;
4303
4304 shell_fade(shell, FADE_OUT);
4305
4306 return 1;
4307}
4308
4309static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004310handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02004311{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004312 struct desktop_shell *shell =
4313 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004314
Pekka Paalanen18027e52011-12-02 16:31:49 +02004315 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004316
4317 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02004318 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02004319}
4320
4321static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004322launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004323{
4324 if (shell->screensaver.binding)
4325 return;
4326
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004327 if (!shell->screensaver.path) {
4328 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004329 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004330 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004331
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004332 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02004333 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004334 return;
4335 }
4336
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004337 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004338 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004339 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004340 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004341}
4342
4343static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004344terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004345{
Pekka Paalanen18027e52011-12-02 16:31:49 +02004346 if (shell->screensaver.process.pid == 0)
4347 return;
4348
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03004349 /* Disarm the screensaver timer, otherwise it may fire when the
4350 * compositor is not in the idle state. In that case, the screen will
4351 * be locked, but the wake_signal won't fire on user input, making the
4352 * system unresponsive. */
4353 wl_event_source_timer_update(shell->screensaver.timer, 0);
4354
Pekka Paalanen18027e52011-12-02 16:31:49 +02004355 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004356}
4357
4358static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004359configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004360{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004361 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004362
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004363 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004364 if (v->output == ev->output && v != ev) {
4365 weston_view_unmap(v);
4366 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004367 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004368 }
4369 }
4370
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004371 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004372
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004373 if (wl_list_empty(&ev->layer_link.link)) {
4374 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004375 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004376 }
4377}
4378
Pekka Paalanen8274d902014-08-06 19:36:51 +03004379static int
4380background_get_label(struct weston_surface *surface, char *buf, size_t len)
4381{
4382 return snprintf(buf, len, "background for output %s",
4383 surface->output->name);
4384}
4385
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004386static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004387background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004388{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004389 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004390 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004391
Jason Ekstranda7af7042013-10-12 22:38:11 -05004392 view = container_of(es->views.next, struct weston_view, surface_link);
4393
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004394 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004395}
4396
4397static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004398desktop_shell_set_background(struct wl_client *client,
4399 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004400 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004401 struct wl_resource *surface_resource)
4402{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004403 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004404 struct weston_surface *surface =
4405 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004407
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004408 if (surface->configure) {
4409 wl_resource_post_error(surface_resource,
4410 WL_DISPLAY_ERROR_INVALID_OBJECT,
4411 "surface role already assigned");
4412 return;
4413 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004414
Jason Ekstranda7af7042013-10-12 22:38:11 -05004415 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4416 weston_view_destroy(view);
4417 view = weston_view_create(surface);
4418
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004419 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004420 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004421 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004422 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004423 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004424 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004425 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004426 surface->output->width,
4427 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004428}
4429
Pekka Paalanen8274d902014-08-06 19:36:51 +03004430static int
4431panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4432{
4433 return snprintf(buf, len, "panel for output %s",
4434 surface->output->name);
4435}
4436
Kristian Høgsberg75840622011-09-06 13:48:16 -04004437static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004438panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004439{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004440 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004441 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004442
Jason Ekstranda7af7042013-10-12 22:38:11 -05004443 view = container_of(es->views.next, struct weston_view, surface_link);
4444
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004445 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004446}
4447
4448static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004449desktop_shell_set_panel(struct wl_client *client,
4450 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004451 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004452 struct wl_resource *surface_resource)
4453{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004454 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004455 struct weston_surface *surface =
4456 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004457 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004458
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004459 if (surface->configure) {
4460 wl_resource_post_error(surface_resource,
4461 WL_DISPLAY_ERROR_INVALID_OBJECT,
4462 "surface role already assigned");
4463 return;
4464 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004465
Jason Ekstranda7af7042013-10-12 22:38:11 -05004466 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4467 weston_view_destroy(view);
4468 view = weston_view_create(surface);
4469
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004470 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004471 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004472 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004473 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004474 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004475 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004476 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004477 surface->output->width,
4478 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004479}
4480
Pekka Paalanen8274d902014-08-06 19:36:51 +03004481static int
4482lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4483{
4484 return snprintf(buf, len, "lock window");
4485}
4486
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004487static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004488lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004489{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004490 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004491 struct weston_view *view;
4492
4493 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004494
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004495 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004496 return;
4497
Jason Ekstranda7af7042013-10-12 22:38:11 -05004498 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004499
4500 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004501 weston_layer_entry_insert(&shell->lock_layer.view_list,
4502 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004503 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004504 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004505 }
4506}
4507
4508static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004509handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004510{
Tiago Vignattibe143262012-04-16 17:31:41 +03004511 struct desktop_shell *shell =
4512 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004513
Martin Minarik6d118362012-06-07 18:01:59 +02004514 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004515 shell->lock_surface = NULL;
4516}
4517
4518static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004519desktop_shell_set_lock_surface(struct wl_client *client,
4520 struct wl_resource *resource,
4521 struct wl_resource *surface_resource)
4522{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004523 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004524 struct weston_surface *surface =
4525 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004526
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004527 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004528
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004529 if (!shell->locked)
4530 return;
4531
Pekka Paalanen98262232011-12-01 10:42:22 +02004532 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004533
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004534 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004535 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004536 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004537
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004538 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004539 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004540 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004541 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004542}
4543
4544static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004545resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004546{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004547 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004548
Pekka Paalanen77346a62011-11-30 16:26:35 +02004549 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004550
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004551 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004552 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004553 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004554 &shell->input_panel_layer.link);
4555 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004556 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004557 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004558 wl_list_insert(&shell->compositor->cursor_layer.link,
4559 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004560 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004561 wl_list_insert(&shell->fullscreen_layer.link,
4562 &shell->panel_layer.link);
4563 wl_list_insert(&shell->panel_layer.link,
4564 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004565
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004566 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004567
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004568 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004569 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004570 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004571}
4572
4573static void
4574desktop_shell_unlock(struct wl_client *client,
4575 struct wl_resource *resource)
4576{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004577 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004578
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004579 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004580
4581 if (shell->locked)
4582 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004583}
4584
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004585static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004586desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004587 struct wl_resource *resource,
4588 struct wl_resource *surface_resource)
4589{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004590 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004591
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004592 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004593 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004594}
4595
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004596static void
4597desktop_shell_desktop_ready(struct wl_client *client,
4598 struct wl_resource *resource)
4599{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004600 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004601
4602 shell_fade_startup(shell);
4603}
4604
Jonny Lamb765760d2014-08-20 15:53:19 +02004605static void
4606desktop_shell_set_panel_position(struct wl_client *client,
4607 struct wl_resource *resource,
4608 uint32_t position)
4609{
4610 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4611
4612 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4613 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4614 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4615 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4616 wl_resource_post_error(resource,
4617 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4618 "bad position argument");
4619 return;
4620 }
4621
4622 shell->panel_position = position;
4623}
4624
Kristian Høgsberg75840622011-09-06 13:48:16 -04004625static const struct desktop_shell_interface desktop_shell_implementation = {
4626 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004627 desktop_shell_set_panel,
4628 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004629 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004630 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004631 desktop_shell_desktop_ready,
4632 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004633};
4634
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004635static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004636get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004637{
4638 struct shell_surface *shsurf;
4639
4640 shsurf = get_shell_surface(surface);
4641 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004642 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004643 return shsurf->type;
4644}
4645
Kristian Høgsberg75840622011-09-06 13:48:16 -04004646static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004647move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004648{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004649 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004650 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004651 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004652
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004653 if (seat->pointer->focus == NULL)
4654 return;
4655
4656 focus = seat->pointer->focus->surface;
4657
Pekka Paalanen01388e22013-04-25 13:57:44 +03004658 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004659 if (surface == NULL)
4660 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004661
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004662 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004663 if (shsurf == NULL || shsurf->state.fullscreen ||
4664 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004665 return;
4666
Derek Foreman8aeeac82015-01-30 13:24:36 -06004667 surface_move(shsurf, seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004668}
4669
4670static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004671maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4672{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004673 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004674 struct weston_surface *surface;
4675 struct shell_surface *shsurf;
4676
4677 surface = weston_surface_get_main_surface(focus);
4678 if (surface == NULL)
4679 return;
4680
4681 shsurf = get_shell_surface(surface);
4682 if (shsurf == NULL)
4683 return;
4684
4685 if (!shell_surface_is_xdg_surface(shsurf))
4686 return;
4687
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004688 shsurf->state_requested = true;
4689 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004690 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004691}
4692
4693static void
4694fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4695{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004696 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004697 struct weston_surface *surface;
4698 struct shell_surface *shsurf;
4699
4700 surface = weston_surface_get_main_surface(focus);
4701 if (surface == NULL)
4702 return;
4703
4704 shsurf = get_shell_surface(surface);
4705 if (shsurf == NULL)
4706 return;
4707
4708 if (!shell_surface_is_xdg_surface(shsurf))
4709 return;
4710
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004711 shsurf->state_requested = true;
4712 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004713 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004714 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004715}
4716
4717static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004718touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4719{
Derek Foreman362656b2014-09-04 10:23:05 -05004720 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004721 struct weston_surface *surface;
4722 struct shell_surface *shsurf;
4723
Derek Foreman362656b2014-09-04 10:23:05 -05004724 if (seat->touch->focus == NULL)
4725 return;
4726
4727 focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004728 surface = weston_surface_get_main_surface(focus);
4729 if (surface == NULL)
4730 return;
4731
4732 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004733 if (shsurf == NULL || shsurf->state.fullscreen ||
4734 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004735 return;
4736
Derek Foreman8aeeac82015-01-30 13:24:36 -06004737 surface_touch_move(shsurf, seat);
Neil Robertsaba0f252013-10-03 16:43:05 +01004738}
4739
4740static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004741resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004742{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004743 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004744 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004745 uint32_t edges = 0;
4746 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004747 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004748
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004749 if (seat->pointer->focus == NULL)
4750 return;
4751
4752 focus = seat->pointer->focus->surface;
4753
Pekka Paalanen01388e22013-04-25 13:57:44 +03004754 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004755 if (surface == NULL)
4756 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004757
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004758 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004759 if (shsurf == NULL || shsurf->state.fullscreen ||
4760 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004761 return;
4762
Jason Ekstranda7af7042013-10-12 22:38:11 -05004763 weston_view_from_global(shsurf->view,
4764 wl_fixed_to_int(seat->pointer->grab_x),
4765 wl_fixed_to_int(seat->pointer->grab_y),
4766 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004767
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004768 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004769 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004770 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004771 edges |= 0;
4772 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004773 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004774
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004775 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004776 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004777 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004778 edges |= 0;
4779 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004780 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004781
Derek Foreman8aeeac82015-01-30 13:24:36 -06004782 surface_resize(shsurf, seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004783}
4784
4785static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004786surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004787 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004788{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004789 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004790 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004791 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004792 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004793
Pekka Paalanen01388e22013-04-25 13:57:44 +03004794 /* XXX: broken for windows containing sub-surfaces */
4795 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004796 if (surface == NULL)
4797 return;
4798
4799 shsurf = get_shell_surface(surface);
4800 if (!shsurf)
4801 return;
4802
Jason Ekstranda7af7042013-10-12 22:38:11 -05004803 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004804
Jason Ekstranda7af7042013-10-12 22:38:11 -05004805 if (shsurf->view->alpha > 1.0)
4806 shsurf->view->alpha = 1.0;
4807 if (shsurf->view->alpha < step)
4808 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004809
Jason Ekstranda7af7042013-10-12 22:38:11 -05004810 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004811 weston_surface_damage(surface);
4812}
4813
4814static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004815do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004816 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004817{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004818 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004819 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004820 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004821
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004822 if (!seat->pointer) {
4823 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4824 return;
4825 }
4826
Scott Moreauccbf29d2012-02-22 14:21:41 -07004827 wl_list_for_each(output, &compositor->output_list, link) {
4828 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004829 wl_fixed_to_double(seat->pointer->x),
4830 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004831 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004832 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004833 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004834 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004835 increment = -output->zoom.increment;
4836 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004837 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004838 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004839 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004840 else
4841 increment = 0;
4842
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004843 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004844
Scott Moreaue6603982012-06-11 13:07:51 -06004845 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004846 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004847 else if (output->zoom.level > output->zoom.max_level)
4848 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004849 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004850 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004851 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004852
Scott Moreaue6603982012-06-11 13:07:51 -06004853 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004854
Jason Ekstranda7af7042013-10-12 22:38:11 -05004855 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004856 }
4857 }
4858}
4859
Scott Moreauccbf29d2012-02-22 14:21:41 -07004860static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004861zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004862 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004863{
4864 do_zoom(seat, time, 0, axis, value);
4865}
4866
4867static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004868zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004869 void *data)
4870{
4871 do_zoom(seat, time, key, 0, 0);
4872}
4873
4874static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004875terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004876 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004877{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004878 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004879
Daniel Stone325fc2d2012-05-30 16:31:58 +01004880 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004881}
4882
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004883static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004884rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4885 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004886{
4887 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004888 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004889 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004890 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004891 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004892
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004893 weston_pointer_move(pointer, x, y);
4894
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004895 if (!shsurf)
4896 return;
4897
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004898 cx = 0.5f * shsurf->surface->width;
4899 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004900
Daniel Stone37816df2012-05-16 18:45:18 +01004901 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4902 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004903 r = sqrtf(dx * dx + dy * dy);
4904
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004905 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004906 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004907
4908 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004909 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004910 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004911
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004912 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004913 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004914
4915 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004916 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004917 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004918 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004919 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004920
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004921 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004922 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004923 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004924 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004925 wl_list_init(&shsurf->rotation.transform.link);
4926 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004927 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004928 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004929
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004930 /* We need to adjust the position of the surface
4931 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004932 cposx = shsurf->view->geometry.x + cx;
4933 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004934 dposx = rotate->center.x - cposx;
4935 dposy = rotate->center.y - cposy;
4936 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004937 weston_view_set_position(shsurf->view,
4938 shsurf->view->geometry.x + dposx,
4939 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004940 }
4941
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004942 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004943 * lazily applies the damage due to rotation update.
4944 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004945 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004946}
4947
4948static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004949rotate_grab_button(struct weston_pointer_grab *grab,
4950 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004951{
4952 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004953 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004954 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004955 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004956 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004957
Daniel Stone4dbadb12012-05-30 16:31:51 +01004958 if (pointer->button_count == 0 &&
4959 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004960 if (shsurf)
4961 weston_matrix_multiply(&shsurf->rotation.rotation,
4962 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004963 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004964 free(rotate);
4965 }
4966}
4967
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004968static void
4969rotate_grab_cancel(struct weston_pointer_grab *grab)
4970{
4971 struct rotate_grab *rotate =
4972 container_of(grab, struct rotate_grab, base.grab);
4973
4974 shell_grab_end(&rotate->base);
4975 free(rotate);
4976}
4977
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004978static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004979 noop_grab_focus,
4980 rotate_grab_motion,
4981 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004982 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004983};
4984
4985static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004986surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004987{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004988 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004989 float dx, dy;
4990 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004991
Pekka Paalanen460099f2012-01-20 16:48:25 +02004992 rotate = malloc(sizeof *rotate);
4993 if (!rotate)
4994 return;
4995
Jason Ekstranda7af7042013-10-12 22:38:11 -05004996 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004997 surface->surface->width * 0.5f,
4998 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004999 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005000
Daniel Stone37816df2012-05-16 18:45:18 +01005001 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
5002 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005003 r = sqrtf(dx * dx + dy * dy);
5004 if (r > 20.0f) {
5005 struct weston_matrix inverse;
5006
5007 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005008 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005009 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005010
5011 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005012 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005013 } else {
5014 weston_matrix_init(&surface->rotation.rotation);
5015 weston_matrix_init(&rotate->rotation);
5016 }
5017
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005018 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
5019 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005020}
5021
5022static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005023rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005024 void *data)
5025{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005026 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005027 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005028 struct shell_surface *surface;
5029
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005030 if (seat->pointer->focus == NULL)
5031 return;
5032
5033 focus = seat->pointer->focus->surface;
5034
Pekka Paalanen01388e22013-04-25 13:57:44 +03005035 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005036 if (base_surface == NULL)
5037 return;
5038
5039 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005040 if (surface == NULL || surface->state.fullscreen ||
5041 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005042 return;
5043
5044 surface_rotate(surface, seat);
5045}
5046
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005047/* Move all fullscreen layers down to the current workspace and hide their
5048 * black views. The surfaces' state is set to both fullscreen and lowered,
5049 * and this is reversed when such a surface is re-configured, see
5050 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5051 *
5052 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005053 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005054void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005055lower_fullscreen_layer(struct desktop_shell *shell)
5056{
5057 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005058 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005059
5060 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005061 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005062 &shell->fullscreen_layer.view_list.link,
5063 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005064 struct shell_surface *shsurf = get_shell_surface(view->surface);
5065
5066 if (!shsurf)
5067 continue;
5068
5069 /* We can have a non-fullscreen popup for a fullscreen surface
5070 * in the fullscreen layer. */
5071 if (shsurf->state.fullscreen) {
5072 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005073 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5074 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005075 weston_view_damage_below(shsurf->fullscreen.black_view);
5076
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005077 }
5078
5079 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005080 weston_layer_entry_remove(&view->layer_link);
5081 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005082 weston_view_damage_below(view);
5083 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005084
5085 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005086 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005087}
5088
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005089void
Tiago Vignattibe143262012-04-16 17:31:41 +03005090activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005091 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005092{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005093 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005094 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005095 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005096 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005097 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005098
Kristian Høgsberg6110d072014-04-29 15:15:45 -07005099 lower_fullscreen_layer(shell);
5100
Pekka Paalanen01388e22013-04-25 13:57:44 +03005101 main_surface = weston_surface_get_main_surface(es);
5102
Daniel Stone37816df2012-05-16 18:45:18 +01005103 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005104
Jonas Ådahl8538b222012-08-29 22:13:03 +02005105 state = ensure_focus_state(shell, seat);
5106 if (state == NULL)
5107 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005108
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005109 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005110 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005111
Rafael Antognolli03b16592013-12-03 15:35:42 -02005112 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005113 assert(shsurf);
5114
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005115 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005116 shell_configure_fullscreen(shsurf);
5117 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005118 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005119
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005120 /* Update the surface’s layer. This brings it to the top of the stacking
5121 * order as appropriate. */
5122 shell_surface_update_layer(shsurf);
5123
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005124 if (shell->focus_animation_type != ANIMATION_NONE) {
5125 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005126 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005127 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005128}
5129
Alex Wu21858432012-04-01 20:13:08 +08005130/* no-op func for checking black surface */
5131static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005132black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005133{
5134}
5135
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005136static bool
Alex Wu21858432012-04-01 20:13:08 +08005137is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5138{
5139 if (es->configure == black_surface_configure) {
5140 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005141 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005142 return true;
5143 }
5144 return false;
5145}
5146
Kristian Høgsberg75840622011-09-06 13:48:16 -04005147static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005148activate_binding(struct weston_seat *seat,
5149 struct desktop_shell *shell,
5150 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005151{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005152 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005153
Alex Wu9c35e6b2012-03-05 14:13:13 +08005154 if (!focus)
5155 return;
5156
Pekka Paalanen01388e22013-04-25 13:57:44 +03005157 if (is_black_surface(focus, &main_surface))
5158 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005159
Pekka Paalanen01388e22013-04-25 13:57:44 +03005160 main_surface = weston_surface_get_main_surface(focus);
5161 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005162 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005163
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005164 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005165}
5166
5167static void
5168click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
5169 void *data)
5170{
5171 if (seat->pointer->grab != &seat->pointer->default_grab)
5172 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005173 if (seat->pointer->focus == NULL)
5174 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005175
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005176 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005177}
5178
5179static void
5180touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
5181{
5182 if (seat->touch->grab != &seat->touch->default_grab)
5183 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005184 if (seat->touch->focus == NULL)
5185 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005186
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005187 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005188}
5189
5190static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005191unfocus_all_seats(struct desktop_shell *shell)
5192{
5193 struct weston_seat *seat, *next;
5194
5195 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5196 if (seat->keyboard == NULL)
5197 continue;
5198
5199 weston_keyboard_set_focus(seat->keyboard, NULL);
5200 }
5201}
5202
5203static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005204lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005205{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005206 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005207
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005208 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005209 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005210 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005211 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005212
5213 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005214
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005215 /* Hide all surfaces by removing the fullscreen, panel and
5216 * toplevel layers. This way nothing else can show or receive
5217 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005218
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005219 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005220 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005221 if (shell->showing_input_panels)
5222 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005223 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005224 wl_list_insert(&shell->compositor->cursor_layer.link,
5225 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005226
Pekka Paalanen77346a62011-11-30 16:26:35 +02005227 launch_screensaver(shell);
5228
Neil Robertsb4a91702014-04-09 16:33:32 +01005229 /* Remove the keyboard focus on all seats. This will be
5230 * restored to the workspace's saved state via
5231 * restore_focus_state when the compositor is unlocked */
5232 unfocus_all_seats(shell);
5233
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005234 /* TODO: disable bindings that should not work while locked. */
5235
5236 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005237}
5238
5239static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005240unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005241{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005242 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005243 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005244 return;
5245 }
5246
5247 /* If desktop-shell client has gone away, unlock immediately. */
5248 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005249 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005250 return;
5251 }
5252
5253 if (shell->prepare_event_sent)
5254 return;
5255
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005256 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005257 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005258}
5259
5260static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005261shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005262{
5263 struct desktop_shell *shell = data;
5264
5265 shell->fade.animation = NULL;
5266
5267 switch (shell->fade.type) {
5268 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005269 weston_surface_destroy(shell->fade.view->surface);
5270 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005271 break;
5272 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005273 lock(shell);
5274 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005275 default:
5276 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005277 }
5278}
5279
Jason Ekstranda7af7042013-10-12 22:38:11 -05005280static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005281shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005282{
5283 struct weston_compositor *compositor = shell->compositor;
5284 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005285 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005286
5287 surface = weston_surface_create(compositor);
5288 if (!surface)
5289 return NULL;
5290
Jason Ekstranda7af7042013-10-12 22:38:11 -05005291 view = weston_view_create(surface);
5292 if (!view) {
5293 weston_surface_destroy(surface);
5294 return NULL;
5295 }
5296
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005297 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005298 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005299 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005300 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5301 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005302 pixman_region32_init(&surface->input);
5303
Jason Ekstranda7af7042013-10-12 22:38:11 -05005304 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005305}
5306
5307static void
5308shell_fade(struct desktop_shell *shell, enum fade_type type)
5309{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005310 float tint;
5311
5312 switch (type) {
5313 case FADE_IN:
5314 tint = 0.0;
5315 break;
5316 case FADE_OUT:
5317 tint = 1.0;
5318 break;
5319 default:
5320 weston_log("shell: invalid fade type\n");
5321 return;
5322 }
5323
5324 shell->fade.type = type;
5325
Jason Ekstranda7af7042013-10-12 22:38:11 -05005326 if (shell->fade.view == NULL) {
5327 shell->fade.view = shell_fade_create_surface(shell);
5328 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005329 return;
5330
Jason Ekstranda7af7042013-10-12 22:38:11 -05005331 shell->fade.view->alpha = 1.0 - tint;
5332 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005333 }
5334
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005335 if (shell->fade.view->output == NULL) {
5336 /* If the black view gets a NULL output, we lost the
5337 * last output and we'll just cancel the fade. This
5338 * happens when you close the last window under the
5339 * X11 or Wayland backends. */
5340 shell->locked = false;
5341 weston_surface_destroy(shell->fade.view->surface);
5342 shell->fade.view = NULL;
5343 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005344 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005345 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005346 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005347 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005348 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005349 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005350 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005351}
5352
5353static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005354do_shell_fade_startup(void *data)
5355{
5356 struct desktop_shell *shell = data;
5357
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005358 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005359 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005360 } else {
5361 weston_log("desktop shell: "
5362 "unexpected fade-in animation type %d\n",
5363 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005364 weston_surface_destroy(shell->fade.view->surface);
5365 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005366 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005367}
5368
5369static void
5370shell_fade_startup(struct desktop_shell *shell)
5371{
5372 struct wl_event_loop *loop;
5373
5374 if (!shell->fade.startup_timer)
5375 return;
5376
5377 wl_event_source_remove(shell->fade.startup_timer);
5378 shell->fade.startup_timer = NULL;
5379
5380 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5381 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5382}
5383
5384static int
5385fade_startup_timeout(void *data)
5386{
5387 struct desktop_shell *shell = data;
5388
5389 shell_fade_startup(shell);
5390 return 0;
5391}
5392
5393static void
5394shell_fade_init(struct desktop_shell *shell)
5395{
5396 /* Make compositor output all black, and wait for the desktop-shell
5397 * client to signal it is ready, then fade in. The timer triggers a
5398 * fade-in, in case the desktop-shell client takes too long.
5399 */
5400
5401 struct wl_event_loop *loop;
5402
Jason Ekstranda7af7042013-10-12 22:38:11 -05005403 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005404 weston_log("%s: warning: fade surface already exists\n",
5405 __func__);
5406 return;
5407 }
5408
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005409 if (shell->startup_animation_type == ANIMATION_NONE)
5410 return;
5411
Jason Ekstranda7af7042013-10-12 22:38:11 -05005412 shell->fade.view = shell_fade_create_surface(shell);
5413 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005414 return;
5415
Jason Ekstranda7af7042013-10-12 22:38:11 -05005416 weston_view_update_transform(shell->fade.view);
5417 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005418
5419 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5420 shell->fade.startup_timer =
5421 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5422 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5423}
5424
5425static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005426idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005427{
5428 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005429 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005430 struct weston_seat *seat;
5431
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005432 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005433 if (seat->pointer)
5434 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005435 if (seat->touch)
5436 touch_popup_grab_end(seat->touch);
5437 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005438
5439 shell_fade(shell, FADE_OUT);
5440 /* lock() is called from shell_fade_done() */
5441}
5442
5443static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005444wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005445{
5446 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005447 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005448
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005449 unlock(shell);
5450}
5451
5452static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005453center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005454{
Giulio Camuffob8366642013-04-25 13:57:46 +03005455 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005456 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005457
Jason Ekstranda7af7042013-10-12 22:38:11 -05005458 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005459
5460 x = output->x + (output->width - width) / 2 - surf_x / 2;
5461 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005462
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005463 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005464}
5465
5466static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005467weston_view_set_initial_position(struct weston_view *view,
5468 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005469{
5470 struct weston_compositor *compositor = shell->compositor;
5471 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005472 int32_t range_x, range_y;
5473 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005474 struct weston_output *output, *target_output = NULL;
5475 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005476 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005477
5478 /* As a heuristic place the new window on the same output as the
5479 * pointer. Falling back to the output containing 0, 0.
5480 *
5481 * TODO: Do something clever for touch too?
5482 */
5483 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005484 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005485 ix = wl_fixed_to_int(seat->pointer->x);
5486 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005487 break;
5488 }
5489 }
5490
5491 wl_list_for_each(output, &compositor->output_list, link) {
5492 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5493 target_output = output;
5494 break;
5495 }
5496 }
5497
5498 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005499 weston_view_set_position(view, 10 + random() % 400,
5500 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005501 return;
5502 }
5503
5504 /* Valid range within output where the surface will still be onscreen.
5505 * If this is negative it means that the surface is bigger than
5506 * output.
5507 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005508 get_output_work_area(shell, target_output, &area);
5509
5510 dx = area.x;
5511 dy = area.y;
5512 range_x = area.width - view->surface->width;
5513 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005514
Rob Bradford4cb88c72012-08-13 15:18:44 +01005515 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005516 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005517
5518 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005519 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005520
5521 x = target_output->x + dx;
5522 y = target_output->y + dy;
5523
Jason Ekstranda7af7042013-10-12 22:38:11 -05005524 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005525}
5526
5527static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005528set_maximized_position(struct desktop_shell *shell,
5529 struct shell_surface *shsurf)
5530{
5531 int32_t surf_x, surf_y;
5532 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005533 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005534
Jonny Lambd73c6942014-08-20 15:53:20 +02005535 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005536 if (shsurf->has_set_geometry) {
5537 surf_x = shsurf->geometry.x;
5538 surf_y = shsurf->geometry.y;
5539 } else {
5540 surface_subsurfaces_boundingbox(shsurf->surface,
5541 &surf_x, &surf_y, NULL, NULL);
5542 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005543 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005544
5545 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005546 e->x1 + area.x - surf_x,
5547 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005548}
5549
5550static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005551map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005552 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005553{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005554 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005555 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005556
Pekka Paalanen77346a62011-11-30 16:26:35 +02005557 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005558 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005559 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005560 if (shsurf->state.fullscreen) {
5561 center_on_output(shsurf->view, shsurf->fullscreen_output);
5562 shell_map_fullscreen(shsurf);
5563 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005564 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005565 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005566 weston_view_set_initial_position(shsurf->view, shell);
5567 }
Juan Zhao96879df2012-02-07 08:45:41 +08005568 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005569 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005570 if (shell_map_popup(shsurf) != 0)
5571 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005572 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005573 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005574 weston_view_set_position(shsurf->view,
5575 shsurf->view->geometry.x + sx,
5576 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005577 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005578 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005579 default:
5580 ;
5581 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005582
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005583 /* Surface stacking order, see also activate(). */
5584 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005585
Jason Ekstranda7af7042013-10-12 22:38:11 -05005586 if (shsurf->type != SHELL_SURFACE_NONE) {
5587 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005588 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005589 shsurf->surface->output = shsurf->output;
5590 shsurf->view->output = shsurf->output;
5591 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005592 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005593
Jason Ekstranda7af7042013-10-12 22:38:11 -05005594 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005595 /* XXX: xwayland's using the same fields for transient type */
5596 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005597 if (shsurf->transient.flags ==
5598 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5599 break;
5600 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005601 if (shsurf->state.relative &&
5602 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5603 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005604 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005605 break;
5606 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005607 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005608 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005609 case SHELL_SURFACE_POPUP:
5610 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005611 default:
5612 break;
5613 }
5614
Rafael Antognolli03b16592013-12-03 15:35:42 -02005615 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5616 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005617 {
5618 switch (shell->win_animation_type) {
5619 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005620 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005621 break;
5622 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005623 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005624 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005625 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005626 default:
5627 break;
5628 }
5629 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005630}
5631
5632static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005633configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005634 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005635{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005636 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005637 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005638
Pekka Paalanen77346a62011-11-30 16:26:35 +02005639 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005640
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005641 assert(shsurf);
5642
Rafael Antognolli03b16592013-12-03 15:35:42 -02005643 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005644 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005645 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005646 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005647 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005648 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005649 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005650
Alex Wu4539b082012-03-01 12:57:46 +08005651 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005652 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005653 wl_list_for_each(view, &surface->views, surface_link)
5654 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005655
Rafael Antognolli03b16592013-12-03 15:35:42 -02005656 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005657 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005658 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005659}
5660
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005661static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005662shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005663{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005664 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005665 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005666 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005667
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005668 assert(shsurf);
5669
5670 shell = shsurf->shell;
5671
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005672 if (!weston_surface_is_mapped(es) &&
5673 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005674 remove_popup_grab(shsurf);
5675 }
5676
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005677 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005678 return;
5679
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005680 if (shsurf->has_next_geometry) {
5681 shsurf->geometry = shsurf->next_geometry;
5682 shsurf->has_next_geometry = false;
5683 shsurf->has_set_geometry = true;
5684 } else if (!shsurf->has_set_geometry) {
5685 surface_subsurfaces_boundingbox(shsurf->surface,
5686 &shsurf->geometry.x,
5687 &shsurf->geometry.y,
5688 &shsurf->geometry.width,
5689 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005690 }
5691
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005692 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005693 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005694 type_changed = 1;
5695 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005696
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005697 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005698 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005699 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005700 shsurf->last_width != es->width ||
5701 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005702 float from_x, from_y;
5703 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005704
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005705 if (shsurf->resize_edges) {
5706 sx = 0;
5707 sy = 0;
5708 }
5709
5710 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5711 sx = shsurf->last_width - es->width;
5712 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5713 sy = shsurf->last_height - es->height;
5714
5715 shsurf->last_width = es->width;
5716 shsurf->last_height = es->height;
5717
Jason Ekstranda7af7042013-10-12 22:38:11 -05005718 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5719 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005720 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005721 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005722 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005723 }
5724}
5725
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005726static bool
5727check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5728{
5729 struct timespec now;
5730
5731 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5732 return false;
5733
5734 /*
5735 * If the shell helper client dies before the session has been
5736 * up for roughly 30 seconds, better just make Weston shut down,
5737 * because the user likely has no way to interact with the desktop
5738 * anyway.
5739 */
5740 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5741 weston_log("Error: %s apparently cannot run at all.\n",
5742 shell->client);
5743 weston_log_continue(STAMP_SPACE "Quitting...");
5744 wl_display_terminate(shell->compositor->wl_display);
5745
5746 return true;
5747 }
5748
5749 return false;
5750}
5751
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005752static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005753
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005754static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005755respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005756{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005757 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005758
5759 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5760 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005761 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005762 shell->child.deathstamp = time;
5763 shell->child.deathcount = 0;
5764 }
5765
5766 shell->child.deathcount++;
5767 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005768 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005769 return;
5770 }
5771
Pekka Paalanen826dc142014-08-27 12:11:53 +03005772 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005773 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005774}
5775
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005776static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005777desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5778{
5779 struct desktop_shell *shell;
5780
5781 shell = container_of(listener, struct desktop_shell,
5782 child.client_destroy_listener);
5783
Pekka Paalanen826dc142014-08-27 12:11:53 +03005784 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005785 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005786 /*
5787 * unbind_desktop_shell() will reset shell->child.desktop_shell
5788 * before the respawned process has a chance to create a new
5789 * desktop_shell object, because we are being called from the
5790 * wl_client destructor which destroys all wl_resources before
5791 * returning.
5792 */
5793
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005794 if (!check_desktop_shell_crash_too_early(shell))
5795 respawn_desktop_shell_process(shell);
5796
Pekka Paalanen826dc142014-08-27 12:11:53 +03005797 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005798}
5799
5800static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005801launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005802{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005803 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005804
Pekka Paalanen826dc142014-08-27 12:11:53 +03005805 shell->child.client = weston_client_start(shell->compositor,
5806 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005807
Arnaud Vrac42631192014-08-25 20:56:46 +02005808 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005809 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005810 return;
5811 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005812
5813 shell->child.client_destroy_listener.notify =
5814 desktop_shell_client_destroy;
5815 wl_client_add_destroy_listener(shell->child.client,
5816 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005817}
5818
5819static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005820handle_shell_client_destroy(struct wl_listener *listener, void *data)
5821{
5822 struct shell_client *sc =
5823 container_of(listener, struct shell_client, destroy_listener);
5824
5825 if (sc->ping_timer)
5826 wl_event_source_remove(sc->ping_timer);
5827 free(sc);
5828}
5829
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005830static struct shell_client *
5831shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5832 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005833{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005834 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005835
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005836 sc = zalloc(sizeof *sc);
5837 if (sc == NULL) {
5838 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005839 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005840 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005841
5842 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005843 if (sc->resource == NULL) {
5844 free(sc);
5845 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005846 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005847 }
5848
5849 sc->client = client;
5850 sc->shell = shell;
5851 sc->destroy_listener.notify = handle_shell_client_destroy;
5852 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005853 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005854
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005855 return sc;
5856}
5857
5858static void
5859bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5860{
5861 struct desktop_shell *shell = data;
5862 struct shell_client *sc;
5863
5864 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5865 if (sc)
5866 wl_resource_set_implementation(sc->resource,
5867 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005868 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005869}
5870
5871static void
5872bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5873{
5874 struct desktop_shell *shell = data;
5875 struct shell_client *sc;
5876
5877 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5878 if (sc)
5879 wl_resource_set_dispatcher(sc->resource,
5880 xdg_shell_unversioned_dispatch,
5881 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005882}
5883
5884static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005885unbind_desktop_shell(struct wl_resource *resource)
5886{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005887 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005888
5889 if (shell->locked)
5890 resume_desktop(shell);
5891
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005892 shell->child.desktop_shell = NULL;
5893 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005894}
5895
5896static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005897bind_desktop_shell(struct wl_client *client,
5898 void *data, uint32_t version, uint32_t id)
5899{
Tiago Vignattibe143262012-04-16 17:31:41 +03005900 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005901 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005902
Jason Ekstranda85118c2013-06-27 20:17:02 -05005903 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005904 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005905
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005906 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005907 wl_resource_set_implementation(resource,
5908 &desktop_shell_implementation,
5909 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005910 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005911
5912 if (version < 2)
5913 shell_fade_startup(shell);
5914
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005915 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005916 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005917
5918 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5919 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005920}
5921
Pekka Paalanen8274d902014-08-06 19:36:51 +03005922static int
5923screensaver_get_label(struct weston_surface *surface, char *buf, size_t len)
5924{
5925 return snprintf(buf, len, "screensaver for output %s",
5926 surface->output->name);
5927}
5928
Pekka Paalanen6e168112011-11-24 11:34:05 +02005929static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005930screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005931{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005932 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005933 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005934 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005935
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005936 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005937 return;
5938
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005939 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005940 if (!shell->locked)
5941 return;
5942
Jason Ekstranda7af7042013-10-12 22:38:11 -05005943 view = container_of(surface->views.next, struct weston_view, surface_link);
5944 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005945
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005946 if (wl_list_empty(&view->layer_link.link)) {
5947 prev = container_of(shell->lock_layer.view_list.link.prev,
5948 struct weston_layer_entry, link);
5949 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005950 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005951 wl_event_source_timer_update(shell->screensaver.timer,
5952 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005953 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005954 }
5955}
5956
5957static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005958screensaver_set_surface(struct wl_client *client,
5959 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005960 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005961 struct wl_resource *output_resource)
5962{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005963 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005964 struct weston_surface *surface =
5965 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005966 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005967 struct weston_view *view, *next;
5968
5969 /* Make sure we only have one view */
5970 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5971 weston_view_destroy(view);
5972 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005973
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005974 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005975 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03005976 weston_surface_set_label_func(surface, screensaver_get_label);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005977 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005978}
5979
5980static const struct screensaver_interface screensaver_implementation = {
5981 screensaver_set_surface
5982};
5983
5984static void
5985unbind_screensaver(struct wl_resource *resource)
5986{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005987 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005988
Pekka Paalanen77346a62011-11-30 16:26:35 +02005989 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005990}
5991
5992static void
5993bind_screensaver(struct wl_client *client,
5994 void *data, uint32_t version, uint32_t id)
5995{
Tiago Vignattibe143262012-04-16 17:31:41 +03005996 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005997 struct wl_resource *resource;
5998
Jason Ekstranda85118c2013-06-27 20:17:02 -05005999 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02006000
Pekka Paalanen77346a62011-11-30 16:26:35 +02006001 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05006002 wl_resource_set_implementation(resource,
6003 &screensaver_implementation,
6004 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02006005 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02006006 return;
6007 }
6008
6009 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
6010 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02006011}
6012
Kristian Høgsberg07045392012-02-19 18:52:44 -05006013struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03006014 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006015 struct weston_surface *current;
6016 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006017 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006018 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006019};
6020
6021static void
6022switcher_next(struct switcher *switcher)
6023{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006024 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006025 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006026 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006027 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006028
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006029 /* temporary re-display minimized surfaces */
6030 struct weston_view *tmp;
6031 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006032 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6033 weston_layer_entry_remove(&view->layer_link);
6034 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006035 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6036 *minimized = view;
6037 }
6038
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006039 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006040 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006041 if (shsurf &&
6042 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6043 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006044 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006045 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006046 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006047 next = view->surface;
6048 prev = view->surface;
6049 view->alpha = 0.25;
6050 weston_view_geometry_dirty(view);
6051 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006052 }
Alex Wu1659daa2012-04-01 20:13:09 +08006053
Jason Ekstranda7af7042013-10-12 22:38:11 -05006054 if (is_black_surface(view->surface, NULL)) {
6055 view->alpha = 0.25;
6056 weston_view_geometry_dirty(view);
6057 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006058 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006059 }
6060
6061 if (next == NULL)
6062 next = first;
6063
Alex Wu07b26062012-03-12 16:06:01 +08006064 if (next == NULL)
6065 return;
6066
Kristian Høgsberg07045392012-02-19 18:52:44 -05006067 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006068 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006069
6070 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006071 wl_list_for_each(view, &next->views, surface_link)
6072 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006073
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006074 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006075 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006076 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006077}
6078
6079static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006080switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006081{
6082 struct switcher *switcher =
6083 container_of(listener, struct switcher, listener);
6084
6085 switcher_next(switcher);
6086}
6087
6088static void
Daniel Stone351eb612012-05-31 15:27:47 -04006089switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006090{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006091 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006092 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006093 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006094
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006095 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006096 if (is_focus_view(view))
6097 continue;
6098
Jason Ekstranda7af7042013-10-12 22:38:11 -05006099 view->alpha = 1.0;
6100 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006101 }
6102
Alex Wu07b26062012-03-12 16:06:01 +08006103 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006104 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006105 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006106 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006107 weston_keyboard_end_grab(keyboard);
6108 if (keyboard->input_method_resource)
6109 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006110
6111 /* re-hide surfaces that were temporary shown during the switch */
6112 struct weston_view **minimized;
6113 wl_array_for_each(minimized, &switcher->minimized_array) {
6114 /* with the exception of the current selected */
6115 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006116 weston_layer_entry_remove(&(*minimized)->layer_link);
6117 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006118 weston_view_damage_below(*minimized);
6119 }
6120 }
6121 wl_array_release(&switcher->minimized_array);
6122
Kristian Høgsberg07045392012-02-19 18:52:44 -05006123 free(switcher);
6124}
6125
6126static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006127switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006128 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006129{
6130 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006131 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006132
Daniel Stonec9785ea2012-05-30 16:31:52 +01006133 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006134 switcher_next(switcher);
6135}
6136
6137static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006138switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006139 uint32_t mods_depressed, uint32_t mods_latched,
6140 uint32_t mods_locked, uint32_t group)
6141{
6142 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006143 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006144
Daniel Stone351eb612012-05-31 15:27:47 -04006145 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6146 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006147}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006148
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006149static void
6150switcher_cancel(struct weston_keyboard_grab *grab)
6151{
6152 struct switcher *switcher = container_of(grab, struct switcher, grab);
6153
6154 switcher_destroy(switcher);
6155}
6156
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006157static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006158 switcher_key,
6159 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006160 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006161};
6162
6163static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006164switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006165 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006166{
Tiago Vignattibe143262012-04-16 17:31:41 +03006167 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006168 struct switcher *switcher;
6169
6170 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006171 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006172 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006173 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006174 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006175 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006176
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006177 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006178 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006179 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006180 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
6181 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006182 switcher_next(switcher);
6183}
6184
Pekka Paalanen3c647232011-12-22 13:43:43 +02006185static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006186backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006187 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006188{
6189 struct weston_compositor *compositor = data;
6190 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006191 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006192
6193 /* TODO: we're limiting to simple use cases, where we assume just
6194 * control on the primary display. We'd have to extend later if we
6195 * ever get support for setting backlights on random desktop LCD
6196 * panels though */
6197 output = get_default_output(compositor);
6198 if (!output)
6199 return;
6200
6201 if (!output->set_backlight)
6202 return;
6203
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006204 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6205 backlight_new = output->backlight_current - 25;
6206 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6207 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006208
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006209 if (backlight_new < 5)
6210 backlight_new = 5;
6211 if (backlight_new > 255)
6212 backlight_new = 255;
6213
6214 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006215 output->set_backlight(output, output->backlight_current);
6216}
6217
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006218static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006219force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006220 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006221{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006222 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006223 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006224 struct desktop_shell *shell = data;
6225 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006226 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006227
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006228 focus_surface = seat->keyboard->focus;
6229 if (!focus_surface)
6230 return;
6231
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006232 wl_signal_emit(&compositor->kill_signal, focus_surface);
6233
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006234 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006235 wl_client_get_credentials(client, &pid, NULL, NULL);
6236
6237 /* Skip clients that we launched ourselves (the credentials of
6238 * the socketpair is ours) */
6239 if (pid == getpid())
6240 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006241
Daniel Stone325fc2d2012-05-30 16:31:58 +01006242 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006243}
6244
6245static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006246workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006247 uint32_t key, void *data)
6248{
6249 struct desktop_shell *shell = data;
6250 unsigned int new_index = shell->workspaces.current;
6251
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006252 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006253 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006254 if (new_index != 0)
6255 new_index--;
6256
6257 change_workspace(shell, new_index);
6258}
6259
6260static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006261workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006262 uint32_t key, void *data)
6263{
6264 struct desktop_shell *shell = data;
6265 unsigned int new_index = shell->workspaces.current;
6266
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006267 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006268 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006269 if (new_index < shell->workspaces.num - 1)
6270 new_index++;
6271
6272 change_workspace(shell, new_index);
6273}
6274
6275static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006276workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006277 uint32_t key, void *data)
6278{
6279 struct desktop_shell *shell = data;
6280 unsigned int new_index;
6281
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006282 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006283 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006284 new_index = key - KEY_F1;
6285 if (new_index >= shell->workspaces.num)
6286 new_index = shell->workspaces.num - 1;
6287
6288 change_workspace(shell, new_index);
6289}
6290
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006291static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006292workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006293 uint32_t key, void *data)
6294{
6295 struct desktop_shell *shell = data;
6296 unsigned int new_index = shell->workspaces.current;
6297
6298 if (shell->locked)
6299 return;
6300
6301 if (new_index != 0)
6302 new_index--;
6303
6304 take_surface_to_workspace_by_seat(shell, seat, new_index);
6305}
6306
6307static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006308workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006309 uint32_t key, void *data)
6310{
6311 struct desktop_shell *shell = data;
6312 unsigned int new_index = shell->workspaces.current;
6313
6314 if (shell->locked)
6315 return;
6316
6317 if (new_index < shell->workspaces.num - 1)
6318 new_index++;
6319
6320 take_surface_to_workspace_by_seat(shell, seat, new_index);
6321}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006322
6323static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006324shell_reposition_view_on_output_destroy(struct weston_view *view)
6325{
6326 struct weston_output *output, *first_output;
6327 struct weston_compositor *ec = view->surface->compositor;
6328 struct shell_surface *shsurf;
6329 float x, y;
6330 int visible;
6331
6332 x = view->geometry.x;
6333 y = view->geometry.y;
6334
6335 /* At this point the destroyed output is not in the list anymore.
6336 * If the view is still visible somewhere, we leave where it is,
6337 * otherwise, move it to the first output. */
6338 visible = 0;
6339 wl_list_for_each(output, &ec->output_list, link) {
6340 if (pixman_region32_contains_point(&output->region,
6341 x, y, NULL)) {
6342 visible = 1;
6343 break;
6344 }
6345 }
6346
6347 if (!visible) {
6348 first_output = container_of(ec->output_list.next,
6349 struct weston_output, link);
6350
6351 x = first_output->x + first_output->width / 4;
6352 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006353
6354 weston_view_set_position(view, x, y);
6355 } else {
6356 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006357 }
6358
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006359
6360 shsurf = get_shell_surface(view->surface);
6361
6362 if (shsurf) {
6363 shsurf->saved_position_valid = false;
6364 shsurf->next_state.maximized = false;
6365 shsurf->next_state.fullscreen = false;
6366 shsurf->state_changed = true;
6367 }
6368}
6369
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006370void
6371shell_for_each_layer(struct desktop_shell *shell,
6372 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006373{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006374 struct workspace **ws;
6375
6376 func(shell, &shell->fullscreen_layer, data);
6377 func(shell, &shell->panel_layer, data);
6378 func(shell, &shell->background_layer, data);
6379 func(shell, &shell->lock_layer, data);
6380 func(shell, &shell->input_panel_layer, data);
6381
6382 wl_array_for_each(ws, &shell->workspaces.array)
6383 func(shell, &(*ws)->layer, data);
6384}
6385
6386static void
6387shell_output_destroy_move_layer(struct desktop_shell *shell,
6388 struct weston_layer *layer,
6389 void *data)
6390{
6391 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006392 struct weston_view *view;
6393
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006394 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006395 if (view->output != output)
6396 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006397
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006398 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006399 }
6400}
6401
6402static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006403handle_output_destroy(struct wl_listener *listener, void *data)
6404{
6405 struct shell_output *output_listener =
6406 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006407 struct weston_output *output = output_listener->output;
6408 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006409
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006410 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006411
Xiong Zhang6b481422013-10-23 13:58:32 +08006412 wl_list_remove(&output_listener->destroy_listener.link);
6413 wl_list_remove(&output_listener->link);
6414 free(output_listener);
6415}
6416
6417static void
6418create_shell_output(struct desktop_shell *shell,
6419 struct weston_output *output)
6420{
6421 struct shell_output *shell_output;
6422
6423 shell_output = zalloc(sizeof *shell_output);
6424 if (shell_output == NULL)
6425 return;
6426
6427 shell_output->output = output;
6428 shell_output->shell = shell;
6429 shell_output->destroy_listener.notify = handle_output_destroy;
6430 wl_signal_add(&output->destroy_signal,
6431 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006432 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006433}
6434
6435static void
6436handle_output_create(struct wl_listener *listener, void *data)
6437{
6438 struct desktop_shell *shell =
6439 container_of(listener, struct desktop_shell, output_create_listener);
6440 struct weston_output *output = (struct weston_output *)data;
6441
6442 create_shell_output(shell, output);
6443}
6444
6445static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006446handle_output_move_layer(struct desktop_shell *shell,
6447 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006448{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006449 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006450 struct weston_view *view;
6451 float x, y;
6452
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006453 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006454 if (view->output != output)
6455 continue;
6456
6457 x = view->geometry.x + output->move_x;
6458 y = view->geometry.y + output->move_y;
6459 weston_view_set_position(view, x, y);
6460 }
6461}
6462
6463static void
6464handle_output_move(struct wl_listener *listener, void *data)
6465{
6466 struct desktop_shell *shell;
6467
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006468 shell = container_of(listener, struct desktop_shell,
6469 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006470
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006471 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006472}
6473
6474static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006475setup_output_destroy_handler(struct weston_compositor *ec,
6476 struct desktop_shell *shell)
6477{
6478 struct weston_output *output;
6479
6480 wl_list_init(&shell->output_list);
6481 wl_list_for_each(output, &ec->output_list, link)
6482 create_shell_output(shell, output);
6483
6484 shell->output_create_listener.notify = handle_output_create;
6485 wl_signal_add(&ec->output_created_signal,
6486 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006487
6488 shell->output_move_listener.notify = handle_output_move;
6489 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006490}
6491
6492static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006493shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006494{
Tiago Vignattibe143262012-04-16 17:31:41 +03006495 struct desktop_shell *shell =
6496 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006497 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006498 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006499
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006500 /* Force state to unlocked so we don't try to fade */
6501 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006502
6503 if (shell->child.client) {
6504 /* disable respawn */
6505 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006506 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006507 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006508
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09006509 wl_event_source_remove(shell->screensaver.timer);
6510
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006511 wl_list_remove(&shell->idle_listener.link);
6512 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006513
6514 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006515
Xiong Zhang6b481422013-10-23 13:58:32 +08006516 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6517 wl_list_remove(&shell_output->destroy_listener.link);
6518 wl_list_remove(&shell_output->link);
6519 free(shell_output);
6520 }
6521
6522 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006523 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006524
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006525 wl_array_for_each(ws, &shell->workspaces.array)
6526 workspace_destroy(*ws);
6527 wl_array_release(&shell->workspaces.array);
6528
Pekka Paalanen3c647232011-12-22 13:43:43 +02006529 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006530 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006531 free(shell);
6532}
6533
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006534static void
6535shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6536{
6537 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006538 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006539
6540 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006541 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6542 MODIFIER_CTRL | MODIFIER_ALT,
6543 terminate_binding, ec);
6544 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6545 click_to_activate_binding,
6546 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006547 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6548 click_to_activate_binding,
6549 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006550 weston_compositor_add_touch_binding(ec, 0,
6551 touch_to_activate_binding,
6552 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006553 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6554 MODIFIER_SUPER | MODIFIER_ALT,
6555 surface_opacity_binding, NULL);
6556 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6557 MODIFIER_SUPER, zoom_axis_binding,
6558 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006559
6560 /* configurable bindings */
6561 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006562 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6563 zoom_key_binding, NULL);
6564 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6565 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006566 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6567 maximize_binding, NULL);
6568 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6569 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006570 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6571 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006572 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006573 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6574 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006575 weston_compositor_add_button_binding(ec, BTN_LEFT,
6576 mod | MODIFIER_SHIFT,
6577 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006578
6579 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6580 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6581 rotate_binding, NULL);
6582
Daniel Stone325fc2d2012-05-30 16:31:58 +01006583 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6584 shell);
6585 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6586 ec);
6587 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6588 backlight_binding, ec);
6589 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6590 ec);
6591 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6592 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006593 weston_compositor_add_key_binding(ec, KEY_K, mod,
6594 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006595 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6596 workspace_up_binding, shell);
6597 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6598 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006599 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6600 workspace_move_surface_up_binding,
6601 shell);
6602 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6603 workspace_move_surface_down_binding,
6604 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006605
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006606 if (shell->exposay_modifier)
6607 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6608 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006609
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006610 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6611 if (shell->workspaces.num > 1) {
6612 num_workspace_bindings = shell->workspaces.num;
6613 if (num_workspace_bindings > 6)
6614 num_workspace_bindings = 6;
6615 for (i = 0; i < num_workspace_bindings; i++)
6616 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6617 workspace_f_binding,
6618 shell);
6619 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006620
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006621 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006622}
6623
Jason Ekstrand024177c2014-04-21 19:42:58 -05006624static void
6625handle_seat_created(struct wl_listener *listener, void *data)
6626{
6627 struct weston_seat *seat = data;
6628
6629 create_shell_seat(seat);
6630}
6631
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006632WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006633module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006634 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006635{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006636 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006637 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006638 struct workspace **pws;
6639 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006640 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006641
Peter Huttererf3d62272013-08-08 11:57:05 +10006642 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006643 if (shell == NULL)
6644 return -1;
6645
Kristian Høgsberg75840622011-09-06 13:48:16 -04006646 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006647
6648 shell->destroy_listener.notify = shell_destroy;
6649 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006650 shell->idle_listener.notify = idle_handler;
6651 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6652 shell->wake_listener.notify = wake_handler;
6653 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006654
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006655 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006656 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006657 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006658 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006659 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006660 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006661 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006662 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006663 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006664 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006665 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006666 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006667 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006668
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006669 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6670 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006671 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6672 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006673 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006674
6675 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006676 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006677
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006678 if (input_panel_setup(shell) < 0)
6679 return -1;
6680
Murray Calavera9a51cd72015-06-10 21:16:02 +00006681 if (text_backend_init(ec) < 0)
6682 return -1;
6683
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006684 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006685
Daniel Stonedf8133b2013-11-19 11:37:14 +01006686 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6687 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6688
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006689 for (i = 0; i < shell->workspaces.num; i++) {
6690 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6691 if (pws == NULL)
6692 return -1;
6693
6694 *pws = workspace_create();
6695 if (*pws == NULL)
6696 return -1;
6697 }
6698 activate_workspace(shell, 0);
6699
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006700 weston_layer_init(&shell->minimized_layer, NULL);
6701
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006702 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006703 wl_list_init(&shell->workspaces.animation.link);
6704 shell->workspaces.animation.frame = animate_workspace_change_frame;
6705
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006706 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006707 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006708 return -1;
6709
Rafael Antognollie2a34552013-12-03 15:35:45 -02006710 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6711 shell, bind_xdg_shell) == NULL)
6712 return -1;
6713
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006714 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006715 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006716 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006717 return -1;
6718
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006719 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6720 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006721 return -1;
6722
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006723 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6724 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006725 return -1;
6726
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006727 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006728
Jonny Lamb765760d2014-08-20 15:53:19 +02006729 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6730
Xiong Zhang6b481422013-10-23 13:58:32 +08006731 setup_output_destroy_handler(ec, shell);
6732
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006733 loop = wl_display_get_event_loop(ec->wl_display);
6734 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006735
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006736 shell->screensaver.timer =
6737 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6738
Jason Ekstrand024177c2014-04-21 19:42:58 -05006739 wl_list_for_each(seat, &ec->seat_list, link)
6740 handle_seat_created(NULL, seat);
6741 shell->seat_create_listener.notify = handle_seat_created;
6742 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006743
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006744 screenshooter_create(ec);
6745
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006746 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006747
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006748 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006749
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006750 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6751
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006752 return 0;
6753}