blob: 4a44f9669929b5abb46556f4ceac58da27c3c31e [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;
Armin Krezović4663aca2016-06-30 06:04:29 +0200752 surface->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100753 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300754 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100755
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800756 fsurf->view = weston_view_create(surface);
757 if (fsurf->view == NULL) {
758 weston_surface_destroy(surface);
759 free(fsurf);
760 return NULL;
761 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100762 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200763 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100764
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600765 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600766 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100767 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
768 pixman_region32_fini(&surface->opaque);
769 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
770 output->width, output->height);
771 pixman_region32_fini(&surface->input);
772 pixman_region32_init(&surface->input);
773
774 wl_list_init(&fsurf->workspace_transform.link);
775
776 return fsurf;
777}
778
779static void
780focus_surface_destroy(struct focus_surface *fsurf)
781{
782 weston_surface_destroy(fsurf->surface);
783 free(fsurf);
784}
785
786static void
787focus_animation_done(struct weston_view_animation *animation, void *data)
788{
789 struct workspace *ws = data;
790
791 ws->focus_animation = NULL;
792}
793
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200794static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200795focus_state_destroy(struct focus_state *state)
796{
797 wl_list_remove(&state->seat_destroy_listener.link);
798 wl_list_remove(&state->surface_destroy_listener.link);
799 free(state);
800}
801
802static void
803focus_state_seat_destroy(struct wl_listener *listener, void *data)
804{
805 struct focus_state *state = container_of(listener,
806 struct focus_state,
807 seat_destroy_listener);
808
809 wl_list_remove(&state->link);
810 focus_state_destroy(state);
811}
812
813static void
814focus_state_surface_destroy(struct wl_listener *listener, void *data)
815{
816 struct focus_state *state = container_of(listener,
817 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400818 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400819 struct desktop_shell *shell;
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200820 struct weston_surface *main_surface;
821 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500822 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200823
Pekka Paalanen01388e22013-04-25 13:57:44 +0300824 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
825
Kristian Høgsberge3778222012-07-31 17:29:30 -0400826 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300827 wl_list_for_each(view,
828 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500829 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400830 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100831 if (is_focus_view(view))
832 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400833
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200834 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400835 break;
836 }
837
Pekka Paalanen01388e22013-04-25 13:57:44 +0300838 /* if the focus was a sub-surface, activate its main surface */
839 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200840 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300841
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100842 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400843 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100844 state->keyboard_focus = NULL;
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200845 activate(shell, next, state->seat,
846 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400847 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100848 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
849 if (state->ws->focus_animation)
850 weston_view_animation_destroy(state->ws->focus_animation);
851
852 state->ws->focus_animation = weston_fade_run(
853 state->ws->fsurf_front->view,
854 state->ws->fsurf_front->view->alpha, 0.0, 300,
855 focus_animation_done, state->ws);
856 }
857
Kristian Høgsberge3778222012-07-31 17:29:30 -0400858 wl_list_remove(&state->link);
859 focus_state_destroy(state);
860 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200861}
862
863static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400864focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200865{
Jonas Ådahl04769742012-06-13 00:01:24 +0200866 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200867
868 state = malloc(sizeof *state);
869 if (state == NULL)
870 return NULL;
871
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100872 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400873 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200874 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400875 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200876
877 state->seat_destroy_listener.notify = focus_state_seat_destroy;
878 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400879 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200880 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400881 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200882
883 return state;
884}
885
Jonas Ådahl8538b222012-08-29 22:13:03 +0200886static struct focus_state *
887ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
888{
889 struct workspace *ws = get_current_workspace(shell);
890 struct focus_state *state;
891
892 wl_list_for_each(state, &ws->focus_list, link)
893 if (state->seat == seat)
894 break;
895
896 if (&state->link == &ws->focus_list)
897 state = focus_state_create(seat, ws);
898
899 return state;
900}
901
Jonas Ådahl04769742012-06-13 00:01:24 +0200902static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800903focus_state_set_focus(struct focus_state *state,
904 struct weston_surface *surface)
905{
906 if (state->keyboard_focus) {
907 wl_list_remove(&state->surface_destroy_listener.link);
908 wl_list_init(&state->surface_destroy_listener.link);
909 }
910
911 state->keyboard_focus = surface;
912 if (surface)
913 wl_signal_add(&surface->destroy_signal,
914 &state->surface_destroy_listener);
915}
916
917static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400918restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200919{
920 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400921 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100922 struct wl_list pending_seat_list;
923 struct weston_seat *seat, *next_seat;
924
925 /* Temporarily steal the list of seats so that we can keep
926 * track of the seats we've already processed */
927 wl_list_init(&pending_seat_list);
928 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
929 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200930
931 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500932 struct weston_keyboard *keyboard =
933 weston_seat_get_keyboard(state->seat);
934
Neil Roberts4237f502014-04-09 16:33:31 +0100935 wl_list_remove(&state->seat->link);
936 wl_list_insert(&shell->compositor->seat_list,
937 &state->seat->link);
938
Derek Foreman1281a362015-07-31 16:55:32 -0500939 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800940 continue;
941
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400942 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200943
Derek Foreman1281a362015-07-31 16:55:32 -0500944 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200945 }
Neil Roberts4237f502014-04-09 16:33:31 +0100946
947 /* For any remaining seats that we don't have a focus state
948 * for we'll reset the keyboard focus to NULL */
949 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500950 struct weston_keyboard *keyboard =
951 weston_seat_get_keyboard(seat);
952
Neil Roberts4237f502014-04-09 16:33:31 +0100953 wl_list_insert(&shell->compositor->seat_list, &seat->link);
954
Derek Foreman1281a362015-07-31 16:55:32 -0500955 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100956 continue;
957
Derek Foreman1281a362015-07-31 16:55:32 -0500958 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100959 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200960}
961
962static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200963replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
964 struct weston_seat *seat)
965{
Derek Foreman1281a362015-07-31 16:55:32 -0500966 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200967 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200968
969 wl_list_for_each(state, &ws->focus_list, link) {
970 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500971 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200972 return;
973 }
974 }
975}
976
977static void
978drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
979 struct weston_surface *surface)
980{
981 struct focus_state *state;
982
983 wl_list_for_each(state, &ws->focus_list, link)
984 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800985 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200986}
987
988static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100989animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
990 struct weston_view *from, struct weston_view *to)
991{
992 struct weston_output *output;
993 bool focus_surface_created = false;
994
995 /* FIXME: Only support dim animation using two layers */
996 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
997 return;
998
999 output = get_default_output(shell->compositor);
1000 if (ws->fsurf_front == NULL && (from || to)) {
1001 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001002 if (ws->fsurf_front == NULL)
1003 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001004 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001005
1006 ws->fsurf_back = create_focus_surface(shell->compositor, output);
1007 if (ws->fsurf_back == NULL) {
1008 focus_surface_destroy(ws->fsurf_front);
1009 return;
1010 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001011 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001012
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001013 focus_surface_created = true;
1014 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001015 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1016 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001017 }
1018
1019 if (ws->focus_animation) {
1020 weston_view_animation_destroy(ws->focus_animation);
1021 ws->focus_animation = NULL;
1022 }
1023
1024 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001025 weston_layer_entry_insert(&to->layer_link,
1026 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001027 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001028 weston_layer_entry_insert(&ws->layer.view_list,
1029 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001030
1031 if (focus_surface_created) {
1032 ws->focus_animation = weston_fade_run(
1033 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001034 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001035 focus_animation_done, ws);
1036 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001037 weston_layer_entry_insert(&from->layer_link,
1038 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001039 ws->focus_animation = weston_stable_fade_run(
1040 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001041 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001042 focus_animation_done, ws);
1043 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001044 weston_layer_entry_insert(&ws->layer.view_list,
1045 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001046 ws->focus_animation = weston_stable_fade_run(
1047 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001048 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001049 focus_animation_done, ws);
1050 }
1051}
1052
1053static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001054workspace_destroy(struct workspace *ws)
1055{
Jonas Ådahl04769742012-06-13 00:01:24 +02001056 struct focus_state *state, *next;
1057
1058 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1059 focus_state_destroy(state);
1060
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001061 if (ws->fsurf_front)
1062 focus_surface_destroy(ws->fsurf_front);
1063 if (ws->fsurf_back)
1064 focus_surface_destroy(ws->fsurf_back);
1065
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001066 free(ws);
1067}
1068
Jonas Ådahl04769742012-06-13 00:01:24 +02001069static void
1070seat_destroyed(struct wl_listener *listener, void *data)
1071{
1072 struct weston_seat *seat = data;
1073 struct focus_state *state, *next;
1074 struct workspace *ws = container_of(listener,
1075 struct workspace,
1076 seat_destroyed_listener);
1077
1078 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1079 if (state->seat == seat)
1080 wl_list_remove(&state->link);
1081}
1082
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001083static struct workspace *
1084workspace_create(void)
1085{
1086 struct workspace *ws = malloc(sizeof *ws);
1087 if (ws == NULL)
1088 return NULL;
1089
1090 weston_layer_init(&ws->layer, NULL);
1091
Jonas Ådahl04769742012-06-13 00:01:24 +02001092 wl_list_init(&ws->focus_list);
1093 wl_list_init(&ws->seat_destroyed_listener.link);
1094 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001095 ws->fsurf_front = NULL;
1096 ws->fsurf_back = NULL;
1097 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001098
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001099 return ws;
1100}
1101
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001102static int
1103workspace_is_empty(struct workspace *ws)
1104{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001105 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106}
1107
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001108static struct workspace *
1109get_workspace(struct desktop_shell *shell, unsigned int index)
1110{
1111 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001112 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001113 pws += index;
1114 return *pws;
1115}
1116
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001117struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001118get_current_workspace(struct desktop_shell *shell)
1119{
1120 return get_workspace(shell, shell->workspaces.current);
1121}
1122
1123static void
1124activate_workspace(struct desktop_shell *shell, unsigned int index)
1125{
1126 struct workspace *ws;
1127
1128 ws = get_workspace(shell, index);
1129 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1130
1131 shell->workspaces.current = index;
1132}
1133
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001134static unsigned int
1135get_output_height(struct weston_output *output)
1136{
1137 return abs(output->region.extents.y1 - output->region.extents.y2);
1138}
1139
1140static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001141view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001142{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143 struct weston_transform *transform;
1144
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001145 if (is_focus_view(view)) {
1146 struct focus_surface *fsurf = get_focus_surface(view->surface);
1147 transform = &fsurf->workspace_transform;
1148 } else {
1149 struct shell_surface *shsurf = get_shell_surface(view->surface);
1150 transform = &shsurf->workspace_transform;
1151 }
1152
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001153 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001155 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001156
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001157 weston_matrix_init(&transform->matrix);
1158 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001159 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001161}
1162
1163static void
1164workspace_translate_out(struct workspace *ws, double fraction)
1165{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001167 unsigned int height;
1168 double d;
1169
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001170 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001172 d = height * fraction;
1173
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001174 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001175 }
1176}
1177
1178static void
1179workspace_translate_in(struct workspace *ws, double fraction)
1180{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182 unsigned int height;
1183 double d;
1184
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001185 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001187
1188 if (fraction > 0)
1189 d = -(height - height * fraction);
1190 else
1191 d = height + height * fraction;
1192
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001193 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001194 }
1195}
1196
1197static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001198reverse_workspace_change_animation(struct desktop_shell *shell,
1199 unsigned int index,
1200 struct workspace *from,
1201 struct workspace *to)
1202{
1203 shell->workspaces.current = index;
1204
1205 shell->workspaces.anim_to = to;
1206 shell->workspaces.anim_from = from;
1207 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1208 shell->workspaces.anim_timestamp = 0;
1209
Scott Moreau4272e632012-08-13 09:58:41 -06001210 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001211}
1212
1213static void
1214workspace_deactivate_transforms(struct workspace *ws)
1215{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001217 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001218
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001219 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001220 if (is_focus_view(view)) {
1221 struct focus_surface *fsurf = get_focus_surface(view->surface);
1222 transform = &fsurf->workspace_transform;
1223 } else {
1224 struct shell_surface *shsurf = get_shell_surface(view->surface);
1225 transform = &shsurf->workspace_transform;
1226 }
1227
1228 if (!wl_list_empty(&transform->link)) {
1229 wl_list_remove(&transform->link);
1230 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001231 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001233 }
1234}
1235
1236static void
1237finish_workspace_change_animation(struct desktop_shell *shell,
1238 struct workspace *from,
1239 struct workspace *to)
1240{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001241 struct weston_view *view;
1242
Scott Moreau4272e632012-08-13 09:58:41 -06001243 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001244
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001245 /* Views that extend past the bottom of the output are still
1246 * visible after the workspace animation ends but before its layer
1247 * is hidden. In that case, we need to damage below those views so
1248 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001249 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001250 weston_view_damage_below(view);
1251
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001252 wl_list_remove(&shell->workspaces.animation.link);
1253 workspace_deactivate_transforms(from);
1254 workspace_deactivate_transforms(to);
1255 shell->workspaces.anim_to = NULL;
1256
1257 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1258}
1259
1260static void
1261animate_workspace_change_frame(struct weston_animation *animation,
1262 struct weston_output *output, uint32_t msecs)
1263{
1264 struct desktop_shell *shell =
1265 container_of(animation, struct desktop_shell,
1266 workspaces.animation);
1267 struct workspace *from = shell->workspaces.anim_from;
1268 struct workspace *to = shell->workspaces.anim_to;
1269 uint32_t t;
1270 double x, y;
1271
1272 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1273 finish_workspace_change_animation(shell, from, to);
1274 return;
1275 }
1276
1277 if (shell->workspaces.anim_timestamp == 0) {
1278 if (shell->workspaces.anim_current == 0.0)
1279 shell->workspaces.anim_timestamp = msecs;
1280 else
1281 shell->workspaces.anim_timestamp =
1282 msecs -
1283 /* Invers of movement function 'y' below. */
1284 (asin(1.0 - shell->workspaces.anim_current) *
1285 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1286 M_2_PI);
1287 }
1288
1289 t = msecs - shell->workspaces.anim_timestamp;
1290
1291 /*
1292 * x = [0, π/2]
1293 * y(x) = sin(x)
1294 */
1295 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1296 y = sin(x);
1297
1298 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001299 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001300
1301 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1302 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1303 shell->workspaces.anim_current = y;
1304
Scott Moreau4272e632012-08-13 09:58:41 -06001305 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001306 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001307 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001308 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001309}
1310
1311static void
1312animate_workspace_change(struct desktop_shell *shell,
1313 unsigned int index,
1314 struct workspace *from,
1315 struct workspace *to)
1316{
1317 struct weston_output *output;
1318
1319 int dir;
1320
1321 if (index > shell->workspaces.current)
1322 dir = -1;
1323 else
1324 dir = 1;
1325
1326 shell->workspaces.current = index;
1327
1328 shell->workspaces.anim_dir = dir;
1329 shell->workspaces.anim_from = from;
1330 shell->workspaces.anim_to = to;
1331 shell->workspaces.anim_current = 0.0;
1332 shell->workspaces.anim_timestamp = 0;
1333
1334 output = container_of(shell->compositor->output_list.next,
1335 struct weston_output, link);
1336 wl_list_insert(&output->animation_list,
1337 &shell->workspaces.animation.link);
1338
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001339 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001340
1341 workspace_translate_in(to, 0);
1342
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001343 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001344
Scott Moreau4272e632012-08-13 09:58:41 -06001345 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001346}
1347
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001348static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001349update_workspace(struct desktop_shell *shell, unsigned int index,
1350 struct workspace *from, struct workspace *to)
1351{
1352 shell->workspaces.current = index;
1353 wl_list_insert(&from->layer.link, &to->layer.link);
1354 wl_list_remove(&from->layer.link);
1355}
1356
1357static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001358change_workspace(struct desktop_shell *shell, unsigned int index)
1359{
1360 struct workspace *from;
1361 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001362 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001363
1364 if (index == shell->workspaces.current)
1365 return;
1366
1367 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001368 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001369 return;
1370
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001371 from = get_current_workspace(shell);
1372 to = get_workspace(shell, index);
1373
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001374 if (shell->workspaces.anim_from == to &&
1375 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001376 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001377 reverse_workspace_change_animation(shell, index, from, to);
1378 return;
1379 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001380
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001381 if (shell->workspaces.anim_to != NULL)
1382 finish_workspace_change_animation(shell,
1383 shell->workspaces.anim_from,
1384 shell->workspaces.anim_to);
1385
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001386 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001387
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001388 if (shell->focus_animation_type != ANIMATION_NONE) {
1389 wl_list_for_each(state, &from->focus_list, link)
1390 if (state->keyboard_focus)
1391 animate_focus_change(shell, from,
1392 get_default_view(state->keyboard_focus), NULL);
1393
1394 wl_list_for_each(state, &to->focus_list, link)
1395 if (state->keyboard_focus)
1396 animate_focus_change(shell, to,
1397 NULL, get_default_view(state->keyboard_focus));
1398 }
1399
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001400 if (workspace_is_empty(to) && workspace_is_empty(from))
1401 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001402 else
1403 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001404}
1405
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001406static bool
1407workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1408{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001409 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001410 struct wl_list *e;
1411
1412 if (wl_list_empty(list))
1413 return false;
1414
1415 e = list->next;
1416
1417 if (e->next != list)
1418 return false;
1419
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001420 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001421}
1422
1423static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001424surface_keyboard_focus_lost(struct weston_surface *surface)
1425{
1426 struct weston_compositor *compositor = surface->compositor;
1427 struct weston_seat *seat;
1428 struct weston_surface *focus;
1429
1430 wl_list_for_each(seat, &compositor->seat_list, link) {
1431 struct weston_keyboard *keyboard =
1432 weston_seat_get_keyboard(seat);
1433
1434 if (!keyboard)
1435 continue;
1436
1437 focus = weston_surface_get_main_surface(keyboard->focus);
1438 if (focus == surface)
1439 weston_keyboard_set_focus(keyboard, NULL);
1440 }
1441}
1442
1443static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001444take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001445 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001446 unsigned int index)
1447{
Derek Foreman1281a362015-07-31 16:55:32 -05001448 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001449 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001451 struct shell_surface *shsurf;
1452 struct workspace *from;
1453 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001454 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001455
Derek Foreman1281a362015-07-31 16:55:32 -05001456 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 view = get_default_view(surface);
1458 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001459 index == shell->workspaces.current ||
1460 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001461 return;
1462
1463 from = get_current_workspace(shell);
1464 to = get_workspace(shell, index);
1465
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001466 weston_layer_entry_remove(&view->layer_link);
1467 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001468
Philip Withnall659163d2013-11-25 18:01:36 +00001469 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001470 if (shsurf != NULL)
1471 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001472
Jonas Ådahle9d22502012-08-29 22:13:01 +02001473 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001474 drop_focus_state(shell, from, surface);
1475
1476 if (shell->workspaces.anim_from == to &&
1477 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001478 wl_list_remove(&to->layer.link);
1479 wl_list_insert(from->layer.link.prev, &to->layer.link);
1480
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001481 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001482
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001483 return;
1484 }
1485
1486 if (shell->workspaces.anim_to != NULL)
1487 finish_workspace_change_animation(shell,
1488 shell->workspaces.anim_from,
1489 shell->workspaces.anim_to);
1490
1491 if (workspace_is_empty(from) &&
1492 workspace_has_only(to, surface))
1493 update_workspace(shell, index, from, to);
1494 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001495 if (shsurf != NULL &&
1496 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001497 wl_list_insert(&shell->workspaces.anim_sticky_list,
1498 &shsurf->workspace_transform.link);
1499
1500 animate_workspace_change(shell, index, from, to);
1501 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001502
Jonas Ådahl8538b222012-08-29 22:13:03 +02001503 state = ensure_focus_state(shell, seat);
1504 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001505 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001506}
1507
1508static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001509touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001510 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001511{
1512}
1513
1514static void
1515touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1516{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001517 struct weston_touch_move_grab *move =
1518 (struct weston_touch_move_grab *) container_of(
1519 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001520
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001521 if (touch_id == 0)
1522 move->active = 0;
1523
Jonas Ådahl9484b692013-12-02 22:05:03 +01001524 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001525 shell_touch_grab_end(&move->base);
1526 free(move);
1527 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001528}
1529
1530static void
1531touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001532 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001533{
1534 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1535 struct shell_surface *shsurf = move->base.shsurf;
1536 struct weston_surface *es;
1537 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1538 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1539
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001540 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001541 return;
1542
1543 es = shsurf->surface;
1544
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001545 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001546
1547 weston_compositor_schedule_repaint(es->compositor);
1548}
1549
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001550static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001551touch_move_grab_frame(struct weston_touch_grab *grab)
1552{
1553}
1554
1555static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001556touch_move_grab_cancel(struct weston_touch_grab *grab)
1557{
1558 struct weston_touch_move_grab *move =
1559 (struct weston_touch_move_grab *) container_of(
1560 grab, struct shell_touch_grab, grab);
1561
1562 shell_touch_grab_end(&move->base);
1563 free(move);
1564}
1565
Rusty Lynch1084da52013-08-15 09:10:08 -07001566static const struct weston_touch_grab_interface touch_move_grab_interface = {
1567 touch_move_grab_down,
1568 touch_move_grab_up,
1569 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001570 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001571 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001572};
1573
1574static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001575surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001576{
1577 struct weston_touch_move_grab *move;
1578
1579 if (!shsurf)
1580 return -1;
1581
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001582 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001583 return 0;
1584
1585 move = malloc(sizeof *move);
1586 if (!move)
1587 return -1;
1588
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001589 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001590 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001591 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001593 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001594
1595 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001596 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001597
1598 return 0;
1599}
1600
1601static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001602noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001603{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001604}
1605
1606static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001607noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001608 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001609{
1610}
1611
1612static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001613noop_grab_axis_source(struct weston_pointer_grab *grab,
1614 uint32_t source)
1615{
1616}
1617
1618static void
1619noop_grab_frame(struct weston_pointer_grab *grab)
1620{
1621}
1622
1623static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001624constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1625{
1626 struct shell_surface *shsurf = move->base.shsurf;
1627 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001628 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001629 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001630 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001631
1632 x = wl_fixed_to_int(pointer->x + move->dx);
1633 y = wl_fixed_to_int(pointer->y + move->dy);
1634
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001635 if (shsurf->shell->panel_position ==
1636 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001637 get_output_work_area(shsurf->shell,
1638 shsurf->surface->output,
1639 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001640
Derek Foreman6feb0f92015-04-15 12:23:56 -05001641 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1642 if (bottom - safety < area.y)
1643 y = area.y + safety - shsurf->geometry.height
1644 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001645
1646 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001647 y + shsurf->geometry.y < area.y)
1648 y = area.y - shsurf->geometry.y;
1649
Jonny Lambd73c6942014-08-20 15:53:20 +02001650 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001651
1652 *cx = x;
1653 *cy = y;
1654}
1655
1656static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001657move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001658 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001659{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001660 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001661 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001662 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001663 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001664
Jonas Ådahld2510102014-10-05 21:39:14 +02001665 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001666 if (!shsurf)
1667 return;
1668
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001669 constrain_position(move, &cx, &cy);
1670
1671 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001672
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001674}
1675
1676static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001677move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001678 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001679{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001680 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1681 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001682 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001683 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001684
Daniel Stone4dbadb12012-05-30 16:31:51 +01001685 if (pointer->button_count == 0 &&
1686 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001687 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001688 free(grab);
1689 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001690}
1691
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001692static void
1693move_grab_cancel(struct weston_pointer_grab *grab)
1694{
1695 struct shell_grab *shell_grab =
1696 container_of(grab, struct shell_grab, grab);
1697
1698 shell_grab_end(shell_grab);
1699 free(grab);
1700}
1701
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001702static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001703 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001704 move_grab_motion,
1705 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001706 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001707 noop_grab_axis_source,
1708 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001709 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001710};
1711
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001712static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001713surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001714 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715{
1716 struct weston_move_grab *move;
1717
1718 if (!shsurf)
1719 return -1;
1720
Derek Foreman45a7c272015-09-11 14:27:40 -05001721 shsurf = find_toplevel_surface(shsurf);
1722
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001723 if (shsurf->grabbed ||
1724 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001725 return 0;
1726
1727 move = malloc(sizeof *move);
1728 if (!move)
1729 return -1;
1730
Jason Ekstranda7af7042013-10-12 22:38:11 -05001731 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001732 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001733 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001734 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001735 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001736
1737 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001738 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001739
1740 return 0;
1741}
1742
1743static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001744common_surface_move(struct wl_resource *resource,
1745 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001746{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001747 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001748 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1749 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001750 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001751 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001752
Derek Foreman1281a362015-07-31 16:55:32 -05001753 if (pointer &&
1754 pointer->focus &&
1755 pointer->button_count > 0 &&
1756 pointer->grab_serial == serial) {
1757 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001758 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001759 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001760 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001761 } else if (touch &&
1762 touch->focus &&
1763 touch->grab_serial == serial) {
1764 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001765 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001766 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001767 wl_resource_post_no_memory(resource);
1768 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001769}
1770
Rafael Antognollie2a34552013-12-03 15:35:45 -02001771static void
1772shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1773 struct wl_resource *seat_resource, uint32_t serial)
1774{
1775 common_surface_move(resource, seat_resource, serial);
1776}
1777
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001778struct weston_resize_grab {
1779 struct shell_grab base;
1780 uint32_t edges;
1781 int32_t width, height;
1782};
1783
1784static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001785resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001786 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001787{
1788 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001789 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001790 struct shell_surface *shsurf = resize->base.shsurf;
1791 int32_t width, height;
1792 wl_fixed_t from_x, from_y;
1793 wl_fixed_t to_x, to_y;
1794
Jonas Ådahld2510102014-10-05 21:39:14 +02001795 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001796
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001797 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001798 return;
1799
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800 weston_view_from_global_fixed(shsurf->view,
1801 pointer->grab_x, pointer->grab_y,
1802 &from_x, &from_y);
1803 weston_view_from_global_fixed(shsurf->view,
1804 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001805
1806 width = resize->width;
1807 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1808 width += wl_fixed_to_int(from_x - to_x);
1809 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1810 width += wl_fixed_to_int(to_x - from_x);
1811 }
1812
1813 height = resize->height;
1814 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1815 height += wl_fixed_to_int(from_y - to_y);
1816 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1817 height += wl_fixed_to_int(to_y - from_y);
1818 }
1819
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001820 if (width < 1)
1821 width = 1;
1822 if (height < 1)
1823 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001824 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001825}
1826
1827static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001828send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001829{
1830 struct shell_surface *shsurf = get_shell_surface(surface);
1831
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001832 assert(shsurf);
1833
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001834 if (shsurf->resource)
1835 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001836 shsurf->resize_edges,
1837 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001838}
1839
1840static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001841 send_configure,
1842 NULL
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001843};
1844
1845static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001846resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001847 uint32_t time, uint32_t button, uint32_t state_w)
1848{
1849 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001850 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001851 enum wl_pointer_button_state state = state_w;
1852
1853 if (pointer->button_count == 0 &&
1854 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1855 shell_grab_end(&resize->base);
1856 free(grab);
1857 }
1858}
1859
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001860static void
1861resize_grab_cancel(struct weston_pointer_grab *grab)
1862{
1863 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1864
1865 shell_grab_end(&resize->base);
1866 free(grab);
1867}
1868
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001869static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001870 noop_grab_focus,
1871 resize_grab_motion,
1872 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001873 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001874 noop_grab_axis_source,
1875 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001876 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001877};
1878
Giulio Camuffob8366642013-04-25 13:57:46 +03001879/*
1880 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001881 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001882static void
1883surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1884 int32_t *y, int32_t *w, int32_t *h) {
1885 pixman_region32_t region;
1886 pixman_box32_t *box;
1887 struct weston_subsurface *subsurface;
1888
1889 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001890 surface->width,
1891 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001892
1893 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1894 pixman_region32_union_rect(&region, &region,
1895 subsurface->position.x,
1896 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001897 subsurface->surface->width,
1898 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001899 }
1900
1901 box = pixman_region32_extents(&region);
1902 if (x)
1903 *x = box->x1;
1904 if (y)
1905 *y = box->y1;
1906 if (w)
1907 *w = box->x2 - box->x1;
1908 if (h)
1909 *h = box->y2 - box->y1;
1910
1911 pixman_region32_fini(&region);
1912}
1913
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001914static int
1915surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001916 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001917{
1918 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001919 const unsigned resize_topbottom =
1920 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1921 const unsigned resize_leftright =
1922 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1923 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001924
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001925 if (shsurf->grabbed ||
1926 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001927 return 0;
1928
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001929 /* Check for invalid edge combinations. */
1930 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1931 (edges & resize_topbottom) == resize_topbottom ||
1932 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001933 return 0;
1934
1935 resize = malloc(sizeof *resize);
1936 if (!resize)
1937 return -1;
1938
1939 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001940
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001941 resize->width = shsurf->geometry.width;
1942 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001943
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001944 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001945 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001946 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001947 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001948
1949 return 0;
1950}
1951
1952static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001953common_surface_resize(struct wl_resource *resource,
1954 struct wl_resource *seat_resource, uint32_t serial,
1955 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001956{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001957 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001958 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001959 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001960 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001961
Derek Foreman1281a362015-07-31 16:55:32 -05001962 if (!pointer ||
1963 pointer->button_count == 0 ||
1964 pointer->grab_serial != serial ||
1965 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001966 return;
1967
Derek Foreman1281a362015-07-31 16:55:32 -05001968 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001969 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001970 return;
1971
Derek Foreman1281a362015-07-31 16:55:32 -05001972 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001973 wl_resource_post_no_memory(resource);
1974}
1975
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001976static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001977shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1978 struct wl_resource *seat_resource, uint32_t serial,
1979 uint32_t edges)
1980{
1981 common_surface_resize(resource, seat_resource, serial, edges);
1982}
1983
1984static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001985busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001986{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001987 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001988 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001990 wl_fixed_t sx, sy;
1991
Jason Ekstranda7af7042013-10-12 22:38:11 -05001992 view = weston_compositor_pick_view(pointer->seat->compositor,
1993 pointer->x, pointer->y,
1994 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001995
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001996 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1997 shell_grab_end(grab);
1998 free(grab);
1999 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002000}
2001
2002static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002003busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02002004 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002005{
Jonas Ådahld2510102014-10-05 21:39:14 +02002006 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002007}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002008
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002009static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002010busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002011 uint32_t time, uint32_t button, uint32_t state)
2012{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002013 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002014 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002015 struct weston_pointer *pointer = grab->grab.pointer;
2016 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002017
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002018 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02002019 activate(shsurf->shell, shsurf->view, seat,
2020 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002021 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002022 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02002023 activate(shsurf->shell, shsurf->view, seat,
2024 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05002025 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002026 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002027}
2028
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002029static void
2030busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2031{
2032 struct shell_grab *grab = (struct shell_grab *) base;
2033
2034 shell_grab_end(grab);
2035 free(grab);
2036}
2037
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002038static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002039 busy_cursor_grab_focus,
2040 busy_cursor_grab_motion,
2041 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002042 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002043 noop_grab_axis_source,
2044 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002045 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002046};
2047
2048static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002049set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002050{
2051 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002052
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002053 if (pointer->grab->interface == &busy_cursor_grab_interface)
2054 return;
2055
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002056 grab = malloc(sizeof *grab);
2057 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002058 return;
2059
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002060 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002061 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002062 /* Mark the shsurf as ungrabbed so that button binding is able
2063 * to move it. */
2064 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002065}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002066
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002067static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002068end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002069{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002070 struct shell_grab *grab;
2071 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002072
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002073 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002074 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2075
2076 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002077 continue;
2078
Derek Foreman1281a362015-07-31 16:55:32 -05002079 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002080 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002081 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002082 wl_resource_get_client(grab->shsurf->resource) == client) {
2083 shell_grab_end(grab);
2084 free(grab);
2085 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002086 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002087}
2088
Scott Moreau9521d5e2012-04-19 13:06:17 -06002089static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002090handle_shell_client_destroy(struct wl_listener *listener, void *data);
2091
2092static int
2093xdg_ping_timeout_handler(void *data)
2094{
2095 struct shell_client *sc = data;
2096 struct weston_seat *seat;
2097 struct shell_surface *shsurf;
2098
2099 /* Client is not responding */
2100 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002101 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002102 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2103
2104 if (!pointer ||
2105 !pointer->focus ||
2106 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002107 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002108
Derek Foreman1281a362015-07-31 16:55:32 -05002109 shsurf = get_shell_surface(pointer->focus->surface);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002110 if (shsurf && shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002111 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002112 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002113 }
2114
2115 return 1;
2116}
2117
2118static void
2119handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2120{
2121 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002122 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002123 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002124 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002125
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002126 if (sc->unresponsive) {
2127 xdg_ping_timeout_handler(sc);
2128 return;
2129 }
2130
2131 sc->ping_serial = serial;
2132 loop = wl_display_get_event_loop(compositor->wl_display);
2133 if (sc->ping_timer == NULL)
2134 sc->ping_timer =
2135 wl_event_loop_add_timer(loop,
2136 xdg_ping_timeout_handler, sc);
2137 if (sc->ping_timer == NULL)
2138 return;
2139
2140 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2141
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002142 if (shell_surface_is_xdg_surface(shsurf) ||
2143 shell_surface_is_xdg_popup(shsurf))
2144 xdg_shell_send_ping(sc->resource, serial);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002145 else if (shell_surface_is_wl_shell_surface(shsurf)
2146 && shsurf->resource)
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002147 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002148}
2149
Scott Moreauff1db4a2012-04-17 19:06:18 -06002150static void
2151ping_handler(struct weston_surface *surface, uint32_t serial)
2152{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002153 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002154
2155 if (!shsurf)
2156 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002157 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002158 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002159 if (shsurf->surface == shsurf->shell->grab_surface)
2160 return;
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08002161 if (!shsurf->owner)
2162 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002163
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002164 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002165}
2166
2167static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002168handle_pointer_focus(struct wl_listener *listener, void *data)
2169{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002170 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002172 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002173 uint32_t serial;
2174
Jason Ekstranda7af7042013-10-12 22:38:11 -05002175 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002176 return;
2177
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002179 serial = wl_display_next_serial(compositor->wl_display);
2180 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002181}
2182
2183static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002184shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2185{
2186 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002187 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002188}
2189
2190static void
2191shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2192{
2193 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002194 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002195}
2196
2197static void
2198handle_keyboard_focus(struct wl_listener *listener, void *data)
2199{
2200 struct weston_keyboard *keyboard = data;
2201 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2202
2203 if (seat->focused_surface) {
2204 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2205 if (shsurf)
2206 shell_surface_lose_keyboard_focus(shsurf);
2207 }
2208
2209 seat->focused_surface = keyboard->focus;
2210
2211 if (seat->focused_surface) {
2212 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2213 if (shsurf)
2214 shell_surface_gain_keyboard_focus(shsurf);
2215 }
2216}
2217
2218static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002219shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002220{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002221 if (sc->ping_serial != serial)
2222 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002223
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002224 sc->unresponsive = 0;
2225 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002226
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002227 if (sc->ping_timer) {
2228 wl_event_source_remove(sc->ping_timer);
2229 sc->ping_timer = NULL;
2230 }
2231
2232}
2233
2234static void
2235shell_surface_pong(struct wl_client *client,
2236 struct wl_resource *resource, uint32_t serial)
2237{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002238 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2239 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002240
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002241 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002242}
2243
2244static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002245set_title(struct shell_surface *shsurf, const char *title)
2246{
2247 free(shsurf->title);
2248 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002249 shsurf->surface->timeline.force_refresh = 1;
2250}
2251
2252static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002253set_pid(struct shell_surface *shsurf, pid_t pid)
2254{
2255 /* We have no use for it */
2256}
2257
2258static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002259set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2260{
2261 shsurf->type = t;
2262 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002263}
2264
2265static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002266set_window_geometry(struct shell_surface *shsurf,
2267 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002268{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002269 shsurf->next_geometry.x = x;
2270 shsurf->next_geometry.y = y;
2271 shsurf->next_geometry.width = width;
2272 shsurf->next_geometry.height = height;
2273 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002274}
2275
2276static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002277shell_surface_set_title(struct wl_client *client,
2278 struct wl_resource *resource, const char *title)
2279{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002280 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002281
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002282 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002283}
2284
2285static void
2286shell_surface_set_class(struct wl_client *client,
2287 struct wl_resource *resource, const char *class)
2288{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002289 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002290
2291 free(shsurf->class);
2292 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002293 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002294}
2295
Alex Wu4539b082012-03-01 12:57:46 +08002296static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002297restore_output_mode(struct weston_output *output)
2298{
Armin Krezovićc77f2582016-06-23 11:59:32 +02002299 if (output && output->original_mode)
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002300 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002301}
2302
2303static void
2304restore_all_output_modes(struct weston_compositor *compositor)
2305{
2306 struct weston_output *output;
2307
2308 wl_list_for_each(output, &compositor->output_list, link)
2309 restore_output_mode(output);
2310}
2311
Philip Withnall07926d92013-11-25 18:01:40 +00002312/* The surface will be inserted into the list immediately after the link
2313 * returned by this function (i.e. will be stacked immediately above the
2314 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002315static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002316shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2317{
2318 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002319 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002320
2321 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002322 case SHELL_SURFACE_XWAYLAND:
2323 return &shsurf->shell->fullscreen_layer.view_list;
2324
2325 case SHELL_SURFACE_NONE:
2326 return NULL;
2327
Kristian Høgsbergead52422014-01-01 13:51:52 -08002328 case SHELL_SURFACE_POPUP:
2329 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002330 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002331 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002332 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002333 /* Move the surface to its parent layer so
2334 * that surfaces which are transient for
2335 * fullscreen surfaces don't get hidden by the
2336 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002337
2338 /* TODO: Handle a parent with multiple views */
2339 parent = get_default_view(shsurf->parent);
2340 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002341 return container_of(parent->layer_link.link.prev,
2342 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002343 }
Philip Withnall07926d92013-11-25 18:01:40 +00002344
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002345 /* Move the surface to a normal workspace layer so that surfaces
2346 * which were previously fullscreen or transient are no longer
2347 * rendered on top. */
2348 ws = get_current_workspace(shsurf->shell);
2349 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002350 }
2351
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002352 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002353}
2354
Philip Withnall648a4dd2013-11-25 18:01:44 +00002355static void
2356shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2357{
2358 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002359 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002360
2361 /* Move the child layers to the same workspace as shsurf. They will be
2362 * stacked above shsurf. */
2363 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002364 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002365 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002366 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002367 prev = container_of(shsurf->view->layer_link.link.prev,
2368 struct weston_layer_entry, link);
2369 weston_layer_entry_remove(&child->view->layer_link);
2370 weston_layer_entry_insert(prev,
2371 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002372 weston_view_geometry_dirty(child->view);
2373 weston_surface_damage(child->surface);
2374
2375 /* Recurse. We don’t expect this to recurse very far (if
2376 * at all) because that would imply we have transient
2377 * (or popup) children of transient surfaces, which
2378 * would be unusual. */
2379 shell_surface_update_child_surface_layers(child);
2380 }
2381 }
2382}
2383
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002384/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002385 * geometry to ensure the changes are redrawn.
2386 *
2387 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2388 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002389static void
2390shell_surface_update_layer(struct shell_surface *shsurf)
2391{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002392 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002393
2394 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2395
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002396 if (new_layer_link == NULL)
2397 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002398 if (new_layer_link == &shsurf->view->layer_link)
2399 return;
2400
2401 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002402 weston_layer_entry_remove(&shsurf->view->layer_link);
2403 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002404 weston_view_geometry_dirty(shsurf->view);
2405 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002406
2407 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002408}
2409
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002410static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002411shell_surface_set_parent(struct shell_surface *shsurf,
2412 struct weston_surface *parent)
2413{
2414 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002415
2416 wl_list_remove(&shsurf->children_link);
2417 wl_list_init(&shsurf->children_link);
2418
2419 /* Insert into the parent surface’s child list. */
2420 if (parent != NULL) {
2421 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2422 if (parent_shsurf != NULL)
2423 wl_list_insert(&parent_shsurf->children_list,
2424 &shsurf->children_link);
2425 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002426}
2427
2428static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002429shell_surface_set_output(struct shell_surface *shsurf,
2430 struct weston_output *output)
2431{
2432 struct weston_surface *es = shsurf->surface;
2433
2434 /* get the default output, if the client set it as NULL
2435 check whether the ouput is available */
2436 if (output)
2437 shsurf->output = output;
2438 else if (es->output)
2439 shsurf->output = es->output;
2440 else
2441 shsurf->output = get_default_output(es->compositor);
2442}
2443
2444static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002445surface_clear_next_states(struct shell_surface *shsurf)
2446{
2447 shsurf->next_state.maximized = false;
2448 shsurf->next_state.fullscreen = false;
2449
2450 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2451 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2452 shsurf->state_changed = true;
2453}
2454
2455static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002456set_toplevel(struct shell_surface *shsurf)
2457{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002458 shell_surface_set_parent(shsurf, NULL);
2459 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002460 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002461
2462 /* The layer_link is updated in set_surface_type(),
2463 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002464}
2465
2466static void
2467shell_surface_set_toplevel(struct wl_client *client,
2468 struct wl_resource *resource)
2469{
2470 struct shell_surface *surface = wl_resource_get_user_data(resource);
2471
2472 set_toplevel(surface);
2473}
2474
2475static void
2476set_transient(struct shell_surface *shsurf,
2477 struct weston_surface *parent, int x, int y, uint32_t flags)
2478{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002479 assert(parent != NULL);
2480
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002481 shell_surface_set_parent(shsurf, parent);
2482
2483 surface_clear_next_states(shsurf);
2484
Philip Withnallbecb77e2013-11-25 18:01:30 +00002485 shsurf->transient.x = x;
2486 shsurf->transient.y = y;
2487 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002488
Rafael Antognollied207b42013-12-03 15:35:43 -02002489 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002490 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002491 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002492
2493 /* The layer_link is updated in set_surface_type(),
2494 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002495}
2496
2497static void
2498shell_surface_set_transient(struct wl_client *client,
2499 struct wl_resource *resource,
2500 struct wl_resource *parent_resource,
2501 int x, int y, uint32_t flags)
2502{
2503 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2504 struct weston_surface *parent =
2505 wl_resource_get_user_data(parent_resource);
2506
2507 set_transient(shsurf, parent, x, y, flags);
2508}
2509
2510static void
2511set_fullscreen(struct shell_surface *shsurf,
2512 uint32_t method,
2513 uint32_t framerate,
2514 struct weston_output *output)
2515{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002516 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002517 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002518
2519 shsurf->fullscreen_output = shsurf->output;
2520 shsurf->fullscreen.type = method;
2521 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002522
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002523 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002524}
2525
2526static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002527weston_view_set_initial_position(struct weston_view *view,
2528 struct desktop_shell *shell);
2529
2530static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002531unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002532{
Philip Withnallf85fe842013-11-25 18:01:37 +00002533 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002534 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2535 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002536 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002537 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002538
Alex Wu4539b082012-03-01 12:57:46 +08002539 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2540 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002541
Alex Wu4539b082012-03-01 12:57:46 +08002542 wl_list_remove(&shsurf->fullscreen.transform.link);
2543 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002544
Jason Ekstranda7af7042013-10-12 22:38:11 -05002545 if (shsurf->fullscreen.black_view)
2546 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2547 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002548
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002549 if (shsurf->saved_position_valid)
2550 weston_view_set_position(shsurf->view,
2551 shsurf->saved_x, shsurf->saved_y);
2552 else
2553 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2554
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002555 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002556 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002557 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002558 shsurf->saved_rotation_valid = false;
2559 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002560
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002561 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002562}
2563
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002564static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002565shell_surface_set_fullscreen(struct wl_client *client,
2566 struct wl_resource *resource,
2567 uint32_t method,
2568 uint32_t framerate,
2569 struct wl_resource *output_resource)
2570{
2571 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2572 struct weston_output *output;
2573
Emmanuel Gil Peyrot28834b62016-05-10 03:22:43 +02002574 if (shsurf->fullscreen_output == shsurf->output &&
2575 shsurf->fullscreen.type == method &&
2576 shsurf->fullscreen.framerate == framerate) {
2577 send_configure_for_surface(shsurf);
2578 return;
2579 }
2580
Philip Withnallbecb77e2013-11-25 18:01:30 +00002581 if (output_resource)
2582 output = wl_resource_get_user_data(output_resource);
2583 else
2584 output = NULL;
2585
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002586 shell_surface_set_parent(shsurf, NULL);
2587
Rafael Antognolli03b16592013-12-03 15:35:42 -02002588 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002589 shsurf->next_state.fullscreen = true;
2590 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002591 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002592}
2593
2594static void
2595set_popup(struct shell_surface *shsurf,
2596 struct weston_surface *parent,
2597 struct weston_seat *seat,
2598 uint32_t serial,
2599 int32_t x,
2600 int32_t y)
2601{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002602 assert(parent != NULL);
2603
Philip Withnallbecb77e2013-11-25 18:01:30 +00002604 shsurf->popup.shseat = get_shell_seat(seat);
2605 shsurf->popup.serial = serial;
2606 shsurf->popup.x = x;
2607 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002608
Pekka Paalanenb5026542014-11-12 15:09:24 +02002609 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002610}
2611
2612static void
2613shell_surface_set_popup(struct wl_client *client,
2614 struct wl_resource *resource,
2615 struct wl_resource *seat_resource,
2616 uint32_t serial,
2617 struct wl_resource *parent_resource,
2618 int32_t x, int32_t y, uint32_t flags)
2619{
2620 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002621 struct weston_surface *parent =
2622 wl_resource_get_user_data(parent_resource);
2623
2624 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002625
Rafael Antognolli03b16592013-12-03 15:35:42 -02002626 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002627 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002628 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002629 wl_resource_get_user_data(seat_resource),
2630 serial, x, y);
2631}
2632
2633static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002634unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002635{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002636 /* undo all maximized things here */
2637 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002638
2639 if (shsurf->saved_position_valid)
2640 weston_view_set_position(shsurf->view,
2641 shsurf->saved_x, shsurf->saved_y);
2642 else
2643 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002644
2645 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002646 wl_list_insert(&shsurf->view->geometry.transformation_list,
2647 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002648 shsurf->saved_rotation_valid = false;
2649 }
2650
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002651 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002652}
2653
Philip Withnallbecb77e2013-11-25 18:01:30 +00002654static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002655set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002656{
2657 struct shell_surface *shsurf;
2658 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002659 struct weston_view *view;
2660
2661 view = get_default_view(surface);
2662 if (!view)
2663 return;
2664
2665 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2666
2667 shsurf = get_shell_surface(surface);
2668 current_ws = get_current_workspace(shsurf->shell);
2669
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002670 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002671 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002672
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002673 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002674 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002675
2676 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002677 weston_view_damage_below(view);
2678}
2679
2680static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002681shell_surface_set_maximized(struct wl_client *client,
2682 struct wl_resource *resource,
2683 struct wl_resource *output_resource)
2684{
2685 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2686 struct weston_output *output;
2687
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002688 surface_clear_next_states(shsurf);
2689 shsurf->next_state.maximized = true;
2690 shsurf->state_changed = true;
2691
Pekka Paalanenb5026542014-11-12 15:09:24 +02002692 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002693 shell_surface_set_parent(shsurf, NULL);
2694
Philip Withnallbecb77e2013-11-25 18:01:30 +00002695 if (output_resource)
2696 output = wl_resource_get_user_data(output_resource);
2697 else
2698 output = NULL;
2699
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002700 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002701
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002702 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002703}
2704
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002705/* This is only ever called from set_surface_type(), so there’s no need to
2706 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002707static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002708reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002709{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002710 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002711 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002712 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002713 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002714
Pekka Paalanen98262232011-12-01 10:42:22 +02002715 return 0;
2716}
2717
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002718static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002719set_full_output(struct shell_surface *shsurf)
2720{
2721 shsurf->saved_x = shsurf->view->geometry.x;
2722 shsurf->saved_y = shsurf->view->geometry.y;
2723 shsurf->saved_position_valid = true;
2724
2725 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2726 wl_list_remove(&shsurf->rotation.transform.link);
2727 wl_list_init(&shsurf->rotation.transform.link);
2728 weston_view_geometry_dirty(shsurf->view);
2729 shsurf->saved_rotation_valid = true;
2730 }
2731}
2732
2733static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002734set_surface_type(struct shell_surface *shsurf)
2735{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002736 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002737 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002738
Philip Withnallbecb77e2013-11-25 18:01:30 +00002739 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002740
Rafael Antognolli03b16592013-12-03 15:35:42 -02002741 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002742 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002743
2744 switch (shsurf->type) {
2745 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002746 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002747 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002748 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002749 weston_view_set_position(shsurf->view,
2750 pev->geometry.x + shsurf->transient.x,
2751 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002752 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002753 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002754
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002755 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756 weston_view_set_position(shsurf->view, shsurf->transient.x,
2757 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002758 break;
2759
Philip Withnall0f640e22013-11-25 18:01:31 +00002760 case SHELL_SURFACE_POPUP:
2761 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002762 default:
2763 break;
2764 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002765
2766 /* Update the surface’s layer. */
2767 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002768}
2769
Tiago Vignattibe143262012-04-16 17:31:41 +03002770static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002771shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002772{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002773 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002774}
2775
Pekka Paalanen8274d902014-08-06 19:36:51 +03002776static int
2777black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2778{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002779 struct weston_view *fs_view = surface->configure_private;
2780 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002781 int n;
2782 int rem;
2783 int ret;
2784
2785 n = snprintf(buf, len, "black background surface for ");
2786 if (n < 0)
2787 return n;
2788
2789 rem = (int)len - n;
2790 if (rem < 0)
2791 rem = 0;
2792
2793 if (fs_surface->get_label)
2794 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2795 else
2796 ret = snprintf(buf + n, rem, "<unknown>");
2797
2798 if (ret < 0)
2799 return n;
2800
2801 return n + ret;
2802}
2803
Alex Wu21858432012-04-01 20:13:08 +08002804static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002805black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002806
Jason Ekstranda7af7042013-10-12 22:38:11 -05002807static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002808create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002809 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002810 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002811{
2812 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002813 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002814
2815 surface = weston_surface_create(ec);
2816 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002817 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002818 return NULL;
2819 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820 view = weston_view_create(surface);
2821 if (surface == NULL) {
2822 weston_log("no memory\n");
2823 weston_surface_destroy(surface);
2824 return NULL;
2825 }
Alex Wu4539b082012-03-01 12:57:46 +08002826
Alex Wu21858432012-04-01 20:13:08 +08002827 surface->configure = black_surface_configure;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002828 surface->configure_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002829 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002830 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002831 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002832 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002833 pixman_region32_fini(&surface->input);
2834 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002835
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002836 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002837 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002838
2839 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002840}
2841
Philip Withnalled8f1a92013-11-25 18:01:43 +00002842static void
2843shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2844{
2845 struct weston_output *output = shsurf->fullscreen_output;
2846
Rafael Antognolli03b16592013-12-03 15:35:42 -02002847 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002848
2849 if (!shsurf->fullscreen.black_view)
2850 shsurf->fullscreen.black_view =
2851 create_black_surface(shsurf->surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002852 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002853 output->x, output->y,
2854 output->width,
2855 output->height);
2856
2857 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002858 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2859 weston_layer_entry_insert(&shsurf->view->layer_link,
2860 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002861 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2862 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002863
Armin Krezović4663aca2016-06-30 06:04:29 +02002864 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002865 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002866}
2867
Alex Wu4539b082012-03-01 12:57:46 +08002868/* Create black surface and append it to the associated fullscreen surface.
2869 * Handle size dismatch and positioning according to the method. */
2870static void
2871shell_configure_fullscreen(struct shell_surface *shsurf)
2872{
2873 struct weston_output *output = shsurf->fullscreen_output;
2874 struct weston_surface *surface = shsurf->surface;
2875 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002876 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002877 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002878
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002879 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2880 restore_output_mode(output);
2881
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002882 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002883 weston_layer_entry_remove(&shsurf->view->layer_link);
2884 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002885
Philip Withnalled8f1a92013-11-25 18:01:43 +00002886 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002887
Jason Ekstranda7af7042013-10-12 22:38:11 -05002888 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002889 &surf_width, &surf_height);
2890
Alex Wu4539b082012-03-01 12:57:46 +08002891 switch (shsurf->fullscreen.type) {
2892 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002893 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002894 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002895 break;
2896 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002897 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002898 if (output->width == surf_width &&
2899 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002900 weston_view_set_position(shsurf->view,
2901 output->x - surf_x,
2902 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002903 break;
2904 }
2905
Alex Wu4539b082012-03-01 12:57:46 +08002906 matrix = &shsurf->fullscreen.transform.matrix;
2907 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002908
Scott Moreau1bad5db2012-08-18 01:04:05 -06002909 output_aspect = (float) output->width /
2910 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002911 /* XXX: Use surf_width and surf_height here? */
2912 surface_aspect = (float) surface->width /
2913 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002914 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002915 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002916 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002917 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002918 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002919 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002920
Alex Wu4539b082012-03-01 12:57:46 +08002921 weston_matrix_scale(matrix, scale, scale, 1);
2922 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002923 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002924 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002925 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2926 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002927 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002928
Alex Wu4539b082012-03-01 12:57:46 +08002929 break;
2930 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002931 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002932 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002933 surf_width * surface->buffer_viewport.buffer.scale,
2934 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002935 shsurf->fullscreen.framerate};
2936
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002937 if (weston_output_mode_switch_to_temporary(output, &mode,
2938 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002939 weston_view_set_position(shsurf->view,
2940 output->x - surf_x,
2941 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002942 shsurf->fullscreen.black_view->surface->width = output->width;
2943 shsurf->fullscreen.black_view->surface->height = output->height;
2944 weston_view_set_position(shsurf->fullscreen.black_view,
2945 output->x - surf_x,
2946 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002947 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002948 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002949 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002950 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002951 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002952 }
Alex Wubd3354b2012-04-17 17:20:49 +08002953 }
Alex Wu4539b082012-03-01 12:57:46 +08002954 break;
2955 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002957 break;
2958 default:
2959 break;
2960 }
2961}
2962
Alex Wu4539b082012-03-01 12:57:46 +08002963static void
2964shell_map_fullscreen(struct shell_surface *shsurf)
2965{
Alex Wubd3354b2012-04-17 17:20:49 +08002966 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002967}
2968
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002969static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002970set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2971{
2972 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002973 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002974 shsurf->transient.x = x;
2975 shsurf->transient.y = y;
2976 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002977
2978 shell_surface_set_parent(shsurf, NULL);
2979
Pekka Paalanenb5026542014-11-12 15:09:24 +02002980 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002981 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002982}
2983
Kristian Høgsberg47792412014-05-04 13:47:06 -07002984static void
2985shell_interface_set_fullscreen(struct shell_surface *shsurf,
2986 uint32_t method,
2987 uint32_t framerate,
2988 struct weston_output *output)
2989{
2990 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002991 shsurf->next_state.fullscreen = true;
2992 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002993
2994 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002995}
2996
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002997static struct weston_output *
2998get_focused_output(struct weston_compositor *compositor)
2999{
3000 struct weston_seat *seat;
3001 struct weston_output *output = NULL;
3002
3003 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003004 struct weston_touch *touch = weston_seat_get_touch(seat);
3005 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3006 struct weston_keyboard *keyboard =
3007 weston_seat_get_keyboard(seat);
3008
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003009 /* Priority has touch focus, then pointer and
3010 * then keyboard focus. We should probably have
3011 * three for loops and check frist for touch,
3012 * then for pointer, etc. but unless somebody has some
3013 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05003014 if (touch && touch->focus)
3015 output = touch->focus->output;
3016 else if (pointer && pointer->focus)
3017 output = pointer->focus->output;
3018 else if (keyboard && keyboard->focus)
3019 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003020
3021 if (output)
3022 break;
3023 }
3024
3025 return output;
3026}
3027
3028static void
3029shell_interface_set_maximized(struct shell_surface *shsurf)
3030{
3031 struct weston_output *output;
3032
3033 surface_clear_next_states(shsurf);
3034 shsurf->next_state.maximized = true;
3035 shsurf->state_changed = true;
3036 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3037
3038 if (!weston_surface_is_mapped(shsurf->surface))
3039 output = get_focused_output(shsurf->surface->compositor);
3040 else
3041 output = shsurf->surface->output;
3042
3043 shell_surface_set_output(shsurf, output);
3044 send_configure_for_surface(shsurf);
3045}
3046
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003047static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003048shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003049{
Derek Foremane4d6c832015-08-05 14:48:11 -05003050 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003051}
3052
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003053static int
3054shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003055 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003056 uint32_t edges)
3057{
Derek Foremane4d6c832015-08-05 14:48:11 -05003058 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003059}
3060
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003061static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003062
3063static void
3064destroy_shell_seat(struct wl_listener *listener, void *data)
3065{
3066 struct shell_seat *shseat =
3067 container_of(listener,
3068 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003069 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003070
3071 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003072 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003073 shseat->popup_grab.client = NULL;
3074
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003075 wl_list_for_each_safe(shsurf, next,
3076 &shseat->popup_grab.surfaces_list,
3077 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003078 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003079 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003080 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003081 }
3082
3083 wl_list_remove(&shseat->seat_destroy_listener.link);
3084 free(shseat);
3085}
3086
Jason Ekstrand024177c2014-04-21 19:42:58 -05003087static void
3088shell_seat_caps_changed(struct wl_listener *listener, void *data)
3089{
Derek Foreman1281a362015-07-31 16:55:32 -05003090 struct weston_keyboard *keyboard;
3091 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003092 struct shell_seat *seat;
3093
3094 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003095 keyboard = weston_seat_get_keyboard(seat->seat);
3096 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003097
Derek Foreman1281a362015-07-31 16:55:32 -05003098 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003099 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003100 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003101 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003102 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003103 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003104 wl_list_init(&seat->keyboard_focus_listener.link);
3105 }
3106
Derek Foreman1281a362015-07-31 16:55:32 -05003107 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003108 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003109 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003110 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003111 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003112 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003113 wl_list_init(&seat->pointer_focus_listener.link);
3114 }
3115}
3116
Giulio Camuffo5085a752013-03-25 21:42:45 +01003117static struct shell_seat *
3118create_shell_seat(struct weston_seat *seat)
3119{
3120 struct shell_seat *shseat;
3121
3122 shseat = calloc(1, sizeof *shseat);
3123 if (!shseat) {
3124 weston_log("no memory to allocate shell seat\n");
3125 return NULL;
3126 }
3127
3128 shseat->seat = seat;
3129 wl_list_init(&shseat->popup_grab.surfaces_list);
3130
3131 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3132 wl_signal_add(&seat->destroy_signal,
3133 &shseat->seat_destroy_listener);
3134
Jason Ekstrand024177c2014-04-21 19:42:58 -05003135 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3136 wl_list_init(&shseat->keyboard_focus_listener.link);
3137
3138 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3139 wl_list_init(&shseat->pointer_focus_listener.link);
3140
3141 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3142 wl_signal_add(&seat->updated_caps_signal,
3143 &shseat->caps_changed_listener);
3144 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3145
Giulio Camuffo5085a752013-03-25 21:42:45 +01003146 return shseat;
3147}
3148
3149static struct shell_seat *
3150get_shell_seat(struct weston_seat *seat)
3151{
3152 struct wl_listener *listener;
3153
3154 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003155 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003156
3157 return container_of(listener,
3158 struct shell_seat, seat_destroy_listener);
3159}
3160
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003161static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003162popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003163{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003164 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003165 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003166 struct shell_seat *shseat =
3167 container_of(grab, struct shell_seat, popup_grab.grab);
3168 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003169 wl_fixed_t sx, sy;
3170
Jason Ekstranda7af7042013-10-12 22:38:11 -05003171 view = weston_compositor_pick_view(pointer->seat->compositor,
3172 pointer->x, pointer->y,
3173 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003174
Jason Ekstranda7af7042013-10-12 22:38:11 -05003175 if (view && view->surface->resource &&
3176 wl_resource_get_client(view->surface->resource) == client) {
3177 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003178 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003179 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003180 }
3181}
3182
3183static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003184popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003185 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003186{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003187 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003188 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003189 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003190 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003191 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003192
Jonas Ådahl61917c82014-08-11 22:44:02 +02003193 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003194 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003195 weston_view_from_global_fixed(pointer->focus, x, y,
3196 &pointer->sx, &pointer->sy);
3197 }
3198
Jonas Ådahld2510102014-10-05 21:39:14 +02003199 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003200
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003201 if (!pointer->focus_client)
3202 return;
3203
3204 resource_list = &pointer->focus_client->pointer_resources;
3205 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003206 weston_view_from_global_fixed(pointer->focus,
3207 pointer->x, pointer->y,
3208 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003209 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003210 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003211}
3212
3213static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003214popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003215 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003216{
3217 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003218 struct shell_seat *shseat =
3219 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003220 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003221 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003222 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003223 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003224
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003225 if (grab->pointer->focus_client)
3226 resource_list = &grab->pointer->focus_client->pointer_resources;
3227 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003228 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003229 wl_resource_for_each(resource, resource_list) {
3230 wl_pointer_send_button(resource, serial,
3231 time, button, state);
3232 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003233 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003234 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003235 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003236 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003237 }
3238
Daniel Stone4dbadb12012-05-30 16:31:51 +01003239 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003240 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003241}
3242
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003243static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003244popup_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003245 uint32_t time,
3246 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003247{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003248 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003249}
3250
3251static void
Peter Hutterer87743e92016-01-18 16:38:22 +10003252popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3253{
3254 weston_pointer_send_axis_source(grab->pointer, source);
3255}
3256
3257static void
3258popup_grab_frame(struct weston_pointer_grab *grab)
3259{
3260 weston_pointer_send_frame(grab->pointer);
3261}
3262
3263static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003264popup_grab_cancel(struct weston_pointer_grab *grab)
3265{
3266 popup_grab_end(grab->pointer);
3267}
3268
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003269static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003270 popup_grab_focus,
3271 popup_grab_motion,
3272 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003273 popup_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003274 popup_grab_axis_source,
3275 popup_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003276 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003277};
3278
3279static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003280touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003281 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003282{
3283 struct wl_resource *resource;
3284 struct shell_seat *shseat =
3285 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3286 struct wl_display *display = shseat->seat->compositor->wl_display;
3287 uint32_t serial;
3288 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003289 wl_fixed_t sx, sy;
3290
3291 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003292
3293 resource_list = &grab->touch->focus_resource_list;
3294 if (!wl_list_empty(resource_list)) {
3295 serial = wl_display_get_serial(display);
3296 wl_resource_for_each(resource, resource_list) {
3297 wl_touch_send_down(resource, serial, time,
3298 grab->touch->focus->surface->resource,
3299 touch_id, sx, sy);
3300 }
3301 }
3302}
3303
3304static void
3305touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3306{
3307 struct wl_resource *resource;
3308 struct shell_seat *shseat =
3309 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3310 struct wl_display *display = shseat->seat->compositor->wl_display;
3311 uint32_t serial;
3312 struct wl_list *resource_list;
3313
3314 resource_list = &grab->touch->focus_resource_list;
3315 if (!wl_list_empty(resource_list)) {
3316 serial = wl_display_get_serial(display);
3317 wl_resource_for_each(resource, resource_list) {
3318 wl_touch_send_up(resource, serial, time, touch_id);
3319 }
3320 }
3321}
3322
3323static void
3324touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003325 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003326{
3327 struct wl_resource *resource;
3328 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003329 wl_fixed_t sx, sy;
3330
3331 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003332
3333 resource_list = &grab->touch->focus_resource_list;
3334 if (!wl_list_empty(resource_list)) {
3335 wl_resource_for_each(resource, resource_list) {
3336 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3337 }
3338 }
3339}
3340
3341static void
3342touch_popup_grab_frame(struct weston_touch_grab *grab)
3343{
3344}
3345
3346static void
3347touch_popup_grab_cancel(struct weston_touch_grab *grab)
3348{
3349 touch_popup_grab_end(grab->touch);
3350}
3351
3352static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3353 touch_popup_grab_down,
3354 touch_popup_grab_up,
3355 touch_popup_grab_motion,
3356 touch_popup_grab_frame,
3357 touch_popup_grab_cancel,
3358};
3359
3360static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003361shell_surface_send_popup_done(struct shell_surface *shsurf)
3362{
Bryce Harrington22b1f932015-09-23 17:30:43 -07003363 if (shsurf->resource == NULL)
3364 return;
3365 else if (shell_surface_is_wl_shell_surface(shsurf))
Rafael Antognollie2a34552013-12-03 15:35:45 -02003366 wl_shell_surface_send_popup_done(shsurf->resource);
3367 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003368 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003369}
3370
3371static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003372popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003373{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003374 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003375 struct shell_seat *shseat =
3376 container_of(grab, struct shell_seat, popup_grab.grab);
3377 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003378 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003379
3380 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003381 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003382 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003383 shseat->popup_grab.grab.interface = NULL;
3384 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003385 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003386 wl_list_for_each_safe(shsurf, next,
3387 &shseat->popup_grab.surfaces_list,
3388 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003389 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003390 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003391 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003392 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003393 wl_list_init(&shseat->popup_grab.surfaces_list);
3394 }
3395}
3396
3397static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003398touch_popup_grab_end(struct weston_touch *touch)
3399{
3400 struct weston_touch_grab *grab = touch->grab;
3401 struct shell_seat *shseat =
3402 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3403 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003404 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003405
3406 if (touch->grab->interface == &touch_popup_grab_interface) {
3407 weston_touch_end_grab(grab->touch);
3408 shseat->popup_grab.client = NULL;
3409 shseat->popup_grab.touch_grab.interface = NULL;
3410 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3411 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003412 wl_list_for_each_safe(shsurf, next,
3413 &shseat->popup_grab.surfaces_list,
3414 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003415 shell_surface_send_popup_done(shsurf);
3416 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003417 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003418 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003419 wl_list_init(&shseat->popup_grab.surfaces_list);
3420 }
3421}
3422
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003423static struct shell_surface *
3424get_top_popup(struct shell_seat *shseat)
3425{
3426 struct shell_surface *shsurf;
3427
3428 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3429 return NULL;
3430 } else {
3431 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3432 struct shell_surface,
3433 popup.grab_link);
3434 return shsurf;
3435 }
3436}
3437
3438static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003439add_popup_grab(struct shell_surface *shsurf,
3440 struct shell_seat *shseat,
3441 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003442{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003443 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003444 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003445 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3446 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003447
3448 parent = get_shell_surface(shsurf->parent);
3449 top_surface = get_top_popup(shseat);
3450 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003451 (!parent ||
3452 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003453 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003454 wl_resource_post_error(shsurf->owner_resource,
3455 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003456 "xdg_popup was not created on the "
3457 "topmost popup");
3458 return -1;
3459 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003460
3461 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003462 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003463 shseat->popup_grab.client =
3464 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003465
3466 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003467 shseat->popup_grab.grab.interface =
3468 &popup_grab_interface;
3469
3470 /* We must make sure here that this popup was opened
3471 * after a mouse press, and not just by moving around
3472 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003473 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003474 shseat->popup_grab.initial_up = 0;
3475 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003476 shseat->popup_grab.touch_grab.interface =
3477 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003478 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003479
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003480 wl_list_insert(&shseat->popup_grab.surfaces_list,
3481 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003482
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003483 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003484 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003485 &shseat->popup_grab.grab);
3486 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003487 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003488 &shseat->popup_grab.touch_grab);
3489 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003490 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003491 wl_list_insert(&shseat->popup_grab.surfaces_list,
3492 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003493 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003494
3495 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003496}
3497
3498static void
3499remove_popup_grab(struct shell_surface *shsurf)
3500{
3501 struct shell_seat *shseat = shsurf->popup.shseat;
3502
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003503 if (shell_surface_is_xdg_popup(shsurf) &&
3504 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003505 wl_resource_post_error(shsurf->owner_resource,
3506 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003507 "xdg_popup was destroyed while it was "
3508 "not the topmost popup.");
3509 return;
3510 }
3511
Giulio Camuffo5085a752013-03-25 21:42:45 +01003512 wl_list_remove(&shsurf->popup.grab_link);
3513 wl_list_init(&shsurf->popup.grab_link);
3514 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003515 if (shseat->popup_grab.type == POINTER) {
3516 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3517 shseat->popup_grab.grab.interface = NULL;
3518 } else if (shseat->popup_grab.type == TOUCH) {
3519 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3520 shseat->popup_grab.touch_grab.interface = NULL;
3521 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003522 }
3523}
3524
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003525static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003526shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003527{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003528 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003529 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003530 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3531 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003532
Jason Ekstranda7af7042013-10-12 22:38:11 -05003533 shsurf->surface->output = parent_view->output;
Armin Krezović4663aca2016-06-30 06:04:29 +02003534 shsurf->surface->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003535 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003536
Jason Ekstranda7af7042013-10-12 22:38:11 -05003537 weston_view_set_transform_parent(shsurf->view, parent_view);
3538 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3539 weston_view_update_transform(shsurf->view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003540 shsurf->view->is_mapped = true;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003541
Derek Foreman1281a362015-07-31 16:55:32 -05003542 if (pointer &&
3543 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003544 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3545 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003546 } else if (touch &&
3547 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003548 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3549 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003550 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003551 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003552 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003553 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003554
3555 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003556}
3557
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003558static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003559 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003560 shell_surface_move,
3561 shell_surface_resize,
3562 shell_surface_set_toplevel,
3563 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003564 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003565 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003566 shell_surface_set_maximized,
3567 shell_surface_set_title,
3568 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003569};
3570
3571static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003572destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003573{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003574 struct shell_surface *child, *next;
3575
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003576 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3577
Giulio Camuffo5085a752013-03-25 21:42:45 +01003578 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3579 remove_popup_grab(shsurf);
3580 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003581
Alex Wubd3354b2012-04-17 17:20:49 +08003582 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003583 shell_surface_is_top_fullscreen(shsurf))
3584 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003585
Jason Ekstranda7af7042013-10-12 22:38:11 -05003586 if (shsurf->fullscreen.black_view)
3587 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003588
Alex Wubd3354b2012-04-17 17:20:49 +08003589 /* As destroy_resource() use wl_list_for_each_safe(),
3590 * we can always remove the listener.
3591 */
3592 wl_list_remove(&shsurf->surface_destroy_listener.link);
3593 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003594 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003595 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003596
Jason Ekstranda7af7042013-10-12 22:38:11 -05003597 weston_view_destroy(shsurf->view);
3598
Philip Withnall648a4dd2013-11-25 18:01:44 +00003599 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003600 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3601 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003602
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003603 wl_list_remove(&shsurf->link);
3604 free(shsurf);
3605}
3606
3607static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003608shell_destroy_shell_surface(struct wl_resource *resource)
3609{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003610 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003611
Bryan Caina46b9462014-04-04 17:41:24 -05003612 if (!wl_list_empty(&shsurf->popup.grab_link))
3613 remove_popup_grab(shsurf);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003614 if (shsurf->resource)
3615 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003616 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003617}
3618
3619static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003620shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003621{
3622 struct shell_surface *shsurf = container_of(listener,
3623 struct shell_surface,
3624 surface_destroy_listener);
3625
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003626 if (shsurf->resource)
3627 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003628
3629 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003630}
3631
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003632static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003633fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003634{
3635 struct shell_surface *shsurf = data;
3636
3637 weston_surface_destroy(shsurf->surface);
3638}
3639
3640static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003641fade_out_done(struct weston_view_animation *animation, void *data)
3642{
3643 struct shell_surface *shsurf = data;
3644 struct wl_event_loop *loop;
3645
3646 loop = wl_display_get_event_loop(
3647 shsurf->surface->compositor->wl_display);
3648
3649 if (!shsurf->destroying) {
3650 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3651 shsurf->destroying = true;
3652 }
3653}
3654
3655static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003656handle_resource_destroy(struct wl_listener *listener, void *data)
3657{
3658 struct shell_surface *shsurf =
3659 container_of(listener, struct shell_surface,
3660 resource_destroy_listener);
3661
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003662 if (!weston_surface_is_mapped(shsurf->surface))
3663 return;
3664
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003665 shsurf->surface->ref_count++;
3666
3667 pixman_region32_fini(&shsurf->surface->pending.input);
3668 pixman_region32_init(&shsurf->surface->pending.input);
3669 pixman_region32_fini(&shsurf->surface->input);
3670 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003671 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3672 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3673 fade_out_done, shsurf);
3674 } else {
3675 weston_surface_destroy(shsurf->surface);
3676 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003677}
3678
3679static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003680shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003681
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003682struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003683get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003684{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003685 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003686 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003687 else
3688 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003689}
3690
Rafael Antognollie2a34552013-12-03 15:35:45 -02003691static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003692create_common_surface(struct shell_client *owner, void *shell,
3693 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003694 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003695{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003696 struct shell_surface *shsurf;
3697
Pekka Paalanen50b67472014-10-01 15:02:41 +03003698 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003699
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003700 shsurf = calloc(1, sizeof *shsurf);
3701 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003702 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003703 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003704 }
3705
Jason Ekstranda7af7042013-10-12 22:38:11 -05003706 shsurf->view = weston_view_create(surface);
3707 if (!shsurf->view) {
3708 weston_log("no memory to allocate shell surface\n");
3709 free(shsurf);
3710 return NULL;
3711 }
3712
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003713 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003714 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003715 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003716
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003717 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3718 wl_resource_add_destroy_listener(surface->resource,
3719 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003720 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003721
Tiago Vignattibc052c92012-04-19 16:18:18 +03003722 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003723 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003724 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003725 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003726 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003727 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3728 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003729 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003730 wl_list_init(&shsurf->fullscreen.transform.link);
3731
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003732 shsurf->output = get_default_output(shsurf->shell->compositor);
3733
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003734 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003735 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003736 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003737 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003738
3739 /* init link so its safe to always remove it in destroy_shell_surface */
3740 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003741 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003742
Pekka Paalanen460099f2012-01-20 16:48:25 +02003743 /* empty when not in use */
3744 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003745 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003746
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003747 wl_list_init(&shsurf->workspace_transform.link);
3748
Philip Withnall648a4dd2013-11-25 18:01:44 +00003749 wl_list_init(&shsurf->children_link);
3750 wl_list_init(&shsurf->children_list);
3751 shsurf->parent = NULL;
3752
Pekka Paalanenb5026542014-11-12 15:09:24 +02003753 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003754
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003755 shsurf->client = client;
3756
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003757 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003758}
3759
Rafael Antognollie2a34552013-12-03 15:35:45 -02003760static struct shell_surface *
3761create_shell_surface(void *shell, struct weston_surface *surface,
3762 const struct weston_shell_client *client)
3763{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003764 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003765}
3766
Tiago Vignattibc052c92012-04-19 16:18:18 +03003767static void
3768shell_get_shell_surface(struct wl_client *client,
3769 struct wl_resource *resource,
3770 uint32_t id,
3771 struct wl_resource *surface_resource)
3772{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003773 struct weston_surface *surface =
3774 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003775 struct shell_client *sc = wl_resource_get_user_data(resource);
3776 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003777 struct shell_surface *shsurf;
3778
Pekka Paalanen50b67472014-10-01 15:02:41 +03003779 if (weston_surface_set_role(surface, "wl_shell_surface",
3780 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003781 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003782
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003783 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003784 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003785 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003786 return;
3787 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003788
Jason Ekstranda85118c2013-06-27 20:17:02 -05003789 shsurf->resource =
3790 wl_resource_create(client,
3791 &wl_shell_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003792 if (!shsurf->resource) {
3793 wl_resource_post_no_memory(surface_resource);
3794 return;
3795 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003796 wl_resource_set_implementation(shsurf->resource,
3797 &shell_surface_implementation,
3798 shsurf, shell_destroy_shell_surface);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08003799 wl_list_insert(&sc->surface_list,
3800 wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003801}
3802
Rafael Antognollie2a34552013-12-03 15:35:45 -02003803static bool
3804shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3805{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003806 /* A shell surface without a resource is created from xwayland
3807 * and is considered a wl_shell surface for now. */
3808
3809 return shsurf->resource == NULL ||
3810 wl_resource_instance_of(shsurf->resource,
3811 &wl_shell_surface_interface,
3812 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003813}
3814
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003815static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003816 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003817};
3818
Rafael Antognollie2a34552013-12-03 15:35:45 -02003819/****************************
3820 * xdg-shell implementation */
3821
3822static void
3823xdg_surface_destroy(struct wl_client *client,
3824 struct wl_resource *resource)
3825{
3826 wl_resource_destroy(resource);
3827}
3828
3829static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003830xdg_surface_set_parent(struct wl_client *client,
3831 struct wl_resource *resource,
3832 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003833{
3834 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003835 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003836
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003837 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003838 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003839 shell_surface_set_parent(shsurf, parent->surface);
3840 } else {
3841 shell_surface_set_parent(shsurf, NULL);
3842 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003843}
3844
3845static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003846xdg_surface_set_app_id(struct wl_client *client,
3847 struct wl_resource *resource,
3848 const char *app_id)
3849{
3850 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3851
3852 free(shsurf->class);
3853 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003854 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003855}
3856
3857static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003858xdg_surface_show_window_menu(struct wl_client *client,
3859 struct wl_resource *surface_resource,
3860 struct wl_resource *seat_resource,
3861 uint32_t serial,
3862 int32_t x,
3863 int32_t y)
3864{
3865 /* TODO */
3866}
3867
3868static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003869xdg_surface_set_title(struct wl_client *client,
3870 struct wl_resource *resource, const char *title)
3871{
3872 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3873
3874 set_title(shsurf, title);
3875}
3876
3877static void
3878xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3879 struct wl_resource *seat_resource, uint32_t serial)
3880{
3881 common_surface_move(resource, seat_resource, serial);
3882}
3883
3884static void
3885xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3886 struct wl_resource *seat_resource, uint32_t serial,
3887 uint32_t edges)
3888{
3889 common_surface_resize(resource, seat_resource, serial, edges);
3890}
3891
3892static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003893xdg_surface_ack_configure(struct wl_client *client,
3894 struct wl_resource *resource,
3895 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003896{
3897 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3898
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003899 if (shsurf->state_requested) {
3900 shsurf->next_state = shsurf->requested_state;
3901 shsurf->state_changed = true;
3902 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003903 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003904}
3905
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003906static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003907xdg_surface_set_window_geometry(struct wl_client *client,
3908 struct wl_resource *resource,
3909 int32_t x,
3910 int32_t y,
3911 int32_t width,
3912 int32_t height)
3913{
3914 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3915
3916 set_window_geometry(shsurf, x, y, width, height);
3917}
3918
3919static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003920xdg_surface_set_maximized(struct wl_client *client,
3921 struct wl_resource *resource)
3922{
3923 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003924 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003925
3926 shsurf->state_requested = true;
3927 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003928
3929 if (!weston_surface_is_mapped(shsurf->surface))
3930 output = get_focused_output(shsurf->surface->compositor);
3931 else
3932 output = shsurf->surface->output;
3933
3934 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003935 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003936}
3937
3938static void
3939xdg_surface_unset_maximized(struct wl_client *client,
3940 struct wl_resource *resource)
3941{
3942 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3943
3944 shsurf->state_requested = true;
3945 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003946 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003947}
3948
3949static void
3950xdg_surface_set_fullscreen(struct wl_client *client,
3951 struct wl_resource *resource,
3952 struct wl_resource *output_resource)
3953{
3954 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3955 struct weston_output *output;
3956
3957 shsurf->state_requested = true;
3958 shsurf->requested_state.fullscreen = true;
3959
3960 if (output_resource)
3961 output = wl_resource_get_user_data(output_resource);
3962 else
3963 output = NULL;
3964
Marek Chalupa052917a2014-09-02 11:35:12 +02003965 /* handle clients launching in fullscreen */
3966 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3967 /* Set the output to the one that has focus currently. */
3968 assert(shsurf->surface);
3969 output = get_focused_output(shsurf->surface->compositor);
3970 }
3971
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003972 shell_surface_set_output(shsurf, output);
3973 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003974
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003975 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003976}
3977
3978static void
3979xdg_surface_unset_fullscreen(struct wl_client *client,
3980 struct wl_resource *resource)
3981{
3982 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3983
3984 shsurf->state_requested = true;
3985 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003986 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003987}
3988
3989static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003990xdg_surface_set_minimized(struct wl_client *client,
3991 struct wl_resource *resource)
3992{
3993 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3994
3995 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3996 return;
3997
3998 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003999 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004000}
4001
Rafael Antognollie2a34552013-12-03 15:35:45 -02004002static const struct xdg_surface_interface xdg_surface_implementation = {
4003 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004004 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004005 xdg_surface_set_title,
4006 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04004007 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004008 xdg_surface_move,
4009 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004010 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004011 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004012 xdg_surface_set_maximized,
4013 xdg_surface_unset_maximized,
4014 xdg_surface_set_fullscreen,
4015 xdg_surface_unset_fullscreen,
4016 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004017};
4018
4019static void
4020xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004021 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004022{
4023 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004024 uint32_t *s;
4025 struct wl_array states;
4026 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004027
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004028 assert(shsurf);
4029
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004030 if (!shsurf->resource)
4031 return;
4032
4033 wl_array_init(&states);
4034 if (shsurf->requested_state.fullscreen) {
4035 s = wl_array_add(&states, sizeof *s);
4036 *s = XDG_SURFACE_STATE_FULLSCREEN;
4037 } else if (shsurf->requested_state.maximized) {
4038 s = wl_array_add(&states, sizeof *s);
4039 *s = XDG_SURFACE_STATE_MAXIMIZED;
4040 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004041 if (shsurf->resize_edges != 0) {
4042 s = wl_array_add(&states, sizeof *s);
4043 *s = XDG_SURFACE_STATE_RESIZING;
4044 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004045 if (shsurf->focus_count > 0) {
4046 s = wl_array_add(&states, sizeof *s);
4047 *s = XDG_SURFACE_STATE_ACTIVATED;
4048 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004049
4050 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4051 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4052
4053 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004054}
4055
4056static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004057 xdg_send_configure,
4058 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004059};
4060
4061static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004062xdg_shell_destroy(struct wl_client *client,
4063 struct wl_resource *resource)
4064{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004065 struct shell_client *sc = wl_resource_get_user_data(resource);
4066 struct wl_resource *shsurf_resource;
4067 struct shell_surface *shsurf;
4068
4069 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4070 shsurf = wl_resource_get_user_data(shsurf_resource);
4071 if (shsurf->owner_resource == resource) {
4072 wl_resource_post_error(
4073 resource,
4074 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4075 "not all child surface objects destroyed");
4076 return;
4077 }
4078 }
4079
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004080 wl_resource_destroy(resource);
4081}
4082
4083static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004084xdg_use_unstable_version(struct wl_client *client,
4085 struct wl_resource *resource,
4086 int32_t version)
4087{
4088 if (version > 1) {
4089 wl_resource_post_error(resource,
4090 1,
4091 "xdg-shell:: version not implemented yet.");
4092 return;
4093 }
4094}
4095
4096static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004097create_xdg_surface(struct shell_client *owner, void *shell,
4098 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004099 const struct weston_shell_client *client)
4100{
4101 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004102
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004103 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004104 if (!shsurf)
4105 return NULL;
4106
Pekka Paalanenb5026542014-11-12 15:09:24 +02004107 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004108
4109 return shsurf;
4110}
4111
4112static void
4113xdg_get_xdg_surface(struct wl_client *client,
4114 struct wl_resource *resource,
4115 uint32_t id,
4116 struct wl_resource *surface_resource)
4117{
4118 struct weston_surface *surface =
4119 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004120 struct shell_client *sc = wl_resource_get_user_data(resource);
4121 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004122 struct shell_surface *shsurf;
4123
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004124 shsurf = get_shell_surface(surface);
4125 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4126 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4127 "This wl_surface is already an "
4128 "xdg_surface");
4129 return;
4130 }
4131
Pekka Paalanen50b67472014-10-01 15:02:41 +03004132 if (weston_surface_set_role(surface, "xdg_surface",
4133 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004134 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004135
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004136 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004137 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004138 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004139 return;
4140 }
4141
4142 shsurf->resource =
4143 wl_resource_create(client,
4144 &xdg_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004145 if (!shsurf->resource) {
4146 wl_resource_post_no_memory(surface_resource);
4147 return;
4148 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004149 wl_resource_set_implementation(shsurf->resource,
4150 &xdg_surface_implementation,
4151 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004152 shsurf->owner_resource = resource;
4153 wl_list_insert(&sc->surface_list,
4154 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004155}
4156
4157static bool
4158shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4159{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004160 return shsurf->resource &&
4161 wl_resource_instance_of(shsurf->resource,
4162 &xdg_surface_interface,
4163 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004164}
4165
4166/* xdg-popup implementation */
4167
4168static void
4169xdg_popup_destroy(struct wl_client *client,
4170 struct wl_resource *resource)
4171{
4172 wl_resource_destroy(resource);
4173}
4174
Rafael Antognollie2a34552013-12-03 15:35:45 -02004175static const struct xdg_popup_interface xdg_popup_implementation = {
4176 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004177};
4178
4179static void
4180xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004181 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004182{
4183}
4184
4185static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004186 xdg_popup_send_configure,
4187 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004188};
4189
4190static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004191create_xdg_popup(struct shell_client *owner, void *shell,
4192 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004193 const struct weston_shell_client *client,
4194 struct weston_surface *parent,
4195 struct shell_seat *seat,
4196 uint32_t serial,
4197 int32_t x, int32_t y)
4198{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004199 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004200
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004201 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004202 if (!shsurf)
4203 return NULL;
4204
Pekka Paalanenb5026542014-11-12 15:09:24 +02004205 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004206 shsurf->popup.shseat = seat;
4207 shsurf->popup.serial = serial;
4208 shsurf->popup.x = x;
4209 shsurf->popup.y = y;
4210 shell_surface_set_parent(shsurf, parent);
4211
4212 return shsurf;
4213}
4214
4215static void
4216xdg_get_xdg_popup(struct wl_client *client,
4217 struct wl_resource *resource,
4218 uint32_t id,
4219 struct wl_resource *surface_resource,
4220 struct wl_resource *parent_resource,
4221 struct wl_resource *seat_resource,
4222 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004223 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004224{
4225 struct weston_surface *surface =
4226 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004227 struct shell_client *sc = wl_resource_get_user_data(resource);
4228 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004229 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004230 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004231 struct weston_surface *parent;
4232 struct shell_seat *seat;
4233
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004234 shsurf = get_shell_surface(surface);
4235 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4236 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4237 "This wl_surface is already an "
4238 "xdg_popup");
4239 return;
4240 }
4241
Pekka Paalanen50b67472014-10-01 15:02:41 +03004242 if (weston_surface_set_role(surface, "xdg_popup",
4243 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004244 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004245
4246 if (!parent_resource) {
4247 wl_resource_post_error(surface_resource,
4248 WL_DISPLAY_ERROR_INVALID_OBJECT,
4249 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004250 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004251 }
4252
4253 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004254 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004255
Jonas Ådahlee45a552015-03-18 16:37:47 +08004256 /* Verify that we are creating the top most popup when mapping,
4257 * as it's not until then we know whether it was mapped as most
4258 * top level or not. */
4259
4260 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004261 if (!parent_shsurf ||
4262 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4263 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004264 wl_resource_post_error(resource,
4265 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4266 "xdg_popup parent was invalid");
4267 return;
4268 }
4269
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004270 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004271 parent, seat, serial, x, y);
4272 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004273 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004274 return;
4275 }
4276
4277 shsurf->resource =
4278 wl_resource_create(client,
4279 &xdg_popup_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004280 if (!shsurf->resource) {
4281 wl_resource_post_no_memory(surface_resource);
4282 return;
4283 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004284 wl_resource_set_implementation(shsurf->resource,
4285 &xdg_popup_implementation,
4286 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004287 shsurf->owner_resource = resource;
4288 wl_list_insert(&sc->surface_list,
4289 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004290}
4291
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004292static void
4293xdg_pong(struct wl_client *client,
4294 struct wl_resource *resource, uint32_t serial)
4295{
4296 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004297
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004298 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004299}
4300
Rafael Antognollie2a34552013-12-03 15:35:45 -02004301static bool
4302shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4303{
Bryce Harrington22b1f932015-09-23 17:30:43 -07004304 return (shsurf->resource &&
4305 wl_resource_instance_of(shsurf->resource,
4306 &xdg_popup_interface,
4307 &xdg_popup_implementation));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004308}
4309
4310static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004311 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004312 xdg_use_unstable_version,
4313 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004314 xdg_get_xdg_popup,
4315 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004316};
4317
4318static int
4319xdg_shell_unversioned_dispatch(const void *implementation,
4320 void *_target, uint32_t opcode,
4321 const struct wl_message *message,
4322 union wl_argument *args)
4323{
4324 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004325 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004326
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004327 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004328 wl_resource_post_error(resource,
4329 WL_DISPLAY_ERROR_INVALID_OBJECT,
4330 "must call use_unstable_version first");
4331 return 0;
4332 }
4333
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004334#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004335
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004336 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4337 "shell implementation doesn't match protocol version");
4338
Rafael Antognollie2a34552013-12-03 15:35:45 -02004339 if (args[0].i != XDG_SERVER_VERSION) {
4340 wl_resource_post_error(resource,
4341 WL_DISPLAY_ERROR_INVALID_OBJECT,
4342 "incompatible version, server is %d "
4343 "client wants %d",
4344 XDG_SERVER_VERSION, args[0].i);
4345 return 0;
4346 }
4347
4348 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004349 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004350
4351 return 1;
4352}
4353
4354/* end of xdg-shell implementation */
4355/***********************************/
4356
Kristian Høgsberg07937562011-04-12 17:25:42 -04004357static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004358shell_fade(struct desktop_shell *shell, enum fade_type type);
4359
Pekka Paalanen77346a62011-11-30 16:26:35 +02004360static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004361configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004362{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004363 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004364
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004365 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004366 if (v->output == ev->output && v != ev) {
4367 weston_view_unmap(v);
4368 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004369 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004370 }
4371 }
4372
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004373 weston_view_set_position(ev, ev->output->x, ev->output->y);
Armin Krezović4663aca2016-06-30 06:04:29 +02004374 ev->surface->is_mapped = true;
4375 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004376
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004377 if (wl_list_empty(&ev->layer_link.link)) {
4378 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004379 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004380 }
4381}
4382
David Fort50d962f2016-06-09 17:55:52 +02004383
4384static struct shell_output *
4385find_shell_output_from_weston_output(struct desktop_shell *shell, struct weston_output *output)
4386{
4387 struct shell_output *shell_output;
4388
4389 wl_list_for_each(shell_output, &shell->output_list, link) {
4390 if (shell_output->output == output)
4391 return shell_output;
4392 }
4393
4394 return NULL;
4395}
4396
Pekka Paalanen8274d902014-08-06 19:36:51 +03004397static int
4398background_get_label(struct weston_surface *surface, char *buf, size_t len)
4399{
4400 return snprintf(buf, len, "background for output %s",
4401 surface->output->name);
4402}
4403
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004404static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004405background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004406{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004407 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004408 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004409
Jason Ekstranda7af7042013-10-12 22:38:11 -05004410 view = container_of(es->views.next, struct weston_view, surface_link);
4411
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004412 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004413}
4414
4415static void
David Fort50d962f2016-06-09 17:55:52 +02004416handle_background_surface_destroy(struct wl_listener *listener, void *data)
4417{
4418 struct shell_output *output =
4419 container_of(listener, struct shell_output, background_surface_listener);
4420
4421 weston_log("background surface gone\n");
4422 output->background_surface = NULL;
4423}
4424
4425static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004426desktop_shell_set_background(struct wl_client *client,
4427 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004428 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004429 struct wl_resource *surface_resource)
4430{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004431 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004432 struct weston_surface *surface =
4433 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02004434 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004435 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004436
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004437 if (surface->configure) {
4438 wl_resource_post_error(surface_resource,
4439 WL_DISPLAY_ERROR_INVALID_OBJECT,
4440 "surface role already assigned");
4441 return;
4442 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004443
Jason Ekstranda7af7042013-10-12 22:38:11 -05004444 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4445 weston_view_destroy(view);
4446 view = weston_view_create(surface);
4447
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004448 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004449 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004450 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004451 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004452 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004453 weston_desktop_shell_send_configure(resource, 0,
4454 surface_resource,
4455 surface->output->width,
4456 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004457
4458 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4459 sh_output->background_surface = surface;
4460
4461 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
4462 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004463}
4464
Pekka Paalanen8274d902014-08-06 19:36:51 +03004465static int
4466panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4467{
4468 return snprintf(buf, len, "panel for output %s",
4469 surface->output->name);
4470}
4471
Kristian Høgsberg75840622011-09-06 13:48:16 -04004472static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004473panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004474{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004475 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004476 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004477
Jason Ekstranda7af7042013-10-12 22:38:11 -05004478 view = container_of(es->views.next, struct weston_view, surface_link);
4479
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004480 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004481}
4482
4483static void
David Fort50d962f2016-06-09 17:55:52 +02004484handle_panel_surface_destroy(struct wl_listener *listener, void *data)
4485{
4486 struct shell_output *output =
4487 container_of(listener, struct shell_output, panel_surface_listener);
4488
4489 weston_log("panel surface gone\n");
4490 output->panel_surface = NULL;
4491}
4492
4493
4494static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004495desktop_shell_set_panel(struct wl_client *client,
4496 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004497 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004498 struct wl_resource *surface_resource)
4499{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004500 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004501 struct weston_surface *surface =
4502 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004503 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02004504 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004505
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004506 if (surface->configure) {
4507 wl_resource_post_error(surface_resource,
4508 WL_DISPLAY_ERROR_INVALID_OBJECT,
4509 "surface role already assigned");
4510 return;
4511 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004512
Jason Ekstranda7af7042013-10-12 22:38:11 -05004513 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4514 weston_view_destroy(view);
4515 view = weston_view_create(surface);
4516
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004517 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004518 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004519 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004520 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004521 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004522 weston_desktop_shell_send_configure(resource, 0,
4523 surface_resource,
4524 surface->output->width,
4525 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004526
4527 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4528 sh_output->panel_surface = surface;
4529
4530 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
4531 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004532}
4533
Pekka Paalanen8274d902014-08-06 19:36:51 +03004534static int
4535lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4536{
4537 return snprintf(buf, len, "lock window");
4538}
4539
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004540static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004541lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004542{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004543 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004544 struct weston_view *view;
4545
4546 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004547
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004548 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004549 return;
4550
Jason Ekstranda7af7042013-10-12 22:38:11 -05004551 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004552
4553 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004554 weston_layer_entry_insert(&shell->lock_layer.view_list,
4555 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004556 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02004557 surface->is_mapped = true;
4558 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004559 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004560 }
4561}
4562
4563static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004564handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004565{
Tiago Vignattibe143262012-04-16 17:31:41 +03004566 struct desktop_shell *shell =
4567 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004568
Martin Minarik6d118362012-06-07 18:01:59 +02004569 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004570 shell->lock_surface = NULL;
4571}
4572
4573static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004574desktop_shell_set_lock_surface(struct wl_client *client,
4575 struct wl_resource *resource,
4576 struct wl_resource *surface_resource)
4577{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004578 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004579 struct weston_surface *surface =
4580 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004581
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004582 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004583
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004584 if (!shell->locked)
4585 return;
4586
Pekka Paalanen98262232011-12-01 10:42:22 +02004587 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004588
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004589 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004590 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004591 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004592
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004593 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004594 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004595 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004596 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004597}
4598
4599static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004600resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004601{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004602 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004603
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004604 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004605 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004606 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004607 &shell->input_panel_layer.link);
4608 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004609 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004610 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004611 wl_list_insert(&shell->compositor->cursor_layer.link,
4612 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004613 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004614 wl_list_insert(&shell->fullscreen_layer.link,
4615 &shell->panel_layer.link);
4616 wl_list_insert(&shell->panel_layer.link,
4617 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004618
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004619 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004620
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004621 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004622 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004623 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004624}
4625
4626static void
4627desktop_shell_unlock(struct wl_client *client,
4628 struct wl_resource *resource)
4629{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004630 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004631
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004632 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004633
4634 if (shell->locked)
4635 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004636}
4637
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004638static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004639desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004640 struct wl_resource *resource,
4641 struct wl_resource *surface_resource)
4642{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004643 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004644
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004645 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004646 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004647}
4648
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004649static void
4650desktop_shell_desktop_ready(struct wl_client *client,
4651 struct wl_resource *resource)
4652{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004653 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004654
4655 shell_fade_startup(shell);
4656}
4657
Jonny Lamb765760d2014-08-20 15:53:19 +02004658static void
4659desktop_shell_set_panel_position(struct wl_client *client,
4660 struct wl_resource *resource,
4661 uint32_t position)
4662{
4663 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4664
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004665 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4666 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4667 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4668 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004669 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004670 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004671 "bad position argument");
4672 return;
4673 }
4674
4675 shell->panel_position = position;
4676}
4677
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004678static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004679 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004680 desktop_shell_set_panel,
4681 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004682 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004683 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004684 desktop_shell_desktop_ready,
4685 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004686};
4687
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004688static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004689get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004690{
4691 struct shell_surface *shsurf;
4692
4693 shsurf = get_shell_surface(surface);
4694 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004695 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004696 return shsurf->type;
4697}
4698
Kristian Høgsberg75840622011-09-06 13:48:16 -04004699static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004700move_binding(struct weston_pointer *pointer, uint32_t time,
4701 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004702{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004703 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004704 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004705 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004706
Derek Foreman8ae2db52015-07-15 13:00:36 -05004707 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004708 return;
4709
Derek Foreman8ae2db52015-07-15 13:00:36 -05004710 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004711
Pekka Paalanen01388e22013-04-25 13:57:44 +03004712 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004713 if (surface == NULL)
4714 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004715
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004716 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004717 if (shsurf == NULL || shsurf->state.fullscreen ||
4718 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004719 return;
4720
Derek Foreman794fa0e2015-07-15 13:00:38 -05004721 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004722}
4723
4724static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004725maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4726 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004727{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004728 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004729 struct weston_surface *surface;
4730 struct shell_surface *shsurf;
4731
4732 surface = weston_surface_get_main_surface(focus);
4733 if (surface == NULL)
4734 return;
4735
4736 shsurf = get_shell_surface(surface);
4737 if (shsurf == NULL)
4738 return;
4739
4740 if (!shell_surface_is_xdg_surface(shsurf))
4741 return;
4742
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004743 shsurf->state_requested = true;
4744 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004745 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004746}
4747
4748static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004749fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4750 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004751{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004752 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004753 struct weston_surface *surface;
4754 struct shell_surface *shsurf;
4755
4756 surface = weston_surface_get_main_surface(focus);
4757 if (surface == NULL)
4758 return;
4759
4760 shsurf = get_shell_surface(surface);
4761 if (shsurf == NULL)
4762 return;
4763
4764 if (!shell_surface_is_xdg_surface(shsurf))
4765 return;
4766
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004767 shsurf->state_requested = true;
4768 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004769 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004770 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004771}
4772
4773static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004774touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004775{
Derek Foreman362656b2014-09-04 10:23:05 -05004776 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004777 struct weston_surface *surface;
4778 struct shell_surface *shsurf;
4779
Derek Foreman8ae2db52015-07-15 13:00:36 -05004780 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004781 return;
4782
Derek Foreman8ae2db52015-07-15 13:00:36 -05004783 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004784 surface = weston_surface_get_main_surface(focus);
4785 if (surface == NULL)
4786 return;
4787
4788 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004789 if (shsurf == NULL || shsurf->state.fullscreen ||
4790 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004791 return;
4792
Derek Foremanb7674ae2015-07-15 13:00:37 -05004793 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004794}
4795
4796static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004797resize_binding(struct weston_pointer *pointer, uint32_t time,
4798 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004799{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004800 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004801 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004802 uint32_t edges = 0;
4803 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004804 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004805
Derek Foreman8ae2db52015-07-15 13:00:36 -05004806 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004807 return;
4808
Derek Foreman8ae2db52015-07-15 13:00:36 -05004809 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004810
Pekka Paalanen01388e22013-04-25 13:57:44 +03004811 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004812 if (surface == NULL)
4813 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004814
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004815 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004816 if (shsurf == NULL || shsurf->state.fullscreen ||
4817 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004818 return;
4819
Jason Ekstranda7af7042013-10-12 22:38:11 -05004820 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004821 wl_fixed_to_int(pointer->grab_x),
4822 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004823 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004824
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004825 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004826 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004827 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004828 edges |= 0;
4829 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004830 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004831
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004832 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004833 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004834 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004835 edges |= 0;
4836 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004837 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004838
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004839 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004840}
4841
4842static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004843surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004844 struct weston_pointer_axis_event *event,
4845 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004846{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004847 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004848 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004849 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004850 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004851
Pekka Paalanen01388e22013-04-25 13:57:44 +03004852 /* XXX: broken for windows containing sub-surfaces */
4853 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004854 if (surface == NULL)
4855 return;
4856
4857 shsurf = get_shell_surface(surface);
4858 if (!shsurf)
4859 return;
4860
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004861 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004862
Jason Ekstranda7af7042013-10-12 22:38:11 -05004863 if (shsurf->view->alpha > 1.0)
4864 shsurf->view->alpha = 1.0;
4865 if (shsurf->view->alpha < step)
4866 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004867
Jason Ekstranda7af7042013-10-12 22:38:11 -05004868 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004869 weston_surface_damage(surface);
4870}
4871
4872static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004873do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004874 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004875{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004876 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004877 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004878 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004879 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004880
Derek Foreman1281a362015-07-31 16:55:32 -05004881 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004882 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4883 return;
4884 }
4885
Scott Moreauccbf29d2012-02-22 14:21:41 -07004886 wl_list_for_each(output, &compositor->output_list, link) {
4887 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004888 wl_fixed_to_double(pointer->x),
4889 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004890 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004891 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004892 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004893 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004894 increment = -output->zoom.increment;
4895 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004896 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004897 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004898 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004899 else
4900 increment = 0;
4901
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004902 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004903
Scott Moreaue6603982012-06-11 13:07:51 -06004904 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004905 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004906 else if (output->zoom.level > output->zoom.max_level)
4907 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004908
4909 if (!output->zoom.active) {
4910 if (output->zoom.level <= 0.0)
4911 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004912 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004913 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004914
Scott Moreaue6603982012-06-11 13:07:51 -06004915 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004916
Jason Ekstranda7af7042013-10-12 22:38:11 -05004917 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004918 }
4919 }
4920}
4921
Scott Moreauccbf29d2012-02-22 14:21:41 -07004922static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004923zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004924 struct weston_pointer_axis_event *event,
4925 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004926{
Peter Hutterer89b6a492016-01-18 15:58:17 +10004927 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004928}
4929
4930static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004931zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4932 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004933{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004934 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004935}
4936
4937static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004938terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4939 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004940{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004941 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004942
Daniel Stone325fc2d2012-05-30 16:31:58 +01004943 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004944}
4945
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004946static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004947rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004948 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004949{
4950 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004951 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004952 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004953 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004954 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004955
Jonas Ådahld2510102014-10-05 21:39:14 +02004956 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004957
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004958 if (!shsurf)
4959 return;
4960
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004961 cx = 0.5f * shsurf->surface->width;
4962 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004963
Daniel Stone37816df2012-05-16 18:45:18 +01004964 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4965 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004966 r = sqrtf(dx * dx + dy * dy);
4967
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004968 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004969 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004970
4971 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004972 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004973 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004974
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004975 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004976 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004977
4978 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004979 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004980 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004981 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004982 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004983
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004984 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004985 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004986 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004987 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004988 wl_list_init(&shsurf->rotation.transform.link);
4989 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004990 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004991 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004992
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004993 /* We need to adjust the position of the surface
4994 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004995 cposx = shsurf->view->geometry.x + cx;
4996 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004997 dposx = rotate->center.x - cposx;
4998 dposy = rotate->center.y - cposy;
4999 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005000 weston_view_set_position(shsurf->view,
5001 shsurf->view->geometry.x + dposx,
5002 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005003 }
5004
Derek Foreman4b1a0a12014-09-10 15:37:33 -05005005 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02005006 * lazily applies the damage due to rotation update.
5007 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005008 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005009}
5010
5011static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005012rotate_grab_button(struct weston_pointer_grab *grab,
5013 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005014{
5015 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005016 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005017 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005018 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01005019 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005020
Daniel Stone4dbadb12012-05-30 16:31:51 +01005021 if (pointer->button_count == 0 &&
5022 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005023 if (shsurf)
5024 weston_matrix_multiply(&shsurf->rotation.rotation,
5025 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005026 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005027 free(rotate);
5028 }
5029}
5030
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005031static void
5032rotate_grab_cancel(struct weston_pointer_grab *grab)
5033{
5034 struct rotate_grab *rotate =
5035 container_of(grab, struct rotate_grab, base.grab);
5036
5037 shell_grab_end(&rotate->base);
5038 free(rotate);
5039}
5040
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005041static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02005042 noop_grab_focus,
5043 rotate_grab_motion,
5044 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02005045 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10005046 noop_grab_axis_source,
5047 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005048 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02005049};
5050
5051static void
Derek Foreman74de4692015-07-15 13:00:39 -05005052surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005053{
Pekka Paalanen460099f2012-01-20 16:48:25 +02005054 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005055 float dx, dy;
5056 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005057
Derek Foreman45a7c272015-09-11 14:27:40 -05005058 surface = find_toplevel_surface(surface);
5059
Pekka Paalanen460099f2012-01-20 16:48:25 +02005060 rotate = malloc(sizeof *rotate);
5061 if (!rotate)
5062 return;
5063
Jason Ekstranda7af7042013-10-12 22:38:11 -05005064 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005065 surface->surface->width * 0.5f,
5066 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005067 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005068
Derek Foreman74de4692015-07-15 13:00:39 -05005069 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5070 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005071 r = sqrtf(dx * dx + dy * dy);
5072 if (r > 20.0f) {
5073 struct weston_matrix inverse;
5074
5075 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005076 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005077 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005078
5079 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005080 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005081 } else {
5082 weston_matrix_init(&surface->rotation.rotation);
5083 weston_matrix_init(&rotate->rotation);
5084 }
5085
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005086 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005087 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005088}
5089
5090static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005091rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005092 void *data)
5093{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005094 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005095 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005096 struct shell_surface *surface;
5097
Derek Foreman8ae2db52015-07-15 13:00:36 -05005098 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005099 return;
5100
Derek Foreman8ae2db52015-07-15 13:00:36 -05005101 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005102
Pekka Paalanen01388e22013-04-25 13:57:44 +03005103 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005104 if (base_surface == NULL)
5105 return;
5106
5107 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005108 if (surface == NULL || surface->state.fullscreen ||
5109 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005110 return;
5111
Derek Foreman74de4692015-07-15 13:00:39 -05005112 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005113}
5114
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005115/* Move all fullscreen layers down to the current workspace and hide their
5116 * black views. The surfaces' state is set to both fullscreen and lowered,
5117 * and this is reversed when such a surface is re-configured, see
5118 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5119 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005120 * lowering_output = NULL - Lower on all outputs, else only lower on the
5121 * specified output.
5122 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005123 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005124 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005125void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005126lower_fullscreen_layer(struct desktop_shell *shell,
5127 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005128{
5129 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005130 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005131
5132 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005133 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005134 &shell->fullscreen_layer.view_list.link,
5135 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005136 struct shell_surface *shsurf = get_shell_surface(view->surface);
5137
5138 if (!shsurf)
5139 continue;
5140
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005141 /* Only lower surfaces which have lowering_output as their fullscreen
5142 * output, unless a NULL output asks for lowering on all outputs.
5143 */
5144 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5145 continue;
5146
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005147 /* We can have a non-fullscreen popup for a fullscreen surface
5148 * in the fullscreen layer. */
5149 if (shsurf->state.fullscreen) {
5150 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005151 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5152 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005153 weston_view_damage_below(shsurf->fullscreen.black_view);
5154
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005155 }
5156
5157 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005158 weston_layer_entry_remove(&view->layer_link);
5159 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005160 weston_view_damage_below(view);
5161 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005162
5163 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005164 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005165}
5166
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005167void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02005168activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005169 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005170{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02005171 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005172 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005173 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005174 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005175 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005176 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005177
Pekka Paalanen01388e22013-04-25 13:57:44 +03005178 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005179 shsurf = get_shell_surface(main_surface);
5180 assert(shsurf);
5181
5182 /* Only demote fullscreen surfaces on the output of activated shsurf.
5183 * Leave fullscreen surfaces on unrelated outputs alone. */
5184 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005185
Bryce Harrington260c2ff2016-06-29 19:04:06 -07005186 weston_seat_set_keyboard_focus(seat, es);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005187
Jonas Ådahl8538b222012-08-29 22:13:03 +02005188 state = ensure_focus_state(shell, seat);
5189 if (state == NULL)
5190 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005191
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005192 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005193 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005194
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005195 if (shsurf->state.fullscreen && flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005196 shell_configure_fullscreen(shsurf);
5197 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005198 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005199
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005200 /* Update the surface’s layer. This brings it to the top of the stacking
5201 * order as appropriate. */
5202 shell_surface_update_layer(shsurf);
5203
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005204 if (shell->focus_animation_type != ANIMATION_NONE) {
5205 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005206 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005207 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005208}
5209
Alex Wu21858432012-04-01 20:13:08 +08005210/* no-op func for checking black surface */
5211static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005212black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005213{
5214}
5215
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005216static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005217is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08005218{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005219 struct weston_surface *surface = view->surface;
5220
5221 if (surface->configure == black_surface_configure) {
5222 if (fs_view)
5223 *fs_view = surface->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005224 return true;
5225 }
5226 return false;
5227}
5228
Kristian Høgsberg75840622011-09-06 13:48:16 -04005229static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005230activate_binding(struct weston_seat *seat,
5231 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005232 struct weston_view *focus_view,
5233 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005234{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005235 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005236 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005237
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005238 if (!focus_view)
5239 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005240
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005241 if (is_black_surface_view(focus_view, &main_view))
5242 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08005243
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005244 main_surface = weston_surface_get_main_surface(focus_view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005245 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005246 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005247
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005248 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01005249}
5250
5251static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005252click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5253 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005254{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005255 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005256 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005257 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005258 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005259
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005260 activate_binding(pointer->seat, data, pointer->focus,
5261 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01005262}
5263
5264static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005265touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5266 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005267{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005268 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005269 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005270 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005271 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005272
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005273 activate_binding(touch->seat, data, touch->focus,
5274 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005275}
5276
5277static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005278unfocus_all_seats(struct desktop_shell *shell)
5279{
5280 struct weston_seat *seat, *next;
5281
5282 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005283 struct weston_keyboard *keyboard =
5284 weston_seat_get_keyboard(seat);
5285
5286 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005287 continue;
5288
Derek Foreman1281a362015-07-31 16:55:32 -05005289 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005290 }
5291}
5292
5293static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005294lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005295{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005296 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005297
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005298 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005299 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005300 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005301 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005302
5303 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005304
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005305 /* Hide all surfaces by removing the fullscreen, panel and
5306 * toplevel layers. This way nothing else can show or receive
5307 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005308
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005309 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005310 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005311 if (shell->showing_input_panels)
5312 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005313 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005314 wl_list_insert(&shell->compositor->cursor_layer.link,
5315 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005316
Derek Foreman004b4a12015-07-20 16:28:13 -05005317 weston_compositor_sleep(shell->compositor);
5318
Neil Robertsb4a91702014-04-09 16:33:32 +01005319 /* Remove the keyboard focus on all seats. This will be
5320 * restored to the workspace's saved state via
5321 * restore_focus_state when the compositor is unlocked */
5322 unfocus_all_seats(shell);
5323
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005324 /* TODO: disable bindings that should not work while locked. */
5325
5326 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005327}
5328
5329static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005330unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005331{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005332 struct wl_resource *shell_resource;
5333
Pekka Paalanend81c2162011-11-16 13:47:34 +02005334 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005335 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005336 return;
5337 }
5338
5339 /* If desktop-shell client has gone away, unlock immediately. */
5340 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005341 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005342 return;
5343 }
5344
5345 if (shell->prepare_event_sent)
5346 return;
5347
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005348 shell_resource = shell->child.desktop_shell;
5349 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005350 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005351}
5352
5353static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005354shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005355{
5356 struct desktop_shell *shell = data;
5357
5358 shell->fade.animation = NULL;
5359
5360 switch (shell->fade.type) {
5361 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005362 weston_surface_destroy(shell->fade.view->surface);
5363 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005364 break;
5365 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005366 lock(shell);
5367 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005368 default:
5369 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005370 }
5371}
5372
Jason Ekstranda7af7042013-10-12 22:38:11 -05005373static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005374shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005375{
5376 struct weston_compositor *compositor = shell->compositor;
5377 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005378 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005379
5380 surface = weston_surface_create(compositor);
5381 if (!surface)
5382 return NULL;
5383
Jason Ekstranda7af7042013-10-12 22:38:11 -05005384 view = weston_view_create(surface);
5385 if (!view) {
5386 weston_surface_destroy(surface);
5387 return NULL;
5388 }
5389
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005390 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005391 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005392 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005393 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5394 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005395 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02005396 surface->is_mapped = true;
5397 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005398
Jason Ekstranda7af7042013-10-12 22:38:11 -05005399 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005400}
5401
5402static void
5403shell_fade(struct desktop_shell *shell, enum fade_type type)
5404{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005405 float tint;
5406
5407 switch (type) {
5408 case FADE_IN:
5409 tint = 0.0;
5410 break;
5411 case FADE_OUT:
5412 tint = 1.0;
5413 break;
5414 default:
5415 weston_log("shell: invalid fade type\n");
5416 return;
5417 }
5418
5419 shell->fade.type = type;
5420
Jason Ekstranda7af7042013-10-12 22:38:11 -05005421 if (shell->fade.view == NULL) {
5422 shell->fade.view = shell_fade_create_surface(shell);
5423 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005424 return;
5425
Jason Ekstranda7af7042013-10-12 22:38:11 -05005426 shell->fade.view->alpha = 1.0 - tint;
5427 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005428 }
5429
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005430 if (shell->fade.view->output == NULL) {
5431 /* If the black view gets a NULL output, we lost the
5432 * last output and we'll just cancel the fade. This
5433 * happens when you close the last window under the
5434 * X11 or Wayland backends. */
5435 shell->locked = false;
5436 weston_surface_destroy(shell->fade.view->surface);
5437 shell->fade.view = NULL;
5438 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005439 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005440 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005441 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005442 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005443 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005444 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005445 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005446}
5447
5448static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005449do_shell_fade_startup(void *data)
5450{
5451 struct desktop_shell *shell = data;
5452
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005453 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005454 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005455 } else {
5456 weston_log("desktop shell: "
5457 "unexpected fade-in animation type %d\n",
5458 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005459 weston_surface_destroy(shell->fade.view->surface);
5460 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005461 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005462}
5463
5464static void
5465shell_fade_startup(struct desktop_shell *shell)
5466{
5467 struct wl_event_loop *loop;
5468
5469 if (!shell->fade.startup_timer)
5470 return;
5471
5472 wl_event_source_remove(shell->fade.startup_timer);
5473 shell->fade.startup_timer = NULL;
5474
5475 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5476 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5477}
5478
5479static int
5480fade_startup_timeout(void *data)
5481{
5482 struct desktop_shell *shell = data;
5483
5484 shell_fade_startup(shell);
5485 return 0;
5486}
5487
5488static void
5489shell_fade_init(struct desktop_shell *shell)
5490{
5491 /* Make compositor output all black, and wait for the desktop-shell
5492 * client to signal it is ready, then fade in. The timer triggers a
5493 * fade-in, in case the desktop-shell client takes too long.
5494 */
5495
5496 struct wl_event_loop *loop;
5497
Jason Ekstranda7af7042013-10-12 22:38:11 -05005498 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005499 weston_log("%s: warning: fade surface already exists\n",
5500 __func__);
5501 return;
5502 }
5503
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005504 if (shell->startup_animation_type == ANIMATION_NONE)
5505 return;
5506
Jason Ekstranda7af7042013-10-12 22:38:11 -05005507 shell->fade.view = shell_fade_create_surface(shell);
5508 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005509 return;
5510
Jason Ekstranda7af7042013-10-12 22:38:11 -05005511 weston_view_update_transform(shell->fade.view);
5512 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005513
5514 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5515 shell->fade.startup_timer =
5516 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5517 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5518}
5519
5520static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005521idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005522{
5523 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005524 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005525 struct weston_seat *seat;
5526
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005527 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005528 struct weston_touch *touch = weston_seat_get_touch(seat);
5529 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5530
5531 if (pointer)
5532 popup_grab_end(pointer);
5533 if (touch)
5534 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005535 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005536
5537 shell_fade(shell, FADE_OUT);
5538 /* lock() is called from shell_fade_done() */
5539}
5540
5541static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005542wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005543{
5544 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005545 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005546
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005547 unlock(shell);
5548}
5549
5550static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005551transform_handler(struct wl_listener *listener, void *data)
5552{
5553 struct weston_surface *surface = data;
5554 struct shell_surface *shsurf = get_shell_surface(surface);
5555 struct weston_view *view;;
5556 int x, y;
5557
5558 if (!shsurf || !shsurf->client->send_position)
5559 return;
5560
5561 view = shsurf->view;
5562 if (!view || !weston_view_is_mapped(view))
5563 return;
5564
5565 x = view->geometry.x;
5566 y = view->geometry.y;
5567
5568 shsurf->client->send_position(surface, x, y);
5569}
5570
5571static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005572center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005573{
Giulio Camuffob8366642013-04-25 13:57:46 +03005574 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005575 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005576
Jason Ekstranda7af7042013-10-12 22:38:11 -05005577 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005578
5579 x = output->x + (output->width - width) / 2 - surf_x / 2;
5580 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005581
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005582 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005583}
5584
5585static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005586weston_view_set_initial_position(struct weston_view *view,
5587 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005588{
5589 struct weston_compositor *compositor = shell->compositor;
5590 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005591 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005592 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005593 struct weston_output *output, *target_output = NULL;
5594 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005595 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005596
5597 /* As a heuristic place the new window on the same output as the
5598 * pointer. Falling back to the output containing 0, 0.
5599 *
5600 * TODO: Do something clever for touch too?
5601 */
5602 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005603 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5604
5605 if (pointer) {
5606 ix = wl_fixed_to_int(pointer->x);
5607 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005608 break;
5609 }
5610 }
5611
5612 wl_list_for_each(output, &compositor->output_list, link) {
5613 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5614 target_output = output;
5615 break;
5616 }
5617 }
5618
5619 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005620 weston_view_set_position(view, 10 + random() % 400,
5621 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005622 return;
5623 }
5624
5625 /* Valid range within output where the surface will still be onscreen.
5626 * If this is negative it means that the surface is bigger than
5627 * output.
5628 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005629 get_output_work_area(shell, target_output, &area);
5630
Derek Foremanf814c5d2015-04-15 12:23:54 -05005631 x = area.x;
5632 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005633 range_x = area.width - view->surface->width;
5634 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005635
Rob Bradford4cb88c72012-08-13 15:18:44 +01005636 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005637 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005638
5639 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005640 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005641
Jason Ekstranda7af7042013-10-12 22:38:11 -05005642 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005643}
5644
5645static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005646set_maximized_position(struct desktop_shell *shell,
5647 struct shell_surface *shsurf)
5648{
5649 int32_t surf_x, surf_y;
5650 pixman_rectangle32_t area;
5651
Jonny Lambd73c6942014-08-20 15:53:20 +02005652 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005653 if (shsurf->has_set_geometry) {
5654 surf_x = shsurf->geometry.x;
5655 surf_y = shsurf->geometry.y;
5656 } else {
5657 surface_subsurfaces_boundingbox(shsurf->surface,
5658 &surf_x, &surf_y, NULL, NULL);
5659 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005660
5661 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005662 area.x - surf_x,
5663 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005664}
5665
5666static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005667map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005668 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005669{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005670 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005671 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005672
Pekka Paalanen77346a62011-11-30 16:26:35 +02005673 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005674 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005675 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005676 if (shsurf->state.fullscreen) {
5677 center_on_output(shsurf->view, shsurf->fullscreen_output);
5678 shell_map_fullscreen(shsurf);
5679 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005680 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005681 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005682 weston_view_set_initial_position(shsurf->view, shell);
5683 }
Juan Zhao96879df2012-02-07 08:45:41 +08005684 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005685 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005686 if (shell_map_popup(shsurf) != 0)
5687 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005688 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005689 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005690 weston_view_set_position(shsurf->view,
5691 shsurf->view->geometry.x + sx,
5692 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005693 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005694 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005695 default:
5696 ;
5697 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005698
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005699 /* Surface stacking order, see also activate(). */
5700 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005701
Jason Ekstranda7af7042013-10-12 22:38:11 -05005702 if (shsurf->type != SHELL_SURFACE_NONE) {
5703 weston_view_update_transform(shsurf->view);
Armin Krezović4663aca2016-06-30 06:04:29 +02005704 shsurf->view->is_mapped = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005705 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005706 shsurf->surface->output = shsurf->output;
5707 shsurf->view->output = shsurf->output;
5708 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005709 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005710
Jason Ekstranda7af7042013-10-12 22:38:11 -05005711 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005712 /* XXX: xwayland's using the same fields for transient type */
5713 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005714 if (shsurf->transient.flags ==
5715 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5716 break;
5717 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005718 if (shsurf->state.relative &&
5719 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5720 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005721 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005722 break;
5723 wl_list_for_each(seat, &compositor->seat_list, link)
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005724 activate(shell, shsurf->view, seat,
5725 WESTON_ACTIVATE_FLAG_CONFIGURE);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005726 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005727 case SHELL_SURFACE_POPUP:
5728 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005729 default:
5730 break;
5731 }
5732
Rafael Antognolli03b16592013-12-03 15:35:42 -02005733 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5734 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005735 {
5736 switch (shell->win_animation_type) {
5737 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005738 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005739 break;
5740 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005741 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005742 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005743 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005744 default:
5745 break;
5746 }
5747 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005748}
5749
5750static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005751configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005752 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005753{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005754 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005755 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005756
Pekka Paalanen77346a62011-11-30 16:26:35 +02005757 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005758
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005759 assert(shsurf);
5760
Rafael Antognolli03b16592013-12-03 15:35:42 -02005761 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005762 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005763 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005764 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005765 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005766 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005767 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005768
Alex Wu4539b082012-03-01 12:57:46 +08005769 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005770 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005771 wl_list_for_each(view, &surface->views, surface_link)
5772 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005773
Rafael Antognolli03b16592013-12-03 15:35:42 -02005774 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005775 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005776 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005777}
5778
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005779static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005780shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005781{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005782 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005783 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005784 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005785
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005786 assert(shsurf);
5787
5788 shell = shsurf->shell;
5789
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005790 if (!weston_surface_is_mapped(es) &&
5791 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005792 remove_popup_grab(shsurf);
5793 }
5794
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005795 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005796 return;
5797
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005798 if (shsurf->has_next_geometry) {
5799 shsurf->geometry = shsurf->next_geometry;
5800 shsurf->has_next_geometry = false;
5801 shsurf->has_set_geometry = true;
5802 } else if (!shsurf->has_set_geometry) {
5803 surface_subsurfaces_boundingbox(shsurf->surface,
5804 &shsurf->geometry.x,
5805 &shsurf->geometry.y,
5806 &shsurf->geometry.width,
5807 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005808 }
5809
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005810 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005811 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005812 type_changed = 1;
5813 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005814
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005815 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005816 map(shell, shsurf, sx, sy);
Armin Krezović4663aca2016-06-30 06:04:29 +02005817 es->is_mapped = true;
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005818 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005819 shsurf->last_width != es->width ||
5820 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005821 float from_x, from_y;
5822 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005823
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005824 if (shsurf->resize_edges) {
5825 sx = 0;
5826 sy = 0;
5827 }
5828
5829 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5830 sx = shsurf->last_width - es->width;
5831 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5832 sy = shsurf->last_height - es->height;
5833
5834 shsurf->last_width = es->width;
5835 shsurf->last_height = es->height;
5836
Jason Ekstranda7af7042013-10-12 22:38:11 -05005837 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5838 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005839 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005840 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005841 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005842 }
5843}
5844
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005845static bool
5846check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5847{
5848 struct timespec now;
5849
5850 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5851 return false;
5852
5853 /*
5854 * If the shell helper client dies before the session has been
5855 * up for roughly 30 seconds, better just make Weston shut down,
5856 * because the user likely has no way to interact with the desktop
5857 * anyway.
5858 */
5859 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5860 weston_log("Error: %s apparently cannot run at all.\n",
5861 shell->client);
5862 weston_log_continue(STAMP_SPACE "Quitting...");
5863 wl_display_terminate(shell->compositor->wl_display);
5864
5865 return true;
5866 }
5867
5868 return false;
5869}
5870
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005871static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005872
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005873static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005874respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005875{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005876 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005877
5878 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5879 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005880 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005881 shell->child.deathstamp = time;
5882 shell->child.deathcount = 0;
5883 }
5884
5885 shell->child.deathcount++;
5886 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005887 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005888 return;
5889 }
5890
Pekka Paalanen826dc142014-08-27 12:11:53 +03005891 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005892 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005893}
5894
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005895static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005896desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5897{
5898 struct desktop_shell *shell;
5899
5900 shell = container_of(listener, struct desktop_shell,
5901 child.client_destroy_listener);
5902
Pekka Paalanen826dc142014-08-27 12:11:53 +03005903 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005904 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005905 /*
5906 * unbind_desktop_shell() will reset shell->child.desktop_shell
5907 * before the respawned process has a chance to create a new
5908 * desktop_shell object, because we are being called from the
5909 * wl_client destructor which destroys all wl_resources before
5910 * returning.
5911 */
5912
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005913 if (!check_desktop_shell_crash_too_early(shell))
5914 respawn_desktop_shell_process(shell);
5915
Pekka Paalanen826dc142014-08-27 12:11:53 +03005916 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005917}
5918
5919static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005920launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005921{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005922 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005923
Pekka Paalanen826dc142014-08-27 12:11:53 +03005924 shell->child.client = weston_client_start(shell->compositor,
5925 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005926
Arnaud Vrac42631192014-08-25 20:56:46 +02005927 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005928 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005929 return;
5930 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005931
5932 shell->child.client_destroy_listener.notify =
5933 desktop_shell_client_destroy;
5934 wl_client_add_destroy_listener(shell->child.client,
5935 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005936}
5937
5938static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005939handle_shell_client_destroy(struct wl_listener *listener, void *data)
5940{
5941 struct shell_client *sc =
5942 container_of(listener, struct shell_client, destroy_listener);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005943 struct wl_resource *shsurf_resource;
5944 struct shell_surface *shsurf;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005945
5946 if (sc->ping_timer)
5947 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005948
5949 /* Since we're about to free shell_client, we remove it from the
5950 * head of the surface list so we don't use that freed list node
5951 * during surface clean up later on.
5952 */
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005953 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
5954 shsurf = wl_resource_get_user_data(shsurf_resource);
5955 shsurf->owner = NULL;
5956 }
Derek Foremane42d7542015-05-29 10:46:44 -05005957 wl_list_remove(&sc->surface_list);
5958
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005959 free(sc);
5960}
5961
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005962static struct shell_client *
5963shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5964 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005965{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005966 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005967
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005968 sc = zalloc(sizeof *sc);
5969 if (sc == NULL) {
5970 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005971 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005972 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005973
5974 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005975 if (sc->resource == NULL) {
5976 free(sc);
5977 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005978 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005979 }
5980
5981 sc->client = client;
5982 sc->shell = shell;
5983 sc->destroy_listener.notify = handle_shell_client_destroy;
5984 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005985 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005986
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005987 return sc;
5988}
5989
5990static void
5991bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5992{
5993 struct desktop_shell *shell = data;
5994 struct shell_client *sc;
5995
5996 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5997 if (sc)
5998 wl_resource_set_implementation(sc->resource,
5999 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05006000 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08006001}
6002
6003static void
6004bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
6005{
6006 struct desktop_shell *shell = data;
6007 struct shell_client *sc;
6008
6009 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
6010 if (sc)
6011 wl_resource_set_dispatcher(sc->resource,
6012 xdg_shell_unversioned_dispatch,
6013 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02006014}
6015
6016static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006017unbind_desktop_shell(struct wl_resource *resource)
6018{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05006019 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05006020
6021 if (shell->locked)
6022 resume_desktop(shell);
6023
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006024 shell->child.desktop_shell = NULL;
6025 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006026}
6027
6028static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04006029bind_desktop_shell(struct wl_client *client,
6030 void *data, uint32_t version, uint32_t id)
6031{
Tiago Vignattibe143262012-04-16 17:31:41 +03006032 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006033 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04006034
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006035 resource = wl_resource_create(client, &weston_desktop_shell_interface,
6036 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006037
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006038 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05006039 wl_resource_set_implementation(resource,
6040 &desktop_shell_implementation,
6041 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006042 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006043 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006044 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006045
6046 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
6047 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04006048}
6049
Kristian Høgsberg07045392012-02-19 18:52:44 -05006050struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03006051 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006052 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006053 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006054 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006055 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006056};
6057
6058static void
6059switcher_next(struct switcher *switcher)
6060{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006061 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006062 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006063 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006064 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006065
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006066 /* temporary re-display minimized surfaces */
6067 struct weston_view *tmp;
6068 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006069 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6070 weston_layer_entry_remove(&view->layer_link);
6071 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006072 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6073 *minimized = view;
6074 }
6075
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006076 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006077 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006078 if (shsurf &&
6079 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6080 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006081 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006082 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006083 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006084 next = view;
6085 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006086 view->alpha = 0.25;
6087 weston_view_geometry_dirty(view);
6088 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006089 }
Alex Wu1659daa2012-04-01 20:13:09 +08006090
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02006091 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05006092 view->alpha = 0.25;
6093 weston_view_geometry_dirty(view);
6094 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006095 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006096 }
6097
6098 if (next == NULL)
6099 next = first;
6100
Alex Wu07b26062012-03-12 16:06:01 +08006101 if (next == NULL)
6102 return;
6103
Kristian Høgsberg07045392012-02-19 18:52:44 -05006104 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006105 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006106
6107 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006108 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006109 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006110
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006111 shsurf = get_shell_surface(switcher->current->surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006112 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006113 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006114}
6115
6116static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006117switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006118{
6119 struct switcher *switcher =
6120 container_of(listener, struct switcher, listener);
6121
6122 switcher_next(switcher);
6123}
6124
6125static void
Daniel Stone351eb612012-05-31 15:27:47 -04006126switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006127{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006128 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006129 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006130 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006131
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006132 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006133 if (is_focus_view(view))
6134 continue;
6135
Jason Ekstranda7af7042013-10-12 22:38:11 -05006136 view->alpha = 1.0;
6137 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006138 }
6139
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02006140 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02006141 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02006142 keyboard->seat,
6143 WESTON_ACTIVATE_FLAG_CONFIGURE);
6144 }
6145
Kristian Høgsberg07045392012-02-19 18:52:44 -05006146 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006147 weston_keyboard_end_grab(keyboard);
6148 if (keyboard->input_method_resource)
6149 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006150
6151 /* re-hide surfaces that were temporary shown during the switch */
6152 struct weston_view **minimized;
6153 wl_array_for_each(minimized, &switcher->minimized_array) {
6154 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006155 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006156 weston_layer_entry_remove(&(*minimized)->layer_link);
6157 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006158 weston_view_damage_below(*minimized);
6159 }
6160 }
6161 wl_array_release(&switcher->minimized_array);
6162
Kristian Høgsberg07045392012-02-19 18:52:44 -05006163 free(switcher);
6164}
6165
6166static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006167switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006168 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006169{
6170 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006171 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006172
Daniel Stonec9785ea2012-05-30 16:31:52 +01006173 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006174 switcher_next(switcher);
6175}
6176
6177static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006178switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006179 uint32_t mods_depressed, uint32_t mods_latched,
6180 uint32_t mods_locked, uint32_t group)
6181{
6182 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006183 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006184
Daniel Stone351eb612012-05-31 15:27:47 -04006185 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6186 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006187}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006188
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006189static void
6190switcher_cancel(struct weston_keyboard_grab *grab)
6191{
6192 struct switcher *switcher = container_of(grab, struct switcher, grab);
6193
6194 switcher_destroy(switcher);
6195}
6196
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006197static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006198 switcher_key,
6199 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006200 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006201};
6202
6203static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006204switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6205 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006206{
Tiago Vignattibe143262012-04-16 17:31:41 +03006207 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006208 struct switcher *switcher;
6209
6210 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006211 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006212 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006213 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006214 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006215 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006216
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006217 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006218 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006219 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006220 weston_keyboard_start_grab(keyboard, &switcher->grab);
6221 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006222 switcher_next(switcher);
6223}
6224
Pekka Paalanen3c647232011-12-22 13:43:43 +02006225static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006226backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6227 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006228{
6229 struct weston_compositor *compositor = data;
6230 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006231 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006232
6233 /* TODO: we're limiting to simple use cases, where we assume just
6234 * control on the primary display. We'd have to extend later if we
6235 * ever get support for setting backlights on random desktop LCD
6236 * panels though */
6237 output = get_default_output(compositor);
6238 if (!output)
6239 return;
6240
6241 if (!output->set_backlight)
6242 return;
6243
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006244 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6245 backlight_new = output->backlight_current - 25;
6246 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6247 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006248
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006249 if (backlight_new < 5)
6250 backlight_new = 5;
6251 if (backlight_new > 255)
6252 backlight_new = 255;
6253
6254 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006255 output->set_backlight(output, output->backlight_current);
6256}
6257
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006258static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006259force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6260 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006261{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006262 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006263 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006264 struct desktop_shell *shell = data;
6265 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006266 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006267
Derek Foreman8ae2db52015-07-15 13:00:36 -05006268 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006269 if (!focus_surface)
6270 return;
6271
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006272 wl_signal_emit(&compositor->kill_signal, focus_surface);
6273
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006274 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006275 wl_client_get_credentials(client, &pid, NULL, NULL);
6276
6277 /* Skip clients that we launched ourselves (the credentials of
6278 * the socketpair is ours) */
6279 if (pid == getpid())
6280 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006281
Daniel Stone325fc2d2012-05-30 16:31:58 +01006282 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006283}
6284
6285static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006286workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006287 uint32_t key, void *data)
6288{
6289 struct desktop_shell *shell = data;
6290 unsigned int new_index = shell->workspaces.current;
6291
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006292 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006293 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006294 if (new_index != 0)
6295 new_index--;
6296
6297 change_workspace(shell, new_index);
6298}
6299
6300static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006301workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006302 uint32_t key, void *data)
6303{
6304 struct desktop_shell *shell = data;
6305 unsigned int new_index = shell->workspaces.current;
6306
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006307 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006308 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006309 if (new_index < shell->workspaces.num - 1)
6310 new_index++;
6311
6312 change_workspace(shell, new_index);
6313}
6314
6315static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006316workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006317 uint32_t key, void *data)
6318{
6319 struct desktop_shell *shell = data;
6320 unsigned int new_index;
6321
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006322 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006323 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006324 new_index = key - KEY_F1;
6325 if (new_index >= shell->workspaces.num)
6326 new_index = shell->workspaces.num - 1;
6327
6328 change_workspace(shell, new_index);
6329}
6330
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006331static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006332workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6333 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006334{
6335 struct desktop_shell *shell = data;
6336 unsigned int new_index = shell->workspaces.current;
6337
6338 if (shell->locked)
6339 return;
6340
6341 if (new_index != 0)
6342 new_index--;
6343
Derek Foreman8ae2db52015-07-15 13:00:36 -05006344 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006345}
6346
6347static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006348workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6349 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006350{
6351 struct desktop_shell *shell = data;
6352 unsigned int new_index = shell->workspaces.current;
6353
6354 if (shell->locked)
6355 return;
6356
6357 if (new_index < shell->workspaces.num - 1)
6358 new_index++;
6359
Derek Foreman8ae2db52015-07-15 13:00:36 -05006360 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006361}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006362
6363static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006364shell_reposition_view_on_output_destroy(struct weston_view *view)
6365{
6366 struct weston_output *output, *first_output;
6367 struct weston_compositor *ec = view->surface->compositor;
6368 struct shell_surface *shsurf;
6369 float x, y;
6370 int visible;
6371
6372 x = view->geometry.x;
6373 y = view->geometry.y;
6374
6375 /* At this point the destroyed output is not in the list anymore.
6376 * If the view is still visible somewhere, we leave where it is,
6377 * otherwise, move it to the first output. */
6378 visible = 0;
6379 wl_list_for_each(output, &ec->output_list, link) {
6380 if (pixman_region32_contains_point(&output->region,
6381 x, y, NULL)) {
6382 visible = 1;
6383 break;
6384 }
6385 }
6386
6387 if (!visible) {
6388 first_output = container_of(ec->output_list.next,
6389 struct weston_output, link);
6390
6391 x = first_output->x + first_output->width / 4;
6392 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006393
6394 weston_view_set_position(view, x, y);
6395 } else {
6396 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006397 }
6398
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006399
6400 shsurf = get_shell_surface(view->surface);
6401
6402 if (shsurf) {
6403 shsurf->saved_position_valid = false;
6404 shsurf->next_state.maximized = false;
6405 shsurf->next_state.fullscreen = false;
6406 shsurf->state_changed = true;
6407 }
6408}
6409
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006410void
6411shell_for_each_layer(struct desktop_shell *shell,
6412 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006413{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006414 struct workspace **ws;
6415
6416 func(shell, &shell->fullscreen_layer, data);
6417 func(shell, &shell->panel_layer, data);
6418 func(shell, &shell->background_layer, data);
6419 func(shell, &shell->lock_layer, data);
6420 func(shell, &shell->input_panel_layer, data);
6421
6422 wl_array_for_each(ws, &shell->workspaces.array)
6423 func(shell, &(*ws)->layer, data);
6424}
6425
6426static void
6427shell_output_destroy_move_layer(struct desktop_shell *shell,
6428 struct weston_layer *layer,
6429 void *data)
6430{
6431 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006432 struct weston_view *view;
6433
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006434 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006435 if (view->output != output)
6436 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006437
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006438 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006439 }
6440}
6441
6442static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006443handle_output_destroy(struct wl_listener *listener, void *data)
6444{
6445 struct shell_output *output_listener =
6446 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006447 struct weston_output *output = output_listener->output;
6448 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006449
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006450 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006451
Xiong Zhang6b481422013-10-23 13:58:32 +08006452 wl_list_remove(&output_listener->destroy_listener.link);
6453 wl_list_remove(&output_listener->link);
6454 free(output_listener);
6455}
6456
6457static void
David Fort50d962f2016-06-09 17:55:52 +02006458shell_resize_surface_to_output(struct desktop_shell *shell,
6459 struct weston_surface *surface,
6460 const struct weston_output *output)
6461{
6462 if (!surface)
6463 return;
6464
6465 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
6466 surface->resource,
6467 output->width,
6468 output->height);
6469}
6470
6471
6472static void
6473handle_output_resized(struct wl_listener *listener, void *data)
6474{
6475 struct desktop_shell *shell =
6476 container_of(listener, struct desktop_shell, resized_listener);
6477 struct weston_output *output = (struct weston_output *)data;
6478 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
6479
6480 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
6481 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
6482}
6483
6484static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006485create_shell_output(struct desktop_shell *shell,
6486 struct weston_output *output)
6487{
6488 struct shell_output *shell_output;
6489
6490 shell_output = zalloc(sizeof *shell_output);
6491 if (shell_output == NULL)
6492 return;
6493
6494 shell_output->output = output;
6495 shell_output->shell = shell;
6496 shell_output->destroy_listener.notify = handle_output_destroy;
6497 wl_signal_add(&output->destroy_signal,
6498 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006499 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006500}
6501
6502static void
6503handle_output_create(struct wl_listener *listener, void *data)
6504{
6505 struct desktop_shell *shell =
6506 container_of(listener, struct desktop_shell, output_create_listener);
6507 struct weston_output *output = (struct weston_output *)data;
6508
6509 create_shell_output(shell, output);
6510}
6511
6512static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006513handle_output_move_layer(struct desktop_shell *shell,
6514 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006515{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006516 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006517 struct weston_view *view;
6518 float x, y;
6519
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006520 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006521 if (view->output != output)
6522 continue;
6523
6524 x = view->geometry.x + output->move_x;
6525 y = view->geometry.y + output->move_y;
6526 weston_view_set_position(view, x, y);
6527 }
6528}
6529
6530static void
6531handle_output_move(struct wl_listener *listener, void *data)
6532{
6533 struct desktop_shell *shell;
6534
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006535 shell = container_of(listener, struct desktop_shell,
6536 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006537
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006538 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006539}
6540
6541static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006542setup_output_destroy_handler(struct weston_compositor *ec,
6543 struct desktop_shell *shell)
6544{
6545 struct weston_output *output;
6546
6547 wl_list_init(&shell->output_list);
6548 wl_list_for_each(output, &ec->output_list, link)
6549 create_shell_output(shell, output);
6550
6551 shell->output_create_listener.notify = handle_output_create;
6552 wl_signal_add(&ec->output_created_signal,
6553 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006554
6555 shell->output_move_listener.notify = handle_output_move;
6556 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006557}
6558
6559static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006560shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006561{
Tiago Vignattibe143262012-04-16 17:31:41 +03006562 struct desktop_shell *shell =
6563 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006564 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006565 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006566
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006567 /* Force state to unlocked so we don't try to fade */
6568 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006569
6570 if (shell->child.client) {
6571 /* disable respawn */
6572 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006573 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006574 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006575
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006576 wl_list_remove(&shell->idle_listener.link);
6577 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006578 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006579
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006580 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006581 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006582
Xiong Zhang6b481422013-10-23 13:58:32 +08006583 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6584 wl_list_remove(&shell_output->destroy_listener.link);
6585 wl_list_remove(&shell_output->link);
6586 free(shell_output);
6587 }
6588
6589 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006590 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02006591 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006592
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006593 wl_array_for_each(ws, &shell->workspaces.array)
6594 workspace_destroy(*ws);
6595 wl_array_release(&shell->workspaces.array);
6596
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006597 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006598 free(shell);
6599}
6600
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006601static void
6602shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6603{
6604 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006605 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006606
Bob Ham744e6532016-01-12 10:21:48 +00006607 if (shell->allow_zap)
6608 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6609 MODIFIER_CTRL | MODIFIER_ALT,
6610 terminate_binding, ec);
6611
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006612 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006613 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6614 click_to_activate_binding,
6615 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006616 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6617 click_to_activate_binding,
6618 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006619 weston_compositor_add_touch_binding(ec, 0,
6620 touch_to_activate_binding,
6621 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006622 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6623 MODIFIER_SUPER | MODIFIER_ALT,
6624 surface_opacity_binding, NULL);
6625 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6626 MODIFIER_SUPER, zoom_axis_binding,
6627 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006628 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6629 backlight_binding, ec);
6630 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6631 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006632
6633 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006634 if (shell->exposay_modifier)
6635 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6636 exposay_binding, shell);
6637
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006638 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006639 if (!mod)
6640 return;
6641
Daniel Stone325fc2d2012-05-30 16:31:58 +01006642 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6643 zoom_key_binding, NULL);
6644 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6645 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006646 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6647 maximize_binding, NULL);
6648 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6649 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006650 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6651 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006652 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006653 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006654 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006655 weston_compositor_add_button_binding(ec, BTN_LEFT,
6656 mod | MODIFIER_SHIFT,
6657 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006658
6659 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006660 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006661 rotate_binding, NULL);
6662
Daniel Stone325fc2d2012-05-30 16:31:58 +01006663 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6664 shell);
6665 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6666 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006667 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6668 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006669 weston_compositor_add_key_binding(ec, KEY_K, mod,
6670 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006671 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6672 workspace_up_binding, shell);
6673 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6674 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006675 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6676 workspace_move_surface_up_binding,
6677 shell);
6678 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6679 workspace_move_surface_down_binding,
6680 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006681
6682 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6683 if (shell->workspaces.num > 1) {
6684 num_workspace_bindings = shell->workspaces.num;
6685 if (num_workspace_bindings > 6)
6686 num_workspace_bindings = 6;
6687 for (i = 0; i < num_workspace_bindings; i++)
6688 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6689 workspace_f_binding,
6690 shell);
6691 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006692
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006693 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006694}
6695
Jason Ekstrand024177c2014-04-21 19:42:58 -05006696static void
6697handle_seat_created(struct wl_listener *listener, void *data)
6698{
6699 struct weston_seat *seat = data;
6700
6701 create_shell_seat(seat);
6702}
6703
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006704WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006705module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006706 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006707{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006708 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006709 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006710 struct workspace **pws;
6711 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006712 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006713
Peter Huttererf3d62272013-08-08 11:57:05 +10006714 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006715 if (shell == NULL)
6716 return -1;
6717
Kristian Høgsberg75840622011-09-06 13:48:16 -04006718 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006719
6720 shell->destroy_listener.notify = shell_destroy;
6721 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006722 shell->idle_listener.notify = idle_handler;
6723 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6724 shell->wake_listener.notify = wake_handler;
6725 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006726 shell->transform_listener.notify = transform_handler;
6727 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006728
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006729 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006730 ec->shell_interface.create_shell_surface = create_shell_surface;
6731 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006732 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006733 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006734 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006735 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006736 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006737 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006738 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006739 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006740 ec->shell_interface.set_pid = set_pid;
Quentin Glidic5c201952016-03-23 13:50:49 +01006741 ec->shell_interface.get_output_work_area = get_output_work_area;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006742
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006743 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6744 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006745 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6746 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006747 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006748
6749 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006750 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006751
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006752 if (input_panel_setup(shell) < 0)
6753 return -1;
6754
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006755 shell->text_backend = text_backend_init(ec);
6756 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006757 return -1;
6758
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006759 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006760
Daniel Stonedf8133b2013-11-19 11:37:14 +01006761 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6762 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6763
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006764 for (i = 0; i < shell->workspaces.num; i++) {
6765 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6766 if (pws == NULL)
6767 return -1;
6768
6769 *pws = workspace_create();
6770 if (*pws == NULL)
6771 return -1;
6772 }
6773 activate_workspace(shell, 0);
6774
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006775 weston_layer_init(&shell->minimized_layer, NULL);
6776
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006777 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006778 wl_list_init(&shell->workspaces.animation.link);
6779 shell->workspaces.animation.frame = animate_workspace_change_frame;
6780
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006781 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006782 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006783 return -1;
6784
Rafael Antognollie2a34552013-12-03 15:35:45 -02006785 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6786 shell, bind_xdg_shell) == NULL)
6787 return -1;
6788
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006789 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006790 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006791 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006792 return -1;
6793
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006794 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006795
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006796 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006797
Xiong Zhang6b481422013-10-23 13:58:32 +08006798 setup_output_destroy_handler(ec, shell);
6799
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006800 loop = wl_display_get_event_loop(ec->wl_display);
6801 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006802
Jason Ekstrand024177c2014-04-21 19:42:58 -05006803 wl_list_for_each(seat, &ec->seat_list, link)
6804 handle_seat_created(NULL, seat);
6805 shell->seat_create_listener.notify = handle_seat_created;
6806 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006807
David Fort50d962f2016-06-09 17:55:52 +02006808 shell->resized_listener.notify = handle_output_resized;
6809 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
6810
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006811 screenshooter_create(ec);
6812
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006813 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006814
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006815 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006816
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006817 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6818
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006819 return 0;
6820}