blob: c125d5561ba128bc0a2823dbc6d892e5f4ed0159 [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"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030039#include "compositor/weston.h"
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080040#include "weston-desktop-shell-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;
Alex Wu4539b082012-03-01 12:57:46 +0800128 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800129 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700130 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800131 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100132
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500133 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200134 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500135 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200136 } rotation;
137
138 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100139 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500140 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100141 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400142 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 } popup;
144
Alex Wu4539b082012-03-01 12:57:46 +0800145 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300146 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400147 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300148 } transient;
149
150 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800151 enum wl_shell_surface_fullscreen_method type;
152 struct weston_transform transform; /* matrix from x, y */
153 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500154 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800155 } fullscreen;
156
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200157 struct weston_transform workspace_transform;
158
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500159 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500160 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100161 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400162
163 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200164
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700165 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200166 bool maximized;
167 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200168 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100169 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500170 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200171 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500172 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500173
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700174 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400175 int32_t x, y, width, height;
176 } geometry, next_geometry;
177 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700178
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500179 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500180
181 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200182};
183
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300184struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400185 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300186 struct shell_surface *shsurf;
187 struct wl_listener shsurf_destroy_listener;
188};
189
Rusty Lynch1084da52013-08-15 09:10:08 -0700190struct shell_touch_grab {
191 struct weston_touch_grab grab;
192 struct shell_surface *shsurf;
193 struct wl_listener shsurf_destroy_listener;
194 struct weston_touch *touch;
195};
196
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300197struct weston_move_grab {
198 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100199 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500200 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500201};
202
Rusty Lynch1084da52013-08-15 09:10:08 -0700203struct weston_touch_move_grab {
204 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800205 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700206 wl_fixed_t dx, dy;
207};
208
Pekka Paalanen460099f2012-01-20 16:48:25 +0200209struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300210 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500211 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200212 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200213 float x;
214 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200215 } center;
216};
217
Giulio Camuffo5085a752013-03-25 21:42:45 +0100218struct shell_seat {
219 struct weston_seat *seat;
220 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500221 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100222
Jason Ekstrand024177c2014-04-21 19:42:58 -0500223 struct wl_listener caps_changed_listener;
224 struct wl_listener pointer_focus_listener;
225 struct wl_listener keyboard_focus_listener;
226
Giulio Camuffo5085a752013-03-25 21:42:45 +0100227 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400228 struct weston_pointer_grab grab;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200229 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100230 struct wl_list surfaces_list;
231 struct wl_client *client;
232 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200233 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100234 } popup_grab;
235};
236
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800237struct shell_client {
238 struct wl_resource *resource;
239 struct wl_client *client;
240 struct desktop_shell *shell;
241 struct wl_listener destroy_listener;
242 struct wl_event_source *ping_timer;
243 uint32_t ping_serial;
244 int unresponsive;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800245 struct wl_list surface_list;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800246};
247
Alex Wubd3354b2012-04-17 17:20:49 +0800248static struct desktop_shell *
249shell_surface_get_shell(struct shell_surface *shsurf);
250
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500251static void
Derek Foreman74de4692015-07-15 13:00:39 -0500252surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500253
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300254static void
255shell_fade_startup(struct desktop_shell *shell);
256
Philip Withnallbecb77e2013-11-25 18:01:30 +0000257static struct shell_seat *
258get_shell_seat(struct weston_seat *seat);
259
Jonny Lambd73c6942014-08-20 15:53:20 +0200260static void
261get_output_panel_size(struct desktop_shell *shell,
262 struct weston_output *output,
263 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700264
Philip Withnall648a4dd2013-11-25 18:01:44 +0000265static void
266shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
267
Alex Wubd3354b2012-04-17 17:20:49 +0800268static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200269shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
270
271static bool
272shell_surface_is_xdg_surface(struct shell_surface *shsurf);
273
274static bool
275shell_surface_is_xdg_popup(struct shell_surface *shsurf);
276
277static void
278shell_surface_set_parent(struct shell_surface *shsurf,
279 struct weston_surface *parent);
280
Pekka Paalanen8274d902014-08-06 19:36:51 +0300281static int
282shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
283{
284 struct shell_surface *shsurf;
285 const char *typestr[] = {
286 [SHELL_SURFACE_NONE] = "unidentified",
287 [SHELL_SURFACE_TOPLEVEL] = "top-level",
288 [SHELL_SURFACE_POPUP] = "popup",
289 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
290 };
291 const char *t, *c;
292
293 shsurf = get_shell_surface(surface);
294 if (!shsurf)
295 return snprintf(buf, len, "unidentified window");
296
297 t = shsurf->title;
298 c = shsurf->class;
299
300 return snprintf(buf, len, "%s window%s%s%s%s%s",
301 typestr[shsurf->type],
302 t ? " '" : "", t ?: "", t ? "'" : "",
303 c ? " of " : "", c ?: "");
304}
305
Rafael Antognollie2a34552013-12-03 15:35:45 -0200306static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800307shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
308{
309 struct desktop_shell *shell;
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200310 struct weston_view *view;
311 struct shell_surface *top_fs_shsurf = NULL;
Alex Wubd3354b2012-04-17 17:20:49 +0800312
313 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100314
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300315 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800316 return false;
317
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200318 /* Find topmost shsurf on the same fullscreen output on which shsurf
319 * is displaying. We don't care about other outputs.
320 */
321 wl_list_for_each(view, &shell->fullscreen_layer.view_list.link,
322 layer_link.link) {
323 struct shell_surface *cand_shsurf = get_shell_surface(view->surface);
324
325 if (cand_shsurf &&
326 (cand_shsurf->fullscreen_output == shsurf->fullscreen_output)) {
327 top_fs_shsurf = cand_shsurf;
328 break;
329 }
330 }
331
332 return (shsurf == top_fs_shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800333}
334
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500335static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400336destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300337{
338 struct shell_grab *grab;
339
340 grab = container_of(listener, struct shell_grab,
341 shsurf_destroy_listener);
342
343 grab->shsurf = NULL;
344}
345
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800346struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500347get_default_view(struct weston_surface *surface)
348{
349 struct shell_surface *shsurf;
350 struct weston_view *view;
351
352 if (!surface || wl_list_empty(&surface->views))
353 return NULL;
354
355 shsurf = get_shell_surface(surface);
356 if (shsurf)
357 return shsurf->view;
358
359 wl_list_for_each(view, &surface->views, surface_link)
360 if (weston_view_is_mapped(view))
361 return view;
362
363 return container_of(surface->views.next, struct weston_view, surface_link);
364}
365
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300366static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400367popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200368static void
369touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400370
371static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300372shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400373 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400375 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800376 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300377{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300378 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500379 struct weston_touch *touch = weston_seat_get_touch(pointer->seat);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300380
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400381 popup_grab_end(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -0500382 if (touch)
383 touch_popup_grab_end(touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400384
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300385 grab->grab.interface = interface;
386 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400387 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500388 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400389 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300390
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700391 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400392 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400393 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800394 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400395 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500396 weston_pointer_set_focus(pointer,
397 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400398 wl_fixed_from_int(0),
399 wl_fixed_from_int(0));
400 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300401}
402
Jonny Lambd73c6942014-08-20 15:53:20 +0200403static void
404get_output_panel_size(struct desktop_shell *shell,
405 struct weston_output *output,
406 int *width,
407 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700408{
409 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200410
411 *width = 0;
412 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700413
414 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200415 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700416
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300417 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200418 float x, y;
419
420 if (view->surface->output != output)
421 continue;
422
423 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800424 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
425 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200426 weston_view_to_global_float(view,
427 view->surface->width, 0,
428 &x, &y);
429
Derek Foreman612341f2015-04-15 12:23:55 -0500430 *width = (int)x - output->x;
431 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200432 return;
433
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800434 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
435 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200436 weston_view_to_global_float(view,
437 0, view->surface->height,
438 &x, &y);
439
Derek Foreman612341f2015-04-15 12:23:55 -0500440 *width = view->surface->width + (int)x - output->x;
441 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200442 return;
443
444 default:
445 /* we've already set width and height to
446 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700447 break;
448 }
449 }
450
Jonny Lambd73c6942014-08-20 15:53:20 +0200451 /* the correct view wasn't found */
452}
453
454static void
Quentin Glidic5c201952016-03-23 13:50:49 +0100455get_output_work_area(void *data,
Jonny Lambd73c6942014-08-20 15:53:20 +0200456 struct weston_output *output,
457 pixman_rectangle32_t *area)
458{
Quentin Glidic5c201952016-03-23 13:50:49 +0100459 struct desktop_shell *shell = data;
Jonny Lambd73c6942014-08-20 15:53:20 +0200460 int32_t panel_width = 0, panel_height = 0;
461
Derek Foremanf814c5d2015-04-15 12:23:54 -0500462 area->x = output->x;
463 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200464
465 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200466 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800467 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200468 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500469 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800470 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200471 area->width = output->width;
472 area->height = output->height - panel_height;
473 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800474 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500475 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800476 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200477 area->width = output->width - panel_width;
478 area->height = output->height;
479 break;
480 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700481}
482
Derek Foreman45a7c272015-09-11 14:27:40 -0500483static struct shell_surface *
484find_toplevel_surface(struct shell_surface *in_surface)
485{
486 struct shell_surface *surface = in_surface;
487
Ben Hummon445e44c2015-10-06 11:48:14 -0500488 while (surface) {
489 if (surface->type == SHELL_SURFACE_TOPLEVEL)
490 return surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500491 surface = get_shell_surface(surface->parent);
Ben Hummon445e44c2015-10-06 11:48:14 -0500492 }
Derek Foreman45a7c272015-09-11 14:27:40 -0500493
494 /* If no top level surface was found, just use whatever surface was
495 originally provided. */
Ben Hummon445e44c2015-10-06 11:48:14 -0500496 return in_surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500497}
498
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700499static void
500send_configure_for_surface(struct shell_surface *shsurf)
501{
502 int32_t width, height;
503 struct surface_state *state;
504
505 if (shsurf->state_requested)
506 state = &shsurf->requested_state;
507 else if (shsurf->state_changed)
508 state = &shsurf->next_state;
509 else
510 state = &shsurf->state;
511
512 if (state->fullscreen) {
513 width = shsurf->output->width;
514 height = shsurf->output->height;
515 } else if (state->maximized) {
516 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200517 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700518
519 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200520 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700521
Jonny Lambd73c6942014-08-20 15:53:20 +0200522 width = area.width;
523 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900524 } else if (shsurf->resize_edges) {
525 width = shsurf->geometry.width;
526 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700527 } else {
528 width = 0;
529 height = 0;
530 }
531
532 shsurf->client->send_configure(shsurf->surface, width, height);
533}
534
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300535static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400536shell_surface_state_changed(struct shell_surface *shsurf)
537{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700538 if (shell_surface_is_xdg_surface(shsurf))
539 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400540}
541
542static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300543shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300544{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700545 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400546 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700547 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400548
549 if (grab->shsurf->resize_edges) {
550 grab->shsurf->resize_edges = 0;
551 shell_surface_state_changed(grab->shsurf);
552 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700553 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300554
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700555 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300556}
557
558static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700559shell_touch_grab_start(struct shell_touch_grab *grab,
560 const struct weston_touch_grab_interface *interface,
561 struct shell_surface *shsurf,
562 struct weston_touch *touch)
563{
564 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500565 struct weston_pointer *pointer = weston_seat_get_pointer(touch->seat);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800566
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200567 touch_popup_grab_end(touch);
Derek Foreman1281a362015-07-31 16:55:32 -0500568 if (pointer)
569 popup_grab_end(pointer);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700570
Rusty Lynch1084da52013-08-15 09:10:08 -0700571 grab->grab.interface = interface;
572 grab->shsurf = shsurf;
573 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
574 wl_signal_add(&shsurf->destroy_signal,
575 &grab->shsurf_destroy_listener);
576
577 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700578 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700579
580 weston_touch_start_grab(touch, &grab->grab);
581 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500582 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700584}
585
586static void
587shell_touch_grab_end(struct shell_touch_grab *grab)
588{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700589 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700590 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700591 grab->shsurf->grabbed = 0;
592 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700593
594 weston_touch_end_grab(grab->touch);
595}
596
597static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500598center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800599 struct weston_output *output);
600
Daniel Stone496ca172012-05-30 16:31:42 +0100601static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300602get_modifier(char *modifier)
603{
604 if (!modifier)
605 return MODIFIER_SUPER;
606
607 if (!strcmp("ctrl", modifier))
608 return MODIFIER_CTRL;
609 else if (!strcmp("alt", modifier))
610 return MODIFIER_ALT;
611 else if (!strcmp("super", modifier))
612 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000613 else if (!strcmp("none", modifier))
614 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300615 else
616 return MODIFIER_SUPER;
617}
618
Juan Zhaoe10d2792012-04-25 19:09:52 +0800619static enum animation_type
620get_animation_type(char *animation)
621{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800622 if (!animation)
623 return ANIMATION_NONE;
624
Juan Zhaoe10d2792012-04-25 19:09:52 +0800625 if (!strcmp("zoom", animation))
626 return ANIMATION_ZOOM;
627 else if (!strcmp("fade", animation))
628 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100629 else if (!strcmp("dim-layer", animation))
630 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800631 else
632 return ANIMATION_NONE;
633}
634
Alex Wu4539b082012-03-01 12:57:46 +0800635static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400636shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200637{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400638 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500639 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300640 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000641 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200642
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300643 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400644 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300645 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
646 WESTON_SHELL_CLIENT);
647 if (ret < 0)
648 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400649 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500650 "client", &s, client);
651 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100652 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000653
654 weston_config_section_get_bool(section,
655 "allow-zap", &allow_zap, true);
656 shell->allow_zap = allow_zap;
657
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100658 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400659 "binding-modifier", &s, "super");
660 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200661 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800662
663 weston_config_section_get_string(section,
664 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000665 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800666 free(s);
667
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400668 weston_config_section_get_string(section, "animation", &s, "none");
669 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200670 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200671 weston_config_section_get_string(section, "close-animation", &s, "fade");
672 shell->win_close_animation_type = get_animation_type(s);
673 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700674 weston_config_section_get_string(section,
675 "startup-animation", &s, "fade");
676 shell->startup_animation_type = get_animation_type(s);
677 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700678 if (shell->startup_animation_type == ANIMATION_ZOOM)
679 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100680 weston_config_section_get_string(section, "focus-animation", &s, "none");
681 shell->focus_animation_type = get_animation_type(s);
682 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400683 weston_config_section_get_uint(section, "num-workspaces",
684 &shell->workspaces.num,
685 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200686}
687
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800688struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100689get_default_output(struct weston_compositor *compositor)
690{
Armin Krezović10b06182016-06-23 11:59:30 +0200691 if (wl_list_empty(&compositor->output_list))
692 return NULL;
693
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100694 return container_of(compositor->output_list.next,
695 struct weston_output, link);
696}
697
Pekka Paalanen8274d902014-08-06 19:36:51 +0300698static int
699focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
700{
701 return snprintf(buf, len, "focus highlight effect for output %s",
702 surface->output->name);
703}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100704
705/* no-op func for checking focus surface */
706static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600707focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100708{
709}
710
711static struct focus_surface *
712get_focus_surface(struct weston_surface *surface)
713{
714 if (surface->configure == focus_surface_configure)
715 return surface->configure_private;
716 else
717 return NULL;
718}
719
720static bool
721is_focus_surface (struct weston_surface *es)
722{
723 return (es->configure == focus_surface_configure);
724}
725
726static bool
727is_focus_view (struct weston_view *view)
728{
729 return is_focus_surface (view->surface);
730}
731
732static struct focus_surface *
733create_focus_surface(struct weston_compositor *ec,
734 struct weston_output *output)
735{
736 struct focus_surface *fsurf = NULL;
737 struct weston_surface *surface = NULL;
738
739 fsurf = malloc(sizeof *fsurf);
740 if (!fsurf)
741 return NULL;
742
743 fsurf->surface = weston_surface_create(ec);
744 surface = fsurf->surface;
745 if (surface == NULL) {
746 free(fsurf);
747 return NULL;
748 }
749
750 surface->configure = focus_surface_configure;
751 surface->output = output;
752 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300753 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100754
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800755 fsurf->view = weston_view_create(surface);
756 if (fsurf->view == NULL) {
757 weston_surface_destroy(surface);
758 free(fsurf);
759 return NULL;
760 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100761 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100762
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600763 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600764 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100765 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
766 pixman_region32_fini(&surface->opaque);
767 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
768 output->width, output->height);
769 pixman_region32_fini(&surface->input);
770 pixman_region32_init(&surface->input);
771
772 wl_list_init(&fsurf->workspace_transform.link);
773
774 return fsurf;
775}
776
777static void
778focus_surface_destroy(struct focus_surface *fsurf)
779{
780 weston_surface_destroy(fsurf->surface);
781 free(fsurf);
782}
783
784static void
785focus_animation_done(struct weston_view_animation *animation, void *data)
786{
787 struct workspace *ws = data;
788
789 ws->focus_animation = NULL;
790}
791
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200792static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200793focus_state_destroy(struct focus_state *state)
794{
795 wl_list_remove(&state->seat_destroy_listener.link);
796 wl_list_remove(&state->surface_destroy_listener.link);
797 free(state);
798}
799
800static void
801focus_state_seat_destroy(struct wl_listener *listener, void *data)
802{
803 struct focus_state *state = container_of(listener,
804 struct focus_state,
805 seat_destroy_listener);
806
807 wl_list_remove(&state->link);
808 focus_state_destroy(state);
809}
810
811static void
812focus_state_surface_destroy(struct wl_listener *listener, void *data)
813{
814 struct focus_state *state = container_of(listener,
815 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400816 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400817 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500818 struct weston_surface *main_surface, *next;
819 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200820
Pekka Paalanen01388e22013-04-25 13:57:44 +0300821 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
822
Kristian Høgsberge3778222012-07-31 17:29:30 -0400823 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300824 wl_list_for_each(view,
825 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500826 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400827 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100828 if (is_focus_view(view))
829 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400830
Jason Ekstranda7af7042013-10-12 22:38:11 -0500831 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400832 break;
833 }
834
Pekka Paalanen01388e22013-04-25 13:57:44 +0300835 /* if the focus was a sub-surface, activate its main surface */
836 if (main_surface != state->keyboard_focus)
837 next = main_surface;
838
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100839 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400840 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100842 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400843 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100844 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
845 if (state->ws->focus_animation)
846 weston_view_animation_destroy(state->ws->focus_animation);
847
848 state->ws->focus_animation = weston_fade_run(
849 state->ws->fsurf_front->view,
850 state->ws->fsurf_front->view->alpha, 0.0, 300,
851 focus_animation_done, state->ws);
852 }
853
Kristian Høgsberge3778222012-07-31 17:29:30 -0400854 wl_list_remove(&state->link);
855 focus_state_destroy(state);
856 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200857}
858
859static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400860focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200861{
Jonas Ådahl04769742012-06-13 00:01:24 +0200862 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200863
864 state = malloc(sizeof *state);
865 if (state == NULL)
866 return NULL;
867
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100868 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400869 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200870 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400871 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200872
873 state->seat_destroy_listener.notify = focus_state_seat_destroy;
874 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400875 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200876 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400877 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200878
879 return state;
880}
881
Jonas Ådahl8538b222012-08-29 22:13:03 +0200882static struct focus_state *
883ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
884{
885 struct workspace *ws = get_current_workspace(shell);
886 struct focus_state *state;
887
888 wl_list_for_each(state, &ws->focus_list, link)
889 if (state->seat == seat)
890 break;
891
892 if (&state->link == &ws->focus_list)
893 state = focus_state_create(seat, ws);
894
895 return state;
896}
897
Jonas Ådahl04769742012-06-13 00:01:24 +0200898static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800899focus_state_set_focus(struct focus_state *state,
900 struct weston_surface *surface)
901{
902 if (state->keyboard_focus) {
903 wl_list_remove(&state->surface_destroy_listener.link);
904 wl_list_init(&state->surface_destroy_listener.link);
905 }
906
907 state->keyboard_focus = surface;
908 if (surface)
909 wl_signal_add(&surface->destroy_signal,
910 &state->surface_destroy_listener);
911}
912
913static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400914restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200915{
916 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400917 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100918 struct wl_list pending_seat_list;
919 struct weston_seat *seat, *next_seat;
920
921 /* Temporarily steal the list of seats so that we can keep
922 * track of the seats we've already processed */
923 wl_list_init(&pending_seat_list);
924 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
925 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200926
927 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500928 struct weston_keyboard *keyboard =
929 weston_seat_get_keyboard(state->seat);
930
Neil Roberts4237f502014-04-09 16:33:31 +0100931 wl_list_remove(&state->seat->link);
932 wl_list_insert(&shell->compositor->seat_list,
933 &state->seat->link);
934
Derek Foreman1281a362015-07-31 16:55:32 -0500935 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800936 continue;
937
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400938 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200939
Derek Foreman1281a362015-07-31 16:55:32 -0500940 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200941 }
Neil Roberts4237f502014-04-09 16:33:31 +0100942
943 /* For any remaining seats that we don't have a focus state
944 * for we'll reset the keyboard focus to NULL */
945 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500946 struct weston_keyboard *keyboard =
947 weston_seat_get_keyboard(seat);
948
Neil Roberts4237f502014-04-09 16:33:31 +0100949 wl_list_insert(&shell->compositor->seat_list, &seat->link);
950
Derek Foreman1281a362015-07-31 16:55:32 -0500951 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100952 continue;
953
Derek Foreman1281a362015-07-31 16:55:32 -0500954 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100955 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200956}
957
958static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200959replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
960 struct weston_seat *seat)
961{
Derek Foreman1281a362015-07-31 16:55:32 -0500962 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200963 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200964
965 wl_list_for_each(state, &ws->focus_list, link) {
966 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500967 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200968 return;
969 }
970 }
971}
972
973static void
974drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
975 struct weston_surface *surface)
976{
977 struct focus_state *state;
978
979 wl_list_for_each(state, &ws->focus_list, link)
980 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800981 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200982}
983
984static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100985animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
986 struct weston_view *from, struct weston_view *to)
987{
988 struct weston_output *output;
989 bool focus_surface_created = false;
990
991 /* FIXME: Only support dim animation using two layers */
992 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
993 return;
994
995 output = get_default_output(shell->compositor);
996 if (ws->fsurf_front == NULL && (from || to)) {
997 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800998 if (ws->fsurf_front == NULL)
999 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001000 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001001
1002 ws->fsurf_back = create_focus_surface(shell->compositor, output);
1003 if (ws->fsurf_back == NULL) {
1004 focus_surface_destroy(ws->fsurf_front);
1005 return;
1006 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001007 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001008
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001009 focus_surface_created = true;
1010 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001011 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1012 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001013 }
1014
1015 if (ws->focus_animation) {
1016 weston_view_animation_destroy(ws->focus_animation);
1017 ws->focus_animation = NULL;
1018 }
1019
1020 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001021 weston_layer_entry_insert(&to->layer_link,
1022 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001023 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001024 weston_layer_entry_insert(&ws->layer.view_list,
1025 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001026
1027 if (focus_surface_created) {
1028 ws->focus_animation = weston_fade_run(
1029 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001030 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001031 focus_animation_done, ws);
1032 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001033 weston_layer_entry_insert(&from->layer_link,
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 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001040 weston_layer_entry_insert(&ws->layer.view_list,
1041 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001042 ws->focus_animation = weston_stable_fade_run(
1043 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001044 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001045 focus_animation_done, ws);
1046 }
1047}
1048
1049static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001050workspace_destroy(struct workspace *ws)
1051{
Jonas Ådahl04769742012-06-13 00:01:24 +02001052 struct focus_state *state, *next;
1053
1054 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1055 focus_state_destroy(state);
1056
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001057 if (ws->fsurf_front)
1058 focus_surface_destroy(ws->fsurf_front);
1059 if (ws->fsurf_back)
1060 focus_surface_destroy(ws->fsurf_back);
1061
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001062 free(ws);
1063}
1064
Jonas Ådahl04769742012-06-13 00:01:24 +02001065static void
1066seat_destroyed(struct wl_listener *listener, void *data)
1067{
1068 struct weston_seat *seat = data;
1069 struct focus_state *state, *next;
1070 struct workspace *ws = container_of(listener,
1071 struct workspace,
1072 seat_destroyed_listener);
1073
1074 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1075 if (state->seat == seat)
1076 wl_list_remove(&state->link);
1077}
1078
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001079static struct workspace *
1080workspace_create(void)
1081{
1082 struct workspace *ws = malloc(sizeof *ws);
1083 if (ws == NULL)
1084 return NULL;
1085
1086 weston_layer_init(&ws->layer, NULL);
1087
Jonas Ådahl04769742012-06-13 00:01:24 +02001088 wl_list_init(&ws->focus_list);
1089 wl_list_init(&ws->seat_destroyed_listener.link);
1090 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001091 ws->fsurf_front = NULL;
1092 ws->fsurf_back = NULL;
1093 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001094
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001095 return ws;
1096}
1097
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098static int
1099workspace_is_empty(struct workspace *ws)
1100{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001101 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001102}
1103
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001104static struct workspace *
1105get_workspace(struct desktop_shell *shell, unsigned int index)
1106{
1107 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001108 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001109 pws += index;
1110 return *pws;
1111}
1112
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001113struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001114get_current_workspace(struct desktop_shell *shell)
1115{
1116 return get_workspace(shell, shell->workspaces.current);
1117}
1118
1119static void
1120activate_workspace(struct desktop_shell *shell, unsigned int index)
1121{
1122 struct workspace *ws;
1123
1124 ws = get_workspace(shell, index);
1125 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1126
1127 shell->workspaces.current = index;
1128}
1129
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130static unsigned int
1131get_output_height(struct weston_output *output)
1132{
1133 return abs(output->region.extents.y1 - output->region.extents.y2);
1134}
1135
1136static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001137view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001138{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001139 struct weston_transform *transform;
1140
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001141 if (is_focus_view(view)) {
1142 struct focus_surface *fsurf = get_focus_surface(view->surface);
1143 transform = &fsurf->workspace_transform;
1144 } else {
1145 struct shell_surface *shsurf = get_shell_surface(view->surface);
1146 transform = &shsurf->workspace_transform;
1147 }
1148
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001149 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001151 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001153 weston_matrix_init(&transform->matrix);
1154 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001157}
1158
1159static void
1160workspace_translate_out(struct workspace *ws, double fraction)
1161{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163 unsigned int height;
1164 double d;
1165
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001166 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001168 d = height * fraction;
1169
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001170 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001171 }
1172}
1173
1174static void
1175workspace_translate_in(struct workspace *ws, double fraction)
1176{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001178 unsigned int height;
1179 double d;
1180
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001181 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001183
1184 if (fraction > 0)
1185 d = -(height - height * fraction);
1186 else
1187 d = height + height * fraction;
1188
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001189 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001190 }
1191}
1192
1193static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001194reverse_workspace_change_animation(struct desktop_shell *shell,
1195 unsigned int index,
1196 struct workspace *from,
1197 struct workspace *to)
1198{
1199 shell->workspaces.current = index;
1200
1201 shell->workspaces.anim_to = to;
1202 shell->workspaces.anim_from = from;
1203 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1204 shell->workspaces.anim_timestamp = 0;
1205
Scott Moreau4272e632012-08-13 09:58:41 -06001206 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001207}
1208
1209static void
1210workspace_deactivate_transforms(struct workspace *ws)
1211{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001213 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001214
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001215 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001216 if (is_focus_view(view)) {
1217 struct focus_surface *fsurf = get_focus_surface(view->surface);
1218 transform = &fsurf->workspace_transform;
1219 } else {
1220 struct shell_surface *shsurf = get_shell_surface(view->surface);
1221 transform = &shsurf->workspace_transform;
1222 }
1223
1224 if (!wl_list_empty(&transform->link)) {
1225 wl_list_remove(&transform->link);
1226 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001227 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001229 }
1230}
1231
1232static void
1233finish_workspace_change_animation(struct desktop_shell *shell,
1234 struct workspace *from,
1235 struct workspace *to)
1236{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001237 struct weston_view *view;
1238
Scott Moreau4272e632012-08-13 09:58:41 -06001239 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001240
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001241 /* Views that extend past the bottom of the output are still
1242 * visible after the workspace animation ends but before its layer
1243 * is hidden. In that case, we need to damage below those views so
1244 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001245 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001246 weston_view_damage_below(view);
1247
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001248 wl_list_remove(&shell->workspaces.animation.link);
1249 workspace_deactivate_transforms(from);
1250 workspace_deactivate_transforms(to);
1251 shell->workspaces.anim_to = NULL;
1252
1253 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1254}
1255
1256static void
1257animate_workspace_change_frame(struct weston_animation *animation,
1258 struct weston_output *output, uint32_t msecs)
1259{
1260 struct desktop_shell *shell =
1261 container_of(animation, struct desktop_shell,
1262 workspaces.animation);
1263 struct workspace *from = shell->workspaces.anim_from;
1264 struct workspace *to = shell->workspaces.anim_to;
1265 uint32_t t;
1266 double x, y;
1267
1268 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1269 finish_workspace_change_animation(shell, from, to);
1270 return;
1271 }
1272
1273 if (shell->workspaces.anim_timestamp == 0) {
1274 if (shell->workspaces.anim_current == 0.0)
1275 shell->workspaces.anim_timestamp = msecs;
1276 else
1277 shell->workspaces.anim_timestamp =
1278 msecs -
1279 /* Invers of movement function 'y' below. */
1280 (asin(1.0 - shell->workspaces.anim_current) *
1281 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1282 M_2_PI);
1283 }
1284
1285 t = msecs - shell->workspaces.anim_timestamp;
1286
1287 /*
1288 * x = [0, π/2]
1289 * y(x) = sin(x)
1290 */
1291 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1292 y = sin(x);
1293
1294 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001295 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001296
1297 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1298 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1299 shell->workspaces.anim_current = y;
1300
Scott Moreau4272e632012-08-13 09:58:41 -06001301 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001302 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001303 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001304 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001305}
1306
1307static void
1308animate_workspace_change(struct desktop_shell *shell,
1309 unsigned int index,
1310 struct workspace *from,
1311 struct workspace *to)
1312{
1313 struct weston_output *output;
1314
1315 int dir;
1316
1317 if (index > shell->workspaces.current)
1318 dir = -1;
1319 else
1320 dir = 1;
1321
1322 shell->workspaces.current = index;
1323
1324 shell->workspaces.anim_dir = dir;
1325 shell->workspaces.anim_from = from;
1326 shell->workspaces.anim_to = to;
1327 shell->workspaces.anim_current = 0.0;
1328 shell->workspaces.anim_timestamp = 0;
1329
1330 output = container_of(shell->compositor->output_list.next,
1331 struct weston_output, link);
1332 wl_list_insert(&output->animation_list,
1333 &shell->workspaces.animation.link);
1334
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001335 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001336
1337 workspace_translate_in(to, 0);
1338
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001339 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001340
Scott Moreau4272e632012-08-13 09:58:41 -06001341 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001342}
1343
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001344static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001345update_workspace(struct desktop_shell *shell, unsigned int index,
1346 struct workspace *from, struct workspace *to)
1347{
1348 shell->workspaces.current = index;
1349 wl_list_insert(&from->layer.link, &to->layer.link);
1350 wl_list_remove(&from->layer.link);
1351}
1352
1353static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001354change_workspace(struct desktop_shell *shell, unsigned int index)
1355{
1356 struct workspace *from;
1357 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001358 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001359
1360 if (index == shell->workspaces.current)
1361 return;
1362
1363 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001364 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001365 return;
1366
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001367 from = get_current_workspace(shell);
1368 to = get_workspace(shell, index);
1369
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001370 if (shell->workspaces.anim_from == to &&
1371 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001372 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001373 reverse_workspace_change_animation(shell, index, from, to);
1374 return;
1375 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001376
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001377 if (shell->workspaces.anim_to != NULL)
1378 finish_workspace_change_animation(shell,
1379 shell->workspaces.anim_from,
1380 shell->workspaces.anim_to);
1381
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001382 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001383
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001384 if (shell->focus_animation_type != ANIMATION_NONE) {
1385 wl_list_for_each(state, &from->focus_list, link)
1386 if (state->keyboard_focus)
1387 animate_focus_change(shell, from,
1388 get_default_view(state->keyboard_focus), NULL);
1389
1390 wl_list_for_each(state, &to->focus_list, link)
1391 if (state->keyboard_focus)
1392 animate_focus_change(shell, to,
1393 NULL, get_default_view(state->keyboard_focus));
1394 }
1395
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001396 if (workspace_is_empty(to) && workspace_is_empty(from))
1397 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001398 else
1399 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001400}
1401
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001402static bool
1403workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1404{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001405 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001406 struct wl_list *e;
1407
1408 if (wl_list_empty(list))
1409 return false;
1410
1411 e = list->next;
1412
1413 if (e->next != list)
1414 return false;
1415
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001416 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001417}
1418
1419static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001420surface_keyboard_focus_lost(struct weston_surface *surface)
1421{
1422 struct weston_compositor *compositor = surface->compositor;
1423 struct weston_seat *seat;
1424 struct weston_surface *focus;
1425
1426 wl_list_for_each(seat, &compositor->seat_list, link) {
1427 struct weston_keyboard *keyboard =
1428 weston_seat_get_keyboard(seat);
1429
1430 if (!keyboard)
1431 continue;
1432
1433 focus = weston_surface_get_main_surface(keyboard->focus);
1434 if (focus == surface)
1435 weston_keyboard_set_focus(keyboard, NULL);
1436 }
1437}
1438
1439static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001440take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001441 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001442 unsigned int index)
1443{
Derek Foreman1281a362015-07-31 16:55:32 -05001444 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001445 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001447 struct shell_surface *shsurf;
1448 struct workspace *from;
1449 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001450 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001451
Derek Foreman1281a362015-07-31 16:55:32 -05001452 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 view = get_default_view(surface);
1454 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001455 index == shell->workspaces.current ||
1456 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001457 return;
1458
1459 from = get_current_workspace(shell);
1460 to = get_workspace(shell, index);
1461
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001462 weston_layer_entry_remove(&view->layer_link);
1463 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001464
Philip Withnall659163d2013-11-25 18:01:36 +00001465 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001466 if (shsurf != NULL)
1467 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001468
Jonas Ådahle9d22502012-08-29 22:13:01 +02001469 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001470 drop_focus_state(shell, from, surface);
1471
1472 if (shell->workspaces.anim_from == to &&
1473 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001474 wl_list_remove(&to->layer.link);
1475 wl_list_insert(from->layer.link.prev, &to->layer.link);
1476
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001477 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001478
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001479 return;
1480 }
1481
1482 if (shell->workspaces.anim_to != NULL)
1483 finish_workspace_change_animation(shell,
1484 shell->workspaces.anim_from,
1485 shell->workspaces.anim_to);
1486
1487 if (workspace_is_empty(from) &&
1488 workspace_has_only(to, surface))
1489 update_workspace(shell, index, from, to);
1490 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001491 if (shsurf != NULL &&
1492 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001493 wl_list_insert(&shell->workspaces.anim_sticky_list,
1494 &shsurf->workspace_transform.link);
1495
1496 animate_workspace_change(shell, index, from, to);
1497 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001498
Jonas Ådahl8538b222012-08-29 22:13:03 +02001499 state = ensure_focus_state(shell, seat);
1500 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001501 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001502}
1503
1504static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001505touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001506 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001507{
1508}
1509
1510static void
1511touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1512{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001513 struct weston_touch_move_grab *move =
1514 (struct weston_touch_move_grab *) container_of(
1515 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001516
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001517 if (touch_id == 0)
1518 move->active = 0;
1519
Jonas Ådahl9484b692013-12-02 22:05:03 +01001520 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001521 shell_touch_grab_end(&move->base);
1522 free(move);
1523 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001524}
1525
1526static void
1527touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001528 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001529{
1530 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1531 struct shell_surface *shsurf = move->base.shsurf;
1532 struct weston_surface *es;
1533 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1534 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1535
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001536 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001537 return;
1538
1539 es = shsurf->surface;
1540
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001541 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001542
1543 weston_compositor_schedule_repaint(es->compositor);
1544}
1545
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001546static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001547touch_move_grab_frame(struct weston_touch_grab *grab)
1548{
1549}
1550
1551static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001552touch_move_grab_cancel(struct weston_touch_grab *grab)
1553{
1554 struct weston_touch_move_grab *move =
1555 (struct weston_touch_move_grab *) container_of(
1556 grab, struct shell_touch_grab, grab);
1557
1558 shell_touch_grab_end(&move->base);
1559 free(move);
1560}
1561
Rusty Lynch1084da52013-08-15 09:10:08 -07001562static const struct weston_touch_grab_interface touch_move_grab_interface = {
1563 touch_move_grab_down,
1564 touch_move_grab_up,
1565 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001566 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001567 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001568};
1569
1570static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001571surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001572{
1573 struct weston_touch_move_grab *move;
1574
1575 if (!shsurf)
1576 return -1;
1577
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001578 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001579 return 0;
1580
1581 move = malloc(sizeof *move);
1582 if (!move)
1583 return -1;
1584
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001585 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001587 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001589 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001590
1591 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001592 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001593
1594 return 0;
1595}
1596
1597static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001598noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001599{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001600}
1601
1602static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001603noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001604 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001605{
1606}
1607
1608static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001609noop_grab_axis_source(struct weston_pointer_grab *grab,
1610 uint32_t source)
1611{
1612}
1613
1614static void
1615noop_grab_frame(struct weston_pointer_grab *grab)
1616{
1617}
1618
1619static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001620constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1621{
1622 struct shell_surface *shsurf = move->base.shsurf;
1623 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001624 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001625 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001626 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001627
1628 x = wl_fixed_to_int(pointer->x + move->dx);
1629 y = wl_fixed_to_int(pointer->y + move->dy);
1630
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001631 if (shsurf->shell->panel_position ==
1632 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001633 get_output_work_area(shsurf->shell,
1634 shsurf->surface->output,
1635 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001636
Derek Foreman6feb0f92015-04-15 12:23:56 -05001637 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1638 if (bottom - safety < area.y)
1639 y = area.y + safety - shsurf->geometry.height
1640 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001641
1642 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001643 y + shsurf->geometry.y < area.y)
1644 y = area.y - shsurf->geometry.y;
1645
Jonny Lambd73c6942014-08-20 15:53:20 +02001646 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001647
1648 *cx = x;
1649 *cy = y;
1650}
1651
1652static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001653move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001654 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001655{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001656 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001657 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001658 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001659 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001660
Jonas Ådahld2510102014-10-05 21:39:14 +02001661 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001662 if (!shsurf)
1663 return;
1664
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001665 constrain_position(move, &cx, &cy);
1666
1667 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001668
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001670}
1671
1672static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001673move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001674 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001675{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001676 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1677 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001678 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001679 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001680
Daniel Stone4dbadb12012-05-30 16:31:51 +01001681 if (pointer->button_count == 0 &&
1682 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001683 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001684 free(grab);
1685 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001686}
1687
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001688static void
1689move_grab_cancel(struct weston_pointer_grab *grab)
1690{
1691 struct shell_grab *shell_grab =
1692 container_of(grab, struct shell_grab, grab);
1693
1694 shell_grab_end(shell_grab);
1695 free(grab);
1696}
1697
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001698static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001699 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001700 move_grab_motion,
1701 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001702 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001703 noop_grab_axis_source,
1704 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001705 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001706};
1707
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001709surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001710 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001711{
1712 struct weston_move_grab *move;
1713
1714 if (!shsurf)
1715 return -1;
1716
Derek Foreman45a7c272015-09-11 14:27:40 -05001717 shsurf = find_toplevel_surface(shsurf);
1718
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001719 if (shsurf->grabbed ||
1720 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001721 return 0;
1722
1723 move = malloc(sizeof *move);
1724 if (!move)
1725 return -1;
1726
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001728 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001729 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001730 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001731 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732
1733 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001734 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001735
1736 return 0;
1737}
1738
1739static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001740common_surface_move(struct wl_resource *resource,
1741 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001742{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001743 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001744 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1745 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001746 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001747 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001748
Derek Foreman1281a362015-07-31 16:55:32 -05001749 if (pointer &&
1750 pointer->focus &&
1751 pointer->button_count > 0 &&
1752 pointer->grab_serial == serial) {
1753 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001754 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001755 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001756 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001757 } else if (touch &&
1758 touch->focus &&
1759 touch->grab_serial == serial) {
1760 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001761 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001762 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001763 wl_resource_post_no_memory(resource);
1764 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001765}
1766
Rafael Antognollie2a34552013-12-03 15:35:45 -02001767static void
1768shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1769 struct wl_resource *seat_resource, uint32_t serial)
1770{
1771 common_surface_move(resource, seat_resource, serial);
1772}
1773
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774struct weston_resize_grab {
1775 struct shell_grab base;
1776 uint32_t edges;
1777 int32_t width, height;
1778};
1779
1780static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001781resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001782 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001783{
1784 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001785 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001786 struct shell_surface *shsurf = resize->base.shsurf;
1787 int32_t width, height;
1788 wl_fixed_t from_x, from_y;
1789 wl_fixed_t to_x, to_y;
1790
Jonas Ådahld2510102014-10-05 21:39:14 +02001791 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001792
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001793 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001794 return;
1795
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796 weston_view_from_global_fixed(shsurf->view,
1797 pointer->grab_x, pointer->grab_y,
1798 &from_x, &from_y);
1799 weston_view_from_global_fixed(shsurf->view,
1800 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001801
1802 width = resize->width;
1803 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1804 width += wl_fixed_to_int(from_x - to_x);
1805 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1806 width += wl_fixed_to_int(to_x - from_x);
1807 }
1808
1809 height = resize->height;
1810 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1811 height += wl_fixed_to_int(from_y - to_y);
1812 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1813 height += wl_fixed_to_int(to_y - from_y);
1814 }
1815
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001816 if (width < 1)
1817 width = 1;
1818 if (height < 1)
1819 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001820 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001821}
1822
1823static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001824send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001825{
1826 struct shell_surface *shsurf = get_shell_surface(surface);
1827
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001828 assert(shsurf);
1829
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001830 if (shsurf->resource)
1831 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001832 shsurf->resize_edges,
1833 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001834}
1835
1836static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001837 send_configure,
1838 NULL
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001839};
1840
1841static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001842resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001843 uint32_t time, uint32_t button, uint32_t state_w)
1844{
1845 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001846 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001847 enum wl_pointer_button_state state = state_w;
1848
1849 if (pointer->button_count == 0 &&
1850 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1851 shell_grab_end(&resize->base);
1852 free(grab);
1853 }
1854}
1855
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001856static void
1857resize_grab_cancel(struct weston_pointer_grab *grab)
1858{
1859 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1860
1861 shell_grab_end(&resize->base);
1862 free(grab);
1863}
1864
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001865static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001866 noop_grab_focus,
1867 resize_grab_motion,
1868 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001869 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001870 noop_grab_axis_source,
1871 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001872 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001873};
1874
Giulio Camuffob8366642013-04-25 13:57:46 +03001875/*
1876 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001877 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001878static void
1879surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1880 int32_t *y, int32_t *w, int32_t *h) {
1881 pixman_region32_t region;
1882 pixman_box32_t *box;
1883 struct weston_subsurface *subsurface;
1884
1885 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001886 surface->width,
1887 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001888
1889 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1890 pixman_region32_union_rect(&region, &region,
1891 subsurface->position.x,
1892 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001893 subsurface->surface->width,
1894 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001895 }
1896
1897 box = pixman_region32_extents(&region);
1898 if (x)
1899 *x = box->x1;
1900 if (y)
1901 *y = box->y1;
1902 if (w)
1903 *w = box->x2 - box->x1;
1904 if (h)
1905 *h = box->y2 - box->y1;
1906
1907 pixman_region32_fini(&region);
1908}
1909
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001910static int
1911surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001912 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001913{
1914 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001915 const unsigned resize_topbottom =
1916 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1917 const unsigned resize_leftright =
1918 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1919 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001920
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001921 if (shsurf->grabbed ||
1922 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001923 return 0;
1924
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001925 /* Check for invalid edge combinations. */
1926 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1927 (edges & resize_topbottom) == resize_topbottom ||
1928 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001929 return 0;
1930
1931 resize = malloc(sizeof *resize);
1932 if (!resize)
1933 return -1;
1934
1935 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001936
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001937 resize->width = shsurf->geometry.width;
1938 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001939
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001940 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001941 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001942 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001943 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001944
1945 return 0;
1946}
1947
1948static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001949common_surface_resize(struct wl_resource *resource,
1950 struct wl_resource *seat_resource, uint32_t serial,
1951 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001952{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001953 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001954 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001955 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001956 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001957
Derek Foreman1281a362015-07-31 16:55:32 -05001958 if (!pointer ||
1959 pointer->button_count == 0 ||
1960 pointer->grab_serial != serial ||
1961 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001962 return;
1963
Derek Foreman1281a362015-07-31 16:55:32 -05001964 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001965 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001966 return;
1967
Derek Foreman1281a362015-07-31 16:55:32 -05001968 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001969 wl_resource_post_no_memory(resource);
1970}
1971
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001972static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001973shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1974 struct wl_resource *seat_resource, uint32_t serial,
1975 uint32_t edges)
1976{
1977 common_surface_resize(resource, seat_resource, serial, edges);
1978}
1979
1980static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001981busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001982{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001983 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001984 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001986 wl_fixed_t sx, sy;
1987
Jason Ekstranda7af7042013-10-12 22:38:11 -05001988 view = weston_compositor_pick_view(pointer->seat->compositor,
1989 pointer->x, pointer->y,
1990 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001991
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001992 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1993 shell_grab_end(grab);
1994 free(grab);
1995 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001996}
1997
1998static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001999busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02002000 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002001{
Jonas Ådahld2510102014-10-05 21:39:14 +02002002 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002003}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002004
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002005static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002006busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002007 uint32_t time, uint32_t button, uint32_t state)
2008{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002009 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002010 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002011 struct weston_pointer *pointer = grab->grab.pointer;
2012 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002013
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002014 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002015 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002016 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002017 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002018 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002019 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002020 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002021}
2022
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002023static void
2024busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2025{
2026 struct shell_grab *grab = (struct shell_grab *) base;
2027
2028 shell_grab_end(grab);
2029 free(grab);
2030}
2031
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002032static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002033 busy_cursor_grab_focus,
2034 busy_cursor_grab_motion,
2035 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002036 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002037 noop_grab_axis_source,
2038 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002039 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002040};
2041
2042static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002043set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002044{
2045 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002046
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002047 if (pointer->grab->interface == &busy_cursor_grab_interface)
2048 return;
2049
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002050 grab = malloc(sizeof *grab);
2051 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002052 return;
2053
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002054 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002055 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002056 /* Mark the shsurf as ungrabbed so that button binding is able
2057 * to move it. */
2058 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002059}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002060
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002061static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002062end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002063{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002064 struct shell_grab *grab;
2065 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002066
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002067 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002068 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2069
2070 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002071 continue;
2072
Derek Foreman1281a362015-07-31 16:55:32 -05002073 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002074 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002075 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002076 wl_resource_get_client(grab->shsurf->resource) == client) {
2077 shell_grab_end(grab);
2078 free(grab);
2079 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002080 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002081}
2082
Scott Moreau9521d5e2012-04-19 13:06:17 -06002083static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002084handle_shell_client_destroy(struct wl_listener *listener, void *data);
2085
2086static int
2087xdg_ping_timeout_handler(void *data)
2088{
2089 struct shell_client *sc = data;
2090 struct weston_seat *seat;
2091 struct shell_surface *shsurf;
2092
2093 /* Client is not responding */
2094 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002095 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002096 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2097
2098 if (!pointer ||
2099 !pointer->focus ||
2100 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002101 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002102
Derek Foreman1281a362015-07-31 16:55:32 -05002103 shsurf = get_shell_surface(pointer->focus->surface);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002104 if (shsurf && shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002105 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002106 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002107 }
2108
2109 return 1;
2110}
2111
2112static void
2113handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2114{
2115 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002116 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002117 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002118 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002119
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002120 if (sc->unresponsive) {
2121 xdg_ping_timeout_handler(sc);
2122 return;
2123 }
2124
2125 sc->ping_serial = serial;
2126 loop = wl_display_get_event_loop(compositor->wl_display);
2127 if (sc->ping_timer == NULL)
2128 sc->ping_timer =
2129 wl_event_loop_add_timer(loop,
2130 xdg_ping_timeout_handler, sc);
2131 if (sc->ping_timer == NULL)
2132 return;
2133
2134 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2135
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002136 if (shell_surface_is_xdg_surface(shsurf) ||
2137 shell_surface_is_xdg_popup(shsurf))
2138 xdg_shell_send_ping(sc->resource, serial);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002139 else if (shell_surface_is_wl_shell_surface(shsurf)
2140 && shsurf->resource)
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002141 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002142}
2143
Scott Moreauff1db4a2012-04-17 19:06:18 -06002144static void
2145ping_handler(struct weston_surface *surface, uint32_t serial)
2146{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002147 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002148
2149 if (!shsurf)
2150 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002151 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002152 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002153 if (shsurf->surface == shsurf->shell->grab_surface)
2154 return;
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08002155 if (!shsurf->owner)
2156 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002157
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002158 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002159}
2160
2161static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002162handle_pointer_focus(struct wl_listener *listener, void *data)
2163{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002164 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002166 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002167 uint32_t serial;
2168
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002170 return;
2171
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002173 serial = wl_display_next_serial(compositor->wl_display);
2174 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002175}
2176
2177static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002178shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2179{
2180 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002181 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002182}
2183
2184static void
2185shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2186{
2187 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002188 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002189}
2190
2191static void
2192handle_keyboard_focus(struct wl_listener *listener, void *data)
2193{
2194 struct weston_keyboard *keyboard = data;
2195 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2196
2197 if (seat->focused_surface) {
2198 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2199 if (shsurf)
2200 shell_surface_lose_keyboard_focus(shsurf);
2201 }
2202
2203 seat->focused_surface = keyboard->focus;
2204
2205 if (seat->focused_surface) {
2206 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2207 if (shsurf)
2208 shell_surface_gain_keyboard_focus(shsurf);
2209 }
2210}
2211
2212static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002213shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002214{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002215 if (sc->ping_serial != serial)
2216 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002217
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002218 sc->unresponsive = 0;
2219 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002220
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002221 if (sc->ping_timer) {
2222 wl_event_source_remove(sc->ping_timer);
2223 sc->ping_timer = NULL;
2224 }
2225
2226}
2227
2228static void
2229shell_surface_pong(struct wl_client *client,
2230 struct wl_resource *resource, uint32_t serial)
2231{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002232 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2233 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002234
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002235 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002236}
2237
2238static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002239set_title(struct shell_surface *shsurf, const char *title)
2240{
2241 free(shsurf->title);
2242 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002243 shsurf->surface->timeline.force_refresh = 1;
2244}
2245
2246static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002247set_pid(struct shell_surface *shsurf, pid_t pid)
2248{
2249 /* We have no use for it */
2250}
2251
2252static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002253set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2254{
2255 shsurf->type = t;
2256 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002257}
2258
2259static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002260set_window_geometry(struct shell_surface *shsurf,
2261 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002262{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002263 shsurf->next_geometry.x = x;
2264 shsurf->next_geometry.y = y;
2265 shsurf->next_geometry.width = width;
2266 shsurf->next_geometry.height = height;
2267 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002268}
2269
2270static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002271shell_surface_set_title(struct wl_client *client,
2272 struct wl_resource *resource, const char *title)
2273{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002274 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002275
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002276 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002277}
2278
2279static void
2280shell_surface_set_class(struct wl_client *client,
2281 struct wl_resource *resource, const char *class)
2282{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002283 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002284
2285 free(shsurf->class);
2286 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002287 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002288}
2289
Alex Wu4539b082012-03-01 12:57:46 +08002290static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002291restore_output_mode(struct weston_output *output)
2292{
Armin Krezovićc77f2582016-06-23 11:59:32 +02002293 if (output && output->original_mode)
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002294 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002295}
2296
2297static void
2298restore_all_output_modes(struct weston_compositor *compositor)
2299{
2300 struct weston_output *output;
2301
2302 wl_list_for_each(output, &compositor->output_list, link)
2303 restore_output_mode(output);
2304}
2305
Philip Withnall07926d92013-11-25 18:01:40 +00002306/* The surface will be inserted into the list immediately after the link
2307 * returned by this function (i.e. will be stacked immediately above the
2308 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002309static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002310shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2311{
2312 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002313 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002314
2315 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002316 case SHELL_SURFACE_XWAYLAND:
2317 return &shsurf->shell->fullscreen_layer.view_list;
2318
2319 case SHELL_SURFACE_NONE:
2320 return NULL;
2321
Kristian Høgsbergead52422014-01-01 13:51:52 -08002322 case SHELL_SURFACE_POPUP:
2323 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002324 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002325 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002326 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002327 /* Move the surface to its parent layer so
2328 * that surfaces which are transient for
2329 * fullscreen surfaces don't get hidden by the
2330 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002331
2332 /* TODO: Handle a parent with multiple views */
2333 parent = get_default_view(shsurf->parent);
2334 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002335 return container_of(parent->layer_link.link.prev,
2336 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002337 }
Philip Withnall07926d92013-11-25 18:01:40 +00002338
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002339 /* Move the surface to a normal workspace layer so that surfaces
2340 * which were previously fullscreen or transient are no longer
2341 * rendered on top. */
2342 ws = get_current_workspace(shsurf->shell);
2343 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002344 }
2345
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002346 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002347}
2348
Philip Withnall648a4dd2013-11-25 18:01:44 +00002349static void
2350shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2351{
2352 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002353 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002354
2355 /* Move the child layers to the same workspace as shsurf. They will be
2356 * stacked above shsurf. */
2357 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002358 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002359 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002360 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002361 prev = container_of(shsurf->view->layer_link.link.prev,
2362 struct weston_layer_entry, link);
2363 weston_layer_entry_remove(&child->view->layer_link);
2364 weston_layer_entry_insert(prev,
2365 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002366 weston_view_geometry_dirty(child->view);
2367 weston_surface_damage(child->surface);
2368
2369 /* Recurse. We don’t expect this to recurse very far (if
2370 * at all) because that would imply we have transient
2371 * (or popup) children of transient surfaces, which
2372 * would be unusual. */
2373 shell_surface_update_child_surface_layers(child);
2374 }
2375 }
2376}
2377
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002378/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002379 * geometry to ensure the changes are redrawn.
2380 *
2381 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2382 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002383static void
2384shell_surface_update_layer(struct shell_surface *shsurf)
2385{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002386 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002387
2388 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2389
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002390 if (new_layer_link == NULL)
2391 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002392 if (new_layer_link == &shsurf->view->layer_link)
2393 return;
2394
2395 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002396 weston_layer_entry_remove(&shsurf->view->layer_link);
2397 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002398 weston_view_geometry_dirty(shsurf->view);
2399 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002400
2401 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002402}
2403
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002404static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002405shell_surface_set_parent(struct shell_surface *shsurf,
2406 struct weston_surface *parent)
2407{
2408 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002409
2410 wl_list_remove(&shsurf->children_link);
2411 wl_list_init(&shsurf->children_link);
2412
2413 /* Insert into the parent surface’s child list. */
2414 if (parent != NULL) {
2415 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2416 if (parent_shsurf != NULL)
2417 wl_list_insert(&parent_shsurf->children_list,
2418 &shsurf->children_link);
2419 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002420}
2421
2422static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002423shell_surface_set_output(struct shell_surface *shsurf,
2424 struct weston_output *output)
2425{
2426 struct weston_surface *es = shsurf->surface;
2427
2428 /* get the default output, if the client set it as NULL
2429 check whether the ouput is available */
2430 if (output)
2431 shsurf->output = output;
2432 else if (es->output)
2433 shsurf->output = es->output;
2434 else
2435 shsurf->output = get_default_output(es->compositor);
2436}
2437
2438static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002439surface_clear_next_states(struct shell_surface *shsurf)
2440{
2441 shsurf->next_state.maximized = false;
2442 shsurf->next_state.fullscreen = false;
2443
2444 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2445 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2446 shsurf->state_changed = true;
2447}
2448
2449static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002450set_toplevel(struct shell_surface *shsurf)
2451{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002452 shell_surface_set_parent(shsurf, NULL);
2453 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002454 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002455
2456 /* The layer_link is updated in set_surface_type(),
2457 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002458}
2459
2460static void
2461shell_surface_set_toplevel(struct wl_client *client,
2462 struct wl_resource *resource)
2463{
2464 struct shell_surface *surface = wl_resource_get_user_data(resource);
2465
2466 set_toplevel(surface);
2467}
2468
2469static void
2470set_transient(struct shell_surface *shsurf,
2471 struct weston_surface *parent, int x, int y, uint32_t flags)
2472{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002473 assert(parent != NULL);
2474
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002475 shell_surface_set_parent(shsurf, parent);
2476
2477 surface_clear_next_states(shsurf);
2478
Philip Withnallbecb77e2013-11-25 18:01:30 +00002479 shsurf->transient.x = x;
2480 shsurf->transient.y = y;
2481 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002482
Rafael Antognollied207b42013-12-03 15:35:43 -02002483 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002484 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002485 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002486
2487 /* The layer_link is updated in set_surface_type(),
2488 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002489}
2490
2491static void
2492shell_surface_set_transient(struct wl_client *client,
2493 struct wl_resource *resource,
2494 struct wl_resource *parent_resource,
2495 int x, int y, uint32_t flags)
2496{
2497 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2498 struct weston_surface *parent =
2499 wl_resource_get_user_data(parent_resource);
2500
2501 set_transient(shsurf, parent, x, y, flags);
2502}
2503
2504static void
2505set_fullscreen(struct shell_surface *shsurf,
2506 uint32_t method,
2507 uint32_t framerate,
2508 struct weston_output *output)
2509{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002510 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002511 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002512
2513 shsurf->fullscreen_output = shsurf->output;
2514 shsurf->fullscreen.type = method;
2515 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002516
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002517 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002518}
2519
2520static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002521weston_view_set_initial_position(struct weston_view *view,
2522 struct desktop_shell *shell);
2523
2524static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002525unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002526{
Philip Withnallf85fe842013-11-25 18:01:37 +00002527 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002528 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2529 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002530 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002531 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002532
Alex Wu4539b082012-03-01 12:57:46 +08002533 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2534 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002535
Alex Wu4539b082012-03-01 12:57:46 +08002536 wl_list_remove(&shsurf->fullscreen.transform.link);
2537 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002538
Jason Ekstranda7af7042013-10-12 22:38:11 -05002539 if (shsurf->fullscreen.black_view)
2540 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2541 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002542
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002543 if (shsurf->saved_position_valid)
2544 weston_view_set_position(shsurf->view,
2545 shsurf->saved_x, shsurf->saved_y);
2546 else
2547 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2548
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002549 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002550 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002551 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002552 shsurf->saved_rotation_valid = false;
2553 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002554
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002555 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002556}
2557
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002558static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002559shell_surface_set_fullscreen(struct wl_client *client,
2560 struct wl_resource *resource,
2561 uint32_t method,
2562 uint32_t framerate,
2563 struct wl_resource *output_resource)
2564{
2565 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2566 struct weston_output *output;
2567
Emmanuel Gil Peyrot28834b62016-05-10 03:22:43 +02002568 if (shsurf->fullscreen_output == shsurf->output &&
2569 shsurf->fullscreen.type == method &&
2570 shsurf->fullscreen.framerate == framerate) {
2571 send_configure_for_surface(shsurf);
2572 return;
2573 }
2574
Philip Withnallbecb77e2013-11-25 18:01:30 +00002575 if (output_resource)
2576 output = wl_resource_get_user_data(output_resource);
2577 else
2578 output = NULL;
2579
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002580 shell_surface_set_parent(shsurf, NULL);
2581
Rafael Antognolli03b16592013-12-03 15:35:42 -02002582 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002583 shsurf->next_state.fullscreen = true;
2584 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002585 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002586}
2587
2588static void
2589set_popup(struct shell_surface *shsurf,
2590 struct weston_surface *parent,
2591 struct weston_seat *seat,
2592 uint32_t serial,
2593 int32_t x,
2594 int32_t y)
2595{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002596 assert(parent != NULL);
2597
Philip Withnallbecb77e2013-11-25 18:01:30 +00002598 shsurf->popup.shseat = get_shell_seat(seat);
2599 shsurf->popup.serial = serial;
2600 shsurf->popup.x = x;
2601 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002602
Pekka Paalanenb5026542014-11-12 15:09:24 +02002603 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002604}
2605
2606static void
2607shell_surface_set_popup(struct wl_client *client,
2608 struct wl_resource *resource,
2609 struct wl_resource *seat_resource,
2610 uint32_t serial,
2611 struct wl_resource *parent_resource,
2612 int32_t x, int32_t y, uint32_t flags)
2613{
2614 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002615 struct weston_surface *parent =
2616 wl_resource_get_user_data(parent_resource);
2617
2618 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002619
Rafael Antognolli03b16592013-12-03 15:35:42 -02002620 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002621 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002622 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002623 wl_resource_get_user_data(seat_resource),
2624 serial, x, y);
2625}
2626
2627static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002628unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002629{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002630 /* undo all maximized things here */
2631 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002632
2633 if (shsurf->saved_position_valid)
2634 weston_view_set_position(shsurf->view,
2635 shsurf->saved_x, shsurf->saved_y);
2636 else
2637 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002638
2639 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002640 wl_list_insert(&shsurf->view->geometry.transformation_list,
2641 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002642 shsurf->saved_rotation_valid = false;
2643 }
2644
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002645 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002646}
2647
Philip Withnallbecb77e2013-11-25 18:01:30 +00002648static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002649set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002650{
2651 struct shell_surface *shsurf;
2652 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002653 struct weston_view *view;
2654
2655 view = get_default_view(surface);
2656 if (!view)
2657 return;
2658
2659 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2660
2661 shsurf = get_shell_surface(surface);
2662 current_ws = get_current_workspace(shsurf->shell);
2663
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002664 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002665 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002666
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002667 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002668 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002669
2670 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002671 weston_view_damage_below(view);
2672}
2673
2674static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002675shell_surface_set_maximized(struct wl_client *client,
2676 struct wl_resource *resource,
2677 struct wl_resource *output_resource)
2678{
2679 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2680 struct weston_output *output;
2681
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002682 surface_clear_next_states(shsurf);
2683 shsurf->next_state.maximized = true;
2684 shsurf->state_changed = true;
2685
Pekka Paalanenb5026542014-11-12 15:09:24 +02002686 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002687 shell_surface_set_parent(shsurf, NULL);
2688
Philip Withnallbecb77e2013-11-25 18:01:30 +00002689 if (output_resource)
2690 output = wl_resource_get_user_data(output_resource);
2691 else
2692 output = NULL;
2693
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002694 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002695
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002696 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002697}
2698
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002699/* This is only ever called from set_surface_type(), so there’s no need to
2700 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002701static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002702reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002703{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002704 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002705 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002706 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002707 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002708
Pekka Paalanen98262232011-12-01 10:42:22 +02002709 return 0;
2710}
2711
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002712static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002713set_full_output(struct shell_surface *shsurf)
2714{
2715 shsurf->saved_x = shsurf->view->geometry.x;
2716 shsurf->saved_y = shsurf->view->geometry.y;
2717 shsurf->saved_position_valid = true;
2718
2719 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2720 wl_list_remove(&shsurf->rotation.transform.link);
2721 wl_list_init(&shsurf->rotation.transform.link);
2722 weston_view_geometry_dirty(shsurf->view);
2723 shsurf->saved_rotation_valid = true;
2724 }
2725}
2726
2727static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002728set_surface_type(struct shell_surface *shsurf)
2729{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002730 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002731 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002732
Philip Withnallbecb77e2013-11-25 18:01:30 +00002733 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002734
Rafael Antognolli03b16592013-12-03 15:35:42 -02002735 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002736 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002737
2738 switch (shsurf->type) {
2739 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002740 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002741 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002742 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002743 weston_view_set_position(shsurf->view,
2744 pev->geometry.x + shsurf->transient.x,
2745 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002746 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002747 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002748
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002749 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002750 weston_view_set_position(shsurf->view, shsurf->transient.x,
2751 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002752 break;
2753
Philip Withnall0f640e22013-11-25 18:01:31 +00002754 case SHELL_SURFACE_POPUP:
2755 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002756 default:
2757 break;
2758 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002759
2760 /* Update the surface’s layer. */
2761 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002762}
2763
Tiago Vignattibe143262012-04-16 17:31:41 +03002764static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002765shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002766{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002767 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002768}
2769
Pekka Paalanen8274d902014-08-06 19:36:51 +03002770static int
2771black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2772{
2773 struct weston_surface *fs_surface = surface->configure_private;
2774 int n;
2775 int rem;
2776 int ret;
2777
2778 n = snprintf(buf, len, "black background surface for ");
2779 if (n < 0)
2780 return n;
2781
2782 rem = (int)len - n;
2783 if (rem < 0)
2784 rem = 0;
2785
2786 if (fs_surface->get_label)
2787 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2788 else
2789 ret = snprintf(buf + n, rem, "<unknown>");
2790
2791 if (ret < 0)
2792 return n;
2793
2794 return n + ret;
2795}
2796
Alex Wu21858432012-04-01 20:13:08 +08002797static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002798black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002799
Jason Ekstranda7af7042013-10-12 22:38:11 -05002800static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002801create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002802 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002803 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002804{
2805 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002806 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002807
2808 surface = weston_surface_create(ec);
2809 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002810 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002811 return NULL;
2812 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002813 view = weston_view_create(surface);
2814 if (surface == NULL) {
2815 weston_log("no memory\n");
2816 weston_surface_destroy(surface);
2817 return NULL;
2818 }
Alex Wu4539b082012-03-01 12:57:46 +08002819
Alex Wu21858432012-04-01 20:13:08 +08002820 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002821 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002822 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002823 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002824 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002825 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002826 pixman_region32_fini(&surface->input);
2827 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002828
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002829 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002830 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002831
2832 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002833}
2834
Philip Withnalled8f1a92013-11-25 18:01:43 +00002835static void
2836shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2837{
2838 struct weston_output *output = shsurf->fullscreen_output;
2839
Rafael Antognolli03b16592013-12-03 15:35:42 -02002840 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002841
2842 if (!shsurf->fullscreen.black_view)
2843 shsurf->fullscreen.black_view =
2844 create_black_surface(shsurf->surface->compositor,
2845 shsurf->surface,
2846 output->x, output->y,
2847 output->width,
2848 output->height);
2849
2850 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002851 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2852 weston_layer_entry_insert(&shsurf->view->layer_link,
2853 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002854 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2855 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002856
2857 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002858}
2859
Alex Wu4539b082012-03-01 12:57:46 +08002860/* Create black surface and append it to the associated fullscreen surface.
2861 * Handle size dismatch and positioning according to the method. */
2862static void
2863shell_configure_fullscreen(struct shell_surface *shsurf)
2864{
2865 struct weston_output *output = shsurf->fullscreen_output;
2866 struct weston_surface *surface = shsurf->surface;
2867 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002868 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002869 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002870
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002871 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2872 restore_output_mode(output);
2873
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002874 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002875 weston_layer_entry_remove(&shsurf->view->layer_link);
2876 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002877
Philip Withnalled8f1a92013-11-25 18:01:43 +00002878 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002879
Jason Ekstranda7af7042013-10-12 22:38:11 -05002880 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002881 &surf_width, &surf_height);
2882
Alex Wu4539b082012-03-01 12:57:46 +08002883 switch (shsurf->fullscreen.type) {
2884 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002885 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002886 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002887 break;
2888 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002889 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002890 if (output->width == surf_width &&
2891 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002892 weston_view_set_position(shsurf->view,
2893 output->x - surf_x,
2894 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002895 break;
2896 }
2897
Alex Wu4539b082012-03-01 12:57:46 +08002898 matrix = &shsurf->fullscreen.transform.matrix;
2899 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002900
Scott Moreau1bad5db2012-08-18 01:04:05 -06002901 output_aspect = (float) output->width /
2902 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002903 /* XXX: Use surf_width and surf_height here? */
2904 surface_aspect = (float) surface->width /
2905 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002906 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002907 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002908 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002909 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002910 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002911 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002912
Alex Wu4539b082012-03-01 12:57:46 +08002913 weston_matrix_scale(matrix, scale, scale, 1);
2914 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002915 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002916 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002917 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2918 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002919 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002920
Alex Wu4539b082012-03-01 12:57:46 +08002921 break;
2922 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002923 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002924 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002925 surf_width * surface->buffer_viewport.buffer.scale,
2926 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002927 shsurf->fullscreen.framerate};
2928
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002929 if (weston_output_mode_switch_to_temporary(output, &mode,
2930 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002931 weston_view_set_position(shsurf->view,
2932 output->x - surf_x,
2933 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002934 shsurf->fullscreen.black_view->surface->width = output->width;
2935 shsurf->fullscreen.black_view->surface->height = output->height;
2936 weston_view_set_position(shsurf->fullscreen.black_view,
2937 output->x - surf_x,
2938 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002939 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002940 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002941 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002942 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002943 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002944 }
Alex Wubd3354b2012-04-17 17:20:49 +08002945 }
Alex Wu4539b082012-03-01 12:57:46 +08002946 break;
2947 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002948 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002949 break;
2950 default:
2951 break;
2952 }
2953}
2954
Alex Wu4539b082012-03-01 12:57:46 +08002955static void
2956shell_map_fullscreen(struct shell_surface *shsurf)
2957{
Alex Wubd3354b2012-04-17 17:20:49 +08002958 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002959}
2960
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002961static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002962set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2963{
2964 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002965 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002966 shsurf->transient.x = x;
2967 shsurf->transient.y = y;
2968 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002969
2970 shell_surface_set_parent(shsurf, NULL);
2971
Pekka Paalanenb5026542014-11-12 15:09:24 +02002972 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002973 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002974}
2975
Kristian Høgsberg47792412014-05-04 13:47:06 -07002976static void
2977shell_interface_set_fullscreen(struct shell_surface *shsurf,
2978 uint32_t method,
2979 uint32_t framerate,
2980 struct weston_output *output)
2981{
2982 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002983 shsurf->next_state.fullscreen = true;
2984 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002985
2986 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002987}
2988
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002989static struct weston_output *
2990get_focused_output(struct weston_compositor *compositor)
2991{
2992 struct weston_seat *seat;
2993 struct weston_output *output = NULL;
2994
2995 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002996 struct weston_touch *touch = weston_seat_get_touch(seat);
2997 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2998 struct weston_keyboard *keyboard =
2999 weston_seat_get_keyboard(seat);
3000
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003001 /* Priority has touch focus, then pointer and
3002 * then keyboard focus. We should probably have
3003 * three for loops and check frist for touch,
3004 * then for pointer, etc. but unless somebody has some
3005 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05003006 if (touch && touch->focus)
3007 output = touch->focus->output;
3008 else if (pointer && pointer->focus)
3009 output = pointer->focus->output;
3010 else if (keyboard && keyboard->focus)
3011 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003012
3013 if (output)
3014 break;
3015 }
3016
3017 return output;
3018}
3019
3020static void
3021shell_interface_set_maximized(struct shell_surface *shsurf)
3022{
3023 struct weston_output *output;
3024
3025 surface_clear_next_states(shsurf);
3026 shsurf->next_state.maximized = true;
3027 shsurf->state_changed = true;
3028 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3029
3030 if (!weston_surface_is_mapped(shsurf->surface))
3031 output = get_focused_output(shsurf->surface->compositor);
3032 else
3033 output = shsurf->surface->output;
3034
3035 shell_surface_set_output(shsurf, output);
3036 send_configure_for_surface(shsurf);
3037}
3038
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003039static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003040shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003041{
Derek Foremane4d6c832015-08-05 14:48:11 -05003042 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003043}
3044
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003045static int
3046shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003047 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003048 uint32_t edges)
3049{
Derek Foremane4d6c832015-08-05 14:48:11 -05003050 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003051}
3052
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003053static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003054
3055static void
3056destroy_shell_seat(struct wl_listener *listener, void *data)
3057{
3058 struct shell_seat *shseat =
3059 container_of(listener,
3060 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003061 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003062
3063 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003064 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003065 shseat->popup_grab.client = NULL;
3066
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003067 wl_list_for_each_safe(shsurf, next,
3068 &shseat->popup_grab.surfaces_list,
3069 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003070 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003071 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003072 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003073 }
3074
3075 wl_list_remove(&shseat->seat_destroy_listener.link);
3076 free(shseat);
3077}
3078
Jason Ekstrand024177c2014-04-21 19:42:58 -05003079static void
3080shell_seat_caps_changed(struct wl_listener *listener, void *data)
3081{
Derek Foreman1281a362015-07-31 16:55:32 -05003082 struct weston_keyboard *keyboard;
3083 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003084 struct shell_seat *seat;
3085
3086 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003087 keyboard = weston_seat_get_keyboard(seat->seat);
3088 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003089
Derek Foreman1281a362015-07-31 16:55:32 -05003090 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003091 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003092 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003093 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003094 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003095 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003096 wl_list_init(&seat->keyboard_focus_listener.link);
3097 }
3098
Derek Foreman1281a362015-07-31 16:55:32 -05003099 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003100 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003101 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003102 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003103 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003104 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003105 wl_list_init(&seat->pointer_focus_listener.link);
3106 }
3107}
3108
Giulio Camuffo5085a752013-03-25 21:42:45 +01003109static struct shell_seat *
3110create_shell_seat(struct weston_seat *seat)
3111{
3112 struct shell_seat *shseat;
3113
3114 shseat = calloc(1, sizeof *shseat);
3115 if (!shseat) {
3116 weston_log("no memory to allocate shell seat\n");
3117 return NULL;
3118 }
3119
3120 shseat->seat = seat;
3121 wl_list_init(&shseat->popup_grab.surfaces_list);
3122
3123 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3124 wl_signal_add(&seat->destroy_signal,
3125 &shseat->seat_destroy_listener);
3126
Jason Ekstrand024177c2014-04-21 19:42:58 -05003127 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3128 wl_list_init(&shseat->keyboard_focus_listener.link);
3129
3130 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3131 wl_list_init(&shseat->pointer_focus_listener.link);
3132
3133 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3134 wl_signal_add(&seat->updated_caps_signal,
3135 &shseat->caps_changed_listener);
3136 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3137
Giulio Camuffo5085a752013-03-25 21:42:45 +01003138 return shseat;
3139}
3140
3141static struct shell_seat *
3142get_shell_seat(struct weston_seat *seat)
3143{
3144 struct wl_listener *listener;
3145
3146 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003147 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003148
3149 return container_of(listener,
3150 struct shell_seat, seat_destroy_listener);
3151}
3152
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003153static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003154popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003155{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003156 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003157 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003158 struct shell_seat *shseat =
3159 container_of(grab, struct shell_seat, popup_grab.grab);
3160 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003161 wl_fixed_t sx, sy;
3162
Jason Ekstranda7af7042013-10-12 22:38:11 -05003163 view = weston_compositor_pick_view(pointer->seat->compositor,
3164 pointer->x, pointer->y,
3165 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003166
Jason Ekstranda7af7042013-10-12 22:38:11 -05003167 if (view && view->surface->resource &&
3168 wl_resource_get_client(view->surface->resource) == client) {
3169 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003170 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003171 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003172 }
3173}
3174
3175static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003176popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003177 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003178{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003179 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003180 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003181 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003182 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003183 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003184
Jonas Ådahl61917c82014-08-11 22:44:02 +02003185 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003186 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003187 weston_view_from_global_fixed(pointer->focus, x, y,
3188 &pointer->sx, &pointer->sy);
3189 }
3190
Jonas Ådahld2510102014-10-05 21:39:14 +02003191 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003192
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003193 if (!pointer->focus_client)
3194 return;
3195
3196 resource_list = &pointer->focus_client->pointer_resources;
3197 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003198 weston_view_from_global_fixed(pointer->focus,
3199 pointer->x, pointer->y,
3200 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003201 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003202 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003203}
3204
3205static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003206popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003207 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003208{
3209 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003210 struct shell_seat *shseat =
3211 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003212 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003213 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003214 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003215 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003216
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003217 if (grab->pointer->focus_client)
3218 resource_list = &grab->pointer->focus_client->pointer_resources;
3219 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003220 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003221 wl_resource_for_each(resource, resource_list) {
3222 wl_pointer_send_button(resource, serial,
3223 time, button, state);
3224 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003225 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003226 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003227 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003228 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003229 }
3230
Daniel Stone4dbadb12012-05-30 16:31:51 +01003231 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003232 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003233}
3234
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003235static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003236popup_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003237 uint32_t time,
3238 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003239{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003240 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003241}
3242
3243static void
Peter Hutterer87743e92016-01-18 16:38:22 +10003244popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3245{
3246 weston_pointer_send_axis_source(grab->pointer, source);
3247}
3248
3249static void
3250popup_grab_frame(struct weston_pointer_grab *grab)
3251{
3252 weston_pointer_send_frame(grab->pointer);
3253}
3254
3255static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003256popup_grab_cancel(struct weston_pointer_grab *grab)
3257{
3258 popup_grab_end(grab->pointer);
3259}
3260
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003261static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003262 popup_grab_focus,
3263 popup_grab_motion,
3264 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003265 popup_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003266 popup_grab_axis_source,
3267 popup_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003268 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003269};
3270
3271static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003272touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003273 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003274{
3275 struct wl_resource *resource;
3276 struct shell_seat *shseat =
3277 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3278 struct wl_display *display = shseat->seat->compositor->wl_display;
3279 uint32_t serial;
3280 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003281 wl_fixed_t sx, sy;
3282
3283 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003284
3285 resource_list = &grab->touch->focus_resource_list;
3286 if (!wl_list_empty(resource_list)) {
3287 serial = wl_display_get_serial(display);
3288 wl_resource_for_each(resource, resource_list) {
3289 wl_touch_send_down(resource, serial, time,
3290 grab->touch->focus->surface->resource,
3291 touch_id, sx, sy);
3292 }
3293 }
3294}
3295
3296static void
3297touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3298{
3299 struct wl_resource *resource;
3300 struct shell_seat *shseat =
3301 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3302 struct wl_display *display = shseat->seat->compositor->wl_display;
3303 uint32_t serial;
3304 struct wl_list *resource_list;
3305
3306 resource_list = &grab->touch->focus_resource_list;
3307 if (!wl_list_empty(resource_list)) {
3308 serial = wl_display_get_serial(display);
3309 wl_resource_for_each(resource, resource_list) {
3310 wl_touch_send_up(resource, serial, time, touch_id);
3311 }
3312 }
3313}
3314
3315static void
3316touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003317 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003318{
3319 struct wl_resource *resource;
3320 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003321 wl_fixed_t sx, sy;
3322
3323 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003324
3325 resource_list = &grab->touch->focus_resource_list;
3326 if (!wl_list_empty(resource_list)) {
3327 wl_resource_for_each(resource, resource_list) {
3328 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3329 }
3330 }
3331}
3332
3333static void
3334touch_popup_grab_frame(struct weston_touch_grab *grab)
3335{
3336}
3337
3338static void
3339touch_popup_grab_cancel(struct weston_touch_grab *grab)
3340{
3341 touch_popup_grab_end(grab->touch);
3342}
3343
3344static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3345 touch_popup_grab_down,
3346 touch_popup_grab_up,
3347 touch_popup_grab_motion,
3348 touch_popup_grab_frame,
3349 touch_popup_grab_cancel,
3350};
3351
3352static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003353shell_surface_send_popup_done(struct shell_surface *shsurf)
3354{
Bryce Harrington22b1f932015-09-23 17:30:43 -07003355 if (shsurf->resource == NULL)
3356 return;
3357 else if (shell_surface_is_wl_shell_surface(shsurf))
Rafael Antognollie2a34552013-12-03 15:35:45 -02003358 wl_shell_surface_send_popup_done(shsurf->resource);
3359 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003360 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003361}
3362
3363static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003364popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003365{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003366 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003367 struct shell_seat *shseat =
3368 container_of(grab, struct shell_seat, popup_grab.grab);
3369 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003370 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003371
3372 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003373 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003374 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003375 shseat->popup_grab.grab.interface = NULL;
3376 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003377 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003378 wl_list_for_each_safe(shsurf, next,
3379 &shseat->popup_grab.surfaces_list,
3380 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003381 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003382 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003383 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003384 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003385 wl_list_init(&shseat->popup_grab.surfaces_list);
3386 }
3387}
3388
3389static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003390touch_popup_grab_end(struct weston_touch *touch)
3391{
3392 struct weston_touch_grab *grab = touch->grab;
3393 struct shell_seat *shseat =
3394 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3395 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003396 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003397
3398 if (touch->grab->interface == &touch_popup_grab_interface) {
3399 weston_touch_end_grab(grab->touch);
3400 shseat->popup_grab.client = NULL;
3401 shseat->popup_grab.touch_grab.interface = NULL;
3402 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3403 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003404 wl_list_for_each_safe(shsurf, next,
3405 &shseat->popup_grab.surfaces_list,
3406 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003407 shell_surface_send_popup_done(shsurf);
3408 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003409 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003410 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003411 wl_list_init(&shseat->popup_grab.surfaces_list);
3412 }
3413}
3414
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003415static struct shell_surface *
3416get_top_popup(struct shell_seat *shseat)
3417{
3418 struct shell_surface *shsurf;
3419
3420 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3421 return NULL;
3422 } else {
3423 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3424 struct shell_surface,
3425 popup.grab_link);
3426 return shsurf;
3427 }
3428}
3429
3430static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003431add_popup_grab(struct shell_surface *shsurf,
3432 struct shell_seat *shseat,
3433 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003434{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003435 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003436 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003437 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3438 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003439
3440 parent = get_shell_surface(shsurf->parent);
3441 top_surface = get_top_popup(shseat);
3442 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003443 (!parent ||
3444 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003445 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003446 wl_resource_post_error(shsurf->owner_resource,
3447 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003448 "xdg_popup was not created on the "
3449 "topmost popup");
3450 return -1;
3451 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003452
3453 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003454 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003455 shseat->popup_grab.client =
3456 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003457
3458 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003459 shseat->popup_grab.grab.interface =
3460 &popup_grab_interface;
3461
3462 /* We must make sure here that this popup was opened
3463 * after a mouse press, and not just by moving around
3464 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003465 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003466 shseat->popup_grab.initial_up = 0;
3467 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003468 shseat->popup_grab.touch_grab.interface =
3469 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003470 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003471
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003472 wl_list_insert(&shseat->popup_grab.surfaces_list,
3473 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003474
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003475 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003476 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003477 &shseat->popup_grab.grab);
3478 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003479 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003480 &shseat->popup_grab.touch_grab);
3481 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003482 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003483 wl_list_insert(&shseat->popup_grab.surfaces_list,
3484 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003485 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003486
3487 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003488}
3489
3490static void
3491remove_popup_grab(struct shell_surface *shsurf)
3492{
3493 struct shell_seat *shseat = shsurf->popup.shseat;
3494
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003495 if (shell_surface_is_xdg_popup(shsurf) &&
3496 get_top_popup(shseat) != shsurf) {
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 destroyed while it was "
3500 "not the topmost popup.");
3501 return;
3502 }
3503
Giulio Camuffo5085a752013-03-25 21:42:45 +01003504 wl_list_remove(&shsurf->popup.grab_link);
3505 wl_list_init(&shsurf->popup.grab_link);
3506 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003507 if (shseat->popup_grab.type == POINTER) {
3508 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3509 shseat->popup_grab.grab.interface = NULL;
3510 } else if (shseat->popup_grab.type == TOUCH) {
3511 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3512 shseat->popup_grab.touch_grab.interface = NULL;
3513 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003514 }
3515}
3516
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003517static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003518shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003519{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003520 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003521 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003522 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3523 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003524
Jason Ekstranda7af7042013-10-12 22:38:11 -05003525 shsurf->surface->output = parent_view->output;
3526 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003527
Jason Ekstranda7af7042013-10-12 22:38:11 -05003528 weston_view_set_transform_parent(shsurf->view, parent_view);
3529 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3530 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003531
Derek Foreman1281a362015-07-31 16:55:32 -05003532 if (pointer &&
3533 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003534 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3535 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003536 } else if (touch &&
3537 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003538 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3539 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003540 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003541 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003542 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003543 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003544
3545 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003546}
3547
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003548static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003549 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003550 shell_surface_move,
3551 shell_surface_resize,
3552 shell_surface_set_toplevel,
3553 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003554 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003555 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003556 shell_surface_set_maximized,
3557 shell_surface_set_title,
3558 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003559};
3560
3561static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003562destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003563{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003564 struct shell_surface *child, *next;
3565
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003566 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3567
Giulio Camuffo5085a752013-03-25 21:42:45 +01003568 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3569 remove_popup_grab(shsurf);
3570 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003571
Alex Wubd3354b2012-04-17 17:20:49 +08003572 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003573 shell_surface_is_top_fullscreen(shsurf))
3574 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003575
Jason Ekstranda7af7042013-10-12 22:38:11 -05003576 if (shsurf->fullscreen.black_view)
3577 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003578
Alex Wubd3354b2012-04-17 17:20:49 +08003579 /* As destroy_resource() use wl_list_for_each_safe(),
3580 * we can always remove the listener.
3581 */
3582 wl_list_remove(&shsurf->surface_destroy_listener.link);
3583 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003584 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003585 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003586
Jason Ekstranda7af7042013-10-12 22:38:11 -05003587 weston_view_destroy(shsurf->view);
3588
Philip Withnall648a4dd2013-11-25 18:01:44 +00003589 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003590 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3591 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003592
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003593 wl_list_remove(&shsurf->link);
3594 free(shsurf);
3595}
3596
3597static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003598shell_destroy_shell_surface(struct wl_resource *resource)
3599{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003600 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003601
Bryan Caina46b9462014-04-04 17:41:24 -05003602 if (!wl_list_empty(&shsurf->popup.grab_link))
3603 remove_popup_grab(shsurf);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003604 if (shsurf->resource)
3605 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003606 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003607}
3608
3609static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003610shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003611{
3612 struct shell_surface *shsurf = container_of(listener,
3613 struct shell_surface,
3614 surface_destroy_listener);
3615
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003616 if (shsurf->resource)
3617 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003618
3619 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003620}
3621
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003622static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003623fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003624{
3625 struct shell_surface *shsurf = data;
3626
3627 weston_surface_destroy(shsurf->surface);
3628}
3629
3630static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003631fade_out_done(struct weston_view_animation *animation, void *data)
3632{
3633 struct shell_surface *shsurf = data;
3634 struct wl_event_loop *loop;
3635
3636 loop = wl_display_get_event_loop(
3637 shsurf->surface->compositor->wl_display);
3638
3639 if (!shsurf->destroying) {
3640 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3641 shsurf->destroying = true;
3642 }
3643}
3644
3645static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003646handle_resource_destroy(struct wl_listener *listener, void *data)
3647{
3648 struct shell_surface *shsurf =
3649 container_of(listener, struct shell_surface,
3650 resource_destroy_listener);
3651
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003652 if (!weston_surface_is_mapped(shsurf->surface))
3653 return;
3654
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003655 shsurf->surface->ref_count++;
3656
3657 pixman_region32_fini(&shsurf->surface->pending.input);
3658 pixman_region32_init(&shsurf->surface->pending.input);
3659 pixman_region32_fini(&shsurf->surface->input);
3660 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003661 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3662 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3663 fade_out_done, shsurf);
3664 } else {
3665 weston_surface_destroy(shsurf->surface);
3666 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003667}
3668
3669static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003670shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003671
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003672struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003673get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003674{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003675 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003676 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003677 else
3678 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003679}
3680
Rafael Antognollie2a34552013-12-03 15:35:45 -02003681static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003682create_common_surface(struct shell_client *owner, void *shell,
3683 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003684 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003685{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003686 struct shell_surface *shsurf;
3687
Pekka Paalanen50b67472014-10-01 15:02:41 +03003688 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003689
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003690 shsurf = calloc(1, sizeof *shsurf);
3691 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003692 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003693 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003694 }
3695
Jason Ekstranda7af7042013-10-12 22:38:11 -05003696 shsurf->view = weston_view_create(surface);
3697 if (!shsurf->view) {
3698 weston_log("no memory to allocate shell surface\n");
3699 free(shsurf);
3700 return NULL;
3701 }
3702
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003703 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003704 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003705 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003706
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003707 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3708 wl_resource_add_destroy_listener(surface->resource,
3709 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003710 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003711
Tiago Vignattibc052c92012-04-19 16:18:18 +03003712 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003713 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003714 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003715 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003716 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003717 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3718 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003719 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003720 wl_list_init(&shsurf->fullscreen.transform.link);
3721
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003722 shsurf->output = get_default_output(shsurf->shell->compositor);
3723
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003724 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003725 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003726 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003727 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003728
3729 /* init link so its safe to always remove it in destroy_shell_surface */
3730 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003731 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003732
Pekka Paalanen460099f2012-01-20 16:48:25 +02003733 /* empty when not in use */
3734 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003735 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003736
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003737 wl_list_init(&shsurf->workspace_transform.link);
3738
Philip Withnall648a4dd2013-11-25 18:01:44 +00003739 wl_list_init(&shsurf->children_link);
3740 wl_list_init(&shsurf->children_list);
3741 shsurf->parent = NULL;
3742
Pekka Paalanenb5026542014-11-12 15:09:24 +02003743 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003744
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003745 shsurf->client = client;
3746
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003747 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003748}
3749
Rafael Antognollie2a34552013-12-03 15:35:45 -02003750static struct shell_surface *
3751create_shell_surface(void *shell, struct weston_surface *surface,
3752 const struct weston_shell_client *client)
3753{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003754 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003755}
3756
Tiago Vignattibc052c92012-04-19 16:18:18 +03003757static void
3758shell_get_shell_surface(struct wl_client *client,
3759 struct wl_resource *resource,
3760 uint32_t id,
3761 struct wl_resource *surface_resource)
3762{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003763 struct weston_surface *surface =
3764 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003765 struct shell_client *sc = wl_resource_get_user_data(resource);
3766 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003767 struct shell_surface *shsurf;
3768
Pekka Paalanen50b67472014-10-01 15:02:41 +03003769 if (weston_surface_set_role(surface, "wl_shell_surface",
3770 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003771 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003772
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003773 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003774 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003775 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003776 return;
3777 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003778
Jason Ekstranda85118c2013-06-27 20:17:02 -05003779 shsurf->resource =
3780 wl_resource_create(client,
3781 &wl_shell_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003782 if (!shsurf->resource) {
3783 wl_resource_post_no_memory(surface_resource);
3784 return;
3785 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003786 wl_resource_set_implementation(shsurf->resource,
3787 &shell_surface_implementation,
3788 shsurf, shell_destroy_shell_surface);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08003789 wl_list_insert(&sc->surface_list,
3790 wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003791}
3792
Rafael Antognollie2a34552013-12-03 15:35:45 -02003793static bool
3794shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3795{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003796 /* A shell surface without a resource is created from xwayland
3797 * and is considered a wl_shell surface for now. */
3798
3799 return shsurf->resource == NULL ||
3800 wl_resource_instance_of(shsurf->resource,
3801 &wl_shell_surface_interface,
3802 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003803}
3804
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003805static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003806 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003807};
3808
Rafael Antognollie2a34552013-12-03 15:35:45 -02003809/****************************
3810 * xdg-shell implementation */
3811
3812static void
3813xdg_surface_destroy(struct wl_client *client,
3814 struct wl_resource *resource)
3815{
3816 wl_resource_destroy(resource);
3817}
3818
3819static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003820xdg_surface_set_parent(struct wl_client *client,
3821 struct wl_resource *resource,
3822 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003823{
3824 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003825 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003826
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003827 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003828 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003829 shell_surface_set_parent(shsurf, parent->surface);
3830 } else {
3831 shell_surface_set_parent(shsurf, NULL);
3832 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003833}
3834
3835static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003836xdg_surface_set_app_id(struct wl_client *client,
3837 struct wl_resource *resource,
3838 const char *app_id)
3839{
3840 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3841
3842 free(shsurf->class);
3843 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003844 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003845}
3846
3847static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003848xdg_surface_show_window_menu(struct wl_client *client,
3849 struct wl_resource *surface_resource,
3850 struct wl_resource *seat_resource,
3851 uint32_t serial,
3852 int32_t x,
3853 int32_t y)
3854{
3855 /* TODO */
3856}
3857
3858static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003859xdg_surface_set_title(struct wl_client *client,
3860 struct wl_resource *resource, const char *title)
3861{
3862 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3863
3864 set_title(shsurf, title);
3865}
3866
3867static void
3868xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3869 struct wl_resource *seat_resource, uint32_t serial)
3870{
3871 common_surface_move(resource, seat_resource, serial);
3872}
3873
3874static void
3875xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3876 struct wl_resource *seat_resource, uint32_t serial,
3877 uint32_t edges)
3878{
3879 common_surface_resize(resource, seat_resource, serial, edges);
3880}
3881
3882static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003883xdg_surface_ack_configure(struct wl_client *client,
3884 struct wl_resource *resource,
3885 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003886{
3887 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3888
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003889 if (shsurf->state_requested) {
3890 shsurf->next_state = shsurf->requested_state;
3891 shsurf->state_changed = true;
3892 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003893 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003894}
3895
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003896static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003897xdg_surface_set_window_geometry(struct wl_client *client,
3898 struct wl_resource *resource,
3899 int32_t x,
3900 int32_t y,
3901 int32_t width,
3902 int32_t height)
3903{
3904 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3905
3906 set_window_geometry(shsurf, x, y, width, height);
3907}
3908
3909static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003910xdg_surface_set_maximized(struct wl_client *client,
3911 struct wl_resource *resource)
3912{
3913 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003914 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003915
3916 shsurf->state_requested = true;
3917 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003918
3919 if (!weston_surface_is_mapped(shsurf->surface))
3920 output = get_focused_output(shsurf->surface->compositor);
3921 else
3922 output = shsurf->surface->output;
3923
3924 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003925 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003926}
3927
3928static void
3929xdg_surface_unset_maximized(struct wl_client *client,
3930 struct wl_resource *resource)
3931{
3932 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3933
3934 shsurf->state_requested = true;
3935 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003936 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003937}
3938
3939static void
3940xdg_surface_set_fullscreen(struct wl_client *client,
3941 struct wl_resource *resource,
3942 struct wl_resource *output_resource)
3943{
3944 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3945 struct weston_output *output;
3946
3947 shsurf->state_requested = true;
3948 shsurf->requested_state.fullscreen = true;
3949
3950 if (output_resource)
3951 output = wl_resource_get_user_data(output_resource);
3952 else
3953 output = NULL;
3954
Marek Chalupa052917a2014-09-02 11:35:12 +02003955 /* handle clients launching in fullscreen */
3956 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3957 /* Set the output to the one that has focus currently. */
3958 assert(shsurf->surface);
3959 output = get_focused_output(shsurf->surface->compositor);
3960 }
3961
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003962 shell_surface_set_output(shsurf, output);
3963 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003964
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003965 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003966}
3967
3968static void
3969xdg_surface_unset_fullscreen(struct wl_client *client,
3970 struct wl_resource *resource)
3971{
3972 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3973
3974 shsurf->state_requested = true;
3975 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003976 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003977}
3978
3979static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003980xdg_surface_set_minimized(struct wl_client *client,
3981 struct wl_resource *resource)
3982{
3983 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3984
3985 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3986 return;
3987
3988 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003989 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003990}
3991
Rafael Antognollie2a34552013-12-03 15:35:45 -02003992static const struct xdg_surface_interface xdg_surface_implementation = {
3993 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003994 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003995 xdg_surface_set_title,
3996 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003997 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003998 xdg_surface_move,
3999 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004000 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004001 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004002 xdg_surface_set_maximized,
4003 xdg_surface_unset_maximized,
4004 xdg_surface_set_fullscreen,
4005 xdg_surface_unset_fullscreen,
4006 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004007};
4008
4009static void
4010xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004011 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004012{
4013 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004014 uint32_t *s;
4015 struct wl_array states;
4016 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004017
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004018 assert(shsurf);
4019
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004020 if (!shsurf->resource)
4021 return;
4022
4023 wl_array_init(&states);
4024 if (shsurf->requested_state.fullscreen) {
4025 s = wl_array_add(&states, sizeof *s);
4026 *s = XDG_SURFACE_STATE_FULLSCREEN;
4027 } else if (shsurf->requested_state.maximized) {
4028 s = wl_array_add(&states, sizeof *s);
4029 *s = XDG_SURFACE_STATE_MAXIMIZED;
4030 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004031 if (shsurf->resize_edges != 0) {
4032 s = wl_array_add(&states, sizeof *s);
4033 *s = XDG_SURFACE_STATE_RESIZING;
4034 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004035 if (shsurf->focus_count > 0) {
4036 s = wl_array_add(&states, sizeof *s);
4037 *s = XDG_SURFACE_STATE_ACTIVATED;
4038 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004039
4040 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4041 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4042
4043 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004044}
4045
4046static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004047 xdg_send_configure,
4048 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004049};
4050
4051static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004052xdg_shell_destroy(struct wl_client *client,
4053 struct wl_resource *resource)
4054{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004055 struct shell_client *sc = wl_resource_get_user_data(resource);
4056 struct wl_resource *shsurf_resource;
4057 struct shell_surface *shsurf;
4058
4059 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4060 shsurf = wl_resource_get_user_data(shsurf_resource);
4061 if (shsurf->owner_resource == resource) {
4062 wl_resource_post_error(
4063 resource,
4064 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4065 "not all child surface objects destroyed");
4066 return;
4067 }
4068 }
4069
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004070 wl_resource_destroy(resource);
4071}
4072
4073static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004074xdg_use_unstable_version(struct wl_client *client,
4075 struct wl_resource *resource,
4076 int32_t version)
4077{
4078 if (version > 1) {
4079 wl_resource_post_error(resource,
4080 1,
4081 "xdg-shell:: version not implemented yet.");
4082 return;
4083 }
4084}
4085
4086static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004087create_xdg_surface(struct shell_client *owner, void *shell,
4088 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004089 const struct weston_shell_client *client)
4090{
4091 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004092
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004093 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004094 if (!shsurf)
4095 return NULL;
4096
Pekka Paalanenb5026542014-11-12 15:09:24 +02004097 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004098
4099 return shsurf;
4100}
4101
4102static void
4103xdg_get_xdg_surface(struct wl_client *client,
4104 struct wl_resource *resource,
4105 uint32_t id,
4106 struct wl_resource *surface_resource)
4107{
4108 struct weston_surface *surface =
4109 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004110 struct shell_client *sc = wl_resource_get_user_data(resource);
4111 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004112 struct shell_surface *shsurf;
4113
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004114 shsurf = get_shell_surface(surface);
4115 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4116 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4117 "This wl_surface is already an "
4118 "xdg_surface");
4119 return;
4120 }
4121
Pekka Paalanen50b67472014-10-01 15:02:41 +03004122 if (weston_surface_set_role(surface, "xdg_surface",
4123 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004124 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004125
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004126 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004127 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004128 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004129 return;
4130 }
4131
4132 shsurf->resource =
4133 wl_resource_create(client,
4134 &xdg_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004135 if (!shsurf->resource) {
4136 wl_resource_post_no_memory(surface_resource);
4137 return;
4138 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004139 wl_resource_set_implementation(shsurf->resource,
4140 &xdg_surface_implementation,
4141 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004142 shsurf->owner_resource = resource;
4143 wl_list_insert(&sc->surface_list,
4144 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004145}
4146
4147static bool
4148shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4149{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004150 return shsurf->resource &&
4151 wl_resource_instance_of(shsurf->resource,
4152 &xdg_surface_interface,
4153 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004154}
4155
4156/* xdg-popup implementation */
4157
4158static void
4159xdg_popup_destroy(struct wl_client *client,
4160 struct wl_resource *resource)
4161{
4162 wl_resource_destroy(resource);
4163}
4164
Rafael Antognollie2a34552013-12-03 15:35:45 -02004165static const struct xdg_popup_interface xdg_popup_implementation = {
4166 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004167};
4168
4169static void
4170xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004171 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004172{
4173}
4174
4175static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004176 xdg_popup_send_configure,
4177 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004178};
4179
4180static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004181create_xdg_popup(struct shell_client *owner, void *shell,
4182 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004183 const struct weston_shell_client *client,
4184 struct weston_surface *parent,
4185 struct shell_seat *seat,
4186 uint32_t serial,
4187 int32_t x, int32_t y)
4188{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004189 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004190
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004191 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004192 if (!shsurf)
4193 return NULL;
4194
Pekka Paalanenb5026542014-11-12 15:09:24 +02004195 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004196 shsurf->popup.shseat = seat;
4197 shsurf->popup.serial = serial;
4198 shsurf->popup.x = x;
4199 shsurf->popup.y = y;
4200 shell_surface_set_parent(shsurf, parent);
4201
4202 return shsurf;
4203}
4204
4205static void
4206xdg_get_xdg_popup(struct wl_client *client,
4207 struct wl_resource *resource,
4208 uint32_t id,
4209 struct wl_resource *surface_resource,
4210 struct wl_resource *parent_resource,
4211 struct wl_resource *seat_resource,
4212 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004213 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004214{
4215 struct weston_surface *surface =
4216 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004217 struct shell_client *sc = wl_resource_get_user_data(resource);
4218 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004219 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004220 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004221 struct weston_surface *parent;
4222 struct shell_seat *seat;
4223
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004224 shsurf = get_shell_surface(surface);
4225 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4226 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4227 "This wl_surface is already an "
4228 "xdg_popup");
4229 return;
4230 }
4231
Pekka Paalanen50b67472014-10-01 15:02:41 +03004232 if (weston_surface_set_role(surface, "xdg_popup",
4233 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004234 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004235
4236 if (!parent_resource) {
4237 wl_resource_post_error(surface_resource,
4238 WL_DISPLAY_ERROR_INVALID_OBJECT,
4239 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004240 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004241 }
4242
4243 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004244 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004245
Jonas Ådahlee45a552015-03-18 16:37:47 +08004246 /* Verify that we are creating the top most popup when mapping,
4247 * as it's not until then we know whether it was mapped as most
4248 * top level or not. */
4249
4250 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004251 if (!parent_shsurf ||
4252 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4253 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004254 wl_resource_post_error(resource,
4255 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4256 "xdg_popup parent was invalid");
4257 return;
4258 }
4259
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004260 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004261 parent, seat, serial, x, y);
4262 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004263 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004264 return;
4265 }
4266
4267 shsurf->resource =
4268 wl_resource_create(client,
4269 &xdg_popup_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004270 if (!shsurf->resource) {
4271 wl_resource_post_no_memory(surface_resource);
4272 return;
4273 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004274 wl_resource_set_implementation(shsurf->resource,
4275 &xdg_popup_implementation,
4276 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004277 shsurf->owner_resource = resource;
4278 wl_list_insert(&sc->surface_list,
4279 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004280}
4281
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004282static void
4283xdg_pong(struct wl_client *client,
4284 struct wl_resource *resource, uint32_t serial)
4285{
4286 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004287
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004288 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004289}
4290
Rafael Antognollie2a34552013-12-03 15:35:45 -02004291static bool
4292shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4293{
Bryce Harrington22b1f932015-09-23 17:30:43 -07004294 return (shsurf->resource &&
4295 wl_resource_instance_of(shsurf->resource,
4296 &xdg_popup_interface,
4297 &xdg_popup_implementation));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004298}
4299
4300static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004301 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004302 xdg_use_unstable_version,
4303 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004304 xdg_get_xdg_popup,
4305 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004306};
4307
4308static int
4309xdg_shell_unversioned_dispatch(const void *implementation,
4310 void *_target, uint32_t opcode,
4311 const struct wl_message *message,
4312 union wl_argument *args)
4313{
4314 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004315 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004316
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004317 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004318 wl_resource_post_error(resource,
4319 WL_DISPLAY_ERROR_INVALID_OBJECT,
4320 "must call use_unstable_version first");
4321 return 0;
4322 }
4323
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004324#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004325
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004326 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4327 "shell implementation doesn't match protocol version");
4328
Rafael Antognollie2a34552013-12-03 15:35:45 -02004329 if (args[0].i != XDG_SERVER_VERSION) {
4330 wl_resource_post_error(resource,
4331 WL_DISPLAY_ERROR_INVALID_OBJECT,
4332 "incompatible version, server is %d "
4333 "client wants %d",
4334 XDG_SERVER_VERSION, args[0].i);
4335 return 0;
4336 }
4337
4338 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004339 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004340
4341 return 1;
4342}
4343
4344/* end of xdg-shell implementation */
4345/***********************************/
4346
Kristian Høgsberg07937562011-04-12 17:25:42 -04004347static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004348shell_fade(struct desktop_shell *shell, enum fade_type type);
4349
Pekka Paalanen77346a62011-11-30 16:26:35 +02004350static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004351configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004352{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004353 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004354
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004355 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004356 if (v->output == ev->output && v != ev) {
4357 weston_view_unmap(v);
4358 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004359 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004360 }
4361 }
4362
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004363 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004364
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004365 if (wl_list_empty(&ev->layer_link.link)) {
4366 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004367 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004368 }
4369}
4370
David Fort50d962f2016-06-09 17:55:52 +02004371
4372static struct shell_output *
4373find_shell_output_from_weston_output(struct desktop_shell *shell, struct weston_output *output)
4374{
4375 struct shell_output *shell_output;
4376
4377 wl_list_for_each(shell_output, &shell->output_list, link) {
4378 if (shell_output->output == output)
4379 return shell_output;
4380 }
4381
4382 return NULL;
4383}
4384
Pekka Paalanen8274d902014-08-06 19:36:51 +03004385static int
4386background_get_label(struct weston_surface *surface, char *buf, size_t len)
4387{
4388 return snprintf(buf, len, "background for output %s",
4389 surface->output->name);
4390}
4391
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004392static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004393background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004394{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004395 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004397
Jason Ekstranda7af7042013-10-12 22:38:11 -05004398 view = container_of(es->views.next, struct weston_view, surface_link);
4399
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004400 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004401}
4402
4403static void
David Fort50d962f2016-06-09 17:55:52 +02004404handle_background_surface_destroy(struct wl_listener *listener, void *data)
4405{
4406 struct shell_output *output =
4407 container_of(listener, struct shell_output, background_surface_listener);
4408
4409 weston_log("background surface gone\n");
4410 output->background_surface = NULL;
4411}
4412
4413static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004414desktop_shell_set_background(struct wl_client *client,
4415 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004416 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004417 struct wl_resource *surface_resource)
4418{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004419 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004420 struct weston_surface *surface =
4421 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02004422 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004423 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004424
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004425 if (surface->configure) {
4426 wl_resource_post_error(surface_resource,
4427 WL_DISPLAY_ERROR_INVALID_OBJECT,
4428 "surface role already assigned");
4429 return;
4430 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004431
Jason Ekstranda7af7042013-10-12 22:38:11 -05004432 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4433 weston_view_destroy(view);
4434 view = weston_view_create(surface);
4435
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004436 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004437 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004438 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004439 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004440 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004441 weston_desktop_shell_send_configure(resource, 0,
4442 surface_resource,
4443 surface->output->width,
4444 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004445
4446 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4447 sh_output->background_surface = surface;
4448
4449 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
4450 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004451}
4452
Pekka Paalanen8274d902014-08-06 19:36:51 +03004453static int
4454panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4455{
4456 return snprintf(buf, len, "panel for output %s",
4457 surface->output->name);
4458}
4459
Kristian Høgsberg75840622011-09-06 13:48:16 -04004460static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004461panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004462{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004463 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004464 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004465
Jason Ekstranda7af7042013-10-12 22:38:11 -05004466 view = container_of(es->views.next, struct weston_view, surface_link);
4467
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004468 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004469}
4470
4471static void
David Fort50d962f2016-06-09 17:55:52 +02004472handle_panel_surface_destroy(struct wl_listener *listener, void *data)
4473{
4474 struct shell_output *output =
4475 container_of(listener, struct shell_output, panel_surface_listener);
4476
4477 weston_log("panel surface gone\n");
4478 output->panel_surface = NULL;
4479}
4480
4481
4482static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004483desktop_shell_set_panel(struct wl_client *client,
4484 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004485 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004486 struct wl_resource *surface_resource)
4487{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004488 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004489 struct weston_surface *surface =
4490 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004491 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02004492 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004493
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004494 if (surface->configure) {
4495 wl_resource_post_error(surface_resource,
4496 WL_DISPLAY_ERROR_INVALID_OBJECT,
4497 "surface role already assigned");
4498 return;
4499 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004500
Jason Ekstranda7af7042013-10-12 22:38:11 -05004501 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4502 weston_view_destroy(view);
4503 view = weston_view_create(surface);
4504
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004505 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004506 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004507 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004508 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004509 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004510 weston_desktop_shell_send_configure(resource, 0,
4511 surface_resource,
4512 surface->output->width,
4513 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004514
4515 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4516 sh_output->panel_surface = surface;
4517
4518 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
4519 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004520}
4521
Pekka Paalanen8274d902014-08-06 19:36:51 +03004522static int
4523lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4524{
4525 return snprintf(buf, len, "lock window");
4526}
4527
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004528static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004529lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004530{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004531 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004532 struct weston_view *view;
4533
4534 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004535
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004536 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004537 return;
4538
Jason Ekstranda7af7042013-10-12 22:38:11 -05004539 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004540
4541 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004542 weston_layer_entry_insert(&shell->lock_layer.view_list,
4543 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004544 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004545 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004546 }
4547}
4548
4549static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004550handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004551{
Tiago Vignattibe143262012-04-16 17:31:41 +03004552 struct desktop_shell *shell =
4553 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004554
Martin Minarik6d118362012-06-07 18:01:59 +02004555 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004556 shell->lock_surface = NULL;
4557}
4558
4559static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004560desktop_shell_set_lock_surface(struct wl_client *client,
4561 struct wl_resource *resource,
4562 struct wl_resource *surface_resource)
4563{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004564 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004565 struct weston_surface *surface =
4566 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004567
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004568 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004569
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004570 if (!shell->locked)
4571 return;
4572
Pekka Paalanen98262232011-12-01 10:42:22 +02004573 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004574
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004575 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004576 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004577 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004578
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004579 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004580 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004581 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004582 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004583}
4584
4585static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004586resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004587{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004588 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004589
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004590 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004591 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004592 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004593 &shell->input_panel_layer.link);
4594 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004595 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004596 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004597 wl_list_insert(&shell->compositor->cursor_layer.link,
4598 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004599 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004600 wl_list_insert(&shell->fullscreen_layer.link,
4601 &shell->panel_layer.link);
4602 wl_list_insert(&shell->panel_layer.link,
4603 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004604
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004605 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004606
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004607 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004608 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004609 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004610}
4611
4612static void
4613desktop_shell_unlock(struct wl_client *client,
4614 struct wl_resource *resource)
4615{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004616 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004617
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004618 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004619
4620 if (shell->locked)
4621 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004622}
4623
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004624static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004625desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004626 struct wl_resource *resource,
4627 struct wl_resource *surface_resource)
4628{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004629 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004630
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004631 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004632 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004633}
4634
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004635static void
4636desktop_shell_desktop_ready(struct wl_client *client,
4637 struct wl_resource *resource)
4638{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004639 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004640
4641 shell_fade_startup(shell);
4642}
4643
Jonny Lamb765760d2014-08-20 15:53:19 +02004644static void
4645desktop_shell_set_panel_position(struct wl_client *client,
4646 struct wl_resource *resource,
4647 uint32_t position)
4648{
4649 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4650
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004651 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4652 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4653 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4654 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004655 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004656 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004657 "bad position argument");
4658 return;
4659 }
4660
4661 shell->panel_position = position;
4662}
4663
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004664static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004665 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004666 desktop_shell_set_panel,
4667 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004668 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004669 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004670 desktop_shell_desktop_ready,
4671 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004672};
4673
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004674static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004675get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004676{
4677 struct shell_surface *shsurf;
4678
4679 shsurf = get_shell_surface(surface);
4680 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004681 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004682 return shsurf->type;
4683}
4684
Kristian Høgsberg75840622011-09-06 13:48:16 -04004685static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004686move_binding(struct weston_pointer *pointer, uint32_t time,
4687 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004688{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004689 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004690 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004691 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004692
Derek Foreman8ae2db52015-07-15 13:00:36 -05004693 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004694 return;
4695
Derek Foreman8ae2db52015-07-15 13:00:36 -05004696 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004697
Pekka Paalanen01388e22013-04-25 13:57:44 +03004698 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004699 if (surface == NULL)
4700 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004701
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004702 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004703 if (shsurf == NULL || shsurf->state.fullscreen ||
4704 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004705 return;
4706
Derek Foreman794fa0e2015-07-15 13:00:38 -05004707 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004708}
4709
4710static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004711maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4712 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004713{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004714 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004715 struct weston_surface *surface;
4716 struct shell_surface *shsurf;
4717
4718 surface = weston_surface_get_main_surface(focus);
4719 if (surface == NULL)
4720 return;
4721
4722 shsurf = get_shell_surface(surface);
4723 if (shsurf == NULL)
4724 return;
4725
4726 if (!shell_surface_is_xdg_surface(shsurf))
4727 return;
4728
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004729 shsurf->state_requested = true;
4730 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004731 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004732}
4733
4734static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004735fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4736 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004737{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004738 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004739 struct weston_surface *surface;
4740 struct shell_surface *shsurf;
4741
4742 surface = weston_surface_get_main_surface(focus);
4743 if (surface == NULL)
4744 return;
4745
4746 shsurf = get_shell_surface(surface);
4747 if (shsurf == NULL)
4748 return;
4749
4750 if (!shell_surface_is_xdg_surface(shsurf))
4751 return;
4752
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004753 shsurf->state_requested = true;
4754 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004755 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004756 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004757}
4758
4759static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004760touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004761{
Derek Foreman362656b2014-09-04 10:23:05 -05004762 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004763 struct weston_surface *surface;
4764 struct shell_surface *shsurf;
4765
Derek Foreman8ae2db52015-07-15 13:00:36 -05004766 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004767 return;
4768
Derek Foreman8ae2db52015-07-15 13:00:36 -05004769 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004770 surface = weston_surface_get_main_surface(focus);
4771 if (surface == NULL)
4772 return;
4773
4774 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004775 if (shsurf == NULL || shsurf->state.fullscreen ||
4776 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004777 return;
4778
Derek Foremanb7674ae2015-07-15 13:00:37 -05004779 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004780}
4781
4782static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004783resize_binding(struct weston_pointer *pointer, uint32_t time,
4784 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004785{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004786 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004787 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004788 uint32_t edges = 0;
4789 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004790 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004791
Derek Foreman8ae2db52015-07-15 13:00:36 -05004792 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004793 return;
4794
Derek Foreman8ae2db52015-07-15 13:00:36 -05004795 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004796
Pekka Paalanen01388e22013-04-25 13:57:44 +03004797 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004798 if (surface == NULL)
4799 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004800
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004801 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004802 if (shsurf == NULL || shsurf->state.fullscreen ||
4803 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004804 return;
4805
Jason Ekstranda7af7042013-10-12 22:38:11 -05004806 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004807 wl_fixed_to_int(pointer->grab_x),
4808 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004809 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004810
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004811 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004812 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004813 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004814 edges |= 0;
4815 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004816 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004817
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004818 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004819 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004820 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004821 edges |= 0;
4822 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004823 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004824
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004825 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004826}
4827
4828static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004829surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004830 struct weston_pointer_axis_event *event,
4831 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004832{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004833 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004834 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004835 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004836 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004837
Pekka Paalanen01388e22013-04-25 13:57:44 +03004838 /* XXX: broken for windows containing sub-surfaces */
4839 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004840 if (surface == NULL)
4841 return;
4842
4843 shsurf = get_shell_surface(surface);
4844 if (!shsurf)
4845 return;
4846
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004847 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004848
Jason Ekstranda7af7042013-10-12 22:38:11 -05004849 if (shsurf->view->alpha > 1.0)
4850 shsurf->view->alpha = 1.0;
4851 if (shsurf->view->alpha < step)
4852 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004853
Jason Ekstranda7af7042013-10-12 22:38:11 -05004854 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004855 weston_surface_damage(surface);
4856}
4857
4858static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004859do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004860 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004861{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004862 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004863 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004864 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004865 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004866
Derek Foreman1281a362015-07-31 16:55:32 -05004867 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004868 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4869 return;
4870 }
4871
Scott Moreauccbf29d2012-02-22 14:21:41 -07004872 wl_list_for_each(output, &compositor->output_list, link) {
4873 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004874 wl_fixed_to_double(pointer->x),
4875 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004876 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004877 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004878 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004879 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004880 increment = -output->zoom.increment;
4881 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004882 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004883 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004884 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004885 else
4886 increment = 0;
4887
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004888 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004889
Scott Moreaue6603982012-06-11 13:07:51 -06004890 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004891 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004892 else if (output->zoom.level > output->zoom.max_level)
4893 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004894
4895 if (!output->zoom.active) {
4896 if (output->zoom.level <= 0.0)
4897 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004898 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004899 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004900
Scott Moreaue6603982012-06-11 13:07:51 -06004901 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004902
Jason Ekstranda7af7042013-10-12 22:38:11 -05004903 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004904 }
4905 }
4906}
4907
Scott Moreauccbf29d2012-02-22 14:21:41 -07004908static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004909zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004910 struct weston_pointer_axis_event *event,
4911 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004912{
Peter Hutterer89b6a492016-01-18 15:58:17 +10004913 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004914}
4915
4916static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004917zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4918 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004919{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004920 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004921}
4922
4923static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004924terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4925 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004926{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004927 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004928
Daniel Stone325fc2d2012-05-30 16:31:58 +01004929 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004930}
4931
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004932static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004933rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004934 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004935{
4936 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004937 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004938 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004939 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004940 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004941
Jonas Ådahld2510102014-10-05 21:39:14 +02004942 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004943
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004944 if (!shsurf)
4945 return;
4946
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004947 cx = 0.5f * shsurf->surface->width;
4948 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004949
Daniel Stone37816df2012-05-16 18:45:18 +01004950 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4951 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004952 r = sqrtf(dx * dx + dy * dy);
4953
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004954 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004955 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004956
4957 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004958 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004959 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004960
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004961 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004962 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004963
4964 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004965 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004966 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004967 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004968 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004969
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004970 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004971 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004972 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004973 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004974 wl_list_init(&shsurf->rotation.transform.link);
4975 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004976 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004977 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004978
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004979 /* We need to adjust the position of the surface
4980 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004981 cposx = shsurf->view->geometry.x + cx;
4982 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004983 dposx = rotate->center.x - cposx;
4984 dposy = rotate->center.y - cposy;
4985 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004986 weston_view_set_position(shsurf->view,
4987 shsurf->view->geometry.x + dposx,
4988 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004989 }
4990
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004991 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004992 * lazily applies the damage due to rotation update.
4993 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004994 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004995}
4996
4997static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004998rotate_grab_button(struct weston_pointer_grab *grab,
4999 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005000{
5001 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005002 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005003 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005004 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01005005 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005006
Daniel Stone4dbadb12012-05-30 16:31:51 +01005007 if (pointer->button_count == 0 &&
5008 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005009 if (shsurf)
5010 weston_matrix_multiply(&shsurf->rotation.rotation,
5011 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005012 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005013 free(rotate);
5014 }
5015}
5016
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005017static void
5018rotate_grab_cancel(struct weston_pointer_grab *grab)
5019{
5020 struct rotate_grab *rotate =
5021 container_of(grab, struct rotate_grab, base.grab);
5022
5023 shell_grab_end(&rotate->base);
5024 free(rotate);
5025}
5026
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005027static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02005028 noop_grab_focus,
5029 rotate_grab_motion,
5030 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02005031 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10005032 noop_grab_axis_source,
5033 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005034 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02005035};
5036
5037static void
Derek Foreman74de4692015-07-15 13:00:39 -05005038surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005039{
Pekka Paalanen460099f2012-01-20 16:48:25 +02005040 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005041 float dx, dy;
5042 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005043
Derek Foreman45a7c272015-09-11 14:27:40 -05005044 surface = find_toplevel_surface(surface);
5045
Pekka Paalanen460099f2012-01-20 16:48:25 +02005046 rotate = malloc(sizeof *rotate);
5047 if (!rotate)
5048 return;
5049
Jason Ekstranda7af7042013-10-12 22:38:11 -05005050 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005051 surface->surface->width * 0.5f,
5052 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005053 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005054
Derek Foreman74de4692015-07-15 13:00:39 -05005055 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5056 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005057 r = sqrtf(dx * dx + dy * dy);
5058 if (r > 20.0f) {
5059 struct weston_matrix inverse;
5060
5061 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005062 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005063 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005064
5065 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005066 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005067 } else {
5068 weston_matrix_init(&surface->rotation.rotation);
5069 weston_matrix_init(&rotate->rotation);
5070 }
5071
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005072 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005073 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005074}
5075
5076static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005077rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005078 void *data)
5079{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005080 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005081 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005082 struct shell_surface *surface;
5083
Derek Foreman8ae2db52015-07-15 13:00:36 -05005084 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005085 return;
5086
Derek Foreman8ae2db52015-07-15 13:00:36 -05005087 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005088
Pekka Paalanen01388e22013-04-25 13:57:44 +03005089 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005090 if (base_surface == NULL)
5091 return;
5092
5093 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005094 if (surface == NULL || surface->state.fullscreen ||
5095 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005096 return;
5097
Derek Foreman74de4692015-07-15 13:00:39 -05005098 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005099}
5100
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005101/* Move all fullscreen layers down to the current workspace and hide their
5102 * black views. The surfaces' state is set to both fullscreen and lowered,
5103 * and this is reversed when such a surface is re-configured, see
5104 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5105 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005106 * lowering_output = NULL - Lower on all outputs, else only lower on the
5107 * specified output.
5108 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005109 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005110 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005111void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005112lower_fullscreen_layer(struct desktop_shell *shell,
5113 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005114{
5115 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005116 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005117
5118 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005119 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005120 &shell->fullscreen_layer.view_list.link,
5121 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005122 struct shell_surface *shsurf = get_shell_surface(view->surface);
5123
5124 if (!shsurf)
5125 continue;
5126
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005127 /* Only lower surfaces which have lowering_output as their fullscreen
5128 * output, unless a NULL output asks for lowering on all outputs.
5129 */
5130 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5131 continue;
5132
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005133 /* We can have a non-fullscreen popup for a fullscreen surface
5134 * in the fullscreen layer. */
5135 if (shsurf->state.fullscreen) {
5136 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005137 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5138 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005139 weston_view_damage_below(shsurf->fullscreen.black_view);
5140
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005141 }
5142
5143 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005144 weston_layer_entry_remove(&view->layer_link);
5145 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005146 weston_view_damage_below(view);
5147 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005148
5149 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005150 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005151}
5152
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005153void
Tiago Vignattibe143262012-04-16 17:31:41 +03005154activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005155 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005156{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005157 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005158 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005159 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005160 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005161 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005162
Pekka Paalanen01388e22013-04-25 13:57:44 +03005163 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005164 shsurf = get_shell_surface(main_surface);
5165 assert(shsurf);
5166
5167 /* Only demote fullscreen surfaces on the output of activated shsurf.
5168 * Leave fullscreen surfaces on unrelated outputs alone. */
5169 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005170
Bryce Harrington260c2ff2016-06-29 19:04:06 -07005171 weston_seat_set_keyboard_focus(seat, es);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005172
Jonas Ådahl8538b222012-08-29 22:13:03 +02005173 state = ensure_focus_state(shell, seat);
5174 if (state == NULL)
5175 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005176
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005177 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005178 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005179
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005180 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005181 shell_configure_fullscreen(shsurf);
5182 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005183 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005184
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005185 /* Update the surface’s layer. This brings it to the top of the stacking
5186 * order as appropriate. */
5187 shell_surface_update_layer(shsurf);
5188
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005189 if (shell->focus_animation_type != ANIMATION_NONE) {
5190 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005191 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005192 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005193}
5194
Alex Wu21858432012-04-01 20:13:08 +08005195/* no-op func for checking black surface */
5196static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005197black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005198{
5199}
5200
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005201static bool
Alex Wu21858432012-04-01 20:13:08 +08005202is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5203{
5204 if (es->configure == black_surface_configure) {
5205 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005206 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005207 return true;
5208 }
5209 return false;
5210}
5211
Kristian Høgsberg75840622011-09-06 13:48:16 -04005212static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005213activate_binding(struct weston_seat *seat,
5214 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005215 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005216{
Derek Foremanbbde6432015-12-10 13:08:01 -06005217 struct focus_state *state;
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005218 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005219 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005220
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005221 focus = focus_view->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005222
Pekka Paalanen01388e22013-04-25 13:57:44 +03005223 if (is_black_surface(focus, &main_surface))
5224 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005225
Pekka Paalanen01388e22013-04-25 13:57:44 +03005226 main_surface = weston_surface_get_main_surface(focus);
5227 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005228 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005229
Derek Foremanbbde6432015-12-10 13:08:01 -06005230 state = ensure_focus_state(shell, seat);
5231 if (state == NULL)
5232 return;
5233
5234 if (state->keyboard_focus == focus)
5235 return;
5236
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005237 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005238}
5239
5240static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005241click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5242 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005243{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005244 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005245 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005246 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005247 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005248
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005249 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005250}
5251
5252static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005253touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5254 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005255{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005256 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005257 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005258 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005259 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005260
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005261 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005262}
5263
5264static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005265unfocus_all_seats(struct desktop_shell *shell)
5266{
5267 struct weston_seat *seat, *next;
5268
5269 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005270 struct weston_keyboard *keyboard =
5271 weston_seat_get_keyboard(seat);
5272
5273 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005274 continue;
5275
Derek Foreman1281a362015-07-31 16:55:32 -05005276 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005277 }
5278}
5279
5280static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005281lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005282{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005283 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005284
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005285 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005286 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005287 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005288 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005289
5290 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005291
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005292 /* Hide all surfaces by removing the fullscreen, panel and
5293 * toplevel layers. This way nothing else can show or receive
5294 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005295
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005296 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005297 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005298 if (shell->showing_input_panels)
5299 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005300 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005301 wl_list_insert(&shell->compositor->cursor_layer.link,
5302 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005303
Derek Foreman004b4a12015-07-20 16:28:13 -05005304 weston_compositor_sleep(shell->compositor);
5305
Neil Robertsb4a91702014-04-09 16:33:32 +01005306 /* Remove the keyboard focus on all seats. This will be
5307 * restored to the workspace's saved state via
5308 * restore_focus_state when the compositor is unlocked */
5309 unfocus_all_seats(shell);
5310
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005311 /* TODO: disable bindings that should not work while locked. */
5312
5313 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005314}
5315
5316static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005317unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005318{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005319 struct wl_resource *shell_resource;
5320
Pekka Paalanend81c2162011-11-16 13:47:34 +02005321 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005322 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005323 return;
5324 }
5325
5326 /* If desktop-shell client has gone away, unlock immediately. */
5327 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005328 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005329 return;
5330 }
5331
5332 if (shell->prepare_event_sent)
5333 return;
5334
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005335 shell_resource = shell->child.desktop_shell;
5336 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005337 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005338}
5339
5340static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005341shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005342{
5343 struct desktop_shell *shell = data;
5344
5345 shell->fade.animation = NULL;
5346
5347 switch (shell->fade.type) {
5348 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005349 weston_surface_destroy(shell->fade.view->surface);
5350 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005351 break;
5352 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005353 lock(shell);
5354 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005355 default:
5356 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005357 }
5358}
5359
Jason Ekstranda7af7042013-10-12 22:38:11 -05005360static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005361shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005362{
5363 struct weston_compositor *compositor = shell->compositor;
5364 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005365 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005366
5367 surface = weston_surface_create(compositor);
5368 if (!surface)
5369 return NULL;
5370
Jason Ekstranda7af7042013-10-12 22:38:11 -05005371 view = weston_view_create(surface);
5372 if (!view) {
5373 weston_surface_destroy(surface);
5374 return NULL;
5375 }
5376
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005377 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005378 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005379 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005380 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5381 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005382 pixman_region32_init(&surface->input);
5383
Jason Ekstranda7af7042013-10-12 22:38:11 -05005384 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005385}
5386
5387static void
5388shell_fade(struct desktop_shell *shell, enum fade_type type)
5389{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005390 float tint;
5391
5392 switch (type) {
5393 case FADE_IN:
5394 tint = 0.0;
5395 break;
5396 case FADE_OUT:
5397 tint = 1.0;
5398 break;
5399 default:
5400 weston_log("shell: invalid fade type\n");
5401 return;
5402 }
5403
5404 shell->fade.type = type;
5405
Jason Ekstranda7af7042013-10-12 22:38:11 -05005406 if (shell->fade.view == NULL) {
5407 shell->fade.view = shell_fade_create_surface(shell);
5408 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005409 return;
5410
Jason Ekstranda7af7042013-10-12 22:38:11 -05005411 shell->fade.view->alpha = 1.0 - tint;
5412 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005413 }
5414
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005415 if (shell->fade.view->output == NULL) {
5416 /* If the black view gets a NULL output, we lost the
5417 * last output and we'll just cancel the fade. This
5418 * happens when you close the last window under the
5419 * X11 or Wayland backends. */
5420 shell->locked = false;
5421 weston_surface_destroy(shell->fade.view->surface);
5422 shell->fade.view = NULL;
5423 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005424 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005425 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005426 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005427 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005428 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005429 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005430 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005431}
5432
5433static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005434do_shell_fade_startup(void *data)
5435{
5436 struct desktop_shell *shell = data;
5437
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005438 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005439 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005440 } else {
5441 weston_log("desktop shell: "
5442 "unexpected fade-in animation type %d\n",
5443 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005444 weston_surface_destroy(shell->fade.view->surface);
5445 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005446 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005447}
5448
5449static void
5450shell_fade_startup(struct desktop_shell *shell)
5451{
5452 struct wl_event_loop *loop;
5453
5454 if (!shell->fade.startup_timer)
5455 return;
5456
5457 wl_event_source_remove(shell->fade.startup_timer);
5458 shell->fade.startup_timer = NULL;
5459
5460 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5461 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5462}
5463
5464static int
5465fade_startup_timeout(void *data)
5466{
5467 struct desktop_shell *shell = data;
5468
5469 shell_fade_startup(shell);
5470 return 0;
5471}
5472
5473static void
5474shell_fade_init(struct desktop_shell *shell)
5475{
5476 /* Make compositor output all black, and wait for the desktop-shell
5477 * client to signal it is ready, then fade in. The timer triggers a
5478 * fade-in, in case the desktop-shell client takes too long.
5479 */
5480
5481 struct wl_event_loop *loop;
5482
Jason Ekstranda7af7042013-10-12 22:38:11 -05005483 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005484 weston_log("%s: warning: fade surface already exists\n",
5485 __func__);
5486 return;
5487 }
5488
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005489 if (shell->startup_animation_type == ANIMATION_NONE)
5490 return;
5491
Jason Ekstranda7af7042013-10-12 22:38:11 -05005492 shell->fade.view = shell_fade_create_surface(shell);
5493 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005494 return;
5495
Jason Ekstranda7af7042013-10-12 22:38:11 -05005496 weston_view_update_transform(shell->fade.view);
5497 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005498
5499 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5500 shell->fade.startup_timer =
5501 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5502 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5503}
5504
5505static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005506idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005507{
5508 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005509 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005510 struct weston_seat *seat;
5511
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005512 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005513 struct weston_touch *touch = weston_seat_get_touch(seat);
5514 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5515
5516 if (pointer)
5517 popup_grab_end(pointer);
5518 if (touch)
5519 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005520 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005521
5522 shell_fade(shell, FADE_OUT);
5523 /* lock() is called from shell_fade_done() */
5524}
5525
5526static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005527wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005528{
5529 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005530 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005531
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005532 unlock(shell);
5533}
5534
5535static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005536transform_handler(struct wl_listener *listener, void *data)
5537{
5538 struct weston_surface *surface = data;
5539 struct shell_surface *shsurf = get_shell_surface(surface);
5540 struct weston_view *view;;
5541 int x, y;
5542
5543 if (!shsurf || !shsurf->client->send_position)
5544 return;
5545
5546 view = shsurf->view;
5547 if (!view || !weston_view_is_mapped(view))
5548 return;
5549
5550 x = view->geometry.x;
5551 y = view->geometry.y;
5552
5553 shsurf->client->send_position(surface, x, y);
5554}
5555
5556static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005557center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005558{
Giulio Camuffob8366642013-04-25 13:57:46 +03005559 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005560 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005561
Jason Ekstranda7af7042013-10-12 22:38:11 -05005562 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005563
5564 x = output->x + (output->width - width) / 2 - surf_x / 2;
5565 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005566
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005567 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005568}
5569
5570static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005571weston_view_set_initial_position(struct weston_view *view,
5572 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005573{
5574 struct weston_compositor *compositor = shell->compositor;
5575 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005576 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005577 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005578 struct weston_output *output, *target_output = NULL;
5579 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005580 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005581
5582 /* As a heuristic place the new window on the same output as the
5583 * pointer. Falling back to the output containing 0, 0.
5584 *
5585 * TODO: Do something clever for touch too?
5586 */
5587 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005588 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5589
5590 if (pointer) {
5591 ix = wl_fixed_to_int(pointer->x);
5592 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005593 break;
5594 }
5595 }
5596
5597 wl_list_for_each(output, &compositor->output_list, link) {
5598 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5599 target_output = output;
5600 break;
5601 }
5602 }
5603
5604 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005605 weston_view_set_position(view, 10 + random() % 400,
5606 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005607 return;
5608 }
5609
5610 /* Valid range within output where the surface will still be onscreen.
5611 * If this is negative it means that the surface is bigger than
5612 * output.
5613 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005614 get_output_work_area(shell, target_output, &area);
5615
Derek Foremanf814c5d2015-04-15 12:23:54 -05005616 x = area.x;
5617 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005618 range_x = area.width - view->surface->width;
5619 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005620
Rob Bradford4cb88c72012-08-13 15:18:44 +01005621 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005622 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005623
5624 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005625 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005626
Jason Ekstranda7af7042013-10-12 22:38:11 -05005627 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005628}
5629
5630static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005631set_maximized_position(struct desktop_shell *shell,
5632 struct shell_surface *shsurf)
5633{
5634 int32_t surf_x, surf_y;
5635 pixman_rectangle32_t area;
5636
Jonny Lambd73c6942014-08-20 15:53:20 +02005637 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005638 if (shsurf->has_set_geometry) {
5639 surf_x = shsurf->geometry.x;
5640 surf_y = shsurf->geometry.y;
5641 } else {
5642 surface_subsurfaces_boundingbox(shsurf->surface,
5643 &surf_x, &surf_y, NULL, NULL);
5644 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005645
5646 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005647 area.x - surf_x,
5648 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005649}
5650
5651static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005652map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005653 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005654{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005655 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005656 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005657
Pekka Paalanen77346a62011-11-30 16:26:35 +02005658 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005659 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005660 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005661 if (shsurf->state.fullscreen) {
5662 center_on_output(shsurf->view, shsurf->fullscreen_output);
5663 shell_map_fullscreen(shsurf);
5664 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005665 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005666 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005667 weston_view_set_initial_position(shsurf->view, shell);
5668 }
Juan Zhao96879df2012-02-07 08:45:41 +08005669 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005670 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005671 if (shell_map_popup(shsurf) != 0)
5672 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005673 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005674 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005675 weston_view_set_position(shsurf->view,
5676 shsurf->view->geometry.x + sx,
5677 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005678 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005679 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005680 default:
5681 ;
5682 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005683
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005684 /* Surface stacking order, see also activate(). */
5685 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005686
Jason Ekstranda7af7042013-10-12 22:38:11 -05005687 if (shsurf->type != SHELL_SURFACE_NONE) {
5688 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005689 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005690 shsurf->surface->output = shsurf->output;
5691 shsurf->view->output = shsurf->output;
5692 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005693 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005694
Jason Ekstranda7af7042013-10-12 22:38:11 -05005695 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005696 /* XXX: xwayland's using the same fields for transient type */
5697 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005698 if (shsurf->transient.flags ==
5699 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5700 break;
5701 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005702 if (shsurf->state.relative &&
5703 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5704 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005705 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005706 break;
5707 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005708 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005709 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005710 case SHELL_SURFACE_POPUP:
5711 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005712 default:
5713 break;
5714 }
5715
Rafael Antognolli03b16592013-12-03 15:35:42 -02005716 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5717 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005718 {
5719 switch (shell->win_animation_type) {
5720 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005721 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005722 break;
5723 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005724 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005725 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005726 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005727 default:
5728 break;
5729 }
5730 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005731}
5732
5733static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005734configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005735 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005736{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005737 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005738 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005739
Pekka Paalanen77346a62011-11-30 16:26:35 +02005740 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005741
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005742 assert(shsurf);
5743
Rafael Antognolli03b16592013-12-03 15:35:42 -02005744 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005745 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005746 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005747 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005748 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005749 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005750 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005751
Alex Wu4539b082012-03-01 12:57:46 +08005752 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005753 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005754 wl_list_for_each(view, &surface->views, surface_link)
5755 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005756
Rafael Antognolli03b16592013-12-03 15:35:42 -02005757 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005758 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005759 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005760}
5761
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005762static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005763shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005764{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005765 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005766 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005767 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005768
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005769 assert(shsurf);
5770
5771 shell = shsurf->shell;
5772
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005773 if (!weston_surface_is_mapped(es) &&
5774 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005775 remove_popup_grab(shsurf);
5776 }
5777
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005778 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005779 return;
5780
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005781 if (shsurf->has_next_geometry) {
5782 shsurf->geometry = shsurf->next_geometry;
5783 shsurf->has_next_geometry = false;
5784 shsurf->has_set_geometry = true;
5785 } else if (!shsurf->has_set_geometry) {
5786 surface_subsurfaces_boundingbox(shsurf->surface,
5787 &shsurf->geometry.x,
5788 &shsurf->geometry.y,
5789 &shsurf->geometry.width,
5790 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005791 }
5792
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005793 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005794 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005795 type_changed = 1;
5796 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005797
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005798 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005799 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005800 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005801 shsurf->last_width != es->width ||
5802 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005803 float from_x, from_y;
5804 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005805
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005806 if (shsurf->resize_edges) {
5807 sx = 0;
5808 sy = 0;
5809 }
5810
5811 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5812 sx = shsurf->last_width - es->width;
5813 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5814 sy = shsurf->last_height - es->height;
5815
5816 shsurf->last_width = es->width;
5817 shsurf->last_height = es->height;
5818
Jason Ekstranda7af7042013-10-12 22:38:11 -05005819 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5820 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005821 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005822 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005823 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005824 }
5825}
5826
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005827static bool
5828check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5829{
5830 struct timespec now;
5831
5832 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5833 return false;
5834
5835 /*
5836 * If the shell helper client dies before the session has been
5837 * up for roughly 30 seconds, better just make Weston shut down,
5838 * because the user likely has no way to interact with the desktop
5839 * anyway.
5840 */
5841 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5842 weston_log("Error: %s apparently cannot run at all.\n",
5843 shell->client);
5844 weston_log_continue(STAMP_SPACE "Quitting...");
5845 wl_display_terminate(shell->compositor->wl_display);
5846
5847 return true;
5848 }
5849
5850 return false;
5851}
5852
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005853static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005854
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005855static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005856respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005857{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005858 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005859
5860 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5861 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005862 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005863 shell->child.deathstamp = time;
5864 shell->child.deathcount = 0;
5865 }
5866
5867 shell->child.deathcount++;
5868 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005869 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005870 return;
5871 }
5872
Pekka Paalanen826dc142014-08-27 12:11:53 +03005873 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005874 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005875}
5876
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005877static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005878desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5879{
5880 struct desktop_shell *shell;
5881
5882 shell = container_of(listener, struct desktop_shell,
5883 child.client_destroy_listener);
5884
Pekka Paalanen826dc142014-08-27 12:11:53 +03005885 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005886 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005887 /*
5888 * unbind_desktop_shell() will reset shell->child.desktop_shell
5889 * before the respawned process has a chance to create a new
5890 * desktop_shell object, because we are being called from the
5891 * wl_client destructor which destroys all wl_resources before
5892 * returning.
5893 */
5894
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005895 if (!check_desktop_shell_crash_too_early(shell))
5896 respawn_desktop_shell_process(shell);
5897
Pekka Paalanen826dc142014-08-27 12:11:53 +03005898 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005899}
5900
5901static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005902launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005903{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005904 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005905
Pekka Paalanen826dc142014-08-27 12:11:53 +03005906 shell->child.client = weston_client_start(shell->compositor,
5907 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005908
Arnaud Vrac42631192014-08-25 20:56:46 +02005909 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005910 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005911 return;
5912 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005913
5914 shell->child.client_destroy_listener.notify =
5915 desktop_shell_client_destroy;
5916 wl_client_add_destroy_listener(shell->child.client,
5917 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005918}
5919
5920static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005921handle_shell_client_destroy(struct wl_listener *listener, void *data)
5922{
5923 struct shell_client *sc =
5924 container_of(listener, struct shell_client, destroy_listener);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005925 struct wl_resource *shsurf_resource;
5926 struct shell_surface *shsurf;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005927
5928 if (sc->ping_timer)
5929 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005930
5931 /* Since we're about to free shell_client, we remove it from the
5932 * head of the surface list so we don't use that freed list node
5933 * during surface clean up later on.
5934 */
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005935 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
5936 shsurf = wl_resource_get_user_data(shsurf_resource);
5937 shsurf->owner = NULL;
5938 }
Derek Foremane42d7542015-05-29 10:46:44 -05005939 wl_list_remove(&sc->surface_list);
5940
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005941 free(sc);
5942}
5943
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005944static struct shell_client *
5945shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5946 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005947{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005948 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005949
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005950 sc = zalloc(sizeof *sc);
5951 if (sc == NULL) {
5952 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005953 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005954 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005955
5956 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005957 if (sc->resource == NULL) {
5958 free(sc);
5959 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005960 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005961 }
5962
5963 sc->client = client;
5964 sc->shell = shell;
5965 sc->destroy_listener.notify = handle_shell_client_destroy;
5966 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005967 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005968
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005969 return sc;
5970}
5971
5972static void
5973bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5974{
5975 struct desktop_shell *shell = data;
5976 struct shell_client *sc;
5977
5978 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5979 if (sc)
5980 wl_resource_set_implementation(sc->resource,
5981 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005982 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005983}
5984
5985static void
5986bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5987{
5988 struct desktop_shell *shell = data;
5989 struct shell_client *sc;
5990
5991 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5992 if (sc)
5993 wl_resource_set_dispatcher(sc->resource,
5994 xdg_shell_unversioned_dispatch,
5995 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005996}
5997
5998static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005999unbind_desktop_shell(struct wl_resource *resource)
6000{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05006001 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05006002
6003 if (shell->locked)
6004 resume_desktop(shell);
6005
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006006 shell->child.desktop_shell = NULL;
6007 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006008}
6009
6010static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04006011bind_desktop_shell(struct wl_client *client,
6012 void *data, uint32_t version, uint32_t id)
6013{
Tiago Vignattibe143262012-04-16 17:31:41 +03006014 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006015 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04006016
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006017 resource = wl_resource_create(client, &weston_desktop_shell_interface,
6018 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006019
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006020 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05006021 wl_resource_set_implementation(resource,
6022 &desktop_shell_implementation,
6023 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006024 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006025 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006026 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006027
6028 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
6029 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04006030}
6031
Kristian Høgsberg07045392012-02-19 18:52:44 -05006032struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03006033 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006034 struct weston_surface *current;
6035 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006036 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006037 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006038};
6039
6040static void
6041switcher_next(struct switcher *switcher)
6042{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006043 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006044 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006045 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006046 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006047
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006048 /* temporary re-display minimized surfaces */
6049 struct weston_view *tmp;
6050 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006051 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6052 weston_layer_entry_remove(&view->layer_link);
6053 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006054 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6055 *minimized = view;
6056 }
6057
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006058 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006059 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006060 if (shsurf &&
6061 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6062 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006063 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006064 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006065 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006066 next = view->surface;
6067 prev = view->surface;
6068 view->alpha = 0.25;
6069 weston_view_geometry_dirty(view);
6070 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006071 }
Alex Wu1659daa2012-04-01 20:13:09 +08006072
Jason Ekstranda7af7042013-10-12 22:38:11 -05006073 if (is_black_surface(view->surface, NULL)) {
6074 view->alpha = 0.25;
6075 weston_view_geometry_dirty(view);
6076 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006077 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006078 }
6079
6080 if (next == NULL)
6081 next = first;
6082
Alex Wu07b26062012-03-12 16:06:01 +08006083 if (next == NULL)
6084 return;
6085
Kristian Høgsberg07045392012-02-19 18:52:44 -05006086 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006087 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006088
6089 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006090 wl_list_for_each(view, &next->views, surface_link)
6091 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006092
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006093 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006094 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006095 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006096}
6097
6098static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006099switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006100{
6101 struct switcher *switcher =
6102 container_of(listener, struct switcher, listener);
6103
6104 switcher_next(switcher);
6105}
6106
6107static void
Daniel Stone351eb612012-05-31 15:27:47 -04006108switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006109{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006110 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006111 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006112 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006113
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006114 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006115 if (is_focus_view(view))
6116 continue;
6117
Jason Ekstranda7af7042013-10-12 22:38:11 -05006118 view->alpha = 1.0;
6119 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006120 }
6121
Alex Wu07b26062012-03-12 16:06:01 +08006122 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006123 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006124 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006125 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006126 weston_keyboard_end_grab(keyboard);
6127 if (keyboard->input_method_resource)
6128 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006129
6130 /* re-hide surfaces that were temporary shown during the switch */
6131 struct weston_view **minimized;
6132 wl_array_for_each(minimized, &switcher->minimized_array) {
6133 /* with the exception of the current selected */
6134 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006135 weston_layer_entry_remove(&(*minimized)->layer_link);
6136 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006137 weston_view_damage_below(*minimized);
6138 }
6139 }
6140 wl_array_release(&switcher->minimized_array);
6141
Kristian Høgsberg07045392012-02-19 18:52:44 -05006142 free(switcher);
6143}
6144
6145static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006146switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006147 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006148{
6149 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006150 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006151
Daniel Stonec9785ea2012-05-30 16:31:52 +01006152 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006153 switcher_next(switcher);
6154}
6155
6156static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006157switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006158 uint32_t mods_depressed, uint32_t mods_latched,
6159 uint32_t mods_locked, uint32_t group)
6160{
6161 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006162 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006163
Daniel Stone351eb612012-05-31 15:27:47 -04006164 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6165 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006166}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006167
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006168static void
6169switcher_cancel(struct weston_keyboard_grab *grab)
6170{
6171 struct switcher *switcher = container_of(grab, struct switcher, grab);
6172
6173 switcher_destroy(switcher);
6174}
6175
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006176static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006177 switcher_key,
6178 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006179 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006180};
6181
6182static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006183switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6184 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006185{
Tiago Vignattibe143262012-04-16 17:31:41 +03006186 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006187 struct switcher *switcher;
6188
6189 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006190 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006191 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006192 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006193 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006194 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006195
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006196 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006197 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006198 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006199 weston_keyboard_start_grab(keyboard, &switcher->grab);
6200 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006201 switcher_next(switcher);
6202}
6203
Pekka Paalanen3c647232011-12-22 13:43:43 +02006204static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006205backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6206 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006207{
6208 struct weston_compositor *compositor = data;
6209 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006210 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006211
6212 /* TODO: we're limiting to simple use cases, where we assume just
6213 * control on the primary display. We'd have to extend later if we
6214 * ever get support for setting backlights on random desktop LCD
6215 * panels though */
6216 output = get_default_output(compositor);
6217 if (!output)
6218 return;
6219
6220 if (!output->set_backlight)
6221 return;
6222
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006223 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6224 backlight_new = output->backlight_current - 25;
6225 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6226 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006227
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006228 if (backlight_new < 5)
6229 backlight_new = 5;
6230 if (backlight_new > 255)
6231 backlight_new = 255;
6232
6233 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006234 output->set_backlight(output, output->backlight_current);
6235}
6236
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006237static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006238force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6239 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006240{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006241 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006242 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006243 struct desktop_shell *shell = data;
6244 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006245 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006246
Derek Foreman8ae2db52015-07-15 13:00:36 -05006247 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006248 if (!focus_surface)
6249 return;
6250
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006251 wl_signal_emit(&compositor->kill_signal, focus_surface);
6252
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006253 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006254 wl_client_get_credentials(client, &pid, NULL, NULL);
6255
6256 /* Skip clients that we launched ourselves (the credentials of
6257 * the socketpair is ours) */
6258 if (pid == getpid())
6259 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006260
Daniel Stone325fc2d2012-05-30 16:31:58 +01006261 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006262}
6263
6264static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006265workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006266 uint32_t key, void *data)
6267{
6268 struct desktop_shell *shell = data;
6269 unsigned int new_index = shell->workspaces.current;
6270
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006271 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006272 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006273 if (new_index != 0)
6274 new_index--;
6275
6276 change_workspace(shell, new_index);
6277}
6278
6279static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006280workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006281 uint32_t key, void *data)
6282{
6283 struct desktop_shell *shell = data;
6284 unsigned int new_index = shell->workspaces.current;
6285
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006286 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006287 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006288 if (new_index < shell->workspaces.num - 1)
6289 new_index++;
6290
6291 change_workspace(shell, new_index);
6292}
6293
6294static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006295workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006296 uint32_t key, void *data)
6297{
6298 struct desktop_shell *shell = data;
6299 unsigned int new_index;
6300
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006301 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006302 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006303 new_index = key - KEY_F1;
6304 if (new_index >= shell->workspaces.num)
6305 new_index = shell->workspaces.num - 1;
6306
6307 change_workspace(shell, new_index);
6308}
6309
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006310static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006311workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6312 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006313{
6314 struct desktop_shell *shell = data;
6315 unsigned int new_index = shell->workspaces.current;
6316
6317 if (shell->locked)
6318 return;
6319
6320 if (new_index != 0)
6321 new_index--;
6322
Derek Foreman8ae2db52015-07-15 13:00:36 -05006323 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006324}
6325
6326static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006327workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6328 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006329{
6330 struct desktop_shell *shell = data;
6331 unsigned int new_index = shell->workspaces.current;
6332
6333 if (shell->locked)
6334 return;
6335
6336 if (new_index < shell->workspaces.num - 1)
6337 new_index++;
6338
Derek Foreman8ae2db52015-07-15 13:00:36 -05006339 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006340}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006341
6342static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006343shell_reposition_view_on_output_destroy(struct weston_view *view)
6344{
6345 struct weston_output *output, *first_output;
6346 struct weston_compositor *ec = view->surface->compositor;
6347 struct shell_surface *shsurf;
6348 float x, y;
6349 int visible;
6350
6351 x = view->geometry.x;
6352 y = view->geometry.y;
6353
6354 /* At this point the destroyed output is not in the list anymore.
6355 * If the view is still visible somewhere, we leave where it is,
6356 * otherwise, move it to the first output. */
6357 visible = 0;
6358 wl_list_for_each(output, &ec->output_list, link) {
6359 if (pixman_region32_contains_point(&output->region,
6360 x, y, NULL)) {
6361 visible = 1;
6362 break;
6363 }
6364 }
6365
6366 if (!visible) {
6367 first_output = container_of(ec->output_list.next,
6368 struct weston_output, link);
6369
6370 x = first_output->x + first_output->width / 4;
6371 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006372
6373 weston_view_set_position(view, x, y);
6374 } else {
6375 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006376 }
6377
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006378
6379 shsurf = get_shell_surface(view->surface);
6380
6381 if (shsurf) {
6382 shsurf->saved_position_valid = false;
6383 shsurf->next_state.maximized = false;
6384 shsurf->next_state.fullscreen = false;
6385 shsurf->state_changed = true;
6386 }
6387}
6388
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006389void
6390shell_for_each_layer(struct desktop_shell *shell,
6391 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006392{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006393 struct workspace **ws;
6394
6395 func(shell, &shell->fullscreen_layer, data);
6396 func(shell, &shell->panel_layer, data);
6397 func(shell, &shell->background_layer, data);
6398 func(shell, &shell->lock_layer, data);
6399 func(shell, &shell->input_panel_layer, data);
6400
6401 wl_array_for_each(ws, &shell->workspaces.array)
6402 func(shell, &(*ws)->layer, data);
6403}
6404
6405static void
6406shell_output_destroy_move_layer(struct desktop_shell *shell,
6407 struct weston_layer *layer,
6408 void *data)
6409{
6410 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006411 struct weston_view *view;
6412
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006413 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006414 if (view->output != output)
6415 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006416
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006417 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006418 }
6419}
6420
6421static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006422handle_output_destroy(struct wl_listener *listener, void *data)
6423{
6424 struct shell_output *output_listener =
6425 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006426 struct weston_output *output = output_listener->output;
6427 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006428
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006429 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006430
Xiong Zhang6b481422013-10-23 13:58:32 +08006431 wl_list_remove(&output_listener->destroy_listener.link);
6432 wl_list_remove(&output_listener->link);
6433 free(output_listener);
6434}
6435
6436static void
David Fort50d962f2016-06-09 17:55:52 +02006437shell_resize_surface_to_output(struct desktop_shell *shell,
6438 struct weston_surface *surface,
6439 const struct weston_output *output)
6440{
6441 if (!surface)
6442 return;
6443
6444 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
6445 surface->resource,
6446 output->width,
6447 output->height);
6448}
6449
6450
6451static void
6452handle_output_resized(struct wl_listener *listener, void *data)
6453{
6454 struct desktop_shell *shell =
6455 container_of(listener, struct desktop_shell, resized_listener);
6456 struct weston_output *output = (struct weston_output *)data;
6457 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
6458
6459 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
6460 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
6461}
6462
6463static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006464create_shell_output(struct desktop_shell *shell,
6465 struct weston_output *output)
6466{
6467 struct shell_output *shell_output;
6468
6469 shell_output = zalloc(sizeof *shell_output);
6470 if (shell_output == NULL)
6471 return;
6472
6473 shell_output->output = output;
6474 shell_output->shell = shell;
6475 shell_output->destroy_listener.notify = handle_output_destroy;
6476 wl_signal_add(&output->destroy_signal,
6477 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006478 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006479}
6480
6481static void
6482handle_output_create(struct wl_listener *listener, void *data)
6483{
6484 struct desktop_shell *shell =
6485 container_of(listener, struct desktop_shell, output_create_listener);
6486 struct weston_output *output = (struct weston_output *)data;
6487
6488 create_shell_output(shell, output);
6489}
6490
6491static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006492handle_output_move_layer(struct desktop_shell *shell,
6493 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006494{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006495 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006496 struct weston_view *view;
6497 float x, y;
6498
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006499 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006500 if (view->output != output)
6501 continue;
6502
6503 x = view->geometry.x + output->move_x;
6504 y = view->geometry.y + output->move_y;
6505 weston_view_set_position(view, x, y);
6506 }
6507}
6508
6509static void
6510handle_output_move(struct wl_listener *listener, void *data)
6511{
6512 struct desktop_shell *shell;
6513
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006514 shell = container_of(listener, struct desktop_shell,
6515 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006516
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006517 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006518}
6519
6520static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006521setup_output_destroy_handler(struct weston_compositor *ec,
6522 struct desktop_shell *shell)
6523{
6524 struct weston_output *output;
6525
6526 wl_list_init(&shell->output_list);
6527 wl_list_for_each(output, &ec->output_list, link)
6528 create_shell_output(shell, output);
6529
6530 shell->output_create_listener.notify = handle_output_create;
6531 wl_signal_add(&ec->output_created_signal,
6532 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006533
6534 shell->output_move_listener.notify = handle_output_move;
6535 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006536}
6537
6538static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006539shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006540{
Tiago Vignattibe143262012-04-16 17:31:41 +03006541 struct desktop_shell *shell =
6542 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006543 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006544 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006545
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006546 /* Force state to unlocked so we don't try to fade */
6547 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006548
6549 if (shell->child.client) {
6550 /* disable respawn */
6551 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006552 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006553 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006554
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006555 wl_list_remove(&shell->idle_listener.link);
6556 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006557 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006558
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006559 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006560 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006561
Xiong Zhang6b481422013-10-23 13:58:32 +08006562 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6563 wl_list_remove(&shell_output->destroy_listener.link);
6564 wl_list_remove(&shell_output->link);
6565 free(shell_output);
6566 }
6567
6568 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006569 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02006570 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006571
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006572 wl_array_for_each(ws, &shell->workspaces.array)
6573 workspace_destroy(*ws);
6574 wl_array_release(&shell->workspaces.array);
6575
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006576 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006577 free(shell);
6578}
6579
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006580static void
6581shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6582{
6583 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006584 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006585
Bob Ham744e6532016-01-12 10:21:48 +00006586 if (shell->allow_zap)
6587 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6588 MODIFIER_CTRL | MODIFIER_ALT,
6589 terminate_binding, ec);
6590
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006591 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006592 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6593 click_to_activate_binding,
6594 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006595 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6596 click_to_activate_binding,
6597 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006598 weston_compositor_add_touch_binding(ec, 0,
6599 touch_to_activate_binding,
6600 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006601 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6602 MODIFIER_SUPER | MODIFIER_ALT,
6603 surface_opacity_binding, NULL);
6604 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6605 MODIFIER_SUPER, zoom_axis_binding,
6606 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006607 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6608 backlight_binding, ec);
6609 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6610 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006611
6612 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006613 if (shell->exposay_modifier)
6614 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6615 exposay_binding, shell);
6616
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006617 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006618 if (!mod)
6619 return;
6620
Daniel Stone325fc2d2012-05-30 16:31:58 +01006621 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6622 zoom_key_binding, NULL);
6623 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6624 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006625 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6626 maximize_binding, NULL);
6627 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6628 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006629 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6630 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006631 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006632 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006633 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006634 weston_compositor_add_button_binding(ec, BTN_LEFT,
6635 mod | MODIFIER_SHIFT,
6636 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006637
6638 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006639 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006640 rotate_binding, NULL);
6641
Daniel Stone325fc2d2012-05-30 16:31:58 +01006642 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6643 shell);
6644 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6645 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006646 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6647 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006648 weston_compositor_add_key_binding(ec, KEY_K, mod,
6649 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006650 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6651 workspace_up_binding, shell);
6652 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6653 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006654 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6655 workspace_move_surface_up_binding,
6656 shell);
6657 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6658 workspace_move_surface_down_binding,
6659 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006660
6661 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6662 if (shell->workspaces.num > 1) {
6663 num_workspace_bindings = shell->workspaces.num;
6664 if (num_workspace_bindings > 6)
6665 num_workspace_bindings = 6;
6666 for (i = 0; i < num_workspace_bindings; i++)
6667 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6668 workspace_f_binding,
6669 shell);
6670 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006671
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006672 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006673}
6674
Jason Ekstrand024177c2014-04-21 19:42:58 -05006675static void
6676handle_seat_created(struct wl_listener *listener, void *data)
6677{
6678 struct weston_seat *seat = data;
6679
6680 create_shell_seat(seat);
6681}
6682
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006683WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006684module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006685 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006686{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006687 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006688 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006689 struct workspace **pws;
6690 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006691 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006692
Peter Huttererf3d62272013-08-08 11:57:05 +10006693 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006694 if (shell == NULL)
6695 return -1;
6696
Kristian Høgsberg75840622011-09-06 13:48:16 -04006697 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006698
6699 shell->destroy_listener.notify = shell_destroy;
6700 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006701 shell->idle_listener.notify = idle_handler;
6702 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6703 shell->wake_listener.notify = wake_handler;
6704 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006705 shell->transform_listener.notify = transform_handler;
6706 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006707
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006708 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006709 ec->shell_interface.create_shell_surface = create_shell_surface;
6710 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006711 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006712 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006713 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006714 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006715 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006716 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006717 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006718 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006719 ec->shell_interface.set_pid = set_pid;
Quentin Glidic5c201952016-03-23 13:50:49 +01006720 ec->shell_interface.get_output_work_area = get_output_work_area;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006721
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006722 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6723 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006724 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6725 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006726 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006727
6728 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006729 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006730
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006731 if (input_panel_setup(shell) < 0)
6732 return -1;
6733
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006734 shell->text_backend = text_backend_init(ec);
6735 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006736 return -1;
6737
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006738 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006739
Daniel Stonedf8133b2013-11-19 11:37:14 +01006740 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6741 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6742
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006743 for (i = 0; i < shell->workspaces.num; i++) {
6744 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6745 if (pws == NULL)
6746 return -1;
6747
6748 *pws = workspace_create();
6749 if (*pws == NULL)
6750 return -1;
6751 }
6752 activate_workspace(shell, 0);
6753
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006754 weston_layer_init(&shell->minimized_layer, NULL);
6755
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006756 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006757 wl_list_init(&shell->workspaces.animation.link);
6758 shell->workspaces.animation.frame = animate_workspace_change_frame;
6759
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006760 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006761 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006762 return -1;
6763
Rafael Antognollie2a34552013-12-03 15:35:45 -02006764 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6765 shell, bind_xdg_shell) == NULL)
6766 return -1;
6767
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006768 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006769 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006770 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006771 return -1;
6772
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006773 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006774
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006775 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006776
Xiong Zhang6b481422013-10-23 13:58:32 +08006777 setup_output_destroy_handler(ec, shell);
6778
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006779 loop = wl_display_get_event_loop(ec->wl_display);
6780 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006781
Jason Ekstrand024177c2014-04-21 19:42:58 -05006782 wl_list_for_each(seat, &ec->seat_list, link)
6783 handle_seat_created(NULL, seat);
6784 shell->seat_create_listener.notify = handle_seat_created;
6785 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006786
David Fort50d962f2016-06-09 17:55:52 +02006787 shell->resized_listener.notify = handle_output_resized;
6788 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
6789
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006790 screenshooter_create(ec);
6791
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006792 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006793
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006794 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006795
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006796 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6797
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006798 return 0;
6799}