blob: f93dc941941207b1613646c660f542d59f753c31 [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"
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080039#include "weston-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"
Jonas Ådahl2a229332015-11-17 16:00:32 +080043#include "xdg-shell-unstable-v5-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;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500202 bool 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
Derek Foreman74de4692015-07-15 13:00:39 -0500254surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
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;
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200312 struct weston_view *view;
313 struct shell_surface *top_fs_shsurf = NULL;
Alex Wubd3354b2012-04-17 17:20:49 +0800314
315 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100316
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300317 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800318 return false;
319
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200320 /* Find topmost shsurf on the same fullscreen output on which shsurf
321 * is displaying. We don't care about other outputs.
322 */
323 wl_list_for_each(view, &shell->fullscreen_layer.view_list.link,
324 layer_link.link) {
325 struct shell_surface *cand_shsurf = get_shell_surface(view->surface);
326
327 if (cand_shsurf &&
328 (cand_shsurf->fullscreen_output == shsurf->fullscreen_output)) {
329 top_fs_shsurf = cand_shsurf;
330 break;
331 }
332 }
333
334 return (shsurf == top_fs_shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800335}
336
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500337static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400338destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300339{
340 struct shell_grab *grab;
341
342 grab = container_of(listener, struct shell_grab,
343 shsurf_destroy_listener);
344
345 grab->shsurf = NULL;
346}
347
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800348struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349get_default_view(struct weston_surface *surface)
350{
351 struct shell_surface *shsurf;
352 struct weston_view *view;
353
354 if (!surface || wl_list_empty(&surface->views))
355 return NULL;
356
357 shsurf = get_shell_surface(surface);
358 if (shsurf)
359 return shsurf->view;
360
361 wl_list_for_each(view, &surface->views, surface_link)
362 if (weston_view_is_mapped(view))
363 return view;
364
365 return container_of(surface->views.next, struct weston_view, surface_link);
366}
367
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300368static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400369popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200370static void
371touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400372
373static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400375 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300376 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400377 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800378 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300379{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300380 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500381 struct weston_touch *touch = weston_seat_get_touch(pointer->seat);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300382
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400383 popup_grab_end(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -0500384 if (touch)
385 touch_popup_grab_end(touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400386
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300387 grab->grab.interface = interface;
388 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400389 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500390 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400391 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300392
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700393 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400394 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400395 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800396 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400397 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500398 weston_pointer_set_focus(pointer,
399 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400400 wl_fixed_from_int(0),
401 wl_fixed_from_int(0));
402 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300403}
404
Jonny Lambd73c6942014-08-20 15:53:20 +0200405static void
406get_output_panel_size(struct desktop_shell *shell,
407 struct weston_output *output,
408 int *width,
409 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700410{
411 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200412
413 *width = 0;
414 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700415
416 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200417 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700418
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300419 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200420 float x, y;
421
422 if (view->surface->output != output)
423 continue;
424
425 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800426 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
427 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200428 weston_view_to_global_float(view,
429 view->surface->width, 0,
430 &x, &y);
431
Derek Foreman612341f2015-04-15 12:23:55 -0500432 *width = (int)x - output->x;
433 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200434 return;
435
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800436 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
437 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200438 weston_view_to_global_float(view,
439 0, view->surface->height,
440 &x, &y);
441
Derek Foreman612341f2015-04-15 12:23:55 -0500442 *width = view->surface->width + (int)x - output->x;
443 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200444 return;
445
446 default:
447 /* we've already set width and height to
448 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700449 break;
450 }
451 }
452
Jonny Lambd73c6942014-08-20 15:53:20 +0200453 /* the correct view wasn't found */
454}
455
456static void
457get_output_work_area(struct desktop_shell *shell,
458 struct weston_output *output,
459 pixman_rectangle32_t *area)
460{
461 int32_t panel_width = 0, panel_height = 0;
462
Derek Foremanf814c5d2015-04-15 12:23:54 -0500463 area->x = output->x;
464 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200465
466 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200467 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800468 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200469 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500470 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800471 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200472 area->width = output->width;
473 area->height = output->height - panel_height;
474 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800475 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500476 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800477 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200478 area->width = output->width - panel_width;
479 area->height = output->height;
480 break;
481 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700482}
483
Derek Foreman45a7c272015-09-11 14:27:40 -0500484static struct shell_surface *
485find_toplevel_surface(struct shell_surface *in_surface)
486{
487 struct shell_surface *surface = in_surface;
488
Ben Hummon445e44c2015-10-06 11:48:14 -0500489 while (surface) {
490 if (surface->type == SHELL_SURFACE_TOPLEVEL)
491 return surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500492 surface = get_shell_surface(surface->parent);
Ben Hummon445e44c2015-10-06 11:48:14 -0500493 }
Derek Foreman45a7c272015-09-11 14:27:40 -0500494
495 /* If no top level surface was found, just use whatever surface was
496 originally provided. */
Ben Hummon445e44c2015-10-06 11:48:14 -0500497 return in_surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500498}
499
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700500static void
501send_configure_for_surface(struct shell_surface *shsurf)
502{
503 int32_t width, height;
504 struct surface_state *state;
505
506 if (shsurf->state_requested)
507 state = &shsurf->requested_state;
508 else if (shsurf->state_changed)
509 state = &shsurf->next_state;
510 else
511 state = &shsurf->state;
512
513 if (state->fullscreen) {
514 width = shsurf->output->width;
515 height = shsurf->output->height;
516 } else if (state->maximized) {
517 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200518 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700519
520 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200521 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700522
Jonny Lambd73c6942014-08-20 15:53:20 +0200523 width = area.width;
524 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900525 } else if (shsurf->resize_edges) {
526 width = shsurf->geometry.width;
527 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700528 } else {
529 width = 0;
530 height = 0;
531 }
532
533 shsurf->client->send_configure(shsurf->surface, width, height);
534}
535
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300536static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400537shell_surface_state_changed(struct shell_surface *shsurf)
538{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700539 if (shell_surface_is_xdg_surface(shsurf))
540 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400541}
542
543static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300544shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300545{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700546 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400547 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700548 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400549
550 if (grab->shsurf->resize_edges) {
551 grab->shsurf->resize_edges = 0;
552 shell_surface_state_changed(grab->shsurf);
553 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700554 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300555
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700556 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300557}
558
559static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700560shell_touch_grab_start(struct shell_touch_grab *grab,
561 const struct weston_touch_grab_interface *interface,
562 struct shell_surface *shsurf,
563 struct weston_touch *touch)
564{
565 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500566 struct weston_pointer *pointer = weston_seat_get_pointer(touch->seat);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800567
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200568 touch_popup_grab_end(touch);
Derek Foreman1281a362015-07-31 16:55:32 -0500569 if (pointer)
570 popup_grab_end(pointer);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700571
Rusty Lynch1084da52013-08-15 09:10:08 -0700572 grab->grab.interface = interface;
573 grab->shsurf = shsurf;
574 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
575 wl_signal_add(&shsurf->destroy_signal,
576 &grab->shsurf_destroy_listener);
577
578 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700579 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700580
581 weston_touch_start_grab(touch, &grab->grab);
582 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500583 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500584 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700585}
586
587static void
588shell_touch_grab_end(struct shell_touch_grab *grab)
589{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700590 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700591 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700592 grab->shsurf->grabbed = 0;
593 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700594
595 weston_touch_end_grab(grab->touch);
596}
597
598static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500599center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800600 struct weston_output *output);
601
Daniel Stone496ca172012-05-30 16:31:42 +0100602static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300603get_modifier(char *modifier)
604{
605 if (!modifier)
606 return MODIFIER_SUPER;
607
608 if (!strcmp("ctrl", modifier))
609 return MODIFIER_CTRL;
610 else if (!strcmp("alt", modifier))
611 return MODIFIER_ALT;
612 else if (!strcmp("super", modifier))
613 return MODIFIER_SUPER;
614 else
615 return MODIFIER_SUPER;
616}
617
Juan Zhaoe10d2792012-04-25 19:09:52 +0800618static enum animation_type
619get_animation_type(char *animation)
620{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800621 if (!animation)
622 return ANIMATION_NONE;
623
Juan Zhaoe10d2792012-04-25 19:09:52 +0800624 if (!strcmp("zoom", animation))
625 return ANIMATION_ZOOM;
626 else if (!strcmp("fade", animation))
627 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100628 else if (!strcmp("dim-layer", animation))
629 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800630 else
631 return ANIMATION_NONE;
632}
633
Alex Wu4539b082012-03-01 12:57:46 +0800634static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400635shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200636{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400637 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500638 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300639 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200640
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400641 section = weston_config_get_section(shell->compositor->config,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400642 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300643 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
644 WESTON_SHELL_CLIENT);
645 if (ret < 0)
646 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400647 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500648 "client", &s, client);
649 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100650 shell->client = s;
651 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400652 "binding-modifier", &s, "super");
653 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200654 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800655
656 weston_config_section_get_string(section,
657 "exposay-modifier", &s, "none");
658 if (strcmp(s, "none") == 0)
659 shell->exposay_modifier = 0;
660 else
661 shell->exposay_modifier = get_modifier(s);
662 free(s);
663
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400664 weston_config_section_get_string(section, "animation", &s, "none");
665 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200666 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200667 weston_config_section_get_string(section, "close-animation", &s, "fade");
668 shell->win_close_animation_type = get_animation_type(s);
669 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700670 weston_config_section_get_string(section,
671 "startup-animation", &s, "fade");
672 shell->startup_animation_type = get_animation_type(s);
673 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700674 if (shell->startup_animation_type == ANIMATION_ZOOM)
675 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100676 weston_config_section_get_string(section, "focus-animation", &s, "none");
677 shell->focus_animation_type = get_animation_type(s);
678 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400679 weston_config_section_get_uint(section, "num-workspaces",
680 &shell->workspaces.num,
681 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200682}
683
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800684struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100685get_default_output(struct weston_compositor *compositor)
686{
687 return container_of(compositor->output_list.next,
688 struct weston_output, link);
689}
690
Pekka Paalanen8274d902014-08-06 19:36:51 +0300691static int
692focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
693{
694 return snprintf(buf, len, "focus highlight effect for output %s",
695 surface->output->name);
696}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100697
698/* no-op func for checking focus surface */
699static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600700focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100701{
702}
703
704static struct focus_surface *
705get_focus_surface(struct weston_surface *surface)
706{
707 if (surface->configure == focus_surface_configure)
708 return surface->configure_private;
709 else
710 return NULL;
711}
712
713static bool
714is_focus_surface (struct weston_surface *es)
715{
716 return (es->configure == focus_surface_configure);
717}
718
719static bool
720is_focus_view (struct weston_view *view)
721{
722 return is_focus_surface (view->surface);
723}
724
725static struct focus_surface *
726create_focus_surface(struct weston_compositor *ec,
727 struct weston_output *output)
728{
729 struct focus_surface *fsurf = NULL;
730 struct weston_surface *surface = NULL;
731
732 fsurf = malloc(sizeof *fsurf);
733 if (!fsurf)
734 return NULL;
735
736 fsurf->surface = weston_surface_create(ec);
737 surface = fsurf->surface;
738 if (surface == NULL) {
739 free(fsurf);
740 return NULL;
741 }
742
743 surface->configure = focus_surface_configure;
744 surface->output = output;
745 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300746 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100747
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800748 fsurf->view = weston_view_create(surface);
749 if (fsurf->view == NULL) {
750 weston_surface_destroy(surface);
751 free(fsurf);
752 return NULL;
753 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100754 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100755
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600756 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600757 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100758 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
759 pixman_region32_fini(&surface->opaque);
760 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
761 output->width, output->height);
762 pixman_region32_fini(&surface->input);
763 pixman_region32_init(&surface->input);
764
765 wl_list_init(&fsurf->workspace_transform.link);
766
767 return fsurf;
768}
769
770static void
771focus_surface_destroy(struct focus_surface *fsurf)
772{
773 weston_surface_destroy(fsurf->surface);
774 free(fsurf);
775}
776
777static void
778focus_animation_done(struct weston_view_animation *animation, void *data)
779{
780 struct workspace *ws = data;
781
782 ws->focus_animation = NULL;
783}
784
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200785static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200786focus_state_destroy(struct focus_state *state)
787{
788 wl_list_remove(&state->seat_destroy_listener.link);
789 wl_list_remove(&state->surface_destroy_listener.link);
790 free(state);
791}
792
793static void
794focus_state_seat_destroy(struct wl_listener *listener, void *data)
795{
796 struct focus_state *state = container_of(listener,
797 struct focus_state,
798 seat_destroy_listener);
799
800 wl_list_remove(&state->link);
801 focus_state_destroy(state);
802}
803
804static void
805focus_state_surface_destroy(struct wl_listener *listener, void *data)
806{
807 struct focus_state *state = container_of(listener,
808 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400809 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400810 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500811 struct weston_surface *main_surface, *next;
812 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200813
Pekka Paalanen01388e22013-04-25 13:57:44 +0300814 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
815
Kristian Høgsberge3778222012-07-31 17:29:30 -0400816 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300817 wl_list_for_each(view,
818 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500819 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400820 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100821 if (is_focus_view(view))
822 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400823
Jason Ekstranda7af7042013-10-12 22:38:11 -0500824 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400825 break;
826 }
827
Pekka Paalanen01388e22013-04-25 13:57:44 +0300828 /* if the focus was a sub-surface, activate its main surface */
829 if (main_surface != state->keyboard_focus)
830 next = main_surface;
831
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100832 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400833 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100834 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100835 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400836 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
838 if (state->ws->focus_animation)
839 weston_view_animation_destroy(state->ws->focus_animation);
840
841 state->ws->focus_animation = weston_fade_run(
842 state->ws->fsurf_front->view,
843 state->ws->fsurf_front->view->alpha, 0.0, 300,
844 focus_animation_done, state->ws);
845 }
846
Kristian Høgsberge3778222012-07-31 17:29:30 -0400847 wl_list_remove(&state->link);
848 focus_state_destroy(state);
849 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200850}
851
852static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400853focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200854{
Jonas Ådahl04769742012-06-13 00:01:24 +0200855 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200856
857 state = malloc(sizeof *state);
858 if (state == NULL)
859 return NULL;
860
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100861 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400862 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200863 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400864 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200865
866 state->seat_destroy_listener.notify = focus_state_seat_destroy;
867 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400868 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200869 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400870 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200871
872 return state;
873}
874
Jonas Ådahl8538b222012-08-29 22:13:03 +0200875static struct focus_state *
876ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
877{
878 struct workspace *ws = get_current_workspace(shell);
879 struct focus_state *state;
880
881 wl_list_for_each(state, &ws->focus_list, link)
882 if (state->seat == seat)
883 break;
884
885 if (&state->link == &ws->focus_list)
886 state = focus_state_create(seat, ws);
887
888 return state;
889}
890
Jonas Ådahl04769742012-06-13 00:01:24 +0200891static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800892focus_state_set_focus(struct focus_state *state,
893 struct weston_surface *surface)
894{
895 if (state->keyboard_focus) {
896 wl_list_remove(&state->surface_destroy_listener.link);
897 wl_list_init(&state->surface_destroy_listener.link);
898 }
899
900 state->keyboard_focus = surface;
901 if (surface)
902 wl_signal_add(&surface->destroy_signal,
903 &state->surface_destroy_listener);
904}
905
906static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400907restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200908{
909 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400910 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100911 struct wl_list pending_seat_list;
912 struct weston_seat *seat, *next_seat;
913
914 /* Temporarily steal the list of seats so that we can keep
915 * track of the seats we've already processed */
916 wl_list_init(&pending_seat_list);
917 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
918 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200919
920 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500921 struct weston_keyboard *keyboard =
922 weston_seat_get_keyboard(state->seat);
923
Neil Roberts4237f502014-04-09 16:33:31 +0100924 wl_list_remove(&state->seat->link);
925 wl_list_insert(&shell->compositor->seat_list,
926 &state->seat->link);
927
Derek Foreman1281a362015-07-31 16:55:32 -0500928 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800929 continue;
930
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400931 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200932
Derek Foreman1281a362015-07-31 16:55:32 -0500933 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200934 }
Neil Roberts4237f502014-04-09 16:33:31 +0100935
936 /* For any remaining seats that we don't have a focus state
937 * for we'll reset the keyboard focus to NULL */
938 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500939 struct weston_keyboard *keyboard =
940 weston_seat_get_keyboard(seat);
941
Neil Roberts4237f502014-04-09 16:33:31 +0100942 wl_list_insert(&shell->compositor->seat_list, &seat->link);
943
Derek Foreman1281a362015-07-31 16:55:32 -0500944 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100945 continue;
946
Derek Foreman1281a362015-07-31 16:55:32 -0500947 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100948 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200949}
950
951static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200952replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
953 struct weston_seat *seat)
954{
Derek Foreman1281a362015-07-31 16:55:32 -0500955 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200956 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200957
958 wl_list_for_each(state, &ws->focus_list, link) {
959 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500960 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200961 return;
962 }
963 }
964}
965
966static void
967drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
968 struct weston_surface *surface)
969{
970 struct focus_state *state;
971
972 wl_list_for_each(state, &ws->focus_list, link)
973 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800974 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200975}
976
977static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100978animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
979 struct weston_view *from, struct weston_view *to)
980{
981 struct weston_output *output;
982 bool focus_surface_created = false;
983
984 /* FIXME: Only support dim animation using two layers */
985 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
986 return;
987
988 output = get_default_output(shell->compositor);
989 if (ws->fsurf_front == NULL && (from || to)) {
990 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800991 if (ws->fsurf_front == NULL)
992 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100993 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800994
995 ws->fsurf_back = create_focus_surface(shell->compositor, output);
996 if (ws->fsurf_back == NULL) {
997 focus_surface_destroy(ws->fsurf_front);
998 return;
999 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001000 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001001
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001002 focus_surface_created = true;
1003 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001004 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1005 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001006 }
1007
1008 if (ws->focus_animation) {
1009 weston_view_animation_destroy(ws->focus_animation);
1010 ws->focus_animation = NULL;
1011 }
1012
1013 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001014 weston_layer_entry_insert(&to->layer_link,
1015 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001016 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001017 weston_layer_entry_insert(&ws->layer.view_list,
1018 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001019
1020 if (focus_surface_created) {
1021 ws->focus_animation = weston_fade_run(
1022 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001023 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001024 focus_animation_done, ws);
1025 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001026 weston_layer_entry_insert(&from->layer_link,
1027 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001028 ws->focus_animation = weston_stable_fade_run(
1029 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001030 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001031 focus_animation_done, ws);
1032 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001033 weston_layer_entry_insert(&ws->layer.view_list,
1034 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001035 ws->focus_animation = weston_stable_fade_run(
1036 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001037 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001038 focus_animation_done, ws);
1039 }
1040}
1041
1042static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001043workspace_destroy(struct workspace *ws)
1044{
Jonas Ådahl04769742012-06-13 00:01:24 +02001045 struct focus_state *state, *next;
1046
1047 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1048 focus_state_destroy(state);
1049
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001050 if (ws->fsurf_front)
1051 focus_surface_destroy(ws->fsurf_front);
1052 if (ws->fsurf_back)
1053 focus_surface_destroy(ws->fsurf_back);
1054
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001055 free(ws);
1056}
1057
Jonas Ådahl04769742012-06-13 00:01:24 +02001058static void
1059seat_destroyed(struct wl_listener *listener, void *data)
1060{
1061 struct weston_seat *seat = data;
1062 struct focus_state *state, *next;
1063 struct workspace *ws = container_of(listener,
1064 struct workspace,
1065 seat_destroyed_listener);
1066
1067 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1068 if (state->seat == seat)
1069 wl_list_remove(&state->link);
1070}
1071
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001072static struct workspace *
1073workspace_create(void)
1074{
1075 struct workspace *ws = malloc(sizeof *ws);
1076 if (ws == NULL)
1077 return NULL;
1078
1079 weston_layer_init(&ws->layer, NULL);
1080
Jonas Ådahl04769742012-06-13 00:01:24 +02001081 wl_list_init(&ws->focus_list);
1082 wl_list_init(&ws->seat_destroyed_listener.link);
1083 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001084 ws->fsurf_front = NULL;
1085 ws->fsurf_back = NULL;
1086 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001087
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001088 return ws;
1089}
1090
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001091static int
1092workspace_is_empty(struct workspace *ws)
1093{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001094 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001095}
1096
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001097static struct workspace *
1098get_workspace(struct desktop_shell *shell, unsigned int index)
1099{
1100 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001101 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001102 pws += index;
1103 return *pws;
1104}
1105
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001106struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001107get_current_workspace(struct desktop_shell *shell)
1108{
1109 return get_workspace(shell, shell->workspaces.current);
1110}
1111
1112static void
1113activate_workspace(struct desktop_shell *shell, unsigned int index)
1114{
1115 struct workspace *ws;
1116
1117 ws = get_workspace(shell, index);
1118 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1119
1120 shell->workspaces.current = index;
1121}
1122
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001123static unsigned int
1124get_output_height(struct weston_output *output)
1125{
1126 return abs(output->region.extents.y1 - output->region.extents.y2);
1127}
1128
1129static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001130view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001131{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001132 struct weston_transform *transform;
1133
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001134 if (is_focus_view(view)) {
1135 struct focus_surface *fsurf = get_focus_surface(view->surface);
1136 transform = &fsurf->workspace_transform;
1137 } else {
1138 struct shell_surface *shsurf = get_shell_surface(view->surface);
1139 transform = &shsurf->workspace_transform;
1140 }
1141
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001142 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001144 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001145
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001146 weston_matrix_init(&transform->matrix);
1147 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001148 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001149 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001150}
1151
1152static void
1153workspace_translate_out(struct workspace *ws, double fraction)
1154{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001156 unsigned int height;
1157 double d;
1158
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001159 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001161 d = height * fraction;
1162
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001163 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001164 }
1165}
1166
1167static void
1168workspace_translate_in(struct workspace *ws, double fraction)
1169{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001171 unsigned int height;
1172 double d;
1173
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001174 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001176
1177 if (fraction > 0)
1178 d = -(height - height * fraction);
1179 else
1180 d = height + height * fraction;
1181
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001182 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001183 }
1184}
1185
1186static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001187broadcast_current_workspace_state(struct desktop_shell *shell)
1188{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001189 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001190
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001191 wl_resource_for_each(resource, &shell->workspaces.client_list)
1192 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001193 shell->workspaces.current,
1194 shell->workspaces.num);
1195}
1196
1197static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001198reverse_workspace_change_animation(struct desktop_shell *shell,
1199 unsigned int index,
1200 struct workspace *from,
1201 struct workspace *to)
1202{
1203 shell->workspaces.current = index;
1204
1205 shell->workspaces.anim_to = to;
1206 shell->workspaces.anim_from = from;
1207 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1208 shell->workspaces.anim_timestamp = 0;
1209
Scott Moreau4272e632012-08-13 09:58:41 -06001210 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001211}
1212
1213static void
1214workspace_deactivate_transforms(struct workspace *ws)
1215{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001217 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001218
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001219 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001220 if (is_focus_view(view)) {
1221 struct focus_surface *fsurf = get_focus_surface(view->surface);
1222 transform = &fsurf->workspace_transform;
1223 } else {
1224 struct shell_surface *shsurf = get_shell_surface(view->surface);
1225 transform = &shsurf->workspace_transform;
1226 }
1227
1228 if (!wl_list_empty(&transform->link)) {
1229 wl_list_remove(&transform->link);
1230 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001231 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001233 }
1234}
1235
1236static void
1237finish_workspace_change_animation(struct desktop_shell *shell,
1238 struct workspace *from,
1239 struct workspace *to)
1240{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001241 struct weston_view *view;
1242
Scott Moreau4272e632012-08-13 09:58:41 -06001243 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001244
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001245 /* Views that extend past the bottom of the output are still
1246 * visible after the workspace animation ends but before its layer
1247 * is hidden. In that case, we need to damage below those views so
1248 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001249 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001250 weston_view_damage_below(view);
1251
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001252 wl_list_remove(&shell->workspaces.animation.link);
1253 workspace_deactivate_transforms(from);
1254 workspace_deactivate_transforms(to);
1255 shell->workspaces.anim_to = NULL;
1256
1257 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1258}
1259
1260static void
1261animate_workspace_change_frame(struct weston_animation *animation,
1262 struct weston_output *output, uint32_t msecs)
1263{
1264 struct desktop_shell *shell =
1265 container_of(animation, struct desktop_shell,
1266 workspaces.animation);
1267 struct workspace *from = shell->workspaces.anim_from;
1268 struct workspace *to = shell->workspaces.anim_to;
1269 uint32_t t;
1270 double x, y;
1271
1272 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1273 finish_workspace_change_animation(shell, from, to);
1274 return;
1275 }
1276
1277 if (shell->workspaces.anim_timestamp == 0) {
1278 if (shell->workspaces.anim_current == 0.0)
1279 shell->workspaces.anim_timestamp = msecs;
1280 else
1281 shell->workspaces.anim_timestamp =
1282 msecs -
1283 /* Invers of movement function 'y' below. */
1284 (asin(1.0 - shell->workspaces.anim_current) *
1285 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1286 M_2_PI);
1287 }
1288
1289 t = msecs - shell->workspaces.anim_timestamp;
1290
1291 /*
1292 * x = [0, π/2]
1293 * y(x) = sin(x)
1294 */
1295 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1296 y = sin(x);
1297
1298 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001299 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001300
1301 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1302 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1303 shell->workspaces.anim_current = y;
1304
Scott Moreau4272e632012-08-13 09:58:41 -06001305 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001306 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001307 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001308 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001309}
1310
1311static void
1312animate_workspace_change(struct desktop_shell *shell,
1313 unsigned int index,
1314 struct workspace *from,
1315 struct workspace *to)
1316{
1317 struct weston_output *output;
1318
1319 int dir;
1320
1321 if (index > shell->workspaces.current)
1322 dir = -1;
1323 else
1324 dir = 1;
1325
1326 shell->workspaces.current = index;
1327
1328 shell->workspaces.anim_dir = dir;
1329 shell->workspaces.anim_from = from;
1330 shell->workspaces.anim_to = to;
1331 shell->workspaces.anim_current = 0.0;
1332 shell->workspaces.anim_timestamp = 0;
1333
1334 output = container_of(shell->compositor->output_list.next,
1335 struct weston_output, link);
1336 wl_list_insert(&output->animation_list,
1337 &shell->workspaces.animation.link);
1338
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001339 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001340
1341 workspace_translate_in(to, 0);
1342
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001343 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001344
Scott Moreau4272e632012-08-13 09:58:41 -06001345 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001346}
1347
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001348static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001349update_workspace(struct desktop_shell *shell, unsigned int index,
1350 struct workspace *from, struct workspace *to)
1351{
1352 shell->workspaces.current = index;
1353 wl_list_insert(&from->layer.link, &to->layer.link);
1354 wl_list_remove(&from->layer.link);
1355}
1356
1357static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001358change_workspace(struct desktop_shell *shell, unsigned int index)
1359{
1360 struct workspace *from;
1361 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001362 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001363
1364 if (index == shell->workspaces.current)
1365 return;
1366
1367 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001368 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001369 return;
1370
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001371 from = get_current_workspace(shell);
1372 to = get_workspace(shell, index);
1373
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001374 if (shell->workspaces.anim_from == to &&
1375 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001376 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001377 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001378 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001379 return;
1380 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001381
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001382 if (shell->workspaces.anim_to != NULL)
1383 finish_workspace_change_animation(shell,
1384 shell->workspaces.anim_from,
1385 shell->workspaces.anim_to);
1386
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001387 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001388
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001389 if (shell->focus_animation_type != ANIMATION_NONE) {
1390 wl_list_for_each(state, &from->focus_list, link)
1391 if (state->keyboard_focus)
1392 animate_focus_change(shell, from,
1393 get_default_view(state->keyboard_focus), NULL);
1394
1395 wl_list_for_each(state, &to->focus_list, link)
1396 if (state->keyboard_focus)
1397 animate_focus_change(shell, to,
1398 NULL, get_default_view(state->keyboard_focus));
1399 }
1400
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001401 if (workspace_is_empty(to) && workspace_is_empty(from))
1402 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001403 else
1404 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001405
1406 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001407}
1408
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001409static bool
1410workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1411{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001412 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001413 struct wl_list *e;
1414
1415 if (wl_list_empty(list))
1416 return false;
1417
1418 e = list->next;
1419
1420 if (e->next != list)
1421 return false;
1422
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001423 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001424}
1425
1426static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001427surface_keyboard_focus_lost(struct weston_surface *surface)
1428{
1429 struct weston_compositor *compositor = surface->compositor;
1430 struct weston_seat *seat;
1431 struct weston_surface *focus;
1432
1433 wl_list_for_each(seat, &compositor->seat_list, link) {
1434 struct weston_keyboard *keyboard =
1435 weston_seat_get_keyboard(seat);
1436
1437 if (!keyboard)
1438 continue;
1439
1440 focus = weston_surface_get_main_surface(keyboard->focus);
1441 if (focus == surface)
1442 weston_keyboard_set_focus(keyboard, NULL);
1443 }
1444}
1445
1446static void
Philip Withnall659163d2013-11-25 18:01:36 +00001447move_surface_to_workspace(struct desktop_shell *shell,
1448 struct shell_surface *shsurf,
1449 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001450{
1451 struct workspace *from;
1452 struct workspace *to;
Philip Withnall659163d2013-11-25 18:01:36 +00001453 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001454
1455 if (workspace == shell->workspaces.current)
1456 return;
1457
Philip Withnall659163d2013-11-25 18:01:36 +00001458 view = get_default_view(shsurf->surface);
1459 if (!view)
1460 return;
1461
1462 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1463
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001464 if (workspace >= shell->workspaces.num)
1465 workspace = shell->workspaces.num - 1;
1466
Jonas Ådahle9d22502012-08-29 22:13:01 +02001467 from = get_current_workspace(shell);
1468 to = get_workspace(shell, workspace);
1469
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001470 weston_layer_entry_remove(&view->layer_link);
1471 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001472
Philip Withnall648a4dd2013-11-25 18:01:44 +00001473 shell_surface_update_child_surface_layers(shsurf);
1474
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 drop_focus_state(shell, from, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02001476 surface_keyboard_focus_lost(view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001477
Jason Ekstranda7af7042013-10-12 22:38:11 -05001478 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001479}
1480
1481static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001482take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001483 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001484 unsigned int index)
1485{
Derek Foreman1281a362015-07-31 16:55:32 -05001486 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001487 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001489 struct shell_surface *shsurf;
1490 struct workspace *from;
1491 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001492 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001493
Derek Foreman1281a362015-07-31 16:55:32 -05001494 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495 view = get_default_view(surface);
1496 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001497 index == shell->workspaces.current ||
1498 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001499 return;
1500
1501 from = get_current_workspace(shell);
1502 to = get_workspace(shell, index);
1503
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001504 weston_layer_entry_remove(&view->layer_link);
1505 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001506
Philip Withnall659163d2013-11-25 18:01:36 +00001507 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001508 if (shsurf != NULL)
1509 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001510
Jonas Ådahle9d22502012-08-29 22:13:01 +02001511 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001512 drop_focus_state(shell, from, surface);
1513
1514 if (shell->workspaces.anim_from == to &&
1515 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001516 wl_list_remove(&to->layer.link);
1517 wl_list_insert(from->layer.link.prev, &to->layer.link);
1518
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001519 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001520 broadcast_current_workspace_state(shell);
1521
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001522 return;
1523 }
1524
1525 if (shell->workspaces.anim_to != NULL)
1526 finish_workspace_change_animation(shell,
1527 shell->workspaces.anim_from,
1528 shell->workspaces.anim_to);
1529
1530 if (workspace_is_empty(from) &&
1531 workspace_has_only(to, surface))
1532 update_workspace(shell, index, from, to);
1533 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001534 if (shsurf != NULL &&
1535 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001536 wl_list_insert(&shell->workspaces.anim_sticky_list,
1537 &shsurf->workspace_transform.link);
1538
1539 animate_workspace_change(shell, index, from, to);
1540 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001541
1542 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001543
1544 state = ensure_focus_state(shell, seat);
1545 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001546 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001547}
1548
1549static void
1550workspace_manager_move_surface(struct wl_client *client,
1551 struct wl_resource *resource,
1552 struct wl_resource *surface_resource,
1553 uint32_t workspace)
1554{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001555 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001556 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001557 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001558 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001559 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001560
Pekka Paalanen01388e22013-04-25 13:57:44 +03001561 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001562 shell_surface = get_shell_surface(main_surface);
1563 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001565
1566 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001567}
1568
1569static const struct workspace_manager_interface workspace_manager_implementation = {
1570 workspace_manager_move_surface,
1571};
1572
1573static void
1574unbind_resource(struct wl_resource *resource)
1575{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001576 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001577}
1578
1579static void
1580bind_workspace_manager(struct wl_client *client,
1581 void *data, uint32_t version, uint32_t id)
1582{
1583 struct desktop_shell *shell = data;
1584 struct wl_resource *resource;
1585
Jason Ekstranda85118c2013-06-27 20:17:02 -05001586 resource = wl_resource_create(client,
1587 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001588
1589 if (resource == NULL) {
1590 weston_log("couldn't add workspace manager object");
1591 return;
1592 }
1593
Jason Ekstranda85118c2013-06-27 20:17:02 -05001594 wl_resource_set_implementation(resource,
1595 &workspace_manager_implementation,
1596 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001597 wl_list_insert(&shell->workspaces.client_list,
1598 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001599
1600 workspace_manager_send_state(resource,
1601 shell->workspaces.current,
1602 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001603}
1604
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001605static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001606touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001607 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001608{
1609}
1610
1611static void
1612touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1613{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001614 struct weston_touch_move_grab *move =
1615 (struct weston_touch_move_grab *) container_of(
1616 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001617
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001618 if (touch_id == 0)
1619 move->active = 0;
1620
Jonas Ådahl9484b692013-12-02 22:05:03 +01001621 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001622 shell_touch_grab_end(&move->base);
1623 free(move);
1624 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001625}
1626
1627static void
1628touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001629 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001630{
1631 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1632 struct shell_surface *shsurf = move->base.shsurf;
1633 struct weston_surface *es;
1634 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1635 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1636
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001637 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001638 return;
1639
1640 es = shsurf->surface;
1641
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001642 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001643
1644 weston_compositor_schedule_repaint(es->compositor);
1645}
1646
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001647static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001648touch_move_grab_frame(struct weston_touch_grab *grab)
1649{
1650}
1651
1652static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001653touch_move_grab_cancel(struct weston_touch_grab *grab)
1654{
1655 struct weston_touch_move_grab *move =
1656 (struct weston_touch_move_grab *) container_of(
1657 grab, struct shell_touch_grab, grab);
1658
1659 shell_touch_grab_end(&move->base);
1660 free(move);
1661}
1662
Rusty Lynch1084da52013-08-15 09:10:08 -07001663static const struct weston_touch_grab_interface touch_move_grab_interface = {
1664 touch_move_grab_down,
1665 touch_move_grab_up,
1666 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001667 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001668 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001669};
1670
1671static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001672surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001673{
1674 struct weston_touch_move_grab *move;
1675
1676 if (!shsurf)
1677 return -1;
1678
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001679 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001680 return 0;
1681
1682 move = malloc(sizeof *move);
1683 if (!move)
1684 return -1;
1685
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001686 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001687 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001688 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001689 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001690 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001691
1692 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001693 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001694
1695 return 0;
1696}
1697
1698static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001699noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001700{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001701}
1702
1703static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001704noop_grab_axis(struct weston_pointer_grab *grab,
1705 uint32_t time, uint32_t axis, wl_fixed_t value)
1706{
1707}
1708
1709static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001710constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1711{
1712 struct shell_surface *shsurf = move->base.shsurf;
1713 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001714 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001715 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001716 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001717
1718 x = wl_fixed_to_int(pointer->x + move->dx);
1719 y = wl_fixed_to_int(pointer->y + move->dy);
1720
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001721 if (shsurf->shell->panel_position ==
1722 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001723 get_output_work_area(shsurf->shell,
1724 shsurf->surface->output,
1725 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001726
Derek Foreman6feb0f92015-04-15 12:23:56 -05001727 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1728 if (bottom - safety < area.y)
1729 y = area.y + safety - shsurf->geometry.height
1730 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001731
1732 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001733 y + shsurf->geometry.y < area.y)
1734 y = area.y - shsurf->geometry.y;
1735
Jonny Lambd73c6942014-08-20 15:53:20 +02001736 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001737
1738 *cx = x;
1739 *cy = y;
1740}
1741
1742static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001743move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1744 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001745{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001746 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001747 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001748 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001749 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001750
1751 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001752 if (!shsurf)
1753 return;
1754
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001755 constrain_position(move, &cx, &cy);
1756
1757 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001758
Jason Ekstranda7af7042013-10-12 22:38:11 -05001759 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001760}
1761
1762static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001763move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001764 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001765{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001766 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1767 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001768 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001769 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001770
Daniel Stone4dbadb12012-05-30 16:31:51 +01001771 if (pointer->button_count == 0 &&
1772 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001773 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001774 free(grab);
1775 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001776}
1777
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001778static void
1779move_grab_cancel(struct weston_pointer_grab *grab)
1780{
1781 struct shell_grab *shell_grab =
1782 container_of(grab, struct shell_grab, grab);
1783
1784 shell_grab_end(shell_grab);
1785 free(grab);
1786}
1787
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001788static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001789 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001790 move_grab_motion,
1791 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001792 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001793 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001794};
1795
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001796static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001797surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001798 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001799{
1800 struct weston_move_grab *move;
1801
1802 if (!shsurf)
1803 return -1;
1804
Derek Foreman45a7c272015-09-11 14:27:40 -05001805 shsurf = find_toplevel_surface(shsurf);
1806
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001807 if (shsurf->grabbed ||
1808 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001809 return 0;
1810
1811 move = malloc(sizeof *move);
1812 if (!move)
1813 return -1;
1814
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001816 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001817 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001818 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001819 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001820
1821 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001822 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001823
1824 return 0;
1825}
1826
1827static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001828common_surface_move(struct wl_resource *resource,
1829 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001830{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001831 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001832 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1833 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001834 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001835 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001836
Derek Foreman1281a362015-07-31 16:55:32 -05001837 if (pointer &&
1838 pointer->focus &&
1839 pointer->button_count > 0 &&
1840 pointer->grab_serial == serial) {
1841 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001842 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001843 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001844 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001845 } else if (touch &&
1846 touch->focus &&
1847 touch->grab_serial == serial) {
1848 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001849 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001850 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001851 wl_resource_post_no_memory(resource);
1852 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001853}
1854
Rafael Antognollie2a34552013-12-03 15:35:45 -02001855static void
1856shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1857 struct wl_resource *seat_resource, uint32_t serial)
1858{
1859 common_surface_move(resource, seat_resource, serial);
1860}
1861
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001862struct weston_resize_grab {
1863 struct shell_grab base;
1864 uint32_t edges;
1865 int32_t width, height;
1866};
1867
1868static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001869resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1870 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001871{
1872 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001873 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001874 struct shell_surface *shsurf = resize->base.shsurf;
1875 int32_t width, height;
1876 wl_fixed_t from_x, from_y;
1877 wl_fixed_t to_x, to_y;
1878
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001879 weston_pointer_move(pointer, x, y);
1880
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001881 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001882 return;
1883
Jason Ekstranda7af7042013-10-12 22:38:11 -05001884 weston_view_from_global_fixed(shsurf->view,
1885 pointer->grab_x, pointer->grab_y,
1886 &from_x, &from_y);
1887 weston_view_from_global_fixed(shsurf->view,
1888 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001889
1890 width = resize->width;
1891 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1892 width += wl_fixed_to_int(from_x - to_x);
1893 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1894 width += wl_fixed_to_int(to_x - from_x);
1895 }
1896
1897 height = resize->height;
1898 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1899 height += wl_fixed_to_int(from_y - to_y);
1900 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1901 height += wl_fixed_to_int(to_y - from_y);
1902 }
1903
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001904 if (width < 1)
1905 width = 1;
1906 if (height < 1)
1907 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001908 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001909}
1910
1911static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001912send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001913{
1914 struct shell_surface *shsurf = get_shell_surface(surface);
1915
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001916 assert(shsurf);
1917
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001918 if (shsurf->resource)
1919 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001920 shsurf->resize_edges,
1921 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001922}
1923
1924static const struct weston_shell_client shell_client = {
1925 send_configure
1926};
1927
1928static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001929resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001930 uint32_t time, uint32_t button, uint32_t state_w)
1931{
1932 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001933 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001934 enum wl_pointer_button_state state = state_w;
1935
1936 if (pointer->button_count == 0 &&
1937 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1938 shell_grab_end(&resize->base);
1939 free(grab);
1940 }
1941}
1942
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001943static void
1944resize_grab_cancel(struct weston_pointer_grab *grab)
1945{
1946 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1947
1948 shell_grab_end(&resize->base);
1949 free(grab);
1950}
1951
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001952static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001953 noop_grab_focus,
1954 resize_grab_motion,
1955 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001956 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001957 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001958};
1959
Giulio Camuffob8366642013-04-25 13:57:46 +03001960/*
1961 * Returns the bounding box of a surface and all its sub-surfaces,
1962 * in the surface coordinates system. */
1963static void
1964surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1965 int32_t *y, int32_t *w, int32_t *h) {
1966 pixman_region32_t region;
1967 pixman_box32_t *box;
1968 struct weston_subsurface *subsurface;
1969
1970 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001971 surface->width,
1972 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001973
1974 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1975 pixman_region32_union_rect(&region, &region,
1976 subsurface->position.x,
1977 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001978 subsurface->surface->width,
1979 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001980 }
1981
1982 box = pixman_region32_extents(&region);
1983 if (x)
1984 *x = box->x1;
1985 if (y)
1986 *y = box->y1;
1987 if (w)
1988 *w = box->x2 - box->x1;
1989 if (h)
1990 *h = box->y2 - box->y1;
1991
1992 pixman_region32_fini(&region);
1993}
1994
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001995static int
1996surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001997 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001998{
1999 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02002000 const unsigned resize_topbottom =
2001 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
2002 const unsigned resize_leftright =
2003 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
2004 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002005
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07002006 if (shsurf->grabbed ||
2007 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002008 return 0;
2009
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02002010 /* Check for invalid edge combinations. */
2011 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
2012 (edges & resize_topbottom) == resize_topbottom ||
2013 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002014 return 0;
2015
2016 resize = malloc(sizeof *resize);
2017 if (!resize)
2018 return -1;
2019
2020 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002021
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002022 resize->width = shsurf->geometry.width;
2023 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04002024
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08002025 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04002026 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002027 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05002028 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002029
2030 return 0;
2031}
2032
2033static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002034common_surface_resize(struct wl_resource *resource,
2035 struct wl_resource *seat_resource, uint32_t serial,
2036 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002037{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002038 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002039 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002040 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03002041 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002042
Derek Foreman1281a362015-07-31 16:55:32 -05002043 if (!pointer ||
2044 pointer->button_count == 0 ||
2045 pointer->grab_serial != serial ||
2046 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08002047 return;
2048
Derek Foreman1281a362015-07-31 16:55:32 -05002049 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08002050 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002051 return;
2052
Derek Foreman1281a362015-07-31 16:55:32 -05002053 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002054 wl_resource_post_no_memory(resource);
2055}
2056
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002057static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002058shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
2059 struct wl_resource *seat_resource, uint32_t serial,
2060 uint32_t edges)
2061{
2062 common_surface_resize(resource, seat_resource, serial, edges);
2063}
2064
2065static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002066busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002067{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002068 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002069 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002071 wl_fixed_t sx, sy;
2072
Jason Ekstranda7af7042013-10-12 22:38:11 -05002073 view = weston_compositor_pick_view(pointer->seat->compositor,
2074 pointer->x, pointer->y,
2075 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002076
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002077 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2078 shell_grab_end(grab);
2079 free(grab);
2080 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002081}
2082
2083static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002084busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2085 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002086{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002087 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002088}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002089
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002090static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002091busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002092 uint32_t time, uint32_t button, uint32_t state)
2093{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002094 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002095 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002096 struct weston_pointer *pointer = grab->grab.pointer;
2097 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002098
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002099 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002100 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002101 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002102 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002103 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002104 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002105 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002106}
2107
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002108static void
2109busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2110{
2111 struct shell_grab *grab = (struct shell_grab *) base;
2112
2113 shell_grab_end(grab);
2114 free(grab);
2115}
2116
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002117static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002118 busy_cursor_grab_focus,
2119 busy_cursor_grab_motion,
2120 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002121 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002122 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002123};
2124
2125static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002126set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002127{
2128 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002129
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002130 if (pointer->grab->interface == &busy_cursor_grab_interface)
2131 return;
2132
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002133 grab = malloc(sizeof *grab);
2134 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002135 return;
2136
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002137 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002138 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002139 /* Mark the shsurf as ungrabbed so that button binding is able
2140 * to move it. */
2141 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002142}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002143
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002144static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002145end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002146{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002147 struct shell_grab *grab;
2148 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002149
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002150 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002151 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2152
2153 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002154 continue;
2155
Derek Foreman1281a362015-07-31 16:55:32 -05002156 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002157 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002158 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002159 wl_resource_get_client(grab->shsurf->resource) == client) {
2160 shell_grab_end(grab);
2161 free(grab);
2162 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002163 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002164}
2165
Scott Moreau9521d5e2012-04-19 13:06:17 -06002166static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002167handle_shell_client_destroy(struct wl_listener *listener, void *data);
2168
2169static int
2170xdg_ping_timeout_handler(void *data)
2171{
2172 struct shell_client *sc = data;
2173 struct weston_seat *seat;
2174 struct shell_surface *shsurf;
2175
2176 /* Client is not responding */
2177 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002178 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002179 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2180
2181 if (!pointer ||
2182 !pointer->focus ||
2183 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002184 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002185
Derek Foreman1281a362015-07-31 16:55:32 -05002186 shsurf = get_shell_surface(pointer->focus->surface);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002187 if (shsurf &&
2188 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002189 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002190 }
2191
2192 return 1;
2193}
2194
2195static void
2196handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2197{
2198 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002199 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002200 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002201 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002202
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002203 if (sc->unresponsive) {
2204 xdg_ping_timeout_handler(sc);
2205 return;
2206 }
2207
2208 sc->ping_serial = serial;
2209 loop = wl_display_get_event_loop(compositor->wl_display);
2210 if (sc->ping_timer == NULL)
2211 sc->ping_timer =
2212 wl_event_loop_add_timer(loop,
2213 xdg_ping_timeout_handler, sc);
2214 if (sc->ping_timer == NULL)
2215 return;
2216
2217 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2218
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002219 if (shell_surface_is_xdg_surface(shsurf) ||
2220 shell_surface_is_xdg_popup(shsurf))
2221 xdg_shell_send_ping(sc->resource, serial);
2222 else if (shell_surface_is_wl_shell_surface(shsurf))
2223 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002224}
2225
Scott Moreauff1db4a2012-04-17 19:06:18 -06002226static void
2227ping_handler(struct weston_surface *surface, uint32_t serial)
2228{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002229 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002230
2231 if (!shsurf)
2232 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002233 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002234 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002235 if (shsurf->surface == shsurf->shell->grab_surface)
2236 return;
2237
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002238 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002239}
2240
2241static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002242handle_pointer_focus(struct wl_listener *listener, void *data)
2243{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002244 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002245 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002246 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002247 uint32_t serial;
2248
Jason Ekstranda7af7042013-10-12 22:38:11 -05002249 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002250 return;
2251
Jason Ekstranda7af7042013-10-12 22:38:11 -05002252 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002253 serial = wl_display_next_serial(compositor->wl_display);
2254 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002255}
2256
2257static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002258shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2259{
2260 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002261 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002262}
2263
2264static void
2265shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2266{
2267 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002268 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002269}
2270
2271static void
2272handle_keyboard_focus(struct wl_listener *listener, void *data)
2273{
2274 struct weston_keyboard *keyboard = data;
2275 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2276
2277 if (seat->focused_surface) {
2278 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2279 if (shsurf)
2280 shell_surface_lose_keyboard_focus(shsurf);
2281 }
2282
2283 seat->focused_surface = keyboard->focus;
2284
2285 if (seat->focused_surface) {
2286 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2287 if (shsurf)
2288 shell_surface_gain_keyboard_focus(shsurf);
2289 }
2290}
2291
2292static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002293shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002294{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002295 if (sc->ping_serial != serial)
2296 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002297
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002298 sc->unresponsive = 0;
2299 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002300
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002301 if (sc->ping_timer) {
2302 wl_event_source_remove(sc->ping_timer);
2303 sc->ping_timer = NULL;
2304 }
2305
2306}
2307
2308static void
2309shell_surface_pong(struct wl_client *client,
2310 struct wl_resource *resource, uint32_t serial)
2311{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002312 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2313 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002314
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002315 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002316}
2317
2318static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002319set_title(struct shell_surface *shsurf, const char *title)
2320{
2321 free(shsurf->title);
2322 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002323 shsurf->surface->timeline.force_refresh = 1;
2324}
2325
2326static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002327set_pid(struct shell_surface *shsurf, pid_t pid)
2328{
2329 /* We have no use for it */
2330}
2331
2332static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002333set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2334{
2335 shsurf->type = t;
2336 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002337}
2338
2339static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002340set_window_geometry(struct shell_surface *shsurf,
2341 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002342{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002343 shsurf->next_geometry.x = x;
2344 shsurf->next_geometry.y = y;
2345 shsurf->next_geometry.width = width;
2346 shsurf->next_geometry.height = height;
2347 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002348}
2349
2350static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002351shell_surface_set_title(struct wl_client *client,
2352 struct wl_resource *resource, const char *title)
2353{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002354 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002355
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002356 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002357}
2358
2359static void
2360shell_surface_set_class(struct wl_client *client,
2361 struct wl_resource *resource, const char *class)
2362{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002363 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002364
2365 free(shsurf->class);
2366 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002367 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002368}
2369
Alex Wu4539b082012-03-01 12:57:46 +08002370static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002371restore_output_mode(struct weston_output *output)
2372{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002373 if (output->original_mode)
2374 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002375}
2376
2377static void
2378restore_all_output_modes(struct weston_compositor *compositor)
2379{
2380 struct weston_output *output;
2381
2382 wl_list_for_each(output, &compositor->output_list, link)
2383 restore_output_mode(output);
2384}
2385
Philip Withnall07926d92013-11-25 18:01:40 +00002386/* The surface will be inserted into the list immediately after the link
2387 * returned by this function (i.e. will be stacked immediately above the
2388 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002389static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002390shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2391{
2392 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002393 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002394
2395 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002396 case SHELL_SURFACE_XWAYLAND:
2397 return &shsurf->shell->fullscreen_layer.view_list;
2398
2399 case SHELL_SURFACE_NONE:
2400 return NULL;
2401
Kristian Høgsbergead52422014-01-01 13:51:52 -08002402 case SHELL_SURFACE_POPUP:
2403 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002404 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002405 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002406 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002407 /* Move the surface to its parent layer so
2408 * that surfaces which are transient for
2409 * fullscreen surfaces don't get hidden by the
2410 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002411
2412 /* TODO: Handle a parent with multiple views */
2413 parent = get_default_view(shsurf->parent);
2414 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002415 return container_of(parent->layer_link.link.prev,
2416 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002417 }
Philip Withnall07926d92013-11-25 18:01:40 +00002418
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002419 /* Move the surface to a normal workspace layer so that surfaces
2420 * which were previously fullscreen or transient are no longer
2421 * rendered on top. */
2422 ws = get_current_workspace(shsurf->shell);
2423 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002424 }
2425
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002426 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002427}
2428
Philip Withnall648a4dd2013-11-25 18:01:44 +00002429static void
2430shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2431{
2432 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002433 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002434
2435 /* Move the child layers to the same workspace as shsurf. They will be
2436 * stacked above shsurf. */
2437 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002438 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002439 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002440 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002441 prev = container_of(shsurf->view->layer_link.link.prev,
2442 struct weston_layer_entry, link);
2443 weston_layer_entry_remove(&child->view->layer_link);
2444 weston_layer_entry_insert(prev,
2445 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002446 weston_view_geometry_dirty(child->view);
2447 weston_surface_damage(child->surface);
2448
2449 /* Recurse. We don’t expect this to recurse very far (if
2450 * at all) because that would imply we have transient
2451 * (or popup) children of transient surfaces, which
2452 * would be unusual. */
2453 shell_surface_update_child_surface_layers(child);
2454 }
2455 }
2456}
2457
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002458/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002459 * geometry to ensure the changes are redrawn.
2460 *
2461 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2462 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002463static void
2464shell_surface_update_layer(struct shell_surface *shsurf)
2465{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002466 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002467
2468 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2469
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002470 if (new_layer_link == NULL)
2471 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002472 if (new_layer_link == &shsurf->view->layer_link)
2473 return;
2474
2475 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002476 weston_layer_entry_remove(&shsurf->view->layer_link);
2477 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002478 weston_view_geometry_dirty(shsurf->view);
2479 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002480
2481 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002482}
2483
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002484static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002485shell_surface_set_parent(struct shell_surface *shsurf,
2486 struct weston_surface *parent)
2487{
2488 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002489
2490 wl_list_remove(&shsurf->children_link);
2491 wl_list_init(&shsurf->children_link);
2492
2493 /* Insert into the parent surface’s child list. */
2494 if (parent != NULL) {
2495 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2496 if (parent_shsurf != NULL)
2497 wl_list_insert(&parent_shsurf->children_list,
2498 &shsurf->children_link);
2499 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002500}
2501
2502static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002503shell_surface_set_output(struct shell_surface *shsurf,
2504 struct weston_output *output)
2505{
2506 struct weston_surface *es = shsurf->surface;
2507
2508 /* get the default output, if the client set it as NULL
2509 check whether the ouput is available */
2510 if (output)
2511 shsurf->output = output;
2512 else if (es->output)
2513 shsurf->output = es->output;
2514 else
2515 shsurf->output = get_default_output(es->compositor);
2516}
2517
2518static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002519surface_clear_next_states(struct shell_surface *shsurf)
2520{
2521 shsurf->next_state.maximized = false;
2522 shsurf->next_state.fullscreen = false;
2523
2524 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2525 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2526 shsurf->state_changed = true;
2527}
2528
2529static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002530set_toplevel(struct shell_surface *shsurf)
2531{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002532 shell_surface_set_parent(shsurf, NULL);
2533 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002534 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002535
2536 /* The layer_link is updated in set_surface_type(),
2537 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002538}
2539
2540static void
2541shell_surface_set_toplevel(struct wl_client *client,
2542 struct wl_resource *resource)
2543{
2544 struct shell_surface *surface = wl_resource_get_user_data(resource);
2545
2546 set_toplevel(surface);
2547}
2548
2549static void
2550set_transient(struct shell_surface *shsurf,
2551 struct weston_surface *parent, int x, int y, uint32_t flags)
2552{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002553 assert(parent != NULL);
2554
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002555 shell_surface_set_parent(shsurf, parent);
2556
2557 surface_clear_next_states(shsurf);
2558
Philip Withnallbecb77e2013-11-25 18:01:30 +00002559 shsurf->transient.x = x;
2560 shsurf->transient.y = y;
2561 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002562
Rafael Antognollied207b42013-12-03 15:35:43 -02002563 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002564 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002565 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002566
2567 /* The layer_link is updated in set_surface_type(),
2568 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002569}
2570
2571static void
2572shell_surface_set_transient(struct wl_client *client,
2573 struct wl_resource *resource,
2574 struct wl_resource *parent_resource,
2575 int x, int y, uint32_t flags)
2576{
2577 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2578 struct weston_surface *parent =
2579 wl_resource_get_user_data(parent_resource);
2580
2581 set_transient(shsurf, parent, x, y, flags);
2582}
2583
2584static void
2585set_fullscreen(struct shell_surface *shsurf,
2586 uint32_t method,
2587 uint32_t framerate,
2588 struct weston_output *output)
2589{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002590 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002591 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002592
2593 shsurf->fullscreen_output = shsurf->output;
2594 shsurf->fullscreen.type = method;
2595 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002596
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002597 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002598}
2599
2600static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002601weston_view_set_initial_position(struct weston_view *view,
2602 struct desktop_shell *shell);
2603
2604static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002605unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002606{
Philip Withnallf85fe842013-11-25 18:01:37 +00002607 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002608 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2609 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002610 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002611 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002612
Alex Wu4539b082012-03-01 12:57:46 +08002613 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2614 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002615
Alex Wu4539b082012-03-01 12:57:46 +08002616 wl_list_remove(&shsurf->fullscreen.transform.link);
2617 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002618
Jason Ekstranda7af7042013-10-12 22:38:11 -05002619 if (shsurf->fullscreen.black_view)
2620 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2621 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002622
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002623 if (shsurf->saved_position_valid)
2624 weston_view_set_position(shsurf->view,
2625 shsurf->saved_x, shsurf->saved_y);
2626 else
2627 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2628
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002629 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002630 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002631 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002632 shsurf->saved_rotation_valid = false;
2633 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002634
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002635 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002636}
2637
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002638static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002639shell_surface_set_fullscreen(struct wl_client *client,
2640 struct wl_resource *resource,
2641 uint32_t method,
2642 uint32_t framerate,
2643 struct wl_resource *output_resource)
2644{
2645 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2646 struct weston_output *output;
2647
2648 if (output_resource)
2649 output = wl_resource_get_user_data(output_resource);
2650 else
2651 output = NULL;
2652
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002653 shell_surface_set_parent(shsurf, NULL);
2654
Rafael Antognolli03b16592013-12-03 15:35:42 -02002655 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002656 shsurf->next_state.fullscreen = true;
2657 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002658 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002659}
2660
2661static void
2662set_popup(struct shell_surface *shsurf,
2663 struct weston_surface *parent,
2664 struct weston_seat *seat,
2665 uint32_t serial,
2666 int32_t x,
2667 int32_t y)
2668{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002669 assert(parent != NULL);
2670
Philip Withnallbecb77e2013-11-25 18:01:30 +00002671 shsurf->popup.shseat = get_shell_seat(seat);
2672 shsurf->popup.serial = serial;
2673 shsurf->popup.x = x;
2674 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002675
Pekka Paalanenb5026542014-11-12 15:09:24 +02002676 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002677}
2678
2679static void
2680shell_surface_set_popup(struct wl_client *client,
2681 struct wl_resource *resource,
2682 struct wl_resource *seat_resource,
2683 uint32_t serial,
2684 struct wl_resource *parent_resource,
2685 int32_t x, int32_t y, uint32_t flags)
2686{
2687 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002688 struct weston_surface *parent =
2689 wl_resource_get_user_data(parent_resource);
2690
2691 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002692
Rafael Antognolli03b16592013-12-03 15:35:42 -02002693 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002694 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002695 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002696 wl_resource_get_user_data(seat_resource),
2697 serial, x, y);
2698}
2699
2700static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002701unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002702{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002703 /* undo all maximized things here */
2704 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002705
2706 if (shsurf->saved_position_valid)
2707 weston_view_set_position(shsurf->view,
2708 shsurf->saved_x, shsurf->saved_y);
2709 else
2710 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002711
2712 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713 wl_list_insert(&shsurf->view->geometry.transformation_list,
2714 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002715 shsurf->saved_rotation_valid = false;
2716 }
2717
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002718 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002719}
2720
Philip Withnallbecb77e2013-11-25 18:01:30 +00002721static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002722set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002723{
2724 struct shell_surface *shsurf;
2725 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002726 struct weston_view *view;
2727
2728 view = get_default_view(surface);
2729 if (!view)
2730 return;
2731
2732 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2733
2734 shsurf = get_shell_surface(surface);
2735 current_ws = get_current_workspace(shsurf->shell);
2736
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002737 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002738 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002739
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002740 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002741 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002742
2743 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002744 weston_view_damage_below(view);
2745}
2746
2747static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002748shell_surface_set_maximized(struct wl_client *client,
2749 struct wl_resource *resource,
2750 struct wl_resource *output_resource)
2751{
2752 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2753 struct weston_output *output;
2754
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002755 surface_clear_next_states(shsurf);
2756 shsurf->next_state.maximized = true;
2757 shsurf->state_changed = true;
2758
Pekka Paalanenb5026542014-11-12 15:09:24 +02002759 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002760 shell_surface_set_parent(shsurf, NULL);
2761
Philip Withnallbecb77e2013-11-25 18:01:30 +00002762 if (output_resource)
2763 output = wl_resource_get_user_data(output_resource);
2764 else
2765 output = NULL;
2766
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002767 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002768
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002769 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002770}
2771
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002772/* This is only ever called from set_surface_type(), so there’s no need to
2773 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002774static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002775reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002776{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002777 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002778 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002779 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002780 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002781
Pekka Paalanen98262232011-12-01 10:42:22 +02002782 return 0;
2783}
2784
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002785static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002786set_full_output(struct shell_surface *shsurf)
2787{
2788 shsurf->saved_x = shsurf->view->geometry.x;
2789 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002790 shsurf->saved_width = shsurf->surface->width;
2791 shsurf->saved_height = shsurf->surface->height;
2792 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002793 shsurf->saved_position_valid = true;
2794
2795 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2796 wl_list_remove(&shsurf->rotation.transform.link);
2797 wl_list_init(&shsurf->rotation.transform.link);
2798 weston_view_geometry_dirty(shsurf->view);
2799 shsurf->saved_rotation_valid = true;
2800 }
2801}
2802
2803static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002804set_surface_type(struct shell_surface *shsurf)
2805{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002806 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002807 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002808
Philip Withnallbecb77e2013-11-25 18:01:30 +00002809 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002810
Rafael Antognolli03b16592013-12-03 15:35:42 -02002811 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002812 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002813
2814 switch (shsurf->type) {
2815 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002816 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002817 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002818 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002819 weston_view_set_position(shsurf->view,
2820 pev->geometry.x + shsurf->transient.x,
2821 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002822 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002823 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002824
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002825 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002826 weston_view_set_position(shsurf->view, shsurf->transient.x,
2827 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002828 break;
2829
Philip Withnall0f640e22013-11-25 18:01:31 +00002830 case SHELL_SURFACE_POPUP:
2831 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002832 default:
2833 break;
2834 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002835
2836 /* Update the surface’s layer. */
2837 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002838}
2839
Tiago Vignattibe143262012-04-16 17:31:41 +03002840static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002841shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002842{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002843 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002844}
2845
Pekka Paalanen8274d902014-08-06 19:36:51 +03002846static int
2847black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2848{
2849 struct weston_surface *fs_surface = surface->configure_private;
2850 int n;
2851 int rem;
2852 int ret;
2853
2854 n = snprintf(buf, len, "black background surface for ");
2855 if (n < 0)
2856 return n;
2857
2858 rem = (int)len - n;
2859 if (rem < 0)
2860 rem = 0;
2861
2862 if (fs_surface->get_label)
2863 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2864 else
2865 ret = snprintf(buf + n, rem, "<unknown>");
2866
2867 if (ret < 0)
2868 return n;
2869
2870 return n + ret;
2871}
2872
Alex Wu21858432012-04-01 20:13:08 +08002873static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002874black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002875
Jason Ekstranda7af7042013-10-12 22:38:11 -05002876static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002877create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002878 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002879 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002880{
2881 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002882 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002883
2884 surface = weston_surface_create(ec);
2885 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002886 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002887 return NULL;
2888 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002889 view = weston_view_create(surface);
2890 if (surface == NULL) {
2891 weston_log("no memory\n");
2892 weston_surface_destroy(surface);
2893 return NULL;
2894 }
Alex Wu4539b082012-03-01 12:57:46 +08002895
Alex Wu21858432012-04-01 20:13:08 +08002896 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002897 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002898 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002899 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002900 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002901 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002902 pixman_region32_fini(&surface->input);
2903 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002904
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002905 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002906 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002907
2908 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002909}
2910
Philip Withnalled8f1a92013-11-25 18:01:43 +00002911static void
2912shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2913{
2914 struct weston_output *output = shsurf->fullscreen_output;
2915
Rafael Antognolli03b16592013-12-03 15:35:42 -02002916 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002917
2918 if (!shsurf->fullscreen.black_view)
2919 shsurf->fullscreen.black_view =
2920 create_black_surface(shsurf->surface->compositor,
2921 shsurf->surface,
2922 output->x, output->y,
2923 output->width,
2924 output->height);
2925
2926 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002927 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2928 weston_layer_entry_insert(&shsurf->view->layer_link,
2929 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002930 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2931 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002932
2933 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002934}
2935
Alex Wu4539b082012-03-01 12:57:46 +08002936/* Create black surface and append it to the associated fullscreen surface.
2937 * Handle size dismatch and positioning according to the method. */
2938static void
2939shell_configure_fullscreen(struct shell_surface *shsurf)
2940{
2941 struct weston_output *output = shsurf->fullscreen_output;
2942 struct weston_surface *surface = shsurf->surface;
2943 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002944 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002945 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002946
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002947 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2948 restore_output_mode(output);
2949
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002950 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002951 weston_layer_entry_remove(&shsurf->view->layer_link);
2952 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002953
Philip Withnalled8f1a92013-11-25 18:01:43 +00002954 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002955
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002957 &surf_width, &surf_height);
2958
Alex Wu4539b082012-03-01 12:57:46 +08002959 switch (shsurf->fullscreen.type) {
2960 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002961 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002962 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002963 break;
2964 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002965 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002966 if (output->width == surf_width &&
2967 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002968 weston_view_set_position(shsurf->view,
2969 output->x - surf_x,
2970 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002971 break;
2972 }
2973
Alex Wu4539b082012-03-01 12:57:46 +08002974 matrix = &shsurf->fullscreen.transform.matrix;
2975 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002976
Scott Moreau1bad5db2012-08-18 01:04:05 -06002977 output_aspect = (float) output->width /
2978 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002979 /* XXX: Use surf_width and surf_height here? */
2980 surface_aspect = (float) surface->width /
2981 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002982 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002983 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002984 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002985 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002986 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002987 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002988
Alex Wu4539b082012-03-01 12:57:46 +08002989 weston_matrix_scale(matrix, scale, scale, 1);
2990 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002991 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002992 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002993 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2994 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002995 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002996
Alex Wu4539b082012-03-01 12:57:46 +08002997 break;
2998 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002999 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003000 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003001 surf_width * surface->buffer_viewport.buffer.scale,
3002 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08003003 shsurf->fullscreen.framerate};
3004
Derek Foreman6ae7bc92014-11-04 10:47:33 -06003005 if (weston_output_mode_switch_to_temporary(output, &mode,
3006 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003007 weston_view_set_position(shsurf->view,
3008 output->x - surf_x,
3009 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003010 shsurf->fullscreen.black_view->surface->width = output->width;
3011 shsurf->fullscreen.black_view->surface->height = output->height;
3012 weston_view_set_position(shsurf->fullscreen.black_view,
3013 output->x - surf_x,
3014 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08003015 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02003016 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02003017 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003018 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003019 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02003020 }
Alex Wubd3354b2012-04-17 17:20:49 +08003021 }
Alex Wu4539b082012-03-01 12:57:46 +08003022 break;
3023 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003024 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08003025 break;
3026 default:
3027 break;
3028 }
3029}
3030
Alex Wu4539b082012-03-01 12:57:46 +08003031static void
3032shell_map_fullscreen(struct shell_surface *shsurf)
3033{
Alex Wubd3354b2012-04-17 17:20:49 +08003034 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003035}
3036
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003037static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003038set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
3039{
3040 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02003041 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003042 shsurf->transient.x = x;
3043 shsurf->transient.y = y;
3044 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00003045
3046 shell_surface_set_parent(shsurf, NULL);
3047
Pekka Paalanenb5026542014-11-12 15:09:24 +02003048 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08003049 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003050}
3051
Kristian Høgsberg47792412014-05-04 13:47:06 -07003052static void
3053shell_interface_set_fullscreen(struct shell_surface *shsurf,
3054 uint32_t method,
3055 uint32_t framerate,
3056 struct weston_output *output)
3057{
3058 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003059 shsurf->next_state.fullscreen = true;
3060 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003061
3062 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003063}
3064
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003065static struct weston_output *
3066get_focused_output(struct weston_compositor *compositor)
3067{
3068 struct weston_seat *seat;
3069 struct weston_output *output = NULL;
3070
3071 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003072 struct weston_touch *touch = weston_seat_get_touch(seat);
3073 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3074 struct weston_keyboard *keyboard =
3075 weston_seat_get_keyboard(seat);
3076
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003077 /* Priority has touch focus, then pointer and
3078 * then keyboard focus. We should probably have
3079 * three for loops and check frist for touch,
3080 * then for pointer, etc. but unless somebody has some
3081 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05003082 if (touch && touch->focus)
3083 output = touch->focus->output;
3084 else if (pointer && pointer->focus)
3085 output = pointer->focus->output;
3086 else if (keyboard && keyboard->focus)
3087 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003088
3089 if (output)
3090 break;
3091 }
3092
3093 return output;
3094}
3095
3096static void
3097shell_interface_set_maximized(struct shell_surface *shsurf)
3098{
3099 struct weston_output *output;
3100
3101 surface_clear_next_states(shsurf);
3102 shsurf->next_state.maximized = true;
3103 shsurf->state_changed = true;
3104 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3105
3106 if (!weston_surface_is_mapped(shsurf->surface))
3107 output = get_focused_output(shsurf->surface->compositor);
3108 else
3109 output = shsurf->surface->output;
3110
3111 shell_surface_set_output(shsurf, output);
3112 send_configure_for_surface(shsurf);
3113}
3114
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003115static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003116shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003117{
Derek Foremane4d6c832015-08-05 14:48:11 -05003118 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003119}
3120
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003121static int
3122shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003123 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003124 uint32_t edges)
3125{
Derek Foremane4d6c832015-08-05 14:48:11 -05003126 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003127}
3128
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003129static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003130
3131static void
3132destroy_shell_seat(struct wl_listener *listener, void *data)
3133{
3134 struct shell_seat *shseat =
3135 container_of(listener,
3136 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003137 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003138
3139 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003140 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003141 shseat->popup_grab.client = NULL;
3142
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003143 wl_list_for_each_safe(shsurf, next,
3144 &shseat->popup_grab.surfaces_list,
3145 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003146 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003147 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003148 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003149 }
3150
3151 wl_list_remove(&shseat->seat_destroy_listener.link);
3152 free(shseat);
3153}
3154
Jason Ekstrand024177c2014-04-21 19:42:58 -05003155static void
3156shell_seat_caps_changed(struct wl_listener *listener, void *data)
3157{
Derek Foreman1281a362015-07-31 16:55:32 -05003158 struct weston_keyboard *keyboard;
3159 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003160 struct shell_seat *seat;
3161
3162 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003163 keyboard = weston_seat_get_keyboard(seat->seat);
3164 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003165
Derek Foreman1281a362015-07-31 16:55:32 -05003166 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003167 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003168 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003169 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003170 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003171 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003172 wl_list_init(&seat->keyboard_focus_listener.link);
3173 }
3174
Derek Foreman1281a362015-07-31 16:55:32 -05003175 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003176 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003177 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003178 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003179 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003180 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003181 wl_list_init(&seat->pointer_focus_listener.link);
3182 }
3183}
3184
Giulio Camuffo5085a752013-03-25 21:42:45 +01003185static struct shell_seat *
3186create_shell_seat(struct weston_seat *seat)
3187{
3188 struct shell_seat *shseat;
3189
3190 shseat = calloc(1, sizeof *shseat);
3191 if (!shseat) {
3192 weston_log("no memory to allocate shell seat\n");
3193 return NULL;
3194 }
3195
3196 shseat->seat = seat;
3197 wl_list_init(&shseat->popup_grab.surfaces_list);
3198
3199 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3200 wl_signal_add(&seat->destroy_signal,
3201 &shseat->seat_destroy_listener);
3202
Jason Ekstrand024177c2014-04-21 19:42:58 -05003203 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3204 wl_list_init(&shseat->keyboard_focus_listener.link);
3205
3206 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3207 wl_list_init(&shseat->pointer_focus_listener.link);
3208
3209 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3210 wl_signal_add(&seat->updated_caps_signal,
3211 &shseat->caps_changed_listener);
3212 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3213
Giulio Camuffo5085a752013-03-25 21:42:45 +01003214 return shseat;
3215}
3216
3217static struct shell_seat *
3218get_shell_seat(struct weston_seat *seat)
3219{
3220 struct wl_listener *listener;
3221
3222 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003223 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003224
3225 return container_of(listener,
3226 struct shell_seat, seat_destroy_listener);
3227}
3228
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003229static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003230popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003231{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003232 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003233 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003234 struct shell_seat *shseat =
3235 container_of(grab, struct shell_seat, popup_grab.grab);
3236 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003237 wl_fixed_t sx, sy;
3238
Jason Ekstranda7af7042013-10-12 22:38:11 -05003239 view = weston_compositor_pick_view(pointer->seat->compositor,
3240 pointer->x, pointer->y,
3241 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003242
Jason Ekstranda7af7042013-10-12 22:38:11 -05003243 if (view && view->surface->resource &&
3244 wl_resource_get_client(view->surface->resource) == client) {
3245 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003246 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003247 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003248 }
3249}
3250
3251static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003252popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3253 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003254{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003255 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003256 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003257 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003258
Jonas Ådahl61917c82014-08-11 22:44:02 +02003259 if (pointer->focus) {
3260 weston_view_from_global_fixed(pointer->focus, x, y,
3261 &pointer->sx, &pointer->sy);
3262 }
3263
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003264 weston_pointer_move(pointer, x, y);
3265
Neil Roberts96d790e2013-09-19 17:32:00 +01003266 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003267 weston_view_from_global_fixed(pointer->focus,
3268 pointer->x, pointer->y,
3269 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003270 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003271 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003272}
3273
3274static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003275popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003276 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003277{
3278 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003279 struct shell_seat *shseat =
3280 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003281 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003282 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003283 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003284 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003285
Neil Roberts96d790e2013-09-19 17:32:00 +01003286 resource_list = &grab->pointer->focus_resource_list;
3287 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003288 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003289 wl_resource_for_each(resource, resource_list) {
3290 wl_pointer_send_button(resource, serial,
3291 time, button, state);
3292 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003293 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003294 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003295 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003296 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003297 }
3298
Daniel Stone4dbadb12012-05-30 16:31:51 +01003299 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003300 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003301}
3302
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003303static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003304popup_grab_axis(struct weston_pointer_grab *grab,
3305 uint32_t time, uint32_t axis, wl_fixed_t value)
3306{
Peter Hutterer11189522015-11-16 12:35:57 +10003307 weston_pointer_send_axis(grab->pointer, time, axis, value);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003308}
3309
3310static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003311popup_grab_cancel(struct weston_pointer_grab *grab)
3312{
3313 popup_grab_end(grab->pointer);
3314}
3315
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003316static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003317 popup_grab_focus,
3318 popup_grab_motion,
3319 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003320 popup_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003321 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003322};
3323
3324static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003325touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003326 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003327{
3328 struct wl_resource *resource;
3329 struct shell_seat *shseat =
3330 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3331 struct wl_display *display = shseat->seat->compositor->wl_display;
3332 uint32_t serial;
3333 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003334 wl_fixed_t sx, sy;
3335
3336 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003337
3338 resource_list = &grab->touch->focus_resource_list;
3339 if (!wl_list_empty(resource_list)) {
3340 serial = wl_display_get_serial(display);
3341 wl_resource_for_each(resource, resource_list) {
3342 wl_touch_send_down(resource, serial, time,
3343 grab->touch->focus->surface->resource,
3344 touch_id, sx, sy);
3345 }
3346 }
3347}
3348
3349static void
3350touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3351{
3352 struct wl_resource *resource;
3353 struct shell_seat *shseat =
3354 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3355 struct wl_display *display = shseat->seat->compositor->wl_display;
3356 uint32_t serial;
3357 struct wl_list *resource_list;
3358
3359 resource_list = &grab->touch->focus_resource_list;
3360 if (!wl_list_empty(resource_list)) {
3361 serial = wl_display_get_serial(display);
3362 wl_resource_for_each(resource, resource_list) {
3363 wl_touch_send_up(resource, serial, time, touch_id);
3364 }
3365 }
3366}
3367
3368static void
3369touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003370 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003371{
3372 struct wl_resource *resource;
3373 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003374 wl_fixed_t sx, sy;
3375
3376 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003377
3378 resource_list = &grab->touch->focus_resource_list;
3379 if (!wl_list_empty(resource_list)) {
3380 wl_resource_for_each(resource, resource_list) {
3381 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3382 }
3383 }
3384}
3385
3386static void
3387touch_popup_grab_frame(struct weston_touch_grab *grab)
3388{
3389}
3390
3391static void
3392touch_popup_grab_cancel(struct weston_touch_grab *grab)
3393{
3394 touch_popup_grab_end(grab->touch);
3395}
3396
3397static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3398 touch_popup_grab_down,
3399 touch_popup_grab_up,
3400 touch_popup_grab_motion,
3401 touch_popup_grab_frame,
3402 touch_popup_grab_cancel,
3403};
3404
3405static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003406shell_surface_send_popup_done(struct shell_surface *shsurf)
3407{
3408 if (shell_surface_is_wl_shell_surface(shsurf))
3409 wl_shell_surface_send_popup_done(shsurf->resource);
3410 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003411 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003412}
3413
3414static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003415popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003416{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003417 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003418 struct shell_seat *shseat =
3419 container_of(grab, struct shell_seat, popup_grab.grab);
3420 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003421 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003422
3423 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003424 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003425 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003426 shseat->popup_grab.grab.interface = NULL;
3427 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003428 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003429 wl_list_for_each_safe(shsurf, next,
3430 &shseat->popup_grab.surfaces_list,
3431 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003432 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003433 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003434 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003435 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003436 wl_list_init(&shseat->popup_grab.surfaces_list);
3437 }
3438}
3439
3440static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003441touch_popup_grab_end(struct weston_touch *touch)
3442{
3443 struct weston_touch_grab *grab = touch->grab;
3444 struct shell_seat *shseat =
3445 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3446 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003447 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003448
3449 if (touch->grab->interface == &touch_popup_grab_interface) {
3450 weston_touch_end_grab(grab->touch);
3451 shseat->popup_grab.client = NULL;
3452 shseat->popup_grab.touch_grab.interface = NULL;
3453 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3454 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003455 wl_list_for_each_safe(shsurf, next,
3456 &shseat->popup_grab.surfaces_list,
3457 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003458 shell_surface_send_popup_done(shsurf);
3459 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003460 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003461 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003462 wl_list_init(&shseat->popup_grab.surfaces_list);
3463 }
3464}
3465
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003466static struct shell_surface *
3467get_top_popup(struct shell_seat *shseat)
3468{
3469 struct shell_surface *shsurf;
3470
3471 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3472 return NULL;
3473 } else {
3474 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3475 struct shell_surface,
3476 popup.grab_link);
3477 return shsurf;
3478 }
3479}
3480
3481static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003482add_popup_grab(struct shell_surface *shsurf,
3483 struct shell_seat *shseat,
3484 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003485{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003486 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003487 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003488 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3489 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003490
3491 parent = get_shell_surface(shsurf->parent);
3492 top_surface = get_top_popup(shseat);
3493 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003494 (!parent ||
3495 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003496 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003497 wl_resource_post_error(shsurf->owner_resource,
3498 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003499 "xdg_popup was not created on the "
3500 "topmost popup");
3501 return -1;
3502 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003503
3504 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003505 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003506 shseat->popup_grab.client =
3507 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003508
3509 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003510 shseat->popup_grab.grab.interface =
3511 &popup_grab_interface;
3512
3513 /* We must make sure here that this popup was opened
3514 * after a mouse press, and not just by moving around
3515 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003516 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003517 shseat->popup_grab.initial_up = 0;
3518 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003519 shseat->popup_grab.touch_grab.interface =
3520 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003521 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003522
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003523 wl_list_insert(&shseat->popup_grab.surfaces_list,
3524 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003525
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003526 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003527 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003528 &shseat->popup_grab.grab);
3529 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003530 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003531 &shseat->popup_grab.touch_grab);
3532 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003533 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003534 wl_list_insert(&shseat->popup_grab.surfaces_list,
3535 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003536 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003537
3538 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003539}
3540
3541static void
3542remove_popup_grab(struct shell_surface *shsurf)
3543{
3544 struct shell_seat *shseat = shsurf->popup.shseat;
3545
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003546 if (shell_surface_is_xdg_popup(shsurf) &&
3547 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003548 wl_resource_post_error(shsurf->owner_resource,
3549 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003550 "xdg_popup was destroyed while it was "
3551 "not the topmost popup.");
3552 return;
3553 }
3554
Giulio Camuffo5085a752013-03-25 21:42:45 +01003555 wl_list_remove(&shsurf->popup.grab_link);
3556 wl_list_init(&shsurf->popup.grab_link);
3557 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003558 if (shseat->popup_grab.type == POINTER) {
3559 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3560 shseat->popup_grab.grab.interface = NULL;
3561 } else if (shseat->popup_grab.type == TOUCH) {
3562 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3563 shseat->popup_grab.touch_grab.interface = NULL;
3564 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003565 }
3566}
3567
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003568static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003569shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003570{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003571 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003572 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003573 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3574 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003575
Jason Ekstranda7af7042013-10-12 22:38:11 -05003576 shsurf->surface->output = parent_view->output;
3577 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003578
Jason Ekstranda7af7042013-10-12 22:38:11 -05003579 weston_view_set_transform_parent(shsurf->view, parent_view);
3580 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3581 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003582
Derek Foreman1281a362015-07-31 16:55:32 -05003583 if (pointer &&
3584 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003585 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3586 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003587 } else if (touch &&
3588 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003589 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3590 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003591 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003592 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003593 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003594 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003595
3596 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003597}
3598
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003599static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003600 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003601 shell_surface_move,
3602 shell_surface_resize,
3603 shell_surface_set_toplevel,
3604 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003605 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003606 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003607 shell_surface_set_maximized,
3608 shell_surface_set_title,
3609 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003610};
3611
3612static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003613destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003614{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003615 struct shell_surface *child, *next;
3616
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003617 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3618
Giulio Camuffo5085a752013-03-25 21:42:45 +01003619 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3620 remove_popup_grab(shsurf);
3621 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003622
Alex Wubd3354b2012-04-17 17:20:49 +08003623 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003624 shell_surface_is_top_fullscreen(shsurf))
3625 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003626
Jason Ekstranda7af7042013-10-12 22:38:11 -05003627 if (shsurf->fullscreen.black_view)
3628 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003629
Alex Wubd3354b2012-04-17 17:20:49 +08003630 /* As destroy_resource() use wl_list_for_each_safe(),
3631 * we can always remove the listener.
3632 */
3633 wl_list_remove(&shsurf->surface_destroy_listener.link);
3634 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003635 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003636 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003637
Jason Ekstranda7af7042013-10-12 22:38:11 -05003638 weston_view_destroy(shsurf->view);
3639
Philip Withnall648a4dd2013-11-25 18:01:44 +00003640 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003641 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3642 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003643
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003644 wl_list_remove(&shsurf->link);
3645 free(shsurf);
3646}
3647
3648static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003649shell_destroy_shell_surface(struct wl_resource *resource)
3650{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003651 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003652
Bryan Caina46b9462014-04-04 17:41:24 -05003653 if (!wl_list_empty(&shsurf->popup.grab_link))
3654 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003655 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003656 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003657}
3658
3659static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003660shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003661{
3662 struct shell_surface *shsurf = container_of(listener,
3663 struct shell_surface,
3664 surface_destroy_listener);
3665
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003666 if (shsurf->resource)
3667 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003668
3669 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003670}
3671
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003672static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003673fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003674{
3675 struct shell_surface *shsurf = data;
3676
3677 weston_surface_destroy(shsurf->surface);
3678}
3679
3680static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003681fade_out_done(struct weston_view_animation *animation, void *data)
3682{
3683 struct shell_surface *shsurf = data;
3684 struct wl_event_loop *loop;
3685
3686 loop = wl_display_get_event_loop(
3687 shsurf->surface->compositor->wl_display);
3688
3689 if (!shsurf->destroying) {
3690 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3691 shsurf->destroying = true;
3692 }
3693}
3694
3695static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003696handle_resource_destroy(struct wl_listener *listener, void *data)
3697{
3698 struct shell_surface *shsurf =
3699 container_of(listener, struct shell_surface,
3700 resource_destroy_listener);
3701
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003702 if (!weston_surface_is_mapped(shsurf->surface))
3703 return;
3704
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003705 shsurf->surface->ref_count++;
3706
3707 pixman_region32_fini(&shsurf->surface->pending.input);
3708 pixman_region32_init(&shsurf->surface->pending.input);
3709 pixman_region32_fini(&shsurf->surface->input);
3710 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003711 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3712 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3713 fade_out_done, shsurf);
3714 } else {
3715 weston_surface_destroy(shsurf->surface);
3716 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003717}
3718
3719static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003720shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003721
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003722struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003723get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003724{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003725 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003726 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003727 else
3728 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003729}
3730
Rafael Antognollie2a34552013-12-03 15:35:45 -02003731static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003732create_common_surface(struct shell_client *owner, void *shell,
3733 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003734 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003735{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003736 struct shell_surface *shsurf;
3737
Pekka Paalanen50b67472014-10-01 15:02:41 +03003738 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003739
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003740 shsurf = calloc(1, sizeof *shsurf);
3741 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003742 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003743 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003744 }
3745
Jason Ekstranda7af7042013-10-12 22:38:11 -05003746 shsurf->view = weston_view_create(surface);
3747 if (!shsurf->view) {
3748 weston_log("no memory to allocate shell surface\n");
3749 free(shsurf);
3750 return NULL;
3751 }
3752
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003753 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003754 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003755 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003756
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003757 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3758 wl_resource_add_destroy_listener(surface->resource,
3759 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003760 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003761
Tiago Vignattibc052c92012-04-19 16:18:18 +03003762 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003763 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003764 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003765 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003766 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003767 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003768 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3769 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003770 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003771 wl_list_init(&shsurf->fullscreen.transform.link);
3772
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003773 shsurf->output = get_default_output(shsurf->shell->compositor);
3774
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003775 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003776 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003777 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003778 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003779
3780 /* init link so its safe to always remove it in destroy_shell_surface */
3781 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003782 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003783
Pekka Paalanen460099f2012-01-20 16:48:25 +02003784 /* empty when not in use */
3785 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003786 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003787
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003788 wl_list_init(&shsurf->workspace_transform.link);
3789
Philip Withnall648a4dd2013-11-25 18:01:44 +00003790 wl_list_init(&shsurf->children_link);
3791 wl_list_init(&shsurf->children_list);
3792 shsurf->parent = NULL;
3793
Pekka Paalanenb5026542014-11-12 15:09:24 +02003794 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003795
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003796 shsurf->client = client;
3797
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003798 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003799}
3800
Rafael Antognollie2a34552013-12-03 15:35:45 -02003801static struct shell_surface *
3802create_shell_surface(void *shell, struct weston_surface *surface,
3803 const struct weston_shell_client *client)
3804{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003805 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003806}
3807
Jason Ekstranda7af7042013-10-12 22:38:11 -05003808static struct weston_view *
3809get_primary_view(void *shell, struct shell_surface *shsurf)
3810{
3811 return shsurf->view;
3812}
3813
Tiago Vignattibc052c92012-04-19 16:18:18 +03003814static void
3815shell_get_shell_surface(struct wl_client *client,
3816 struct wl_resource *resource,
3817 uint32_t id,
3818 struct wl_resource *surface_resource)
3819{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003820 struct weston_surface *surface =
3821 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003822 struct shell_client *sc = wl_resource_get_user_data(resource);
3823 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003824 struct shell_surface *shsurf;
3825
Pekka Paalanen50b67472014-10-01 15:02:41 +03003826 if (weston_surface_set_role(surface, "wl_shell_surface",
3827 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003828 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003829
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003830 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003831 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003832 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003833 return;
3834 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003835
Jason Ekstranda85118c2013-06-27 20:17:02 -05003836 shsurf->resource =
3837 wl_resource_create(client,
3838 &wl_shell_surface_interface, 1, id);
3839 wl_resource_set_implementation(shsurf->resource,
3840 &shell_surface_implementation,
3841 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003842 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003843}
3844
Rafael Antognollie2a34552013-12-03 15:35:45 -02003845static bool
3846shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3847{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003848 /* A shell surface without a resource is created from xwayland
3849 * and is considered a wl_shell surface for now. */
3850
3851 return shsurf->resource == NULL ||
3852 wl_resource_instance_of(shsurf->resource,
3853 &wl_shell_surface_interface,
3854 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003855}
3856
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003857static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003858 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003859};
3860
Rafael Antognollie2a34552013-12-03 15:35:45 -02003861/****************************
3862 * xdg-shell implementation */
3863
3864static void
3865xdg_surface_destroy(struct wl_client *client,
3866 struct wl_resource *resource)
3867{
3868 wl_resource_destroy(resource);
3869}
3870
3871static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003872xdg_surface_set_parent(struct wl_client *client,
3873 struct wl_resource *resource,
3874 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003875{
3876 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003877 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003878
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003879 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003880 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003881 shell_surface_set_parent(shsurf, parent->surface);
3882 } else {
3883 shell_surface_set_parent(shsurf, NULL);
3884 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003885}
3886
3887static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003888xdg_surface_set_app_id(struct wl_client *client,
3889 struct wl_resource *resource,
3890 const char *app_id)
3891{
3892 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3893
3894 free(shsurf->class);
3895 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003896 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003897}
3898
3899static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003900xdg_surface_show_window_menu(struct wl_client *client,
3901 struct wl_resource *surface_resource,
3902 struct wl_resource *seat_resource,
3903 uint32_t serial,
3904 int32_t x,
3905 int32_t y)
3906{
3907 /* TODO */
3908}
3909
3910static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003911xdg_surface_set_title(struct wl_client *client,
3912 struct wl_resource *resource, const char *title)
3913{
3914 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3915
3916 set_title(shsurf, title);
3917}
3918
3919static void
3920xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3921 struct wl_resource *seat_resource, uint32_t serial)
3922{
3923 common_surface_move(resource, seat_resource, serial);
3924}
3925
3926static void
3927xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3928 struct wl_resource *seat_resource, uint32_t serial,
3929 uint32_t edges)
3930{
3931 common_surface_resize(resource, seat_resource, serial, edges);
3932}
3933
3934static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003935xdg_surface_ack_configure(struct wl_client *client,
3936 struct wl_resource *resource,
3937 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003938{
3939 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3940
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003941 if (shsurf->state_requested) {
3942 shsurf->next_state = shsurf->requested_state;
3943 shsurf->state_changed = true;
3944 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003945 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003946}
3947
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003948static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003949xdg_surface_set_window_geometry(struct wl_client *client,
3950 struct wl_resource *resource,
3951 int32_t x,
3952 int32_t y,
3953 int32_t width,
3954 int32_t height)
3955{
3956 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3957
3958 set_window_geometry(shsurf, x, y, width, height);
3959}
3960
3961static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003962xdg_surface_set_maximized(struct wl_client *client,
3963 struct wl_resource *resource)
3964{
3965 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003966 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003967
3968 shsurf->state_requested = true;
3969 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003970
3971 if (!weston_surface_is_mapped(shsurf->surface))
3972 output = get_focused_output(shsurf->surface->compositor);
3973 else
3974 output = shsurf->surface->output;
3975
3976 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003977 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003978}
3979
3980static void
3981xdg_surface_unset_maximized(struct wl_client *client,
3982 struct wl_resource *resource)
3983{
3984 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3985
3986 shsurf->state_requested = true;
3987 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003988 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003989}
3990
3991static void
3992xdg_surface_set_fullscreen(struct wl_client *client,
3993 struct wl_resource *resource,
3994 struct wl_resource *output_resource)
3995{
3996 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3997 struct weston_output *output;
3998
3999 shsurf->state_requested = true;
4000 shsurf->requested_state.fullscreen = true;
4001
4002 if (output_resource)
4003 output = wl_resource_get_user_data(output_resource);
4004 else
4005 output = NULL;
4006
Marek Chalupa052917a2014-09-02 11:35:12 +02004007 /* handle clients launching in fullscreen */
4008 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
4009 /* Set the output to the one that has focus currently. */
4010 assert(shsurf->surface);
4011 output = get_focused_output(shsurf->surface->compositor);
4012 }
4013
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004014 shell_surface_set_output(shsurf, output);
4015 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004016
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004017 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004018}
4019
4020static void
4021xdg_surface_unset_fullscreen(struct wl_client *client,
4022 struct wl_resource *resource)
4023{
4024 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
4025
4026 shsurf->state_requested = true;
4027 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07004028 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004029}
4030
4031static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004032xdg_surface_set_minimized(struct wl_client *client,
4033 struct wl_resource *resource)
4034{
4035 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
4036
4037 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
4038 return;
4039
4040 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01004041 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004042}
4043
Rafael Antognollie2a34552013-12-03 15:35:45 -02004044static const struct xdg_surface_interface xdg_surface_implementation = {
4045 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004046 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004047 xdg_surface_set_title,
4048 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04004049 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004050 xdg_surface_move,
4051 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004052 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004053 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004054 xdg_surface_set_maximized,
4055 xdg_surface_unset_maximized,
4056 xdg_surface_set_fullscreen,
4057 xdg_surface_unset_fullscreen,
4058 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004059};
4060
4061static void
4062xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004063 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004064{
4065 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004066 uint32_t *s;
4067 struct wl_array states;
4068 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004069
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004070 assert(shsurf);
4071
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004072 if (!shsurf->resource)
4073 return;
4074
4075 wl_array_init(&states);
4076 if (shsurf->requested_state.fullscreen) {
4077 s = wl_array_add(&states, sizeof *s);
4078 *s = XDG_SURFACE_STATE_FULLSCREEN;
4079 } else if (shsurf->requested_state.maximized) {
4080 s = wl_array_add(&states, sizeof *s);
4081 *s = XDG_SURFACE_STATE_MAXIMIZED;
4082 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004083 if (shsurf->resize_edges != 0) {
4084 s = wl_array_add(&states, sizeof *s);
4085 *s = XDG_SURFACE_STATE_RESIZING;
4086 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004087 if (shsurf->focus_count > 0) {
4088 s = wl_array_add(&states, sizeof *s);
4089 *s = XDG_SURFACE_STATE_ACTIVATED;
4090 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004091
4092 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4093 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4094
4095 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004096}
4097
4098static const struct weston_shell_client xdg_client = {
4099 xdg_send_configure
4100};
4101
4102static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004103xdg_shell_destroy(struct wl_client *client,
4104 struct wl_resource *resource)
4105{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004106 struct shell_client *sc = wl_resource_get_user_data(resource);
4107 struct wl_resource *shsurf_resource;
4108 struct shell_surface *shsurf;
4109
4110 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4111 shsurf = wl_resource_get_user_data(shsurf_resource);
4112 if (shsurf->owner_resource == resource) {
4113 wl_resource_post_error(
4114 resource,
4115 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4116 "not all child surface objects destroyed");
4117 return;
4118 }
4119 }
4120
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004121 wl_resource_destroy(resource);
4122}
4123
4124static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004125xdg_use_unstable_version(struct wl_client *client,
4126 struct wl_resource *resource,
4127 int32_t version)
4128{
4129 if (version > 1) {
4130 wl_resource_post_error(resource,
4131 1,
4132 "xdg-shell:: version not implemented yet.");
4133 return;
4134 }
4135}
4136
4137static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004138create_xdg_surface(struct shell_client *owner, void *shell,
4139 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004140 const struct weston_shell_client *client)
4141{
4142 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004143
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004144 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004145 if (!shsurf)
4146 return NULL;
4147
Pekka Paalanenb5026542014-11-12 15:09:24 +02004148 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004149
4150 return shsurf;
4151}
4152
4153static void
4154xdg_get_xdg_surface(struct wl_client *client,
4155 struct wl_resource *resource,
4156 uint32_t id,
4157 struct wl_resource *surface_resource)
4158{
4159 struct weston_surface *surface =
4160 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004161 struct shell_client *sc = wl_resource_get_user_data(resource);
4162 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004163 struct shell_surface *shsurf;
4164
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004165 shsurf = get_shell_surface(surface);
4166 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4167 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4168 "This wl_surface is already an "
4169 "xdg_surface");
4170 return;
4171 }
4172
Pekka Paalanen50b67472014-10-01 15:02:41 +03004173 if (weston_surface_set_role(surface, "xdg_surface",
4174 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004175 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004176
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004177 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004178 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004179 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004180 return;
4181 }
4182
4183 shsurf->resource =
4184 wl_resource_create(client,
4185 &xdg_surface_interface, 1, id);
4186 wl_resource_set_implementation(shsurf->resource,
4187 &xdg_surface_implementation,
4188 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004189 shsurf->owner_resource = resource;
4190 wl_list_insert(&sc->surface_list,
4191 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004192}
4193
4194static bool
4195shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4196{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004197 return shsurf->resource &&
4198 wl_resource_instance_of(shsurf->resource,
4199 &xdg_surface_interface,
4200 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004201}
4202
4203/* xdg-popup implementation */
4204
4205static void
4206xdg_popup_destroy(struct wl_client *client,
4207 struct wl_resource *resource)
4208{
4209 wl_resource_destroy(resource);
4210}
4211
Rafael Antognollie2a34552013-12-03 15:35:45 -02004212static const struct xdg_popup_interface xdg_popup_implementation = {
4213 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004214};
4215
4216static void
4217xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004218 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004219{
4220}
4221
4222static const struct weston_shell_client xdg_popup_client = {
4223 xdg_popup_send_configure
4224};
4225
4226static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004227create_xdg_popup(struct shell_client *owner, void *shell,
4228 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004229 const struct weston_shell_client *client,
4230 struct weston_surface *parent,
4231 struct shell_seat *seat,
4232 uint32_t serial,
4233 int32_t x, int32_t y)
4234{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004235 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004236
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004237 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004238 if (!shsurf)
4239 return NULL;
4240
Pekka Paalanenb5026542014-11-12 15:09:24 +02004241 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004242 shsurf->popup.shseat = seat;
4243 shsurf->popup.serial = serial;
4244 shsurf->popup.x = x;
4245 shsurf->popup.y = y;
4246 shell_surface_set_parent(shsurf, parent);
4247
4248 return shsurf;
4249}
4250
4251static void
4252xdg_get_xdg_popup(struct wl_client *client,
4253 struct wl_resource *resource,
4254 uint32_t id,
4255 struct wl_resource *surface_resource,
4256 struct wl_resource *parent_resource,
4257 struct wl_resource *seat_resource,
4258 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004259 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004260{
4261 struct weston_surface *surface =
4262 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004263 struct shell_client *sc = wl_resource_get_user_data(resource);
4264 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004265 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004266 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004267 struct weston_surface *parent;
4268 struct shell_seat *seat;
4269
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004270 shsurf = get_shell_surface(surface);
4271 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4272 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4273 "This wl_surface is already an "
4274 "xdg_popup");
4275 return;
4276 }
4277
Pekka Paalanen50b67472014-10-01 15:02:41 +03004278 if (weston_surface_set_role(surface, "xdg_popup",
4279 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004280 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004281
4282 if (!parent_resource) {
4283 wl_resource_post_error(surface_resource,
4284 WL_DISPLAY_ERROR_INVALID_OBJECT,
4285 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004286 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004287 }
4288
4289 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004290 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004291
Jonas Ådahlee45a552015-03-18 16:37:47 +08004292 /* Verify that we are creating the top most popup when mapping,
4293 * as it's not until then we know whether it was mapped as most
4294 * top level or not. */
4295
4296 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004297 if (!parent_shsurf ||
4298 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4299 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004300 wl_resource_post_error(resource,
4301 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4302 "xdg_popup parent was invalid");
4303 return;
4304 }
4305
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004306 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004307 parent, seat, serial, x, y);
4308 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004309 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004310 return;
4311 }
4312
4313 shsurf->resource =
4314 wl_resource_create(client,
4315 &xdg_popup_interface, 1, id);
4316 wl_resource_set_implementation(shsurf->resource,
4317 &xdg_popup_implementation,
4318 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004319 shsurf->owner_resource = resource;
4320 wl_list_insert(&sc->surface_list,
4321 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004322}
4323
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004324static void
4325xdg_pong(struct wl_client *client,
4326 struct wl_resource *resource, uint32_t serial)
4327{
4328 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004329
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004330 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004331}
4332
Rafael Antognollie2a34552013-12-03 15:35:45 -02004333static bool
4334shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4335{
4336 return wl_resource_instance_of(shsurf->resource,
4337 &xdg_popup_interface,
4338 &xdg_popup_implementation);
4339}
4340
4341static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004342 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004343 xdg_use_unstable_version,
4344 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004345 xdg_get_xdg_popup,
4346 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004347};
4348
4349static int
4350xdg_shell_unversioned_dispatch(const void *implementation,
4351 void *_target, uint32_t opcode,
4352 const struct wl_message *message,
4353 union wl_argument *args)
4354{
4355 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004356 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004357
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004358 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004359 wl_resource_post_error(resource,
4360 WL_DISPLAY_ERROR_INVALID_OBJECT,
4361 "must call use_unstable_version first");
4362 return 0;
4363 }
4364
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004365#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004366
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004367 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4368 "shell implementation doesn't match protocol version");
4369
Rafael Antognollie2a34552013-12-03 15:35:45 -02004370 if (args[0].i != XDG_SERVER_VERSION) {
4371 wl_resource_post_error(resource,
4372 WL_DISPLAY_ERROR_INVALID_OBJECT,
4373 "incompatible version, server is %d "
4374 "client wants %d",
4375 XDG_SERVER_VERSION, args[0].i);
4376 return 0;
4377 }
4378
4379 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004380 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004381
4382 return 1;
4383}
4384
4385/* end of xdg-shell implementation */
4386/***********************************/
4387
Kristian Høgsberg07937562011-04-12 17:25:42 -04004388static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004389shell_fade(struct desktop_shell *shell, enum fade_type type);
4390
Pekka Paalanen77346a62011-11-30 16:26:35 +02004391static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004392configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004393{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004394 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004395
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004396 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004397 if (v->output == ev->output && v != ev) {
4398 weston_view_unmap(v);
4399 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004400 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004401 }
4402 }
4403
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004404 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004405
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004406 if (wl_list_empty(&ev->layer_link.link)) {
4407 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004408 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004409 }
4410}
4411
Pekka Paalanen8274d902014-08-06 19:36:51 +03004412static int
4413background_get_label(struct weston_surface *surface, char *buf, size_t len)
4414{
4415 return snprintf(buf, len, "background for output %s",
4416 surface->output->name);
4417}
4418
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004419static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004420background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004421{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004422 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004423 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004424
Jason Ekstranda7af7042013-10-12 22:38:11 -05004425 view = container_of(es->views.next, struct weston_view, surface_link);
4426
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004427 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004428}
4429
4430static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004431desktop_shell_set_background(struct wl_client *client,
4432 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004433 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004434 struct wl_resource *surface_resource)
4435{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004436 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004437 struct weston_surface *surface =
4438 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004439 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004440
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004441 if (surface->configure) {
4442 wl_resource_post_error(surface_resource,
4443 WL_DISPLAY_ERROR_INVALID_OBJECT,
4444 "surface role already assigned");
4445 return;
4446 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004447
Jason Ekstranda7af7042013-10-12 22:38:11 -05004448 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4449 weston_view_destroy(view);
4450 view = weston_view_create(surface);
4451
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004452 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004453 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004454 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004455 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004456 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004457 weston_desktop_shell_send_configure(resource, 0,
4458 surface_resource,
4459 surface->output->width,
4460 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004461}
4462
Pekka Paalanen8274d902014-08-06 19:36:51 +03004463static int
4464panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4465{
4466 return snprintf(buf, len, "panel for output %s",
4467 surface->output->name);
4468}
4469
Kristian Høgsberg75840622011-09-06 13:48:16 -04004470static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004471panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004472{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004473 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004474 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004475
Jason Ekstranda7af7042013-10-12 22:38:11 -05004476 view = container_of(es->views.next, struct weston_view, surface_link);
4477
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004478 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004479}
4480
4481static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004482desktop_shell_set_panel(struct wl_client *client,
4483 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004484 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004485 struct wl_resource *surface_resource)
4486{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004487 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004488 struct weston_surface *surface =
4489 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004490 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004491
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004492 if (surface->configure) {
4493 wl_resource_post_error(surface_resource,
4494 WL_DISPLAY_ERROR_INVALID_OBJECT,
4495 "surface role already assigned");
4496 return;
4497 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004498
Jason Ekstranda7af7042013-10-12 22:38:11 -05004499 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4500 weston_view_destroy(view);
4501 view = weston_view_create(surface);
4502
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004503 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004504 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004505 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004506 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004507 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004508 weston_desktop_shell_send_configure(resource, 0,
4509 surface_resource,
4510 surface->output->width,
4511 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004512}
4513
Pekka Paalanen8274d902014-08-06 19:36:51 +03004514static int
4515lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4516{
4517 return snprintf(buf, len, "lock window");
4518}
4519
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004520static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004521lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004522{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004523 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004524 struct weston_view *view;
4525
4526 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004527
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004528 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004529 return;
4530
Jason Ekstranda7af7042013-10-12 22:38:11 -05004531 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004532
4533 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004534 weston_layer_entry_insert(&shell->lock_layer.view_list,
4535 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004536 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004537 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004538 }
4539}
4540
4541static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004542handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004543{
Tiago Vignattibe143262012-04-16 17:31:41 +03004544 struct desktop_shell *shell =
4545 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004546
Martin Minarik6d118362012-06-07 18:01:59 +02004547 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004548 shell->lock_surface = NULL;
4549}
4550
4551static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004552desktop_shell_set_lock_surface(struct wl_client *client,
4553 struct wl_resource *resource,
4554 struct wl_resource *surface_resource)
4555{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004556 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004557 struct weston_surface *surface =
4558 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004559
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004560 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004561
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004562 if (!shell->locked)
4563 return;
4564
Pekka Paalanen98262232011-12-01 10:42:22 +02004565 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004566
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004567 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004568 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004569 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004570
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004571 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004572 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004573 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004574 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004575}
4576
4577static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004578resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004579{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004580 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004581
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004582 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004583 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004584 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004585 &shell->input_panel_layer.link);
4586 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004587 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004588 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004589 wl_list_insert(&shell->compositor->cursor_layer.link,
4590 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004591 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004592 wl_list_insert(&shell->fullscreen_layer.link,
4593 &shell->panel_layer.link);
4594 wl_list_insert(&shell->panel_layer.link,
4595 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004596
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004597 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004598
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004599 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004600 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004601 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004602}
4603
4604static void
4605desktop_shell_unlock(struct wl_client *client,
4606 struct wl_resource *resource)
4607{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004608 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004609
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004610 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004611
4612 if (shell->locked)
4613 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004614}
4615
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004616static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004617desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004618 struct wl_resource *resource,
4619 struct wl_resource *surface_resource)
4620{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004621 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004622
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004623 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004624 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004625}
4626
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004627static void
4628desktop_shell_desktop_ready(struct wl_client *client,
4629 struct wl_resource *resource)
4630{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004631 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004632
4633 shell_fade_startup(shell);
4634}
4635
Jonny Lamb765760d2014-08-20 15:53:19 +02004636static void
4637desktop_shell_set_panel_position(struct wl_client *client,
4638 struct wl_resource *resource,
4639 uint32_t position)
4640{
4641 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4642
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004643 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4644 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4645 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4646 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004647 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004648 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004649 "bad position argument");
4650 return;
4651 }
4652
4653 shell->panel_position = position;
4654}
4655
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004656static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004657 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004658 desktop_shell_set_panel,
4659 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004660 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004661 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004662 desktop_shell_desktop_ready,
4663 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004664};
4665
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004666static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004667get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004668{
4669 struct shell_surface *shsurf;
4670
4671 shsurf = get_shell_surface(surface);
4672 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004673 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004674 return shsurf->type;
4675}
4676
Kristian Høgsberg75840622011-09-06 13:48:16 -04004677static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004678move_binding(struct weston_pointer *pointer, uint32_t time,
4679 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004680{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004681 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004682 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004683 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004684
Derek Foreman8ae2db52015-07-15 13:00:36 -05004685 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004686 return;
4687
Derek Foreman8ae2db52015-07-15 13:00:36 -05004688 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004689
Pekka Paalanen01388e22013-04-25 13:57:44 +03004690 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004691 if (surface == NULL)
4692 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004693
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004694 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004695 if (shsurf == NULL || shsurf->state.fullscreen ||
4696 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004697 return;
4698
Derek Foreman794fa0e2015-07-15 13:00:38 -05004699 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004700}
4701
4702static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004703maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4704 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004705{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004706 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004707 struct weston_surface *surface;
4708 struct shell_surface *shsurf;
4709
4710 surface = weston_surface_get_main_surface(focus);
4711 if (surface == NULL)
4712 return;
4713
4714 shsurf = get_shell_surface(surface);
4715 if (shsurf == NULL)
4716 return;
4717
4718 if (!shell_surface_is_xdg_surface(shsurf))
4719 return;
4720
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004721 shsurf->state_requested = true;
4722 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004723 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004724}
4725
4726static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004727fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4728 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004729{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004730 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004731 struct weston_surface *surface;
4732 struct shell_surface *shsurf;
4733
4734 surface = weston_surface_get_main_surface(focus);
4735 if (surface == NULL)
4736 return;
4737
4738 shsurf = get_shell_surface(surface);
4739 if (shsurf == NULL)
4740 return;
4741
4742 if (!shell_surface_is_xdg_surface(shsurf))
4743 return;
4744
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004745 shsurf->state_requested = true;
4746 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004747 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004748 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004749}
4750
4751static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004752touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004753{
Derek Foreman362656b2014-09-04 10:23:05 -05004754 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004755 struct weston_surface *surface;
4756 struct shell_surface *shsurf;
4757
Derek Foreman8ae2db52015-07-15 13:00:36 -05004758 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004759 return;
4760
Derek Foreman8ae2db52015-07-15 13:00:36 -05004761 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004762 surface = weston_surface_get_main_surface(focus);
4763 if (surface == NULL)
4764 return;
4765
4766 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004767 if (shsurf == NULL || shsurf->state.fullscreen ||
4768 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004769 return;
4770
Derek Foremanb7674ae2015-07-15 13:00:37 -05004771 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004772}
4773
4774static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004775resize_binding(struct weston_pointer *pointer, uint32_t time,
4776 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004777{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004778 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004779 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004780 uint32_t edges = 0;
4781 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004782 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004783
Derek Foreman8ae2db52015-07-15 13:00:36 -05004784 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004785 return;
4786
Derek Foreman8ae2db52015-07-15 13:00:36 -05004787 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004788
Pekka Paalanen01388e22013-04-25 13:57:44 +03004789 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004790 if (surface == NULL)
4791 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004792
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004793 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004794 if (shsurf == NULL || shsurf->state.fullscreen ||
4795 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004796 return;
4797
Jason Ekstranda7af7042013-10-12 22:38:11 -05004798 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004799 wl_fixed_to_int(pointer->grab_x),
4800 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004801 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004802
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004803 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004804 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004805 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004806 edges |= 0;
4807 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004808 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004809
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004810 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004811 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004812 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004813 edges |= 0;
4814 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004815 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004816
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004817 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004818}
4819
4820static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004821surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4822 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004823{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004824 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004825 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004826 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004827 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004828
Pekka Paalanen01388e22013-04-25 13:57:44 +03004829 /* XXX: broken for windows containing sub-surfaces */
4830 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004831 if (surface == NULL)
4832 return;
4833
4834 shsurf = get_shell_surface(surface);
4835 if (!shsurf)
4836 return;
4837
Jason Ekstranda7af7042013-10-12 22:38:11 -05004838 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004839
Jason Ekstranda7af7042013-10-12 22:38:11 -05004840 if (shsurf->view->alpha > 1.0)
4841 shsurf->view->alpha = 1.0;
4842 if (shsurf->view->alpha < step)
4843 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004844
Jason Ekstranda7af7042013-10-12 22:38:11 -05004845 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004846 weston_surface_damage(surface);
4847}
4848
4849static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004850do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004851 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004852{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004853 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004854 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004855 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004856 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004857
Derek Foreman1281a362015-07-31 16:55:32 -05004858 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004859 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4860 return;
4861 }
4862
Scott Moreauccbf29d2012-02-22 14:21:41 -07004863 wl_list_for_each(output, &compositor->output_list, link) {
4864 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004865 wl_fixed_to_double(pointer->x),
4866 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004867 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004868 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004869 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004870 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004871 increment = -output->zoom.increment;
4872 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004873 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004874 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004875 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004876 else
4877 increment = 0;
4878
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004879 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004880
Scott Moreaue6603982012-06-11 13:07:51 -06004881 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004882 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004883 else if (output->zoom.level > output->zoom.max_level)
4884 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004885
4886 if (!output->zoom.active) {
4887 if (output->zoom.level <= 0.0)
4888 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004889 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004890 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004891
Scott Moreaue6603982012-06-11 13:07:51 -06004892 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004893
Jason Ekstranda7af7042013-10-12 22:38:11 -05004894 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004895 }
4896 }
4897}
4898
Scott Moreauccbf29d2012-02-22 14:21:41 -07004899static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004900zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4901 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004902{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004903 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004904}
4905
4906static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004907zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4908 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004909{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004910 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004911}
4912
4913static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004914terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4915 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004916{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004917 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004918
Daniel Stone325fc2d2012-05-30 16:31:58 +01004919 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004920}
4921
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004922static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004923rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4924 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004925{
4926 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004927 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004928 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004929 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004930 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004931
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004932 weston_pointer_move(pointer, x, y);
4933
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004934 if (!shsurf)
4935 return;
4936
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004937 cx = 0.5f * shsurf->surface->width;
4938 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004939
Daniel Stone37816df2012-05-16 18:45:18 +01004940 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4941 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004942 r = sqrtf(dx * dx + dy * dy);
4943
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004944 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004945 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004946
4947 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004948 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004949 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004950
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004951 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004952 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004953
4954 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004955 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004956 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004957 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004958 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004959
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004960 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004961 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004962 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004963 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004964 wl_list_init(&shsurf->rotation.transform.link);
4965 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004966 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004967 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004968
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004969 /* We need to adjust the position of the surface
4970 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004971 cposx = shsurf->view->geometry.x + cx;
4972 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004973 dposx = rotate->center.x - cposx;
4974 dposy = rotate->center.y - cposy;
4975 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004976 weston_view_set_position(shsurf->view,
4977 shsurf->view->geometry.x + dposx,
4978 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004979 }
4980
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004981 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004982 * lazily applies the damage due to rotation update.
4983 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004984 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004985}
4986
4987static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004988rotate_grab_button(struct weston_pointer_grab *grab,
4989 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004990{
4991 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004992 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004993 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004994 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004995 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004996
Daniel Stone4dbadb12012-05-30 16:31:51 +01004997 if (pointer->button_count == 0 &&
4998 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004999 if (shsurf)
5000 weston_matrix_multiply(&shsurf->rotation.rotation,
5001 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005002 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005003 free(rotate);
5004 }
5005}
5006
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005007static void
5008rotate_grab_cancel(struct weston_pointer_grab *grab)
5009{
5010 struct rotate_grab *rotate =
5011 container_of(grab, struct rotate_grab, base.grab);
5012
5013 shell_grab_end(&rotate->base);
5014 free(rotate);
5015}
5016
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005017static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02005018 noop_grab_focus,
5019 rotate_grab_motion,
5020 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02005021 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005022 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02005023};
5024
5025static void
Derek Foreman74de4692015-07-15 13:00:39 -05005026surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005027{
Pekka Paalanen460099f2012-01-20 16:48:25 +02005028 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005029 float dx, dy;
5030 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005031
Derek Foreman45a7c272015-09-11 14:27:40 -05005032 surface = find_toplevel_surface(surface);
5033
Pekka Paalanen460099f2012-01-20 16:48:25 +02005034 rotate = malloc(sizeof *rotate);
5035 if (!rotate)
5036 return;
5037
Jason Ekstranda7af7042013-10-12 22:38:11 -05005038 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005039 surface->surface->width * 0.5f,
5040 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005041 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005042
Derek Foreman74de4692015-07-15 13:00:39 -05005043 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5044 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005045 r = sqrtf(dx * dx + dy * dy);
5046 if (r > 20.0f) {
5047 struct weston_matrix inverse;
5048
5049 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005050 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005051 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005052
5053 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005054 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005055 } else {
5056 weston_matrix_init(&surface->rotation.rotation);
5057 weston_matrix_init(&rotate->rotation);
5058 }
5059
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005060 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005061 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005062}
5063
5064static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005065rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005066 void *data)
5067{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005068 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005069 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005070 struct shell_surface *surface;
5071
Derek Foreman8ae2db52015-07-15 13:00:36 -05005072 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005073 return;
5074
Derek Foreman8ae2db52015-07-15 13:00:36 -05005075 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005076
Pekka Paalanen01388e22013-04-25 13:57:44 +03005077 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005078 if (base_surface == NULL)
5079 return;
5080
5081 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005082 if (surface == NULL || surface->state.fullscreen ||
5083 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005084 return;
5085
Derek Foreman74de4692015-07-15 13:00:39 -05005086 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005087}
5088
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005089/* Move all fullscreen layers down to the current workspace and hide their
5090 * black views. The surfaces' state is set to both fullscreen and lowered,
5091 * and this is reversed when such a surface is re-configured, see
5092 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5093 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005094 * lowering_output = NULL - Lower on all outputs, else only lower on the
5095 * specified output.
5096 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005097 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005098 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005099void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005100lower_fullscreen_layer(struct desktop_shell *shell,
5101 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005102{
5103 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005104 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005105
5106 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005107 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005108 &shell->fullscreen_layer.view_list.link,
5109 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005110 struct shell_surface *shsurf = get_shell_surface(view->surface);
5111
5112 if (!shsurf)
5113 continue;
5114
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005115 /* Only lower surfaces which have lowering_output as their fullscreen
5116 * output, unless a NULL output asks for lowering on all outputs.
5117 */
5118 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5119 continue;
5120
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005121 /* We can have a non-fullscreen popup for a fullscreen surface
5122 * in the fullscreen layer. */
5123 if (shsurf->state.fullscreen) {
5124 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005125 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5126 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005127 weston_view_damage_below(shsurf->fullscreen.black_view);
5128
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005129 }
5130
5131 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005132 weston_layer_entry_remove(&view->layer_link);
5133 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005134 weston_view_damage_below(view);
5135 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005136
5137 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005138 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005139}
5140
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005141void
Tiago Vignattibe143262012-04-16 17:31:41 +03005142activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005143 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005144{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005145 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005146 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005147 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005148 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005149 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005150
Pekka Paalanen01388e22013-04-25 13:57:44 +03005151 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005152 shsurf = get_shell_surface(main_surface);
5153 assert(shsurf);
5154
5155 /* Only demote fullscreen surfaces on the output of activated shsurf.
5156 * Leave fullscreen surfaces on unrelated outputs alone. */
5157 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005158
Daniel Stone37816df2012-05-16 18:45:18 +01005159 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005160
Jonas Ådahl8538b222012-08-29 22:13:03 +02005161 state = ensure_focus_state(shell, seat);
5162 if (state == NULL)
5163 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005164
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005165 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005166 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005167
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005168 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005169 shell_configure_fullscreen(shsurf);
5170 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005171 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005172
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005173 /* Update the surface’s layer. This brings it to the top of the stacking
5174 * order as appropriate. */
5175 shell_surface_update_layer(shsurf);
5176
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005177 if (shell->focus_animation_type != ANIMATION_NONE) {
5178 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005179 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005180 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005181}
5182
Alex Wu21858432012-04-01 20:13:08 +08005183/* no-op func for checking black surface */
5184static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005185black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005186{
5187}
5188
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005189static bool
Alex Wu21858432012-04-01 20:13:08 +08005190is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5191{
5192 if (es->configure == black_surface_configure) {
5193 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005194 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005195 return true;
5196 }
5197 return false;
5198}
5199
Kristian Høgsberg75840622011-09-06 13:48:16 -04005200static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005201activate_binding(struct weston_seat *seat,
5202 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005203 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005204{
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005205 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005206 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005207
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005208 focus = focus_view->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005209
Pekka Paalanen01388e22013-04-25 13:57:44 +03005210 if (is_black_surface(focus, &main_surface))
5211 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005212
Pekka Paalanen01388e22013-04-25 13:57:44 +03005213 main_surface = weston_surface_get_main_surface(focus);
5214 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005215 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005216
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005217 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005218}
5219
5220static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005221click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5222 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005223{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005224 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005225 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005226 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005227 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005228
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005229 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005230}
5231
5232static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005233touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5234 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005235{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005236 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005237 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005238 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005239 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005240
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005241 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005242}
5243
5244static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005245unfocus_all_seats(struct desktop_shell *shell)
5246{
5247 struct weston_seat *seat, *next;
5248
5249 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005250 struct weston_keyboard *keyboard =
5251 weston_seat_get_keyboard(seat);
5252
5253 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005254 continue;
5255
Derek Foreman1281a362015-07-31 16:55:32 -05005256 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005257 }
5258}
5259
5260static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005261lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005262{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005263 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005264
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005265 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005266 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005267 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005268 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005269
5270 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005271
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005272 /* Hide all surfaces by removing the fullscreen, panel and
5273 * toplevel layers. This way nothing else can show or receive
5274 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005275
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005276 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005277 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005278 if (shell->showing_input_panels)
5279 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005280 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005281 wl_list_insert(&shell->compositor->cursor_layer.link,
5282 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005283
Derek Foreman004b4a12015-07-20 16:28:13 -05005284 weston_compositor_sleep(shell->compositor);
5285
Neil Robertsb4a91702014-04-09 16:33:32 +01005286 /* Remove the keyboard focus on all seats. This will be
5287 * restored to the workspace's saved state via
5288 * restore_focus_state when the compositor is unlocked */
5289 unfocus_all_seats(shell);
5290
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005291 /* TODO: disable bindings that should not work while locked. */
5292
5293 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005294}
5295
5296static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005297unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005298{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005299 struct wl_resource *shell_resource;
5300
Pekka Paalanend81c2162011-11-16 13:47:34 +02005301 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005302 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005303 return;
5304 }
5305
5306 /* If desktop-shell client has gone away, unlock immediately. */
5307 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005308 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005309 return;
5310 }
5311
5312 if (shell->prepare_event_sent)
5313 return;
5314
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005315 shell_resource = shell->child.desktop_shell;
5316 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005317 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005318}
5319
5320static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005321shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005322{
5323 struct desktop_shell *shell = data;
5324
5325 shell->fade.animation = NULL;
5326
5327 switch (shell->fade.type) {
5328 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005329 weston_surface_destroy(shell->fade.view->surface);
5330 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005331 break;
5332 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005333 lock(shell);
5334 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005335 default:
5336 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005337 }
5338}
5339
Jason Ekstranda7af7042013-10-12 22:38:11 -05005340static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005341shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005342{
5343 struct weston_compositor *compositor = shell->compositor;
5344 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005345 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005346
5347 surface = weston_surface_create(compositor);
5348 if (!surface)
5349 return NULL;
5350
Jason Ekstranda7af7042013-10-12 22:38:11 -05005351 view = weston_view_create(surface);
5352 if (!view) {
5353 weston_surface_destroy(surface);
5354 return NULL;
5355 }
5356
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005357 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005358 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005359 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005360 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5361 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005362 pixman_region32_init(&surface->input);
5363
Jason Ekstranda7af7042013-10-12 22:38:11 -05005364 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005365}
5366
5367static void
5368shell_fade(struct desktop_shell *shell, enum fade_type type)
5369{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005370 float tint;
5371
5372 switch (type) {
5373 case FADE_IN:
5374 tint = 0.0;
5375 break;
5376 case FADE_OUT:
5377 tint = 1.0;
5378 break;
5379 default:
5380 weston_log("shell: invalid fade type\n");
5381 return;
5382 }
5383
5384 shell->fade.type = type;
5385
Jason Ekstranda7af7042013-10-12 22:38:11 -05005386 if (shell->fade.view == NULL) {
5387 shell->fade.view = shell_fade_create_surface(shell);
5388 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005389 return;
5390
Jason Ekstranda7af7042013-10-12 22:38:11 -05005391 shell->fade.view->alpha = 1.0 - tint;
5392 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005393 }
5394
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005395 if (shell->fade.view->output == NULL) {
5396 /* If the black view gets a NULL output, we lost the
5397 * last output and we'll just cancel the fade. This
5398 * happens when you close the last window under the
5399 * X11 or Wayland backends. */
5400 shell->locked = false;
5401 weston_surface_destroy(shell->fade.view->surface);
5402 shell->fade.view = NULL;
5403 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005404 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005405 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005406 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005407 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005408 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005409 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005410 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005411}
5412
5413static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005414do_shell_fade_startup(void *data)
5415{
5416 struct desktop_shell *shell = data;
5417
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005418 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005419 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005420 } else {
5421 weston_log("desktop shell: "
5422 "unexpected fade-in animation type %d\n",
5423 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005424 weston_surface_destroy(shell->fade.view->surface);
5425 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005426 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005427}
5428
5429static void
5430shell_fade_startup(struct desktop_shell *shell)
5431{
5432 struct wl_event_loop *loop;
5433
5434 if (!shell->fade.startup_timer)
5435 return;
5436
5437 wl_event_source_remove(shell->fade.startup_timer);
5438 shell->fade.startup_timer = NULL;
5439
5440 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5441 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5442}
5443
5444static int
5445fade_startup_timeout(void *data)
5446{
5447 struct desktop_shell *shell = data;
5448
5449 shell_fade_startup(shell);
5450 return 0;
5451}
5452
5453static void
5454shell_fade_init(struct desktop_shell *shell)
5455{
5456 /* Make compositor output all black, and wait for the desktop-shell
5457 * client to signal it is ready, then fade in. The timer triggers a
5458 * fade-in, in case the desktop-shell client takes too long.
5459 */
5460
5461 struct wl_event_loop *loop;
5462
Jason Ekstranda7af7042013-10-12 22:38:11 -05005463 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005464 weston_log("%s: warning: fade surface already exists\n",
5465 __func__);
5466 return;
5467 }
5468
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005469 if (shell->startup_animation_type == ANIMATION_NONE)
5470 return;
5471
Jason Ekstranda7af7042013-10-12 22:38:11 -05005472 shell->fade.view = shell_fade_create_surface(shell);
5473 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005474 return;
5475
Jason Ekstranda7af7042013-10-12 22:38:11 -05005476 weston_view_update_transform(shell->fade.view);
5477 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005478
5479 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5480 shell->fade.startup_timer =
5481 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5482 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5483}
5484
5485static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005486idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005487{
5488 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005489 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005490 struct weston_seat *seat;
5491
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005492 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005493 struct weston_touch *touch = weston_seat_get_touch(seat);
5494 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5495
5496 if (pointer)
5497 popup_grab_end(pointer);
5498 if (touch)
5499 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005500 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005501
5502 shell_fade(shell, FADE_OUT);
5503 /* lock() is called from shell_fade_done() */
5504}
5505
5506static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005507wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005508{
5509 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005510 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005511
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005512 unlock(shell);
5513}
5514
5515static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005516center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005517{
Giulio Camuffob8366642013-04-25 13:57:46 +03005518 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005519 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005520
Jason Ekstranda7af7042013-10-12 22:38:11 -05005521 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005522
5523 x = output->x + (output->width - width) / 2 - surf_x / 2;
5524 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005525
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005526 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005527}
5528
5529static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005530weston_view_set_initial_position(struct weston_view *view,
5531 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005532{
5533 struct weston_compositor *compositor = shell->compositor;
5534 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005535 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005536 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005537 struct weston_output *output, *target_output = NULL;
5538 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005539 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005540
5541 /* As a heuristic place the new window on the same output as the
5542 * pointer. Falling back to the output containing 0, 0.
5543 *
5544 * TODO: Do something clever for touch too?
5545 */
5546 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005547 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5548
5549 if (pointer) {
5550 ix = wl_fixed_to_int(pointer->x);
5551 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005552 break;
5553 }
5554 }
5555
5556 wl_list_for_each(output, &compositor->output_list, link) {
5557 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5558 target_output = output;
5559 break;
5560 }
5561 }
5562
5563 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005564 weston_view_set_position(view, 10 + random() % 400,
5565 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005566 return;
5567 }
5568
5569 /* Valid range within output where the surface will still be onscreen.
5570 * If this is negative it means that the surface is bigger than
5571 * output.
5572 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005573 get_output_work_area(shell, target_output, &area);
5574
Derek Foremanf814c5d2015-04-15 12:23:54 -05005575 x = area.x;
5576 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005577 range_x = area.width - view->surface->width;
5578 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005579
Rob Bradford4cb88c72012-08-13 15:18:44 +01005580 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005581 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005582
5583 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005584 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005585
Jason Ekstranda7af7042013-10-12 22:38:11 -05005586 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005587}
5588
5589static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005590set_maximized_position(struct desktop_shell *shell,
5591 struct shell_surface *shsurf)
5592{
5593 int32_t surf_x, surf_y;
5594 pixman_rectangle32_t area;
5595
Jonny Lambd73c6942014-08-20 15:53:20 +02005596 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005597 if (shsurf->has_set_geometry) {
5598 surf_x = shsurf->geometry.x;
5599 surf_y = shsurf->geometry.y;
5600 } else {
5601 surface_subsurfaces_boundingbox(shsurf->surface,
5602 &surf_x, &surf_y, NULL, NULL);
5603 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005604
5605 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005606 area.x - surf_x,
5607 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005608}
5609
5610static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005611map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005612 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005613{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005614 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005615 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005616
Pekka Paalanen77346a62011-11-30 16:26:35 +02005617 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005618 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005619 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005620 if (shsurf->state.fullscreen) {
5621 center_on_output(shsurf->view, shsurf->fullscreen_output);
5622 shell_map_fullscreen(shsurf);
5623 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005624 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005625 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005626 weston_view_set_initial_position(shsurf->view, shell);
5627 }
Juan Zhao96879df2012-02-07 08:45:41 +08005628 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005629 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005630 if (shell_map_popup(shsurf) != 0)
5631 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005632 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005633 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005634 weston_view_set_position(shsurf->view,
5635 shsurf->view->geometry.x + sx,
5636 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005637 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005638 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005639 default:
5640 ;
5641 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005642
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005643 /* Surface stacking order, see also activate(). */
5644 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005645
Jason Ekstranda7af7042013-10-12 22:38:11 -05005646 if (shsurf->type != SHELL_SURFACE_NONE) {
5647 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005648 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005649 shsurf->surface->output = shsurf->output;
5650 shsurf->view->output = shsurf->output;
5651 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005652 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005653
Jason Ekstranda7af7042013-10-12 22:38:11 -05005654 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005655 /* XXX: xwayland's using the same fields for transient type */
5656 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005657 if (shsurf->transient.flags ==
5658 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5659 break;
5660 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005661 if (shsurf->state.relative &&
5662 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5663 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005664 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005665 break;
5666 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005667 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005668 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005669 case SHELL_SURFACE_POPUP:
5670 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005671 default:
5672 break;
5673 }
5674
Rafael Antognolli03b16592013-12-03 15:35:42 -02005675 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5676 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005677 {
5678 switch (shell->win_animation_type) {
5679 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005680 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005681 break;
5682 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005683 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005684 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005685 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005686 default:
5687 break;
5688 }
5689 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005690}
5691
5692static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005693configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005694 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005695{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005696 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005697 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005698
Pekka Paalanen77346a62011-11-30 16:26:35 +02005699 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005700
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005701 assert(shsurf);
5702
Rafael Antognolli03b16592013-12-03 15:35:42 -02005703 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005704 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005705 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005706 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005707 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005708 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005709 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005710
Alex Wu4539b082012-03-01 12:57:46 +08005711 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005712 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005713 wl_list_for_each(view, &surface->views, surface_link)
5714 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005715
Rafael Antognolli03b16592013-12-03 15:35:42 -02005716 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005717 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005718 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005719}
5720
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005721static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005722shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005723{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005724 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005725 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005726 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005727
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005728 assert(shsurf);
5729
5730 shell = shsurf->shell;
5731
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005732 if (!weston_surface_is_mapped(es) &&
5733 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005734 remove_popup_grab(shsurf);
5735 }
5736
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005737 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005738 return;
5739
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005740 if (shsurf->has_next_geometry) {
5741 shsurf->geometry = shsurf->next_geometry;
5742 shsurf->has_next_geometry = false;
5743 shsurf->has_set_geometry = true;
5744 } else if (!shsurf->has_set_geometry) {
5745 surface_subsurfaces_boundingbox(shsurf->surface,
5746 &shsurf->geometry.x,
5747 &shsurf->geometry.y,
5748 &shsurf->geometry.width,
5749 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005750 }
5751
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005752 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005753 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005754 type_changed = 1;
5755 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005756
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005757 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005758 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005759 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005760 shsurf->last_width != es->width ||
5761 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005762 float from_x, from_y;
5763 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005764
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005765 if (shsurf->resize_edges) {
5766 sx = 0;
5767 sy = 0;
5768 }
5769
5770 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5771 sx = shsurf->last_width - es->width;
5772 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5773 sy = shsurf->last_height - es->height;
5774
5775 shsurf->last_width = es->width;
5776 shsurf->last_height = es->height;
5777
Jason Ekstranda7af7042013-10-12 22:38:11 -05005778 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5779 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005780 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005781 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005782 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005783 }
5784}
5785
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005786static bool
5787check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5788{
5789 struct timespec now;
5790
5791 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5792 return false;
5793
5794 /*
5795 * If the shell helper client dies before the session has been
5796 * up for roughly 30 seconds, better just make Weston shut down,
5797 * because the user likely has no way to interact with the desktop
5798 * anyway.
5799 */
5800 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5801 weston_log("Error: %s apparently cannot run at all.\n",
5802 shell->client);
5803 weston_log_continue(STAMP_SPACE "Quitting...");
5804 wl_display_terminate(shell->compositor->wl_display);
5805
5806 return true;
5807 }
5808
5809 return false;
5810}
5811
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005812static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005813
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005814static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005815respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005816{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005817 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005818
5819 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5820 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005821 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005822 shell->child.deathstamp = time;
5823 shell->child.deathcount = 0;
5824 }
5825
5826 shell->child.deathcount++;
5827 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005828 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005829 return;
5830 }
5831
Pekka Paalanen826dc142014-08-27 12:11:53 +03005832 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005833 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005834}
5835
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005836static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005837desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5838{
5839 struct desktop_shell *shell;
5840
5841 shell = container_of(listener, struct desktop_shell,
5842 child.client_destroy_listener);
5843
Pekka Paalanen826dc142014-08-27 12:11:53 +03005844 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005845 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005846 /*
5847 * unbind_desktop_shell() will reset shell->child.desktop_shell
5848 * before the respawned process has a chance to create a new
5849 * desktop_shell object, because we are being called from the
5850 * wl_client destructor which destroys all wl_resources before
5851 * returning.
5852 */
5853
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005854 if (!check_desktop_shell_crash_too_early(shell))
5855 respawn_desktop_shell_process(shell);
5856
Pekka Paalanen826dc142014-08-27 12:11:53 +03005857 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005858}
5859
5860static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005861launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005862{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005863 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005864
Pekka Paalanen826dc142014-08-27 12:11:53 +03005865 shell->child.client = weston_client_start(shell->compositor,
5866 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005867
Arnaud Vrac42631192014-08-25 20:56:46 +02005868 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005869 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005870 return;
5871 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005872
5873 shell->child.client_destroy_listener.notify =
5874 desktop_shell_client_destroy;
5875 wl_client_add_destroy_listener(shell->child.client,
5876 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005877}
5878
5879static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005880handle_shell_client_destroy(struct wl_listener *listener, void *data)
5881{
5882 struct shell_client *sc =
5883 container_of(listener, struct shell_client, destroy_listener);
5884
5885 if (sc->ping_timer)
5886 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005887
5888 /* Since we're about to free shell_client, we remove it from the
5889 * head of the surface list so we don't use that freed list node
5890 * during surface clean up later on.
5891 */
5892 wl_list_remove(&sc->surface_list);
5893
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005894 free(sc);
5895}
5896
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005897static struct shell_client *
5898shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5899 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005900{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005901 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005902
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005903 sc = zalloc(sizeof *sc);
5904 if (sc == NULL) {
5905 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005906 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005907 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005908
5909 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005910 if (sc->resource == NULL) {
5911 free(sc);
5912 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005913 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005914 }
5915
5916 sc->client = client;
5917 sc->shell = shell;
5918 sc->destroy_listener.notify = handle_shell_client_destroy;
5919 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005920 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005921
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005922 return sc;
5923}
5924
5925static void
5926bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5927{
5928 struct desktop_shell *shell = data;
5929 struct shell_client *sc;
5930
5931 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5932 if (sc)
5933 wl_resource_set_implementation(sc->resource,
5934 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005935 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005936}
5937
5938static void
5939bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5940{
5941 struct desktop_shell *shell = data;
5942 struct shell_client *sc;
5943
5944 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5945 if (sc)
5946 wl_resource_set_dispatcher(sc->resource,
5947 xdg_shell_unversioned_dispatch,
5948 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005949}
5950
5951static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005952unbind_desktop_shell(struct wl_resource *resource)
5953{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005954 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005955
5956 if (shell->locked)
5957 resume_desktop(shell);
5958
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005959 shell->child.desktop_shell = NULL;
5960 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005961}
5962
5963static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005964bind_desktop_shell(struct wl_client *client,
5965 void *data, uint32_t version, uint32_t id)
5966{
Tiago Vignattibe143262012-04-16 17:31:41 +03005967 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005968 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005969
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005970 resource = wl_resource_create(client, &weston_desktop_shell_interface,
5971 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005972
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005973 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005974 wl_resource_set_implementation(resource,
5975 &desktop_shell_implementation,
5976 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005977 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005978 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005979 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005980
5981 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5982 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005983}
5984
Kristian Høgsberg07045392012-02-19 18:52:44 -05005985struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005986 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005987 struct weston_surface *current;
5988 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005989 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005990 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005991};
5992
5993static void
5994switcher_next(struct switcher *switcher)
5995{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005996 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005997 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005998 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005999 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006000
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006001 /* temporary re-display minimized surfaces */
6002 struct weston_view *tmp;
6003 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006004 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6005 weston_layer_entry_remove(&view->layer_link);
6006 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006007 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6008 *minimized = view;
6009 }
6010
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006011 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006012 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006013 if (shsurf &&
6014 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6015 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006016 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006017 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006018 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006019 next = view->surface;
6020 prev = view->surface;
6021 view->alpha = 0.25;
6022 weston_view_geometry_dirty(view);
6023 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006024 }
Alex Wu1659daa2012-04-01 20:13:09 +08006025
Jason Ekstranda7af7042013-10-12 22:38:11 -05006026 if (is_black_surface(view->surface, NULL)) {
6027 view->alpha = 0.25;
6028 weston_view_geometry_dirty(view);
6029 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006030 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006031 }
6032
6033 if (next == NULL)
6034 next = first;
6035
Alex Wu07b26062012-03-12 16:06:01 +08006036 if (next == NULL)
6037 return;
6038
Kristian Høgsberg07045392012-02-19 18:52:44 -05006039 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006040 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006041
6042 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006043 wl_list_for_each(view, &next->views, surface_link)
6044 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006045
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006046 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006047 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006048 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006049}
6050
6051static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006052switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006053{
6054 struct switcher *switcher =
6055 container_of(listener, struct switcher, listener);
6056
6057 switcher_next(switcher);
6058}
6059
6060static void
Daniel Stone351eb612012-05-31 15:27:47 -04006061switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006062{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006063 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006064 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006065 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006066
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006067 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006068 if (is_focus_view(view))
6069 continue;
6070
Jason Ekstranda7af7042013-10-12 22:38:11 -05006071 view->alpha = 1.0;
6072 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006073 }
6074
Alex Wu07b26062012-03-12 16:06:01 +08006075 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006076 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006077 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006078 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006079 weston_keyboard_end_grab(keyboard);
6080 if (keyboard->input_method_resource)
6081 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006082
6083 /* re-hide surfaces that were temporary shown during the switch */
6084 struct weston_view **minimized;
6085 wl_array_for_each(minimized, &switcher->minimized_array) {
6086 /* with the exception of the current selected */
6087 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006088 weston_layer_entry_remove(&(*minimized)->layer_link);
6089 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006090 weston_view_damage_below(*minimized);
6091 }
6092 }
6093 wl_array_release(&switcher->minimized_array);
6094
Kristian Høgsberg07045392012-02-19 18:52:44 -05006095 free(switcher);
6096}
6097
6098static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006099switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006100 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006101{
6102 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006103 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006104
Daniel Stonec9785ea2012-05-30 16:31:52 +01006105 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006106 switcher_next(switcher);
6107}
6108
6109static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006110switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006111 uint32_t mods_depressed, uint32_t mods_latched,
6112 uint32_t mods_locked, uint32_t group)
6113{
6114 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006115 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006116
Daniel Stone351eb612012-05-31 15:27:47 -04006117 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6118 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006119}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006120
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006121static void
6122switcher_cancel(struct weston_keyboard_grab *grab)
6123{
6124 struct switcher *switcher = container_of(grab, struct switcher, grab);
6125
6126 switcher_destroy(switcher);
6127}
6128
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006129static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006130 switcher_key,
6131 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006132 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006133};
6134
6135static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006136switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6137 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006138{
Tiago Vignattibe143262012-04-16 17:31:41 +03006139 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006140 struct switcher *switcher;
6141
6142 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006143 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006144 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006145 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006146 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006147 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006148
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006149 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006150 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006151 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006152 weston_keyboard_start_grab(keyboard, &switcher->grab);
6153 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006154 switcher_next(switcher);
6155}
6156
Pekka Paalanen3c647232011-12-22 13:43:43 +02006157static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006158backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6159 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006160{
6161 struct weston_compositor *compositor = data;
6162 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006163 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006164
6165 /* TODO: we're limiting to simple use cases, where we assume just
6166 * control on the primary display. We'd have to extend later if we
6167 * ever get support for setting backlights on random desktop LCD
6168 * panels though */
6169 output = get_default_output(compositor);
6170 if (!output)
6171 return;
6172
6173 if (!output->set_backlight)
6174 return;
6175
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006176 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6177 backlight_new = output->backlight_current - 25;
6178 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6179 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006180
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006181 if (backlight_new < 5)
6182 backlight_new = 5;
6183 if (backlight_new > 255)
6184 backlight_new = 255;
6185
6186 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006187 output->set_backlight(output, output->backlight_current);
6188}
6189
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006190static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006191force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6192 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006193{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006194 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006195 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006196 struct desktop_shell *shell = data;
6197 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006198 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006199
Derek Foreman8ae2db52015-07-15 13:00:36 -05006200 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006201 if (!focus_surface)
6202 return;
6203
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006204 wl_signal_emit(&compositor->kill_signal, focus_surface);
6205
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006206 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006207 wl_client_get_credentials(client, &pid, NULL, NULL);
6208
6209 /* Skip clients that we launched ourselves (the credentials of
6210 * the socketpair is ours) */
6211 if (pid == getpid())
6212 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006213
Daniel Stone325fc2d2012-05-30 16:31:58 +01006214 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006215}
6216
6217static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006218workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006219 uint32_t key, void *data)
6220{
6221 struct desktop_shell *shell = data;
6222 unsigned int new_index = shell->workspaces.current;
6223
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006224 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006225 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006226 if (new_index != 0)
6227 new_index--;
6228
6229 change_workspace(shell, new_index);
6230}
6231
6232static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006233workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006234 uint32_t key, void *data)
6235{
6236 struct desktop_shell *shell = data;
6237 unsigned int new_index = shell->workspaces.current;
6238
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006239 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006240 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006241 if (new_index < shell->workspaces.num - 1)
6242 new_index++;
6243
6244 change_workspace(shell, new_index);
6245}
6246
6247static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006248workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006249 uint32_t key, void *data)
6250{
6251 struct desktop_shell *shell = data;
6252 unsigned int new_index;
6253
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006254 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006255 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006256 new_index = key - KEY_F1;
6257 if (new_index >= shell->workspaces.num)
6258 new_index = shell->workspaces.num - 1;
6259
6260 change_workspace(shell, new_index);
6261}
6262
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006263static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006264workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6265 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006266{
6267 struct desktop_shell *shell = data;
6268 unsigned int new_index = shell->workspaces.current;
6269
6270 if (shell->locked)
6271 return;
6272
6273 if (new_index != 0)
6274 new_index--;
6275
Derek Foreman8ae2db52015-07-15 13:00:36 -05006276 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006277}
6278
6279static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006280workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6281 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006282{
6283 struct desktop_shell *shell = data;
6284 unsigned int new_index = shell->workspaces.current;
6285
6286 if (shell->locked)
6287 return;
6288
6289 if (new_index < shell->workspaces.num - 1)
6290 new_index++;
6291
Derek Foreman8ae2db52015-07-15 13:00:36 -05006292 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006293}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006294
6295static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006296shell_reposition_view_on_output_destroy(struct weston_view *view)
6297{
6298 struct weston_output *output, *first_output;
6299 struct weston_compositor *ec = view->surface->compositor;
6300 struct shell_surface *shsurf;
6301 float x, y;
6302 int visible;
6303
6304 x = view->geometry.x;
6305 y = view->geometry.y;
6306
6307 /* At this point the destroyed output is not in the list anymore.
6308 * If the view is still visible somewhere, we leave where it is,
6309 * otherwise, move it to the first output. */
6310 visible = 0;
6311 wl_list_for_each(output, &ec->output_list, link) {
6312 if (pixman_region32_contains_point(&output->region,
6313 x, y, NULL)) {
6314 visible = 1;
6315 break;
6316 }
6317 }
6318
6319 if (!visible) {
6320 first_output = container_of(ec->output_list.next,
6321 struct weston_output, link);
6322
6323 x = first_output->x + first_output->width / 4;
6324 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006325
6326 weston_view_set_position(view, x, y);
6327 } else {
6328 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006329 }
6330
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006331
6332 shsurf = get_shell_surface(view->surface);
6333
6334 if (shsurf) {
6335 shsurf->saved_position_valid = false;
6336 shsurf->next_state.maximized = false;
6337 shsurf->next_state.fullscreen = false;
6338 shsurf->state_changed = true;
6339 }
6340}
6341
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006342void
6343shell_for_each_layer(struct desktop_shell *shell,
6344 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006345{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006346 struct workspace **ws;
6347
6348 func(shell, &shell->fullscreen_layer, data);
6349 func(shell, &shell->panel_layer, data);
6350 func(shell, &shell->background_layer, data);
6351 func(shell, &shell->lock_layer, data);
6352 func(shell, &shell->input_panel_layer, data);
6353
6354 wl_array_for_each(ws, &shell->workspaces.array)
6355 func(shell, &(*ws)->layer, data);
6356}
6357
6358static void
6359shell_output_destroy_move_layer(struct desktop_shell *shell,
6360 struct weston_layer *layer,
6361 void *data)
6362{
6363 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006364 struct weston_view *view;
6365
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006366 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006367 if (view->output != output)
6368 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006369
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006370 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006371 }
6372}
6373
6374static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006375handle_output_destroy(struct wl_listener *listener, void *data)
6376{
6377 struct shell_output *output_listener =
6378 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006379 struct weston_output *output = output_listener->output;
6380 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006381
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006382 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006383
Xiong Zhang6b481422013-10-23 13:58:32 +08006384 wl_list_remove(&output_listener->destroy_listener.link);
6385 wl_list_remove(&output_listener->link);
6386 free(output_listener);
6387}
6388
6389static void
6390create_shell_output(struct desktop_shell *shell,
6391 struct weston_output *output)
6392{
6393 struct shell_output *shell_output;
6394
6395 shell_output = zalloc(sizeof *shell_output);
6396 if (shell_output == NULL)
6397 return;
6398
6399 shell_output->output = output;
6400 shell_output->shell = shell;
6401 shell_output->destroy_listener.notify = handle_output_destroy;
6402 wl_signal_add(&output->destroy_signal,
6403 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006404 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006405}
6406
6407static void
6408handle_output_create(struct wl_listener *listener, void *data)
6409{
6410 struct desktop_shell *shell =
6411 container_of(listener, struct desktop_shell, output_create_listener);
6412 struct weston_output *output = (struct weston_output *)data;
6413
6414 create_shell_output(shell, output);
6415}
6416
6417static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006418handle_output_move_layer(struct desktop_shell *shell,
6419 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006420{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006421 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006422 struct weston_view *view;
6423 float x, y;
6424
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006425 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006426 if (view->output != output)
6427 continue;
6428
6429 x = view->geometry.x + output->move_x;
6430 y = view->geometry.y + output->move_y;
6431 weston_view_set_position(view, x, y);
6432 }
6433}
6434
6435static void
6436handle_output_move(struct wl_listener *listener, void *data)
6437{
6438 struct desktop_shell *shell;
6439
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006440 shell = container_of(listener, struct desktop_shell,
6441 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006442
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006443 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006444}
6445
6446static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006447setup_output_destroy_handler(struct weston_compositor *ec,
6448 struct desktop_shell *shell)
6449{
6450 struct weston_output *output;
6451
6452 wl_list_init(&shell->output_list);
6453 wl_list_for_each(output, &ec->output_list, link)
6454 create_shell_output(shell, output);
6455
6456 shell->output_create_listener.notify = handle_output_create;
6457 wl_signal_add(&ec->output_created_signal,
6458 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006459
6460 shell->output_move_listener.notify = handle_output_move;
6461 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006462}
6463
6464static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006465shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006466{
Tiago Vignattibe143262012-04-16 17:31:41 +03006467 struct desktop_shell *shell =
6468 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006469 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006470 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006471
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006472 /* Force state to unlocked so we don't try to fade */
6473 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006474
6475 if (shell->child.client) {
6476 /* disable respawn */
6477 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006478 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006479 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006480
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006481 wl_list_remove(&shell->idle_listener.link);
6482 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006483
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006484 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006485 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006486
Xiong Zhang6b481422013-10-23 13:58:32 +08006487 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6488 wl_list_remove(&shell_output->destroy_listener.link);
6489 wl_list_remove(&shell_output->link);
6490 free(shell_output);
6491 }
6492
6493 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006494 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006495
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006496 wl_array_for_each(ws, &shell->workspaces.array)
6497 workspace_destroy(*ws);
6498 wl_array_release(&shell->workspaces.array);
6499
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006500 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006501 free(shell);
6502}
6503
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006504static void
6505shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6506{
6507 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006508 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006509
6510 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006511 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6512 MODIFIER_CTRL | MODIFIER_ALT,
6513 terminate_binding, ec);
6514 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6515 click_to_activate_binding,
6516 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006517 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6518 click_to_activate_binding,
6519 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006520 weston_compositor_add_touch_binding(ec, 0,
6521 touch_to_activate_binding,
6522 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006523 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6524 MODIFIER_SUPER | MODIFIER_ALT,
6525 surface_opacity_binding, NULL);
6526 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6527 MODIFIER_SUPER, zoom_axis_binding,
6528 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006529
6530 /* configurable bindings */
6531 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006532 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6533 zoom_key_binding, NULL);
6534 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6535 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006536 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6537 maximize_binding, NULL);
6538 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6539 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006540 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6541 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006542 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006543 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006544 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006545 weston_compositor_add_button_binding(ec, BTN_LEFT,
6546 mod | MODIFIER_SHIFT,
6547 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006548
6549 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006550 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006551 rotate_binding, NULL);
6552
Daniel Stone325fc2d2012-05-30 16:31:58 +01006553 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6554 shell);
6555 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6556 ec);
6557 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6558 backlight_binding, ec);
6559 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6560 ec);
6561 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6562 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006563 weston_compositor_add_key_binding(ec, KEY_K, mod,
6564 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006565 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6566 workspace_up_binding, shell);
6567 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6568 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006569 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6570 workspace_move_surface_up_binding,
6571 shell);
6572 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6573 workspace_move_surface_down_binding,
6574 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006575
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006576 if (shell->exposay_modifier)
6577 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6578 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006579
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006580 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6581 if (shell->workspaces.num > 1) {
6582 num_workspace_bindings = shell->workspaces.num;
6583 if (num_workspace_bindings > 6)
6584 num_workspace_bindings = 6;
6585 for (i = 0; i < num_workspace_bindings; i++)
6586 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6587 workspace_f_binding,
6588 shell);
6589 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006590
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006591 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006592}
6593
Jason Ekstrand024177c2014-04-21 19:42:58 -05006594static void
6595handle_seat_created(struct wl_listener *listener, void *data)
6596{
6597 struct weston_seat *seat = data;
6598
6599 create_shell_seat(seat);
6600}
6601
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006602WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006603module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006604 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006605{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006606 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006607 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006608 struct workspace **pws;
6609 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006610 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006611
Peter Huttererf3d62272013-08-08 11:57:05 +10006612 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006613 if (shell == NULL)
6614 return -1;
6615
Kristian Høgsberg75840622011-09-06 13:48:16 -04006616 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006617
6618 shell->destroy_listener.notify = shell_destroy;
6619 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006620 shell->idle_listener.notify = idle_handler;
6621 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6622 shell->wake_listener.notify = wake_handler;
6623 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006624
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006625 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006626 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006627 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006628 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006629 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006630 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006631 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006632 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006633 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006634 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006635 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006636 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006637 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006638
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006639 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6640 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006641 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6642 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006643 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006644
6645 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006646 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006647
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006648 if (input_panel_setup(shell) < 0)
6649 return -1;
6650
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006651 shell->text_backend = text_backend_init(ec);
6652 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006653 return -1;
6654
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006655 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006656
Daniel Stonedf8133b2013-11-19 11:37:14 +01006657 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6658 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6659
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006660 for (i = 0; i < shell->workspaces.num; i++) {
6661 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6662 if (pws == NULL)
6663 return -1;
6664
6665 *pws = workspace_create();
6666 if (*pws == NULL)
6667 return -1;
6668 }
6669 activate_workspace(shell, 0);
6670
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006671 weston_layer_init(&shell->minimized_layer, NULL);
6672
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006673 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006674 wl_list_init(&shell->workspaces.animation.link);
6675 shell->workspaces.animation.frame = animate_workspace_change_frame;
6676
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006677 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006678 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006679 return -1;
6680
Rafael Antognollie2a34552013-12-03 15:35:45 -02006681 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6682 shell, bind_xdg_shell) == NULL)
6683 return -1;
6684
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006685 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006686 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006687 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006688 return -1;
6689
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006690 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6691 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006692 return -1;
6693
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006694 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006695
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006696 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006697
Xiong Zhang6b481422013-10-23 13:58:32 +08006698 setup_output_destroy_handler(ec, shell);
6699
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006700 loop = wl_display_get_event_loop(ec->wl_display);
6701 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006702
Jason Ekstrand024177c2014-04-21 19:42:58 -05006703 wl_list_for_each(seat, &ec->seat_list, link)
6704 handle_seat_created(NULL, seat);
6705 shell->seat_create_listener.notify = handle_seat_created;
6706 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006707
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006708 screenshooter_create(ec);
6709
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006710 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006711
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006712 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006713
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006714 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6715
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006716 return 0;
6717}